1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Application\Repository; |
6
|
|
|
|
7
|
|
|
use Application\Model\AbstractModel; |
8
|
|
|
use Application\Model\Organization; |
9
|
|
|
use Application\Model\User; |
10
|
|
|
use Ecodev\Felix\Repository\LimitedAccessSubQuery; |
11
|
|
|
|
12
|
|
|
class ProductRepository extends AbstractRepository implements LimitedAccessSubQuery |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Returns pure SQL to get ID of all objects that are accessible to given user. |
16
|
|
|
* |
17
|
|
|
* A user can access a file if at least one of the following condition is true: |
18
|
|
|
* |
19
|
|
|
* - product is active |
20
|
|
|
* - he is facilitator or administrator |
21
|
|
|
* |
22
|
|
|
* @param null|User $user |
23
|
|
|
*/ |
24
|
11 |
|
public function getAccessibleSubQuery(?\Ecodev\Felix\Model\User $user): string |
25
|
|
|
{ |
26
|
11 |
|
if ($user && in_array($user->getRole(), [User::ROLE_FACILITATOR, User::ROLE_ADMINISTRATOR], true)) { |
27
|
4 |
|
return $this->getAllIdsQuery(); |
28
|
|
|
} |
29
|
|
|
|
30
|
7 |
|
return 'SELECT id FROM product WHERE product.is_active'; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
// Set random sorting on all products |
34
|
|
|
public function randomizeSorting(): void |
35
|
|
|
{ |
36
|
|
|
$count = $this->getCount(); |
37
|
|
|
|
38
|
|
|
if ($count) { |
39
|
|
|
$connection = $this->getEntityManager()->getConnection(); |
40
|
|
|
$connection->executeStatement('UPDATE ' . $this->getClassMetadata()->getTableName() . ' SET sorting = FLOOR(1 + RAND() * ?)', [$count]); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
3 |
|
public function getSubscriptionLastReviewNumber(Organization|User $hasSubscriptionLastReview): ?int |
45
|
|
|
{ |
46
|
3 |
|
$class = $hasSubscriptionLastReview::class; |
47
|
3 |
|
$table = $this->getEntityManager()->getClassMetadata($class)->getTableName(); |
48
|
|
|
|
49
|
3 |
|
$connection = $this->getEntityManager()->getConnection(); |
50
|
|
|
|
51
|
3 |
|
$sql = "SELECT review_number FROM product INNER JOIN $table ON $table.subscription_last_review_id = product.id AND $table.id = " . $hasSubscriptionLastReview->getId(); |
52
|
3 |
|
$result = $connection->fetchOne($sql); |
53
|
|
|
|
54
|
3 |
|
if (is_numeric($result)) { |
55
|
2 |
|
$result = (int) $result; |
56
|
|
|
} else { |
57
|
2 |
|
$result = null; |
58
|
|
|
} |
59
|
|
|
|
60
|
3 |
|
return $result; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|