PluginProbe
User Access Manager / trunk
User Access Manager vtrunk
2.3.20 2.3.19 2.3.18 2.3.17 2.3.16 2.3.15 2.3.14 2.3.13 trunk 0.6 0.6.1 0.6.2 0.7 0.7 Beta 0.7.0.1 0.8 0.8.0.1 0.8.0.2 0.9 0.9.1 0.9.1.1 0.9.1.2 0.9.1.3 0.9.1.4 1.0 All 136 releases
user-access-manager / src / UserGroup / AssignedObjectsLoader.php

AssignedObjectsLoader.php in User Access Manager trunk, at src/UserGroup/AssignedObjectsLoader.php

73 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace UserAccessManager\UserGroup;
6
7 use UserAccessManager\Database\Database;
8 use UserAccessManager\Wrapper\Wordpress;
9
10 class AssignedObjectsLoader
11 {
12 /** @var array<string, array<int, array<string, array<string, AssignmentInformation[]>>>> */
13 private array $assignedObjects = [];
14
15 public function __construct(
16 private Wordpress $wordpress,
17 private Database $database,
18 private AssignmentInformationFactory $assignmentInformationFactory
19 ) {
20 }
21
22 /**
23 * @return AssignmentInformation[] The assigned objects of the user group, keyed by the object id.
24 */
25 public function getAssignedObjects(
26 ?string $userGroupType,
27 int|string|null $userGroupId,
28 string $objectType,
29 bool $ignoreDates
30 ): array {
31 $this->assignedObjects[$objectType][$ignoreDates] ??= $this->loadAssignedObjects($objectType, $ignoreDates);
32
33 return $this->assignedObjects[$objectType][$ignoreDates][$userGroupType][$userGroupId] ?? [];
34 }
35
36 public function flush(): void
37 {
38 $this->assignedObjects = [];
39 }
40
41 /**
42 * @return array<string, array<string, AssignmentInformation[]>>
43 */
44 private function loadAssignedObjects(string $objectType, bool $ignoreDates): array
45 {
46 $query = "SELECT `group_id` AS `groupId`, `group_type` AS `groupType`, `object_id` AS `id`,
47 `object_type` AS `objectType`, `from_date` AS `fromDate`, `to_date` AS `toDate`
48 FROM `{$this->database->getUserGroupToObjectTable()}`
49 WHERE `object_id` != ''
50 AND (`general_object_type` = '%s' OR `object_type` = '%s')";
51
52 $parameters = [
53 $objectType,
54 $objectType
55 ];
56
57 if ($ignoreDates === false) {
58 $query .= " AND (`from_date` IS NULL OR `from_date` <= '%s') AND (`to_date` IS NULL OR `to_date` >= '%s')";
59 $time = $this->wordpress->currentTime('mysql');
60 $parameters = array_merge($parameters, [$time, $time]);
61 }
62
63 $assignedObjects = [];
64
65 foreach ((array) $this->database->getResults($this->database->prepare($query, $parameters)) as $result) {
66 $assignedObjects[$result->groupType][$result->groupId][$result->id] = $this->assignmentInformationFactory
67 ->createAssignmentInformation($result->objectType, $result->fromDate, $result->toDate);
68 }
69
70 return $assignedObjects;
71 }
72 }
73