|
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->executeUpdate('UPDATE ' . $this->getClassMetadata()->getTableName() . ' SET sorting = FLOOR(1 + RAND() * ?)', [$count]); |
|
|
|
|
|
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param Organization|User $hasSubscriptionLastReview |
|
46
|
|
|
*/ |
|
47
|
3 |
|
public function getSubscriptionLastReviewNumber(AbstractModel $hasSubscriptionLastReview): ?int |
|
48
|
|
|
{ |
|
49
|
3 |
|
$class = get_class($hasSubscriptionLastReview); |
|
50
|
3 |
|
$table = $this->getEntityManager()->getClassMetadata($class)->getTableName(); |
|
51
|
|
|
|
|
52
|
3 |
|
$connection = $this->getEntityManager()->getConnection(); |
|
53
|
|
|
|
|
54
|
3 |
|
$sql = "SELECT review_number FROM product INNER JOIN $table ON $table.subscription_last_review_id = product.id AND $table.id = " . $hasSubscriptionLastReview->getId(); |
|
55
|
3 |
|
$result = $connection->fetchOne($sql); |
|
56
|
|
|
|
|
57
|
3 |
|
if (is_numeric($result)) { |
|
58
|
2 |
|
$result = (int) $result; |
|
59
|
|
|
} else { |
|
60
|
2 |
|
$result = null; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
3 |
|
return $result; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.