1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Gewaer\Models; |
6
|
|
|
|
7
|
|
|
use Phalcon\Di; |
8
|
|
|
use Gewaer\Exception\ModelException; |
9
|
|
|
|
10
|
|
|
class Resources extends AbstractModel |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* |
14
|
|
|
* @var integer |
15
|
|
|
*/ |
16
|
|
|
public $id; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
public $name; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
public $description; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* |
32
|
|
|
* @var integer |
33
|
|
|
*/ |
34
|
|
|
public $apps_id; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
public $created_at; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
public $updated_at; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* |
50
|
|
|
* @var integer |
51
|
|
|
*/ |
52
|
|
|
public $is_deleted; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Initialize method for model. |
56
|
|
|
*/ |
57
|
7 |
|
public function initialize() |
58
|
|
|
{ |
59
|
7 |
|
$this->setSource('resources'); |
60
|
|
|
|
61
|
7 |
|
$this->hasMany( |
62
|
7 |
|
'id', |
63
|
7 |
|
'Gewaer\Models\ResourcesAccesses', |
64
|
7 |
|
'resources_id', |
65
|
|
|
[ |
66
|
7 |
|
'alias' => 'accesses', |
67
|
|
|
'params' => [ |
68
|
7 |
|
'conditions' => 'apps_id = ' . $this->di->getApp()->getId() |
|
|
|
|
69
|
|
|
] |
70
|
|
|
] |
71
|
|
|
); |
72
|
7 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* is this name a resource? |
76
|
|
|
* |
77
|
|
|
* @param string $resourceName |
78
|
|
|
* @return boolean |
79
|
|
|
*/ |
80
|
1 |
|
public static function isResource(string $resourceName) : bool |
81
|
|
|
{ |
82
|
1 |
|
return (bool) self::count([ |
83
|
1 |
|
'conditions' => 'name = ?0 AND apps_id in (?1, ?2)', |
84
|
1 |
|
'bind' => [$resourceName, Di::getDefault()->getApp()->getId(), Apps::GEWAER_DEFAULT_APP_ID] |
85
|
|
|
]); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get a resource by it name |
90
|
|
|
* |
91
|
|
|
* @param string $resourceName |
92
|
|
|
* @return ResourcesDB |
|
|
|
|
93
|
|
|
*/ |
94
|
5 |
|
public static function getByName(string $resourceName) : Resources |
95
|
|
|
{ |
96
|
5 |
|
$resource = self::findFirst([ |
97
|
5 |
|
'conditions' => 'name = ?0 AND apps_id in (?1, ?2)', |
98
|
5 |
|
'bind' => [$resourceName, Di::getDefault()->getApp()->getId(), Apps::GEWAER_DEFAULT_APP_ID] |
99
|
|
|
]); |
100
|
|
|
|
101
|
5 |
|
if (!is_object($resource)) { |
102
|
|
|
throw new ModelException(_('Resource ' . $resourceName . ' not found on this app ' . Di::getDefault()->getApp()->getId())); |
103
|
|
|
} |
104
|
|
|
|
105
|
5 |
|
return $resource; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Returns table name mapped in the model. |
110
|
|
|
* |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
7 |
|
public function getSource(): string |
114
|
|
|
{ |
115
|
7 |
|
return 'resources'; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|