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