|
13 | 13 | use Doctrine\DBAL\Query\QueryBuilder; |
14 | 14 | use Ibexa\Contracts\Core\Persistence\Notification\CreateStruct; |
15 | 15 | use Ibexa\Contracts\Core\Persistence\Notification\Notification; |
| 16 | +use Ibexa\Contracts\Core\Persistence\Notification\UpdateStruct; |
16 | 17 | use Ibexa\Contracts\Core\Repository\Values\Notification\Query\CriterionInterface; |
17 | 18 | use Ibexa\Contracts\Core\Repository\Values\Notification\Query\NotificationQuery; |
18 | 19 | use Ibexa\Core\Base\Exceptions\InvalidArgumentException; |
@@ -82,6 +83,78 @@ public function getNotificationById(int $notificationId): array |
82 | 83 | return $query->execute()->fetchAllAssociative(); |
83 | 84 | } |
84 | 85 |
|
| 86 | + /** |
| 87 | + * @param int[] $notificationIds |
| 88 | + * |
| 89 | + * @return int[] |
| 90 | + */ |
| 91 | + public function bulkUpdateUserNotifications( |
| 92 | + int $ownerId, |
| 93 | + UpdateStruct $updateStruct, |
| 94 | + bool $pendingOnly = false, |
| 95 | + array $notificationIds = [] |
| 96 | + ): array { |
| 97 | + $queryBuilder = $this->connection->createQueryBuilder(); |
| 98 | + $this->applyNotificationFilters($queryBuilder, $ownerId, $pendingOnly, $notificationIds); |
| 99 | + |
| 100 | + $idsToUpdate = $queryBuilder->execute()->fetchFirstColumn(); |
| 101 | + if (empty($idsToUpdate)) { |
| 102 | + return []; |
| 103 | + } |
| 104 | + |
| 105 | + if ($updateStruct->isPending !== null) { |
| 106 | + $this->updateNotificationsPendingStatus($idsToUpdate, $updateStruct->isPending); |
| 107 | + } |
| 108 | + |
| 109 | + return array_map('intval', $idsToUpdate); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * @param int[] $notificationIds |
| 114 | + */ |
| 115 | + private function applyNotificationFilters( |
| 116 | + QueryBuilder $queryBuilder, |
| 117 | + int $ownerId, |
| 118 | + bool $pendingOnly, |
| 119 | + array $notificationIds |
| 120 | + ): void { |
| 121 | + $queryBuilder |
| 122 | + ->select(self::COLUMN_ID) |
| 123 | + ->from(self::TABLE_NOTIFICATION) |
| 124 | + ->andWhere($queryBuilder->expr()->eq(self::COLUMN_OWNER_ID, ':ownerId')) |
| 125 | + ->setParameter(':ownerId', $ownerId, ParameterType::INTEGER); |
| 126 | + |
| 127 | + if ($pendingOnly) { |
| 128 | + $queryBuilder->andWhere($queryBuilder->expr()->eq(self::COLUMN_IS_PENDING, ':isPendingFlag')) |
| 129 | + ->setParameter(':isPendingFlag', true, ParameterType::BOOLEAN); |
| 130 | + } |
| 131 | + |
| 132 | + if (!empty($notificationIds)) { |
| 133 | + $queryBuilder->andWhere( |
| 134 | + $queryBuilder->expr()->in(self::COLUMN_ID, ':notificationIds') |
| 135 | + ); |
| 136 | + $queryBuilder->setParameter(':notificationIds', $notificationIds, Connection::PARAM_INT_ARRAY); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * @param int[] $idsToUpdate |
| 142 | + */ |
| 143 | + private function updateNotificationsPendingStatus(array $idsToUpdate, bool $isPending): void |
| 144 | + { |
| 145 | + $updateQuery = $this->connection->createQueryBuilder(); |
| 146 | + $updateQuery |
| 147 | + ->update(self::TABLE_NOTIFICATION) |
| 148 | + ->set(self::COLUMN_IS_PENDING, ':is_pending') |
| 149 | + ->andWhere( |
| 150 | + $updateQuery->expr()->in(self::COLUMN_ID, ':ids') |
| 151 | + ) |
| 152 | + ->setParameter(':is_pending', $isPending, ParameterType::BOOLEAN) |
| 153 | + ->setParameter(':ids', $idsToUpdate, Connection::PARAM_INT_ARRAY); |
| 154 | + |
| 155 | + $updateQuery->execute(); |
| 156 | + } |
| 157 | + |
85 | 158 | public function updateNotification(Notification $notification): void |
86 | 159 | { |
87 | 160 | if (!isset($notification->id) || !is_numeric($notification->id)) { |
@@ -113,7 +186,7 @@ public function countUserNotifications(int $userId, ?NotificationQuery $query = |
113 | 186 | $this->applyFilters($queryBuilder, $query->getCriteria()); |
114 | 187 | } |
115 | 188 |
|
116 | | - return (int)$queryBuilder->execute()->fetchColumn(); |
| 189 | + return (int)$queryBuilder->execute()->fetchOne(); |
117 | 190 | } |
118 | 191 |
|
119 | 192 | public function countUserPendingNotifications(int $userId): int |
|
0 commit comments