feat(search): sort for subscribers

This commit is contained in:
mrilyew 2024-12-22 15:07:26 +03:00
parent 0aecc299cf
commit c6f3d767fc
6 changed files with 73 additions and 2 deletions

View file

@ -62,9 +62,25 @@ class Clubs
$order_str = 'id'; $order_str = 'id';
switch($order['type']) { switch($order['type']) {
default:
case 'id': case 'id':
$order_str = 'id ' . ($order['invert'] ? 'ASC' : 'DESC'); $order_str = 'id ' . ($order['invert'] ? 'ASC' : 'DESC');
break; break;
case 'subs':
$sql_query = <<<EOF
SELECT `groups`.`id`, `subs`
FROM `groups`
LEFT JOIN
(SELECT `target`, COUNT(`follower`) as `subs`
FROM `subscriptions`
WHERE `model` = "openvk\\\\Web\\\\Models\\\\Entities\\\\Club"
GROUP BY `target`) `subscriptions` ON `subscriptions`.`target` = `groups`.`id`
WHERE `groups`.`name` LIKE ? OR `groups`.`about` LIKE ?
ORDER BY `subs` DESC
LIMIT ? OFFSET ?;
EOF;
return new Util\RawEntityStream("Club", $sql_query, $query, $query);
} }
$result = $result->where("name LIKE ? OR about LIKE ?", $query, $query); $result = $result->where("name LIKE ? OR about LIKE ?", $query, $query);

View file

@ -27,7 +27,7 @@ class EntityStream implements \IteratorAggregate
return new $this->entityClass($result); return new $this->entityClass($result);
} }
private function stream(\Traversable $iterator): \Traversable protected function stream(\Traversable $iterator): \Traversable
{ {
foreach($iterator as $result) foreach($iterator as $result)
yield $this->getEntity($result); yield $this->getEntity($result);

View file

@ -0,0 +1,50 @@
<?php declare(strict_types=1);
namespace openvk\Web\Models\Repositories\Util;
use Nette\Database\Row;
class RawEntityStream extends EntityStream
{
function __construct(string $repo, string $sql, ...$db_params)
{
$this->sqlQuery = $sql;
$this->entityRepo = new ($class[0] === "\\" ? $repo : "openvk\\Web\\Models\\Repositories\\$repo"."s");
$this->dbParams = $db_params;
}
private function dbs(int $page = 0, ?int $perPage = NULL): \Traversable
{
if(!$this->dbQuery) {
$this->dbParams[] = $perPage;
$this->dbParams[] = (($page - 1) * $perPage);
$this->dbQuery = \Chandler\Database\DatabaseConnection::i()->getConnection()->query($this->sqlQuery, ...$this->dbParams);
}
return $this->dbQuery;
}
private function getEntity(Row $result)
{
$repo = new $this->entityRepo;
return $repo->get($result->id);
}
protected function stream(\Traversable $iterator): \Traversable
{
foreach($iterator as $result)
yield $this->getEntity($result);
}
function page(int $page, ?int $perPage = NULL): \Traversable
{
$fetchedRows = $this->dbs($page, $perPage);
return $this->stream($fetchedRows);
}
function size(): int
{
bdump($this->dbs()->getRowCount());
return $this->dbs()->getRowCount();
}
}

View file

@ -330,6 +330,9 @@
{if OPENVK_ROOT_CONF["openvk"]["preferences"]["commerce"]} {if OPENVK_ROOT_CONF["openvk"]["preferences"]["commerce"]}
<option value="rating" n:attr="selected => $order == 'rating'">{_s_order_by_rating}</option> <option value="rating" n:attr="selected => $order == 'rating'">{_s_order_by_rating}</option>
{/if} {/if}
{elseif $section == "groups"}
<option value="id" n:attr="selected => $order == 'id'">{_s_order_by_creation_date}</option>
<option value="subs" n:attr="selected => $order == 'subs'">{_s_order_by_subs}</option>
{elseif $section == "posts"} {elseif $section == "posts"}
<option value="id" n:attr="selected => $order == 'id'">{_s_order_by_publishing_date}</option> <option value="id" n:attr="selected => $order == 'id'">{_s_order_by_publishing_date}</option>
{elseif $section == "audios"} {elseif $section == "audios"}
@ -344,7 +347,7 @@
{/if} {/if}
</select> </select>
<label n:if="$order != 'rating'"> <label n:if="$order != 'rating' && $order != 'subs'">
<input type="checkbox" name="invert" value="1" form="search_form" n:attr="checked => $_REQUEST['invert'] == '1'"> <input type="checkbox" name="invert" value="1" form="search_form" n:attr="checked => $_REQUEST['invert'] == '1'">
{_s_order_invert} {_s_order_invert}
</label> </label>

View file

@ -2136,6 +2136,7 @@
"s_order_by_creation_date" = "By creation date"; "s_order_by_creation_date" = "By creation date";
"s_order_by_publishing_date" = "By publication date"; "s_order_by_publishing_date" = "By publication date";
"s_order_by_upload_date" = "By upload date"; "s_order_by_upload_date" = "By upload date";
"s_order_by_subs" = "By followers";
"s_by_date" = "By date"; "s_by_date" = "By date";
"s_registered_before" = "Registered before"; "s_registered_before" = "Registered before";

View file

@ -2031,6 +2031,7 @@
"s_order_by_creation_date" = "По дате создания"; "s_order_by_creation_date" = "По дате создания";
"s_order_by_publishing_date" = "По дате публикации"; "s_order_by_publishing_date" = "По дате публикации";
"s_order_by_upload_date" = "По дате загрузки"; "s_order_by_upload_date" = "По дате загрузки";
"s_order_by_subs" = "По подписчикам";
"s_by_date" = "По дате"; "s_by_date" = "По дате";
"s_registered_before" = "Зарегистрирован до"; "s_registered_before" = "Зарегистрирован до";