| Total Complexity | 66 |
| Total Lines | 506 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Subscription 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 Subscription, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class Subscription extends CommonObject |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * @var string ID to identify managed object |
||
| 37 | */ |
||
| 38 | public $element = 'subscription'; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var string Name of table without prefix where object is stored |
||
| 42 | */ |
||
| 43 | public $table_element = 'subscription'; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var int Does myobject support multicompany module ? 0=No test on entity, 1=Test with field entity, 2=Test with link by fk_soc, 'field@table'=Test with link by field@table |
||
| 47 | */ |
||
| 48 | public $ismultientitymanaged = 'fk_adherent@adherent'; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png |
||
| 52 | */ |
||
| 53 | public $picto = 'payment'; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Date creation record (datec) |
||
| 57 | * |
||
| 58 | * @var integer |
||
| 59 | */ |
||
| 60 | public $datec; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Date modification record (tms) |
||
| 64 | * |
||
| 65 | * @var integer |
||
| 66 | */ |
||
| 67 | public $datem; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Subscription start date (date subscription) |
||
| 71 | * |
||
| 72 | * @var integer |
||
| 73 | */ |
||
| 74 | public $dateh; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Subscription end date |
||
| 78 | * |
||
| 79 | * @var integer |
||
| 80 | */ |
||
| 81 | public $datef; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var int ID |
||
| 85 | */ |
||
| 86 | public $fk_type; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var int Member ID |
||
| 90 | */ |
||
| 91 | public $fk_adherent; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var double amount subscription |
||
| 95 | */ |
||
| 96 | public $amount; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var int ID |
||
| 100 | */ |
||
| 101 | public $fk_bank; |
||
| 102 | |||
| 103 | public $fields = array( |
||
| 104 | 'rowid' =>array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>10), |
||
| 105 | 'tms' =>array('type'=>'timestamp', 'label'=>'DateModification', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>15), |
||
| 106 | 'datec' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-1, 'position'=>20), |
||
| 107 | 'fk_adherent' =>array('type'=>'integer', 'label'=>'Member', 'enabled'=>1, 'visible'=>-1, 'position'=>25), |
||
| 108 | 'dateadh' =>array('type'=>'datetime', 'label'=>'DateSubscription', 'enabled'=>1, 'visible'=>-1, 'position'=>30), |
||
| 109 | 'datef' =>array('type'=>'datetime', 'label'=>'DateEndSubscription', 'enabled'=>1, 'visible'=>-1, 'position'=>35), |
||
| 110 | 'subscription' =>array('type'=>'double(24,8)', 'label'=>'Amount', 'enabled'=>1, 'visible'=>-1, 'position'=>40, 'isameasure'=>1), |
||
| 111 | 'fk_bank' =>array('type'=>'integer', 'label'=>'BankId', 'enabled'=>1, 'visible'=>-1, 'position'=>45), |
||
| 112 | 'note' =>array('type'=>'text', 'label'=>'Note', 'enabled'=>1, 'visible'=>-1, 'position'=>50), |
||
| 113 | 'fk_type' =>array('type'=>'integer', 'label'=>'MemberType', 'enabled'=>1, 'visible'=>-1, 'position'=>55), |
||
| 114 | 'fk_user_creat' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserAuthor', 'enabled'=>1, 'visible'=>-2, 'position'=>60), |
||
| 115 | 'fk_user_valid' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserValidation', 'enabled'=>1, 'visible'=>-1, 'position'=>65), |
||
| 116 | ); |
||
| 117 | |||
| 118 | |||
| 119 | /** |
||
| 120 | * Constructor |
||
| 121 | * |
||
| 122 | * @param DoliDB $db Database handler |
||
| 123 | */ |
||
| 124 | public function __construct($db) |
||
| 127 | } |
||
| 128 | |||
| 129 | |||
| 130 | /** |
||
| 131 | * Function who permitted cretaion of the subscription |
||
| 132 | * |
||
| 133 | * @param User $user User that create |
||
| 134 | * @param bool $notrigger false=launch triggers after, true=disable triggers |
||
| 135 | * @return int <0 if KO, Id subscription created if OK |
||
| 136 | */ |
||
| 137 | public function create($user, $notrigger = false) |
||
| 202 | } |
||
| 203 | } |
||
| 204 | |||
| 205 | |||
| 206 | /** |
||
| 207 | * Method to load a subscription |
||
| 208 | * |
||
| 209 | * @param int $rowid Id subscription |
||
| 210 | * @return int <0 if KO, =0 if not found, >0 if OK |
||
| 211 | */ |
||
| 212 | public function fetch($rowid) |
||
| 213 | { |
||
| 214 | $sql = "SELECT rowid, fk_type, fk_adherent, datec,"; |
||
| 215 | $sql .= " tms,"; |
||
| 216 | $sql .= " dateadh as dateh,"; |
||
| 217 | $sql .= " datef,"; |
||
| 218 | $sql .= " subscription, note as note_public, fk_bank"; |
||
| 219 | $sql .= " FROM ".MAIN_DB_PREFIX."subscription"; |
||
| 220 | $sql .= " WHERE rowid=".((int) $rowid); |
||
| 221 | |||
| 222 | dol_syslog(get_class($this)."::fetch", LOG_DEBUG); |
||
| 223 | $resql = $this->db->query($sql); |
||
| 224 | if ($resql) { |
||
| 225 | if ($this->db->num_rows($resql)) { |
||
| 226 | $obj = $this->db->fetch_object($resql); |
||
| 227 | |||
| 228 | $this->id = $obj->rowid; |
||
| 229 | $this->ref = $obj->rowid; |
||
| 230 | |||
| 231 | $this->fk_type = $obj->fk_type; |
||
| 232 | $this->fk_adherent = $obj->fk_adherent; |
||
| 233 | $this->datec = $this->db->jdate($obj->datec); |
||
| 234 | $this->datem = $this->db->jdate($obj->tms); |
||
| 235 | $this->dateh = $this->db->jdate($obj->dateh); |
||
| 236 | $this->datef = $this->db->jdate($obj->datef); |
||
| 237 | $this->amount = $obj->subscription; |
||
| 238 | $this->note = $obj->note_public; // deprecated |
||
| 239 | $this->note_public = $obj->note_public; |
||
| 240 | $this->fk_bank = $obj->fk_bank; |
||
| 241 | return 1; |
||
| 242 | } else { |
||
| 243 | return 0; |
||
| 244 | } |
||
| 245 | } else { |
||
| 246 | $this->error = $this->db->lasterror(); |
||
| 247 | return -1; |
||
| 248 | } |
||
| 249 | } |
||
| 250 | |||
| 251 | |||
| 252 | /** |
||
| 253 | * Update subscription |
||
| 254 | * |
||
| 255 | * @param User $user User who updated |
||
| 256 | * @param int $notrigger 0=Disable triggers |
||
| 257 | * @return int <0 if KO, >0 if OK |
||
| 258 | */ |
||
| 259 | public function update($user, $notrigger = 0) |
||
| 260 | { |
||
| 261 | $error = 0; |
||
| 262 | |||
| 263 | $this->db->begin(); |
||
| 264 | |||
| 265 | if (!is_numeric($this->amount)) { |
||
| 266 | $this->error = 'BadValueForParameterAmount'; |
||
| 267 | return -1; |
||
| 268 | } |
||
| 269 | |||
| 270 | if (empty($this->note_public) && !empty($this->note)) { // For backward compatibility |
||
| 271 | $this->note_public = $this->note; |
||
| 272 | } |
||
| 273 | |||
| 274 | $sql = "UPDATE ".MAIN_DB_PREFIX."subscription SET "; |
||
| 275 | $sql .= " fk_type = ".((int) $this->fk_type).","; |
||
| 276 | $sql .= " fk_adherent = ".((int) $this->fk_adherent).","; |
||
| 277 | $sql .= " note=".($this->note_public ? "'".$this->db->escape($this->note_public)."'" : 'null').","; |
||
| 278 | $sql .= " subscription = ".price2num($this->amount).","; |
||
| 279 | $sql .= " dateadh='".$this->db->idate($this->dateh)."',"; |
||
| 280 | $sql .= " datef='".$this->db->idate($this->datef)."',"; |
||
| 281 | $sql .= " datec='".$this->db->idate($this->datec)."',"; |
||
| 282 | $sql .= " fk_bank = ".($this->fk_bank ? ((int) $this->fk_bank) : 'null'); |
||
| 283 | $sql .= " WHERE rowid = ".((int) $this->id); |
||
| 284 | |||
| 285 | dol_syslog(get_class($this)."::update", LOG_DEBUG); |
||
| 286 | $resql = $this->db->query($sql); |
||
| 287 | if ($resql) { |
||
| 288 | require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php'; |
||
| 289 | $member = new Adherent($this->db); |
||
| 290 | $result = $member->fetch($this->fk_adherent); |
||
| 291 | $result = $member->update_end_date($user); |
||
| 292 | |||
| 293 | if (!$error && !$notrigger) { |
||
| 294 | $this->context = array('member'=>$member); |
||
| 295 | // Call triggers |
||
| 296 | $result = $this->call_trigger('MEMBER_SUBSCRIPTION_MODIFY', $user); |
||
| 297 | if ($result < 0) { |
||
| 298 | $error++; |
||
| 299 | } //Do also here what you must do to rollback action if trigger fail |
||
| 300 | // End call triggers |
||
| 301 | } |
||
| 302 | } else { |
||
| 303 | $error++; |
||
| 304 | $this->error = $this->db->lasterror(); |
||
| 305 | } |
||
| 306 | |||
| 307 | // Commit or rollback |
||
| 308 | if ($error) { |
||
| 309 | $this->db->rollback(); |
||
| 310 | return -1; |
||
| 311 | } else { |
||
| 312 | $this->db->commit(); |
||
| 313 | return $this->id; |
||
| 314 | } |
||
| 315 | } |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Delete a subscription |
||
| 319 | * |
||
| 320 | * @param User $user User that delete |
||
| 321 | * @param bool $notrigger false=launch triggers after, true=disable triggers |
||
| 322 | * @return int <0 if KO, 0 if not found, >0 if OK |
||
| 323 | */ |
||
| 324 | public function delete($user, $notrigger = false) |
||
| 391 | } |
||
| 392 | } |
||
| 393 | |||
| 394 | |||
| 395 | /** |
||
| 396 | * Return clicable name (with picto eventually) |
||
| 397 | * |
||
| 398 | * @param int $withpicto 0=No picto, 1=Include picto into link, 2=Only picto |
||
| 399 | * @param int $notooltip 1=Disable tooltip |
||
| 400 | * @param string $option Page for link ('', 'nolink', ...) |
||
| 401 | * @param string $morecss Add more css on link |
||
| 402 | * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking |
||
| 403 | * @return string Chaine avec URL |
||
| 404 | */ |
||
| 405 | public function getNomUrl($withpicto = 0, $notooltip = 0, $option = '', $morecss = '', $save_lastsearch_value = -1) |
||
| 406 | { |
||
| 407 | global $langs; |
||
| 408 | |||
| 409 | $result = ''; |
||
| 410 | |||
| 411 | $langs->load("members"); |
||
| 412 | |||
| 413 | $label = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("Subscription").'</u>'; |
||
| 414 | /*if (isset($this->statut)) { |
||
| 415 | $label .= ' '.$this->getLibStatut(5); |
||
| 416 | }*/ |
||
| 417 | $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref; |
||
| 418 | if (!empty($this->dateh)) { |
||
| 419 | $label .= '<br><b>'.$langs->trans('DateStart').':</b> '.dol_print_date($this->dateh, 'day'); |
||
| 420 | } |
||
| 421 | if (!empty($this->datef)) { |
||
| 422 | $label .= '<br><b>'.$langs->trans('DateEnd').':</b> '.dol_print_date($this->datef, 'day'); |
||
| 423 | } |
||
| 424 | |||
| 425 | $url = DOL_URL_ROOT.'/adherents/subscription/card.php?rowid='.((int) $this->id); |
||
| 426 | |||
| 427 | if ($option != 'nolink') { |
||
| 428 | // Add param to save lastsearch_values or not |
||
| 429 | $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0); |
||
| 430 | if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) { |
||
| 431 | $add_save_lastsearch_values = 1; |
||
| 432 | } |
||
| 433 | if ($add_save_lastsearch_values) { |
||
| 434 | $url .= '&save_lastsearch_values=1'; |
||
| 435 | } |
||
| 436 | } |
||
| 437 | |||
| 438 | $linkstart = '<a href="'.$url.'" class="classfortooltip" title="'.dol_escape_htmltag($label, 1).'">'; |
||
| 439 | $linkend = '</a>'; |
||
| 440 | |||
| 441 | $result .= $linkstart; |
||
| 442 | if ($withpicto) { |
||
| 443 | $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); |
||
| 444 | } |
||
| 445 | if ($withpicto != 2) { |
||
| 446 | $result .= $this->ref; |
||
| 447 | } |
||
| 448 | $result .= $linkend; |
||
| 449 | |||
| 450 | return $result; |
||
| 451 | } |
||
| 452 | |||
| 453 | |||
| 454 | /** |
||
| 455 | * Retourne le libelle du statut d'une adhesion |
||
| 456 | * |
||
| 457 | * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto |
||
| 458 | * @return string Label |
||
| 459 | */ |
||
| 460 | public function getLibStatut($mode = 0) |
||
| 461 | { |
||
| 462 | return ''; |
||
| 463 | } |
||
| 464 | |||
| 465 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 466 | /** |
||
| 467 | * Renvoi le libelle d'un statut donne |
||
| 468 | * |
||
| 469 | * @param int $status Id status |
||
| 470 | * @return string Label |
||
| 471 | */ |
||
| 472 | public function LibStatut($status) |
||
| 478 | } |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Load information of the subscription object |
||
| 482 | * |
||
| 483 | * @param int $id Id subscription |
||
| 484 | * @return void |
||
| 485 | */ |
||
| 486 | public function info($id) |
||
| 506 | } |
||
| 507 | } |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Return clicable link of object (with eventually picto) |
||
| 511 | * |
||
| 512 | * @param string $option Where point the link (0=> main card, 1,2 => shipment, 'nolink'=>No link) |
||
| 513 | * @return string HTML Code for Kanban thumb. |
||
| 514 | */ |
||
| 515 | public function getKanbanView($option = '') |
||
| 541 |