1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Jitamin. |
5
|
|
|
* |
6
|
|
|
* Copyright (C) Jitamin Team |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Jitamin\Model; |
13
|
|
|
|
14
|
|
|
use Jitamin\Foundation\Database\Model; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Action Parameter Model. |
18
|
|
|
*/ |
19
|
|
|
class ActionParameterModel extends Model |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* SQL table name. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
const TABLE = 'action_has_params'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Get all action params. |
30
|
|
|
* |
31
|
|
|
* @return array |
32
|
|
|
*/ |
33
|
|
|
public function getAll() |
34
|
|
|
{ |
35
|
|
|
$params = $this->db->table(self::TABLE)->findAll(); |
|
|
|
|
36
|
|
|
|
37
|
|
|
return $this->toDictionary($params); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get all params for a list of actions. |
42
|
|
|
* |
43
|
|
|
* @param array $action_ids |
44
|
|
|
* |
45
|
|
|
* @return array |
46
|
|
|
*/ |
47
|
|
|
public function getAllByActions(array $action_ids) |
48
|
|
|
{ |
49
|
|
|
$params = $this->db->table(self::TABLE)->in('action_id', $action_ids)->findAll(); |
|
|
|
|
50
|
|
|
|
51
|
|
|
return $this->toDictionary($params); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Build params dictionary. |
56
|
|
|
* |
57
|
|
|
* @param array $params |
58
|
|
|
* |
59
|
|
|
* @return array |
60
|
|
|
*/ |
61
|
|
|
private function toDictionary(array $params) |
62
|
|
|
{ |
63
|
|
|
$result = []; |
64
|
|
|
|
65
|
|
|
foreach ($params as $param) { |
66
|
|
|
$result[$param['action_id']][$param['name']] = $param['value']; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $result; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get all action params for a given action. |
74
|
|
|
* |
75
|
|
|
* @param int $action_id |
76
|
|
|
* |
77
|
|
|
* @return array |
78
|
|
|
*/ |
79
|
|
|
public function getAllByAction($action_id) |
80
|
|
|
{ |
81
|
|
|
return $this->db->hashtable(self::TABLE)->eq('action_id', $action_id)->getAll('name', 'value'); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Insert new parameters for an action. |
86
|
|
|
* |
87
|
|
|
* @param int $action_id |
88
|
|
|
* @param array $values |
89
|
|
|
* |
90
|
|
|
* @return bool |
91
|
|
|
*/ |
92
|
|
|
public function create($action_id, array $values) |
93
|
|
|
{ |
94
|
|
|
foreach ($values['params'] as $name => $value) { |
95
|
|
|
$param = [ |
96
|
|
|
'action_id' => $action_id, |
97
|
|
|
'name' => $name, |
98
|
|
|
'value' => $value, |
99
|
|
|
]; |
100
|
|
|
|
101
|
|
|
if (!$this->db->table(self::TABLE)->save($param)) { |
|
|
|
|
102
|
|
|
return false; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return true; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Duplicate action parameters. |
111
|
|
|
* |
112
|
|
|
* @param int $project_id |
113
|
|
|
* @param int $action_id |
114
|
|
|
* @param array $params |
115
|
|
|
* |
116
|
|
|
* @return bool |
117
|
|
|
*/ |
118
|
|
|
public function duplicateParameters($project_id, $action_id, array $params) |
119
|
|
|
{ |
120
|
|
|
foreach ($params as $name => $value) { |
121
|
|
|
$value = $this->resolveParameter($project_id, $name, $value); |
122
|
|
|
|
123
|
|
|
if ($value === false) { |
124
|
|
|
$this->logger->error('ActionParameter::duplicateParameters => unable to resolve '.$name.'='.$value); |
|
|
|
|
125
|
|
|
|
126
|
|
|
return false; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$values = [ |
130
|
|
|
'action_id' => $action_id, |
131
|
|
|
'name' => $name, |
132
|
|
|
'value' => $value, |
133
|
|
|
]; |
134
|
|
|
|
135
|
|
|
if (!$this->db->table(self::TABLE)->insert($values)) { |
|
|
|
|
136
|
|
|
return false; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return true; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Resolve action parameter values according to another project. |
145
|
|
|
* |
146
|
|
|
* @param int $project_id |
147
|
|
|
* @param string $name |
148
|
|
|
* @param string $value |
149
|
|
|
* |
150
|
|
|
* @return mixed |
151
|
|
|
*/ |
152
|
|
|
private function resolveParameter($project_id, $name, $value) |
153
|
|
|
{ |
154
|
|
|
switch ($name) { |
155
|
|
|
case 'project_id': |
156
|
|
|
return $value != $project_id ? $value : false; |
157
|
|
|
case 'category_id': |
158
|
|
|
return $this->categoryModel->getIdByName($project_id, $this->categoryModel->getNameById($value)) ?: false; |
|
|
|
|
159
|
|
|
case 'src_column_id': |
160
|
|
|
case 'dest_column_id': |
161
|
|
|
case 'dst_column_id': |
162
|
|
|
case 'column_id': |
163
|
|
|
$column = $this->columnModel->getById($value); |
|
|
|
|
164
|
|
|
|
165
|
|
|
return empty($column) ? false : $this->columnModel->getColumnIdByTitle($project_id, $column['title']) ?: false; |
|
|
|
|
166
|
|
|
case 'user_id': |
167
|
|
|
case 'owner_id': |
168
|
|
|
return $this->projectPermissionModel->isAssignable($project_id, $value) ? $value : false; |
|
|
|
|
169
|
|
|
case 'swimlane_id': |
170
|
|
|
$column = $this->swimlaneModel->getById($value); |
|
|
|
|
171
|
|
|
|
172
|
|
|
return empty($column) ? false : $this->swimlaneModel->getIdByName($project_id, $column['name']) ?: false; |
|
|
|
|
173
|
|
|
default: |
174
|
|
|
return $value; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.