PluginProbe
User Access Manager / 2.3.17
User Access Manager v2.3.17
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 2.3.17, at src/UserGroup/AssignedObjectsLoader.php

77 lines 2.5 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 if (isset($this->assignedObjects[$objectType][$ignoreDates]) === false) {
32 $this->assignedObjects[$objectType][$ignoreDates] = $this->loadAssignedObjects($objectType, $ignoreDates);
33 }
34
35 return $this->assignedObjects[$objectType][$ignoreDates][$userGroupType][$userGroupId] ?? [];
36 }
37
38 public function flush(): void
39 {
40 $this->assignedObjects = [];
41 }
42
43 /**
44 * @return array<string, array<string, AssignmentInformation[]>>
45 */
46 private function loadAssignedObjects(string $objectType, bool $ignoreDates): array
47 {
48 $query = "SELECT group_id AS groupId, group_type AS groupType, object_id AS id,
49 object_type AS objectType, from_date AS fromDate, to_date AS toDate
50 FROM {$this->database->getUserGroupToObjectTable()}
51 WHERE object_id != ''
52 AND (general_object_type = '%s' OR object_type = '%s')";
53
54 $parameters = [
55 $objectType,
56 $objectType
57 ];
58
59 if ($ignoreDates === false) {
60 $query .= " AND (from_date IS NULL OR from_date <= '%s') AND (to_date IS NULL OR to_date >= '%s')";
61 $time = $this->wordpress->currentTime('mysql');
62 $parameters = array_merge($parameters, [$time, $time]);
63 }
64
65 $query = $this->database->prepare($query, $parameters);
66 $results = (array) $this->database->getResults($query);
67 $assignedObjects = [];
68
69 foreach ($results as $result) {
70 $assignedObjects[$result->groupType][$result->groupId][$result->id] = $this->assignmentInformationFactory
71 ->createAssignmentInformation($result->objectType, $result->fromDate, $result->toDate);
72 }
73
74 return $assignedObjects;
75 }
76 }
77