Merge pull request #4944 from FHenry/develop_advtargetemailiing

NEW : Advance target emailing
This commit is contained in:
Laurent Destailleur
2016-05-05 23:18:27 +02:00
21 changed files with 3402 additions and 118 deletions

View File

@@ -3237,8 +3237,9 @@ abstract class CommonObject
function printObjectLines($action, $seller, $buyer, $selected=0, $dateSelector=0)
{
global $conf, $hookmanager, $langs, $user;
global $inputalsopricewithtax, $usemargins, $disableedit, $disablemove, $disableremove; // TODO We should not use global var for this !
// TODO We should not use global var for this !
global $inputalsopricewithtax, $usemargins, $disableedit, $disablemove, $disableremove;
// Define usemargins
$usemargins=0;
if (! empty($conf->margin->enabled) && ! empty($this->element) && in_array($this->element,array('facture','propal','commande'))) $usemargins=1;
@@ -3367,7 +3368,7 @@ abstract class CommonObject
{
global $conf,$langs,$user,$object,$hookmanager;
global $form,$bc,$bcdd;
global $object_rights, $disableedit, $disablemove, $disableremove; // TODO We should not use global var for this !
global $object_rights, $disableedit, $disablemove; // TODO We should not use global var for this !
$object_rights = $this->getRights();

View File

@@ -63,7 +63,7 @@ class ExtraFields
var $errno;
var $attribute_hidden;
public static $type2label=array(
'varchar'=>'String',
'text'=>'TextLong',
@@ -665,6 +665,7 @@ class ExtraFields
$param=$this->attribute_param[$key];
$perms=$this->attribute_perms[$key];
$list=$this->attribute_list[$key];
$hidden=$this->attribute_hidden[$key];
if (empty($showsize))
{
@@ -686,7 +687,7 @@ class ExtraFields
if ($showsize > 48) $showsize=48;
}
}
if (in_array($type,array('date','datetime')))
{
$tmp=explode(',',$size);
@@ -823,7 +824,7 @@ class ExtraFields
if (strpos($InfoFieldList[4], '$SEL$')!==false) {
$InfoFieldList[4]=str_replace('$SEL$','SELECT',$InfoFieldList[4]);
}
// current object id can be use into filter
if (strpos($InfoFieldList[4], '$ID$')!==false && !empty($objectid)) {
$InfoFieldList[4]=str_replace('$ID$',$objectid,$InfoFieldList[4]);
@@ -841,14 +842,14 @@ class ExtraFields
$sqlwhere.= ' WHERE '.$InfoFieldList[4];
}
}
else
else
{
$sqlwhere.= ' WHERE 1=1';
}
// Some tables may have field, some other not. For the moment we disable it.
if (in_array($InfoFieldList[0],array('tablewithentity')))
if (in_array($InfoFieldList[0],array('tablewithentity')))
{
$sqlwhere.= ' AND entity = '.$conf->entity;
$sqlwhere.= ' AND entity = '.$conf->entity;
}
$sql.=$sqlwhere;
//print $sql;
@@ -1007,19 +1008,19 @@ class ExtraFields
$sql = 'SELECT ' . $keyList;
$sql .= ' FROM ' . MAIN_DB_PREFIX . $InfoFieldList[0];
if (! empty($InfoFieldList[4])) {
// can use SELECT request
if (strpos($InfoFieldList[4], '$SEL$')!==false) {
$InfoFieldList[4]=str_replace('$SEL$','SELECT',$InfoFieldList[4]);
}
// current object id can be use into filter
if (strpos($InfoFieldList[4], '$ID$')!==false && !empty($objectid)) {
$InfoFieldList[4]=str_replace('$ID$',$objectid,$InfoFieldList[4]);
} else {
$InfoFieldList[4]=str_replace('$ID$','0',$InfoFieldList[4]);
}
// We have to join on extrafield table
if (strpos($InfoFieldList[4], 'extra') !== false) {
$sql .= ' as main, ' . MAIN_DB_PREFIX . $InfoFieldList[0] . '_extrafields as extra';
@@ -1031,13 +1032,13 @@ class ExtraFields
$sqlwhere .= ' WHERE 1=1';
}
// Some tables may have field, some other not. For the moment we disable it.
if (in_array($InfoFieldList[0], array ('tablewithentity')))
if (in_array($InfoFieldList[0], array ('tablewithentity')))
{
$sqlwhere .= ' AND entity = ' . $conf->entity;
}
// $sql.=preg_replace('/^ AND /','',$sqlwhere);
// print $sql;
$sql .= $sqlwhere;
dol_syslog(get_class($this) . '::showInputField type=chkbxlst',LOG_DEBUG);
$resql = $this->db->query($sql);
@@ -1143,6 +1144,9 @@ class ExtraFields
$out.='Error bad setup of extrafield';
}
}
if (!empty($hidden)) {
$out='<input type="hidden" value="'.$value.'" name="'.$keysuffix.'options_'.$key.$keyprefix.'" id="'.$keysuffix.'options_'.$key.$keyprefix.'"/>';
}
/* Add comments
if ($type == 'date') $out.=' (YYYY-MM-DD)';
elseif ($type == 'datetime') $out.=' (YYYY-MM-DD HH:MM:SS)';
@@ -1171,6 +1175,7 @@ class ExtraFields
$params=$this->attribute_param[$key];
$perms=$this->attribute_perms[$key];
$list=$this->attribute_list[$key];
$hidden=$this->attribute_hidden[$key];
$showsize=0;
if ($type == 'date')
@@ -1402,10 +1407,14 @@ class ExtraFields
$showsize=round($size);
if ($showsize > 48) $showsize=48;
}
//print $type.'-'.$size;
$out=$value;
if (!empty($hidden)) {
$out='';
}
return $out;
}
@@ -1422,7 +1431,7 @@ class ExtraFields
$type=$this->attribute_type[$key];
$align='';
if ($type == 'date')
{
$align="center";
@@ -1451,10 +1460,10 @@ class ExtraFields
{
$align="center";
}
return $align;
}
/**
* Return HTML string to print separator extrafield
*
@@ -1534,7 +1543,7 @@ class ExtraFields
return 0;
}
}
/**
* return array_options array for object by extrafields value (using for data send by forms)
*

View File

@@ -82,7 +82,7 @@ class FormAccounting
$sql.= " ORDER BY c.label ASC";
}
dol_syslog("Form::select_accounting_category", LOG_DEBUG);
dol_syslog(get_class($this).'::'.__METHOD__, LOG_DEBUG);
$resql=$db->query($sql);
if ($resql)
{

View File

@@ -38,7 +38,7 @@ function emailing_prepare_head(Mailing $object)
$head[$h][1] = $langs->trans("MailCard");
$head[$h][2] = 'card';
$h++;
if (! empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! $user->rights->mailing->mailing_advance->recipient) {
return $head;
}
@@ -48,11 +48,16 @@ function emailing_prepare_head(Mailing $object)
$head[$h][2] = 'targets';
$h++;
$head[$h][0] = DOL_URL_ROOT."/comm/mailing/advtargetemailing.php?id=".$object->id;
$head[$h][1] = $langs->trans("MailAdvTargetRecipients");
$head[$h][2] = 'advtargets';
$h++;
$head[$h][0] = DOL_URL_ROOT."/comm/mailing/info.php?id=".$object->id;
$head[$h][1] = $langs->trans("Info");
$head[$h][2] = 'info';
$h++;
complete_head_from_modules($conf,$langs,$object,$head,$h,'emailing');
complete_head_from_modules($conf,$langs,$object,$head,$h,'emailing','remove');

View File

@@ -0,0 +1,302 @@
<?php
/* Copyright (C) 2005-2010 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2005-2009 Regis Houssin <regis.houssin@capnetworks.com>
*
* This file is an example to follow to add your own email selector inside
* the Dolibarr email tool.
* Follow instructions given in README file to know what to change to build
* your own emailing list selector.
* Code that need to be changed in this file are marked by "CHANGE THIS" tag.
*/
/**
* \file advtargetingemaling/modules/mailings/advthirdparties.modules.php
* \ingroup advtargetingemaling
* \brief Example file to provide a list of recipients for mailing module
*/
include_once DOL_DOCUMENT_ROOT.'/core/modules/mailings/modules_mailings.php';
include_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
include_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
/**
* Class to manage a list of personalised recipients for mailing feature
*/
class mailing_advthirdparties extends MailingTargets
{
var $name='ThirdPartyAdvancedTargeting';
var $desc="Third parties";
var $require_admin=0;
var $require_module=array("none"); // This module should not be displayed as Selector in mailling
var $picto='company';
var $db;
/**
* Constructor
*
* @param DoliDB $db Database handler
*/
function __construct($db)
{
global $conf;
$this->db=$db;
}
/**
* This is the main function that returns the array of emails
*
* @param int $mailing_id Id of mailing. No need to use it.
* @param array $socid Array of id soc to add
* @param int $type_of_target define in advtargetemailing.class.php
* @param array $contactid Array of contact id to add
* @return int <0 if error, number of emails added if ok
*/
function add_to_target($mailing_id,$socid,$type_of_target, $contactid)
{
global $conf, $langs;
dol_syslog(get_class($this)."::add_to_target socid=".var_export($socid,true).' contactid='.var_export($contactid,true));
$cibles = array();
if (($type_of_target==1) || ($type_of_target==3)) {
// Select the third parties from category
if (count($socid)>0)
{
$sql= "SELECT s.rowid as id, s.email as email, s.nom as name, null as fk_contact";
$sql.= " FROM ".MAIN_DB_PREFIX."societe as s LEFT OUTER JOIN ".MAIN_DB_PREFIX."societe_extrafields se ON se.fk_object=s.rowid";
$sql.= " WHERE s.entity IN (".getEntity('societe', 1).")";
$sql.= " AND s.rowid IN (".implode(',',$socid).")";
$sql.= " ORDER BY email";
}
dol_syslog(get_class($this)."::add_to_target societe sql=".$sql, LOG_DEBUG);
// Stock recipients emails into targets table
$result=$this->db->query($sql);
if ($result)
{
$num = $this->db->num_rows($result);
$i = 0;
dol_syslog(get_class($this)."::add_to_target mailing ".$num." targets found", LOG_DEBUG);
$old = '';
while ($i < $num)
{
$obj = $this->db->fetch_object($result);
if (!empty($obj->email) && filter_var($obj->email, FILTER_VALIDATE_EMAIL)) {
if (!array_key_exists($obj->email, $cibles)) {
$cibles[$obj->email] = array(
'email' => $obj->email,
'fk_contact' => $obj->fk_contact,
'name' => $obj->name,
'firstname' => $obj->firstname,
'other' => '',
'source_url' => $this->url($obj->id,'thirdparty'),
'source_id' => $obj->id,
'source_type' => 'thirdparty'
);
}
}
$i++;
}
}
else
{
dol_syslog($this->db->error());
$this->error=$this->db->error();
return -1;
}
}
if (($type_of_target==1) || ($type_of_target==2)) {
// Select the third parties from category
if (count($socid)>0 || count($contactid)>0)
{
$sql= "SELECT socp.rowid as id, socp.email as email, socp.lastname as lastname, socp.firstname as firstname";
$sql.= " FROM ".MAIN_DB_PREFIX."socpeople as socp";
$sql.= " WHERE socp.entity IN (".getEntity('societe', 1).")";
if (count($contactid)>0) {
$sql.= " AND socp.rowid IN (".implode(',',$contactid).")";
}
if (count($socid)>0) {
$sql.= " AND socp.fk_soc IN (".implode(',',$socid).")";
}
$sql.= " ORDER BY email";
}
dol_syslog(get_class($this)."::add_to_target contact sql=".$sql);
// Stock recipients emails into targets table
$result=$this->db->query($sql);
if ($result)
{
$num = $this->db->num_rows($result);
$i = 0;
dol_syslog(get_class($this)."::add_to_target mailing ".$num." targets found");
$old = '';
while ($i < $num)
{
$obj = $this->db->fetch_object($result);
if (!empty($obj->email) && filter_var($obj->email, FILTER_VALIDATE_EMAIL)) {
if (!array_key_exists($obj->email, $cibles)) {
$cibles[$obj->email] = array(
'email' => $obj->email,
'fk_contact' =>$obj->id,
'lastname' => $obj->lastname,
'firstname' => $obj->firstname,
'other' => '',
'source_url' => $this->url($obj->id,'contact'),
'source_id' => $obj->id,
'source_type' => 'contact'
);
}
}
$i++;
}
}
else
{
dol_syslog($this->db->error());
$this->error=$this->db->error();
return -1;
}
}
dol_syslog(get_class($this)."::add_to_target mailing cibles=".var_export($cibles,true), LOG_DEBUG);
return parent::add_to_target($mailing_id, $cibles);
}
/**
* On the main mailing area, there is a box with statistics.
* If you want to add a line in this report you must provide an
* array of SQL request that returns two field:
* One called "label", One called "nb".
*
* @return array Array with SQL requests
*/
function getSqlArrayForStats()
{
// CHANGE THIS: Optionnal
//var $statssql=array();
//$this->statssql[0]="SELECT field1 as label, count(distinct(email)) as nb FROM mytable WHERE email IS NOT NULL";
return array();
}
/**
* Return here number of distinct emails returned by your selector.
* For example if this selector is used to extract 500 different
* emails from a text file, this function must return 500.
*
* @return int Nb of recipients
*/
function getNbOfRecipients()
{
global $conf;
$sql = "SELECT count(distinct(s.email)) as nb";
$sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
$sql.= " WHERE s.email != ''";
$sql.= " AND s.entity IN (".getEntity('societe', 1).")";
// La requete doit retourner un champ "nb" pour etre comprise
// par parent::getNbOfRecipients
return parent::getNbOfRecipients($sql);
}
/**
* This is to add a form filter to provide variant of selector
* If used, the HTML select must be called "filter"
*
* @return string A html select zone
*/
function formFilter()
{
global $conf, $langs;
$langs->load("companies");
$s='';
$s.='<select name="filter" class="flat">';
// Show categories
$sql = "SELECT rowid, label, type, visible";
$sql.= " FROM ".MAIN_DB_PREFIX."categorie";
$sql.= " WHERE type in (1,2)"; // We keep only categories for suppliers and customers/prospects
// $sql.= " AND visible > 0"; // We ignore the property visible because third party's categories does not use this property (only products categories use it).
$sql.= " AND entity = ".$conf->entity;
$sql.= " ORDER BY label";
//print $sql;
$resql = $this->db->query($sql);
if ($resql)
{
$num = $this->db->num_rows($resql);
if (empty($conf->categorie->enabled)) $num=0; // Force empty list if category module is not enabled
if ($num) $s.='<option value="0">&nbsp;</option>';
else $s.='<option value="0">'.$langs->trans("ContactsAllShort").'</option>';
$i = 0;
while ($i < $num)
{
$obj = $this->db->fetch_object($resql);
$type='';
if ($obj->type == 1) $type=$langs->trans("Supplier");
if ($obj->type == 2) $type=$langs->trans("Customer");
$s.='<option value="'.$obj->rowid.'">'.dol_trunc($obj->label,38,'middle');
if ($type) $s.=' ('.$type.')';
$s.='</option>';
$i++;
}
}
else
{
dol_print_error($this->db);
}
$s.='</select>';
return $s;
}
/**
* Can include an URL link on each record provided by selector shown on target page.
*
* @param int $id ID
* @param string $type type
* @return string Url link
*/
function url($id,$type)
{
if ($type=='thirdparty') {
$companystatic=new Societe($this->db);
$companystatic->fetch($id);
return $companystatic->getNomUrl(0);
} elseif ($type=='contact') {
$contactstatic=new Contact($this->db);
$contactstatic->fetch($id);
return $contactstatic->getNomUrl(0);
}
}
}

View File

@@ -169,6 +169,7 @@ class MailingTargets // This can't be abstract as it is used for some method
$sql.= (empty($targetarray['source_id']) ? 'null' : "'".$this->db->escape($targetarray['source_id'])."'").",";
$sql .= "'".$this->db->escape(dol_hash($targetarray['email'].';'.$targetarray['lastname'].';'.$mailing_id.';'.$conf->global->MAILING_EMAIL_UNSUBSCRIBE_KEY))."',";
$sql .= "'".$this->db->escape($targetarray['source_type'])."')";
dol_syslog(get_class($this)."::".__METHOD__,LOG_DEBUG);
$result=$this->db->query($sql);
if ($result)
{
@@ -188,28 +189,26 @@ class MailingTargets // This can't be abstract as it is used for some method
}
}
dol_syslog(get_class($this)."::add_to_target: sql ".$sql,LOG_DEBUG);
dol_syslog(get_class($this)."::add_to_target: mailing ".$j." targets added");
dol_syslog(get_class($this)."::".__METHOD__.": mailing ".$j." targets added");
//Update the status to show thirdparty mail that don't want to be contacted anymore'
$sql = "UPDATE ".MAIN_DB_PREFIX."mailing_cibles";
$sql .= " SET statut=3";
$sql .= " WHERE fk_mailing=".$mailing_id." AND email in (SELECT email FROM ".MAIN_DB_PREFIX."societe where fk_stcomm=-1)";
$sql .= " AND source_type='thirdparty'";
dol_syslog(get_class($this)."::".__METHOD__.": mailing update status to display thirdparty mail that do not want to be contacted");
$result=$this->db->query($sql);
dol_syslog(get_class($this)."::add_to_target: mailing update status to display thirdparty mail that do not want to be contacted sql:".$sql);
//Update the status to show contact mail that don't want to be contacted anymore'
$sql = "UPDATE ".MAIN_DB_PREFIX."mailing_cibles";
$sql .= " SET statut=3";
$sql .= " WHERE fk_mailing=".$mailing_id." AND source_type='contact' AND (email in (SELECT sc.email FROM ".MAIN_DB_PREFIX."socpeople AS sc ";
$sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe s ON s.fk_stcomm=-1 AND s.rowid=sc.fk_soc)";
$sql .= " OR no_email=1)";
$sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe s ON s.rowid=sc.fk_soc WHERE s.fk_stcomm=-1 OR no_email=1))";
dol_syslog(get_class($this)."::".__METHOD__.": mailing update status to display contact mail that do not want to be contacted",LOG_DEBUG);
$result=$this->db->query($sql);
dol_syslog(get_class($this)."::add_to_target: mailing update status to display contact mail that do not want to be contacted sql:".$sql);
$this->update_nb($mailing_id);

View File

@@ -118,9 +118,7 @@
<!-- Always editable -->
<tr><td><?php echo $langs->trans("AlwaysEditable"); ?></td><td class="valeur"><input id="alwayseditable" type="checkbox" name="alwayseditable" <?php echo ((GETPOST('alwayseditable') || ! GETPOST('button'))?' checked':''); ?>></td></tr>
<!-- Is visible or not -->
<?php if (! empty($conf->global->MAIN_CAN_HIDE_EXTRAFIELDS)) { ?>
<tr><td><?php echo $langs->trans("Hidden"); ?></td><td class="valeur"><input id="ishidden" type="checkbox" name="ishidden" <?php echo (GETPOST('ishidden') ?' checked' : ''); ?>></td></tr>
<?php } ?>
<?php if ($conf->global->MAIN_FEATURES_LEVEL >= 2) { ?>
<!-- By default visible into list -->
<tr><td><?php echo $langs->trans("ByDefaultInList"); ?>

View File

@@ -110,7 +110,7 @@ elseif (($type== 'sellist') || ($type == 'chkbxlst') || ($type == 'link') )
<tr><td class="fieldrequired"><?php echo $langs->trans("AttributeCode"); ?></td><td class="valeur"><?php echo $attrname; ?></td></tr>
<!-- Type -->
<tr><td class="fieldrequired"><?php echo $langs->trans("Type"); ?></td><td class="valeur">
<?php
<?php
// Define list of possible type transition
$typewecanchangeinto=array(
'varchar'=>array('varchar', 'phone', 'mail', 'select'),
@@ -135,7 +135,7 @@ else
{
print $type2label[$type];
print '<input type="hidden" name="type" id="type" value="'.$type.'">';
}
}
?>
</td></tr>
<!-- Size -->
@@ -167,11 +167,7 @@ else
<!-- Always editable -->
<tr><td><?php echo $langs->trans("AlwaysEditable"); ?></td><td class="valeur"><input id="alwayseditable" type="checkbox" name="alwayseditable" <?php echo ($alwayseditable?' checked':''); ?>></td></tr>
<!-- Is visible or not -->
<?php if (! empty($conf->global->MAIN_CAN_HIDE_EXTRAFIELDS)) { ?>
<tr><td><?php echo $langs->trans("Hidden"); ?></td><td class="valeur"><input id="ishidden" type="checkbox" name="ishidden" <?php echo ($ishidden ?' checked':''); ?>></td></tr>
<?php } else {
print '<input id="ishidden" type="hidden" name="ishidden" value="'.($ishidden?1:0).'">';
}?>
<!-- By default visible into list -->
<?php if ($conf->global->MAIN_FEATURES_LEVEL >= 2) { ?>
<tr><td><?php echo $langs->trans("ByDefaultInList"); ?>

View File

@@ -61,7 +61,7 @@ foreach($extrafields->attribute_type as $key => $value)
print '<td align="center">'.yn($extrafields->attribute_unique[$key])."</td>\n";
print '<td align="center">'.yn($extrafields->attribute_required[$key])."</td>\n";
print '<td align="center">'.yn($extrafields->attribute_alwayseditable[$key])."</td>\n";
if (! empty($conf->global->MAIN_CAN_HIDE_EXTRAFIELDS)) print '<td align="center">'.yn($extrafields->attribute_hidden[$key])."</td>\n"; // Add hidden option on not working feature. Why hide if user can't see it.
print '<td align="center">'.yn($extrafields->attribute_hidden[$key])."</td>\n"; // Add hidden option on not working feature. Why hide if user can't see it.
print '<td align="right"><a href="'.$_SERVER["PHP_SELF"].'?action=edit&attrname='.$key.'">'.img_edit().'</a>';
print "&nbsp; <a href=\"".$_SERVER["PHP_SELF"]."?action=delete&attrname=$key\">".img_delete()."</a></td>\n";
print "</tr>";