dolibarr/htdocs/comm/mailing/class/html.formadvtargetemailing.class.php

395 lines
11 KiB
PHP
Raw Normal View History

2016-04-01 10:52:19 +02:00
<?php
/* Copyright (C) 2014 Florian Henry <florian.henry@open-concept.pro>
* Copyright (C) 2019 Frédéric France <frederic.france@netlogic.fr>
2017-12-01 15:39:18 +01:00
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2019-09-23 21:55:30 +02:00
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2017-12-01 15:39:18 +01:00
*/
2016-04-01 10:52:19 +02:00
/**
* \file comm/mailing/class/html.formadvtargetemailing.class.php
2016-05-06 21:28:06 +02:00
* \ingroup mailing
2018-08-18 15:56:23 +02:00
* \brief Fichier de la classe des fonctions predefinies de composant html advtargetemailing
2016-04-01 10:52:19 +02:00
*/
/**
* Class to manage building of HTML components
*/
2016-04-04 15:28:25 +02:00
class FormAdvTargetEmailing extends Form
{
2018-08-22 10:51:55 +02:00
/**
* @var DoliDB Database handler.
*/
public $db;
2018-08-29 15:37:35 +02:00
2018-08-17 22:29:21 +02:00
/**
* @var string Error code (or message)
*/
public $error = '';
2016-04-01 17:09:03 +02:00
2016-04-01 10:52:19 +02:00
/**
* Constructor
*
* @param DoliDB $db handler
*/
public function __construct($db)
{
global $langs;
2016-04-01 17:09:03 +02:00
$this->db = $db;
}
2016-04-01 17:09:03 +02:00
2016-04-01 10:52:19 +02:00
/**
* Affiche un champs select contenant une liste
*
2016-04-01 17:09:03 +02:00
* @param array $selected_array à preselectionner
2016-04-01 10:52:19 +02:00
* @param string $htmlname select field
* @return string select field
*/
public function multiselectProspectionStatus($selected_array = array(), $htmlname = 'cust_prospect_status')
{
2016-04-01 10:52:19 +02:00
global $conf, $langs;
2017-12-01 15:39:18 +01:00
$options_array = array();
2016-04-01 17:09:03 +02:00
2016-04-01 10:52:19 +02:00
$sql = "SELECT code, label";
$sql .= " FROM ".MAIN_DB_PREFIX."c_prospectlevel";
2016-04-01 10:52:19 +02:00
$sql .= " WHERE active > 0";
$sql .= " ORDER BY sortorder";
2020-02-17 13:59:44 +01:00
2019-02-10 10:45:49 +01:00
$resql = $this->db->query($sql);
2016-04-01 10:52:19 +02:00
if ($resql) {
2019-02-10 10:45:49 +01:00
$num = $this->db->num_rows($resql);
2016-04-01 10:52:19 +02:00
$i = 0;
while ($i < $num) {
2019-02-10 10:45:49 +01:00
$obj = $this->db->fetch_object($resql);
2016-04-01 17:09:03 +02:00
2019-02-10 10:45:49 +01:00
$level = $langs->trans($obj->code);
2021-02-23 18:59:19 +01:00
if ($level == $obj->code) {
2019-02-10 10:45:49 +01:00
$level = $langs->trans($obj->label);
2021-02-23 18:59:19 +01:00
}
2017-12-01 15:39:18 +01:00
$options_array[$obj->code] = $level;
2016-04-01 17:09:03 +02:00
$i++;
2016-04-01 10:52:19 +02:00
}
} else {
2017-12-01 15:39:18 +01:00
dol_print_error($this->db);
2016-04-01 10:52:19 +02:00
}
2017-12-01 15:39:18 +01:00
return $this->advMultiselectarray($htmlname, $options_array, $selected_array);
}
2016-04-01 17:09:03 +02:00
2016-04-01 10:52:19 +02:00
/**
* Return combo list of activated countries, into language of user
*
2021-02-23 18:59:19 +01:00
* @param string $htmlname of html select object
* @param array $selected_array or Code or Label of preselected country
* @return string HTML string with select
2016-04-01 10:52:19 +02:00
*/
public function multiselectCountry($htmlname = 'country_id', $selected_array = array())
{
2016-04-01 10:52:19 +02:00
global $conf, $langs;
2016-04-01 17:09:03 +02:00
2016-05-08 11:33:46 +02:00
$langs->load("dict");
$maxlength = 0;
2017-07-11 14:47:37 +02:00
2016-04-01 10:52:19 +02:00
$out = '';
2016-05-08 11:33:46 +02:00
$countryArray = array();
$label = array();
2016-04-01 17:09:03 +02:00
2016-05-08 11:33:46 +02:00
$options_array = array();
2016-04-01 17:09:03 +02:00
2016-04-01 10:52:19 +02:00
$sql = "SELECT rowid, code as code_iso, label";
$sql .= " FROM ".MAIN_DB_PREFIX."c_country";
2016-04-01 10:52:19 +02:00
$sql .= " WHERE active = 1 AND code<>''";
$sql .= " ORDER BY code ASC";
2016-04-01 17:09:03 +02:00
2016-05-08 11:33:46 +02:00
$resql = $this->db->query($sql);
2016-04-01 10:52:19 +02:00
if ($resql) {
2016-05-08 11:33:46 +02:00
$num = $this->db->num_rows($resql);
2016-04-01 10:52:19 +02:00
$i = 0;
if ($num) {
$foundselected = false;
2016-04-01 17:09:03 +02:00
2016-05-08 11:33:46 +02:00
while ($i < $num) {
$obj = $this->db->fetch_object($resql);
2016-04-01 10:52:19 +02:00
$countryArray [$i] ['rowid'] = $obj->rowid;
$countryArray [$i] ['code_iso'] = $obj->code_iso;
$countryArray [$i] ['label'] = ($obj->code_iso && $langs->transnoentitiesnoconv("Country".$obj->code_iso) != "Country".$obj->code_iso ? $langs->transnoentitiesnoconv("Country".$obj->code_iso) : ($obj->label != '-' ? $obj->label : ''));
2016-05-08 11:33:46 +02:00
$label[$i] = $countryArray[$i]['label'];
$i++;
2016-04-01 10:52:19 +02:00
}
2016-04-01 17:09:03 +02:00
2022-06-03 14:14:53 +02:00
$array1_sort_order = SORT_ASC;
array_multisort($label, $array1_sort_order, $countryArray);
2016-04-01 17:09:03 +02:00
2016-05-08 11:33:46 +02:00
foreach ($countryArray as $row) {
$label = dol_trunc($row['label'], $maxlength, 'middle');
2021-02-23 18:59:19 +01:00
if ($row['code_iso']) {
$label .= ' ('.$row['code_iso'].')';
2021-02-23 18:59:19 +01:00
}
2016-04-01 17:09:03 +02:00
2016-05-08 11:33:46 +02:00
$options_array[$row['rowid']] = $label;
2016-04-01 10:52:19 +02:00
}
}
} else {
2016-05-08 11:33:46 +02:00
dol_print_error($this->db);
2016-04-01 10:52:19 +02:00
}
2016-04-01 17:09:03 +02:00
2016-05-08 11:33:46 +02:00
return $this->advMultiselectarray($htmlname, $options_array, $selected_array);
}
2016-04-01 17:09:03 +02:00
2016-04-01 10:52:19 +02:00
/**
* Return select list for categories (to use in form search selectors)
*
2016-04-04 15:28:25 +02:00
* @param string $htmlname control name
* @param array $selected_array array of data
* @param User $user User action
2016-04-01 10:52:19 +02:00
* @return string combo list code
*/
public function multiselectselectSalesRepresentatives($htmlname, $selected_array, $user)
{
2016-04-01 17:09:03 +02:00
2016-04-01 10:52:19 +02:00
global $conf;
2016-04-01 17:09:03 +02:00
$options_array = array();
2016-04-01 17:09:03 +02:00
$sql_usr = '';
2016-04-01 10:52:19 +02:00
$sql_usr .= "SELECT DISTINCT u2.rowid, u2.lastname as name, u2.firstname, u2.login";
$sql_usr .= " FROM ".MAIN_DB_PREFIX."user as u2, ".MAIN_DB_PREFIX."societe_commerciaux as sc";
$sql_usr .= " WHERE u2.entity IN (0,".$conf->entity.")";
2016-04-01 10:52:19 +02:00
$sql_usr .= " AND u2.rowid = sc.fk_user ";
2021-02-23 18:59:19 +01:00
if (!empty($conf->global->USER_HIDE_INACTIVE_IN_COMBOBOX)) {
2016-04-01 10:52:19 +02:00
$sql_usr .= " AND u2.statut<>0 ";
2021-02-23 18:59:19 +01:00
}
2016-04-01 10:52:19 +02:00
$sql_usr .= " ORDER BY name ASC";
// print $sql_usr;exit;
2016-04-01 17:09:03 +02:00
$resql_usr = $this->db->query($sql_usr);
2016-04-01 10:52:19 +02:00
if ($resql_usr) {
while ($obj_usr = $this->db->fetch_object($resql_usr)) {
$label = $obj_usr->firstname." ".$obj_usr->name." (".$obj_usr->login.')';
2016-04-01 17:09:03 +02:00
2016-04-01 10:52:19 +02:00
$options_array [$obj_usr->rowid] = $label;
}
$this->db->free($resql_usr);
2016-04-01 10:52:19 +02:00
} else {
dol_print_error($this->db);
2016-04-01 10:52:19 +02:00
}
2016-04-01 17:09:03 +02:00
return $this->advMultiselectarray($htmlname, $options_array, $selected_array);
}
2016-04-01 17:09:03 +02:00
2016-04-01 10:52:19 +02:00
/**
* Return select list for categories (to use in form search selectors)
*
* @param string $htmlname of combo list (example: 'search_sale')
2016-04-04 15:28:25 +02:00
* @param array $selected_array selected array
2016-04-01 10:52:19 +02:00
* @return string combo list code
*/
public function multiselectselectLanguage($htmlname = '', $selected_array = array())
{
2016-04-01 17:09:03 +02:00
global $conf, $langs;
2016-04-01 17:09:03 +02:00
$options_array = array();
2016-04-01 17:09:03 +02:00
$langs_available = $langs->get_available_languages(DOL_DOCUMENT_ROOT, 12);
2016-04-01 17:09:03 +02:00
2021-02-23 18:59:19 +01:00
foreach ($langs_available as $key => $value) {
2016-04-01 10:52:19 +02:00
$label = $value;
2016-05-08 11:33:46 +02:00
$options_array[$key] = $label;
2016-04-01 10:52:19 +02:00
}
asort($options_array);
2016-05-08 11:33:46 +02:00
return $this->advMultiselectarray($htmlname, $options_array, $selected_array);
}
2016-04-01 17:09:03 +02:00
2016-04-01 10:52:19 +02:00
/**
2016-04-01 17:09:03 +02:00
* Return multiselect list of entities for extrafeild type sellist
2016-04-01 10:52:19 +02:00
*
2016-04-04 15:28:25 +02:00
* @param string $htmlname control name
* @param array $sqlqueryparam array
* @param array $selected_array array
2016-04-01 17:09:03 +02:00
*
* @return string HTML combo
2016-04-01 10:52:19 +02:00
*/
2019-02-27 20:45:07 +01:00
public function advMultiselectarraySelllist($htmlname, $sqlqueryparam = array(), $selected_array = array())
2017-12-01 15:39:18 +01:00
{
$options_array = array();
2016-04-01 17:09:03 +02:00
2021-02-23 18:59:19 +01:00
if (is_array($sqlqueryparam)) {
$param_list = array_keys($sqlqueryparam);
$InfoFieldList = explode(":", $param_list [0]);
2016-04-01 17:09:03 +02:00
2016-04-01 10:52:19 +02:00
// 0 1 : tableName
2022-01-18 13:44:52 +01:00
// 1 2 : label field name Name of field that contains the label
2016-04-01 10:52:19 +02:00
// 2 3 : key fields name (if differ of rowid)
// 3 4 : where clause filter on column or table extrafield, syntax field='value' or extra.field=value
2016-04-01 17:09:03 +02:00
2016-04-01 10:52:19 +02:00
$keyList = 'rowid';
2016-04-01 17:09:03 +02:00
if (count($InfoFieldList) >= 3) {
2021-08-27 22:42:04 +02:00
if (strpos($InfoFieldList[3], 'extra.') !== false) {
$keyList = 'main.'.$InfoFieldList[2].' as rowid';
2016-04-01 10:52:19 +02:00
} else {
2021-08-27 22:42:04 +02:00
$keyList = $InfoFieldList[2].' as rowid';
2016-04-01 10:52:19 +02:00
}
}
2016-04-01 17:09:03 +02:00
2021-08-27 22:42:04 +02:00
$sql = "SELECT ".$keyList.", ".$InfoFieldList[1];
$sql .= " FROM ".MAIN_DB_PREFIX.$InfoFieldList[0];
if (!empty($InfoFieldList[3])) {
2016-04-01 10:52:19 +02:00
// We have to join on extrafield table
2021-08-27 22:42:04 +02:00
if (strpos($InfoFieldList[3], 'extra') !== false) {
$sql .= ' as main, '.MAIN_DB_PREFIX.$InfoFieldList[0].'_extrafields as extra';
$sql .= " WHERE extra.fk_object=main.".$InfoFieldList[2]." AND ".$InfoFieldList[3];
2016-04-01 10:52:19 +02:00
} else {
2021-08-27 22:42:04 +02:00
$sql .= " WHERE ".$InfoFieldList[3];
2016-04-01 10:52:19 +02:00
}
}
if (!empty($InfoFieldList[1])) {
2016-04-01 10:52:19 +02:00
$sql .= " ORDER BY nom";
}
// $sql.= ' WHERE entity = '.$conf->entity;
2016-04-01 17:09:03 +02:00
2016-05-08 11:33:46 +02:00
$resql = $this->db->query($sql);
2016-04-01 10:52:19 +02:00
if ($resql) {
2016-05-08 11:33:46 +02:00
$num = $this->db->num_rows($resql);
2016-04-01 10:52:19 +02:00
$i = 0;
if ($num) {
while ($i < $num) {
$obj = $this->db->fetch_object($resql);
2021-08-27 22:42:04 +02:00
$labeltoshow = dol_trunc($obj->$InfoFieldList[1], 90);
$options_array[$obj->rowid] = $labeltoshow;
$i++;
2016-04-01 10:52:19 +02:00
}
}
$this->db->free($resql);
2016-04-01 10:52:19 +02:00
}
}
2016-04-01 17:09:03 +02:00
2016-05-08 11:33:46 +02:00
return $this->advMultiselectarray($htmlname, $options_array, $selected_array);
2016-04-01 10:52:19 +02:00
}
2016-04-01 17:09:03 +02:00
2016-04-01 10:52:19 +02:00
/**
* Return combo list with people title
*
2016-05-08 11:33:46 +02:00
* @param string $htmlname Name of HTML select combo field
* @param array $selected_array Array
* @return string HTML combo
2016-04-01 10:52:19 +02:00
*/
2019-02-27 20:45:07 +01:00
public function multiselectCivility($htmlname = 'civilite_id', $selected_array = array())
2016-04-01 10:52:19 +02:00
{
global $conf, $langs, $user;
2016-04-01 10:52:19 +02:00
$langs->load("dict");
2016-04-01 17:09:03 +02:00
$options_array = array();
2016-04-01 17:09:03 +02:00
2016-04-01 10:52:19 +02:00
$sql = "SELECT rowid, code, label as civilite, active FROM ".MAIN_DB_PREFIX."c_civility";
$sql .= " WHERE active = 1";
2016-04-01 17:09:03 +02:00
dol_syslog(__METHOD__, LOG_DEBUG);
$resql = $this->db->query($sql);
2021-02-23 18:59:19 +01:00
if ($resql) {
2016-04-01 10:52:19 +02:00
$num = $this->db->num_rows($resql);
$i = 0;
2021-02-23 18:59:19 +01:00
if ($num) {
while ($i < $num) {
2016-04-01 10:52:19 +02:00
$obj = $this->db->fetch_object($resql);
2022-01-18 13:44:52 +01:00
// If a translation exists, we use it, else we use the default label
$label = ($langs->trans("Civility".$obj->code) != "Civility".$obj->code ? $langs->trans("Civility".$obj->code) : ($obj->civilite != '-' ? $obj->civilite : ''));
2016-04-01 17:09:03 +02:00
$options_array[$obj->code] = $label;
2016-04-01 17:09:03 +02:00
2016-04-01 10:52:19 +02:00
$i++;
}
}
2020-05-21 15:05:19 +02:00
} else {
2016-04-01 10:52:19 +02:00
dol_print_error($this->db);
}
2016-04-01 17:09:03 +02:00
return $this->advMultiselectarray($htmlname, $options_array, $selected_array);
2016-04-01 10:52:19 +02:00
}
2016-04-01 17:09:03 +02:00
2016-04-01 10:52:19 +02:00
/**
* Return multiselect list of entities.
*
* @param string $htmlname select
* @param array $options_array to manage
* @param array $selected_array to manage
* @param int $showempty show empty
2016-04-01 17:09:03 +02:00
* @return string HTML combo
2016-04-01 10:52:19 +02:00
*/
public function advMultiselectarray($htmlname, $options_array = array(), $selected_array = array(), $showempty = 0)
{
2016-04-01 10:52:19 +02:00
global $conf, $langs;
2016-04-01 17:09:03 +02:00
$form = new Form($this->db);
$return = $form->multiselectarray($htmlname, $options_array, $selected_array, 0, 0, '', 0, 295);
2016-04-01 10:52:19 +02:00
return $return;
}
2016-04-01 17:09:03 +02:00
/**
2018-06-24 17:55:03 +02:00
* Return a combo list to select emailing target selector
2016-04-01 17:09:03 +02:00
*
2018-06-24 17:55:03 +02:00
* @param string $htmlname control name
* @param integer $selected defaut selected
* @param integer $showempty empty lines
* @param string $type_element Type element. Example: 'mailing'
* @param string $morecss More CSS
2018-06-24 17:55:03 +02:00
* @return string HTML combo
2016-04-01 17:09:03 +02:00
*/
public function selectAdvtargetemailingTemplate($htmlname = 'template_id', $selected = 0, $showempty = 0, $type_element = 'mailing', $morecss = '')
{
2016-04-01 10:52:19 +02:00
global $conf, $user, $langs;
2016-04-01 17:09:03 +02:00
2016-04-01 10:52:19 +02:00
$out = '';
2016-04-01 17:09:03 +02:00
$sql = "SELECT c.rowid, c.name, c.fk_element";
$sql .= " FROM ".MAIN_DB_PREFIX."mailing_advtarget as c";
2021-04-30 10:57:21 +02:00
$sql .= " WHERE type_element = '".$this->db->escape($type_element)."'";
2016-04-01 10:52:19 +02:00
$sql .= " ORDER BY c.name";
2016-04-01 17:09:03 +02:00
dol_syslog(__METHOD__, LOG_DEBUG);
$resql = $this->db->query($sql);
2016-04-01 10:52:19 +02:00
if ($resql) {
$out .= '<select id="'.$htmlname.'" class="flat'.($morecss ? ' '.$morecss : '').'" name="'.$htmlname.'">';
2021-02-23 18:59:19 +01:00
if ($showempty) {
2016-04-01 10:52:19 +02:00
$out .= '<option value=""></option>';
2021-02-23 18:59:19 +01:00
}
$num = $this->db->num_rows($resql);
2016-04-01 10:52:19 +02:00
$i = 0;
if ($num) {
while ($i < $num) {
$obj = $this->db->fetch_object($resql);
2016-04-01 10:52:19 +02:00
$label = $obj->name;
if (empty($label)) {
$label = $obj->fk_element;
2016-04-01 10:52:19 +02:00
}
2016-04-01 17:09:03 +02:00
2016-04-01 10:52:19 +02:00
if ($selected > 0 && $selected == $obj->rowid) {
$out .= '<option value="'.$obj->rowid.'" selected="selected">'.$label.'</option>';
2016-04-01 10:52:19 +02:00
} else {
$out .= '<option value="'.$obj->rowid.'">'.$label.'</option>';
2016-04-01 10:52:19 +02:00
}
$i++;
2016-04-01 10:52:19 +02:00
}
}
$out .= '</select>';
} else {
dol_print_error($this->db);
2016-04-01 10:52:19 +02:00
}
$this->db->free($resql);
2016-04-01 10:52:19 +02:00
return $out;
}
2018-08-15 14:28:34 +02:00
}