Total Complexity | 159 |
Total Lines | 1013 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Dolresource 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 Dolresource, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
39 | class Dolresource extends CommonObject |
||
40 | { |
||
41 | use CommonPeople; |
||
|
|||
42 | |||
43 | /** |
||
44 | * @var string ID to identify managed object |
||
45 | */ |
||
46 | public $element = 'dolresource'; |
||
47 | |||
48 | /** |
||
49 | * @var string Name of table without prefix where object is stored |
||
50 | */ |
||
51 | public $table_element = 'resource'; |
||
52 | |||
53 | /** |
||
54 | * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png |
||
55 | */ |
||
56 | public $picto = 'resource'; |
||
57 | |||
58 | /** |
||
59 | * @var string description |
||
60 | */ |
||
61 | public $description; |
||
62 | |||
63 | /** |
||
64 | * @var string telephone number |
||
65 | */ |
||
66 | public $phone; |
||
67 | |||
68 | /** |
||
69 | * @var int Maximum users |
||
70 | */ |
||
71 | public $max_users; |
||
72 | |||
73 | /** |
||
74 | * @var string ID |
||
75 | */ |
||
76 | public $fk_code_type_resource; |
||
77 | |||
78 | public $type_label; |
||
79 | |||
80 | /** |
||
81 | * @var int resource ID |
||
82 | * For resource-element link |
||
83 | * @see updateElementResource() |
||
84 | * @see fetchElementResource() |
||
85 | */ |
||
86 | public $resource_id; |
||
87 | |||
88 | /** |
||
89 | * @var string resource type |
||
90 | */ |
||
91 | public $resource_type; |
||
92 | |||
93 | /** |
||
94 | * @var int element ID |
||
95 | * For resource-element link |
||
96 | * @see updateElementResource() |
||
97 | * @see fetchElementResource() |
||
98 | */ |
||
99 | public $element_id; |
||
100 | |||
101 | /** |
||
102 | * @var string element type |
||
103 | */ |
||
104 | public $element_type; |
||
105 | |||
106 | /** |
||
107 | * @var int |
||
108 | */ |
||
109 | public $busy; |
||
110 | |||
111 | /** |
||
112 | * @var int |
||
113 | */ |
||
114 | public $mandatory; |
||
115 | |||
116 | /** |
||
117 | * @var int |
||
118 | */ |
||
119 | public $fulldayevent; |
||
120 | |||
121 | /** |
||
122 | * @var int ID |
||
123 | */ |
||
124 | public $fk_user_create; |
||
125 | |||
126 | /** |
||
127 | * Used by fetchElementResource() to return an object |
||
128 | */ |
||
129 | public $objelement; |
||
130 | |||
131 | /** |
||
132 | * @var array Cache of type of resources. TODO Use $conf->cache['type_of_resources'] instead |
||
133 | */ |
||
134 | public $cache_code_type_resource; |
||
135 | |||
136 | /** |
||
137 | * @var static Clone of object before changing it |
||
138 | */ |
||
139 | public $oldcopy; |
||
140 | |||
141 | |||
142 | /** |
||
143 | * Constructor |
||
144 | * |
||
145 | * @param DoliDB $db Database handler |
||
146 | */ |
||
147 | public function __construct(DoliDB $db) |
||
148 | { |
||
149 | $this->db = $db; |
||
150 | $this->status = 0; |
||
151 | |||
152 | $this->cache_code_type_resource = array(); |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * Create object in database |
||
157 | * |
||
158 | * @param User $user User that creates |
||
159 | * @param int $no_trigger 0=launch triggers after, 1=disable triggers |
||
160 | * @return int if KO: <0 || if OK: Id of created object |
||
161 | */ |
||
162 | public function create(User $user, int $no_trigger = 0) |
||
163 | { |
||
164 | $error = 0; |
||
165 | $this->date_creation = dol_now(); |
||
166 | |||
167 | // Clean parameters |
||
168 | $new_resource_values = [ |
||
169 | $this->ref, |
||
170 | $this->address, |
||
171 | $this->zip, |
||
172 | $this->town, |
||
173 | $this->country_id, |
||
174 | $this->state_id, |
||
175 | $this->description, |
||
176 | $this->phone, |
||
177 | $this->email, |
||
178 | $this->max_users, |
||
179 | $this->url, |
||
180 | $this->fk_code_type_resource, |
||
181 | $this->note_public, |
||
182 | $this->note_private, |
||
183 | ]; |
||
184 | foreach ($new_resource_values as $key => $value) { |
||
185 | if (isset($value)) { |
||
186 | $new_resource_values[$key] = trim($value); |
||
187 | } |
||
188 | } |
||
189 | |||
190 | // Insert request |
||
191 | $sql = "INSERT INTO " . MAIN_DB_PREFIX . $this->table_element . "("; |
||
192 | $sql .= "entity,"; |
||
193 | $sql .= "ref,"; |
||
194 | $sql .= "address,"; |
||
195 | $sql .= "zip,"; |
||
196 | $sql .= "town,"; |
||
197 | $sql .= "fk_country,"; |
||
198 | $sql .= "fk_state,"; |
||
199 | $sql .= "description,"; |
||
200 | $sql .= "phone,"; |
||
201 | $sql .= "email,"; |
||
202 | $sql .= "max_users,"; |
||
203 | $sql .= "url,"; |
||
204 | $sql .= "fk_code_type_resource,"; |
||
205 | $sql .= "note_public,"; |
||
206 | $sql .= "note_private, "; |
||
207 | $sql .= "datec, "; |
||
208 | $sql .= "fk_user_author "; |
||
209 | $sql .= ") VALUES ("; |
||
210 | $sql .= getEntity('resource') . ", "; |
||
211 | foreach ($new_resource_values as $value) { |
||
212 | $sql .= " " . ((isset($value) && $value > 0) ? "'" . $this->db->escape($value) . "'" : 'NULL') . ","; |
||
213 | } |
||
214 | $sql .= " '" . $this->db->idate($this->date_creation) . "',"; |
||
215 | $sql .= " " . (!empty($user->id) ? ((int) $user->id) : "null"); |
||
216 | $sql .= ")"; |
||
217 | |||
218 | // Database session |
||
219 | $this->db->begin(); |
||
220 | try { |
||
221 | dol_syslog(get_class($this) . "::create", LOG_DEBUG); |
||
222 | } catch (Exception $exception) { |
||
223 | error_log('dol_syslog error: ' . $exception->getMessage()); |
||
224 | } |
||
225 | $resql = $this->db->query($sql); |
||
226 | if (!$resql) { |
||
227 | $error++; |
||
228 | $this->errors[] = "Error " . $this->db->lasterror(); |
||
229 | } |
||
230 | |||
231 | if (!$error) { |
||
232 | $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . $this->table_element); |
||
233 | $result = $this->insertExtraFields(); |
||
234 | if ($result < 0) { |
||
235 | $error = -1; |
||
236 | } |
||
237 | } |
||
238 | |||
239 | if (!$error && !$no_trigger) { |
||
240 | $result = $this->call_trigger('RESOURCE_CREATE', $user); |
||
241 | if ($result < 0) { |
||
242 | $error = -1; |
||
243 | } |
||
244 | } |
||
245 | |||
246 | // Commit or rollback |
||
247 | if ($error) { |
||
248 | foreach ($this->errors as $errmsg) { |
||
249 | try { |
||
250 | dol_syslog(get_class($this) . "::create " . $errmsg, LOG_ERR); |
||
251 | } catch (Exception $exception) { |
||
252 | error_log('dol_syslog error: ' . $exception->getMessage()); |
||
253 | } |
||
254 | $this->error .= ($this->error ? ', ' . $errmsg : $errmsg); |
||
255 | } |
||
256 | $this->db->rollback(); |
||
257 | return $error; |
||
258 | } else { |
||
259 | $this->db->commit(); |
||
260 | return $this->id; |
||
261 | } |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * Load object into memory from database |
||
266 | * |
||
267 | * @param int $id Id of object |
||
268 | * @param string $ref Ref of object |
||
269 | * @return int if KO: <0 || if OK: >0 |
||
270 | */ |
||
271 | public function fetch(int $id, string $ref = '') |
||
272 | { |
||
273 | $sql = "SELECT"; |
||
274 | $sql .= " t.rowid,"; |
||
275 | $sql .= " t.entity,"; |
||
276 | $sql .= " t.ref,"; |
||
277 | $sql .= " t.address,"; |
||
278 | $sql .= " t.zip,"; |
||
279 | $sql .= " t.town,"; |
||
280 | $sql .= " t.fk_country,"; |
||
281 | $sql .= " t.fk_state,"; |
||
282 | $sql .= " t.description,"; |
||
283 | $sql .= " t.phone,"; |
||
284 | $sql .= " t.email,"; |
||
285 | $sql .= " t.max_users,"; |
||
286 | $sql .= " t.url,"; |
||
287 | $sql .= " t.fk_code_type_resource,"; |
||
288 | $sql .= " t.note_public,"; |
||
289 | $sql .= " t.note_private,"; |
||
290 | $sql .= " t.tms as date_modification,"; |
||
291 | $sql .= " t.datec as date_creation,"; |
||
292 | $sql .= " ty.label as type_label"; |
||
293 | $sql .= " FROM " . MAIN_DB_PREFIX . $this->table_element . " as t"; |
||
294 | $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "c_type_resource as ty ON ty.code=t.fk_code_type_resource"; |
||
295 | if ($id) { |
||
296 | $sql .= " WHERE t.rowid = " . ($id); |
||
297 | } else { |
||
298 | $sql .= " WHERE t.ref = '" . $this->db->escape($ref) . "'"; |
||
299 | } |
||
300 | |||
301 | dol_syslog(get_class($this) . "::fetch", LOG_DEBUG); |
||
302 | $resql = $this->db->query($sql); |
||
303 | if ($resql) { |
||
304 | if ($this->db->num_rows($resql)) { |
||
305 | $obj = $this->db->fetch_object($resql); |
||
306 | |||
307 | $this->id = $obj->rowid; |
||
308 | $this->entity = $obj->entity; |
||
309 | $this->ref = $obj->ref; |
||
310 | $this->address = $obj->address; |
||
311 | $this->zip = $obj->zip; |
||
312 | $this->town = $obj->town; |
||
313 | $this->country_id = $obj->fk_country; |
||
314 | $this->state_id = $obj->fk_state; |
||
315 | $this->description = $obj->description; |
||
316 | $this->phone = $obj->phone; |
||
317 | $this->email = $obj->email; |
||
318 | $this->max_users = $obj->max_users; |
||
319 | $this->url = $obj->url; |
||
320 | $this->fk_code_type_resource = $obj->fk_code_type_resource; |
||
321 | $this->note_public = $obj->note_public; |
||
322 | $this->note_private = $obj->note_private; |
||
323 | $this->date_creation = $this->db->jdate($obj->date_creation); |
||
324 | $this->date_modification = $this->db->jdate($obj->date_modification); |
||
325 | $this->type_label = $obj->type_label; |
||
326 | |||
327 | // Retrieve all extrafield |
||
328 | // fetch optionals attributes and labels |
||
329 | $this->fetch_optionals(); |
||
330 | } |
||
331 | $this->db->free($resql); |
||
332 | |||
333 | return $this->id; |
||
334 | } else { |
||
335 | $this->error = "Error " . $this->db->lasterror(); |
||
336 | dol_syslog(get_class($this) . "::fetch " . $this->error, LOG_ERR); |
||
337 | return -1; |
||
338 | } |
||
339 | } |
||
340 | |||
341 | |||
342 | /** |
||
343 | * Update object in database |
||
344 | * |
||
345 | * @param User|null $user User that modifies |
||
346 | * @param int $notrigger 0=launch triggers after, 1=disable triggers |
||
347 | * @return int if KO: <0 || if OK: >0 |
||
348 | */ |
||
349 | public function update(User $user = null, int $notrigger = 0) |
||
350 | { |
||
351 | global $conf, $langs; |
||
352 | $error = 0; |
||
353 | $this->date_modification = dol_now(); |
||
354 | |||
355 | // Clean parameters |
||
356 | if (isset($this->ref)) { |
||
357 | $this->ref = trim($this->ref); |
||
358 | } |
||
359 | if (isset($this->address)) { |
||
360 | $this->address = trim($this->address); |
||
361 | } |
||
362 | if (isset($this->zip)) { |
||
363 | $this->zip = trim($this->zip); |
||
364 | } |
||
365 | if (isset($this->town)) { |
||
366 | $this->town = trim($this->town); |
||
367 | } |
||
368 | if (!is_numeric($this->country_id)) { |
||
369 | $this->country_id = 0; |
||
370 | } |
||
371 | if (!is_numeric($this->state_id)) { |
||
372 | $this->state_id = 0; |
||
373 | } |
||
374 | if (isset($this->description)) { |
||
375 | $this->description = trim($this->description); |
||
376 | } |
||
377 | if (isset($this->phone)) { |
||
378 | $this->phone = trim($this->phone); |
||
379 | } |
||
380 | if (isset($this->email)) { |
||
381 | $this->email = trim($this->email); |
||
382 | } |
||
383 | if (!is_numeric($this->max_users)) { |
||
384 | $this->max_users = 0; |
||
385 | } |
||
386 | if (isset($this->url)) { |
||
387 | $this->url = trim($this->url); |
||
388 | } |
||
389 | if (isset($this->fk_code_type_resource)) { |
||
390 | $this->fk_code_type_resource = trim($this->fk_code_type_resource); |
||
391 | } |
||
392 | |||
393 | // $this->oldcopy should have been set by the caller of update (here properties were already modified) |
||
394 | if (is_null($this->oldcopy) || (is_object($this->oldcopy) && $this->oldcopy->isEmpty())) { |
||
395 | $this->oldcopy = dol_clone($this, 2); |
||
396 | } |
||
397 | |||
398 | // Update request |
||
399 | $sql = "UPDATE " . MAIN_DB_PREFIX . $this->table_element . " SET"; |
||
400 | $sql .= " ref=" . (isset($this->ref) ? "'" . $this->db->escape($this->ref) . "'" : "null") . ","; |
||
401 | $sql .= " address=" . (isset($this->address) ? "'" . $this->db->escape($this->address) . "'" : "null") . ","; |
||
402 | $sql .= " zip=" . (isset($this->zip) ? "'" . $this->db->escape($this->zip) . "'" : "null") . ","; |
||
403 | $sql .= " town=" . (isset($this->town) ? "'" . $this->db->escape($this->town) . "'" : "null") . ","; |
||
404 | $sql .= " fk_country=" . ($this->country_id > 0 ? (int) $this->country_id : "null") . ","; |
||
405 | $sql .= " fk_state=" . ($this->state_id > 0 ? (int) $this->state_id : "null") . ","; |
||
406 | $sql .= " description=" . (isset($this->description) ? "'" . $this->db->escape($this->description) . "'" : "null") . ","; |
||
407 | $sql .= " phone=" . (isset($this->phone) ? "'" . $this->db->escape($this->phone) . "'" : "null") . ","; |
||
408 | $sql .= " email=" . (isset($this->email) ? "'" . $this->db->escape($this->email) . "'" : "null") . ","; |
||
409 | $sql .= " max_users=" . (isset($this->max_users) ? (int) $this->max_users : "null") . ","; |
||
410 | $sql .= " url=" . (isset($this->url) ? "'" . $this->db->escape($this->url) . "'" : "null") . ","; |
||
411 | $sql .= " fk_code_type_resource=" . (isset($this->fk_code_type_resource) ? "'" . $this->db->escape($this->fk_code_type_resource) . "'" : "null") . ","; |
||
412 | $sql .= " tms=" . ("'" . $this->db->idate($this->date_modification) . "',"); |
||
413 | $sql .= " fk_user_modif=" . (!empty($user->id) ? ((int) $user->id) : "null"); |
||
414 | $sql .= " WHERE rowid=" . ((int) $this->id); |
||
415 | |||
416 | $this->db->begin(); |
||
417 | |||
418 | dol_syslog(get_class($this) . "::update", LOG_DEBUG); |
||
419 | $resql = $this->db->query($sql); |
||
420 | if (!$resql) { |
||
421 | $error++; |
||
422 | $this->errors[] = "Error " . $this->db->lasterror(); |
||
423 | } |
||
424 | |||
425 | if (!$error) { |
||
426 | if (!$notrigger) { |
||
427 | // Call trigger |
||
428 | $result = $this->call_trigger('RESOURCE_MODIFY', $user); |
||
429 | if ($result < 0) { |
||
430 | $error++; |
||
431 | } |
||
432 | // End call triggers |
||
433 | } |
||
434 | } |
||
435 | |||
436 | if (!$error && (is_object($this->oldcopy) && $this->oldcopy->ref !== $this->ref)) { |
||
437 | // We remove directory |
||
438 | if (!empty($conf->resource->dir_output)) { |
||
439 | $olddir = $conf->resource->dir_output . "/" . dol_sanitizeFileName($this->oldcopy->ref); |
||
440 | $newdir = $conf->resource->dir_output . "/" . dol_sanitizeFileName($this->ref); |
||
441 | if (file_exists($olddir)) { |
||
442 | $res = @rename($olddir, $newdir); |
||
443 | if (!$res) { |
||
444 | $langs->load("errors"); |
||
445 | $this->error = $langs->trans('ErrorFailToRenameDir', $olddir, $newdir); |
||
446 | $error++; |
||
447 | } |
||
448 | } |
||
449 | } |
||
450 | } |
||
451 | |||
452 | if (!$error) { |
||
453 | // Actions on extra fields |
||
454 | $result = $this->insertExtraFields(); |
||
455 | if ($result < 0) { |
||
456 | $error++; |
||
457 | } |
||
458 | } |
||
459 | |||
460 | // Commit or rollback |
||
461 | if ($error) { |
||
462 | foreach ($this->errors as $errmsg) { |
||
463 | dol_syslog(get_class($this) . "::update " . $errmsg, LOG_ERR); |
||
464 | $this->error .= ($this->error ? ', ' . $errmsg : $errmsg); |
||
465 | } |
||
466 | $this->db->rollback(); |
||
467 | return -1 * $error; |
||
468 | } else { |
||
469 | $this->db->commit(); |
||
470 | return 1; |
||
471 | } |
||
472 | } |
||
473 | |||
474 | /** |
||
475 | * Load data of resource links into memory from database |
||
476 | * |
||
477 | * @param int $id Id of link element_resources |
||
478 | * @return int if KO: <0 || if OK: >0 |
||
479 | */ |
||
480 | public function fetchElementResource(int $id) |
||
481 | { |
||
482 | $sql = "SELECT"; |
||
483 | $sql .= " t.rowid,"; |
||
484 | $sql .= " t.resource_id,"; |
||
485 | $sql .= " t.resource_type,"; |
||
486 | $sql .= " t.element_id,"; |
||
487 | $sql .= " t.element_type,"; |
||
488 | $sql .= " t.busy,"; |
||
489 | $sql .= " t.mandatory,"; |
||
490 | $sql .= " t.fk_user_create,"; |
||
491 | $sql .= " t.tms as date_modification"; |
||
492 | $sql .= " FROM " . MAIN_DB_PREFIX . "element_resources as t"; |
||
493 | $sql .= " WHERE t.rowid = " . ($id); |
||
494 | |||
495 | dol_syslog(get_class($this) . "::fetch", LOG_DEBUG); |
||
496 | $resql = $this->db->query($sql); |
||
497 | if ($resql) { |
||
498 | if ($this->db->num_rows($resql)) { |
||
499 | $obj = $this->db->fetch_object($resql); |
||
500 | |||
501 | $this->id = $obj->rowid; |
||
502 | $this->resource_id = $obj->resource_id; |
||
503 | $this->resource_type = $obj->resource_type; |
||
504 | $this->element_id = $obj->element_id; |
||
505 | $this->element_type = $obj->element_type; |
||
506 | $this->busy = $obj->busy; |
||
507 | $this->mandatory = $obj->mandatory; |
||
508 | $this->fk_user_create = $obj->fk_user_create; |
||
509 | $this->date_modification = $obj->date_modification; |
||
510 | |||
511 | /*if ($obj->resource_id && $obj->resource_type) { |
||
512 | $this->objresource = fetchObjectByElement($obj->resource_id, $obj->resource_type); |
||
513 | }*/ |
||
514 | if ($obj->element_id && $obj->element_type) { |
||
515 | $this->objelement = fetchObjectByElement($obj->element_id, $obj->element_type); |
||
516 | } |
||
517 | } |
||
518 | $this->db->free($resql); |
||
519 | |||
520 | return $this->id; |
||
521 | } else { |
||
522 | $this->error = "Error " . $this->db->lasterror(); |
||
523 | return -1; |
||
524 | } |
||
525 | } |
||
526 | |||
527 | /** |
||
528 | * Delete a resource object |
||
529 | * |
||
530 | * @param User $user User making the change |
||
531 | * @param int $notrigger Disable all triggers |
||
532 | * @return int if OK: >0 || if KO: <0 |
||
533 | */ |
||
534 | public function delete(User $user, int $notrigger = 0) |
||
603 | } |
||
604 | } |
||
605 | |||
606 | /** |
||
607 | * Load resource objects into $this->lines |
||
608 | * |
||
609 | * @param string $sortorder Sort order |
||
610 | * @param string $sortfield Sort field |
||
611 | * @param int $limit Limit page |
||
612 | * @param int $offset Offset page |
||
613 | * @param string|array $filter Filter USF. |
||
614 | * @return int If KO: <0 || if OK number of lines loaded |
||
615 | */ |
||
616 | public function fetchAll(string $sortorder, string $sortfield, int $limit, int $offset, $filter = '') |
||
617 | { |
||
618 | require_once constant('DOL_DOCUMENT_ROOT') . '/core/class/extrafields.class.php'; |
||
619 | $extrafields = new ExtraFields($this->db); |
||
620 | |||
621 | $sql = "SELECT "; |
||
622 | $sql .= " t.rowid,"; |
||
623 | $sql .= " t.entity,"; |
||
624 | $sql .= " t.ref,"; |
||
625 | $sql .= " t.address,"; |
||
626 | $sql .= " t.zip,"; |
||
627 | $sql .= " t.town,"; |
||
628 | $sql .= " t.fk_country,"; |
||
629 | $sql .= " t.fk_state,"; |
||
630 | $sql .= " t.description,"; |
||
631 | $sql .= " t.phone,"; |
||
632 | $sql .= " t.email,"; |
||
633 | $sql .= " t.max_users,"; |
||
634 | $sql .= " t.url,"; |
||
635 | $sql .= " t.fk_code_type_resource,"; |
||
636 | $sql .= " t.tms as date_modification,"; |
||
637 | $sql .= " t.datec as date_creation,"; |
||
638 | // Add fields from extrafields |
||
639 | if (!empty($extrafields->attributes[$this->table_element]) && !empty($extrafields->attributes[$this->table_element]['label'])) { |
||
640 | foreach ($extrafields->attributes[$this->table_element]['label'] as $key => $val) { |
||
641 | $sql .= ($extrafields->attributes[$this->table_element]['type'][$key] != 'separate' ? "ef." . $key . " as options_" . $key . ', ' : ''); |
||
642 | } |
||
643 | } |
||
644 | $sql .= " ty.label as type_label"; |
||
645 | $sql .= " FROM " . MAIN_DB_PREFIX . $this->table_element . " as t"; |
||
646 | $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "c_type_resource as ty ON ty.code=t.fk_code_type_resource"; |
||
647 | $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . $this->table_element . "_extrafields as ef ON ef.fk_object=t.rowid"; |
||
648 | $sql .= " WHERE t.entity IN (" . getEntity('resource') . ")"; |
||
649 | |||
650 | // Manage filter |
||
651 | if (is_array($filter)) { |
||
652 | foreach ($filter as $key => $value) { |
||
653 | if (strpos($key, 'date')) { |
||
654 | $sql .= " AND " . $this->db->sanitize($key) . " = '" . $this->db->idate($value) . "'"; |
||
655 | } elseif (strpos($key, 'ef.') !== false) { |
||
656 | $sql .= " AND " . $this->db->sanitize($key) . " = " . ((float) $value); |
||
657 | } else { |
||
658 | $sql .= " AND " . $this->db->sanitize($key) . " LIKE '%" . $this->db->escape($this->db->escapeforlike($value)) . "%'"; |
||
659 | } |
||
660 | } |
||
661 | |||
662 | $filter = ''; |
||
663 | } |
||
664 | |||
665 | // Manage filter |
||
666 | $errormessage = ''; |
||
667 | $sql .= forgeSQLFromUniversalSearchCriteria($filter, $errormessage); |
||
668 | if ($errormessage) { |
||
669 | $this->errors[] = $errormessage; |
||
670 | dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR); |
||
671 | return -1; |
||
672 | } |
||
673 | |||
674 | $sql .= $this->db->order($sortfield, $sortorder); |
||
675 | if ($limit) { |
||
676 | $sql .= $this->db->plimit($limit, $offset); |
||
677 | } |
||
678 | |||
679 | dol_syslog(get_class($this) . "::fetchAll", LOG_DEBUG); |
||
680 | |||
681 | $this->lines = array(); |
||
682 | $resql = $this->db->query($sql); |
||
683 | if ($resql) { |
||
684 | $num = $this->db->num_rows($resql); |
||
685 | if ($num) { |
||
686 | while ($obj = $this->db->fetch_object($resql)) { |
||
687 | $line = new Dolresource($this->db); |
||
688 | $line->id = $obj->rowid; |
||
689 | $line->ref = $obj->ref; |
||
690 | $line->address = $obj->address; |
||
691 | $line->zip = $obj->zip; |
||
692 | $line->town = $obj->town; |
||
693 | $line->country_id = $obj->fk_country; |
||
694 | $line->state_id = $obj->fk_state; |
||
695 | $line->description = $obj->description; |
||
696 | $this->phone = $obj->phone; |
||
697 | $this->email = $obj->email; |
||
698 | $this->max_users = $obj->max_users; |
||
699 | $this->url = $obj->url; |
||
700 | $line->fk_code_type_resource = $obj->fk_code_type_resource; |
||
701 | $line->date_modification = $obj->date_modification; |
||
702 | $line->date_creation = $obj->date_creation; |
||
703 | $line->type_label = $obj->type_label; |
||
704 | |||
705 | // fetch optionals attributes and labels |
||
706 | |||
707 | $line->fetch_optionals(); |
||
708 | |||
709 | $this->lines[] = $line; |
||
710 | } |
||
711 | $this->db->free($resql); |
||
712 | } |
||
713 | return $num; |
||
714 | } else { |
||
715 | $this->error = $this->db->lasterror(); |
||
716 | return -1; |
||
717 | } |
||
718 | } |
||
719 | |||
720 | /** |
||
721 | * Update element resource in database |
||
722 | * |
||
723 | * @param User|null $user User that modifies |
||
724 | * @param int $notrigger 0=launch triggers after, 1=disable triggers |
||
725 | * @return int if KO: <0 || if OK: >0 |
||
726 | */ |
||
727 | public function updateElementResource(User $user = null, int $notrigger = 0) |
||
728 | { |
||
729 | $error = 0; |
||
730 | $this->date_modification = dol_now(); |
||
731 | |||
732 | // Clean parameters |
||
733 | if (!is_numeric($this->resource_id)) { |
||
734 | $this->resource_id = 0; |
||
735 | } |
||
736 | if (isset($this->resource_type)) { |
||
737 | $this->resource_type = trim($this->resource_type); |
||
738 | } |
||
739 | if (!is_numeric($this->element_id)) { |
||
740 | $this->element_id = 0; |
||
741 | } |
||
742 | if (isset($this->element_type)) { |
||
743 | $this->element_type = trim($this->element_type); |
||
744 | } |
||
745 | if (isset($this->busy)) { |
||
746 | $this->busy = (int) $this->busy; |
||
747 | } |
||
748 | if (isset($this->mandatory)) { |
||
749 | $this->mandatory = (int) $this->mandatory; |
||
750 | } |
||
751 | |||
752 | // Update request |
||
753 | $sql = "UPDATE " . MAIN_DB_PREFIX . "element_resources SET"; |
||
754 | $sql .= " resource_id = " . (isset($this->resource_id) ? (int) $this->resource_id : "null") . ","; |
||
755 | $sql .= " resource_type = " . (isset($this->resource_type) ? "'" . $this->db->escape($this->resource_type) . "'" : "null") . ","; |
||
756 | $sql .= " element_id = " . (isset($this->element_id) ? (int) $this->element_id : "null") . ","; |
||
757 | $sql .= " element_type = " . (isset($this->element_type) ? "'" . $this->db->escape($this->element_type) . "'" : "null") . ","; |
||
758 | $sql .= " busy = " . (isset($this->busy) ? (int) $this->busy : "null") . ","; |
||
759 | $sql .= " mandatory = " . (isset($this->mandatory) ? (int) $this->mandatory : "null") . ","; |
||
760 | $sql .= " tms = " . (dol_strlen($this->date_modification) != 0 ? "'" . $this->db->idate($this->date_modification) . "'" : 'null'); |
||
761 | $sql .= " WHERE rowid=" . ((int) $this->id); |
||
762 | |||
763 | $this->db->begin(); |
||
764 | |||
765 | dol_syslog(get_class($this) . "::update", LOG_DEBUG); |
||
766 | $resql = $this->db->query($sql); |
||
767 | if (!$resql) { |
||
768 | $error++; |
||
769 | $this->errors[] = "Error " . $this->db->lasterror(); |
||
770 | } |
||
771 | |||
772 | if (!$error) { |
||
773 | if (!$notrigger) { |
||
774 | // Call trigger |
||
775 | $result = $this->call_trigger('RESOURCE_MODIFY', $user); |
||
776 | if ($result < 0) { |
||
777 | $error++; |
||
778 | } |
||
779 | // End call triggers |
||
780 | } |
||
781 | } |
||
782 | |||
783 | // Commit or rollback |
||
784 | if ($error) { |
||
785 | foreach ($this->errors as $errmsg) { |
||
786 | dol_syslog(get_class($this) . "::update " . $errmsg, LOG_ERR); |
||
787 | $this->error .= ($this->error ? ', ' . $errmsg : $errmsg); |
||
788 | } |
||
789 | $this->db->rollback(); |
||
790 | return -1 * $error; |
||
791 | } else { |
||
792 | $this->db->commit(); |
||
793 | return 1; |
||
794 | } |
||
795 | } |
||
796 | |||
797 | |||
798 | /** |
||
799 | * Return an array with resources linked to the element |
||
800 | * |
||
801 | * @param string $element Element |
||
802 | * @param int $element_id Id |
||
803 | * @param string $resource_type Type |
||
804 | * @return array Array of resources |
||
805 | */ |
||
806 | public function getElementResources(string $element, int $element_id, string $resource_type = '') |
||
807 | { |
||
808 | // Links between objects are stored in this table |
||
809 | $sql = 'SELECT rowid, resource_id, resource_type, busy, mandatory'; |
||
810 | $sql .= ' FROM ' . MAIN_DB_PREFIX . 'element_resources'; |
||
811 | $sql .= " WHERE element_id=" . ((int) $element_id) . " AND element_type='" . $this->db->escape($element) . "'"; |
||
812 | if ($resource_type) { |
||
813 | $sql .= " AND resource_type LIKE '%" . $this->db->escape($resource_type) . "%'"; |
||
814 | } |
||
815 | $sql .= ' ORDER BY resource_type'; |
||
816 | |||
817 | dol_syslog(get_class($this) . "::getElementResources", LOG_DEBUG); |
||
818 | |||
819 | $resources = array(); |
||
820 | $resql = $this->db->query($sql); |
||
821 | if ($resql) { |
||
822 | $num = $this->db->num_rows($resql); |
||
823 | $i = 0; |
||
824 | while ($i < $num) { |
||
825 | $obj = $this->db->fetch_object($resql); |
||
826 | |||
827 | $resources[$i] = array( |
||
828 | 'rowid' => $obj->rowid, |
||
829 | 'resource_id' => $obj->resource_id, |
||
830 | 'resource_type' => $obj->resource_type, |
||
831 | 'busy' => $obj->busy, |
||
832 | 'mandatory' => $obj->mandatory |
||
833 | ); |
||
834 | $i++; |
||
835 | } |
||
836 | } |
||
837 | |||
838 | return $resources; |
||
839 | } |
||
840 | |||
841 | /** |
||
842 | * Return an int number of resources linked to the element |
||
843 | * |
||
844 | * @param string $elementType Element type |
||
845 | * @param int $elementId Element id |
||
846 | * @return int Nb of resources loaded |
||
847 | */ |
||
848 | public function fetchElementResources(string $elementType, int $elementId) |
||
849 | { |
||
850 | $resources = $this->getElementResources($elementType, $elementId); |
||
851 | $i = 0; |
||
852 | foreach ($resources as $resource) { |
||
853 | $this->lines[$i] = fetchObjectByElement($resource['resource_id'], $resource['resource_type']); |
||
854 | $i++; |
||
855 | } |
||
856 | return $i; |
||
857 | } |
||
858 | |||
859 | /** |
||
860 | * Load in cache resource type code (setup in dictionary) |
||
861 | * |
||
862 | * @return int if KO: <0 || if already loaded: 0 || Number of lines loaded |
||
863 | */ |
||
864 | public function loadCacheCodeTypeResource() |
||
865 | { |
||
866 | global $langs; |
||
867 | |||
868 | if (is_array($this->cache_code_type_resource) && count($this->cache_code_type_resource)) { |
||
869 | return 0; // Cache deja charge |
||
870 | } |
||
871 | |||
872 | $sql = "SELECT rowid, code, label, active"; |
||
873 | $sql .= " FROM " . MAIN_DB_PREFIX . "c_type_resource"; |
||
874 | $sql .= " WHERE active > 0"; |
||
875 | $sql .= " ORDER BY rowid"; |
||
876 | dol_syslog(get_class($this) . "::load_cache_code_type_resource", LOG_DEBUG); |
||
877 | $resql = $this->db->query($sql); |
||
878 | if ($resql) { |
||
879 | $num = $this->db->num_rows($resql); |
||
880 | $i = 0; |
||
881 | while ($i < $num) { |
||
882 | $obj = $this->db->fetch_object($resql); |
||
883 | |||
884 | $label = ($langs->trans("ResourceTypeShort" . $obj->code) != "ResourceTypeShort" . $obj->code ? $langs->trans("ResourceTypeShort" . $obj->code) : ($obj->label != '-' ? $obj->label : '')); |
||
885 | $this->cache_code_type_resource[$obj->rowid]['code'] = $obj->code; |
||
886 | $this->cache_code_type_resource[$obj->rowid]['label'] = $label; |
||
887 | $this->cache_code_type_resource[$obj->rowid]['active'] = $obj->active; |
||
888 | $i++; |
||
889 | } |
||
890 | return $num; |
||
891 | } else { |
||
892 | dol_print_error($this->db); |
||
893 | return -1; |
||
894 | } |
||
895 | } |
||
896 | |||
897 | /** |
||
898 | * getTooltipContentArray |
||
899 | * @since v18 |
||
900 | * @param array $params ex option, infologin |
||
901 | * @return array |
||
902 | */ |
||
903 | public function getTooltipContentArray($params) |
||
904 | { |
||
905 | global $langs; |
||
906 | |||
907 | $langs->load('resource'); |
||
908 | |||
909 | $datas = []; |
||
910 | |||
911 | $datas['picto'] = img_picto('', $this->picto) . ' <u>' . $langs->trans("Resource") . '</u>'; |
||
912 | $datas['ref'] = '<br><b>' . $langs->trans('Ref') . ':</b> ' . $this->ref; |
||
913 | /*if (isset($this->status)) { |
||
914 | $datas['status'] = '<br><b>' . $langs->trans("Status").":</b> ".$this->getLibStatut(5); |
||
915 | }*/ |
||
916 | if (isset($this->type_label)) { |
||
917 | $datas['label'] = '<br><b>' . $langs->trans("ResourceType") . ":</b> " . $this->type_label; |
||
918 | } |
||
919 | |||
920 | return $datas; |
||
921 | } |
||
922 | |||
923 | /** |
||
924 | * Return clickable link of object (with optional picto) |
||
925 | * |
||
926 | * @param int $withpicto Add picto into link |
||
927 | * @param string $option Where point the link ('compta', 'expedition', 'document', ...) |
||
928 | * @param string $get_params Parameters added to url |
||
929 | * @param int $notooltip 1=Disable tooltip |
||
930 | * @param string $morecss Add more css on link |
||
931 | * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking |
||
932 | * @return string String with URL |
||
933 | */ |
||
934 | public function getNomUrl(int $withpicto = 0, string $option = '', string $get_params = '', int $notooltip = 0, string $morecss = '', int $save_lastsearch_value = -1) |
||
935 | { |
||
936 | global $langs, $hookmanager, $action; |
||
937 | |||
938 | $result = ''; |
||
939 | $params = [ |
||
940 | 'id' => $this->id, |
||
941 | 'objecttype' => $this->element, |
||
942 | ]; |
||
943 | $classfortooltip = 'classfortooltip'; |
||
944 | $dataparams = ''; |
||
945 | if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) { |
||
946 | $classfortooltip = 'classforajaxtooltip'; |
||
947 | $dataparams = ' data-params="' . dol_escape_htmltag(json_encode($params)) . '"'; |
||
948 | $label = ''; |
||
949 | } else { |
||
950 | $label = implode($this->getTooltipContentArray($params)); |
||
951 | } |
||
952 | |||
953 | $url = constant('BASE_URL') . '/resource/card.php?id=' . $this->id; |
||
954 | |||
955 | if ($option != 'nolink') { |
||
956 | // Add param to save lastsearch_values or not |
||
957 | $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0); |
||
958 | if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) { |
||
959 | $add_save_lastsearch_values = 1; |
||
960 | } |
||
961 | if ($add_save_lastsearch_values) { |
||
962 | $url .= '&save_lastsearch_values=1'; |
||
963 | } |
||
964 | } |
||
965 | |||
966 | $linkclose = ''; |
||
967 | if (empty($notooltip)) { |
||
968 | if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) { |
||
969 | $label = $langs->trans("ShowMyObject"); |
||
970 | $linkclose .= ' alt="' . dol_escape_htmltag($label, 1) . '"'; |
||
971 | } |
||
972 | $linkclose .= ($label ? ' title="' . dol_escape_htmltag($label, 1) . '"' : ' title="tocomplete"'); |
||
973 | $linkclose .= $dataparams . ' class="' . $classfortooltip . ($morecss ? ' ' . $morecss : '') . '"'; |
||
974 | } else { |
||
975 | $linkclose = ($morecss ? ' class="' . $morecss . '"' : ''); |
||
976 | } |
||
977 | |||
978 | $linkstart = '<a href="' . $url . $get_params . '"'; |
||
979 | $linkstart .= $linkclose . '>'; |
||
980 | $linkend = '</a>'; |
||
981 | /*$linkstart = '<a href="'.DOL_URL_ROOT.'/resource/card.php?id='.$this->id.$get_params.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">'; |
||
982 | $linkend = '</a>';*/ |
||
983 | |||
984 | $result .= $linkstart; |
||
985 | if ($withpicto) { |
||
986 | $result .= img_object(($notooltip ? '' : $label), ($this->picto ?: 'generic'), (($withpicto != 2) ? 'class="paddingright"' : ''), 0, 0, $notooltip ? 0 : 1); |
||
987 | } |
||
988 | if ($withpicto != 2) { |
||
989 | $result .= $this->ref; |
||
990 | } |
||
991 | $result .= $linkend; |
||
992 | |||
993 | $hookmanager->initHooks(array($this->element . 'dao')); |
||
994 | $parameters = array('id' => $this->id, 'getnomurl' => &$result); |
||
995 | $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks |
||
996 | if ($reshook > 0) { |
||
997 | $result = $hookmanager->resPrint; |
||
998 | } else { |
||
999 | $result .= $hookmanager->resPrint; |
||
1000 | } |
||
1001 | return $result; |
||
1002 | } |
||
1003 | |||
1004 | |||
1005 | /** |
||
1006 | * Get status label |
||
1007 | * |
||
1008 | * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto, 6=Long label + Picto |
||
1009 | * @return string Label of status |
||
1010 | */ |
||
1011 | public function getLibStatut(int $mode = 0) |
||
1014 | } |
||
1015 | |||
1016 | /** |
||
1017 | * Get status |
||
1018 | * |
||
1019 | * @param int $status Id status |
||
1020 | * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto, 5=Long label + Picto |
||
1021 | * @return string Label of status |
||
1022 | */ |
||
1023 | public static function getLibStatusLabel(int $status, int $mode = 0) |
||
1024 | { |
||
1025 | return ''; |
||
1026 | } |
||
1027 | |||
1028 | /** |
||
1029 | * Load indicators this->nb for state board |
||
1030 | * |
||
1031 | * @return int if KO: <0 || if OK: >0 |
||
1032 | */ |
||
1033 | public function loadStateBoard() |
||
1052 | } |
||
1053 | } |
||
1054 | } |
||
1055 |