| Total Complexity | 318 |
| Total Lines | 1955 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like EmailCollector 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 EmailCollector, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | class EmailCollector extends CommonObject |
||
| 37 | { |
||
| 38 | /** |
||
| 39 | * @var string ID to identify managed object |
||
| 40 | */ |
||
| 41 | public $element = 'emailcollector'; |
||
| 42 | /** |
||
| 43 | * @var string Name of table without prefix where object is stored |
||
| 44 | */ |
||
| 45 | public $table_element = 'emailcollector_emailcollector'; |
||
| 46 | /** |
||
| 47 | * @var int Does emailcollector support multicompany module ? 0=No test on entity, 1=Test with field entity, 2=Test with link by societe |
||
| 48 | */ |
||
| 49 | public $ismultientitymanaged = 1; |
||
| 50 | /** |
||
| 51 | * @var int Does emailcollector support extrafields ? 0=No, 1=Yes |
||
| 52 | */ |
||
| 53 | public $isextrafieldmanaged = 0; |
||
| 54 | /** |
||
| 55 | * @var string String with name of icon for emailcollector. Must be the part after the 'object_' into object_emailcollector.png |
||
| 56 | */ |
||
| 57 | public $picto = 'generic'; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var int Field with ID of parent key if this field has a parent |
||
| 61 | */ |
||
| 62 | public $fk_element = 'fk_emailcollector'; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var array List of child tables. To test if we can delete object. |
||
| 66 | */ |
||
| 67 | protected $childtables=array(); |
||
| 68 | /** |
||
| 69 | * @var array List of child tables. To know object to delete on cascade. |
||
| 70 | */ |
||
| 71 | protected $childtablesoncascade=array('emailcollector_emailcollectorfilter','emailcollector_emailcollectoraction'); |
||
| 72 | |||
| 73 | |||
| 74 | /** |
||
| 75 | * 'type' if the field format. |
||
| 76 | * 'label' the translation key. |
||
| 77 | * 'enabled' is a condition when the field must be managed. |
||
| 78 | * 'visible' says if field is visible in list (Examples: 0=Not visible, 1=Visible on list and create/update/view forms, 2=Visible on list only. Using a negative value means field is not shown by default on list but can be selected for viewing) |
||
| 79 | * 'notnull' is set to 1 if not null in database. Set to -1 if we must set data to null if empty ('' or 0). |
||
| 80 | * 'default' is a default value for creation (can still be replaced by the global setup of default values) |
||
| 81 | * 'index' if we want an index in database. |
||
| 82 | * 'foreignkey'=>'tablename.field' if the field is a foreign key (it is recommanded to name the field fk_...). |
||
| 83 | * 'position' is the sort order of field. |
||
| 84 | * 'searchall' is 1 if we want to search in this field when making a search from the quick search button. |
||
| 85 | * 'isameasure' must be set to 1 if you want to have a total on list for this field. Field type must be summable like integer or double(24,8). |
||
| 86 | * 'css' is the CSS style to use on field. For example: 'maxwidth200' |
||
| 87 | * 'help' is a string visible as a tooltip on field |
||
| 88 | * 'comment' is not used. You can store here any text of your choice. It is not used by application. |
||
| 89 | * 'showoncombobox' if value of the field must be visible into the label of the combobox that list record |
||
| 90 | * 'arraykeyval' to set list of value if type is a list of predefined values. For example: array("0"=>"Draft","1"=>"Active","-1"=>"Cancel") |
||
| 91 | */ |
||
| 92 | |||
| 93 | // BEGIN MODULEBUILDER PROPERTIES |
||
| 94 | /** |
||
| 95 | * @var array Array with all fields and their property. Do not use it as a static var. It may be modified by constructor. |
||
| 96 | */ |
||
| 97 | public $fields=array( |
||
| 98 | 'rowid' => array('type'=>'integer', 'label'=>'TechnicalID','visible'=>2, 'enabled'=>1, 'position'=>1, 'notnull'=>1, 'index'=>1), |
||
| 99 | 'entity' => array('type'=>'integer', 'label'=>'Entity', 'enabled'=>1, 'visible'=>0, 'default'=>1, 'notnull'=>1, 'index'=>1, 'position'=>20), |
||
| 100 | 'ref' => array('type'=>'varchar(128)', 'label'=>'Ref', 'enabled'=>1, 'visible'=>1, 'notnull'=>1, 'showoncombobox'=>1, 'index'=>1, 'position'=>10, 'searchall'=>1, 'help'=>'Example: MyCollector1'), |
||
| 101 | 'label' => array('type'=>'varchar(255)', 'label'=>'Label', 'visible'=>1, 'enabled'=>1, 'position'=>30, 'notnull'=>-1, 'searchall'=>1, 'help'=>'Example: My Email collector'), |
||
| 102 | 'description' => array('type'=>'text', 'label'=>'Description', 'visible'=>-1, 'enabled'=>1, 'position'=>60, 'notnull'=>-1), |
||
| 103 | 'host' => array('type'=>'varchar(255)', 'label'=>'EMailHost', 'visible'=>1, 'enabled'=>1, 'position'=>100, 'notnull'=>1, 'searchall'=>1, 'comment'=>"IMAP server", 'help'=>'Example: imap.gmail.com'), |
||
| 104 | 'login' => array('type'=>'varchar(128)', 'label'=>'Login', 'visible'=>1, 'enabled'=>1, 'position'=>101, 'notnull'=>-1, 'index'=>1, 'comment'=>"IMAP login", 'help'=>'Example: [email protected]'), |
||
| 105 | 'password' => array('type'=>'password', 'label'=>'Password', 'visible'=>-1, 'enabled'=>1, 'position'=>102, 'notnull'=>-1, 'comment'=>"IMAP password", 'help'=>'WithGMailYouCanCreateADedicatedPassword'), |
||
| 106 | 'source_directory' => array('type'=>'varchar(255)', 'label'=>'MailboxSourceDirectory', 'visible'=>-1, 'enabled'=>1, 'position'=>103, 'notnull'=>1, 'default' => 'Inbox', 'help'=>'Example: INBOX'), |
||
| 107 | //'filter' => array('type'=>'text', 'label'=>'Filter', 'visible'=>1, 'enabled'=>1, 'position'=>105), |
||
| 108 | //'actiontodo' => array('type'=>'varchar(255)', 'label'=>'ActionToDo', 'visible'=>1, 'enabled'=>1, 'position'=>106), |
||
| 109 | 'target_directory' => array('type'=>'varchar(255)', 'label'=>'MailboxTargetDirectory', 'visible'=>1, 'enabled'=>1, 'position'=>110, 'notnull'=>0, 'comment'=>"Where to store messages once processed"), |
||
| 110 | 'maxemailpercollect' => array('type'=>'integer', 'label'=>'MaxEmailCollectPerCollect','visible'=>-1, 'enabled'=>1, 'position'=>111, 'default'=>100), |
||
| 111 | 'datelastresult' => array('type'=>'datetime', 'label'=>'DateLastCollectResult', 'visible'=>1, 'enabled'=>'$action != "create" && $action != "edit"', 'position'=>121, 'notnull'=>-1,), |
||
| 112 | 'codelastresult' => array('type'=>'varchar(16)', 'label'=>'CodeLastResult', 'visible'=>1, 'enabled'=>'$action != "create" && $action != "edit"', 'position'=>122, 'notnull'=>-1,), |
||
| 113 | 'lastresult' => array('type'=>'varchar(255)', 'label'=>'LastResult', 'visible'=>1, 'enabled'=>'$action != "create" && $action != "edit"', 'position'=>123, 'notnull'=>-1,), |
||
| 114 | 'datelastok' => array('type'=>'datetime', 'label'=>'DateLastcollectResultOk', 'visible'=>1, 'enabled'=>'$action != "create"', 'position'=>125, 'notnull'=>-1,), |
||
| 115 | 'note_public' => array('type'=>'html', 'label'=>'NotePublic', 'visible'=>0, 'enabled'=>1, 'position'=>61, 'notnull'=>-1,), |
||
| 116 | 'note_private' => array('type'=>'html', 'label'=>'NotePrivate', 'visible'=>0, 'enabled'=>1, 'position'=>62, 'notnull'=>-1,), |
||
| 117 | 'date_creation' => array('type'=>'datetime', 'label'=>'DateCreation', 'visible'=>-2, 'enabled'=>1, 'position'=>500, 'notnull'=>1,), |
||
| 118 | 'tms' => array('type'=>'timestamp', 'label'=>'DateModification', 'visible'=>-2, 'enabled'=>1, 'position'=>501, 'notnull'=>1,), |
||
| 119 | //'date_validation' => array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-2, 'position'=>502), |
||
| 120 | 'fk_user_creat' => array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserAuthor', 'visible'=>-2, 'enabled'=>1, 'position'=>510, 'notnull'=>1,), |
||
| 121 | 'fk_user_modif' => array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserModif', 'visible'=>-2, 'enabled'=>1, 'position'=>511, 'notnull'=>-1,), |
||
| 122 | //'fk_user_valid' =>array('type'=>'integer', 'label'=>'UserValidation', 'enabled'=>1, 'visible'=>-1, 'position'=>512), |
||
| 123 | 'import_key' => array('type'=>'varchar(14)', 'label'=>'ImportId', 'visible'=>-2, 'enabled'=>1, 'position'=>1000, 'notnull'=>-1,), |
||
| 124 | 'status' => array('type'=>'integer', 'label'=>'Status', 'visible'=>1, 'enabled'=>1, 'position'=>1000, 'notnull'=>1, 'index'=>1, 'arrayofkeyval'=>array('0'=>'Inactive', '1'=>'Active')) |
||
| 125 | ); |
||
| 126 | |||
| 127 | |||
| 128 | /** |
||
| 129 | * @var int ID |
||
| 130 | */ |
||
| 131 | public $rowid; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @var string Ref |
||
| 135 | */ |
||
| 136 | public $ref; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @var int Entity |
||
| 140 | */ |
||
| 141 | public $entity; |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @var string label |
||
| 145 | */ |
||
| 146 | public $label; |
||
| 147 | |||
| 148 | |||
| 149 | /** |
||
| 150 | * @var int Status |
||
| 151 | */ |
||
| 152 | public $status; |
||
| 153 | |||
| 154 | public $date_creation; |
||
| 155 | public $tms; |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @var int ID |
||
| 159 | */ |
||
| 160 | public $fk_user_creat; |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @var int ID |
||
| 164 | */ |
||
| 165 | public $fk_user_modif; |
||
| 166 | |||
| 167 | public $import_key; |
||
| 168 | |||
| 169 | |||
| 170 | public $host; |
||
| 171 | public $login; |
||
| 172 | public $password; |
||
| 173 | public $source_directory; |
||
| 174 | public $target_directory; |
||
| 175 | public $maxemailpercollect; |
||
| 176 | public $datelastresult; |
||
| 177 | public $lastresult; |
||
| 178 | // END MODULEBUILDER PROPERTIES |
||
| 179 | |||
| 180 | public $filters; |
||
| 181 | public $actions; |
||
| 182 | |||
| 183 | public $debuginfo; |
||
| 184 | |||
| 185 | |||
| 186 | /** |
||
| 187 | * Constructor |
||
| 188 | * |
||
| 189 | * @param DoliDb $db Database handler |
||
| 190 | */ |
||
| 191 | public function __construct(DoliDB $db) |
||
| 192 | { |
||
| 193 | global $conf, $langs; |
||
| 194 | |||
| 195 | $this->db = $db; |
||
| 196 | |||
| 197 | if (empty($conf->global->MAIN_SHOW_TECHNICAL_ID) && isset($this->fields['rowid'])) $this->fields['rowid']['visible']=0; |
||
| 198 | if (empty($conf->multicompany->enabled) && isset($this->fields['entity'])) $this->fields['entity']['enabled']=0; |
||
| 199 | |||
| 200 | // Unset fields that are disabled |
||
| 201 | foreach($this->fields as $key => $val) |
||
| 202 | { |
||
| 203 | if (isset($val['enabled']) && empty($val['enabled'])) |
||
| 204 | { |
||
| 205 | unset($this->fields[$key]); |
||
| 206 | } |
||
| 207 | } |
||
| 208 | |||
| 209 | // Translate some data of arrayofkeyval |
||
| 210 | foreach($this->fields as $key => $val) |
||
| 211 | { |
||
| 212 | if (is_array($val['arrayofkeyval'])) |
||
| 213 | { |
||
| 214 | foreach($val['arrayofkeyval'] as $key2 => $val2) |
||
| 215 | { |
||
| 216 | $this->fields[$key]['arrayofkeyval'][$key2]=$langs->trans($val2); |
||
| 217 | } |
||
| 218 | } |
||
| 219 | } |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Create object into database |
||
| 224 | * |
||
| 225 | * @param User $user User that creates |
||
| 226 | * @param bool $notrigger false=launch triggers after, true=disable triggers |
||
| 227 | * @return int <0 if KO, Id of created object if OK |
||
| 228 | */ |
||
| 229 | public function create(User $user, $notrigger = false) |
||
| 230 | { |
||
| 231 | return $this->createCommon($user, $notrigger); |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Clone and object into another one |
||
| 236 | * |
||
| 237 | * @param User $user User that creates |
||
| 238 | * @param int $fromid Id of object to clone |
||
| 239 | * @return mixed New object created, <0 if KO |
||
| 240 | */ |
||
| 241 | public function createFromClone(User $user, $fromid) |
||
| 242 | { |
||
| 243 | global $langs, $extrafields; |
||
| 244 | $error = 0; |
||
| 245 | |||
| 246 | dol_syslog(__METHOD__, LOG_DEBUG); |
||
| 247 | |||
| 248 | $object = new self($this->db); |
||
| 249 | |||
| 250 | $this->db->begin(); |
||
| 251 | |||
| 252 | // Load source object |
||
| 253 | $object->fetchCommon($fromid); |
||
| 254 | // Reset some properties |
||
| 255 | unset($object->id); |
||
| 256 | unset($object->fk_user_creat); |
||
| 257 | unset($object->import_key); |
||
| 258 | |||
| 259 | // Clear fields |
||
| 260 | $object->ref = "copy_of_".$object->ref; |
||
| 261 | $object->title = $langs->trans("CopyOf")." ".$object->title; |
||
| 262 | // ... |
||
| 263 | // Clear extrafields that are unique |
||
| 264 | if (is_array($object->array_options) && count($object->array_options) > 0) |
||
| 265 | { |
||
| 266 | $extrafields->fetch_name_optionals_label($this->element); |
||
| 267 | foreach($object->array_options as $key => $option) |
||
| 268 | { |
||
| 269 | $shortkey = preg_replace('/options_/', '', $key); |
||
| 270 | if (! empty($extrafields->attributes[$this->element]['unique'][$shortkey])) |
||
| 271 | { |
||
| 272 | //var_dump($key); var_dump($clonedObj->array_options[$key]); exit; |
||
| 273 | unset($object->array_options[$key]); |
||
| 274 | } |
||
| 275 | } |
||
| 276 | } |
||
| 277 | |||
| 278 | // Create clone |
||
| 279 | $object->context['createfromclone'] = 'createfromclone'; |
||
| 280 | $result = $object->createCommon($user); |
||
| 281 | if ($result < 0) { |
||
| 282 | $error++; |
||
| 283 | $this->error = $object->error; |
||
| 284 | $this->errors = $object->errors; |
||
| 285 | } |
||
| 286 | |||
| 287 | unset($object->context['createfromclone']); |
||
| 288 | |||
| 289 | // End |
||
| 290 | if (!$error) { |
||
| 291 | $this->db->commit(); |
||
| 292 | return $object; |
||
| 293 | } else { |
||
| 294 | $this->db->rollback(); |
||
| 295 | return -1; |
||
| 296 | } |
||
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Load object in memory from the database |
||
| 301 | * |
||
| 302 | * @param int $id Id object |
||
| 303 | * @param string $ref Ref |
||
| 304 | * @return int <0 if KO, 0 if not found, >0 if OK |
||
| 305 | */ |
||
| 306 | public function fetch($id, $ref = null) |
||
| 307 | { |
||
| 308 | $result = $this->fetchCommon($id, $ref); |
||
| 309 | //if ($result > 0 && ! empty($this->table_element_line)) $this->fetchLines(); |
||
| 310 | return $result; |
||
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Load object lines in memory from the database |
||
| 315 | * |
||
| 316 | * @return int <0 if KO, 0 if not found, >0 if OK |
||
| 317 | */ |
||
| 318 | /* |
||
| 319 | public function fetchLines() |
||
| 320 | { |
||
| 321 | $this->lines=array(); |
||
| 322 | |||
| 323 | // Load lines with object EmailCollectorLine |
||
| 324 | |||
| 325 | return count($this->lines)?1:0; |
||
| 326 | } |
||
| 327 | */ |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Fetch all account and load objects into an array |
||
| 331 | * |
||
| 332 | * @param User $user User |
||
| 333 | * @param int $activeOnly filter if active |
||
| 334 | * @param string $sortfield field for sorting |
||
| 335 | * @param string $sortorder sorting order |
||
| 336 | * @param int $limit sort limit |
||
| 337 | * @param int $page page to start on |
||
| 338 | * @return array Array with key => EmailCollector object |
||
| 339 | */ |
||
| 340 | public function fetchAll(User $user, $activeOnly = 0, $sortfield = 's.rowid', $sortorder = 'ASC', $limit = 100, $page = 0) |
||
| 341 | { |
||
| 342 | global $langs; |
||
| 343 | |||
| 344 | $obj_ret = array(); |
||
| 345 | |||
| 346 | $sql = "SELECT s.rowid"; |
||
| 347 | $sql.= " FROM ".MAIN_DB_PREFIX."emailcollector_emailcollector as s"; |
||
| 348 | $sql.= ' WHERE s.entity IN ('.getEntity('emailcollector').')'; |
||
| 349 | if ($activeOnly) { |
||
| 350 | $sql.= " AND s.status = 1"; |
||
| 351 | } |
||
| 352 | $sql.= $this->db->order($sortfield, $sortorder); |
||
| 353 | if ($limit) { |
||
| 354 | if ($page < 0) { |
||
| 355 | $page = 0; |
||
| 356 | } |
||
| 357 | $offset = $limit * $page; |
||
| 358 | |||
| 359 | $sql.= $this->db->plimit($limit + 1, $offset); |
||
| 360 | } |
||
| 361 | |||
| 362 | $result = $this->db->query($sql); |
||
| 363 | if ($result) { |
||
| 364 | $num = $this->db->num_rows($result); |
||
| 365 | $i = 0; |
||
| 366 | while ($i < $num) |
||
| 367 | { |
||
| 368 | $obj = $this->db->fetch_object($result); |
||
| 369 | $emailcollector_static = new EmailCollector($this->db); |
||
| 370 | if ($emailcollector_static->fetch($obj->rowid)) { |
||
| 371 | $obj_ret[] = $emailcollector_static; |
||
| 372 | } |
||
| 373 | $i++; |
||
| 374 | } |
||
| 375 | } else { |
||
| 376 | $this->errors[] = 'EmailCollector::fetchAll Error when retrieve emailcollector list'; |
||
| 377 | dol_syslog('EmailCollector::fetchAll Error when retrieve emailcollector list', LOG_ERR); |
||
| 378 | $ret = -1; |
||
| 379 | } |
||
| 380 | if (! count($obj_ret)) { |
||
| 381 | dol_syslog('EmailCollector::fetchAll No emailcollector found', LOG_DEBUG); |
||
| 382 | } |
||
| 383 | |||
| 384 | return $obj_ret; |
||
| 385 | } |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Update object into database |
||
| 389 | * |
||
| 390 | * @param User $user User that modifies |
||
| 391 | * @param bool $notrigger false=launch triggers after, true=disable triggers |
||
| 392 | * @return int <0 if KO, >0 if OK |
||
| 393 | */ |
||
| 394 | public function update(User $user, $notrigger = false) |
||
| 395 | { |
||
| 396 | return $this->updateCommon($user, $notrigger); |
||
| 397 | } |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Delete object in database |
||
| 401 | * |
||
| 402 | * @param User $user User that deletes |
||
| 403 | * @param bool $notrigger false=launch triggers after, true=disable triggers |
||
| 404 | * @return int <0 if KO, >0 if OK |
||
| 405 | */ |
||
| 406 | public function delete(User $user, $notrigger = false) |
||
| 409 | } |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Return a link to the object card (with optionaly the picto) |
||
| 413 | * |
||
| 414 | * @param int $withpicto Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto) |
||
| 415 | * @param string $option On what the link point to ('nolink', ...) |
||
| 416 | * @param int $notooltip 1=Disable tooltip |
||
| 417 | * @param string $morecss Add more css on link |
||
| 418 | * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking |
||
| 419 | * @return string String with URL |
||
| 420 | */ |
||
| 421 | public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1) |
||
| 422 | { |
||
| 423 | global $conf, $langs, $hookmanager; |
||
| 424 | |||
| 425 | if (! empty($conf->dol_no_mouse_hover)) $notooltip=1; // Force disable tooltips |
||
| 426 | |||
| 427 | $result = ''; |
||
| 428 | |||
| 429 | $label = '<u>' . $langs->trans("EmailCollector") . '</u>'; |
||
| 430 | $label.= '<br>'; |
||
| 431 | $label.= '<b>' . $langs->trans('Ref') . ':</b> ' . $this->ref; |
||
| 432 | |||
| 433 | $url = dol_buildpath('/admin/emailcollector_card.php', 1).'?id='.$this->id; |
||
| 434 | |||
| 435 | if ($option != 'nolink') |
||
| 436 | { |
||
| 437 | // Add param to save lastsearch_values or not |
||
| 438 | $add_save_lastsearch_values=($save_lastsearch_value == 1 ? 1 : 0); |
||
| 439 | if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) { |
||
| 440 | $add_save_lastsearch_values=1; |
||
| 441 | } |
||
| 442 | if ($add_save_lastsearch_values) { |
||
| 443 | $url.='&save_lastsearch_values=1'; |
||
| 444 | } |
||
| 445 | } |
||
| 446 | |||
| 447 | $linkclose=''; |
||
| 448 | if (empty($notooltip)) |
||
| 449 | { |
||
| 450 | if (! empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) |
||
| 451 | { |
||
| 452 | $label=$langs->trans("ShowEmailCollector"); |
||
| 453 | $linkclose.=' alt="'.dol_escape_htmltag($label, 1).'"'; |
||
| 454 | } |
||
| 455 | $linkclose.=' title="'.dol_escape_htmltag($label, 1).'"'; |
||
| 456 | $linkclose.=' class="classfortooltip'.($morecss?' '.$morecss:'').'"'; |
||
| 457 | |||
| 458 | /* |
||
| 459 | $hookmanager->initHooks(array('myobjectdao')); |
||
| 460 | $parameters=array('id'=>$this->id); |
||
| 461 | $reshook=$hookmanager->executeHooks('getnomurltooltip',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks |
||
| 462 | if ($reshook > 0) $linkclose = $hookmanager->resPrint; |
||
| 463 | */ |
||
| 464 | } |
||
| 465 | else $linkclose = ($morecss?' class="'.$morecss.'"':''); |
||
| 466 | |||
| 467 | $linkstart = '<a href="'.$url.'"'; |
||
| 468 | $linkstart.=$linkclose.'>'; |
||
| 469 | $linkend='</a>'; |
||
| 470 | |||
| 471 | $result .= $linkstart; |
||
| 472 | if ($withpicto) $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); |
||
| 473 | if ($withpicto != 2) $result.= $this->ref; |
||
| 474 | $result .= $linkend; |
||
| 475 | //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : ''); |
||
| 476 | |||
| 477 | global $action,$hookmanager; |
||
| 478 | $hookmanager->initHooks(array('emailcollectordao')); |
||
| 479 | $parameters=array('id'=>$this->id, 'getnomurl'=>$result); |
||
| 480 | $reshook=$hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks |
||
| 481 | if ($reshook > 0) $result = $hookmanager->resPrint; |
||
| 482 | else $result .= $hookmanager->resPrint; |
||
| 483 | |||
| 484 | return $result; |
||
| 485 | } |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Return label of the status |
||
| 489 | * |
||
| 490 | * @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 |
||
| 491 | * @return string Label of status |
||
| 492 | */ |
||
| 493 | public function getLibStatut($mode = 0) |
||
| 494 | { |
||
| 495 | return $this->LibStatut($this->status, $mode); |
||
| 496 | } |
||
| 497 | |||
| 498 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 499 | /** |
||
| 500 | * Return the status |
||
| 501 | * |
||
| 502 | * @param int $status Id status |
||
| 503 | * @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 |
||
| 504 | * @return string Label of status |
||
| 505 | */ |
||
| 506 | public function LibStatut($status, $mode = 0) |
||
| 507 | { |
||
| 508 | // phpcs:enable |
||
| 509 | if (empty($this->labelstatus)) |
||
| 510 | { |
||
| 511 | global $langs; |
||
| 512 | //$langs->load("mymodule"); |
||
| 513 | $this->labelstatus[1] = $langs->trans('Enabled'); |
||
| 514 | $this->labelstatus[0] = $langs->trans('Disabled'); |
||
| 515 | } |
||
| 516 | |||
| 517 | if ($mode == 0) |
||
| 518 | { |
||
| 519 | return $this->labelstatus[$status]; |
||
| 520 | } |
||
| 521 | elseif ($mode == 1) |
||
| 522 | { |
||
| 523 | return $this->labelstatus[$status]; |
||
| 524 | } |
||
| 525 | elseif ($mode == 2) |
||
| 526 | { |
||
| 527 | if ($status == 1) return img_picto($this->labelstatus[$status], 'statut4', '', false, 0, 0, '', 'valignmiddle').' '.$this->labelstatus[$status]; |
||
| 528 | elseif ($status == 0) return img_picto($this->labelstatus[$status], 'statut5', '', false, 0, 0, '', 'valignmiddle').' '.$this->labelstatus[$status]; |
||
| 529 | } |
||
| 530 | elseif ($mode == 3) |
||
| 531 | { |
||
| 532 | if ($status == 1) return img_picto($this->labelstatus[$status], 'statut4', '', false, 0, 0, '', 'valignmiddle'); |
||
| 533 | elseif ($status == 0) return img_picto($this->labelstatus[$status], 'statut5', '', false, 0, 0, '', 'valignmiddle'); |
||
| 534 | } |
||
| 535 | elseif ($mode == 4) |
||
| 536 | { |
||
| 537 | if ($status == 1) return img_picto($this->labelstatus[$status], 'statut4', '', false, 0, 0, '', 'valignmiddle').' '.$this->labelstatus[$status]; |
||
| 538 | elseif ($status == 0) return img_picto($this->labelstatus[$status], 'statut5', '', false, 0, 0, '', 'valignmiddle').' '.$this->labelstatus[$status]; |
||
| 539 | } |
||
| 540 | elseif ($mode == 5) |
||
| 541 | { |
||
| 542 | if ($status == 1) return $this->labelstatus[$status].' '.img_picto($this->labelstatus[$status], 'statut4', '', false, 0, 0, '', 'valignmiddle'); |
||
| 543 | elseif ($status == 0) return $this->labelstatus[$status].' '.img_picto($this->labelstatus[$status], 'statut5', '', false, 0, 0, '', 'valignmiddle'); |
||
| 544 | } |
||
| 545 | elseif ($mode == 6) |
||
| 546 | { |
||
| 547 | if ($status == 1) return $this->labelstatus[$status].' '.img_picto($this->labelstatus[$status], 'statut4', '', false, 0, 0, '', 'valignmiddle'); |
||
| 548 | elseif ($status == 0) return $this->labelstatus[$status].' '.img_picto($this->labelstatus[$status], 'statut5', '', false, 0, 0, '', 'valignmiddle'); |
||
| 549 | } |
||
| 550 | } |
||
| 551 | |||
| 552 | /** |
||
| 553 | * Charge les informations d'ordre info dans l'objet commande |
||
| 554 | * |
||
| 555 | * @param int $id Id of order |
||
| 556 | * @return void |
||
| 557 | */ |
||
| 558 | public function info($id) |
||
| 559 | { |
||
| 560 | $sql = 'SELECT rowid, date_creation as datec, tms as datem,'; |
||
| 561 | $sql.= ' fk_user_creat, fk_user_modif'; |
||
| 562 | $sql.= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t'; |
||
| 563 | $sql.= ' WHERE t.rowid = '.$id; |
||
| 564 | $result=$this->db->query($sql); |
||
| 565 | if ($result) |
||
| 566 | { |
||
| 567 | if ($this->db->num_rows($result)) |
||
| 568 | { |
||
| 569 | $obj = $this->db->fetch_object($result); |
||
| 570 | $this->id = $obj->rowid; |
||
| 571 | if ($obj->fk_user_author) |
||
| 572 | { |
||
| 573 | $cuser = new User($this->db); |
||
| 574 | $cuser->fetch($obj->fk_user_author); |
||
| 575 | $this->user_creation = $cuser; |
||
| 576 | } |
||
| 577 | |||
| 578 | if ($obj->fk_user_valid) |
||
| 579 | { |
||
| 580 | $vuser = new User($this->db); |
||
| 581 | $vuser->fetch($obj->fk_user_valid); |
||
| 582 | $this->user_validation = $vuser; |
||
| 583 | } |
||
| 584 | |||
| 585 | if ($obj->fk_user_cloture) |
||
| 586 | { |
||
| 587 | $cluser = new User($this->db); |
||
| 588 | $cluser->fetch($obj->fk_user_cloture); |
||
| 589 | $this->user_cloture = $cluser; |
||
| 590 | } |
||
| 591 | |||
| 592 | $this->date_creation = $this->db->jdate($obj->datec); |
||
| 593 | $this->date_modification = $this->db->jdate($obj->datem); |
||
| 594 | $this->date_validation = $this->db->jdate($obj->datev); |
||
| 595 | } |
||
| 596 | |||
| 597 | $this->db->free($result); |
||
| 598 | } |
||
| 599 | else |
||
| 600 | { |
||
| 601 | dol_print_error($this->db); |
||
| 602 | } |
||
| 603 | } |
||
| 604 | |||
| 605 | /** |
||
| 606 | * Initialise object with example values |
||
| 607 | * Id must be 0 if object instance is a specimen |
||
| 608 | * |
||
| 609 | * @return void |
||
| 610 | */ |
||
| 611 | public function initAsSpecimen() |
||
| 612 | { |
||
| 613 | $this->initAsSpecimenCommon(); |
||
| 614 | } |
||
| 615 | |||
| 616 | /** |
||
| 617 | * Fetch filters |
||
| 618 | * |
||
| 619 | * @return int <0 if KO, >0 if OK |
||
| 620 | */ |
||
| 621 | public function fetchFilters() |
||
| 622 | { |
||
| 623 | $this->filters = array(); |
||
| 624 | |||
| 625 | $sql = 'SELECT rowid, type, rulevalue, status'; |
||
| 626 | $sql.= ' FROM '.MAIN_DB_PREFIX.'emailcollector_emailcollectorfilter'; |
||
| 627 | $sql.= ' WHERE fk_emailcollector = '.$this->id; |
||
| 628 | //$sql.= ' ORDER BY position'; |
||
| 629 | |||
| 630 | $resql = $this->db->query($sql); |
||
| 631 | if ($resql) |
||
| 632 | { |
||
| 633 | $num=$this->db->num_rows($resql); |
||
| 634 | $i = 0; |
||
| 635 | while($i < $num) |
||
| 636 | { |
||
| 637 | $obj=$this->db->fetch_object($resql); |
||
| 638 | $this->filters[$obj->rowid]=array('id'=>$obj->rowid, 'type'=>$obj->type, 'rulevalue'=>$obj->rulevalue, 'status'=>$obj->status); |
||
| 639 | $i++; |
||
| 640 | } |
||
| 641 | $this->db->free($resql); |
||
| 642 | } |
||
| 643 | else dol_print_error($this->db); |
||
| 644 | |||
| 645 | return 1; |
||
| 646 | } |
||
| 647 | |||
| 648 | /** |
||
| 649 | * Fetch actions |
||
| 650 | * |
||
| 651 | * @return int <0 if KO, >0 if OK |
||
| 652 | */ |
||
| 653 | public function fetchActions() |
||
| 654 | { |
||
| 655 | $this->actions = array(); |
||
| 656 | |||
| 657 | $sql = 'SELECT rowid, type, actionparam, status'; |
||
| 658 | $sql.= ' FROM '.MAIN_DB_PREFIX.'emailcollector_emailcollectoraction'; |
||
| 659 | $sql.= ' WHERE fk_emailcollector = '.$this->id; |
||
| 660 | $sql.= ' ORDER BY position'; |
||
| 661 | |||
| 662 | $resql = $this->db->query($sql); |
||
| 663 | if ($resql) |
||
| 664 | { |
||
| 665 | $num=$this->db->num_rows($resql); |
||
| 666 | $i = 0; |
||
| 667 | while($i < $num) |
||
| 668 | { |
||
| 669 | $obj=$this->db->fetch_object($resql); |
||
| 670 | $this->actions[$obj->rowid]=array('id'=>$obj->rowid, 'type'=>$obj->type, 'actionparam'=>$obj->actionparam, 'status'=>$obj->status); |
||
| 671 | $i++; |
||
| 672 | } |
||
| 673 | $this->db->free($resql); |
||
| 674 | } |
||
| 675 | else dol_print_error($this->db); |
||
| 676 | } |
||
| 677 | |||
| 678 | |||
| 679 | /** |
||
| 680 | * Return the connectstring to use with IMAP connection function |
||
| 681 | * |
||
| 682 | * @return string |
||
| 683 | */ |
||
| 684 | public function getConnectStringIMAP() |
||
| 685 | { |
||
| 686 | // Connect to IMAP |
||
| 687 | $flags ='/service=imap'; // IMAP |
||
| 688 | $flags.='/ssl'; // '/tls' |
||
| 689 | $flags.='/novalidate-cert'; |
||
| 690 | //$flags.='/readonly'; |
||
| 691 | //$flags.='/debug'; |
||
| 692 | |||
| 693 | $connectstringserver = '{'.$this->host.':993'.$flags.'}'; |
||
| 694 | |||
| 695 | return $connectstringserver; |
||
| 696 | } |
||
| 697 | |||
| 698 | /** |
||
| 699 | * Action executed by scheduler |
||
| 700 | * CAN BE A CRON TASK. In such a case, paramerts come from the schedule job setup field 'Parameters' |
||
| 701 | * |
||
| 702 | * @return int 0 if OK, <>0 if KO (this function is used also by cron so only 0 is OK) |
||
| 703 | */ |
||
| 704 | public function doCollect() |
||
| 705 | { |
||
| 706 | global $user; |
||
| 707 | |||
| 708 | $nberror = 0; |
||
| 709 | |||
| 710 | $arrayofcollectors = $this->fetchAll($user, 1); |
||
| 711 | |||
| 712 | // Loop on each collector |
||
| 713 | foreach($arrayofcollectors as $emailcollector) |
||
| 714 | { |
||
| 715 | $result = $emailcollector->doCollectOneCollector(); |
||
| 716 | dol_syslog("doCollect result = ".$result." for emailcollector->id = ".$emailcollector->id); |
||
| 717 | |||
| 718 | $this->error.='EmailCollector ID '.$emailcollector->id.':'.$emailcollector->error.'<br>'; |
||
| 719 | if (! empty($emailcollector->errors)) $this->error.=join('<br>', $emailcollector->errors); |
||
| 720 | $this->output.='EmailCollector ID '.$emailcollector->id.': '.$emailcollector->lastresult.'<br>'; |
||
| 721 | } |
||
| 722 | |||
| 723 | return $nberror; |
||
| 724 | } |
||
| 725 | |||
| 726 | /** |
||
| 727 | * overwitePropertiesOfObject |
||
| 728 | * |
||
| 729 | * @param object $object Current object |
||
| 730 | * @param string $actionparam Action parameters |
||
| 731 | * @param string $messagetext Body |
||
| 732 | * @param string $subject Subject |
||
| 733 | * @param string $header Header |
||
| 734 | * @return int 0=OK, Nb of error if error |
||
| 735 | */ |
||
| 736 | private function overwritePropertiesOfObject(&$object, $actionparam, $messagetext, $subject, $header) |
||
| 737 | { |
||
| 738 | $errorforthisaction = 0; |
||
| 739 | |||
| 740 | // Overwrite values with values extracted from source email |
||
| 741 | // $this->actionparam = 'opportunity_status=123;abc=EXTRACT:BODY:....' |
||
| 742 | $arrayvaluetouse = dolExplodeIntoArray($actionparam, ';', '='); |
||
| 743 | foreach($arrayvaluetouse as $propertytooverwrite => $valueforproperty) |
||
| 744 | { |
||
| 745 | $tmpclass=''; $tmpproperty=''; |
||
| 746 | $tmparray=explode('.', $propertytooverwrite); |
||
| 747 | if (count($tmparray) == 2) |
||
| 748 | { |
||
| 749 | $tmpclass=$tmparray[0]; |
||
| 750 | $tmpproperty=$tmparray[1]; |
||
| 751 | } |
||
| 752 | else |
||
| 753 | { |
||
| 754 | $tmpproperty=$tmparray[0]; |
||
| 755 | } |
||
| 756 | if ($tmpclass && ($tmpclass != $object->element)) continue; // Property is for another type of object |
||
| 757 | |||
| 758 | //if (property_exists($object, $tmpproperty) || preg_match('/^options_/', $tmpproperty)) |
||
| 759 | if ($tmpproperty) |
||
| 760 | { |
||
| 761 | $sourcestring=''; |
||
| 762 | $sourcefield=''; |
||
| 763 | $regexstring=''; |
||
| 764 | //$transformationstring=''; |
||
| 765 | $regforregex=array(); |
||
| 766 | if (preg_match('/^EXTRACT:([a-zA-Z0-9]+):(.*):([^:])$/', $valueforproperty, $regforregex)) |
||
| 767 | { |
||
| 768 | $sourcefield=$regforregex[1]; |
||
| 769 | $regexstring=$regforregex[2]; |
||
| 770 | //$transofrmationstring=$regforregex[3]; |
||
| 771 | } |
||
| 772 | elseif (preg_match('/^EXTRACT:([a-zA-Z0-9]+):(.*)$/', $valueforproperty, $regforregex)) |
||
| 773 | { |
||
| 774 | $sourcefield=$regforregex[1]; |
||
| 775 | $regexstring=$regforregex[2]; |
||
| 776 | } |
||
| 777 | if (! empty($sourcefield) && ! empty($regexstring)) |
||
| 778 | { |
||
| 779 | if (strtolower($sourcefield) == 'body') $sourcestring=$messagetext; |
||
| 780 | elseif (strtolower($sourcefield) == 'subject') $sourcestring=$subject; |
||
| 781 | elseif (strtolower($sourcefield) == 'header') $sourcestring=$header; |
||
| 782 | |||
| 783 | if ($sourcestring) |
||
| 784 | { |
||
| 785 | $regforval=array(); |
||
| 786 | $regexoptions=''; |
||
| 787 | if (strtolower($sourcefield) == 'body') $regexoptions='ms'; // The m means ^ and $ char is valid at each new line. The s means the char '.' is valid for new lines char too |
||
| 788 | if (strtolower($sourcefield) == 'header') $regexoptions='m'; // The m means ^ and $ char is valid at each new line. |
||
| 789 | |||
| 790 | //var_dump($tmpproperty.' - '.$regexstring.' - '.$regexoptions.' - '.$sourcestring); |
||
| 791 | if (preg_match('/'.$regexstring.'/'.$regexoptions, $sourcestring, $regforval)) |
||
| 792 | { |
||
| 793 | //var_dump($regforval[1]);exit; |
||
| 794 | // Overwrite param $tmpproperty |
||
| 795 | $object->$tmpproperty = isset($regforval[1])?trim($regforval[1]):null; |
||
| 796 | } |
||
| 797 | else |
||
| 798 | { |
||
| 799 | // Regex not found |
||
| 800 | $object->$tmpproperty = null; |
||
| 801 | } |
||
| 802 | } |
||
| 803 | else |
||
| 804 | { |
||
| 805 | // Nothing can be done for this param |
||
| 806 | $errorforthisaction++; |
||
| 807 | $this->error = 'The extract rule to use has on an unknown source (must be HEADER, SUBJECT or BODY)'; |
||
| 808 | $this->errors[] = $this->error; |
||
| 809 | } |
||
| 810 | } |
||
| 811 | elseif (preg_match('/^(SET|SETIFEMPTY):(.*)$/', $valueforproperty, $regforregex)) |
||
| 812 | { |
||
| 813 | $valuecurrent=''; |
||
| 814 | if (preg_match('/^options_/', $tmpproperty)) $valuecurrent = $object->array_options[preg_replace('/^options_/', '', $tmpproperty)]; |
||
| 815 | else $valuecurrent = $object->$tmpproperty; |
||
| 816 | |||
| 817 | if ($regforregex[1] == 'SET' || empty($valuecurrent)) |
||
| 818 | { |
||
| 819 | $valuetouse = $regforregex[2]; |
||
| 820 | $substitutionarray=array(); |
||
| 821 | $matcharray=array(); |
||
| 822 | preg_match_all('/__([a-z0-9]+(?:_[a-z0-9]+)?)__/i', $valuetouse, $matcharray); |
||
| 823 | //var_dump($tmpproperty.' - '.$object->$tmpproperty.' - '.$valuetouse); var_dump($matcharray); |
||
| 824 | if (is_array($matcharray[1])) // $matcharray[1] is array with list of substitution key found without the __ |
||
| 825 | { |
||
| 826 | foreach($matcharray[1] as $keytoreplace) |
||
| 827 | { |
||
| 828 | if ($keytoreplace && isset($object->$keytoreplace)) |
||
| 829 | { |
||
| 830 | $substitutionarray['__'.$keytoreplace.'__']=$object->$keytoreplace; |
||
| 831 | } |
||
| 832 | } |
||
| 833 | } |
||
| 834 | //var_dump($substitutionarray); |
||
| 835 | dol_syslog(var_export($substitutionarray, true)); |
||
| 836 | //var_dump($substitutionarray); |
||
| 837 | $valuetouse = make_substitutions($valuetouse, $substitutionarray); |
||
| 838 | if (preg_match('/^options_/', $tmpproperty)) $object->array_options[preg_replace('/^options_/', '', $tmpproperty)] = $valuetouse; |
||
| 839 | else $object->$tmpproperty = $valuetouse; |
||
| 840 | } |
||
| 841 | } |
||
| 842 | else |
||
| 843 | { |
||
| 844 | $errorforthisaction++; |
||
| 845 | $this->error = 'Bad syntax for description of action parameters: '.$actionparam; |
||
| 846 | $this->errors[] = $this->error; |
||
| 847 | } |
||
| 848 | } |
||
| 849 | } |
||
| 850 | |||
| 851 | return $errorforthisaction; |
||
| 852 | } |
||
| 853 | |||
| 854 | /** |
||
| 855 | * Execute collect for current collector loaded previously with fetch. |
||
| 856 | * |
||
| 857 | * @return int <0 if KO, >0 if OK |
||
| 858 | */ |
||
| 859 | public function doCollectOneCollector() |
||
| 1861 | } |
||
| 1862 | |||
| 1863 | |||
| 1864 | |||
| 1865 | // Loop to get part html and plain. Code found on PHP imap_fetchstructure documentation |
||
| 1866 | |||
| 1867 | /** |
||
| 1868 | * getmsg |
||
| 1869 | * |
||
| 1870 | * @param Object $mbox Structure |
||
| 1871 | * @param string $mid prefix |
||
| 1872 | * @return array Array with number and object |
||
| 1873 | */ |
||
| 1874 | private function getmsg($mbox, $mid) |
||
| 1895 | } |
||
| 1896 | } |
||
| 1897 | } |
||
| 1898 | |||
| 1899 | /* partno string |
||
| 1900 | 0 multipart/mixed |
||
| 1901 | 1 multipart/alternative |
||
| 1902 | 1.1 text/plain |
||
| 1903 | 1.2 text/html |
||
| 1904 | 2 message/rfc822 |
||
| 1905 | 2 multipart/mixed |
||
| 1906 | 2.1 multipart/alternative |
||
| 1907 | 2.1.1 text/plain |
||
| 1908 | 2.1.2 text/html |
||
| 1909 | 2.2 message/rfc822 |
||
| 1910 | 2.2 multipart/alternative |
||
| 1911 | 2.2.1 text/plain |
||
| 1912 | 2.2.2 text/html |
||
| 1913 | */ |
||
| 1914 | /** |
||
| 1915 | * Sub function for getpart(). Only called by createPartArray() and itself. |
||
| 1916 | * |
||
| 1917 | * @param Object $mbox Structure |
||
| 1918 | * @param string $mid Part no |
||
| 1919 | * @param Object $p Object p |
||
| 1920 | * @param string $partno Partno |
||
| 1921 | * @return void |
||
| 1922 | */ |
||
| 1923 | private function getpart($mbox, $mid, $p, $partno) |
||
| 1991 | } |
||
| 1992 | } |
||
| 1993 | } |
||
| 1994 | } |
||
| 1995 |