Newer
Older
<?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.
*
*/
/**
* @brief Entities Model
* @author dev@maarch.org
* @ingroup entities
*/
namespace Entities\Models;
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
require_once 'apps/maarch_entreprise/services/Table.php';
class EntitiesModelAbstract extends \Apps_Table_Service
{
public static function getByEmail(array $aArgs = [])
{
static::checkRequired($aArgs, ['email']);
static::checkString($aArgs, ['email']);
$aReturn = static::select([
'select' => empty($aArgs['select']) ? ['*'] : $aArgs['select'],
'table' => ['entities'],
'where' => ['email = ? and enabled = ?'],
'data' => [$aArgs['email'], 'Y'],
'limit' => 1,
]);
return $aReturn;
}
public static function getByUserId(array $aArgs = [])
{
static::checkRequired($aArgs, ['user_id']);
static::checkString($aArgs, ['user_id']);
$aReturn = static::select([
'select' => empty($aArgs['select']) ? ['*'] : $aArgs['select'],
'table' => ['users_entities'],
'where' => ['user_id = ? and primary_entity = ?'],
'data' => [$aArgs['user_id'], 'Y'],
'limit' => 1,
]);
return $aReturn;
}
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
private static function getEntityChilds(array $aArgs = [])
{
static::checkRequired($aArgs, ['entityId']);
static::checkString($aArgs, ['entityId']);
$aReturn = static::select([
'select' => ['entity_id'],
'table' => ['entities'],
'where' => ['parent_entity_id = ?'],
'data' => [$aArgs['entityId']]
]);
$entities = [$aArgs['entityId']];
foreach ($aReturn as $value) {
$entities = array_merge($entities, static::getEntityChilds(['entityId' => $value['entity_id']]));
}
return $entities;
}
public static function getAllEntitiesByUserId(array $aArgs = [])
{
static::checkRequired($aArgs, ['userId']);
static::checkString($aArgs, ['userId']);
$aReturn = UserModel::getEntitiesById(['userId' => $aArgs['userId']]);
$entities = [];
foreach ($aReturn as $value) {
$entities = array_merge($entities, static::getEntityChilds(['entityId' => $value['entity_id']]));
}
return array_unique($entities);
}