Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
81.94% |
295 / 360 |
|
74.19% |
23 / 31 |
CRAP | |
0.00% |
0 / 1 |
| Scope | |
81.94% |
295 / 360 |
|
74.19% |
23 / 31 |
209.61 | |
0.00% |
0 / 1 |
| readEntity | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
7 | |||
| readEntitiesByIds | |
0.00% |
0 / 28 |
|
0.00% |
0 / 1 |
110 | |||
| readResolvedReferences | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| readByClusterId | |
100.00% |
34 / 34 |
|
100.00% |
1 / 1 |
11 | |||
| readByProviderId | |
76.67% |
23 / 30 |
|
0.00% |
0 / 1 |
11.27 | |||
| readByRequestId | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
| readByDepartmentId | |
100.00% |
32 / 32 |
|
100.00% |
1 / 1 |
11 | |||
| readListBySource | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
| testSource | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| readCollection | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| readList | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
9 | |||
| readIsOpened | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| readIsEnabled | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
3 | |||
| readWaitingNumberUpdated | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
3 | |||
| readDisplayNumberUpdated | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
| readIsGivenNumberInContingent | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| readQueueList | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
2 | |||
| readWithWorkstationCount | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
2 | |||
| readQueueListWithWaitingTime | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
| readListWithScopeAdminEmail | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
4 | |||
| writeEntity | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
1 | |||
| updateEntity | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
| replacePreferences | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
4 | |||
| updateGhostWorkstationCount | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| updateEmergency | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
| writeImageData | |
93.33% |
14 / 15 |
|
0.00% |
0 / 1 |
4.00 | |||
| readImageData | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
2 | |||
| deleteImage | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| deleteEntity | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
3 | |||
| deletePreferences | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
| removeCache | |
56.25% |
9 / 16 |
|
0.00% |
0 / 1 |
21.13 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace BO\Zmsdb; |
| 4 | |
| 5 | use BO\Zmsentities\Scope as Entity; |
| 6 | use BO\Zmsentities\Collection\ScopeList as Collection; |
| 7 | use BO\Zmsdb\Application as App; |
| 8 | |
| 9 | /** |
| 10 | * |
| 11 | * @SuppressWarnings(Public) |
| 12 | * @SuppressWarnings(Coupling) |
| 13 | * @SuppressWarnings(Complexity) |
| 14 | * @SuppressWarnings(TooManyMethods) |
| 15 | * |
| 16 | */ |
| 17 | class Scope extends Base |
| 18 | { |
| 19 | public static $cache = [ ]; |
| 20 | |
| 21 | public function readEntity($scopeId, $resolveReferences = 0, $disableCache = false) |
| 22 | { |
| 23 | $cacheKey = "scope-$scopeId-$resolveReferences"; |
| 24 | |
| 25 | if (!$disableCache && App::$cache && App::$cache->has($cacheKey)) { |
| 26 | $scope = App::$cache->get($cacheKey); |
| 27 | } |
| 28 | |
| 29 | if (empty($scope)) { |
| 30 | $query = new Query\Scope(Query\Base::SELECT); |
| 31 | $query->addEntityMapping() |
| 32 | ->addResolvedReferences($resolveReferences) |
| 33 | ->addConditionScopeId($scopeId); |
| 34 | $scope = $this->fetchOne($query, new Entity()); |
| 35 | if (! $scope->hasId()) { |
| 36 | return null; |
| 37 | } |
| 38 | |
| 39 | if (App::$cache) { |
| 40 | App::$cache->set($cacheKey, $scope); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | return $this->readResolvedReferences($scope, $resolveReferences, $disableCache); |
| 45 | } |
| 46 | |
| 47 | public function readEntitiesByIds(array $scopeIds, int $resolveReferences = 0, bool $disableCache = false): array |
| 48 | { |
| 49 | $scopeIds = array_values(array_unique(array_filter(array_map('intval', $scopeIds)))); |
| 50 | if (!$scopeIds) { |
| 51 | return []; |
| 52 | } |
| 53 | |
| 54 | $result = []; |
| 55 | $missing = []; |
| 56 | foreach ($scopeIds as $scopeId) { |
| 57 | $cacheKey = "{$scopeId}-{$resolveReferences}"; |
| 58 | if (!$disableCache && array_key_exists($cacheKey, self::$cache)) { |
| 59 | $result[$scopeId] = self::$cache[$cacheKey]; |
| 60 | } else { |
| 61 | $missing[] = $scopeId; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | if ($missing) { |
| 66 | $query = new Query\Scope(Query\Base::SELECT); |
| 67 | $query |
| 68 | ->addEntityMapping() |
| 69 | ->addResolvedReferences($resolveReferences) |
| 70 | ->addConditionScopeIds($missing); |
| 71 | |
| 72 | $fetched = $this->fetchList($query, new Entity()); |
| 73 | foreach ($fetched as $entity) { |
| 74 | if (!$entity->hasId()) { |
| 75 | continue; |
| 76 | } |
| 77 | $entity = $this->readResolvedReferences($entity, $resolveReferences); |
| 78 | $result[$entity->id] = $entity; |
| 79 | self::$cache["{$entity->id}-{$resolveReferences}"] = $entity; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | $ordered = []; |
| 84 | foreach ($scopeIds as $id) { |
| 85 | if (isset($result[$id])) { |
| 86 | $ordered[$id] = $result[$id]; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | return $ordered; |
| 91 | } |
| 92 | |
| 93 | public function readResolvedReferences( |
| 94 | \BO\Zmsentities\Schema\Entity $scope, |
| 95 | $resolveReferences, |
| 96 | $disableCache = false |
| 97 | ) { |
| 98 | if (0 < $resolveReferences) { |
| 99 | $scope['dayoff'] = (new DayOff())->readByScopeId($scope->id, $disableCache); |
| 100 | $scope['closure'] = (new Closure())->readByScopeId($scope->id, $disableCache); |
| 101 | } |
| 102 | return $scope; |
| 103 | } |
| 104 | |
| 105 | public function readByClusterId( |
| 106 | $clusterId, |
| 107 | $resolveReferences = 0, |
| 108 | $disableCache = false |
| 109 | ) { |
| 110 | $cacheKey = "scopeReadByClusterId-$clusterId-$resolveReferences"; |
| 111 | |
| 112 | if (!$disableCache && App::$cache && App::$cache->has($cacheKey)) { |
| 113 | $result = App::$cache->get($cacheKey); |
| 114 | } |
| 115 | |
| 116 | if (empty($result)) { |
| 117 | if ($resolveReferences > 0) { |
| 118 | $query = new Query\Scope(Query\Base::SELECT); |
| 119 | $query->addEntityMapping() |
| 120 | ->addResolvedReferences($resolveReferences - 1) |
| 121 | ->addConditionClusterId($clusterId); |
| 122 | $result = $this->fetchList($query, new Entity()); |
| 123 | } else { |
| 124 | $result = $this->getReader()->perform( |
| 125 | (new Query\Scope(Query\Base::SELECT))->getQuerySimpleClusterMatch(), |
| 126 | [$clusterId] |
| 127 | ); |
| 128 | } |
| 129 | |
| 130 | if (App::$cache && !($result instanceof \PDOStatement)) { |
| 131 | App::$cache->set($cacheKey, $result); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | $scopeList = new Collection(); |
| 136 | if (!$result) { |
| 137 | return $scopeList; |
| 138 | } |
| 139 | |
| 140 | foreach ($result as $entity) { |
| 141 | if (0 == $resolveReferences) { |
| 142 | $entity = new Entity( |
| 143 | array( |
| 144 | 'id' => $entity['id'], |
| 145 | '$ref' => '/scope/' . $entity['id'] . '/' |
| 146 | ) |
| 147 | ); |
| 148 | $scopeList->addEntity($entity); |
| 149 | } else { |
| 150 | $scopeList->addEntity($this->readResolvedReferences( |
| 151 | $entity, |
| 152 | $resolveReferences - 1, |
| 153 | $disableCache |
| 154 | )); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | return $scopeList; |
| 159 | } |
| 160 | |
| 161 | public function readByProviderId($providerId, $resolveReferences = 0, $disableCache = false) |
| 162 | { |
| 163 | $cacheKey = "scopeReadByProviderId-$providerId-$resolveReferences"; |
| 164 | |
| 165 | if (!$disableCache && App::$cache && App::$cache->has($cacheKey)) { |
| 166 | $result = App::$cache->get($cacheKey); |
| 167 | } |
| 168 | |
| 169 | if (empty($result)) { |
| 170 | $query = new Query\Scope(Query\Base::SELECT); |
| 171 | $query->addEntityMapping() |
| 172 | ->addResolvedReferences($resolveReferences) |
| 173 | ->addConditionProviderId($providerId); |
| 174 | $result = $this->fetchList($query, new Entity()); |
| 175 | |
| 176 | if (App::$cache) { |
| 177 | App::$cache->set($cacheKey, $result); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | $scopeList = new Collection(); |
| 182 | if (count($result)) { |
| 183 | foreach ($result as $entity) { |
| 184 | if (0 == $resolveReferences) { |
| 185 | $entity = new Entity( |
| 186 | array( |
| 187 | 'id' => $entity->id, |
| 188 | '$ref' => '/scope/' . $entity->id . '/' |
| 189 | ) |
| 190 | ); |
| 191 | $scopeList->addEntity($entity); |
| 192 | } else { |
| 193 | if ($entity instanceof Entity) { |
| 194 | $entity = $this->readResolvedReferences( |
| 195 | $entity, |
| 196 | $resolveReferences - 1, |
| 197 | $disableCache |
| 198 | ); |
| 199 | $scopeList->addEntity($entity); |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | return $scopeList; |
| 206 | } |
| 207 | |
| 208 | public function readByRequestId($requestId, $source, $resolveReferences = 0) |
| 209 | { |
| 210 | $scopeList = new Collection(); |
| 211 | $providerList = (new Provider())->readListBySource($source, 0, true, $requestId); |
| 212 | |
| 213 | foreach ($providerList as $provider) { |
| 214 | $scopeListByProvider = $this->readByProviderId($provider->getId(), $resolveReferences); |
| 215 | if ($scopeListByProvider->count()) { |
| 216 | $scopeList->addList($scopeListByProvider); |
| 217 | } |
| 218 | } |
| 219 | return $scopeList->withUniqueScopes(); |
| 220 | } |
| 221 | |
| 222 | public function readByDepartmentId($departmentId, $resolveReferences = 0, $disableCache = false) |
| 223 | { |
| 224 | $cacheKey = "scopeReadByDepartmentId-$departmentId-$resolveReferences"; |
| 225 | |
| 226 | if (!$disableCache && App::$cache && App::$cache->has($cacheKey)) { |
| 227 | $result = App::$cache->get($cacheKey); |
| 228 | } |
| 229 | |
| 230 | $scopeList = new Collection(); |
| 231 | |
| 232 | if (empty($result)) { |
| 233 | if ($resolveReferences > 0) { |
| 234 | $query = new Query\Scope(Query\Base::SELECT); |
| 235 | $query->addEntityMapping() |
| 236 | ->addResolvedReferences($resolveReferences) |
| 237 | ->addConditionDepartmentId($departmentId); |
| 238 | $result = $this->fetchList($query, new Entity()); |
| 239 | |
| 240 | if (App::$cache) { |
| 241 | App::$cache->set($cacheKey, $result); |
| 242 | } |
| 243 | } else { |
| 244 | $result = $this->getReader()->perform( |
| 245 | (new Query\Scope(Query\Base::SELECT))->getQuerySimpleDepartmentMatch(), |
| 246 | [$departmentId] |
| 247 | ); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | if ($result) { |
| 252 | foreach ($result as $entity) { |
| 253 | if (0 == $resolveReferences) { |
| 254 | $entity = new Entity( |
| 255 | array( |
| 256 | 'id' => $entity['id'], |
| 257 | 'contact' => ['name' => $entity['contact__name']], |
| 258 | '$ref' => '/scope/' . $entity['id'] . '/' |
| 259 | ) |
| 260 | ); |
| 261 | $scopeList->addEntity($entity); |
| 262 | } else { |
| 263 | if ($entity instanceof Entity) { |
| 264 | $entity = $this->readResolvedReferences($entity, $resolveReferences, $disableCache); |
| 265 | $scopeList->addEntity($entity); |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | return $scopeList; |
| 272 | } |
| 273 | public function readListBySource($source, $resolveReferences = 0) |
| 274 | { |
| 275 | $this->testSource($source); |
| 276 | $query = new Query\Request(Query\Base::SELECT); |
| 277 | $query->setResolveLevel($resolveReferences); |
| 278 | $query->addConditionRequestSource($source); |
| 279 | $query->addEntityMapping(); |
| 280 | $requestList = $this->readCollection($query); |
| 281 | return ($requestList->count()) ? $requestList->sortByCustomKey('id') : $requestList; |
| 282 | } |
| 283 | |
| 284 | protected function testSource($source) |
| 285 | { |
| 286 | if (! (new Source())->readEntity($source)) { |
| 287 | throw new Exception\Source\UnknownDataSource(); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | protected function readCollection($query) |
| 292 | { |
| 293 | $requestList = new Collection(); |
| 294 | $statement = $this->fetchStatement($query); |
| 295 | while ($requestData = $statement->fetch(\PDO::FETCH_ASSOC)) { |
| 296 | $request = new Entity($query->postProcessJoins($requestData)); |
| 297 | $requestList->addEntity($request); |
| 298 | } |
| 299 | return $requestList; |
| 300 | } |
| 301 | |
| 302 | public function readList($resolveReferences = 0, $disableCache = false) |
| 303 | { |
| 304 | $cacheKey = "scopeReadList-$resolveReferences"; |
| 305 | |
| 306 | if (!$disableCache && App::$cache && App::$cache->has($cacheKey)) { |
| 307 | $result = App::$cache->get($cacheKey); |
| 308 | } |
| 309 | |
| 310 | if (empty($result)) { |
| 311 | $query = new Query\Scope(Query\Base::SELECT); |
| 312 | $query->addEntityMapping() |
| 313 | ->addResolvedReferences($resolveReferences); |
| 314 | $result = $this->fetchList($query, new Entity()); |
| 315 | |
| 316 | if (App::$cache) { |
| 317 | App::$cache->set($cacheKey, $result); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | $scopeList = new Collection(); |
| 322 | if (count($result)) { |
| 323 | foreach ($result as $entity) { |
| 324 | if ($entity instanceof Entity) { |
| 325 | $entity = $this->readResolvedReferences($entity, $resolveReferences); |
| 326 | $scopeList->addEntity($entity); |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | return $scopeList; |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * get a scope and return true if it is opened |
| 335 | * |
| 336 | * * @param |
| 337 | * scopeId |
| 338 | * now |
| 339 | * |
| 340 | * @return Bool |
| 341 | */ |
| 342 | public function readIsOpened($scopeId, $now) |
| 343 | { |
| 344 | $isOpened = false; |
| 345 | $availabilityList = (new Availability())->readOpeningHoursListByDate($scopeId, $now, 2); |
| 346 | if ($availabilityList->isOpened($now)) { |
| 347 | $isOpened = true; |
| 348 | } |
| 349 | return $isOpened; |
| 350 | } |
| 351 | |
| 352 | public function readIsEnabled($scopeId, $now) |
| 353 | { |
| 354 | $query = new Query\Scope(Query\Base::SELECT); |
| 355 | $query->addEntityMapping() |
| 356 | ->setResolveLevel(0) |
| 357 | ->addConditionScopeId($scopeId); |
| 358 | $scope = $this->fetchOne($query, new Entity()); |
| 359 | return ( |
| 360 | $this->readIsOpened($scopeId, $now) && |
| 361 | $this->readIsGivenNumberInContingent($scopeId) && |
| 362 | ! $scope->getStatus('ticketprinter', 'deactivated') |
| 363 | ); |
| 364 | } |
| 365 | |
| 366 | /** |
| 367 | * get last given waitingnumer and return updated (+1) waitingnumber |
| 368 | * |
| 369 | * * @param |
| 370 | * scopeId |
| 371 | * now |
| 372 | * |
| 373 | * @return Bool |
| 374 | */ |
| 375 | public function readWaitingNumberUpdated($scopeId, $dateTime, $respectContingent = true) |
| 376 | { |
| 377 | if (! $this->readIsGivenNumberInContingent($scopeId) && $respectContingent) { |
| 378 | throw new Exception\Scope\GivenNumberCountExceeded(); |
| 379 | } |
| 380 | $this->perform( |
| 381 | (new Query\Scope(Query\Base::SELECT))->getQueryLastWaitingNumber(), |
| 382 | ['scope_id' => $scopeId] |
| 383 | ); |
| 384 | $entity = $this->readEntity($scopeId, 0, true)->updateStatusQueue($dateTime); |
| 385 | $scope = $this->updateEntity($scopeId, $entity); |
| 386 | return $scope->getStatus('queue', 'lastGivenNumber'); |
| 387 | } |
| 388 | |
| 389 | public function readDisplayNumberUpdated($scopeId) |
| 390 | { |
| 391 | $this->perform( |
| 392 | (new Query\Scope(Query\Base::SELECT))->getQueryLastDisplayNumber(), |
| 393 | ['scope_id' => $scopeId] |
| 394 | ); |
| 395 | $entity = $this->readEntity($scopeId, 0, true)->incrementDisplayNumber(); |
| 396 | $scope = $this->updateEntity($scopeId, $entity); |
| 397 | return $scope->getStatus('queue', 'lastDisplayNumber'); |
| 398 | } |
| 399 | |
| 400 | /** |
| 401 | * get last given waitingnumer and return updated (+1) waitingnumber |
| 402 | * |
| 403 | * * @param |
| 404 | * scopeId |
| 405 | * now |
| 406 | * |
| 407 | * @return Bool |
| 408 | */ |
| 409 | public function readIsGivenNumberInContingent($scopeId) |
| 410 | { |
| 411 | $isInContingent = $this->getReader() |
| 412 | ->fetchValue((new Query\Scope(Query\Base::SELECT)) |
| 413 | ->getQueryGivenNumbersInContingent(), ['scope_id' => $scopeId]); |
| 414 | return ($isInContingent) ? true : false; |
| 415 | } |
| 416 | |
| 417 | /** |
| 418 | * get list of queues on scope by daytime |
| 419 | * |
| 420 | * * @param |
| 421 | * scopeId |
| 422 | * now |
| 423 | * |
| 424 | * @return number |
| 425 | */ |
| 426 | public function readQueueList($scopeId, $dateTime, $resolveReferences = 0, $withEntities = []) |
| 427 | { |
| 428 | if ($resolveReferences > 0) { |
| 429 | // resolveReferences > 0 is only necessary for a resolved process |
| 430 | $queueList = (new Process()) |
| 431 | ->readProcessListByScopeAndTime( |
| 432 | $scopeId, |
| 433 | $dateTime, |
| 434 | $resolveReferences - 1, |
| 435 | $withEntities |
| 436 | ) |
| 437 | ->toQueueList($dateTime); |
| 438 | } else { |
| 439 | $queueList = (new Queue()) |
| 440 | ->readListByScopeAndTime($scopeId, $dateTime, $resolveReferences); |
| 441 | } |
| 442 | return $queueList->withSortedArrival(); |
| 443 | } |
| 444 | |
| 445 | /** |
| 446 | * get waitingtime of scope |
| 447 | * |
| 448 | * * @param |
| 449 | * scopeId |
| 450 | * now |
| 451 | * |
| 452 | * @return \BO\Zmsentities\Scope |
| 453 | */ |
| 454 | public function readWithWorkstationCount($scopeId, $dateTime, $resolveReferences = 0, $withEntities = []) |
| 455 | { |
| 456 | $query = new Query\Scope(Query\Base::SELECT, '', false, null, $withEntities); |
| 457 | $query |
| 458 | ->addEntityMapping() |
| 459 | ->addConditionScopeId($scopeId) |
| 460 | ->addResolvedReferences($resolveReferences) |
| 461 | ->addSelectWorkstationCount($dateTime); |
| 462 | $scope = $this->fetchOne($query, new Entity()); |
| 463 | $scope = $this->readResolvedReferences($scope, $resolveReferences); |
| 464 | return ($scope->hasId()) ? $scope : null; |
| 465 | } |
| 466 | |
| 467 | public function readQueueListWithWaitingTime($scope, $dateTime, $resolveReferences = 0, $withEntities = []) |
| 468 | { |
| 469 | $timeAverage = $scope->getPreference('queue', 'processingTimeAverage'); |
| 470 | $scope = (! $timeAverage) ? (new Scope())->readEntity($scope->id) : $scope; |
| 471 | $queueList = $this->readQueueList($scope->id, $dateTime, $resolveReferences, $withEntities); |
| 472 | $timeAverage = $scope->getPreference('queue', 'processingTimeAverage'); |
| 473 | $workstationCount = $scope->getCalculatedWorkstationCount(); |
| 474 | return $queueList->withEstimatedWaitingTime($timeAverage, $workstationCount, $dateTime); |
| 475 | } |
| 476 | |
| 477 | /** |
| 478 | * get list of scopes with admin |
| 479 | * |
| 480 | * * @param |
| 481 | * scopeId |
| 482 | * now |
| 483 | * |
| 484 | * @return number |
| 485 | */ |
| 486 | public function readListWithScopeAdminEmail($resolveReferences = 0) |
| 487 | { |
| 488 | $scopeList = new Collection(); |
| 489 | $query = new Query\Scope(Query\Base::SELECT); |
| 490 | $query |
| 491 | ->addEntityMapping() |
| 492 | ->addConditionWithAdminEmail() |
| 493 | ->addResolvedReferences($resolveReferences); |
| 494 | $result = $this->fetchList($query, new Entity()); |
| 495 | if (count($result)) { |
| 496 | foreach ($result as $entity) { |
| 497 | if ($entity instanceof Entity) { |
| 498 | $entity = $this->readResolvedReferences($entity, $resolveReferences); |
| 499 | $scopeList->addEntity($entity); |
| 500 | } |
| 501 | } |
| 502 | } |
| 503 | return $scopeList; |
| 504 | } |
| 505 | |
| 506 | /** |
| 507 | * write a scope |
| 508 | * |
| 509 | * @return Entity |
| 510 | */ |
| 511 | public function writeEntity(\BO\Zmsentities\Scope $entity, $parentId) |
| 512 | { |
| 513 | self::$cache = []; |
| 514 | $query = new Query\Scope(Query\Base::INSERT); |
| 515 | $values = $query->reverseEntityMapping($entity, $parentId); |
| 516 | $query->addValues($values); |
| 517 | $this->writeItem($query); |
| 518 | $lastInsertId = $this->getWriter() |
| 519 | ->lastInsertId(); |
| 520 | $this->replacePreferences($entity); |
| 521 | |
| 522 | $this->removeCache($entity); |
| 523 | |
| 524 | return $this->readEntity($lastInsertId); |
| 525 | } |
| 526 | |
| 527 | /** |
| 528 | * update a scope |
| 529 | * |
| 530 | * @param |
| 531 | * scopeId |
| 532 | * |
| 533 | * @return Entity |
| 534 | */ |
| 535 | public function updateEntity($scopeId, \BO\Zmsentities\Scope $entity, $resolveReferences = 0) |
| 536 | { |
| 537 | self::$cache = []; |
| 538 | $query = new Query\Scope(Query\Base::UPDATE); |
| 539 | $query->addConditionScopeId($scopeId); |
| 540 | $values = $query->reverseEntityMapping($entity); |
| 541 | $query->addValues($values); |
| 542 | $this->writeItem($query); |
| 543 | $this->replacePreferences($entity); |
| 544 | |
| 545 | $this->removeCache($entity); |
| 546 | |
| 547 | return $this->readEntity($scopeId, $resolveReferences, true); |
| 548 | } |
| 549 | |
| 550 | public function replacePreferences(\BO\Zmsentities\Scope $entity) |
| 551 | { |
| 552 | if (isset($entity['preferences'])) { |
| 553 | $preferenceQuery = new Preferences(); |
| 554 | $entityName = 'scope'; |
| 555 | $entityId = $entity['id']; |
| 556 | foreach ($entity['preferences'] as $groupName => $groupValues) { |
| 557 | foreach ($groupValues as $name => $value) { |
| 558 | $preferenceQuery->replaceProperty($entityName, $entityId, $groupName, $name, $value); |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | $this->removeCache($entity); |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | /** |
| 567 | * update ghostWorkstationCount |
| 568 | * |
| 569 | * @param |
| 570 | * scopeId |
| 571 | * entity |
| 572 | * dateTime (now) |
| 573 | * |
| 574 | * @return Entity |
| 575 | */ |
| 576 | public function updateGhostWorkstationCount(\BO\Zmsentities\Scope $entity, \DateTimeInterface $dateTime) |
| 577 | { |
| 578 | $query = new Query\Scope(Query\Base::UPDATE); |
| 579 | $query->addConditionScopeId($entity->id); |
| 580 | $values = $query->setGhostWorkstationCountEntityMapping($entity, $dateTime); |
| 581 | $query->addValues($values); |
| 582 | $this->writeItem($query); |
| 583 | |
| 584 | $this->removeCache($entity); |
| 585 | |
| 586 | return $entity; |
| 587 | } |
| 588 | |
| 589 | /** |
| 590 | * update emergency |
| 591 | * |
| 592 | * @param |
| 593 | * scopeId |
| 594 | * entity |
| 595 | * |
| 596 | * @return Entity |
| 597 | */ |
| 598 | public function updateEmergency($scopeId, \BO\Zmsentities\Scope $entity) |
| 599 | { |
| 600 | self::$cache = []; |
| 601 | $query = new Query\Scope(Query\Base::UPDATE); |
| 602 | $query->addConditionScopeId($scopeId); |
| 603 | $values = $query->setEmergencyEntityMapping($entity); |
| 604 | $query->addValues($values); |
| 605 | $this->writeItem($query); |
| 606 | |
| 607 | $this->removeCache($entity); |
| 608 | |
| 609 | return $this->readEntity($scopeId, 0, true); |
| 610 | } |
| 611 | |
| 612 | /** |
| 613 | * update image data for call display image |
| 614 | * |
| 615 | * @param |
| 616 | * scopeId |
| 617 | * Mimepart entity |
| 618 | * |
| 619 | * @return Mimepart entity |
| 620 | */ |
| 621 | public function writeImageData($scopeId, \BO\Zmsentities\Mimepart $entity) |
| 622 | { |
| 623 | if ($entity->mime && $entity->content) { |
| 624 | $this->deleteImage($scopeId); |
| 625 | $extension = $entity->getExtension(); |
| 626 | if ($extension == 'jpeg') { |
| 627 | $extension = 'jpg'; //compatibility ZMS1 |
| 628 | } |
| 629 | $imageName = 's_' . $scopeId . '_bild.' . $extension; |
| 630 | $this->getWriter()->perform( |
| 631 | (new Query\Scope(Query\Base::REPLACE))->getQueryWriteImageData(), |
| 632 | array( |
| 633 | 'imagename' => $imageName, |
| 634 | 'imagedata' => $entity->content |
| 635 | ) |
| 636 | ); |
| 637 | } |
| 638 | $entity->id = $scopeId; |
| 639 | return $entity; |
| 640 | } |
| 641 | |
| 642 | /** |
| 643 | * read image data |
| 644 | * |
| 645 | * @param |
| 646 | * scopeId |
| 647 | * |
| 648 | * @return Mimepart entity |
| 649 | */ |
| 650 | public function readImageData($scopeId) |
| 651 | { |
| 652 | $imageName = 's_' . $scopeId . '_bild'; |
| 653 | $imageData = new \BO\Zmsentities\Mimepart(); |
| 654 | $fileData = $this->getReader()->fetchAll( |
| 655 | (new Query\Scope(Query\Base::SELECT))->getQueryReadImageData(), |
| 656 | ['imagename' => "$imageName%"] |
| 657 | ); |
| 658 | if ($fileData) { |
| 659 | $imageData->content = $fileData[0]['imagecontent']; |
| 660 | $imageData->mime = pathinfo($fileData[0]['imagename'])['extension']; |
| 661 | } |
| 662 | return $imageData; |
| 663 | } |
| 664 | |
| 665 | /** |
| 666 | * delete image data for call display image |
| 667 | * |
| 668 | * @param |
| 669 | * scopeId |
| 670 | * |
| 671 | * @return Status |
| 672 | */ |
| 673 | public function deleteImage($scopeId) |
| 674 | { |
| 675 | $imageName = 's_' . $scopeId . '_bild'; |
| 676 | return $this->perform((new Query\Scope(Query\Base::DELETE))->getQueryDeleteImage(), array( |
| 677 | 'imagename' => "$imageName%" |
| 678 | )); |
| 679 | } |
| 680 | |
| 681 | /** |
| 682 | * remove a scope |
| 683 | * |
| 684 | * @param |
| 685 | * scopeId |
| 686 | * |
| 687 | * @return Resource Status |
| 688 | */ |
| 689 | public function deleteEntity($scopeId) |
| 690 | { |
| 691 | $processListCount = (new Process())->readProcessListCountByScope($scopeId); |
| 692 | if (0 < $processListCount) { |
| 693 | throw new Exception\Scope\ScopeHasProcesses(); |
| 694 | } |
| 695 | self::$cache = []; |
| 696 | $entity = $this->readEntity($scopeId); |
| 697 | $query = new Query\Scope(Query\Base::DELETE); |
| 698 | $query->addConditionScopeId($scopeId); |
| 699 | $this->deletePreferences($entity); |
| 700 | |
| 701 | $this->removeCache($entity); |
| 702 | |
| 703 | return ($this->deleteItem($query)) ? $entity : null; |
| 704 | } |
| 705 | |
| 706 | public function deletePreferences(\BO\Zmsentities\Scope $entity) |
| 707 | { |
| 708 | $preferenceQuery = new Preferences(); |
| 709 | $entityName = 'scope'; |
| 710 | $entityId = $entity['id']; |
| 711 | foreach ($entity['preferences'] as $groupName => $groupValues) { |
| 712 | foreach (array_keys($groupValues) as $name) { |
| 713 | $preferenceQuery->deleteProperty($entityName, $entityId, $groupName, $name); |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | $this->removeCache($entity); |
| 718 | } |
| 719 | |
| 720 | public function removeCache($scope) |
| 721 | { |
| 722 | if (!App::$cache) { |
| 723 | return; |
| 724 | } |
| 725 | |
| 726 | if (isset($scope->provider) && isset($this->provider->id)) { |
| 727 | if (App::$cache->has('scopeReadByProviderId-' . $scope->getProviderId() . '-0')) { |
| 728 | App::$cache->delete('scopeReadByProviderId-' . $scope->getProviderId() . '-0'); |
| 729 | } |
| 730 | |
| 731 | if (App::$cache->has('scopeReadByProviderId-' . $scope->getProviderId() . '-1')) { |
| 732 | App::$cache->delete('scopeReadByProviderId-' . $scope->getProviderId() . '-1'); |
| 733 | } |
| 734 | |
| 735 | if (App::$cache->has('scopeReadByProviderId-' . $scope->getProviderId() . '-2')) { |
| 736 | App::$cache->delete('scopeReadByProviderId-' . $scope->getProviderId() . '-2'); |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | if (isset($scope->id)) { |
| 741 | if (App::$cache->has("scope-$scope->id-0")) { |
| 742 | App::$cache->delete("scope-$scope->id-0"); |
| 743 | } |
| 744 | |
| 745 | if (App::$cache->has("scope-$scope->id-1")) { |
| 746 | App::$cache->delete("scope-$scope->id-1"); |
| 747 | } |
| 748 | |
| 749 | if (App::$cache->has("scope-$scope->id-2")) { |
| 750 | App::$cache->delete("scope-$scope->id-2"); |
| 751 | } |
| 752 | } |
| 753 | } |
| 754 | } |