| Conditions | 126 |
| Paths | 4902 |
| Total Lines | 700 |
| Code Lines | 573 |
| 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 |
||
| 616 | public function send($notifcode, $object, $filename_list = array(), $mimetype_list = array(), $mimefilename_list = array()) |
||
| 617 | { |
||
| 618 | global $user, $conf, $langs, $mysoc; |
||
| 619 | global $hookmanager; |
||
| 620 | global $dolibarr_main_url_root; |
||
| 621 | global $action; |
||
| 622 | |||
| 623 | // Complete the array Notify::$arrayofnotifsupported |
||
| 624 | if (!is_object($hookmanager)) { |
||
| 625 | include_once DOL_DOCUMENT_ROOT . '/core/class/hookmanager.class.php'; |
||
| 626 | $hookmanager = new HookManager($this->db); |
||
| 627 | } |
||
| 628 | $hookmanager->initHooks(array('notification')); |
||
| 629 | |||
| 630 | $parameters = array('notifcode' => $notifcode); |
||
| 631 | $reshook = $hookmanager->executeHooks('notifsupported', $parameters, $object, $action); |
||
| 632 | if (empty($reshook)) { |
||
| 633 | if (!empty($hookmanager->resArray['arrayofnotifsupported'])) { |
||
| 634 | Notify::$arrayofnotifsupported = array_merge(Notify::$arrayofnotifsupported, $hookmanager->resArray['arrayofnotifsupported']); |
||
| 635 | } |
||
| 636 | } |
||
| 637 | |||
| 638 | // If the trigger code is not managed by the Notification module |
||
| 639 | if (!in_array($notifcode, Notify::$arrayofnotifsupported)) { |
||
| 640 | return 0; |
||
| 641 | } |
||
| 642 | |||
| 643 | include_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php'; |
||
| 644 | |||
| 645 | dol_syslog(get_class($this) . "::send notifcode=" . $notifcode . ", object id=" . $object->id); |
||
| 646 | |||
| 647 | $langs->load("other"); |
||
| 648 | |||
| 649 | // Define $urlwithroot |
||
| 650 | $urlwithouturlroot = preg_replace('/' . preg_quote(DOL_URL_ROOT, '/') . '$/i', '', trim($dolibarr_main_url_root)); |
||
| 651 | $urlwithroot = $urlwithouturlroot . DOL_URL_ROOT; // This is to use external domain name found into config file |
||
| 652 | //$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current |
||
| 653 | |||
| 654 | // Define some vars |
||
| 655 | $application = 'Dolibarr'; |
||
| 656 | if (getDolGlobalString('MAIN_APPLICATION_TITLE')) { |
||
| 657 | $application = getDolGlobalString('MAIN_APPLICATION_TITLE'); |
||
| 658 | } |
||
| 659 | $from = getDolGlobalString('NOTIFICATION_EMAIL_FROM'); |
||
| 660 | $object_type = ''; |
||
| 661 | $link = ''; |
||
| 662 | $num = 0; |
||
| 663 | $error = 0; |
||
| 664 | |||
| 665 | $oldref = (empty($object->oldref) ? $object->ref : $object->oldref); |
||
| 666 | $newref = (empty($object->newref) ? $object->ref : $object->newref); |
||
| 667 | |||
| 668 | $sql = ''; |
||
| 669 | |||
| 670 | // Check notification per third party |
||
| 671 | if (!empty($object->socid) && $object->socid > 0) { |
||
| 672 | $sql .= "SELECT 'tocontactid' as type_target, c.email, c.rowid as cid, c.lastname, c.firstname, c.default_lang,"; |
||
| 673 | $sql .= " a.rowid as adid, a.label, a.code, n.rowid, n.threshold, n.context, n.type"; |
||
| 674 | $sql .= " FROM " . $this->db->prefix() . "socpeople as c,"; |
||
| 675 | $sql .= " " . $this->db->prefix() . "c_action_trigger as a,"; |
||
| 676 | $sql .= " " . $this->db->prefix() . "notify_def as n,"; |
||
| 677 | $sql .= " " . $this->db->prefix() . "societe as s"; |
||
| 678 | $sql .= " WHERE n.fk_contact = c.rowid AND a.rowid = n.fk_action"; |
||
| 679 | $sql .= " AND n.fk_soc = s.rowid"; |
||
| 680 | $sql .= " AND c.statut = 1"; |
||
| 681 | if (is_numeric($notifcode)) { |
||
| 682 | $sql .= " AND n.fk_action = " . ((int) $notifcode); // Old usage |
||
| 683 | } else { |
||
| 684 | $sql .= " AND a.code = '" . $this->db->escape($notifcode) . "'"; // New usage |
||
| 685 | } |
||
| 686 | $sql .= " AND s.rowid = " . ((int) $object->socid); |
||
| 687 | |||
| 688 | $sql .= "\nUNION\n"; |
||
| 689 | } |
||
| 690 | |||
| 691 | // Check notification per user |
||
| 692 | $sql .= "SELECT 'touserid' as type_target, c.email, c.rowid as cid, c.lastname, c.firstname, c.lang as default_lang,"; |
||
| 693 | $sql .= " a.rowid as adid, a.label, a.code, n.rowid, n.threshold, n.context, n.type"; |
||
| 694 | $sql .= " FROM " . $this->db->prefix() . "user as c,"; |
||
| 695 | $sql .= " " . $this->db->prefix() . "c_action_trigger as a,"; |
||
| 696 | $sql .= " " . $this->db->prefix() . "notify_def as n"; |
||
| 697 | $sql .= " WHERE n.fk_user = c.rowid AND a.rowid = n.fk_action"; |
||
| 698 | $sql .= " AND c.statut = 1"; |
||
| 699 | if (is_numeric($notifcode)) { |
||
| 700 | $sql .= " AND n.fk_action = " . ((int) $notifcode); // Old usage |
||
| 701 | } else { |
||
| 702 | $sql .= " AND a.code = '" . $this->db->escape($notifcode) . "'"; // New usage |
||
| 703 | } |
||
| 704 | |||
| 705 | // Check notification fixed |
||
| 706 | // TODO Move part found after, into a sql here |
||
| 707 | |||
| 708 | |||
| 709 | // Loop on all notifications enabled |
||
| 710 | $result = $this->db->query($sql); |
||
| 711 | if ($result) { |
||
| 712 | $num = $this->db->num_rows($result); |
||
| 713 | $projtitle = ''; |
||
| 714 | if (is_object($object->project) || $object->fetch_project() > 0) { |
||
| 715 | $projtitle = '(' . $object->project->title . ')'; |
||
| 716 | } |
||
| 717 | |||
| 718 | if ($num > 0) { |
||
| 719 | $i = 0; |
||
| 720 | while ($i < $num && !$error) { // For each notification couple defined (third party/actioncode) |
||
| 721 | $obj = $this->db->fetch_object($result); |
||
| 722 | |||
| 723 | $sendto = dolGetFirstLastname($obj->firstname, $obj->lastname) . " <" . $obj->email . ">"; |
||
| 724 | $notifcodedefid = $obj->adid; |
||
| 725 | $trackid = ''; |
||
| 726 | if ($obj->type_target == 'tocontactid') { |
||
| 727 | $trackid = 'ctc' . $obj->cid; |
||
| 728 | } |
||
| 729 | if ($obj->type_target == 'touserid') { |
||
| 730 | $trackid = 'use' . $obj->cid; |
||
| 731 | } |
||
| 732 | |||
| 733 | if (dol_strlen($obj->email)) { |
||
| 734 | // Set output language |
||
| 735 | $outputlangs = $langs; |
||
| 736 | if ($obj->default_lang && $obj->default_lang != $langs->defaultlang) { |
||
| 737 | $outputlangs = new Translate('', $conf); |
||
| 738 | $outputlangs->setDefaultLang($obj->default_lang); |
||
| 739 | $outputlangs->loadLangs(array("main", "other")); |
||
| 740 | } |
||
| 741 | |||
| 742 | $appli = $mysoc->name; |
||
| 743 | |||
| 744 | $subject = '[' . $appli . '] ' . $outputlangs->transnoentitiesnoconv("DolibarrNotification") . ($projtitle ? ' ' . $projtitle : ''); |
||
| 745 | |||
| 746 | switch ($notifcode) { |
||
| 747 | case 'BILL_CANCEL': |
||
| 748 | $link = '<a href="' . $urlwithroot . '/compta/facture/card.php?facid=' . $object->id . '&entity=' . $object->entity . '">' . $newref . '</a>'; |
||
| 749 | $dir_output = $conf->facture->dir_output . "/" . get_exdir(0, 0, 0, 1, $object, 'invoice'); |
||
| 750 | $object_type = 'facture'; |
||
| 751 | $mesg = $outputlangs->transnoentitiesnoconv("EMailTextInvoiceCanceled", $link); |
||
| 752 | break; |
||
| 753 | case 'BILL_VALIDATE': |
||
| 754 | $link = '<a href="' . $urlwithroot . '/compta/facture/card.php?facid=' . $object->id . '&entity=' . $object->entity . '">' . $newref . '</a>'; |
||
| 755 | $dir_output = $conf->facture->dir_output . "/" . get_exdir(0, 0, 0, 1, $object, 'invoice'); |
||
| 756 | $object_type = 'facture'; |
||
| 757 | $mesg = $outputlangs->transnoentitiesnoconv("EMailTextInvoiceValidated", $link); |
||
| 758 | break; |
||
| 759 | case 'BILL_PAYED': |
||
| 760 | $link = '<a href="' . $urlwithroot . '/compta/facture/card.php?facid=' . $object->id . '&entity=' . $object->entity . '">' . $newref . '</a>'; |
||
| 761 | $dir_output = $conf->facture->dir_output . "/" . get_exdir(0, 0, 0, 1, $object, 'invoice'); |
||
| 762 | $object_type = 'facture'; |
||
| 763 | $mesg = $outputlangs->transnoentitiesnoconv("EMailTextInvoicePayed", $link); |
||
| 764 | break; |
||
| 765 | case 'ORDER_CANCEL': |
||
| 766 | $link = '<a href="' . $urlwithroot . '/commande/card.php?id=' . $object->id . '&entity=' . $object->entity . '">' . $newref . '</a>'; |
||
| 767 | $dir_output = $conf->commande->dir_output . "/" . get_exdir(0, 0, 0, 1, $object, 'commande'); |
||
| 768 | $object_type = 'order'; |
||
| 769 | $mesg = $outputlangs->transnoentitiesnoconv("EMailTextOrderCanceled", $link); |
||
| 770 | break; |
||
| 771 | case 'ORDER_VALIDATE': |
||
| 772 | $link = '<a href="' . $urlwithroot . '/commande/card.php?id=' . $object->id . '&entity=' . $object->entity . '">' . $newref . '</a>'; |
||
| 773 | $dir_output = $conf->commande->dir_output . "/" . get_exdir(0, 0, 0, 1, $object, 'commande'); |
||
| 774 | $object_type = 'order'; |
||
| 775 | $mesg = $outputlangs->transnoentitiesnoconv("EMailTextOrderValidated", $link); |
||
| 776 | break; |
||
| 777 | case 'ORDER_CLOSE': |
||
| 778 | $link = '<a href="' . $urlwithroot . '/commande/card.php?id=' . $object->id . '&entity=' . $object->entity . '">' . $newref . '</a>'; |
||
| 779 | $dir_output = $conf->commande->dir_output . "/" . get_exdir(0, 0, 0, 1, $object, 'commande'); |
||
| 780 | $object_type = 'order'; |
||
| 781 | $labeltouse = getDolGlobalString('ORDER_CLOSE_TEMPLATE'); |
||
| 782 | $mesg = $outputlangs->transnoentitiesnoconv("EMailTextOrderClose", $link); |
||
| 783 | break; |
||
| 784 | case 'PROPAL_VALIDATE': |
||
| 785 | $link = '<a href="' . $urlwithroot . '/comm/propal/card.php?id=' . $object->id . '&entity=' . $object->entity . '">' . $newref . '</a>'; |
||
| 786 | $dir_output = $conf->propal->multidir_output[$object->entity] . "/" . get_exdir(0, 0, 0, 1, $object, 'propal'); |
||
| 787 | $object_type = 'propal'; |
||
| 788 | $mesg = $outputlangs->transnoentitiesnoconv("EMailTextProposalValidated", $link); |
||
| 789 | break; |
||
| 790 | case 'PROPAL_CLOSE_REFUSED': |
||
| 791 | $link = '<a href="' . $urlwithroot . '/comm/propal/card.php?id=' . $object->id . '&entity=' . $object->entity . '">' . $newref . '</a>'; |
||
| 792 | $dir_output = $conf->propal->multidir_output[$object->entity] . "/" . get_exdir(0, 0, 0, 1, $object, 'propal'); |
||
| 793 | $object_type = 'propal'; |
||
| 794 | $labeltouse = getDolGlobalString('PROPAL_CLOSE_REFUSED_TEMPLATE'); |
||
| 795 | $mesg = $outputlangs->transnoentitiesnoconv("EMailTextProposalClosedRefused", $link); |
||
| 796 | if (!empty($object->context['closedfromonlinesignature'])) { |
||
| 797 | $mesg .= ' - From online page'; |
||
| 798 | } |
||
| 799 | break; |
||
| 800 | case 'PROPAL_CLOSE_SIGNED': |
||
| 801 | $link = '<a href="' . $urlwithroot . '/comm/propal/card.php?id=' . $object->id . '&entity=' . $object->entity . '">' . $newref . '</a>'; |
||
| 802 | $dir_output = $conf->propal->multidir_output[$object->entity] . "/" . get_exdir(0, 0, 0, 1, $object, 'propal'); |
||
| 803 | $object_type = 'propal'; |
||
| 804 | $mesg = $outputlangs->transnoentitiesnoconv("EMailTextProposalClosedSigned", $link); |
||
| 805 | if (!empty($object->context['closedfromonlinesignature'])) { |
||
| 806 | $mesg .= ' - From online page'; |
||
| 807 | } |
||
| 808 | break; |
||
| 809 | case 'FICHINTER_ADD_CONTACT': |
||
| 810 | $link = '<a href="' . $urlwithroot . '/fichinter/card.php?id=' . $object->id . '&entity=' . $object->entity . '">' . $newref . '</a>'; |
||
| 811 | $dir_output = $conf->ficheinter->dir_output; |
||
| 812 | $object_type = 'ficheinter'; |
||
| 813 | $mesg = $outputlangs->transnoentitiesnoconv("EMailTextInterventionAddedContact", $link); |
||
| 814 | break; |
||
| 815 | case 'FICHINTER_VALIDATE': |
||
| 816 | $link = '<a href="' . $urlwithroot . '/fichinter/card.php?id=' . $object->id . '&entity=' . $object->entity . '">' . $newref . '</a>'; |
||
| 817 | $dir_output = $conf->ficheinter->dir_output; |
||
| 818 | $object_type = 'ficheinter'; |
||
| 819 | $mesg = $outputlangs->transnoentitiesnoconv("EMailTextInterventionValidated", $link); |
||
| 820 | break; |
||
| 821 | case 'FICHINTER_CLOSE': |
||
| 822 | $link = '<a href="' . $urlwithroot . '/fichinter/card.php?id=' . $object->id . '&entity=' . $object->entity . '">' . $newref . '</a>'; |
||
| 823 | $dir_output = $conf->ficheinter->dir_output; |
||
| 824 | $object_type = 'ficheinter'; |
||
| 825 | $mesg = $outputlangs->transnoentitiesnoconv("EMailTextInterventionClosed", $link); |
||
| 826 | break; |
||
| 827 | case 'ORDER_SUPPLIER_VALIDATE': |
||
| 828 | $link = '<a href="' . $urlwithroot . '/fourn/commande/card.php?id=' . $object->id . '&entity=' . $object->entity . '">' . $newref . '</a>'; |
||
| 829 | $dir_output = $conf->fournisseur->commande->multidir_output[$object->entity] . "/" . get_exdir(0, 0, 0, 1, $object); |
||
| 830 | $object_type = 'order_supplier'; |
||
| 831 | $labeltouse = isset($conf->global->ORDER_SUPPLIER_VALIDATE_TEMPLATE) ? $conf->global->ORDER_SUPPLIER_VALIDATE_TEMPLATE : ''; |
||
| 832 | $mesg = $outputlangs->transnoentitiesnoconv("Hello") . ",\n\n"; |
||
| 833 | $mesg .= $outputlangs->transnoentitiesnoconv("EMailTextSupplierOrderValidatedBy", $link, $user->getFullName($outputlangs)); |
||
| 834 | $mesg .= "\n\n" . $outputlangs->transnoentitiesnoconv("Sincerely") . ".\n\n"; |
||
| 835 | break; |
||
| 836 | case 'ORDER_SUPPLIER_CANCEL': |
||
| 837 | $link = '<a href="' . $urlwithroot . '/fourn/commande/card.php?id=' . $object->id . '&entity=' . $object->entity . '">' . $newref . '</a>'; |
||
| 838 | $dir_output = $conf->fournisseur->commande->multidir_output[$object->entity] . "/" . get_exdir(0, 0, 0, 1, $object); |
||
| 839 | $object_type = 'order_supplier'; |
||
| 840 | $mesg = $outputlangs->transnoentitiesnoconv("Hello") . ",\n\n"; |
||
| 841 | $mesg .= $outputlangs->transnoentitiesnoconv("EMailTextSupplierOrderCanceledBy", $link, $user->getFullName($outputlangs)); |
||
| 842 | $mesg .= "\n\n" . $outputlangs->transnoentitiesnoconv("Sincerely") . ".\n\n"; |
||
| 843 | break; |
||
| 844 | case 'ORDER_SUPPLIER_APPROVE': |
||
| 845 | $link = '<a href="' . $urlwithroot . '/fourn/commande/card.php?id=' . $object->id . '&entity=' . $object->entity . '">' . $newref . '</a>'; |
||
| 846 | $dir_output = $conf->fournisseur->commande->multidir_output[$object->entity] . "/" . get_exdir(0, 0, 0, 1, $object); |
||
| 847 | $object_type = 'order_supplier'; |
||
| 848 | $labeltouse = isset($conf->global->ORDER_SUPPLIER_APPROVE_TEMPLATE) ? $conf->global->ORDER_SUPPLIER_APPROVE_TEMPLATE : ''; |
||
| 849 | $mesg = $outputlangs->transnoentitiesnoconv("Hello") . ",\n\n"; |
||
| 850 | $mesg .= $outputlangs->transnoentitiesnoconv("EMailTextSupplierOrderApprovedBy", $link, $user->getFullName($outputlangs)); |
||
| 851 | $mesg .= "\n\n" . $outputlangs->transnoentitiesnoconv("Sincerely") . ".\n\n"; |
||
| 852 | break; |
||
| 853 | case 'ORDER_SUPPLIER_SUBMIT': |
||
| 854 | $link = '<a href="' . $urlwithroot . '/fourn/commande/card.php?id=' . $object->id . '&entity=' . $object->entity . '">' . $newref . '</a>'; |
||
| 855 | $dir_output = $conf->fournisseur->commande->multidir_output[$object->entity] . "/" . get_exdir(0, 0, 0, 1, $object); |
||
| 856 | $object_type = 'order_supplier'; |
||
| 857 | $mesg = $outputlangs->transnoentitiesnoconv("Hello") . ",\n\n"; |
||
| 858 | $mesg .= $outputlangs->transnoentitiesnoconv("EMailTextSupplierOrderSubmittedBy", $link, $user->getFullName($outputlangs)); |
||
| 859 | $mesg .= "\n\n" . $outputlangs->transnoentitiesnoconv("Sincerely") . ".\n\n"; |
||
| 860 | break; |
||
| 861 | case 'ORDER_SUPPLIER_REFUSE': |
||
| 862 | $link = '<a href="' . $urlwithroot . '/fourn/commande/card.php?id=' . $object->id . '&entity=' . $object->entity . '">' . $newref . '</a>'; |
||
| 863 | $dir_output = $conf->fournisseur->commande->multidir_output[$object->entity] . "/" . get_exdir(0, 0, 0, 1, $object); |
||
| 864 | $object_type = 'order_supplier'; |
||
| 865 | $labeltouse = isset($conf->global->ORDER_SUPPLIER_REFUSE_TEMPLATE) ? $conf->global->ORDER_SUPPLIER_REFUSE_TEMPLATE : ''; |
||
| 866 | $mesg = $outputlangs->transnoentitiesnoconv("Hello") . ",\n\n"; |
||
| 867 | $mesg .= $outputlangs->transnoentitiesnoconv("EMailTextSupplierOrderRefusedBy", $link, $user->getFullName($outputlangs)); |
||
| 868 | $mesg .= "\n\n" . $outputlangs->transnoentitiesnoconv("Sincerely") . ".\n\n"; |
||
| 869 | break; |
||
| 870 | case 'SHIPPING_VALIDATE': |
||
| 871 | $link = '<a href="' . $urlwithroot . '/expedition/card.php?id=' . $object->id . '&entity=' . $object->entity . '">' . $newref . '</a>'; |
||
| 872 | $dir_output = $conf->expedition->dir_output . "/sending/" . get_exdir(0, 0, 0, 1, $object, 'shipment'); |
||
| 873 | $object_type = 'shipping'; |
||
| 874 | $mesg = $outputlangs->transnoentitiesnoconv("EMailTextExpeditionValidated", $link); |
||
| 875 | break; |
||
| 876 | case 'EXPENSE_REPORT_VALIDATE': |
||
| 877 | $link = '<a href="' . $urlwithroot . '/expensereport/card.php?id=' . $object->id . '&entity=' . $object->entity . '">' . $newref . '</a>'; |
||
| 878 | $dir_output = $conf->expensereport->dir_output; |
||
| 879 | $object_type = 'expensereport'; |
||
| 880 | $mesg = $outputlangs->transnoentitiesnoconv("EMailTextExpenseReportValidated", $link); |
||
| 881 | break; |
||
| 882 | case 'EXPENSE_REPORT_APPROVE': |
||
| 883 | $link = '<a href="' . $urlwithroot . '/expensereport/card.php?id=' . $object->id . '&entity=' . $object->entity . '">' . $newref . '</a>'; |
||
| 884 | $dir_output = $conf->expensereport->dir_output; |
||
| 885 | $object_type = 'expensereport'; |
||
| 886 | $mesg = $outputlangs->transnoentitiesnoconv("EMailTextExpenseReportApproved", $link); |
||
| 887 | break; |
||
| 888 | case 'HOLIDAY_VALIDATE': |
||
| 889 | $link = '<a href="' . $urlwithroot . '/holiday/card.php?id=' . $object->id . '&entity=' . $object->entity . '">' . $newref . '</a>'; |
||
| 890 | $dir_output = $conf->holiday->dir_output; |
||
| 891 | $object_type = 'holiday'; |
||
| 892 | $mesg = $outputlangs->transnoentitiesnoconv("EMailTextHolidayValidated", $link); |
||
| 893 | break; |
||
| 894 | case 'HOLIDAY_APPROVE': |
||
| 895 | $link = '<a href="' . $urlwithroot . '/holiday/card.php?id=' . $object->id . '&entity=' . $object->entity . '">' . $newref . '</a>'; |
||
| 896 | $dir_output = $conf->holiday->dir_output; |
||
| 897 | $object_type = 'holiday'; |
||
| 898 | $mesg = $outputlangs->transnoentitiesnoconv("EMailTextHolidayApproved", $link); |
||
| 899 | break; |
||
| 900 | case 'ACTION_CREATE': |
||
| 901 | $link = '<a href="' . $urlwithroot . '/comm/action/card.php?id=' . $object->id . '&entity=' . $object->entity . '">' . $newref . '</a>'; |
||
| 902 | $dir_output = $conf->agenda->dir_output; |
||
| 903 | $object_type = 'action'; |
||
| 904 | $mesg = $outputlangs->transnoentitiesnoconv("EMailTextActionAdded", $link); |
||
| 905 | break; |
||
| 906 | default: |
||
| 907 | $object_type = $object->element; |
||
| 908 | $dir_output = $conf->$object_type->multidir_output[$object->entity ? $object->entity : $conf->entity] . "/" . get_exdir(0, 0, 0, 1, $object, $object_type); |
||
| 909 | $template = $notifcode . '_TEMPLATE'; |
||
| 910 | $mesg = $outputlangs->transnoentitiesnoconv('Notify_' . $notifcode) . ' ' . $newref . ' ' . $dir_output; |
||
| 911 | break; |
||
| 912 | } |
||
| 913 | |||
| 914 | include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php'; |
||
| 915 | $formmail = new FormMail($this->db); |
||
| 916 | $arraydefaultmessage = null; |
||
| 917 | |||
| 918 | $template = $notifcode . '_TEMPLATE'; |
||
| 919 | $labeltouse = getDolGlobalString($template); |
||
| 920 | if (!empty($labeltouse)) { |
||
| 921 | $arraydefaultmessage = $formmail->getEMailTemplate($this->db, $object_type . '_send', $user, $outputlangs, 0, 1, $labeltouse); |
||
| 922 | } |
||
| 923 | if (!empty($labeltouse) && is_object($arraydefaultmessage) && $arraydefaultmessage->id > 0) { |
||
| 924 | $substitutionarray = getCommonSubstitutionArray($outputlangs, 0, null, $object); |
||
| 925 | complete_substitutions_array($substitutionarray, $outputlangs, $object); |
||
| 926 | $subject = make_substitutions($arraydefaultmessage->topic, $substitutionarray, $outputlangs); |
||
| 927 | $message = make_substitutions($arraydefaultmessage->content, $substitutionarray, $outputlangs); |
||
| 928 | } else { |
||
| 929 | $message = $outputlangs->transnoentities("YouReceiveMailBecauseOfNotification", $application, $mysoc->name) . "\n"; |
||
| 930 | $message .= $outputlangs->transnoentities("YouReceiveMailBecauseOfNotification2", $application, $mysoc->name) . "\n"; |
||
| 931 | $message .= "\n"; |
||
| 932 | $message .= $mesg; |
||
| 933 | } |
||
| 934 | |||
| 935 | $ref = dol_sanitizeFileName($newref); |
||
| 936 | $pdf_path = $dir_output . "/" . $ref . ".pdf"; |
||
| 937 | if (!dol_is_file($pdf_path) || (is_object($arraydefaultmessage) && $arraydefaultmessage->id > 0 && !$arraydefaultmessage->joinfiles)) { |
||
| 938 | // We can't add PDF as it is not generated yet. |
||
| 939 | $filepdf = ''; |
||
| 940 | } else { |
||
| 941 | $filepdf = $pdf_path; |
||
| 942 | $filename_list[] = $filepdf; |
||
| 943 | $mimetype_list[] = mime_content_type($filepdf); |
||
| 944 | $mimefilename_list[] = $ref . ".pdf"; |
||
| 945 | } |
||
| 946 | |||
| 947 | $labeltouse = !empty($labeltouse) ? $labeltouse : ''; |
||
| 948 | |||
| 949 | // Replace keyword __SUPERVISOREMAIL__ |
||
| 950 | if (preg_match('/__SUPERVISOREMAIL__/', $sendto)) { |
||
| 951 | $newval = ''; |
||
| 952 | if ($user->fk_user > 0) { |
||
| 953 | $supervisoruser = new User($this->db); |
||
| 954 | $supervisoruser->fetch($user->fk_user); |
||
| 955 | if ($supervisoruser->email) { |
||
| 956 | $newval = trim(dolGetFirstLastname($supervisoruser->firstname, $supervisoruser->lastname) . ' <' . $supervisoruser->email . '>'); |
||
| 957 | } |
||
| 958 | } |
||
| 959 | dol_syslog("Replace the __SUPERVISOREMAIL__ key into recipient email string with " . $newval); |
||
| 960 | $sendto = preg_replace('/__SUPERVISOREMAIL__/', $newval, $sendto); |
||
| 961 | $sendto = preg_replace('/,\s*,/', ',', $sendto); // in some case you can have $sendto like "email, __SUPERVISOREMAIL__ , otheremail" then you have "email, , othermail" and it's not valid |
||
| 962 | $sendto = preg_replace('/^[\s,]+/', '', $sendto); // Clean start of string |
||
| 963 | $sendto = preg_replace('/[\s,]+$/', '', $sendto); // Clean end of string |
||
| 964 | } |
||
| 965 | |||
| 966 | $parameters = array('notifcode' => $notifcode, 'sendto' => $sendto, 'from' => $from, 'file' => $filename_list, 'mimefile' => $mimetype_list, 'filename' => $mimefilename_list, 'outputlangs' => $outputlangs, 'labeltouse' => $labeltouse); |
||
| 967 | if (!isset($action)) { |
||
| 968 | $action = ''; |
||
| 969 | } |
||
| 970 | |||
| 971 | $reshook = $hookmanager->executeHooks('formatNotificationMessage', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks |
||
| 972 | if (empty($reshook)) { |
||
| 973 | if (!empty($hookmanager->resArray['files'])) { |
||
| 974 | $filename_list = $hookmanager->resArray['files']['file']; |
||
| 975 | $mimetype_list = $hookmanager->resArray['files']['mimefile']; |
||
| 976 | $mimefilename_list = $hookmanager->resArray['files']['filename']; |
||
| 977 | } |
||
| 978 | if (!empty($hookmanager->resArray['subject'])) { |
||
| 979 | $subject .= $hookmanager->resArray['subject']; |
||
| 980 | } |
||
| 981 | if (!empty($hookmanager->resArray['message'])) { |
||
| 982 | $message .= $hookmanager->resArray['message']; |
||
| 983 | } |
||
| 984 | } |
||
| 985 | |||
| 986 | $mailfile = new CMailFile( |
||
| 987 | $subject, |
||
| 988 | $sendto, |
||
| 989 | $from, |
||
| 990 | $message, |
||
| 991 | $filename_list, |
||
| 992 | $mimetype_list, |
||
| 993 | $mimefilename_list, |
||
| 994 | '', |
||
| 995 | '', |
||
| 996 | 0, |
||
| 997 | -1, |
||
| 998 | '', |
||
| 999 | '', |
||
| 1000 | $trackid, |
||
| 1001 | '', |
||
| 1002 | 'notification' |
||
| 1003 | ); |
||
| 1004 | |||
| 1005 | if ($mailfile->sendfile()) { |
||
| 1006 | if ($obj->type_target == 'touserid') { |
||
| 1007 | $sql = "INSERT INTO " . $this->db->prefix() . "notify (daten, fk_action, fk_soc, fk_user, type, objet_type, type_target, objet_id, email)"; |
||
| 1008 | $sql .= " VALUES ('" . $this->db->idate(dol_now()) . "', " . ((int) $notifcodedefid) . ", " . ($object->socid > 0 ? ((int) $object->socid) : 'null') . ", " . ((int) $obj->cid) . ", '" . $this->db->escape($obj->type) . "', '" . $this->db->escape($object_type) . "', '" . $this->db->escape($obj->type_target) . "', " . ((int) $object->id) . ", '" . $this->db->escape($obj->email) . "')"; |
||
| 1009 | } else { |
||
| 1010 | $sql = "INSERT INTO " . $this->db->prefix() . "notify (daten, fk_action, fk_soc, fk_contact, type, objet_type, type_target, objet_id, email)"; |
||
| 1011 | $sql .= " VALUES ('" . $this->db->idate(dol_now()) . "', " . ((int) $notifcodedefid) . ", " . ($object->socid > 0 ? ((int) $object->socid) : 'null') . ", " . ((int) $obj->cid) . ", '" . $this->db->escape($obj->type) . "', '" . $this->db->escape($object_type) . "', '" . $this->db->escape($obj->type_target) . "', " . ((int) $object->id) . ", '" . $this->db->escape($obj->email) . "')"; |
||
| 1012 | } |
||
| 1013 | if (!$this->db->query($sql)) { |
||
| 1014 | dol_print_error($this->db); |
||
| 1015 | } |
||
| 1016 | } else { |
||
| 1017 | $error++; |
||
| 1018 | $this->errors[] = $mailfile->error; |
||
| 1019 | } |
||
| 1020 | } else { |
||
| 1021 | dol_syslog("No notification sent for " . $sendto . " because email is empty"); |
||
| 1022 | } |
||
| 1023 | $i++; |
||
| 1024 | } |
||
| 1025 | } else { |
||
| 1026 | dol_syslog("No notification to thirdparty sent, nothing into notification setup for the thirdparty socid = " . (empty($object->socid) ? '' : $object->socid)); |
||
| 1027 | } |
||
| 1028 | } else { |
||
| 1029 | $error++; |
||
| 1030 | $this->errors[] = $this->db->lasterror(); |
||
| 1031 | dol_syslog("Failed to get list of notification to send " . $this->db->lasterror(), LOG_ERR); |
||
| 1032 | return -1; |
||
| 1033 | } |
||
| 1034 | |||
| 1035 | // Check notification using fixed email |
||
| 1036 | // TODO Move vars NOTIFICATION_FIXEDEMAIL into table llx_notify_def and inclulde the case into previous loop of sql result |
||
| 1037 | if (!$error) { |
||
| 1038 | foreach ($conf->global as $key => $val) { |
||
| 1039 | $reg = array(); |
||
| 1040 | if ($val == '' || !preg_match('/^NOTIFICATION_FIXEDEMAIL_' . $notifcode . '_THRESHOLD_HIGHER_(.*)$/', $key, $reg)) { |
||
| 1041 | continue; |
||
| 1042 | } |
||
| 1043 | |||
| 1044 | $sendto = $val; |
||
| 1045 | |||
| 1046 | $threshold = (float) $reg[1]; |
||
| 1047 | if (!empty($object->total_ht) && $object->total_ht <= $threshold) { |
||
| 1048 | dol_syslog("A notification is requested for notifcode = " . $notifcode . " but amount = " . $object->total_ht . " so lower than threshold = " . $threshold . ". We discard this notification"); |
||
| 1049 | continue; |
||
| 1050 | } |
||
| 1051 | |||
| 1052 | $notifcodedefid = dol_getIdFromCode($this->db, $notifcode, 'c_action_trigger', 'code', 'rowid'); |
||
| 1053 | if ($notifcodedefid <= 0) { |
||
| 1054 | dol_print_error($this->db, 'Failed to get id from code'); |
||
| 1055 | } |
||
| 1056 | $trackid = ''; |
||
| 1057 | |||
| 1058 | $object_type = ''; |
||
| 1059 | $link = ''; |
||
| 1060 | $num++; |
||
| 1061 | |||
| 1062 | $appli = $mysoc->name; |
||
| 1063 | |||
| 1064 | $subject = '[' . $appli . '] ' . $langs->transnoentitiesnoconv("DolibarrNotification") . ($projtitle ? ' ' . $projtitle : ''); |
||
| 1065 | |||
| 1066 | switch ($notifcode) { |
||
| 1067 | case 'BILL_VALIDATE': |
||
| 1068 | $link = '<a href="' . $urlwithroot . '/compta/facture/card.php?facid=' . $object->id . '&entity=' . $object->entity . '">' . $newref . '</a>'; |
||
| 1069 | $dir_output = $conf->facture->dir_output . "/" . get_exdir(0, 0, 0, 1, $object, 'invoice'); |
||
| 1070 | $object_type = 'facture'; |
||
| 1071 | $mesg = $langs->transnoentitiesnoconv("EMailTextInvoiceValidated", $link); |
||
| 1072 | break; |
||
| 1073 | case 'BILL_PAYED': |
||
| 1074 | $link = '<a href="' . $urlwithroot . '/compta/facture/card.php?facid=' . $object->id . '&entity=' . $object->entity . '">' . $newref . '</a>'; |
||
| 1075 | $dir_output = $conf->facture->dir_output . "/" . get_exdir(0, 0, 0, 1, $object, 'invoice'); |
||
| 1076 | $object_type = 'facture'; |
||
| 1077 | $mesg = $langs->transnoentitiesnoconv("EMailTextInvoicePayed", $link); |
||
| 1078 | break; |
||
| 1079 | case 'ORDER_VALIDATE': |
||
| 1080 | $link = '<a href="' . $urlwithroot . '/commande/card.php?id=' . $object->id . '&entity=' . $object->entity . '">' . $newref . '</a>'; |
||
| 1081 | $dir_output = $conf->commande->dir_output . "/" . get_exdir(0, 0, 0, 1, $object, 'commande'); |
||
| 1082 | $object_type = 'order'; |
||
| 1083 | $mesg = $langs->transnoentitiesnoconv("EMailTextOrderValidated", $link); |
||
| 1084 | break; |
||
| 1085 | case 'ORDER_CLOSE': |
||
| 1086 | $link = '<a href="' . $urlwithroot . '/commande/card.php?id=' . $object->id . '&entity=' . $object->entity . '">' . $newref . '</a>'; |
||
| 1087 | $dir_output = $conf->commande->dir_output . "/" . get_exdir(0, 0, 0, 1, $object, 'commande'); |
||
| 1088 | $object_type = 'order'; |
||
| 1089 | $mesg = $langs->transnoentitiesnoconv("EMailTextOrderClose", $link); |
||
| 1090 | break; |
||
| 1091 | case 'PROPAL_VALIDATE': |
||
| 1092 | $link = '<a href="' . $urlwithroot . '/comm/propal/card.php?id=' . $object->id . '&entity=' . $object->entity . '">' . $newref . '</a>'; |
||
| 1093 | $dir_output = $conf->propal->multidir_output[$object->entity] . "/" . get_exdir(0, 0, 0, 1, $object, 'propal'); |
||
| 1094 | $object_type = 'propal'; |
||
| 1095 | $mesg = $langs->transnoentitiesnoconv("EMailTextProposalValidated", $link); |
||
| 1096 | break; |
||
| 1097 | case 'PROPAL_CLOSE_SIGNED': |
||
| 1098 | $link = '<a href="' . $urlwithroot . '/comm/propal/card.php?id=' . $object->id . '&entity=' . $object->entity . '">' . $newref . '</a>'; |
||
| 1099 | $dir_output = $conf->propal->multidir_output[$object->entity] . "/" . get_exdir(0, 0, 0, 1, $object, 'propal'); |
||
| 1100 | $object_type = 'propal'; |
||
| 1101 | $mesg = $langs->transnoentitiesnoconv("EMailTextProposalClosedSigned", $link); |
||
| 1102 | break; |
||
| 1103 | case 'FICHINTER_ADD_CONTACT': |
||
| 1104 | $link = '<a href="' . $urlwithroot . '/fichinter/card.php?id=' . $object->id . '&entity=' . $object->entity . '">' . $newref . '</a>'; |
||
| 1105 | $dir_output = $conf->ficheinter->dir_output; |
||
| 1106 | $object_type = 'ficheinter'; |
||
| 1107 | $mesg = $langs->transnoentitiesnoconv("EMailTextInterventionAddedContact", $link); |
||
| 1108 | break; |
||
| 1109 | case 'FICHINTER_VALIDATE': |
||
| 1110 | $link = '<a href="' . $urlwithroot . '/fichinter/card.php?id=' . $object->id . '&entity=' . $object->entity . '">' . $newref . '</a>'; |
||
| 1111 | $dir_output = $conf->facture->dir_output; |
||
| 1112 | $object_type = 'ficheinter'; |
||
| 1113 | $mesg = $langs->transnoentitiesnoconv("EMailTextInterventionValidated", $link); |
||
| 1114 | break; |
||
| 1115 | case 'FICHINTER_CLOSE': |
||
| 1116 | $link = '<a href="' . $urlwithroot . '/fichinter/card.php?id=' . $object->id . '&entity=' . $object->entity . '">' . $newref . '</a>'; |
||
| 1117 | $dir_output = $conf->facture->dir_output; |
||
| 1118 | $object_type = 'ficheinter'; |
||
| 1119 | $mesg = $langs->transnoentitiesnoconv("EMailTextInterventionClosed", $link); |
||
| 1120 | break; |
||
| 1121 | case 'ORDER_SUPPLIER_CANCEL': |
||
| 1122 | $link = '<a href="' . $urlwithroot . '/fourn/commande/card.php?id=' . $object->id . '&entity=' . $object->entity . '">' . $newref . '</a>'; |
||
| 1123 | $dir_output = $conf->fournisseur->commande->multidir_output[$object->entity] . "/" . get_exdir(0, 0, 0, 1, $object); |
||
| 1124 | $object_type = 'order_supplier'; |
||
| 1125 | $mesg = $langs->transnoentitiesnoconv("Hello") . ",\n\n"; |
||
| 1126 | $mesg .= $langs->transnoentitiesnoconv("EMailTextSupplierOrderCanceledBy", $link, $user->getFullName($langs)); |
||
| 1127 | $mesg .= "\n\n" . $langs->transnoentitiesnoconv("Sincerely") . ".\n\n"; |
||
| 1128 | break; |
||
| 1129 | case 'ORDER_SUPPLIER_VALIDATE': |
||
| 1130 | $link = '<a href="' . $urlwithroot . '/fourn/commande/card.php?id=' . $object->id . '&entity=' . $object->entity . '">' . $newref . '</a>'; |
||
| 1131 | $dir_output = $conf->fournisseur->commande->multidir_output[$object->entity] . "/" . get_exdir(0, 0, 0, 1, $object); |
||
| 1132 | $object_type = 'order_supplier'; |
||
| 1133 | $mesg = $langs->transnoentitiesnoconv("Hello") . ",\n\n"; |
||
| 1134 | $mesg .= $langs->transnoentitiesnoconv("EMailTextSupplierOrderValidatedBy", $link, $user->getFullName($langs)); |
||
| 1135 | $mesg .= "\n\n" . $langs->transnoentitiesnoconv("Sincerely") . ".\n\n"; |
||
| 1136 | break; |
||
| 1137 | case 'ORDER_SUPPLIER_APPROVE': |
||
| 1138 | $link = '<a href="' . $urlwithroot . '/fourn/commande/card.php?id=' . $object->id . '&entity=' . $object->entity . '">' . $newref . '</a>'; |
||
| 1139 | $dir_output = $conf->fournisseur->commande->multidir_output[$object->entity] . "/" . get_exdir(0, 0, 0, 1, $object); |
||
| 1140 | $object_type = 'order_supplier'; |
||
| 1141 | $mesg = $langs->transnoentitiesnoconv("Hello") . ",\n\n"; |
||
| 1142 | $mesg .= $langs->transnoentitiesnoconv("EMailTextSupplierOrderApprovedBy", $link, $user->getFullName($langs)); |
||
| 1143 | $mesg .= "\n\n" . $langs->transnoentitiesnoconv("Sincerely") . ".\n\n"; |
||
| 1144 | break; |
||
| 1145 | case 'ORDER_SUPPLIER_SUBMIT': |
||
| 1146 | $link = '<a href="' . $urlwithroot . '/fourn/commande/card.php?id=' . $object->id . '&entity=' . $object->entity . '">' . $newref . '</a>'; |
||
| 1147 | $dir_output = $conf->fournisseur->commande->multidir_output[$object->entity] . "/" . get_exdir(0, 0, 0, 1, $object); |
||
| 1148 | $object_type = 'order_supplier'; |
||
| 1149 | $mesg = $langs->transnoentitiesnoconv("Hello") . ",\n\n"; |
||
| 1150 | $mesg .= $langs->transnoentitiesnoconv("EMailTextSupplierOrderSubmittedBy", $link, $user->getFullName($langs)); |
||
| 1151 | $mesg .= "\n\n" . $langs->transnoentitiesnoconv("Sincerely") . ".\n\n"; |
||
| 1152 | break; |
||
| 1153 | case 'ORDER_SUPPLIER_REFUSE': |
||
| 1154 | $link = '<a href="' . $urlwithroot . '/fourn/commande/card.php?id=' . $object->id . '&entity=' . $object->entity . '">' . $newref . '</a>'; |
||
| 1155 | $dir_output = $conf->fournisseur->commande->multidir_output[$object->entity] . "/" . get_exdir(0, 0, 0, 1, $object); |
||
| 1156 | $object_type = 'order_supplier'; |
||
| 1157 | $mesg = $langs->transnoentitiesnoconv("Hello") . ",\n\n"; |
||
| 1158 | $mesg .= $langs->transnoentitiesnoconv("EMailTextSupplierOrderRefusedBy", $link, $user->getFullName($langs)); |
||
| 1159 | $mesg .= "\n\n" . $langs->transnoentitiesnoconv("Sincerely") . ".\n\n"; |
||
| 1160 | break; |
||
| 1161 | case 'SHIPPING_VALIDATE': |
||
| 1162 | $link = '<a href="' . $urlwithroot . '/expedition/card.php?id=' . $object->id . '&entity=' . $object->entity . '">' . $newref . '</a>'; |
||
| 1163 | $dir_output = $conf->expedition->dir_output . "/sending/" . get_exdir(0, 0, 0, 1, $object, 'shipment'); |
||
| 1164 | $object_type = 'order_supplier'; |
||
| 1165 | $mesg = $langs->transnoentitiesnoconv("EMailTextExpeditionValidated", $link); |
||
| 1166 | break; |
||
| 1167 | case 'EXPENSE_REPORT_VALIDATE': |
||
| 1168 | $link = '<a href="' . $urlwithroot . '/expensereport/card.php?id=' . $object->id . '&entity=' . $object->entity . '">' . $newref . '</a>'; |
||
| 1169 | $dir_output = $conf->expensereport->dir_output; |
||
| 1170 | $object_type = 'expensereport'; |
||
| 1171 | $mesg = $langs->transnoentitiesnoconv("EMailTextExpenseReportValidated", $link); |
||
| 1172 | break; |
||
| 1173 | case 'EXPENSE_REPORT_APPROVE': |
||
| 1174 | $link = '<a href="' . $urlwithroot . '/expensereport/card.php?id=' . $object->id . '&entity=' . $object->entity . '">' . $newref . '</a>'; |
||
| 1175 | $dir_output = $conf->expensereport->dir_output; |
||
| 1176 | $object_type = 'expensereport'; |
||
| 1177 | $mesg = $langs->transnoentitiesnoconv("EMailTextExpenseReportApproved", $link); |
||
| 1178 | break; |
||
| 1179 | case 'HOLIDAY_VALIDATE': |
||
| 1180 | $link = '<a href="' . $urlwithroot . '/holiday/card.php?id=' . $object->id . '&entity=' . $object->entity . '">' . $newref . '</a>'; |
||
| 1181 | $dir_output = $conf->holiday->dir_output; |
||
| 1182 | $object_type = 'holiday'; |
||
| 1183 | $mesg = $langs->transnoentitiesnoconv("EMailTextHolidayValidated", $link); |
||
| 1184 | break; |
||
| 1185 | case 'HOLIDAY_APPROVE': |
||
| 1186 | $link = '<a href="' . $urlwithroot . '/holiday/card.php?id=' . $object->id . '&entity=' . $object->entity . '">' . $newref . '</a>'; |
||
| 1187 | $dir_output = $conf->holiday->dir_output; |
||
| 1188 | $object_type = 'holiday'; |
||
| 1189 | $mesg = $langs->transnoentitiesnoconv("EMailTextHolidayApproved", $link); |
||
| 1190 | break; |
||
| 1191 | case 'ACTION_CREATE': |
||
| 1192 | $link = '<a href="' . $urlwithroot . '/comm/action/card.php?id=' . $object->id . '&entity=' . $object->entity . '">' . $newref . '</a>'; |
||
| 1193 | $dir_output = $conf->agenda->dir_output; |
||
| 1194 | $object_type = 'action'; |
||
| 1195 | $mesg = $langs->transnoentitiesnoconv("EMailTextActionAdded", $link); |
||
| 1196 | break; |
||
| 1197 | default: |
||
| 1198 | $object_type = $object->element; |
||
| 1199 | $dir_output = $conf->$object_type->multidir_output[$object->entity ? $object->entity : $conf->entity] . "/" . get_exdir(0, 0, 0, 1, $object, $object_type); |
||
| 1200 | $mesg = $langs->transnoentitiesnoconv('Notify_' . $notifcode) . ' ' . $newref; |
||
| 1201 | break; |
||
| 1202 | } |
||
| 1203 | $ref = dol_sanitizeFileName($newref); |
||
| 1204 | $pdf_path = $dir_output . "/" . $ref . "/" . $ref . ".pdf"; |
||
| 1205 | if (!dol_is_file($pdf_path)) { |
||
| 1206 | // We can't add PDF as it is not generated yet. |
||
| 1207 | $filepdf = ''; |
||
| 1208 | } else { |
||
| 1209 | $filepdf = $pdf_path; |
||
| 1210 | $filename_list[] = $pdf_path; |
||
| 1211 | $mimetype_list[] = mime_content_type($filepdf); |
||
| 1212 | $mimefilename_list[] = $ref . ".pdf"; |
||
| 1213 | } |
||
| 1214 | |||
| 1215 | // Set output language |
||
| 1216 | $outputlangs = $langs; |
||
| 1217 | |||
| 1218 | // if an e-mail template is configured for this notification code (for instance 'SHIPPING_VALIDATE_TEMPLATE', ...), |
||
| 1219 | // we fetch this template by its label. Otherwise, a default message content will be sent. |
||
| 1220 | $mailTemplateLabel = getDolGlobalString($notifcode . '_TEMPLATE'); |
||
| 1221 | $emailTemplate = null; |
||
| 1222 | if (!empty($mailTemplateLabel)) { |
||
| 1223 | include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php'; |
||
| 1224 | $formmail = new FormMail($this->db); |
||
| 1225 | $emailTemplate = $formmail->getEMailTemplate($this->db, $object_type . '_send', $user, $outputlangs, 0, 1, $mailTemplateLabel); |
||
| 1226 | } |
||
| 1227 | if (!empty($mailTemplateLabel) && is_object($emailTemplate) && $emailTemplate->id > 0) { |
||
| 1228 | if (property_exists($object, 'thirdparty') && $object->thirdparty instanceof Societe && $object->thirdparty->default_lang && $object->thirdparty->default_lang != $langs->defaultlang) { |
||
| 1229 | $outputlangs = new Translate('', $conf); |
||
| 1230 | $outputlangs->setDefaultLang($object->thirdparty->default_lang); |
||
| 1231 | $outputlangs->loadLangs(array('main', 'other')); |
||
| 1232 | } |
||
| 1233 | $substitutionarray = getCommonSubstitutionArray($outputlangs, 0, null, $object); |
||
| 1234 | complete_substitutions_array($substitutionarray, $outputlangs, $object); |
||
| 1235 | $subject = make_substitutions($emailTemplate->topic, $substitutionarray, $outputlangs); |
||
| 1236 | $message = make_substitutions($emailTemplate->content, $substitutionarray, $outputlangs); |
||
| 1237 | } else { |
||
| 1238 | $message = ''; |
||
| 1239 | $message .= $outputlangs->transnoentities("YouReceiveMailBecauseOfNotification2", $application, $mysoc->name) . "\n"; |
||
| 1240 | $message .= "\n"; |
||
| 1241 | $message .= $mesg; |
||
| 1242 | |||
| 1243 | $message = nl2br($message); |
||
| 1244 | } |
||
| 1245 | |||
| 1246 | // Replace keyword __SUPERVISOREMAIL__ |
||
| 1247 | if (preg_match('/__SUPERVISOREMAIL__/', $sendto)) { |
||
| 1248 | $newval = ''; |
||
| 1249 | if ($user->fk_user > 0) { |
||
| 1250 | $supervisoruser = new User($this->db); |
||
| 1251 | $supervisoruser->fetch($user->fk_user); |
||
| 1252 | if ($supervisoruser->email) { |
||
| 1253 | $newval = trim(dolGetFirstLastname($supervisoruser->firstname, $supervisoruser->lastname) . ' <' . $supervisoruser->email . '>'); |
||
| 1254 | } |
||
| 1255 | } |
||
| 1256 | dol_syslog("Replace the __SUPERVISOREMAIL__ key into recipient email string with " . $newval); |
||
| 1257 | $sendto = preg_replace('/__SUPERVISOREMAIL__/', $newval, $sendto); |
||
| 1258 | $sendto = preg_replace('/,\s*,/', ',', $sendto); // in some case you can have $sendto like "email, __SUPERVISOREMAIL__ , otheremail" then you have "email, , othermail" and it's not valid |
||
| 1259 | $sendto = preg_replace('/^[\s,]+/', '', $sendto); // Clean start of string |
||
| 1260 | $sendto = preg_replace('/[\s,]+$/', '', $sendto); // Clean end of string |
||
| 1261 | } |
||
| 1262 | |||
| 1263 | if ($sendto) { |
||
| 1264 | $parameters = array('notifcode' => $notifcode, 'sendto' => $sendto, 'from' => $from, 'file' => $filename_list, 'mimefile' => $mimetype_list, 'filename' => $mimefilename_list, 'subject' => &$subject, 'message' => &$message); |
||
| 1265 | $reshook = $hookmanager->executeHooks('formatNotificationMessage', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks |
||
| 1266 | if (empty($reshook)) { |
||
| 1267 | if (!empty($hookmanager->resArray['files'])) { |
||
| 1268 | $filename_list = $hookmanager->resArray['files']['file']; |
||
| 1269 | $mimetype_list = $hookmanager->resArray['files']['mimefile']; |
||
| 1270 | $mimefilename_list = $hookmanager->resArray['files']['filename']; |
||
| 1271 | } |
||
| 1272 | if (!empty($hookmanager->resArray['subject'])) { |
||
| 1273 | $subject .= $hookmanager->resArray['subject']; |
||
| 1274 | } |
||
| 1275 | if (!empty($hookmanager->resArray['message'])) { |
||
| 1276 | $message .= $hookmanager->resArray['message']; |
||
| 1277 | } |
||
| 1278 | } |
||
| 1279 | $mailfile = new CMailFile( |
||
| 1280 | $subject, |
||
| 1281 | $sendto, |
||
| 1282 | $from, |
||
| 1283 | $message, |
||
| 1284 | $filename_list, |
||
| 1285 | $mimetype_list, |
||
| 1286 | $mimefilename_list, |
||
| 1287 | '', |
||
| 1288 | '', |
||
| 1289 | 0, |
||
| 1290 | 1, |
||
| 1291 | '', |
||
| 1292 | $trackid, |
||
| 1293 | '', |
||
| 1294 | '', |
||
| 1295 | 'notification' |
||
| 1296 | ); |
||
| 1297 | |||
| 1298 | if ($mailfile->sendfile()) { |
||
| 1299 | $sql = "INSERT INTO " . $this->db->prefix() . "notify (daten, fk_action, fk_soc, fk_contact, type, type_target, objet_type, objet_id, email)"; |
||
| 1300 | $sql .= " VALUES ('" . $this->db->idate(dol_now()) . "', " . ((int) $notifcodedefid) . ", " . ($object->socid > 0 ? ((int) $object->socid) : 'null') . ", null, 'email', 'tofixedemail', '" . $this->db->escape($object_type) . "', " . ((int) $object->id) . ", '" . $this->db->escape($sendto) . "')"; |
||
| 1301 | if (!$this->db->query($sql)) { |
||
| 1302 | dol_print_error($this->db); |
||
| 1303 | } |
||
| 1304 | } else { |
||
| 1305 | $error++; |
||
| 1306 | $this->errors[] = $mailfile->error; |
||
| 1307 | } |
||
| 1308 | } |
||
| 1309 | } |
||
| 1310 | } |
||
| 1311 | |||
| 1312 | if (!$error) { |
||
| 1313 | return $num; |
||
| 1314 | } else { |
||
| 1315 | return -1 * $error; |
||
| 1316 | } |
||
| 1319 |