Skip to content

Commit

Permalink
NEW : option to send email to salaries
Browse files Browse the repository at this point in the history
  • Loading branch information
atm-florian committed Mar 21, 2018
1 parent d647bf2 commit dc6f4c1
Show file tree
Hide file tree
Showing 8 changed files with 214 additions and 4 deletions.
10 changes: 10 additions & 0 deletions htdocs/admin/mails.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
{
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);
Expand Down Expand Up @@ -271,6 +272,12 @@ function initfields()
print '<input class="flat" name="MAIN_MAIL_FORCE_SENDTO" size="32" value="' . (! empty($conf->global->MAIN_MAIL_FORCE_SENDTO)?$conf->global->MAIN_MAIL_FORCE_SENDTO:'') . '" />';
print '</td></tr>';


//Add user to select destinaries list
print '<tr class="oddeven"><td>'.$langs->trans("MAIN_MAIL_ENABLED_USER_DEST_SELECT").'</td><td>';
print $form->selectyesno('MAIN_MAIL_ENABLED_USER_DEST_SELECT',$conf->global->MAIN_MAIL_ENABLED_USER_DEST_SELECT,1);
print '</td></tr>';

// Separator

print '<tr class="oddeven"><td colspan="2">&nbsp;</td></tr>';
Expand Down Expand Up @@ -541,6 +548,9 @@ function initfields()
if (! empty($conf->global->MAIN_MAIL_FORCE_SENDTO) && ! isValidEmail($conf->global->MAIN_MAIL_FORCE_SENDTO)) print img_warning($langs->trans("ErrorBadEMail"));
print '</td></tr>';

//Add user to select destinaries list
print '<tr class="oddeven"><td>'.$langs->trans("MAIN_MAIL_ENABLED_USER_DEST_SELECT").'</td><td>'.yn($conf->global->MAIN_MAIL_ENABLED_USER_DEST_SELECT).'</td></tr>';

// Separator

print '<tr class="oddeven"><td colspan="2">&nbsp;</td></tr>';
Expand Down
31 changes: 29 additions & 2 deletions htdocs/core/actions_sendmails.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@
$sendtocc='';
$sendtobcc='';
$sendtoid = array();
if (!empty($conf->global->MAIN_MAIL_ENABLED_USER_DEST_SELECT)) {
$sendtouserid=array();
}

// Define $sendto
$receiver=$_POST['receiver'];
Expand All @@ -192,14 +195,25 @@
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
Expand Down Expand Up @@ -230,6 +244,19 @@
}
}
}
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))
Expand Down
49 changes: 48 additions & 1 deletion htdocs/core/class/html.formmail.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -332,7 +335,7 @@ function get_form($addfileaction='addfile',$removefileaction='removefile')
$out.= "\n".'<!-- Begin form mail type='.$this->param["models"].' --><div id="mailformdiv"></div>'."\n";
if ($this->withform == 1)
{
$out.= '<form method="POST" name="mailform" id="mailform" enctype="multipart/form-data" action="'.$this->param["returnurl"].'#formmail">'."\n";
$out.= '<form method="POST" name="mailform" id="mailform" action="'.$this->param["returnurl"].'#formmail">'."\n";

$out.= '<a id="formmail" name="formmail"></a>';
$out.= '<input style="display:none" type="submit" id="sendmail" name="sendmail">';
Expand Down Expand Up @@ -623,6 +626,28 @@ function get_form($addfileaction='addfile',$removefileaction='removefile')
$out.= "</td></tr>\n";
}

// To User
if (! empty($this->withtouser) && is_array($this->withtouser) && !empty($conf->global->MAIN_MAIL_ENABLED_USER_DEST_SELECT))
{
$out.= '<tr><td>';
$out.= $langs->trans("MailToSalaries");
$out.= '</td><td>';

// 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.= "</td></tr>\n";
}

// withoptiononeemailperrecipient
if (! empty($this->withoptiononeemailperrecipient))
{
Expand Down Expand Up @@ -667,6 +692,28 @@ function get_form($addfileaction='addfile',$removefileaction='removefile')
$out.= "</td></tr>\n";
}

// To User cc
if (! empty($this->withtoccuser) && is_array($this->withtoccuser) && !empty($conf->global->MAIN_MAIL_ENABLED_USER_DEST_SELECT))
{
$out.= '<tr><td>';
$out.= $langs->trans("MailToCCSalaries");
$out.= '</td><td>';

// 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.= "</td></tr>\n";
}

// CCC
if (! empty($this->withtoccc) || is_array($this->withtoccc))
{
Expand Down
18 changes: 18 additions & 0 deletions htdocs/core/tpl/card_presend.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,24 @@
$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;
Expand Down
1 change: 1 addition & 0 deletions htdocs/langs/en_US/admin.lang
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions htdocs/langs/en_US/mails.lang
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions htdocs/modulebuilder/template/myobject_list.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@
$title = $langs->trans('ListOf', $langs->transnoentitiesnoconv("MyObjects"));


// TODO : move this SQL request into fetchAll class method
// Build and execute select
// --------------------------------------------------------------------
$sql = 'SELECT ';
Expand Down
106 changes: 105 additions & 1 deletion htdocs/user/class/user.class.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
use Splash\Tests\WsAdmin\A05SelfTestsTest;

/* Copyright (c) 2002-2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
* Copyright (c) 2002-2003 Jean-Louis Bergamo <jlb@j1b.org>
* Copyright (c) 2004-2012 Laurent Destailleur <eldy@users.sourceforge.net>
Expand Down Expand Up @@ -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;

Expand All @@ -135,6 +137,7 @@ class User extends CommonObject
public $default_c_exp_tax_cat;
public $default_range;


/**
* Constructor de la classe
*
Expand Down Expand Up @@ -3011,5 +3014,106 @@ public function generateDocument($modele, $outputlangs, $hidedetails=0, $hidedes
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 <email>"
*/
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;
}

}

}

0 comments on commit dc6f4c1

Please sign in to comment.