| Total Complexity | 49 |
| Total Lines | 398 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like MailingTargets 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 MailingTargets, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 39 | class MailingTargets // This can't be abstract as it is used for some method |
||
| 40 | { |
||
| 41 | /** |
||
| 42 | * @var DoliDB Database handler (result of a new DoliDB) |
||
| 43 | */ |
||
| 44 | public $db; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var string Error code (or message) |
||
| 48 | */ |
||
| 49 | public $error = ''; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var array of errors |
||
| 53 | */ |
||
| 54 | public $errors; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var string Condition to be enabled |
||
| 58 | */ |
||
| 59 | public $enabled; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var string Name of the module |
||
| 63 | */ |
||
| 64 | public $name; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var string Description of the module |
||
| 68 | */ |
||
| 69 | public $desc; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var string Tooltip to show after description of the module |
||
| 73 | */ |
||
| 74 | public $tooltip = ''; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var string To store the SQL string used to find the recipients |
||
| 78 | */ |
||
| 79 | public $sql; |
||
| 80 | |||
| 81 | |||
| 82 | public $evenunsubscribe = 0; // Set this to 1 if you want to flag you also want to include email in target that has opt-out. |
||
| 83 | |||
| 84 | |||
| 85 | /** |
||
| 86 | * Constructor |
||
| 87 | * |
||
| 88 | * @param DoliDB $db Database handler |
||
| 89 | */ |
||
| 90 | public function __construct($db) |
||
| 91 | { |
||
| 92 | $this->db = $db; |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Return description of email selector |
||
| 97 | * |
||
| 98 | * @return string Return translation of module label. Try translation of $this->name then translation of 'MailingModuleDesc'.$this->name, or $this->desc if not found |
||
| 99 | */ |
||
| 100 | public function getDesc() |
||
| 101 | { |
||
| 102 | global $langs, $form; |
||
| 103 | |||
| 104 | $langs->load("mails"); |
||
| 105 | $transstring = "MailingModuleDesc" . $this->name; |
||
| 106 | $s = ''; |
||
| 107 | |||
| 108 | if ($langs->trans($this->name) != $this->name) { |
||
| 109 | $s = $langs->trans($this->name); |
||
| 110 | } elseif ($langs->trans($transstring) != $transstring) { |
||
| 111 | $s = $langs->trans($transstring); |
||
| 112 | } else { |
||
| 113 | $s = $this->desc; |
||
| 114 | } |
||
| 115 | |||
| 116 | if ($this->tooltip && is_object($form)) { |
||
| 117 | $s .= ' ' . $form->textwithpicto('', $langs->trans($this->tooltip), 1, 1); |
||
| 118 | } |
||
| 119 | return $s; |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Return number of records for email selector |
||
| 124 | * |
||
| 125 | * @return integer Example |
||
| 126 | */ |
||
| 127 | public function getNbOfRecords() |
||
| 128 | { |
||
| 129 | return 0; |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Retourne nombre de destinataires |
||
| 134 | * |
||
| 135 | * @param string $sql Sql request to count |
||
| 136 | * @return int|string Nb of recipient, or <0 if error, or '' if NA |
||
| 137 | */ |
||
| 138 | public function getNbOfRecipients($sql) |
||
| 139 | { |
||
| 140 | $result = $this->db->query($sql); |
||
| 141 | if ($result) { |
||
| 142 | $total = 0; |
||
| 143 | while ($obj = $this->db->fetch_object($result)) { |
||
| 144 | $total += $obj->nb; |
||
| 145 | } |
||
| 146 | return $total; |
||
| 147 | } else { |
||
| 148 | $this->error = $this->db->lasterror(); |
||
| 149 | return -1; |
||
| 150 | } |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Affiche formulaire de filtre qui apparait dans page de selection |
||
| 155 | * des destinataires de mailings |
||
| 156 | * |
||
| 157 | * @return string Retourne zone select |
||
| 158 | */ |
||
| 159 | public function formFilter() |
||
| 160 | { |
||
| 161 | return ''; |
||
| 162 | } |
||
| 163 | |||
| 164 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Met a jour nombre de destinataires |
||
| 168 | * |
||
| 169 | * @param int $mailing_id Id of emailing |
||
| 170 | * @return int Return integer < 0 si erreur, nb destinataires si ok |
||
| 171 | */ |
||
| 172 | public function update_nb($mailing_id) |
||
| 173 | { |
||
| 174 | // phpcs:enable |
||
| 175 | // Mise a jour nombre de destinataire dans table des mailings |
||
| 176 | $sql = "SELECT COUNT(*) nb FROM " . MAIN_DB_PREFIX . "mailing_cibles"; |
||
| 177 | $sql .= " WHERE fk_mailing = " . ((int)$mailing_id); |
||
| 178 | $result = $this->db->query($sql); |
||
| 179 | if ($result) { |
||
| 180 | $obj = $this->db->fetch_object($result); |
||
| 181 | $nb = $obj->nb; |
||
| 182 | |||
| 183 | $sql = "UPDATE " . MAIN_DB_PREFIX . "mailing"; |
||
| 184 | $sql .= " SET nbemail = " . ((int)$nb) . " WHERE rowid = " . ((int)$mailing_id); |
||
| 185 | if (!$this->db->query($sql)) { |
||
| 186 | dol_syslog($this->db->error()); |
||
| 187 | $this->error = $this->db->error(); |
||
| 188 | return -1; |
||
| 189 | } |
||
| 190 | } else { |
||
| 191 | return -1; |
||
| 192 | } |
||
| 193 | return $nb; |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Add a list of targets into the database |
||
| 198 | * |
||
| 199 | * @param int $mailing_id Id of emailing |
||
| 200 | * @param array $cibles Array with targets |
||
| 201 | * @return int Return integer < 0 if error, nb added if OK |
||
| 202 | */ |
||
| 203 | public function addTargetsToDatabase($mailing_id, $cibles) |
||
| 204 | { |
||
| 205 | global $conf; |
||
| 206 | |||
| 207 | $this->db->begin(); |
||
| 208 | |||
| 209 | // Insert emailing targets from array into database |
||
| 210 | $j = 0; |
||
| 211 | $num = count($cibles); |
||
| 212 | foreach ($cibles as $targetarray) { |
||
| 213 | if (!empty($targetarray['email'])) { // avoid empty email address |
||
| 214 | $sql = "INSERT INTO " . MAIN_DB_PREFIX . "mailing_cibles"; |
||
| 215 | $sql .= " (fk_mailing,"; |
||
| 216 | $sql .= " fk_contact,"; |
||
| 217 | $sql .= " lastname, firstname, email, other, source_url, source_id,"; |
||
| 218 | $sql .= " tag,"; |
||
| 219 | $sql .= " source_type)"; |
||
| 220 | $sql .= " VALUES (" . ((int)$mailing_id) . ","; |
||
| 221 | $sql .= (empty($targetarray['fk_contact']) ? '0' : (int)$targetarray['fk_contact']) . ","; |
||
| 222 | $sql .= "'" . $this->db->escape($targetarray['lastname']) . "',"; |
||
| 223 | $sql .= "'" . $this->db->escape($targetarray['firstname']) . "',"; |
||
| 224 | $sql .= "'" . $this->db->escape($targetarray['email']) . "',"; |
||
| 225 | $sql .= "'" . $this->db->escape($targetarray['other']) . "',"; |
||
| 226 | $sql .= "'" . $this->db->escape($targetarray['source_url']) . "',"; |
||
| 227 | $sql .= (empty($targetarray['source_id']) ? 'null' : "'" . $this->db->escape($targetarray['source_id']) . "'") . ","; |
||
| 228 | $sql .= "'" . $this->db->escape(dol_hash($conf->file->instance_unique_id . ";" . $targetarray['email'] . ";" . $targetarray['lastname'] . ";" . ((int)$mailing_id) . ";" . getDolGlobalString('MAILING_EMAIL_UNSUBSCRIBE_KEY'), 'md5')) . "',"; |
||
| 229 | $sql .= "'" . $this->db->escape($targetarray['source_type']) . "')"; |
||
| 230 | dol_syslog(__METHOD__, LOG_DEBUG); |
||
| 231 | $result = $this->db->query($sql); |
||
| 232 | if ($result) { |
||
| 233 | $j++; |
||
| 234 | } else { |
||
| 235 | if ($this->db->errno() != 'DB_ERROR_RECORD_ALREADY_EXISTS') { |
||
| 236 | // Si erreur autre que doublon |
||
| 237 | dol_syslog($this->db->error() . ' : ' . $targetarray['email']); |
||
| 238 | $this->error = $this->db->error() . ' : ' . $targetarray['email']; |
||
| 239 | $this->db->rollback(); |
||
| 240 | return -1; |
||
| 241 | } |
||
| 242 | } |
||
| 243 | } |
||
| 244 | } |
||
| 245 | |||
| 246 | dol_syslog(__METHOD__ . ": mailing " . $j . " targets added"); |
||
| 247 | |||
| 248 | /* |
||
| 249 | //Update the status to show thirdparty mail that don't want to be contacted anymore' |
||
| 250 | $sql = "UPDATE ".MAIN_DB_PREFIX."mailing_cibles"; |
||
| 251 | $sql .= " SET statut=3"; |
||
| 252 | $sql .= " WHERE fk_mailing = ".((int) $mailing_id)." AND email in (SELECT email FROM ".MAIN_DB_PREFIX."societe where fk_stcomm=-1)"; |
||
| 253 | $sql .= " AND source_type='thirdparty'"; |
||
| 254 | dol_syslog(__METHOD__.": mailing update status to display thirdparty mail that do not want to be contacted"); |
||
| 255 | $result=$this->db->query($sql); |
||
| 256 | |||
| 257 | //Update the status to show contact mail that don't want to be contacted anymore' |
||
| 258 | $sql = "UPDATE ".MAIN_DB_PREFIX."mailing_cibles"; |
||
| 259 | $sql .= " SET statut=3"; |
||
| 260 | $sql .= " WHERE fk_mailing = ".((int) $mailing_id)." AND source_type='contact' AND (email in (SELECT sc.email FROM ".MAIN_DB_PREFIX."socpeople AS sc "; |
||
| 261 | $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe s ON s.rowid=sc.fk_soc WHERE s.fk_stcomm=-1 OR no_email=1))"; |
||
| 262 | dol_syslog(__METHOD__.": mailing update status to display contact mail that do not want to be contacted",LOG_DEBUG); |
||
| 263 | $result=$this->db->query($sql); |
||
| 264 | */ |
||
| 265 | |||
| 266 | if (empty($this->evenunsubscribe)) { |
||
| 267 | $sql = "UPDATE " . MAIN_DB_PREFIX . "mailing_cibles as mc"; |
||
| 268 | $sql .= " SET mc.statut = 3"; |
||
| 269 | $sql .= " WHERE mc.fk_mailing = " . ((int)$mailing_id); |
||
| 270 | $sql .= " AND EXISTS (SELECT rowid FROM " . MAIN_DB_PREFIX . "mailing_unsubscribe as mu WHERE mu.email = mc.email and mu.entity = " . ((int)$conf->entity) . ")"; |
||
| 271 | |||
| 272 | dol_syslog(__METHOD__ . ":mailing update status to display emails that do not want to be contacted anymore", LOG_DEBUG); |
||
| 273 | $result = $this->db->query($sql); |
||
| 274 | if (!$result) { |
||
| 275 | dol_print_error($this->db); |
||
| 276 | } |
||
| 277 | } |
||
| 278 | |||
| 279 | // Update nb of recipient into emailing record |
||
| 280 | $this->update_nb($mailing_id); |
||
| 281 | |||
| 282 | $this->db->commit(); |
||
| 283 | |||
| 284 | return $j; |
||
| 285 | } |
||
| 286 | |||
| 287 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Supprime tous les destinataires de la table des cibles |
||
| 291 | * |
||
| 292 | * @param int $mailing_id Id of emailing |
||
| 293 | * @return void |
||
| 294 | */ |
||
| 295 | public function clear_target($mailing_id) |
||
| 306 | } |
||
| 307 | |||
| 308 | |||
| 309 | /** |
||
| 310 | * Return list of widget. Function used by admin page htdoc/admin/widget. |
||
| 311 | * List is sorted by widget filename so by priority to run. |
||
| 312 | * |
||
| 313 | * @param array $forcedir null=All default directories. This parameter is used by modulebuilder module only. |
||
| 314 | * @return array Array list of widget |
||
| 315 | */ |
||
| 316 | public static function getEmailingSelectorsList($forcedir = null) |
||
| 437 | } |
||
| 438 | } |
||
| 439 |