EntityModelAbstract.php 18.8 KB
Newer Older
Giovannoni Laurent's avatar
Giovannoni Laurent committed
1
2
3
4
5
6
7
8
9
10
<?php

/**
* Copyright Maarch since 2008 under licence GPLv3.
* See LICENCE.txt file at the root folder for more details.
* This file is part of Maarch software.
*
*/

/**
Damien's avatar
Damien committed
11
* @brief Entity Model Abstract
Giovannoni Laurent's avatar
Giovannoni Laurent committed
12
13
14
* @author dev@maarch.org
*/

Damien's avatar
Damien committed
15
namespace Entity\models;
Giovannoni Laurent's avatar
Giovannoni Laurent committed
16

17
use SrcCore\models\ValidatorModel;
Damien's avatar
Damien committed
18
use SrcCore\models\CoreConfigModel;
Damien's avatar
Damien committed
19
use SrcCore\models\DatabaseModel;
20
use User\controllers\UserController;
Damien's avatar
Damien committed
21
use User\models\UserModel;
22

Damien's avatar
Damien committed
23
abstract class EntityModelAbstract
Giovannoni Laurent's avatar
Giovannoni Laurent committed
24
{
25
26
    public static function get(array $aArgs = [])
    {
Florian Azizian's avatar
Florian Azizian committed
27
        ValidatorModel::arrayType($aArgs, ['select', 'where', 'data', 'orderBy', 'table']);
Damien's avatar
Damien committed
28
        ValidatorModel::intType($aArgs, ['limit']);
29

Damien's avatar
Damien committed
30
        $aEntities = DatabaseModel::select([
31
            'select'    => empty($aArgs['select']) ? ['*'] : $aArgs['select'],
Florian Azizian's avatar
Florian Azizian committed
32
            'table'     => empty($aArgs['table']) ? ['entities'] : $aArgs['table'],
Damien's avatar
Damien committed
33
34
            'where'     => empty($aArgs['where']) ? [] : $aArgs['where'],
            'data'      => empty($aArgs['data']) ? [] : $aArgs['data'],
Florian Azizian's avatar
Florian Azizian committed
35
            'left_join' => empty($aArgs['left_join']) ? [] : $aArgs['left_join'],
Damien's avatar
Damien committed
36
37
            'order_by'  => empty($aArgs['orderBy']) ? [] : $aArgs['orderBy'],
            'limit'     => empty($aArgs['limit']) ? 0 : $aArgs['limit']
38
39
40
41
42
        ]);

        return $aEntities;
    }

43
    public static function getById(array $args)
Damien's avatar
Damien committed
44
    {
45
46
        ValidatorModel::notEmpty($args, ['id']);
        ValidatorModel::intVal($args, ['id']);
Damien's avatar
Damien committed
47

48
49
        $entity = DatabaseModel::select([
            'select'    => empty($args['select']) ? ['*'] : $args['select'],
Damien's avatar
Damien committed
50
51
            'table'     => ['entities'],
            'where'     => ['id = ?'],
52
            'data'      => [$args['id']]
Damien's avatar
Damien committed
53
54
        ]);

55
        if (empty($entity[0])) {
Damien's avatar
Damien committed
56
57
58
            return [];
        }

59
        return $entity[0];
Damien's avatar
Damien committed
60
61
62
    }

    public static function getByEntityId(array $aArgs)
63
    {
Damien's avatar
Damien committed
64
        ValidatorModel::notEmpty($aArgs, ['entityId']);
65
        ValidatorModel::stringType($aArgs, ['entityId']);
Damien's avatar
Damien committed
66

67
        $aEntity = DatabaseModel::select([
68
69
            'select'    => empty($aArgs['select']) ? ['*'] : $aArgs['select'],
            'table'     => ['entities'],
70
            'where'     => ['entity_id = ?'],
71
72
73
            'data'      => [$aArgs['entityId']]
        ]);

Damien's avatar
Damien committed
74
75
76
77
        if (empty($aEntity[0])) {
            return [];
        }

78
79
80
        return $aEntity[0];
    }

81
82
    public static function create(array $aArgs)
    {
Damien's avatar
Damien committed
83
        ValidatorModel::notEmpty($aArgs, ['entity_id', 'entity_label', 'short_label', 'entity_type']);
84
        ValidatorModel::stringType($aArgs, [
Damien's avatar
Damien committed
85
            'entity_id', 'entity_label', 'short_label', 'entity_type', 'adrs_1', 'adrs_2', 'adrs_3',
86
            'zipcode', 'city', 'country', 'email', 'business_id', 'parent_entity_id',
87
            'ldap_id', 'transferring_agency', 'entity_full_name', 'producerService'
88
89
90
91
92
        ]);

        DatabaseModel::insert([
            'table'         => 'entities',
            'columnsValues' => [
Damien's avatar
Damien committed
93
                'entity_id'             => $aArgs['entity_id'],
94
95
96
97
98
99
100
101
102
103
104
105
106
107
                'entity_label'          => $aArgs['entity_label'],
                'short_label'           => $aArgs['short_label'],
                'adrs_1'                => $aArgs['adrs_1'],
                'adrs_2'                => $aArgs['adrs_2'],
                'adrs_3'                => $aArgs['adrs_3'],
                'zipcode'               => $aArgs['zipcode'],
                'city'                  => $aArgs['city'],
                'country'               => $aArgs['country'],
                'email'                 => $aArgs['email'],
                'business_id'           => $aArgs['business_id'],
                'parent_entity_id'      => $aArgs['parent_entity_id'],
                'entity_type'           => $aArgs['entity_type'],
                'ldap_id'               => $aArgs['ldap_id'],
                'entity_full_name'      => $aArgs['entity_full_name'],
108
                'producer_service'      => $aArgs['producerService']
109
110
111
112
113
114
            ]
        ]);

        return true;
    }

115
    public static function update(array $args)
116
    {
117
118
        ValidatorModel::notEmpty($args, ['where', 'data']);
        ValidatorModel::arrayType($args, ['set', 'postSet', 'where', 'data']);
119

Damien's avatar
Damien committed
120
        DatabaseModel::update([
121
122
123
124
125
            'table'     => 'entities',
            'set'       => $args['set'],
            'postSet'   => $args['postSet'],
            'where'     => $args['where'],
            'data'      => $args['data']
126
127
128
        ]);

        return true;
129
    }
Giovannoni Laurent's avatar
Giovannoni Laurent committed
130

131
132
133
134
135
136
137
138
139
140
141
142
143
144
    public static function delete(array $aArgs)
    {
        ValidatorModel::notEmpty($aArgs, ['where', 'data']);
        ValidatorModel::arrayType($aArgs, ['where', 'data']);

        DatabaseModel::delete([
            'table' => 'entities',
            'where' => $aArgs['where'],
            'data'  => $aArgs['data']
        ]);

        return true;
    }

Damien's avatar
Damien committed
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
    public static function getByEmail(array $aArgs = [])
    {
        ValidatorModel::notEmpty($aArgs, ['email']);
        ValidatorModel::stringType($aArgs, ['email']);

        $aReturn = DatabaseModel::select([
            'select'    => empty($aArgs['select']) ? ['*'] : $aArgs['select'],
            'table'     => ['entities'],
            'where'     => ['email = ?', 'enabled = ?'],
            'data'      => [$aArgs['email'], 'Y'],
            'limit'     => 1,
        ]);

        return $aReturn;
    }

161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
    public static function getByBusinessId(array $aArgs = [])
    {
        ValidatorModel::notEmpty($aArgs, ['businessId']);
        ValidatorModel::stringType($aArgs, ['businessId']);

        $aReturn = DatabaseModel::select([
            'select'    => empty($aArgs['select']) ? ['*'] : $aArgs['select'],
            'table'     => ['entities'],
            'where'     => ['business_id = ? and enabled = ?'],
            'data'      => [$aArgs['businessId'], 'Y'],
            'limit'     => 1,
        ]);

        return $aReturn;
    }

177
    public static function getByUserId(array $aArgs)
Giovannoni Laurent's avatar
Giovannoni Laurent committed
178
    {
179
180
        ValidatorModel::notEmpty($aArgs, ['userId']);
        ValidatorModel::intVal($aArgs, ['userId']);
Damien's avatar
Damien committed
181
        ValidatorModel::arrayType($aArgs, ['select']);
Damien's avatar
Damien committed
182

183
        $entities = DatabaseModel::select([
Giovannoni Laurent's avatar
Giovannoni Laurent committed
184
185
            'select'    => empty($aArgs['select']) ? ['*'] : $aArgs['select'],
            'table'     => ['users_entities'],
186
            'where'     => ['user_id = ?'],
187
            'data'      => [$aArgs['userId']]
Giovannoni Laurent's avatar
Giovannoni Laurent committed
188
189
        ]);

190
        return $entities;
Giovannoni Laurent's avatar
Giovannoni Laurent committed
191
    }
192

Damien's avatar
Damien committed
193
    public static function getWithUserEntities(array $args = [])
194
    {
Damien's avatar
Damien committed
195
        ValidatorModel::arrayType($args, ['select', 'where', 'data']);
196

Damien's avatar
Damien committed
197
198
        $entities = DatabaseModel::select([
            'select'    => empty($args['select']) ? ['*'] : $args['select'],
199
200
            'table'     => ['users_entities', 'entities'],
            'left_join' => ['users_entities.entity_id = entities.entity_id'],
Damien's avatar
Damien committed
201
202
            'where'     => empty($args['where']) ? [] : $args['where'],
            'data'      => empty($args['data']) ? [] : $args['data']
203
204
        ]);

Damien's avatar
Damien committed
205
        return $entities;
206
207
208
209
210
211
212
    }

    public static function getEntityRootById(array $aArgs = [])
    {
        ValidatorModel::notEmpty($aArgs, ['entityId']);
        ValidatorModel::stringType($aArgs, ['entityId']);

Damien's avatar
Damien committed
213
        $aReturn = entitymodel::getByEntityId([
214
            'select'   => ['entity_id', 'entity_label', 'parent_entity_id'],
215
            'entityId' => $aArgs['entityId']
216
217
        ]);

Florian Azizian's avatar
Florian Azizian committed
218
        if (!empty($aReturn['parent_entity_id'])) {
Damien's avatar
Damien committed
219
            $aReturn = EntityModel::getEntityRootById(['entityId' => $aReturn['parent_entity_id']]);
220
221
222
223
224
        }

        return $aReturn;
    }

225
    public static function getEntityChildren(array $aArgs)
226
    {
Damien's avatar
Damien committed
227
228
        ValidatorModel::notEmpty($aArgs, ['entityId']);
        ValidatorModel::stringType($aArgs, ['entityId']);
229

230
231
        $allEntities = DatabaseModel::select([
            'select'    => ['entity_id', 'parent_entity_id'],
232
            'table'     => ['entities'],
233
            'where'     => ['parent_entity_id IS NOT NULL AND parent_entity_id <> \'\''],
234
235
        ]);

236
237
238
239
240
241
242
243
244
245
246
247
        $orderedEntities = [];
        foreach ($allEntities as $value) {
            $orderedEntities[$value['parent_entity_id']][] = $value['entity_id'];
        }

        $entities = EntityModel::getEntityChildrenLoop(['entityId' => $aArgs['entityId'], 'entities' => $orderedEntities]);

        return $entities;
    }

    public static function getEntityChildrenLoop(array $aArgs)
    {
248
        $entities = [$aArgs['entityId']];
249
250
251
252
253
254
        if (!empty($aArgs['entities']) && array_key_exists($aArgs['entityId'], $aArgs['entities'])) {
            $childrenEntities = $aArgs['entities'][$aArgs['entityId']];
            unset($aArgs['entities'][$aArgs['entityId']]);
            foreach ($childrenEntities as $child) {
                $entities = array_merge($entities, EntityModel::getEntityChildrenLoop(['entityId' => $child, 'entities' => $aArgs['entities']]));
            }
255
256
257
258
259
        }

        return $entities;
    }

260
261
262
263
264
    public static function getEntityChildrenById(array $args)
    {
        ValidatorModel::notEmpty($args, ['id']);
        ValidatorModel::intVal($args, ['id']);

265
266
267
268
        $allEntities = DatabaseModel::select([
            'select'    => ['id', 'parent_entity_id'],
            'table'     => ['entities'],
            'where'     => ['parent_entity_id IS NOT NULL AND parent_entity_id <> \'\''],
269
270
        ]);

271
272
273
274
275
276
        $orderedEntities = [];
        foreach ($allEntities as $value) {
            $orderedEntities[$value['parent_entity_id']][] = $value['id'];
        }

        $entities = EntityModel::getEntityChildrenLoop(['entityId' => $args['id'], 'entities' => $orderedEntities]);
277
278
279
280

        return $entities;
    }

281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
    public static function getEntityChildrenSubLevel(array $aArgs)
    {
        ValidatorModel::notEmpty($aArgs, ['entitiesId']);
        ValidatorModel::arrayType($aArgs, ['entitiesId']);

        $aReturn = DatabaseModel::select([
            'select'    => empty($aArgs['select']) ? ['*'] : $aArgs['select'],
            'table'     => ['entities'],
            'where'     => ['parent_entity_id in (?)', 'enabled = ?'],
            'data'      => [$aArgs['entitiesId'], 'Y'],
            'order_by'  => ['entity_label']
        ]);

        return $aReturn;
    }

297
    public static function getAllEntitiesByUserId(array $args)
298
    {
299
300
        ValidatorModel::notEmpty($args, ['userId']);
        ValidatorModel::intVal($args, ['userId']);
301
302

        $entities = [];
Damien's avatar
Damien committed
303

304
        if (UserController::isRoot(['id' => $args['userId']])) {
Damien's avatar
Damien committed
305
306
307
308
309
310
311
            $rawEntities = EntityModel::get(['select' => ['entity_id'], 'where' => ['enabled = ?'], 'data' => ['Y']]);
            foreach ($rawEntities as $value) {
                $entities[] = $value['entity_id'];
            }
            return $entities;
        }

312
        $aReturn = UserModel::getEntitiesById(['id' => $args['userId'], 'select' => ['users_entities.entity_id']]);
313
        foreach ($aReturn as $value) {
314
            $entities = array_merge($entities, EntityModel::getEntityChildren(['entityId' => $value['entity_id']]));
315
316
317
318
        }
        
        return array_unique($entities);
    }
319

320
    public static function getAvailableEntitiesForAdministratorByUserId(array $aArgs)
321
    {
Damien's avatar
Damien committed
322
323
        ValidatorModel::notEmpty($aArgs, ['userId', 'administratorUserId']);
        ValidatorModel::stringType($aArgs, ['userId', 'administratorUserId']);
324

325
326
327
        $administrator = UserModel::getByLogin(['login' => $aArgs['administratorUserId'], 'select' => ['id']]);

        if (UserController::isRoot(['id' => $administrator['id']])) {
328
            $rawEntitiesAllowedForAdministrator = EntityModel::get(['select' => ['entity_id'], 'where' => ['enabled = ?'], 'data' => ['Y'], 'orderBy' => ['entity_label']]);
329
330
331
332
333
            $entitiesAllowedForAdministrator = [];
            foreach ($rawEntitiesAllowedForAdministrator as $value) {
                $entitiesAllowedForAdministrator[] = $value['entity_id'];
            }
        } else {
334
            $entitiesAllowedForAdministrator = EntityModel::getAllEntitiesByUserId(['userId' => $administrator['id']]);
335
336
        }

337
338
        $user = UserModel::getByLogin(['login' => $aArgs['userId'], 'select' => ['id']]);
        $rawUserEntities = EntityModel::getByUserId(['userId' => $user['id'], 'select' => ['entity_id']]);
339
340
341
342
343
344

        $userEntities = [];
        foreach ($rawUserEntities as $value) {
            $userEntities[] = $value['entity_id'];
        }

345
        $allEntities = EntityModel::get(['select' => ['entity_id', 'entity_label', 'parent_entity_id'], 'where' => ['enabled = ?'], 'data' => ['Y'], 'orderBy' => ['entity_label']]);
346
347

        foreach ($allEntities as $key => $value) {
348
349
350
351
            $allEntities[$key]['id'] = $value['entity_id'];
            if (empty($value['parent_entity_id'])) {
                $allEntities[$key]['parent'] = '#';
                $allEntities[$key]['icon'] = "fa fa-building";
352
            } else {
353
354
355
356
                $allEntities[$key]['parent'] = $value['parent_entity_id'];
                $allEntities[$key]['icon'] = "fa fa-sitemap";
            }
            $allEntities[$key]['text'] = $value['entity_label'];
357
            if (in_array($value['entity_id'], $userEntities)) {
358
359
                $allEntities[$key]['state']['opened'] = true;
                $allEntities[$key]['state']['selected'] = true;
360
            }
361
362
363
            if (!in_array($value['entity_id'], $entitiesAllowedForAdministrator)) {
                $allEntities[$key]['state']['disabled'] = true;
            }
364
365
366
367
368
        }

        return $allEntities;
    }

369
370
    public static function getAllowedEntitiesByUserId(array $aArgs)
    {
371
372
373
374
375
376
        if (empty($aArgs['root'])) {
            ValidatorModel::notEmpty($aArgs, ['userId']);
            ValidatorModel::stringType($aArgs, ['userId']);

            $user = UserModel::getByLogin(['login' => $aArgs['userId'], 'select' => ['id']]);
        }
377

378
        if (!empty($aArgs['root']) || UserController::isRoot(['id' => $user['id']])) {
379
            $rawEntitiesAllowed = EntityModel::get(['select' => ['entity_id'], 'where' => ['enabled = ?'], 'data' => ['Y'], 'orderBy' => ['entity_label']]);
380
            $entitiesAllowed = array_column($rawEntitiesAllowed, 'entity_id');
381
        } else {
382
            $entitiesAllowed = EntityModel::getAllEntitiesByUserId(['userId' => $user['id']]);
383
384
        }

385
386
387
388
389
390
391
392
        $allEntities = EntityModel::get([
            'select'    => ['e1.id', 'e1.entity_id', 'e1.entity_label', 'e1.parent_entity_id', 'e2.id as parent_id'],
            'table'     => ['entities e1', 'entities e2'],
            'left_join' => ['e1.parent_entity_id = e2.entity_id'],
            'where'     => ['e1.enabled = ?'],
            'data'      => ['Y'],
            'orderBy'   => ['e1.parent_entity_id']
        ]);
393
        $allEntities = EntityModel::removeOrphanedEntities($allEntities);
394
395

        foreach ($allEntities as $key => $value) {
Damien's avatar
Damien committed
396
            $allEntities[$key]['serialId'] = $value['id'];
397
398
            $allEntities[$key]['id'] = $value['entity_id'];
            if (empty($value['parent_entity_id'])) {
399
                $allEntities[$key]['parentSerialId'] = '#';
400
401
402
                $allEntities[$key]['parent'] = '#';
                $allEntities[$key]['icon'] = "fa fa-building";
            } else {
403
                $allEntities[$key]['parentSerialId'] = $value['parent_id'];
404
405
406
407
408
409
410
                $allEntities[$key]['parent'] = $value['parent_entity_id'];
                $allEntities[$key]['icon'] = "fa fa-sitemap";
            }
            if (in_array($value['entity_id'], $entitiesAllowed)) {
                $allEntities[$key]['allowed'] = true;
            } else {
                $allEntities[$key]['allowed'] = false;
Damien's avatar
Damien committed
411
                $allEntities[$key]['state']['disabled'] = true;
412
            }
Damien's avatar
Damien committed
413
            $allEntities[$key]['state']['opened'] = true;
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
            $allEntities[$key]['text'] = $value['entity_label'];
        }

        return $allEntities;
    }

    public static function getUsersById(array $aArgs)
    {
        ValidatorModel::notEmpty($aArgs, ['id']);
        ValidatorModel::stringType($aArgs, ['id']);
        ValidatorModel::arrayType($aArgs, ['select']);

        $aUsers = DatabaseModel::select([
            'select'    => empty($aArgs['select']) ? ['*'] : $aArgs['select'],
            'table'     => ['users_entities, users'],
429
            'where'     => ['users_entities.entity_id = ?', 'users_entities.user_id = users.id', 'users.status != ?'],
Damien's avatar
Damien committed
430
            'data'      => [$aArgs['id'], 'DEL']
431
432
433
434
        ]);

        return $aUsers;
    }
Damien's avatar
Damien committed
435
436
437
438

    public static function getTypes()
    {
        $types = [];
Damien's avatar
Damien committed
439
440
441
442
443
444
445
446
447

        $loadedXml = CoreConfigModel::getXmlLoaded(['path' => 'modules/entities/xml/typentity.xml']);
        if ($loadedXml) {
            foreach ($loadedXml->TYPE as $value) {
                $types[] = [
                    'id'        => (string)$value->id,
                    'label'     => (string)$value->label,
                    'typelevel' => (string)$value->typelevel
                ];
Damien's avatar
Damien committed
448
449
450
451
452
            }
        }

        return $types;
    }
Damien's avatar
Damien committed
453
454
455
456

    public static function getRoles()
    {
        $roles = [];
Damien's avatar
Damien committed
457
458
459
460
461

        $loadedXml = CoreConfigModel::getXmlLoaded(['path' => 'modules/entities/xml/roles.xml']);
        if ($loadedXml) {
            foreach ($loadedXml->ROLES->ROLE as $value) {
                $roles[] = [
462
463
464
                    'id'                    => (string)$value->id,
                    'label'                 => defined((string)$value->label) ? constant((string)$value->label) : (string)$value->label,
                    'keepInListInstance'    => empty((string)$value->keep_in_diffusion_list) || (string)$value->keep_in_diffusion_list != 'true' ? false : true,
Damien's avatar
Damien committed
465
                ];
Damien's avatar
Damien committed
466
467
468
469
470
            }
        }

        return $roles;
    }
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492

    public static function getEntityPathByEntityId(array $args)
    {
        ValidatorModel::notEmpty($args, ['entityId']);
        ValidatorModel::stringType($args, ['entityId', 'path']);

        $entity = EntityModel::getByEntityId([
            'select'   => ['entity_id', 'parent_entity_id'],
            'entityId' => $args['entityId']
        ]);

        if (!empty($args['path'])) {
            $args['path'] = "/{$args['path']}";
        }
        $args['path'] = $entity['entity_id'] . $args['path'];

        if (empty($entity['parent_entity_id'])) {
            return $args['path'];
        }

        return EntityModel::getEntityPathByEntityId(['entityId' => $entity['parent_entity_id'], 'path' => $args['path']]);
    }
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509

    public static function removeOrphanedEntities(array $entities)
    {
        if (!isset($entities[0]['parent_entity_id']) || !isset($entities[0]['entity_id'])) {
            return $entities;
        }

        do {
            $entitiesCount = count($entities);
            $entitiesIds = array_column($entities, 'entity_id');
            $entities = array_values(array_filter($entities, function($entity) use ($entitiesIds) {
                return empty($entity['parent_entity_id']) || ($entity['parent_entity_id'] != $entity['entity_id'] && in_array($entity['parent_entity_id'], $entitiesIds));
            }));
        } while (count($entities) != $entitiesCount);

        return $entities;
    }
Giovannoni Laurent's avatar
Giovannoni Laurent committed
510
}