From dc6f4c1f4d51a9a46516953ddf34adf74607884d Mon Sep 17 00:00:00 2001 From: florian HENRY Date: Wed, 21 Mar 2018 11:48:30 +0100 Subject: [PATCH 1/5] NEW : option to send email to salaries --- htdocs/admin/mails.php | 10 ++ htdocs/core/actions_sendmails.inc.php | 31 ++++- htdocs/core/class/html.formmail.class.php | 49 +++++++- htdocs/core/tpl/card_presend.tpl.php | 18 +++ htdocs/langs/en_US/admin.lang | 1 + htdocs/langs/en_US/mails.lang | 2 + .../modulebuilder/template/myobject_list.php | 1 + htdocs/user/class/user.class.php | 106 +++++++++++++++++- 8 files changed, 214 insertions(+), 4 deletions(-) diff --git a/htdocs/admin/mails.php b/htdocs/admin/mails.php index 7d4e382723c..71b1487ab44 100644 --- a/htdocs/admin/mails.php +++ b/htdocs/admin/mails.php @@ -71,6 +71,7 @@ if ($action == 'update' && empty($_POST["cancel"])) { dolibarr_set_const($db, "MAIN_DISABLE_ALL_MAILS", GETPOST("MAIN_DISABLE_ALL_MAILS"),'chaine',0,'',$conf->entity); dolibarr_set_const($db, "MAIN_MAIL_FORCE_SENDTO", GETPOST("MAIN_MAIL_FORCE_SENDTO"),'chaine',0,'',$conf->entity); + dolibarr_set_const($db, "MAIN_MAIL_ENABLED_USER_DEST_SELECT", GETPOST("MAIN_MAIL_ENABLED_USER_DEST_SELECT"),'chaine',0,'',$conf->entity); // Send mode parameters dolibarr_set_const($db, "MAIN_MAIL_SENDMODE", GETPOST("MAIN_MAIL_SENDMODE"),'chaine',0,'',$conf->entity); dolibarr_set_const($db, "MAIN_MAIL_SMTP_PORT", GETPOST("MAIN_MAIL_SMTP_PORT"),'chaine',0,'',$conf->entity); @@ -271,6 +272,12 @@ if ($action == 'edit') print ''; print ''; + + //Add user to select destinaries list + print ''.$langs->trans("MAIN_MAIL_ENABLED_USER_DEST_SELECT").''; + print $form->selectyesno('MAIN_MAIL_ENABLED_USER_DEST_SELECT',$conf->global->MAIN_MAIL_ENABLED_USER_DEST_SELECT,1); + print ''; + // Separator print ' '; @@ -541,6 +548,9 @@ else if (! empty($conf->global->MAIN_MAIL_FORCE_SENDTO) && ! isValidEmail($conf->global->MAIN_MAIL_FORCE_SENDTO)) print img_warning($langs->trans("ErrorBadEMail")); print ''; + //Add user to select destinaries list + print ''.$langs->trans("MAIN_MAIL_ENABLED_USER_DEST_SELECT").''.yn($conf->global->MAIN_MAIL_ENABLED_USER_DEST_SELECT).''; + // Separator print ' '; diff --git a/htdocs/core/actions_sendmails.inc.php b/htdocs/core/actions_sendmails.inc.php index 9f5224a71d3..3498bbc132c 100644 --- a/htdocs/core/actions_sendmails.inc.php +++ b/htdocs/core/actions_sendmails.inc.php @@ -170,6 +170,9 @@ if (($action == 'send' || $action == 'relance') && ! $_POST['addfile'] && ! $_PO $sendtocc=''; $sendtobcc=''; $sendtoid = array(); + if (!empty($conf->global->MAIN_MAIL_ENABLED_USER_DEST_SELECT)) { + $sendtouserid=array(); + } // Define $sendto $receiver=$_POST['receiver']; @@ -192,14 +195,25 @@ if (($action == 'send' || $action == 'relance') && ! $_POST['addfile'] && ! $_PO if ($val == 'thirdparty') // Id of third party { $tmparray[] = $thirdparty->name.' <'.$thirdparty->email.'>'; - } - elseif ($val) // Id du contact + } elseif ($val) // Id du contact { $tmparray[] = $thirdparty->contact_get_property((int) $val,'email'); $sendtoid[] = $val; } } } + if (!empty($conf->global->MAIN_MAIL_ENABLED_USER_DEST_SELECT)) { + $receiveruser=$_POST['receiveruser']; + if (is_array($receiveruser) && count($receiveruser)>0) + { + $fuserdest = new User($db); + foreach($receiveruser as $key=>$val) + { + $tmparray[] = $fuserdest->user_get_property($key,'email'); + $sendtouserid[] = $key; + } + } + } $sendto=implode(',',$tmparray); // Define $sendtocc @@ -230,6 +244,19 @@ if (($action == 'send' || $action == 'relance') && ! $_POST['addfile'] && ! $_PO } } } + if (!empty($conf->global->MAIN_MAIL_ENABLED_USER_DEST_SELECT)) { + $receiveruser=$_POST['receiveccruser']; + + if (is_array($receiveruser) && count($receiveruser)>0) + { + $fuserdest = new User($db); + foreach($receiveruser as $key=>$val) + { + $tmparray[] = $fuserdest->user_get_property($key,'email'); + $sendtouserid[] = $key; + } + } + } $sendtocc=implode(',',$tmparray); if (dol_strlen($sendto)) diff --git a/htdocs/core/class/html.formmail.class.php b/htdocs/core/class/html.formmail.class.php index 5ff93994e73..b2f64f4298d 100644 --- a/htdocs/core/class/html.formmail.class.php +++ b/htdocs/core/class/html.formmail.class.php @@ -81,6 +81,9 @@ class FormMail extends Form var $substit_lines=array(); var $param=array(); + public $withtouser=array(); + public $withtoccuser=array(); + var $error; public $lines_model; @@ -332,7 +335,7 @@ class FormMail extends Form $out.= "\n".'
'."\n"; if ($this->withform == 1) { - $out.= '
'."\n"; + $out.= ''."\n"; $out.= ''; $out.= ''; @@ -623,6 +626,28 @@ class FormMail extends Form $out.= "\n"; } + // To User + if (! empty($this->withtouser) && is_array($this->withtouser) && !empty($conf->global->MAIN_MAIL_ENABLED_USER_DEST_SELECT)) + { + $out.= ''; + $out.= $langs->trans("MailToSalaries"); + $out.= ''; + + // multiselect array convert html entities into options tags, even if we dont want this, so we encode them a second time + $tmparray = $this->withtouser; + foreach($tmparray as $key => $val) + { + $tmparray[$key]=dol_htmlentities($tmparray[$key], null, 'UTF-8', true); + } + $withtoselected=GETPOST("receiveruser",'none'); // Array of selected value + if (empty($withtoselected) && count($tmparray) == 1 && GETPOST('action','aZ09') == 'presend') + { + $withtoselected = array_keys($tmparray); + } + $out.= $form->multiselectarray("receiveruser", $tmparray, $withtoselected, null, null, 'inline-block minwidth500', null, ""); + $out.= "\n"; + } + // withoptiononeemailperrecipient if (! empty($this->withoptiononeemailperrecipient)) { @@ -667,6 +692,28 @@ class FormMail extends Form $out.= "\n"; } + // To User cc + if (! empty($this->withtoccuser) && is_array($this->withtoccuser) && !empty($conf->global->MAIN_MAIL_ENABLED_USER_DEST_SELECT)) + { + $out.= ''; + $out.= $langs->trans("MailToCCSalaries"); + $out.= ''; + + // multiselect array convert html entities into options tags, even if we dont want this, so we encode them a second time + $tmparray = $this->withtoccuser; + foreach($tmparray as $key => $val) + { + $tmparray[$key]=dol_htmlentities($tmparray[$key], null, 'UTF-8', true); + } + $withtoselected=GETPOST("receiverccuser",'none'); // Array of selected value + if (empty($withtoselected) && count($tmparray) == 1 && GETPOST('action','aZ09') == 'presend') + { + $withtoselected = array_keys($tmparray); + } + $out.= $form->multiselectarray("receiverccuser", $tmparray, $withtoselected, null, null, 'inline-block minwidth500', null, ""); + $out.= "\n"; + } + // CCC if (! empty($this->withtoccc) || is_array($this->withtoccc)) { diff --git a/htdocs/core/tpl/card_presend.tpl.php b/htdocs/core/tpl/card_presend.tpl.php index 43cacab0e1c..ca852a46499 100644 --- a/htdocs/core/tpl/card_presend.tpl.php +++ b/htdocs/core/tpl/card_presend.tpl.php @@ -147,6 +147,24 @@ if ($action == 'presend') $liste[$key] = $value; } } + if (!empty($conf->global->MAIN_MAIL_ENABLED_USER_DEST_SELECT)) { + $listeuser=array(); + $fuserdest = new User($db); + + $result= $fuserdest->fetchAll('ASC', 't.lastname', 0, 0, array('customsql'=>'t.statut=1 AND t.employee=1 AND t.email IS NOT NULL AND t.email<>\'\'')); + if ($result>0 && is_array($fuserdest->users) && count($fuserdest->users)>0) { + foreach($fuserdest->users as $uuserdest) { + $listeuser[$uuserdest->id] = $uuserdest->user_get_property($uuserdest->id,'email'); + } + } elseif ($result<0) { + setEventMessages(null, $fuserdest->errors,'errors'); + } + if (count($listeuser)>0) { + $formmail->withtouser = $listeuser; + $formmail->withtoccuser = $listeuser; + } + + } $formmail->withto = GETPOST('sendto') ? GETPOST('sendto') : $liste; $formmail->withtocc = $liste; diff --git a/htdocs/langs/en_US/admin.lang b/htdocs/langs/en_US/admin.lang index 3a7e8a7d14a..71ade0f2058 100644 --- a/htdocs/langs/en_US/admin.lang +++ b/htdocs/langs/en_US/admin.lang @@ -273,6 +273,7 @@ MAIN_MAIL_ERRORS_TO=Email used as 'Errors-To' field in emails sent MAIN_MAIL_AUTOCOPY_TO= Send systematically a hidden carbon-copy of all sent emails to MAIN_DISABLE_ALL_MAILS=Disable all emails sendings (for test purposes or demos) MAIN_MAIL_FORCE_SENDTO=Send all emails to (instead of real recipients, for test purposes) +MAIN_MAIL_ENABLED_USER_DEST_SELECT=Add salaries users with email into allowed destinaries list MAIN_MAIL_SENDMODE=Method to use to send EMails MAIN_MAIL_SMTPS_ID=SMTP ID if authentication required MAIN_MAIL_SMTPS_PW=SMTP Password if authentication required diff --git a/htdocs/langs/en_US/mails.lang b/htdocs/langs/en_US/mails.lang index 3c512ca7286..3425ea7d603 100644 --- a/htdocs/langs/en_US/mails.lang +++ b/htdocs/langs/en_US/mails.lang @@ -11,7 +11,9 @@ MailFrom=Sender MailErrorsTo=Errors to MailReply=Reply to MailTo=Receiver(s) +MailToSalaries=To salarie(s) MailCC=Copy to +MailToCCSalaries=Copy to salarie(s) MailCCC=Cached copy to MailTopic=EMail topic MailText=Message diff --git a/htdocs/modulebuilder/template/myobject_list.php b/htdocs/modulebuilder/template/myobject_list.php index c1867fb87f7..70a07002955 100644 --- a/htdocs/modulebuilder/template/myobject_list.php +++ b/htdocs/modulebuilder/template/myobject_list.php @@ -202,6 +202,7 @@ $help_url=''; $title = $langs->trans('ListOf', $langs->transnoentitiesnoconv("MyObjects")); +// TODO : move this SQL request into fetchAll class method // Build and execute select // -------------------------------------------------------------------- $sql = 'SELECT '; diff --git a/htdocs/user/class/user.class.php b/htdocs/user/class/user.class.php index f3c8f03329f..10091853134 100644 --- a/htdocs/user/class/user.class.php +++ b/htdocs/user/class/user.class.php @@ -1,4 +1,6 @@ * Copyright (c) 2002-2003 Jean-Louis Bergamo * Copyright (c) 2004-2012 Laurent Destailleur @@ -115,7 +117,7 @@ class User extends CommonObject public $lastsearch_values_tmp; // To store current search criterias for user public $lastsearch_values; // To store last saved search criterias for user - public $users; // To store all tree of users hierarchy + public $users = array(); // To store all tree of users hierarchy public $parentof; // To store an array of all parents for all ids. private $cache_childids; @@ -135,6 +137,7 @@ class User extends CommonObject public $default_c_exp_tax_cat; public $default_range; + /** * Constructor de la classe * @@ -3011,5 +3014,106 @@ class User extends CommonObject return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref); } + /** + * Return property of contact from its id + * + * @param int $rowid id of contact + * @param string $mode 'email' or 'mobile' + * @return string Email of contact with format: "Full name " + */ + function user_get_property($rowid,$mode) + { + $user_property=''; + + if (empty($rowid)) return ''; + + $sql = "SELECT rowid, email, user_mobile, civility, lastname, firstname"; + $sql.= " FROM ".MAIN_DB_PREFIX."user"; + $sql.= " WHERE rowid = '".$rowid."'"; + + $resql=$this->db->query($sql); + if ($resql) + { + $nump = $this->db->num_rows($resql); + + if ($nump) + { + $obj = $this->db->fetch_object($resql); + + if ($mode == 'email') $user_property = dolGetFirstLastname($obj->firstname, $obj->lastname)." <".$obj->email.">"; + else if ($mode == 'mobile') $user_property = $obj->user_mobile; + } + return $user_property; + } + else + { + dol_print_error($this->db); + } + } + + /** + * Load all objects into $this->lines + * + * @param string $sortorder sort order + * @param string $sortfield sort field + * @param int $limit limit page + * @param int $offset page + * @param array $filter filter output + * @return int <0 if KO, >0 if OK + */ + function fetchAll($sortorder='', $sortfield='', $limit=0, $offset=0, $filter=array()) + { + global $conf; + + + $sql="SELECT t.rowid"; + $sql.= ' FROM '.MAIN_DB_PREFIX .$this->table_element.' as t '; + $sql.= " WHERE 1"; + + //Manage filter + if (!empty($filter)){ + foreach($filter as $key => $value) { + if (strpos($key,'date')) { + $sql.= ' AND '.$key.' = \''.$this->db->idate($value).'\''; + } + elseif ($key=='customsql') { + $sql.= ' AND '.$value; + } else { + $sql.= ' AND '.$key.' LIKE \'%'.$value.'%\''; + } + } + } + $sql.= $this->db->order($sortfield,$sortorder); + if ($limit) $sql.= $this->db->plimit($limit+1,$offset); + + dol_syslog(get_class($this)."::".__METHOD__, LOG_DEBUG); + + $resql=$this->db->query($sql); + if ($resql) + { + $this->users=array(); + $num = $this->db->num_rows($resql); + if ($num) + { + while ($obj = $this->db->fetch_object($resql)) + { + $line = new self($this->db); + $result = $line->fetch($obj->rowid); + if ($result>0 && !empty($line->id)) { + $this->users[$obj->rowid] = clone $line; + } + } + $this->db->free($resql); + } + return $num; + } + else + { + $this->errors[] = $this->db->lasterror(); + return -1; + } + + } + } From d3ae285c0c3b2a480893dfe46ed44d7cb44a6844 Mon Sep 17 00:00:00 2001 From: florian HENRY Date: Wed, 21 Mar 2018 22:58:17 +0100 Subject: [PATCH 2/5] finish work --- htdocs/core/actions_sendmails.inc.php | 3 +++ htdocs/core/class/html.formmail.class.php | 2 +- .../triggers/interface_50_modAgenda_ActionsAuto.class.php | 4 ++++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/htdocs/core/actions_sendmails.inc.php b/htdocs/core/actions_sendmails.inc.php index 3498bbc132c..fc21202959c 100644 --- a/htdocs/core/actions_sendmails.inc.php +++ b/htdocs/core/actions_sendmails.inc.php @@ -457,6 +457,9 @@ if (($action == 'send' || $action == 'relance') && ! $_POST['addfile'] && ! $_PO if (is_array($attachedfiles) && count($attachedfiles)>0) { $object->attachedfiles = $attachedfiles; } + if (is_array($sendtouserid) && count($sendtouserid)>0 && !empty($conf->global->MAIN_MAIL_ENABLED_USER_DEST_SELECT)) { + $object->sendtouserid = $sendtouserid; + } // Call of triggers if (! empty($trigger_name)) diff --git a/htdocs/core/class/html.formmail.class.php b/htdocs/core/class/html.formmail.class.php index b2f64f4298d..2b1c94889e3 100644 --- a/htdocs/core/class/html.formmail.class.php +++ b/htdocs/core/class/html.formmail.class.php @@ -335,7 +335,7 @@ class FormMail extends Form $out.= "\n".'
'."\n"; if ($this->withform == 1) { - $out.= ''."\n"; + $out.= ''."\n"; $out.= ''; $out.= ''; diff --git a/htdocs/core/triggers/interface_50_modAgenda_ActionsAuto.class.php b/htdocs/core/triggers/interface_50_modAgenda_ActionsAuto.class.php index 78053a0631c..6922189abc4 100644 --- a/htdocs/core/triggers/interface_50_modAgenda_ActionsAuto.class.php +++ b/htdocs/core/triggers/interface_50_modAgenda_ActionsAuto.class.php @@ -889,6 +889,10 @@ class InterfaceActionsAuto extends DolibarrTriggers $actioncomm->fk_element = $elementid; $actioncomm->elementtype = $elementtype; + if (property_exists($object,'sendtouserid') && is_array($object->sendtouserid) && count($object->sendtouserid)>0) { + $actioncomm->userassigned=$object->sendtouserid; + } + $ret=$actioncomm->create($user); // User creating action if ($ret > 0 && $conf->global->MAIN_COPY_FILE_IN_EVENT_AUTO) From c8d309e5998c6dddbb55fd523c6dba9f6125dcf7 Mon Sep 17 00:00:00 2001 From: florian HENRY Date: Wed, 21 Mar 2018 23:00:33 +0100 Subject: [PATCH 3/5] clean --- htdocs/core/actions_sendmails.inc.php | 3 ++- htdocs/user/class/user.class.php | 2 -- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/htdocs/core/actions_sendmails.inc.php b/htdocs/core/actions_sendmails.inc.php index fc21202959c..dd6eeee4361 100644 --- a/htdocs/core/actions_sendmails.inc.php +++ b/htdocs/core/actions_sendmails.inc.php @@ -195,7 +195,8 @@ if (($action == 'send' || $action == 'relance') && ! $_POST['addfile'] && ! $_PO if ($val == 'thirdparty') // Id of third party { $tmparray[] = $thirdparty->name.' <'.$thirdparty->email.'>'; - } elseif ($val) // Id du contact + } + elseif ($val) // Id du contact { $tmparray[] = $thirdparty->contact_get_property((int) $val,'email'); $sendtoid[] = $val; diff --git a/htdocs/user/class/user.class.php b/htdocs/user/class/user.class.php index 10091853134..6cc7007615a 100644 --- a/htdocs/user/class/user.class.php +++ b/htdocs/user/class/user.class.php @@ -1,6 +1,4 @@ * Copyright (c) 2002-2003 Jean-Louis Bergamo * Copyright (c) 2004-2012 Laurent Destailleur From 487ef3b7c94786436b257e849b92c17ba0870a58 Mon Sep 17 00:00:00 2001 From: florian HENRY Date: Wed, 21 Mar 2018 23:01:20 +0100 Subject: [PATCH 4/5] cealn again --- htdocs/user/class/user.class.php | 1 - 1 file changed, 1 deletion(-) diff --git a/htdocs/user/class/user.class.php b/htdocs/user/class/user.class.php index 6cc7007615a..75bc87f61a7 100644 --- a/htdocs/user/class/user.class.php +++ b/htdocs/user/class/user.class.php @@ -135,7 +135,6 @@ class User extends CommonObject public $default_c_exp_tax_cat; public $default_range; - /** * Constructor de la classe * From 4ebb1385334ec103bdb456a7c7567b32077c6016 Mon Sep 17 00:00:00 2001 From: florian HENRY Date: Wed, 21 Mar 2018 23:03:22 +0100 Subject: [PATCH 5/5] last ones --- htdocs/user/class/user.class.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/htdocs/user/class/user.class.php b/htdocs/user/class/user.class.php index 75bc87f61a7..82b10426622 100644 --- a/htdocs/user/class/user.class.php +++ b/htdocs/user/class/user.class.php @@ -115,7 +115,7 @@ class User extends CommonObject public $lastsearch_values_tmp; // To store current search criterias for user public $lastsearch_values; // To store last saved search criterias for user - public $users = array(); // To store all tree of users hierarchy + public $users = array(); // To store all tree of users hierarchy public $parentof; // To store an array of all parents for all ids. private $cache_childids; @@ -3012,11 +3012,11 @@ class User extends CommonObject } /** - * Return property of contact from its id + * Return property of user from its id * * @param int $rowid id of contact * @param string $mode 'email' or 'mobile' - * @return string Email of contact with format: "Full name " + * @return string Email of user with format: "Full name " */ function user_get_property($rowid,$mode) { @@ -3049,7 +3049,7 @@ class User extends CommonObject } /** - * Load all objects into $this->lines + * Load all objects into $this->users * * @param string $sortorder sort order * @param string $sortfield sort field @@ -3062,7 +3062,6 @@ class User extends CommonObject { global $conf; - $sql="SELECT t.rowid"; $sql.= ' FROM '.MAIN_DB_PREFIX .$this->table_element.' as t '; $sql.= " WHERE 1";