EntityModelAbstract.php 18.4 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
    public static function create(array $args)
82
    {
83
84
85
86
87
        ValidatorModel::notEmpty($args, ['entity_id', 'entity_label', 'short_label', 'entity_type']);
        ValidatorModel::stringType($args, [
            'entity_id', 'entity_label', 'short_label', 'entity_type', 'address_number', 'address_street', 'address_additional1',
            'address_postcode', 'address_town', 'address_country', 'email', 'business_id', 'parent_entity_id',
            'ldap_id', 'transferring_agency', 'entity_full_name', 'producer_service'
88
89
        ]);

90
91
        $nextSequenceId = DatabaseModel::getNextSequenceValue(['sequenceId' => 'entities_id_seq']);

92
93
94
        DatabaseModel::insert([
            'table'         => 'entities',
            'columnsValues' => [
95
                'id'                    => $nextSequenceId,
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
                'entity_id'             => $args['entity_id'],
                'entity_label'          => $args['entity_label'],
                'short_label'           => $args['short_label'],
                'address_number'        => $args['address_number'],
                'address_street'        => $args['address_street'],
                'address_additional1'   => $args['address_additional1'],
                'address_additional2'   => $args['address_additional2'],
                'address_postcode'      => $args['address_postcode'],
                'address_town'          => $args['address_town'],
                'address_country'       => $args['address_country'],
                'email'                 => $args['email'],
                'business_id'           => $args['business_id'],
                'parent_entity_id'      => $args['parent_entity_id'],
                'entity_type'           => $args['entity_type'],
                'ldap_id'               => $args['ldap_id'],
                'entity_full_name'      => $args['entity_full_name'],
112
113
                'producer_service'      => $args['producer_service'],
                'external_id'           => $args['external_id']
114
115
116
            ]
        ]);

117
        return $nextSequenceId;
118
119
    }

120
    public static function update(array $args)
121
    {
122
123
        ValidatorModel::notEmpty($args, ['where', 'data']);
        ValidatorModel::arrayType($args, ['set', 'postSet', 'where', 'data']);
124

Damien's avatar
Damien committed
125
        DatabaseModel::update([
126
127
128
129
130
            'table'     => 'entities',
            'set'       => $args['set'],
            'postSet'   => $args['postSet'],
            'where'     => $args['where'],
            'data'      => $args['data']
131
132
133
        ]);

        return true;
134
    }
Giovannoni Laurent's avatar
Giovannoni Laurent committed
135

136
137
138
139
140
141
142
143
144
145
146
147
148
149
    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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
    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;
    }

166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
    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;
    }

182
    public static function getByUserId(array $aArgs)
Giovannoni Laurent's avatar
Giovannoni Laurent committed
183
    {
184
185
        ValidatorModel::notEmpty($aArgs, ['userId']);
        ValidatorModel::intVal($aArgs, ['userId']);
Damien's avatar
Damien committed
186
        ValidatorModel::arrayType($aArgs, ['select']);
Damien's avatar
Damien committed
187

188
        $entities = DatabaseModel::select([
Giovannoni Laurent's avatar
Giovannoni Laurent committed
189
190
            'select'    => empty($aArgs['select']) ? ['*'] : $aArgs['select'],
            'table'     => ['users_entities'],
191
            'where'     => ['user_id = ?'],
192
            'data'      => [$aArgs['userId']]
Giovannoni Laurent's avatar
Giovannoni Laurent committed
193
194
        ]);

195
        return $entities;
Giovannoni Laurent's avatar
Giovannoni Laurent committed
196
    }
197

Damien's avatar
Damien committed
198
    public static function getWithUserEntities(array $args = [])
199
    {
Damien's avatar
Damien committed
200
        ValidatorModel::arrayType($args, ['select', 'where', 'data']);
201

Damien's avatar
Damien committed
202
203
        $entities = DatabaseModel::select([
            'select'    => empty($args['select']) ? ['*'] : $args['select'],
204
205
            'table'     => ['users_entities', 'entities'],
            'left_join' => ['users_entities.entity_id = entities.entity_id'],
Damien's avatar
Damien committed
206
207
            'where'     => empty($args['where']) ? [] : $args['where'],
            'data'      => empty($args['data']) ? [] : $args['data']
208
209
        ]);

Damien's avatar
Damien committed
210
        return $entities;
211
212
213
214
215
216
217
    }

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

Damien's avatar
Damien committed
218
        $aReturn = entitymodel::getByEntityId([
219
            'select'   => ['entity_id', 'entity_label', 'parent_entity_id'],
220
            'entityId' => $aArgs['entityId']
221
222
        ]);

Florian Azizian's avatar
Florian Azizian committed
223
        if (!empty($aReturn['parent_entity_id'])) {
Damien's avatar
Damien committed
224
            $aReturn = EntityModel::getEntityRootById(['entityId' => $aReturn['parent_entity_id']]);
225
226
227
228
229
        }

        return $aReturn;
    }

230
    public static function getEntityChildren(array $aArgs)
231
    {
Damien's avatar
Damien committed
232
233
        ValidatorModel::notEmpty($aArgs, ['entityId']);
        ValidatorModel::stringType($aArgs, ['entityId']);
234

235
236
        $allEntities = DatabaseModel::select([
            'select'    => ['entity_id', 'parent_entity_id'],
237
            'table'     => ['entities'],
238
            'where'     => ['parent_entity_id IS NOT NULL AND parent_entity_id <> \'\''],
239
240
        ]);

241
242
243
244
245
246
247
248
249
250
251
252
        $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)
    {
253
        $entities = [$aArgs['entityId']];
254
255
256
257
258
259
        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']]));
            }
260
261
262
263
264
        }

        return $entities;
    }

265
266
267
268
269
    public static function getEntityChildrenById(array $args)
    {
        ValidatorModel::notEmpty($args, ['id']);
        ValidatorModel::intVal($args, ['id']);

270
271
        $allEntities = DatabaseModel::select([
            'select'    => ['id', 'parent_entity_id'],
272
            'table'     => ['entities'],
273
            'where'     => ['parent_entity_id IS NOT NULL AND parent_entity_id <> \'\''],
274
275
        ]);

276
277
278
        $orderedEntities = [];
        foreach ($allEntities as $value) {
            $orderedEntities[$value['parent_entity_id']][] = $value['id'];
279
280
        }

281
282
        $entities = EntityModel::getEntityChildrenLoop(['entityId' => $args['id'], 'entities' => $orderedEntities]);

283
284
285
        return $entities;
    }

286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
    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;
    }

302
    public static function getAllEntitiesByUserId(array $args)
303
    {
304
305
        ValidatorModel::notEmpty($args, ['userId']);
        ValidatorModel::intVal($args, ['userId']);
306
307

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

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

317
        $aReturn = UserModel::getEntitiesById(['id' => $args['userId'], 'select' => ['users_entities.entity_id']]);
318
        foreach ($aReturn as $value) {
319
            $entities = array_merge($entities, EntityModel::getEntityChildren(['entityId' => $value['entity_id']]));
320
        }
321

322
323
        return array_unique($entities);
    }
324

325
    public static function getAvailableEntitiesForAdministratorByUserId(array $aArgs)
326
    {
Damien's avatar
Damien committed
327
328
        ValidatorModel::notEmpty($aArgs, ['userId', 'administratorUserId']);
        ValidatorModel::stringType($aArgs, ['userId', 'administratorUserId']);
329

330
331
332
        $administrator = UserModel::getByLogin(['login' => $aArgs['administratorUserId'], 'select' => ['id']]);

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

342
343
        $user = UserModel::getByLogin(['login' => $aArgs['userId'], 'select' => ['id']]);
        $rawUserEntities = EntityModel::getByUserId(['userId' => $user['id'], 'select' => ['entity_id']]);
344
345
346
347
348
349

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

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

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

        return $allEntities;
    }

374
375
    public static function getAllowedEntitiesByUserId(array $aArgs)
    {
376
377
378
379
380
381
        if (empty($aArgs['root'])) {
            ValidatorModel::notEmpty($aArgs, ['userId']);
            ValidatorModel::stringType($aArgs, ['userId']);

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

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

390
391
392
393
394
395
396
397
        $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']
        ]);
398
399

        foreach ($allEntities as $key => $value) {
Damien's avatar
Damien committed
400
            $allEntities[$key]['serialId'] = $value['id'];
401
402
            $allEntities[$key]['id'] = $value['entity_id'];
            if (empty($value['parent_entity_id'])) {
403
                $allEntities[$key]['parentSerialId'] = '#';
404
405
406
                $allEntities[$key]['parent'] = '#';
                $allEntities[$key]['icon'] = "fa fa-building";
            } else {
407
                $allEntities[$key]['parentSerialId'] = $value['parent_id'];
408
409
410
411
412
413
414
                $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
415
                $allEntities[$key]['state']['disabled'] = true;
416
            }
Damien's avatar
Damien committed
417
            $allEntities[$key]['state']['opened'] = true;
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
            $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'],
433
            'where'     => ['users_entities.entity_id = ?', 'users_entities.user_id = users.id', 'users.status != ?'],
Damien's avatar
Damien committed
434
            'data'      => [$aArgs['id'], 'DEL']
435
436
437
438
        ]);

        return $aUsers;
    }
Damien's avatar
Damien committed
439
440
441
442

    public static function getTypes()
    {
        $types = [];
Damien's avatar
Damien committed
443

444
        $loadedXml = CoreConfigModel::getXmlLoaded(['path' => 'config/typentity.xml']);
Damien's avatar
Damien committed
445
446
447
448
449
450
451
        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
452
453
454
455
456
            }
        }

        return $types;
    }
Damien's avatar
Damien committed
457
458
459
460

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

462
        $loadedXml = CoreConfigModel::getXmlLoaded(['path' => 'config/roles.xml']);
Damien's avatar
Damien committed
463
464
465
        if ($loadedXml) {
            foreach ($loadedXml->ROLES->ROLE as $value) {
                $roles[] = [
466
467
468
                    '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
469
                ];
Damien's avatar
Damien committed
470
471
472
473
474
            }
        }

        return $roles;
    }
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496

    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']]);
    }
Giovannoni Laurent's avatar
Giovannoni Laurent committed
497
}