Total Complexity | 54 |
Total Lines | 520 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Establishment 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 Establishment, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
35 | class Establishment extends CommonObject |
||
36 | { |
||
37 | /** |
||
38 | * @var string ID to identify managed object |
||
39 | */ |
||
40 | public $element = 'establishment'; |
||
41 | |||
42 | /** |
||
43 | * @var string Name of table without prefix where object is stored |
||
44 | */ |
||
45 | public $table_element = 'establishment'; |
||
46 | |||
47 | /** |
||
48 | * @var string Name of subtable line |
||
49 | */ |
||
50 | public $table_element_line = ''; |
||
51 | |||
52 | /** |
||
53 | * @var string Field with ID of parent key if this field has a parent |
||
54 | */ |
||
55 | public $fk_element = 'fk_establishment'; |
||
56 | |||
57 | /** |
||
58 | * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png |
||
59 | */ |
||
60 | public $picto = 'establishment'; |
||
61 | |||
62 | /** |
||
63 | * @var int ID |
||
64 | */ |
||
65 | public $id; |
||
66 | |||
67 | /** |
||
68 | * @var string Ref |
||
69 | */ |
||
70 | public $ref; |
||
71 | |||
72 | /** |
||
73 | * @var int ID |
||
74 | */ |
||
75 | public $rowid; |
||
76 | |||
77 | /** |
||
78 | * @var string Label |
||
79 | */ |
||
80 | public $label; |
||
81 | |||
82 | /** |
||
83 | * @var string Address |
||
84 | */ |
||
85 | public $address; |
||
86 | |||
87 | /** |
||
88 | * @var string Zip |
||
89 | */ |
||
90 | public $zip; |
||
91 | |||
92 | /** |
||
93 | * @var string Town |
||
94 | */ |
||
95 | public $town; |
||
96 | |||
97 | /** |
||
98 | * @var int country id |
||
99 | */ |
||
100 | public $country_id; |
||
101 | |||
102 | /** |
||
103 | * @var int Status 0=open, 1=closed |
||
104 | */ |
||
105 | public $status; |
||
106 | |||
107 | /** |
||
108 | * @var int Entity |
||
109 | */ |
||
110 | public $entity; |
||
111 | |||
112 | /** |
||
113 | * @var int user mod id |
||
114 | */ |
||
115 | public $fk_user_mod; |
||
116 | |||
117 | /** |
||
118 | * @var int user author id |
||
119 | */ |
||
120 | public $fk_user_author; |
||
121 | |||
122 | /** |
||
123 | * @var int date create |
||
124 | */ |
||
125 | public $datec; |
||
126 | |||
127 | const STATUS_OPEN = 1; |
||
128 | const STATUS_CLOSED = 0; |
||
129 | |||
130 | |||
131 | public $fields = array( |
||
132 | 'rowid' => array('type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 10), |
||
133 | 'entity' => array('type' => 'integer', 'label' => 'Entity', 'default' => '1', 'enabled' => 1, 'visible' => -2, 'notnull' => 1, 'position' => 15, 'index' => 1), |
||
134 | 'ref' => array('type' => 'varchar(30)', 'label' => 'Ref', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'showoncombobox' => 1, 'position' => 20), |
||
135 | 'label' => array('type' => 'varchar(128)', 'label' => 'Label', 'enabled' => 1, 'visible' => -1, 'showoncombobox' => 2, 'position' => 22), |
||
136 | 'address' => array('type' => 'varchar(255)', 'label' => 'Address', 'enabled' => 1, 'visible' => -1, 'position' => 25), |
||
137 | 'zip' => array('type' => 'varchar(25)', 'label' => 'Zip', 'enabled' => 1, 'visible' => -1, 'position' => 30), |
||
138 | 'town' => array('type' => 'varchar(50)', 'label' => 'Town', 'enabled' => 1, 'visible' => -1, 'position' => 35), |
||
139 | 'fk_state' => array('type' => 'integer', 'label' => 'Fkstate', 'enabled' => 1, 'visible' => -1, 'position' => 40), |
||
140 | 'fk_country' => array('type' => 'integer', 'label' => 'Fkcountry', 'enabled' => 1, 'visible' => -1, 'position' => 45), |
||
141 | 'profid1' => array('type' => 'varchar(20)', 'label' => 'Profid1', 'enabled' => 1, 'visible' => -1, 'position' => 50), |
||
142 | 'profid2' => array('type' => 'varchar(20)', 'label' => 'Profid2', 'enabled' => 1, 'visible' => -1, 'position' => 55), |
||
143 | 'profid3' => array('type' => 'varchar(20)', 'label' => 'Profid3', 'enabled' => 1, 'visible' => -1, 'position' => 60), |
||
144 | 'phone' => array('type' => 'varchar(20)', 'label' => 'Phone', 'enabled' => 1, 'visible' => -1, 'position' => 65), |
||
145 | 'fk_user_author' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'Fkuserauthor', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 70), |
||
146 | 'fk_user_mod' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'Fkusermod', 'enabled' => 1, 'visible' => -1, 'position' => 75), |
||
147 | 'datec' => array('type' => 'datetime', 'label' => 'DateCreation', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 80), |
||
148 | 'tms' => array('type' => 'timestamp', 'label' => 'DateModification', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 85), |
||
149 | 'status' => array('type' => 'integer', 'label' => 'Status', 'enabled' => 1, 'visible' => -1, 'position' => 500), |
||
150 | ); |
||
151 | |||
152 | |||
153 | /** |
||
154 | * Constructor |
||
155 | * |
||
156 | * @param DoliDB $db Database handler |
||
|
|||
157 | */ |
||
158 | public function __construct($db) |
||
159 | { |
||
160 | $this->db = $db; |
||
161 | |||
162 | $this->ismultientitymanaged = 1; |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * Create object in database |
||
167 | * |
||
168 | * @param User $user User making creation |
||
169 | * @return int Return integer <0 if KO, >0 if OK |
||
170 | */ |
||
171 | public function create($user) |
||
172 | { |
||
173 | global $conf, $langs; |
||
174 | |||
175 | $error = 0; |
||
176 | $now = dol_now(); |
||
177 | |||
178 | // Clean parameters |
||
179 | $this->label = trim($this->label); |
||
180 | $this->address = trim($this->address); |
||
181 | $this->zip = trim($this->zip); |
||
182 | $this->town = trim($this->town); |
||
183 | |||
184 | if (empty($this->ref)) { |
||
185 | $this->ref = '(PROV)'; |
||
186 | } |
||
187 | |||
188 | $this->db->begin(); |
||
189 | |||
190 | $sql = "INSERT INTO " . MAIN_DB_PREFIX . "establishment ("; |
||
191 | $sql .= "ref"; |
||
192 | $sql .= ", label"; |
||
193 | $sql .= ", address"; |
||
194 | $sql .= ", zip"; |
||
195 | $sql .= ", town"; |
||
196 | $sql .= ", fk_country"; |
||
197 | $sql .= ", status"; |
||
198 | $sql .= ", entity"; |
||
199 | $sql .= ", datec"; |
||
200 | $sql .= ", fk_user_author"; |
||
201 | $sql .= ", fk_user_mod"; |
||
202 | $sql .= ") VALUES ("; |
||
203 | $sql .= "'" . $this->db->escape($this->ref) . "'"; |
||
204 | $sql .= ", '" . $this->db->escape($this->label) . "'"; |
||
205 | $sql .= ", '" . $this->db->escape($this->address) . "'"; |
||
206 | $sql .= ", '" . $this->db->escape($this->zip) . "'"; |
||
207 | $sql .= ", '" . $this->db->escape($this->town) . "'"; |
||
208 | $sql .= ", " . ((int) $this->country_id); |
||
209 | $sql .= ", " . ((int) $this->status); |
||
210 | $sql .= ", " . ((int) $conf->entity); |
||
211 | $sql .= ", '" . $this->db->idate($now) . "'"; |
||
212 | $sql .= ", " . ((int) $user->id); |
||
213 | $sql .= ", " . ((int) $user->id); |
||
214 | $sql .= ")"; |
||
215 | |||
216 | dol_syslog(get_class($this) . "::create", LOG_DEBUG); |
||
217 | $resql = $this->db->query($sql); |
||
218 | if (!$resql) { |
||
219 | $error++; |
||
220 | $this->errors[] = "Error " . $this->db->lasterror(); |
||
221 | } |
||
222 | |||
223 | // Commit or rollback |
||
224 | if ($error) { |
||
225 | foreach ($this->errors as $errmsg) { |
||
226 | dol_syslog(get_class($this) . "::create " . $errmsg, LOG_ERR); |
||
227 | $this->error .= ($this->error ? ', ' . $errmsg : $errmsg); |
||
228 | } |
||
229 | $this->db->rollback(); |
||
230 | return -1 * $error; |
||
231 | } else { |
||
232 | $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . 'establishment'); |
||
233 | |||
234 | $sql = 'UPDATE ' . MAIN_DB_PREFIX . "establishment SET ref = '" . $this->db->escape($this->id) . "'"; |
||
235 | $sql .= " WHERE rowid = " . ((int) $this->id); |
||
236 | $this->db->query($sql); |
||
237 | |||
238 | $this->db->commit(); |
||
239 | return $this->id; |
||
240 | } |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * Update record |
||
245 | * |
||
246 | * @param User $user User making update |
||
247 | * @return int Return integer <0 if KO, >0 if OK |
||
248 | */ |
||
249 | public function update($user) |
||
250 | { |
||
251 | global $langs; |
||
252 | |||
253 | // Check parameters |
||
254 | if (empty($this->label)) { |
||
255 | $this->error = 'ErrorBadParameter'; |
||
256 | return -1; |
||
257 | } |
||
258 | |||
259 | $this->db->begin(); |
||
260 | |||
261 | $sql = "UPDATE " . MAIN_DB_PREFIX . "establishment"; |
||
262 | $sql .= " SET ref = '" . $this->db->escape($this->ref) . "'"; |
||
263 | $sql .= ", label = '" . $this->db->escape($this->label) . "'"; |
||
264 | $sql .= ", address = '" . $this->db->escape($this->address) . "'"; |
||
265 | $sql .= ", zip = '" . $this->db->escape($this->zip) . "'"; |
||
266 | $sql .= ", town = '" . $this->db->escape($this->town) . "'"; |
||
267 | $sql .= ", fk_country = " . ($this->country_id > 0 ? $this->country_id : 'null'); |
||
268 | $sql .= ", status = " . ((int) $this->status); |
||
269 | $sql .= ", fk_user_mod = " . ((int) $user->id); |
||
270 | $sql .= ", entity = " . ((int) $this->entity); |
||
271 | $sql .= " WHERE rowid = " . ((int) $this->id); |
||
272 | |||
273 | dol_syslog(get_class($this) . "::update", LOG_DEBUG); |
||
274 | $result = $this->db->query($sql); |
||
275 | if ($result) { |
||
276 | $this->db->commit(); |
||
277 | return 1; |
||
278 | } else { |
||
279 | $this->error = $this->db->lasterror(); |
||
280 | $this->db->rollback(); |
||
281 | return -1; |
||
282 | } |
||
283 | } |
||
284 | |||
285 | /** |
||
286 | * Load an object from database |
||
287 | * |
||
288 | * @param int $id Id of record to load |
||
289 | * @return int Return integer <0 if KO, >=0 if OK |
||
290 | */ |
||
291 | public function fetch($id) |
||
292 | { |
||
293 | $sql = "SELECT e.rowid, e.ref, e.label, e.address, e.zip, e.town, e.status, e.fk_country as country_id, e.entity,"; |
||
294 | $sql .= ' c.code as country_code, c.label as country'; |
||
295 | $sql .= " FROM " . MAIN_DB_PREFIX . "establishment as e"; |
||
296 | $sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'c_country as c ON e.fk_country = c.rowid'; |
||
297 | $sql .= " WHERE e.rowid = " . ((int) $id); |
||
298 | |||
299 | dol_syslog(get_class($this) . "::fetch", LOG_DEBUG); |
||
300 | $result = $this->db->query($sql); |
||
301 | if ($result) { |
||
302 | $obj = $this->db->fetch_object($result); |
||
303 | if ($obj) { |
||
304 | $this->id = $obj->rowid; |
||
305 | $this->ref = $obj->ref; |
||
306 | $this->label = $obj->label; |
||
307 | $this->address = $obj->address; |
||
308 | $this->zip = $obj->zip; |
||
309 | $this->town = $obj->town; |
||
310 | $this->status = $obj->status; |
||
311 | $this->entity = $obj->entity; |
||
312 | |||
313 | $this->country_id = $obj->country_id; |
||
314 | $this->country_code = $obj->country_code; |
||
315 | $this->country = $obj->country; |
||
316 | |||
317 | return 1; |
||
318 | } else { |
||
319 | return 0; |
||
320 | } |
||
321 | } else { |
||
322 | $this->error = $this->db->lasterror(); |
||
323 | return -1; |
||
324 | } |
||
325 | } |
||
326 | |||
327 | /** |
||
328 | * Delete record |
||
329 | * |
||
330 | * @param User $user User making the change |
||
331 | * @return int Return integer <0 if KO, >0 if OK |
||
332 | */ |
||
333 | public function delete($user) |
||
349 | } |
||
350 | } |
||
351 | |||
352 | /** |
||
353 | * Give a label from a status |
||
354 | * |
||
355 | * @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 |
||
356 | * @return string Label of status |
||
357 | */ |
||
358 | public function getLibStatut($mode = 0) |
||
361 | } |
||
362 | |||
363 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
364 | /** |
||
365 | * Return the status |
||
366 | * |
||
367 | * @param int $status Id status |
||
368 | * @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 |
||
369 | * @return string Label of status |
||
370 | */ |
||
371 | public function LibStatut($status, $mode = 0) |
||
392 | } |
||
393 | |||
394 | |||
395 | /** |
||
396 | * Information on record |
||
397 | * |
||
398 | * @param int $id Id of record |
||
399 | * @return void |
||
400 | */ |
||
401 | public function info($id) |
||
402 | { |
||
403 | $sql = 'SELECT e.rowid, e.ref, e.datec, e.fk_user_author, e.tms as datem, e.fk_user_mod, e.entity'; |
||
404 | $sql .= ' FROM ' . MAIN_DB_PREFIX . 'establishment as e'; |
||
405 | $sql .= ' WHERE e.rowid = ' . ((int) $id); |
||
406 | |||
407 | dol_syslog(get_class($this) . "::fetch info", LOG_DEBUG); |
||
408 | $result = $this->db->query($sql); |
||
409 | |||
410 | if ($result) { |
||
411 | if ($this->db->num_rows($result)) { |
||
412 | $obj = $this->db->fetch_object($result); |
||
413 | |||
414 | $this->id = $obj->rowid; |
||
415 | |||
416 | $this->user_creation_id = $obj->fk_user_author; |
||
417 | $this->user_modification_id = $obj->fk_user_mod; |
||
418 | $this->date_creation = $this->db->jdate($obj->datec); |
||
419 | $this->date_modification = empty($obj->datem) ? '' : $this->db->jdate($obj->datem); |
||
420 | } |
||
421 | $this->db->free($result); |
||
422 | } else { |
||
423 | dol_print_error($this->db); |
||
424 | } |
||
425 | } |
||
426 | |||
427 | /** |
||
428 | * Return a link to the object card (with optionally the picto) |
||
429 | * |
||
430 | * @param int $withpicto Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto) |
||
431 | * @param string $option On what the link point to ('nolink', ...) |
||
432 | * @param int $notooltip 1=Disable tooltip |
||
433 | * @param string $morecss Add more css on link |
||
434 | * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking |
||
435 | * @return string String with URL |
||
436 | */ |
||
437 | public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1) |
||
438 | { |
||
439 | global $conf, $langs, $hookmanager; |
||
440 | |||
441 | if (!empty($conf->dol_no_mouse_hover)) { |
||
442 | $notooltip = 1; // Force disable tooltips |
||
443 | } |
||
444 | |||
445 | $result = ''; |
||
446 | |||
447 | $label = img_picto('', $this->picto) . ' <u>' . $langs->trans("Establishment") . '</u>'; |
||
448 | if (isset($this->status)) { |
||
449 | $label .= ' ' . $this->getLibStatut(5); |
||
450 | } |
||
451 | $label .= '<br>'; |
||
452 | $label .= '<b>' . $langs->trans('Ref') . ':</b> ' . $this->ref; |
||
453 | |||
454 | $label .= '<br>'; |
||
455 | $label .= '<b>' . $langs->trans('Residence') . ':</b> ' . $this->address . ', ' . $this->zip . ' ' . $this->town; |
||
456 | |||
457 | $url = constant('BASE_URL') . '/hrm/establishment/card.php?id=' . $this->id; |
||
458 | |||
459 | if ($option != 'nolink') { |
||
460 | // Add param to save lastsearch_values or not |
||
461 | $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0); |
||
462 | if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) { |
||
463 | $add_save_lastsearch_values = 1; |
||
464 | } |
||
465 | if ($add_save_lastsearch_values) { |
||
466 | $url .= '&save_lastsearch_values=1'; |
||
467 | } |
||
468 | } |
||
469 | |||
470 | $linkclose = ''; |
||
471 | if (empty($notooltip)) { |
||
472 | if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) { |
||
473 | $label = $langs->trans("Establishment"); |
||
474 | $linkclose .= ' alt="' . dol_escape_htmltag($label, 1) . '"'; |
||
475 | } |
||
476 | $linkclose .= ' title="' . dol_escape_htmltag($label, 1) . '"'; |
||
477 | $linkclose .= ' class="classfortooltip' . ($morecss ? ' ' . $morecss : '') . '"'; |
||
478 | } else { |
||
479 | $linkclose = ($morecss ? ' class="' . $morecss . '"' : ''); |
||
480 | } |
||
481 | |||
482 | if ($option == 'nolink') { |
||
483 | $linkstart = '<span'; |
||
484 | } else { |
||
485 | $linkstart = '<a href="' . $url . '"'; |
||
486 | } |
||
487 | $linkstart .= $linkclose . '>'; |
||
488 | if ($option == 'nolink') { |
||
489 | $linkend = '</span>'; |
||
490 | } else { |
||
491 | $linkend = '</a>'; |
||
492 | } |
||
493 | |||
494 | $result .= $linkstart; |
||
495 | |||
496 | if ($withpicto) { |
||
497 | $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="' . (($withpicto != 2) ? 'paddingright ' : '') . 'classfortooltip"'), 0, 0, $notooltip ? 0 : 1); |
||
498 | } |
||
499 | |||
500 | if ($withpicto != 2) { |
||
501 | $result .= $this->label; |
||
502 | } |
||
503 | |||
504 | $result .= $linkend; |
||
505 | //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : ''); |
||
506 | |||
507 | global $action, $hookmanager; |
||
508 | $hookmanager->initHooks(array('establishmentdao')); |
||
509 | $parameters = array('id' => $this->id, 'getnomurl' => &$result); |
||
510 | $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks |
||
511 | if ($reshook > 0) { |
||
512 | $result = $hookmanager->resPrint; |
||
513 | } else { |
||
514 | $result .= $hookmanager->resPrint; |
||
515 | } |
||
516 | |||
517 | return $result; |
||
518 | } |
||
519 | |||
520 | /** |
||
521 | * Return account country code |
||
522 | * |
||
523 | * @return string country code |
||
524 | */ |
||
525 | public function getCountryCode() |
||
540 | } |
||
541 | |||
542 | /** |
||
543 | * Initialise object with example values |
||
544 | * Id must be 0 if object instance is a specimen |
||
545 | * |
||
546 | * @return int |
||
547 | */ |
||
548 | public function initAsSpecimen() |
||
555 | } |
||
556 | } |
||
557 |