2020-06-07 19:04:43 +03:00
< ? php declare ( strict_types = 1 );
namespace openvk\Web\Models\Repositories ;
2023-07-28 00:47:03 +03:00
use openvk\Web\Models\Entities\Club ;
2020-06-07 19:04:43 +03:00
use Nette\Database\Table\ActiveRow ;
use Chandler\Database\DatabaseConnection ;
class Clubs
{
private $context ;
private $clubs ;
function __construct ()
{
2023-07-28 00:47:03 +03:00
$this -> context = DatabaseConnection :: i () -> getContext ();
$this -> clubs = $this -> context -> table ( " groups " );
2020-06-07 19:04:43 +03:00
}
private function toClub ( ? ActiveRow $ar ) : ? Club
{
return is_null ( $ar ) ? NULL : new Club ( $ar );
}
function getByShortURL ( string $url ) : ? Club
{
2023-07-28 00:47:03 +03:00
return $this -> toClub ( $this -> clubs -> where ( " shortcode " , $url ) -> fetch ());
2020-06-07 19:04:43 +03:00
}
function get ( int $id ) : ? Club
{
return $this -> toClub ( $this -> clubs -> get ( $id ));
}
2023-07-28 00:47:03 +03:00
function find ( string $query , int $page = 1 , ? int $perPage = NULL ) : \Traversable
2020-06-07 19:04:43 +03:00
{
2020-11-22 13:29:27 +03:00
$query = " % $query % " ;
$result = $this -> clubs -> where ( " name LIKE ? OR about LIKE ? " , $query , $query );
2023-07-28 00:47:03 +03:00
return new Util\EntityStream ( " Club " , $result );
2020-06-07 19:04:43 +03:00
}
2022-01-03 18:31:55 +03:00
function getCount () : int
{
return sizeof ( clone $this -> clubs );
}
function getPopularClubs () : \Traversable
{
2022-04-18 09:54:56 +03:00
$query = " SELECT ROW_NUMBER() OVER (ORDER BY `subscriptions` DESC) as `place`, `target` as `id`, COUNT(`follower`) as `subscriptions` FROM `subscriptions` WHERE `model` = \" openvk \\ \ Web \\ \ Models \\ \ Entities \\ \ Club \" GROUP BY `target` ORDER BY `subscriptions` DESC, `id` LIMIT 30; " ;
2022-01-03 18:31:55 +03:00
$entries = DatabaseConnection :: i () -> getConnection () -> query ( $query );
foreach ( $entries as $entry )
yield ( object ) [
" place " => $entry [ " place " ],
" club " => $this -> get ( $entry [ " id " ]),
" subscriptions " => $entry [ " subscriptions " ],
];
2023-07-28 00:44:05 +03:00
}
2023-07-28 00:47:03 +03:00
2020-06-07 19:04:43 +03:00
use \Nette\SmartObject ;
}