diff --git a/htdocs/admin/mails.php b/htdocs/admin/mails.php
index 28d243a3b13..f85853f5b97 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);
@@ -275,6 +276,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 '| |
';
@@ -544,6 +551,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 419180d6830..1ab7e4824f5 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'];
@@ -200,6 +203,18 @@ if (($action == 'send' || $action == 'relance') && ! $_POST['addfile'] && ! $_PO
}
}
}
+ 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 +245,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))
@@ -430,6 +458,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 ef3ff52dcfe..c5d66bc6791 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;
@@ -624,6 +627,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))
{
@@ -668,6 +693,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 5baa70825d8..e57452ef8d3 100644
--- a/htdocs/core/tpl/card_presend.tpl.php
+++ b/htdocs/core/tpl/card_presend.tpl.php
@@ -152,6 +152,24 @@ if ($action == 'presend')
}
}
}
+ 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/core/triggers/interface_50_modAgenda_ActionsAuto.class.php b/htdocs/core/triggers/interface_50_modAgenda_ActionsAuto.class.php
index a3b11222a0f..2301b1642b4 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)
diff --git a/htdocs/langs/en_US/admin.lang b/htdocs/langs/en_US/admin.lang
index efd33b05ba7..699b7690249 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 e38b038cfee..b155e2e4208 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 3f3325c28e7..e085af5e6ef 100644
--- a/htdocs/modulebuilder/template/myobject_list.php
+++ b/htdocs/modulebuilder/template/myobject_list.php
@@ -205,6 +205,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 9df599dd0bc..8ac4963f5b5 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; // 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;
@@ -3003,5 +3003,105 @@ class User extends CommonObject
return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref);
}
+ /**
+ * Return property of user from its id
+ *
+ * @param int $rowid id of contact
+ * @param string $mode 'email' or 'mobile'
+ * @return string Email of user 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->users
+ *
+ * @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;
+ }
+
+ }
+
}