| Total Complexity | 69 |
| Total Lines | 378 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like PartnershipType 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 PartnershipType, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 40 | class PartnershipType extends CommonObject |
||
| 41 | { |
||
| 42 | /** |
||
| 43 | * @var string Name of table without prefix where object is stored |
||
| 44 | */ |
||
| 45 | public $table_element = 'c_partnership_type'; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var string ID to identify managed object |
||
| 49 | */ |
||
| 50 | public $element = 'partnership_type'; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png |
||
| 54 | */ |
||
| 55 | public $picto = 'generic'; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var string Partnership code |
||
| 59 | */ |
||
| 60 | public $code; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var string Partnership type label |
||
| 64 | */ |
||
| 65 | public $label; |
||
| 66 | |||
| 67 | public $active; |
||
| 68 | |||
| 69 | |||
| 70 | public $fields = array( |
||
| 71 | 'rowid' => array('type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 10), |
||
| 72 | 'entity' => array('type' => 'integer', 'label' => 'Entity', 'default' => '1', 'enabled' => 1, 'visible' => -2, 'notnull' => 1, 'position' => 15, 'index' => 1), |
||
| 73 | 'code' => array('type' => 'varchar(32)', 'label' => 'Code', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 20), |
||
| 74 | 'label' => array('type' => 'varchar(64)', 'label' => 'Label', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 25, 'showoncombobox' => 1), |
||
| 75 | 'active' => array('type' => 'integer', 'label' => 'Active', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 30), |
||
| 76 | ); |
||
| 77 | |||
| 78 | |||
| 79 | |||
| 80 | /** |
||
| 81 | * Constructor |
||
| 82 | * |
||
| 83 | * @param DoliDB $db Database handler |
||
| 84 | */ |
||
| 85 | public function __construct(DoliDB $db) |
||
|
|
|||
| 86 | { |
||
| 87 | global $conf, $langs; |
||
| 88 | |||
| 89 | $this->db = $db; |
||
| 90 | |||
| 91 | $this->ismultientitymanaged = 1; |
||
| 92 | |||
| 93 | if (!getDolGlobalString('MAIN_SHOW_TECHNICAL_ID') && isset($this->fields['rowid'])) { |
||
| 94 | $this->fields['rowid']['visible'] = 0; |
||
| 95 | } |
||
| 96 | if (!isModEnabled('multicompany') && isset($this->fields['entity'])) { |
||
| 97 | $this->fields['entity']['enabled'] = 0; |
||
| 98 | } |
||
| 99 | |||
| 100 | // Example to show how to set values of fields definition dynamically |
||
| 101 | /*if ($user->hasRight('mymodule', 'myobject', 'read')) { |
||
| 102 | $this->fields['myfield']['visible'] = 1; |
||
| 103 | $this->fields['myfield']['noteditable'] = 0; |
||
| 104 | }*/ |
||
| 105 | |||
| 106 | // Unset fields that are disabled |
||
| 107 | foreach ($this->fields as $key => $val) { |
||
| 108 | if (isset($val['enabled']) && empty($val['enabled'])) { |
||
| 109 | unset($this->fields[$key]); |
||
| 110 | } |
||
| 111 | } |
||
| 112 | |||
| 113 | // Translate some data of arrayofkeyval |
||
| 114 | if (is_object($langs)) { |
||
| 115 | foreach ($this->fields as $key => $val) { |
||
| 116 | if (!empty($val['arrayofkeyval']) && is_array($val['arrayofkeyval'])) { |
||
| 117 | foreach ($val['arrayofkeyval'] as $key2 => $val2) { |
||
| 118 | $this->fields[$key]['arrayofkeyval'][$key2] = $langs->trans($val2); |
||
| 119 | } |
||
| 120 | } |
||
| 121 | } |
||
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Create object into database |
||
| 127 | * |
||
| 128 | * @param User $user User that creates |
||
| 129 | * @param int $notrigger 0=launch triggers after, 1=disable triggers |
||
| 130 | * @return int Return integer <0 if KO, Id of created object if OK |
||
| 131 | */ |
||
| 132 | public function create(User $user, $notrigger = 0) |
||
| 133 | { |
||
| 134 | $resultcreate = $this->createCommon($user, $notrigger); |
||
| 135 | return $resultcreate; |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Load object in memory from the database |
||
| 140 | * |
||
| 141 | * @param int $id Id object |
||
| 142 | * @param string $ref Ref |
||
| 143 | * @return int Return integer <0 if KO, 0 if not found, >0 if OK |
||
| 144 | */ |
||
| 145 | public function fetch($id, $ref = null) |
||
| 146 | { |
||
| 147 | $result = $this->fetchCommon($id, $ref); |
||
| 148 | return $result; |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Load list of objects in memory from the database. |
||
| 153 | * |
||
| 154 | * @param string $sortorder Sort Order |
||
| 155 | * @param string $sortfield Sort field |
||
| 156 | * @param int $limit limit |
||
| 157 | * @param int $offset Offset |
||
| 158 | * @param string $filter Filter as an Universal Search string. |
||
| 159 | * Example: '((client:=:1) OR ((client:>=:2) AND (client:<=:3))) AND (client:!=:8) AND (nom:like:'a%')' |
||
| 160 | * @param string $filtermode No more used |
||
| 161 | * @return array|int int <0 if KO, array of pages if OK |
||
| 162 | */ |
||
| 163 | public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, $filter = '', $filtermode = 'AND') |
||
| 218 | } |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Update object into database |
||
| 223 | * |
||
| 224 | * @param User $user User that modifies |
||
| 225 | * @param int $notrigger 0=launch triggers after, 1=disable triggers |
||
| 226 | * @return int Return integer <0 if KO, >0 if OK |
||
| 227 | */ |
||
| 228 | public function update(User $user, $notrigger = 0) |
||
| 229 | { |
||
| 230 | return $this->updateCommon($user, $notrigger); |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Delete object in database |
||
| 235 | * |
||
| 236 | * @param User $user User that deletes |
||
| 237 | * @param int $notrigger 0=launch triggers after, 1=disable triggers |
||
| 238 | * @return int Return integer <0 if KO, >0 if OK |
||
| 239 | */ |
||
| 240 | public function delete(User $user, $notrigger = 0) |
||
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Return a link to the object card (with optionally the picto) |
||
| 247 | * |
||
| 248 | * @param int $withpicto Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto) |
||
| 249 | * @param string $option On what the link point to ('nolink', ...) |
||
| 250 | * @param int $notooltip 1=Disable tooltip |
||
| 251 | * @param string $morecss Add more css on link |
||
| 252 | * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking |
||
| 253 | * @return string String with URL |
||
| 254 | */ |
||
| 255 | public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1) |
||
| 256 | { |
||
| 257 | global $conf, $langs, $hookmanager; |
||
| 258 | |||
| 259 | if (!empty($conf->dol_no_mouse_hover)) { |
||
| 260 | $notooltip = 1; // Force disable tooltips |
||
| 261 | } |
||
| 262 | |||
| 263 | $result = ''; |
||
| 264 | |||
| 265 | $label = img_picto('', $this->picto) . ' <u>' . $langs->trans("PartnershipType") . '</u>'; |
||
| 266 | if (isset($this->status)) { |
||
| 267 | $label .= ' ' . $this->getLibStatut(5); |
||
| 268 | } |
||
| 269 | $label .= '<br>'; |
||
| 270 | $label .= '<b>' . $langs->trans('Code') . ':</b> ' . $this->code; |
||
| 271 | $label .= '<br><b>' . $langs->trans('Label') . ':</b> ' . $this->label; |
||
| 272 | |||
| 273 | //$url = dol_buildpath('/partnership/partnership_card.php', 1).'?id='.$this->id; |
||
| 274 | $url = ''; |
||
| 275 | |||
| 276 | if ($option != 'nolink') { |
||
| 277 | // Add param to save lastsearch_values or not |
||
| 278 | $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0); |
||
| 279 | if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) { |
||
| 280 | $add_save_lastsearch_values = 1; |
||
| 281 | } |
||
| 282 | if ($url && $add_save_lastsearch_values) { |
||
| 283 | $url .= '&save_lastsearch_values=1'; |
||
| 284 | } |
||
| 285 | } |
||
| 286 | |||
| 287 | $linkclose = ''; |
||
| 288 | if (empty($notooltip)) { |
||
| 289 | if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) { |
||
| 290 | $label = $langs->trans("ShowMyObject"); |
||
| 291 | $linkclose .= ' alt="' . dol_escape_htmltag($label, 1) . '"'; |
||
| 292 | } |
||
| 293 | $linkclose .= ' title="' . dol_escape_htmltag($label, 1) . '"'; |
||
| 294 | $linkclose .= ' class="classfortooltip' . ($morecss ? ' ' . $morecss : '') . '"'; |
||
| 295 | } else { |
||
| 296 | $linkclose = ($morecss ? ' class="' . $morecss . '"' : ''); |
||
| 297 | } |
||
| 298 | |||
| 299 | if ($option == 'nolink' || empty($url)) { |
||
| 300 | $linkstart = '<span'; |
||
| 301 | } else { |
||
| 302 | $linkstart = '<a href="' . $url . '"'; |
||
| 303 | } |
||
| 304 | $linkstart .= $linkclose . '>'; |
||
| 305 | if ($option == 'nolink' || empty($url)) { |
||
| 306 | $linkend = '</span>'; |
||
| 307 | } else { |
||
| 308 | $linkend = '</a>'; |
||
| 309 | } |
||
| 310 | |||
| 311 | $result .= $linkstart; |
||
| 312 | |||
| 313 | if (empty($this->showphoto_on_popup)) { |
||
| 314 | if ($withpicto) { |
||
| 315 | $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); |
||
| 316 | } |
||
| 317 | } else { |
||
| 318 | if ($withpicto) { |
||
| 319 | require_once constant('DOL_DOCUMENT_ROOT') . '/core/lib/files.lib.php'; |
||
| 320 | |||
| 321 | list($class, $module) = explode('@', $this->picto); |
||
| 322 | $upload_dir = $conf->$module->multidir_output[$conf->entity] . "/$class/" . dol_sanitizeFileName($this->ref); |
||
| 323 | $filearray = dol_dir_list($upload_dir, "files"); |
||
| 324 | $filename = $filearray[0]['name']; |
||
| 325 | if (!empty($filename)) { |
||
| 326 | $pospoint = strpos($filearray[0]['name'], '.'); |
||
| 327 | |||
| 328 | $pathtophoto = $class . '/' . $this->ref . '/thumbs/' . substr($filename, 0, $pospoint) . '_mini' . substr($filename, $pospoint); |
||
| 329 | if (!getDolGlobalString(strtoupper($module . '_' . $class) . '_FORMATLISTPHOTOSASUSERS')) { |
||
| 330 | $result .= '<div class="floatleft inline-block valignmiddle divphotoref"><div class="photoref"><img class="photo' . $module . '" alt="No photo" border="0" src="' . constant('BASE_URL') . '/viewimage.php?modulepart=' . $module . '&entity=' . $conf->entity . '&file=' . urlencode($pathtophoto) . '"></div></div>'; |
||
| 331 | } else { |
||
| 332 | $result .= '<div class="floatleft inline-block valignmiddle divphotoref"><img class="photouserphoto userphoto" alt="No photo" border="0" src="' . constant('BASE_URL') . '/viewimage.php?modulepart=' . $module . '&entity=' . $conf->entity . '&file=' . urlencode($pathtophoto) . '"></div>'; |
||
| 333 | } |
||
| 334 | |||
| 335 | $result .= '</div>'; |
||
| 336 | } else { |
||
| 337 | $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); |
||
| 338 | } |
||
| 339 | } |
||
| 340 | } |
||
| 341 | |||
| 342 | if ($withpicto != 2) { |
||
| 343 | $result .= $this->ref; |
||
| 344 | } |
||
| 345 | |||
| 346 | $result .= $linkend; |
||
| 347 | //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : ''); |
||
| 348 | |||
| 349 | global $action, $hookmanager; |
||
| 350 | $hookmanager->initHooks(array('myobjectdao')); |
||
| 351 | $parameters = array('id' => $this->id, 'getnomurl' => &$result); |
||
| 352 | $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks |
||
| 353 | if ($reshook > 0) { |
||
| 354 | $result = $hookmanager->resPrint; |
||
| 355 | } else { |
||
| 356 | $result .= $hookmanager->resPrint; |
||
| 357 | } |
||
| 358 | |||
| 359 | return $result; |
||
| 360 | } |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Return the label of the status |
||
| 364 | * |
||
| 365 | * @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 |
||
| 366 | * @return string Label of status |
||
| 367 | */ |
||
| 368 | public function getLibStatut($mode = 0) |
||
| 369 | { |
||
| 370 | return ''; |
||
| 371 | } |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Load the info information in the object |
||
| 375 | * |
||
| 376 | * @param int $id Id of object |
||
| 377 | * @return void |
||
| 378 | */ |
||
| 379 | public function info($id) |
||
| 402 | } |
||
| 403 | } |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Initialise object with example values |
||
| 407 | * Id must be 0 if object instance is a specimen |
||
| 408 | * |
||
| 409 | * @return int |
||
| 410 | */ |
||
| 411 | public function initAsSpecimen() |
||
| 418 | } |
||
| 419 | } |
||
| 420 |