| Conditions | 42 |
| Paths | 1024 |
| Total Lines | 230 |
| Code Lines | 144 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 281 | public function doWarningOfPartnershipIfDolibarrBacklinkNotfound($maxpercall = 0) |
||
| 282 | { |
||
| 283 | global $conf, $langs, $user; |
||
| 284 | |||
| 285 | $managedfor = getDolGlobalString('PARTNERSHIP_IS_MANAGED_FOR'); |
||
| 286 | |||
| 287 | $partnership = new Partnership($this->db); |
||
| 288 | if (empty($maxpercall)) { |
||
| 289 | $maxpercall = getDolGlobalInt('PARTNERSHIP_MAX_WARNING_BACKLINK_PER_CALL', 10); |
||
| 290 | } |
||
| 291 | |||
| 292 | $langs->loadLangs(array("partnership", "member")); |
||
| 293 | |||
| 294 | $error = 0; |
||
| 295 | $erroremail = ''; |
||
| 296 | $this->output = ''; |
||
| 297 | $this->error = ''; |
||
| 298 | $partnershipsprocessed = array(); |
||
| 299 | $emailnotfound = ''; |
||
| 300 | $websitenotfound = ''; |
||
| 301 | |||
| 302 | /*$gracedelay = getDolGlobalInt('PARTNERSHIP_NBDAYS_AFTER_MEMBER_EXPIRATION_BEFORE_CANCEL'); |
||
| 303 | if ($gracedelay < 1) { |
||
| 304 | $this->error = 'BadValueForDelayBeforeCancelCheckSetup'; |
||
| 305 | return -1; |
||
| 306 | }*/ |
||
| 307 | |||
| 308 | $fk_partner = ($managedfor == 'member') ? 'fk_member' : 'fk_soc'; |
||
| 309 | |||
| 310 | dol_syslog(get_class($this) . "::doWarningOfPartnershipIfDolibarrBacklinkNotfound Warning of partnership"); |
||
| 311 | |||
| 312 | $now = dol_now(); |
||
| 313 | //$datetotest = dol_time_plus_duree($now, -1 * abs($gracedelay), 'd'); |
||
| 314 | |||
| 315 | $this->db->begin(); |
||
| 316 | |||
| 317 | $sql = "SELECT p.rowid, p.status, p." . $fk_partner; |
||
| 318 | $sql .= ", p.url_to_check, p.last_check_backlink"; |
||
| 319 | $sql .= ', partner.url, partner.email'; |
||
| 320 | $sql .= " FROM " . MAIN_DB_PREFIX . "partnership as p"; |
||
| 321 | if ($managedfor == 'member') { |
||
| 322 | $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "adherent as partner on (partner.rowid = p.fk_member)"; |
||
| 323 | } else { |
||
| 324 | $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "societe as partner on (partner.rowid = p.fk_soc)"; |
||
| 325 | } |
||
| 326 | $sql .= " WHERE p." . $fk_partner . " > 0"; |
||
| 327 | $sql .= " AND p.status = " . ((int) $partnership::STATUS_APPROVED); // Only accepted and not yet canceled |
||
| 328 | $sql .= " AND (p.last_check_backlink IS NULL OR p.last_check_backlink <= '" . $this->db->idate($now - 24 * 3600) . "')"; // Never more than 1 check every day to check that website contains a referral link. |
||
| 329 | $sql .= $this->db->order('p.rowid', 'ASC'); |
||
| 330 | // Limit is managed into loop later |
||
| 331 | |||
| 332 | $resql = $this->db->query($sql); |
||
| 333 | if ($resql) { |
||
| 334 | $numofexpiredmembers = $this->db->num_rows($resql); |
||
| 335 | $somethingdoneonpartnership = 0; |
||
| 336 | $ifetchpartner = 0; |
||
| 337 | while ($ifetchpartner < $numofexpiredmembers) { |
||
| 338 | $ifetchpartner++; |
||
| 339 | |||
| 340 | $obj = $this->db->fetch_object($resql); |
||
| 341 | if ($obj) { |
||
| 342 | if (!empty($partnershipsprocessed[$obj->rowid])) { |
||
| 343 | continue; |
||
| 344 | } |
||
| 345 | |||
| 346 | if ($somethingdoneonpartnership >= $maxpercall) { |
||
| 347 | dol_syslog("We reach the limit of " . $maxpercall . " partnership processed, so we quit loop for this batch doWarningOfPartnershipIfDolibarrBacklinkNotfound to avoid to reach email quota.", LOG_WARNING); |
||
| 348 | break; |
||
| 349 | } |
||
| 350 | |||
| 351 | $backlinkfound = 0; |
||
| 352 | |||
| 353 | $object = new Partnership($this->db); |
||
| 354 | $object->fetch($obj->rowid); |
||
| 355 | |||
| 356 | if ($managedfor == 'member') { |
||
| 357 | $fk_partner = $object->fk_member; |
||
| 358 | } else { |
||
| 359 | $fk_partner = $object->fk_soc; |
||
| 360 | } |
||
| 361 | |||
| 362 | $website = (empty($obj->url_to_check) ? $obj->url : $obj->url_to_check); |
||
| 363 | |||
| 364 | if (empty($website)) { |
||
| 365 | $websitenotfound .= ($websitenotfound ? ', ' : '') . 'Website not found for id="' . $fk_partner . '"' . "\n"; |
||
| 366 | } else { |
||
| 367 | $backlinkfound = $this->checkDolibarrBacklink($website); |
||
| 368 | } |
||
| 369 | |||
| 370 | if (!$backlinkfound) { |
||
| 371 | $tmpcount = $object->count_last_url_check_error + 1; |
||
| 372 | |||
| 373 | $nbminbacklinkerrorforcancel = (int) getDolGlobalString('PARTNERSHIP_MIN_BACKLINK_ERROR_FOR_CANCEL', 3); |
||
| 374 | $nbmaxbacklinkerrorforcancel = (int) getDolGlobalString('PARTNERSHIP_MAX_BACKLINK_ERROR_FOR_CANCEL', (int) $nbminbacklinkerrorforcancel + 2); |
||
| 375 | |||
| 376 | // If $nbminbacklinkerrorforemail = 0, no autoemail |
||
| 377 | if ($nbminbacklinkerrorforcancel > 0) { |
||
| 378 | if ($tmpcount > $nbminbacklinkerrorforcancel && $tmpcount <= $nbmaxbacklinkerrorforcancel) { // Send Warning Email |
||
| 379 | if (!empty($obj->email)) { |
||
| 380 | $emailnotfound .= ($emailnotfound ? ', ' : '') . 'Email not found for id="' . $fk_partner . '"' . "\n"; |
||
| 381 | } else { |
||
| 382 | // Example: 'SendingEmailOnPartnershipWillSoonBeCanceled' |
||
| 383 | $labeltemplate = '(' . getDolGlobalString('PARTNERSHIP_SENDMAIL_IF_NO_LINK', 'SendingEmailOnPartnershipWillSoonBeCanceled') . ')'; |
||
| 384 | |||
| 385 | dol_syslog("Now we will send an email to partner id=" . $fk_partner . " with label " . $labeltemplate); |
||
| 386 | |||
| 387 | // Send deployment email |
||
| 388 | $formmail = new FormMail($this->db); |
||
| 389 | |||
| 390 | // Define output language |
||
| 391 | $outputlangs = $langs; |
||
| 392 | $newlang = ''; |
||
| 393 | if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang) && GETPOST('lang_id', 'aZ09')) { |
||
| 394 | $newlang = GETPOST('lang_id', 'aZ09'); |
||
| 395 | } |
||
| 396 | if (!empty($newlang)) { |
||
| 397 | $outputlangs = new Translate("", $conf); |
||
| 398 | $outputlangs->setDefaultLang($newlang); |
||
| 399 | $outputlangs->loadLangs(array('main', 'member', 'partnership')); |
||
| 400 | } |
||
| 401 | |||
| 402 | $arraydefaultmessage = $formmail->getEMailTemplate($this->db, 'partnership_send', $user, $outputlangs, 0, 1, $labeltemplate); |
||
| 403 | |||
| 404 | $substitutionarray = getCommonSubstitutionArray($outputlangs, 0, null, $object); |
||
| 405 | complete_substitutions_array($substitutionarray, $outputlangs, $object); |
||
| 406 | |||
| 407 | $subject = make_substitutions($arraydefaultmessage->topic, $substitutionarray, $outputlangs); |
||
| 408 | $msg = make_substitutions($arraydefaultmessage->content, $substitutionarray, $outputlangs); |
||
| 409 | $from = dol_string_nospecial($conf->global->MAIN_INFO_SOCIETE_NOM, ' ', array(",")) . ' <' . getDolGlobalString('MAIN_INFO_SOCIETE_MAIL') . '>'; |
||
| 410 | |||
| 411 | $sendto = $obj->email; |
||
| 412 | |||
| 413 | $trackid = 'par' . $object->id; |
||
| 414 | $sendcontext = 'standard'; |
||
| 415 | |||
| 416 | $cmail = new CMailFile($subject, $sendto, $from, $msg, array(), array(), array(), '', '', 0, 1, '', '', $trackid, '', $sendcontext); |
||
| 417 | |||
| 418 | $result = $cmail->sendfile(); |
||
| 419 | |||
| 420 | if (!$result || !empty($cmail->error) || !empty($cmail->errors)) { |
||
| 421 | $erroremail .= ($erroremail ? ', ' : '') . $cmail->error; |
||
| 422 | $this->errors[] = $cmail->error; |
||
| 423 | if (is_array($cmail->errors) && count($cmail->errors) > 0) { |
||
| 424 | $this->errors += $cmail->errors; |
||
| 425 | } |
||
| 426 | } else { |
||
| 427 | // Initialisation of datas of object to call trigger |
||
| 428 | if (is_object($object)) { |
||
| 429 | $actiontypecode = 'AC_OTH_AUTO'; // Event insert into agenda automatically |
||
| 430 | $attachedfiles = array(); |
||
| 431 | |||
| 432 | if ($managedfor != 'member') { |
||
| 433 | $object->socid = $fk_partner; // To link to a company |
||
| 434 | } |
||
| 435 | $object->actiontypecode = $actiontypecode; // Type of event ('AC_OTH', 'AC_OTH_AUTO', 'AC_XXX'...) |
||
| 436 | $object->actionmsg = $arraydefaultmessage->topic . "\n" . $arraydefaultmessage->content; // Long text |
||
| 437 | $object->actionmsg2 = $langs->transnoentities("PartnershipSentByEMail", $object->ref); |
||
| 438 | ; // Short text ($langs->transnoentities('MailSentByTo')...); |
||
| 439 | if (getDolGlobalString('MAIN_MAIL_REPLACE_EVENT_TITLE_BY_EMAIL_SUBJECT')) { |
||
| 440 | $object->actionmsg2 = $subject; // Short text |
||
| 441 | } |
||
| 442 | |||
| 443 | $object->trackid = $trackid; |
||
| 444 | $object->fk_element = $object->id; |
||
| 445 | $object->elementtype = $object->element; |
||
| 446 | if (is_array($attachedfiles) && count($attachedfiles) > 0) { |
||
| 447 | $object->attachedfiles = $attachedfiles; |
||
| 448 | } |
||
| 449 | |||
| 450 | $object->email_from = $from; |
||
| 451 | $object->email_subject = $subject; |
||
| 452 | $object->email_to = $sendto; |
||
| 453 | $object->email_subject = $subject; |
||
| 454 | |||
| 455 | $triggersendname = 'PARTNERSHIP_SENTBYMAIL'; |
||
| 456 | // Call of triggers (you should have set $triggersendname to execute trigger) |
||
| 457 | if (!empty($triggersendname)) { |
||
| 458 | $result = $object->call_trigger($triggersendname, $user); |
||
| 459 | if ($result < 0) { |
||
| 460 | $error++; |
||
| 461 | } |
||
| 462 | } |
||
| 463 | // End call of triggers |
||
| 464 | } |
||
| 465 | } |
||
| 466 | } |
||
| 467 | } elseif ($tmpcount > $nbmaxbacklinkerrorforcancel) { // Cancel Partnership |
||
| 468 | $object->status = $object::STATUS_CANCELED; |
||
| 469 | $object->reason_decline_or_cancel = $langs->trans('BacklinkNotFoundOnPartnerWebsite'); |
||
| 470 | } |
||
| 471 | } |
||
| 472 | |||
| 473 | $object->count_last_url_check_error = $tmpcount; |
||
| 474 | } else { |
||
| 475 | $object->count_last_url_check_error = 0; |
||
| 476 | $object->reason_decline_or_cancel = ''; |
||
| 477 | } |
||
| 478 | |||
| 479 | $partnershipsprocessed[$object->id] = $object->ref; |
||
| 480 | |||
| 481 | $object->last_check_backlink = $now; |
||
| 482 | |||
| 483 | $object->update($user); |
||
| 484 | } |
||
| 485 | } |
||
| 486 | } else { |
||
| 487 | $error++; |
||
| 488 | $this->error = $this->db->lasterror(); |
||
| 489 | } |
||
| 490 | |||
| 491 | if (!$error) { |
||
| 492 | $this->db->commit(); |
||
| 493 | $this->output = ""; |
||
| 494 | } else { |
||
| 495 | $this->db->rollback(); |
||
| 496 | $this->output = "Rollback after error\n"; |
||
| 497 | } |
||
| 498 | $this->output .= $numofexpiredmembers . ' partnership checked' . "\n"; |
||
| 499 | if ($erroremail) { |
||
| 500 | $this->output .= '. Got errors when sending some email : ' . $erroremail . "\n"; |
||
| 501 | } |
||
| 502 | if ($emailnotfound) { |
||
| 503 | $this->output .= '. Email not found for some partner : ' . $emailnotfound . "\n"; |
||
| 504 | } |
||
| 505 | if ($websitenotfound) { |
||
| 506 | $this->output .= '. Website not found for some partner : ' . $websitenotfound . "\n"; |
||
| 507 | } |
||
| 508 | $this->output .= "\nSQL used to find partnerships to scan: " . $sql; |
||
| 509 | |||
| 510 | return ($error ? 1 : 0); |
||
| 511 | } |
||
| 560 |