2025-01-31 18:20:13 +03:00
< ? php
declare ( strict_types = 1 );
2021-12-15 01:27:17 +03:00
namespace openvk\Web\Models\Repositories ;
2025-01-31 18:20:13 +03:00
2021-12-15 01:27:17 +03:00
use openvk\Web\Models\Entities\Topic ;
use openvk\Web\Models\Entities\Club ;
use Nette\Database\Table\ActiveRow ;
use Chandler\Database\DatabaseConnection ;
class Topics
{
private $context ;
private $topics ;
2025-01-31 18:20:13 +03:00
public function __construct ()
2021-12-15 01:27:17 +03:00
{
$this -> context = DatabaseConnection :: i () -> getContext ();
$this -> topics = $this -> context -> table ( " topics " );
}
private function toTopic ( ? ActiveRow $ar ) : ? Topic
{
2025-01-31 18:20:13 +03:00
return is_null ( $ar ) ? null : new Topic ( $ar );
2021-12-15 01:27:17 +03:00
}
2025-01-31 18:20:13 +03:00
public function get ( int $id ) : ? Topic
2021-12-15 01:27:17 +03:00
{
return $this -> toTopic ( $this -> topics -> get ( $id ));
}
2025-01-31 18:20:13 +03:00
public function getTopicById ( int $club , int $topic ) : ? Topic
2021-12-15 01:27:17 +03:00
{
return $this -> toTopic ( $this -> topics -> where ([ " group " => $club , " virtual_id " => $topic , " deleted " => 0 ]) -> fetch ());
}
2025-01-31 18:20:13 +03:00
public function getClubTopics ( Club $club , int $page = 1 , ? int $perPage = null ) : \Traversable
2021-12-15 01:27:17 +03:00
{
2025-01-31 18:20:13 +03:00
$perPage ? ? = OPENVK_DEFAULT_PER_PAGE ;
2021-12-15 01:27:17 +03:00
2022-05-08 13:06:26 +03:00
# Get pinned topics first
2021-12-15 01:27:17 +03:00
$query = " SELECT `id` FROM `topics` WHERE `pinned` = 1 AND `group` = ? AND `deleted` = 0 UNION SELECT `id` FROM `topics` WHERE `pinned` = 0 AND `group` = ? AND `deleted` = 0 " ;
$query .= " LIMIT " . $perPage . " OFFSET " . ( $page - 1 ) * $perPage ;
2025-01-31 18:20:13 +03:00
foreach ( DatabaseConnection :: i () -> getConnection () -> query ( $query , $club -> getId (), $club -> getId ()) as $topic ) {
2021-12-15 01:27:17 +03:00
$topic = $this -> get ( $topic -> id );
2025-01-31 18:20:13 +03:00
if ( ! $topic ) {
continue ;
}
2021-12-15 01:27:17 +03:00
yield $topic ;
}
}
2025-01-31 18:20:13 +03:00
public function getClubTopicsCount ( Club $club ) : int
2021-12-15 01:27:17 +03:00
{
return sizeof ( $this -> topics -> where ([
" group " => $club -> getId (),
2025-01-31 18:20:13 +03:00
" deleted " => false ,
2021-12-15 01:27:17 +03:00
]));
}
2025-01-31 18:20:13 +03:00
public function find ( Club $club , string $query ) : \Traversable
2021-12-15 01:27:17 +03:00
{
return new Util\EntityStream ( " Topic " , $this -> topics -> where ( " title LIKE ? AND group = ? AND deleted = 0 " , " % $query % " , $club -> getId ()));
}
2025-01-31 18:20:13 +03:00
public function getLastTopics ( Club $club , ? int $count = null ) : \Traversable
2021-12-15 01:27:17 +03:00
{
$topics = $this -> topics -> where ([
" group " => $club -> getId (),
2025-01-31 18:20:13 +03:00
" deleted " => false ,
2021-12-15 01:27:17 +03:00
]) -> page ( 1 , $count ? ? OPENVK_DEFAULT_PER_PAGE ) -> order ( " created DESC " );
2025-01-31 18:20:13 +03:00
foreach ( $topics as $topic ) {
2021-12-15 01:27:17 +03:00
yield $this -> toTopic ( $topic );
2025-01-31 18:20:13 +03:00
}
2021-12-15 01:27:17 +03:00
}
}