openvk/Web/Models/Repositories/BugtrackerProducts.php

135 lines
3.9 KiB
PHP
Raw Normal View History

2022-08-20 20:30:04 +03:00
<?php declare(strict_types=1);
namespace openvk\Web\Models\Repositories;
2022-08-23 02:40:11 +03:00
use openvk\Web\Models\Entities\{BugtrackerProduct, BugtrackerPrivateProduct, User};
use openvk\Web\Models\Repositories\{BugtrackerPrivateProducts};
2022-08-20 20:30:04 +03:00
use Nette\Database\Table\ActiveRow;
use Chandler\Database\DatabaseConnection;
class BugtrackerProducts
{
private $context;
private $products;
2022-08-23 02:40:11 +03:00
private $private_products;
2022-08-20 20:30:04 +03:00
function __construct()
{
$this->context = DatabaseConnection::i()->getContext();
2022-08-23 02:40:11 +03:00
$this->products = $this->context->table("bt_products");
2022-08-20 20:30:04 +03:00
}
private function toProduct(?ActiveRow $ar)
{
return is_null($ar) ? NULL : new BugtrackerProduct($ar);
}
function get(int $id): ?BugtrackerProduct
{
return $this->toProduct($this->products->get($id));
}
2022-08-22 02:11:48 +03:00
function getAll(int $page = 1): \Traversable
2022-08-20 20:30:04 +03:00
{
2022-08-23 02:40:11 +03:00
$products = $this->products
->where(["private" => 0])
->order("created DESC")
->page($page, 5);
foreach($products as $product)
yield new BugtrackerProduct($product);
}
function getOpen(int $page = 1, bool $private = FALSE): \Traversable
{
$products = $this->products
->where([
"closed" => 0,
"private" => $private
])
->order("id ASC")
->page($page, 5);
foreach($products as $product)
2022-08-20 20:30:04 +03:00
yield new BugtrackerProduct($product);
}
2022-08-23 02:40:11 +03:00
function getClosed(int $page = 1, bool $private = FALSE): \Traversable
2022-08-20 20:30:04 +03:00
{
2022-08-23 02:40:11 +03:00
$products = $this->products
->where([
"closed" => 1,
"private" => $private
])
->order("id ASC")
->page($page, 5);
foreach($products as $product)
yield new BugtrackerProduct($product);
}
function getPrivate(int $page = 1): \Traversable
{
$products = $this->products
->where([
"private" => 1
])
->order("id ASC")
->page($page, 5);
foreach($products as $product)
2022-08-20 20:30:04 +03:00
yield new BugtrackerProduct($product);
}
2022-08-22 02:11:48 +03:00
2022-08-23 02:40:11 +03:00
function getPrivateForUser(User $user, int $page = 1): \Traversable
2022-08-22 02:11:48 +03:00
{
2022-08-23 02:40:11 +03:00
if (!$user->isBtModerator()) {
return (new BugtrackerPrivateProducts)->getForUser($user, $page);
} else {
return (new BugtrackerPrivateProducts)->getAll($page);
}
}
function getFiltered(User $tester, string $type = "all", int $page = 1): \Traversable
{
switch ($type) {
case 'open':
return $this->getOpen($page);
break;
case 'closed':
return $this->getClosed($page);
break;
case 'private':
if ($tester->isBtModerator())
return $this->getPrivate($page);
return (new BugtrackerPrivateProducts)->getForUser($tester, $page);
break;
default:
return $this->getAll($page);
break;
}
}
function getCount(string $filter = "all", User $user = NULL): ?int
{
switch ($filter) {
case 'open':
return sizeof($this->products->where(["closed" => 0, "private" => 0]));
break;
case 'closed':
return sizeof($this->products->where(["closed" => 1, "private" => 0]));
case 'private':
return (new BugtrackerPrivateProducts)->getCount($user);
break;
default:
return sizeof($this->products->where(["private" => 0]));
break;
}
2022-08-22 02:11:48 +03:00
}
2022-08-20 20:30:04 +03:00
}