Total Complexity | 59 |
Total Lines | 375 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like AgendaEvents often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AgendaEvents, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
32 | class AgendaEvents extends DolibarrApi |
||
33 | { |
||
34 | /** |
||
35 | * @var array $FIELDS Mandatory fields, checked when create and update object |
||
36 | */ |
||
37 | public static $FIELDS = array( |
||
38 | ); |
||
39 | |||
40 | /** |
||
41 | * @var ActionComm $actioncomm {@type ActionComm} |
||
42 | */ |
||
43 | public $actioncomm; |
||
44 | |||
45 | |||
46 | /** |
||
47 | * Constructor |
||
48 | */ |
||
49 | public function __construct() |
||
50 | { |
||
51 | global $db, $conf; |
||
52 | $this->db = $db; |
||
53 | $this->actioncomm = new ActionComm($this->db); |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * Get properties of a Agenda Events object |
||
58 | * |
||
59 | * Return an array with Agenda Events information |
||
60 | * |
||
61 | * @param int $id ID of Agenda Events |
||
62 | * @return Object Object with cleaned properties |
||
63 | * |
||
64 | * @throws RestException |
||
65 | */ |
||
66 | public function get($id) |
||
67 | { |
||
68 | if (!DolibarrApiAccess::$user->hasRight('agenda', 'myactions', 'read')) { |
||
|
|||
69 | throw new RestException(401, "Insufficient rights to read an event"); |
||
70 | } |
||
71 | if ($id === 0) { |
||
72 | $result = $this->actioncomm->initAsSpecimen(); |
||
73 | } else { |
||
74 | $result = $this->actioncomm->fetch($id); |
||
75 | if ($result) { |
||
76 | $this->actioncomm->fetch_optionals(); |
||
77 | $this->actioncomm->fetchObjectLinked(); |
||
78 | } |
||
79 | } |
||
80 | if (!$result) { |
||
81 | throw new RestException(404, 'Agenda Events not found'); |
||
82 | } |
||
83 | |||
84 | if (!DolibarrApiAccess::$user->hasRight('agenda', 'allactions', 'read') && $this->actioncomm->userownerid != DolibarrApiAccess::$user->id) { |
||
85 | throw new RestException(401, 'Insufficient rights to read event of this owner id. Your id is ' . DolibarrApiAccess::$user->id); |
||
86 | } |
||
87 | |||
88 | if (!DolibarrApi::_checkAccessToResource('agenda', $this->actioncomm->id, 'actioncomm', '', 'fk_soc', 'id')) { |
||
89 | throw new RestException(401, 'Access not allowed for login ' . DolibarrApiAccess::$user->login); |
||
90 | } |
||
91 | return $this->_cleanObjectDatas($this->actioncomm); |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * List Agenda Events |
||
96 | * |
||
97 | * Get a list of Agenda Events |
||
98 | * |
||
99 | * @param string $sortfield Sort field |
||
100 | * @param string $sortorder Sort order |
||
101 | * @param int $limit Limit for list |
||
102 | * @param int $page Page number |
||
103 | * @param string $user_ids User ids filter field (owners of event). Example: '1' or '1,2,3' {@pattern /^[0-9,]*$/i} |
||
104 | * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.label:like:'%dol%') and (t.datec:<:'20160101')" |
||
105 | * @param string $properties Restrict the data returned to these properties. Ignored if empty. Comma separated list of properties names |
||
106 | * @return array Array of Agenda Events objects |
||
107 | */ |
||
108 | public function index($sortfield = "t.id", $sortorder = 'ASC', $limit = 100, $page = 0, $user_ids = '', $sqlfilters = '', $properties = '') |
||
109 | { |
||
110 | global $db, $conf; |
||
111 | |||
112 | $obj_ret = array(); |
||
113 | |||
114 | if (!DolibarrApiAccess::$user->hasRight('agenda', 'myactions', 'read')) { |
||
115 | throw new RestException(401, "Insufficient rights to read events"); |
||
116 | } |
||
117 | |||
118 | // case of external user |
||
119 | $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : 0; |
||
120 | |||
121 | // If the internal user must only see his customers, force searching by him |
||
122 | $search_sale = 0; |
||
123 | if (!DolibarrApiAccess::$user->hasRight('societe', 'client', 'voir') && !$socid) { |
||
124 | $search_sale = DolibarrApiAccess::$user->id; |
||
125 | } |
||
126 | if (!isModEnabled('societe')) { |
||
127 | $search_sale = 0; // If module thirdparty not enabled, sale representative is something that does not exists |
||
128 | } |
||
129 | |||
130 | $sql = "SELECT t.id as rowid"; |
||
131 | $sql .= " FROM " . MAIN_DB_PREFIX . "actioncomm AS t"; |
||
132 | $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "actioncomm_extrafields AS ef ON (ef.fk_object = t.id)"; // Modification VMR Global Solutions to include extrafields as search parameters in the API GET call, so we will be able to filter on extrafields |
||
133 | $sql .= ' WHERE t.entity IN (' . getEntity('agenda') . ')'; |
||
134 | if ($user_ids) { |
||
135 | $sql .= " AND t.fk_user_action IN (" . $this->db->sanitize($user_ids) . ")"; |
||
136 | } |
||
137 | if ($socid > 0) { |
||
138 | $sql .= " AND t.fk_soc = " . ((int) $socid); |
||
139 | } |
||
140 | // Search on sale representative |
||
141 | if ($search_sale && $search_sale != '-1') { |
||
142 | if ($search_sale == -2) { |
||
143 | $sql .= " AND NOT EXISTS (SELECT sc.fk_soc FROM " . MAIN_DB_PREFIX . "societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)"; |
||
144 | } elseif ($search_sale > 0) { |
||
145 | $sql .= " AND EXISTS (SELECT sc.fk_soc FROM " . MAIN_DB_PREFIX . "societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc AND sc.fk_user = " . ((int) $search_sale) . ")"; |
||
146 | } |
||
147 | } |
||
148 | // Add sql filters |
||
149 | if ($sqlfilters) { |
||
150 | $errormessage = ''; |
||
151 | $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage); |
||
152 | if ($errormessage) { |
||
153 | throw new RestException(400, 'Error when validating parameter sqlfilters -> ' . $errormessage); |
||
154 | } |
||
155 | } |
||
156 | |||
157 | $sql .= $this->db->order($sortfield, $sortorder); |
||
158 | if ($limit) { |
||
159 | if ($page < 0) { |
||
160 | $page = 0; |
||
161 | } |
||
162 | $offset = $limit * $page; |
||
163 | |||
164 | $sql .= $this->db->plimit($limit + 1, $offset); |
||
165 | } |
||
166 | |||
167 | $result = $this->db->query($sql); |
||
168 | |||
169 | if ($result) { |
||
170 | $i = 0; |
||
171 | $num = $this->db->num_rows($result); |
||
172 | $min = min($num, ($limit <= 0 ? $num : $limit)); |
||
173 | while ($i < $min) { |
||
174 | $obj = $this->db->fetch_object($result); |
||
175 | $actioncomm_static = new ActionComm($this->db); |
||
176 | if ($actioncomm_static->fetch($obj->rowid)) { |
||
177 | $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($actioncomm_static), $properties); |
||
178 | } |
||
179 | $i++; |
||
180 | } |
||
181 | } else { |
||
182 | throw new RestException(503, 'Error when retrieve Agenda Event list : ' . $this->db->lasterror()); |
||
183 | } |
||
184 | |||
185 | return $obj_ret; |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * Create Agenda Event object |
||
190 | * |
||
191 | * @param array $request_data Request data |
||
192 | * @return int ID of Agenda Event |
||
193 | */ |
||
194 | public function post($request_data = null) |
||
195 | { |
||
196 | if (!DolibarrApiAccess::$user->hasRight('agenda', 'myactions', 'create')) { |
||
197 | throw new RestException(401, "Insufficient rights to create your Agenda Event"); |
||
198 | } |
||
199 | if (!DolibarrApiAccess::$user->hasRight('agenda', 'allactions', 'create') && DolibarrApiAccess::$user->id != $request_data['userownerid']) { |
||
200 | throw new RestException(401, "Insufficient rights to create an Agenda Event for owner id " . $request_data['userownerid'] . ' Your id is ' . DolibarrApiAccess::$user->id); |
||
201 | } |
||
202 | |||
203 | // Check mandatory fields |
||
204 | $result = $this->_validate($request_data); |
||
205 | |||
206 | foreach ($request_data as $field => $value) { |
||
207 | if ($field === 'caller') { |
||
208 | // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again with the caller |
||
209 | $this->actioncomm->context['caller'] = $request_data['caller']; |
||
210 | continue; |
||
211 | } |
||
212 | |||
213 | $this->actioncomm->$field = $this->_checkValForAPI($field, $value, $this->actioncomm); |
||
214 | } |
||
215 | /*if (isset($request_data["lines"])) { |
||
216 | $lines = array(); |
||
217 | foreach ($request_data["lines"] as $line) { |
||
218 | array_push($lines, (object) $line); |
||
219 | } |
||
220 | $this->expensereport->lines = $lines; |
||
221 | }*/ |
||
222 | |||
223 | if ($this->actioncomm->create(DolibarrApiAccess::$user) < 0) { |
||
224 | throw new RestException(500, "Error creating event", array_merge(array($this->actioncomm->error), $this->actioncomm->errors)); |
||
225 | } |
||
226 | |||
227 | return $this->actioncomm->id; |
||
228 | } |
||
229 | |||
230 | |||
231 | /** |
||
232 | * Update Agenda Event general fields |
||
233 | * |
||
234 | * @param int $id Id of Agenda Event to update |
||
235 | * @param array $request_data Datas |
||
236 | * @return Object|false Object with cleaned properties |
||
237 | */ |
||
238 | public function put($id, $request_data = null) |
||
278 | } |
||
279 | |||
280 | /** |
||
281 | * Delete Agenda Event |
||
282 | * |
||
283 | * @param int $id Agenda Event ID |
||
284 | * |
||
285 | * @return array |
||
286 | */ |
||
287 | public function delete($id) |
||
288 | { |
||
289 | if (!DolibarrApiAccess::$user->hasRight('agenda', 'myactions', 'delete')) { |
||
290 | throw new RestException(401, "Insufficient rights to delete your Agenda Event"); |
||
291 | } |
||
292 | |||
293 | $result = $this->actioncomm->fetch($id); |
||
294 | if ($result) { |
||
295 | $this->actioncomm->fetch_optionals(); |
||
296 | $this->actioncomm->fetch_userassigned(); |
||
297 | $this->actioncomm->oldcopy = clone $this->actioncomm; |
||
298 | } |
||
299 | |||
300 | if (!DolibarrApiAccess::$user->hasRight('agenda', 'allactions', 'delete') && DolibarrApiAccess::$user->id != $this->actioncomm->userownerid) { |
||
301 | throw new RestException(401, "Insufficient rights to delete an Agenda Event of owner id " . $this->actioncomm->userownerid . ' Your id is ' . DolibarrApiAccess::$user->id); |
||
302 | } |
||
303 | |||
304 | if (!$result) { |
||
305 | throw new RestException(404, 'Agenda Event not found'); |
||
306 | } |
||
307 | |||
308 | if (!DolibarrApi::_checkAccessToResource('actioncomm', $this->actioncomm->id, 'actioncomm', '', 'fk_soc', 'id')) { |
||
309 | throw new RestException(401, 'Access not allowed for login ' . DolibarrApiAccess::$user->login); |
||
310 | } |
||
311 | |||
312 | if (!$this->actioncomm->delete(DolibarrApiAccess::$user)) { |
||
313 | throw new RestException(500, 'Error when delete Agenda Event : ' . $this->actioncomm->error); |
||
314 | } |
||
315 | |||
316 | return array( |
||
317 | 'success' => array( |
||
318 | 'code' => 200, |
||
319 | 'message' => 'Agenda Event deleted' |
||
320 | ) |
||
321 | ); |
||
322 | } |
||
323 | |||
324 | /** |
||
325 | * Validate fields before create or update object |
||
326 | * |
||
327 | * @param array $data Array with data to verify |
||
328 | * @return array |
||
329 | * @throws RestException |
||
330 | */ |
||
331 | private function _validate($data) |
||
341 | } |
||
342 | |||
343 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore |
||
344 | /** |
||
345 | * Clean sensible object datas |
||
346 | * |
||
347 | * @param Object $object Object to clean |
||
348 | * @return Object Object with cleaned properties |
||
349 | */ |
||
350 | protected function _cleanObjectDatas($object) |
||
409 |