Add select component to support correctly option C

ONTACT_USE_SEARCH_TO_SELECT
This commit is contained in:
Laurent Destailleur 2024-05-07 19:59:20 +02:00
parent c46d209e08
commit 8b84be2d6c
7 changed files with 462 additions and 204 deletions

View file

@ -876,7 +876,7 @@ if ($type == Categorie::TYPE_CONTACT) {
print '<table class="noborder centpercent">';
print '<tr class="liste_titre"><td>';
print $langs->trans("AssignCategoryTo").' &nbsp;';
print $form->selectContacts('', '', 'elemid');
print $form->selectcontacts('', '', 'elemid');
print '<input type="submit" class="button buttongen" value="'.$langs->trans("ClassifyInCategory").'"></td>';
print '</tr>';
print '</table>';

View file

@ -0,0 +1,144 @@
<?php
/* Copyright (C) 2006 Andre Cianfarani <acianfa@free.fr>
* Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
* Copyright (C) 2007-2019 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
*
* 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
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* \file htdocs/contact/ajax/contact.php
* \brief File to return Ajax response on contact list request. Used by the combo list of contacts.
* Search done on name, firstname...
*/
if (!defined('NOTOKENRENEWAL')) {
define('NOTOKENRENEWAL', 1); // Disables token renewal
}
if (!defined('NOREQUIREMENU')) {
define('NOREQUIREMENU', '1');
}
if (!defined('NOREQUIREHTML')) {
define('NOREQUIREHTML', '1');
}
if (!defined('NOREQUIREAJAX')) {
define('NOREQUIREAJAX', '1');
}
if (!defined('NOREQUIRESOC')) {
define('NOREQUIRESOC', '1');
}
// Load Dolibarr environment
require '../../main.inc.php';
require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
$htmlname = GETPOST('htmlname', 'aZ09');
$search_project_contact = GETPOST('search_project_contact', 'alpha');
$outjson = (GETPOSTINT('outjson') ? GETPOSTINT('outjson') : 0);
$action = GETPOST('action', 'aZ09');
$id = GETPOSTINT('id');
$socid = GETPOSTINT('socid');
$exclude = GETPOST('exclude', 'intcomma');
$showsoc = GETPOSTINT('showsoc');
$object = new Contact($db);
if ($id > 0) {
$object->fetch($id);
}
// Security check
if ($user->socid > 0) {
unset($action);
$socid = $user->socid;
$object->id = $socid;
}
restrictedArea($user, 'societe', $object->id, '&societe');
/*
* View
*/
top_httphead('application/json');
//print '<!-- Ajax page called with url '.dol_escape_htmltag($_SERVER["PHP_SELF"]).'?'.dol_escape_htmltag($_SERVER["QUERY_STRING"]).' -->'."\n";
if (!empty($action) && $action == 'fetch' && !empty($id)) {
require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
$outjson = array();
if ($object->id > 0) {
$outref = $object->ref;
$outname = $object->name;
$outdesc = '';
$outtype = $object->type;
$outjson = array('ref' => $outref, 'name' => $outname, 'desc' => $outdesc, 'type' => $outtype);
}
echo json_encode($outjson);
} else {
require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
if (empty($htmlname)) {
return;
}
// Filter on the company to search can be:
// Into an array with key $htmlname123 (we take first one found). Which page use this ?
// Into a var with name $htmlname can be 'prodid', 'productid', ...
$match = preg_grep('/('.preg_quote($htmlname, '/').'[0-9]+)/', array_keys($_GET));
sort($match);
$id = (!empty($match[0]) ? $match[0] : ''); // Take first key found into GET array with matching $htmlname123
// When used from jQuery, the search term is added as GET param "term".
$searchkey = (($id && GETPOST($id, 'alpha')) ? GETPOST($id, 'alpha') : (($htmlname && GETPOST($htmlname, 'alpha')) ? GETPOST($htmlname, 'alpha') : ''));
if (!$searchkey) {
return;
}
if (empty($form) || !is_object($form)) {
$form = new Form($db);
}
$limitto = '';
$showfunction = 0;
$morecss = 'minwidth100';
$options_only = 2;
$forcecombo = 0;
$events = array();
$moreparam = '';
$htmlid = '';
$multiple = 0;
$disableifempty = 0;
$prefix = getDolGlobalString('CONTACT_DONOTSEARCH_ANYWHERE') ? '' : '%'; // Can use index if CONTACT_DONOTSEARCH_ANYWHERE is on
$filter = "(lastname:like:'".$prefix.$search_project_contact."%') OR (firstname:like:'".$prefix.$search_project_contact."'%)";
// FIXME
// If CONTACT_USE_SEARCH_TO_SELECT is set, check that nb of chars in $filter is >= to avoid DOS attack
$arrayresult = $form->selectcontacts($socid, array(), $htmlname, 1, $exclude, $limitto, $showfunction, $morecss, $options_only, $showsoc, $forcecombo, $events, $moreparam, $htmlid, $multiple, $disableifempty, $filter);
if ($outjson) {
print json_encode($arrayresult);
}
}
$db->close();

View file

@ -1311,6 +1311,7 @@ class Form
/**
* Output html form to select a third party
* This call select_thirdparty_list() of ajax depending on setup.
*
* @param string $selected Preselected type
* @param string $htmlname Name of field in form
@ -1333,7 +1334,7 @@ class Form
public function select_company($selected = '', $htmlname = 'socid', $filter = '', $showempty = '', $showtype = 0, $forcecombo = 0, $events = array(), $limit = 0, $morecss = 'minwidth100', $moreparam = '', $selected_input_value = '', $hidelabel = 1, $ajaxoptions = array(), $multiple = false, $excludeids = array(), $showcode = 0)
{
// phpcs:enable
global $conf, $user, $langs;
global $conf, $langs;
$out = '';
@ -1373,7 +1374,7 @@ class Form
$out .= ajax_event($htmlname, $events);
$out .= ajax_autocompleter($selected, $htmlname, DOL_URL_ROOT . '/societe/ajax/company.php', $urloption, $conf->global->COMPANY_USE_SEARCH_TO_SELECT, 0, $ajaxoptions);
$out .= ajax_autocompleter($selected, $htmlname, DOL_URL_ROOT.'/societe/ajax/company.php', $urloption, getDolGlobalString('COMPANY_USE_SEARCH_TO_SELECT'), 0, $ajaxoptions);
} else {
// Immediate load of all database
$out .= $this->select_thirdparty_list($selected, $htmlname, $filter, $showempty, $showtype, $forcecombo, $events, '', 0, $limit, $morecss, $moreparam, $multiple, $excludeids, $showcode);
@ -1382,36 +1383,113 @@ class Form
return $out;
}
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
/**
* Output html form to select a contact
* This call select_contacts() of ajax depending on setup.
*
* Return HTML code of the SELECT of list of all contacts (for a third party or all).
* This also set the number of contacts found into $this->num
*
* @param int $socid Id of third party or 0 for all or -1 for empty list
* @param array|int $selected Array of ID of preselected contact id
* @param string $htmlname Name of HTML field ('none' for a not editable field)
* @param int<0,3>|string $showempty 0=no empty value, 1=add an empty value, 2=add line 'Internal' (used by user edit), 3=add an empty value only if more than one record into list
* @param string $exclude List of contacts id to exclude
* @param string $limitto Disable answers that are not id in this array list
* @param integer $showfunction Add function into label
* @param string $morecss Add more class to class style
* @param bool $options_only Return options only (for ajax treatment)
* @param integer $showsoc Add company into label
* @param int $forcecombo Force to use combo box (so no ajax beautify effect)
* @param array<array{method:string,url:string,htmlname:string,params:array<string,string>}> $events Event options. Example: array(array('method'=>'getContacts', 'url'=>dol_buildpath('/core/ajax/contacts.php',1), 'htmlname'=>'contactid', 'params'=>array('add-customer-contact'=>'disabled')))
* @param string $moreparam Add more parameters onto the select tag. For example 'style="width: 95%"' to avoid select2 component to go over parent container
* @param string $htmlid Html id to use instead of htmlname
* @param bool $multiple add [] in the name of element and add 'multiple' attribute
* @param integer $disableifempty Set tag 'disabled' on select if there is no choice
* @param string $selected_input_value Value of preselected input text (for use with ajax)
* @param string $filter Optional filters criteras. WARNING: To avoid SQL injection, only few chars [.a-z0-9 =<>()] are allowed here. Example: ((s.client:IN:1,3) AND (s.status:=:1)). Do not use a filter coming from input of users.
* @return int|string Return integer <0 if KO, HTML with select string if OK.
*/
public function select_contact($socid, $selected = array(), $htmlname = 'contactid', $showempty = 0, $exclude = '', $limitto = '', $showfunction = 0, $morecss = '', $options_only = false, $showsoc = 0, $forcecombo = 0, $events = array(), $moreparam = '', $htmlid = '', $multiple = false, $disableifempty = 0, $selected_input_value = '', $filter = '')
{
// phpcs:enable
global $conf, $langs;
$out = '';
if (!empty($conf->use_javascript_ajax) && getDolGlobalString('CONTACT_USE_SEARCH_TO_SELECT') && !$forcecombo) {
if (is_null($events)) {
$events = array();
}
require_once DOL_DOCUMENT_ROOT . '/core/lib/ajax.lib.php';
// No immediate load of all database
$placeholder = '';
if ($selected && empty($selected_input_value)) {
require_once DOL_DOCUMENT_ROOT . '/contact/class/contact.class.php';
$contacttmp = new Contact($this->db);
$contacttmp->fetch($selected);
$selected_input_value = $contacttmp->getFullName($langs);
unset($contacttmp);
}
if (!is_numeric($showempty)) {
$placeholder = $showempty;
}
// mode 1
$urloption = 'htmlname=' . urlencode((string) (str_replace('.', '_', $htmlname))) . '&outjson=1&filter=' . urlencode((string) ($filter)) . (empty($exclude) ? '' : '&exclude=' . urlencode($exclude)) . ($showsoc ? '&showsoc=' . urlencode((string) ($showsoc)) : '');
$out .= '<!-- force css to be higher than dialog popup --><style type="text/css">.ui-autocomplete { z-index: 1010; }</style>';
$out .= '<input type="text" class="' . $morecss . '" name="search_' . $htmlname . '" id="search_' . $htmlname . '" value="' . $selected_input_value . '"' . ($placeholder ? ' placeholder="' . dol_escape_htmltag($placeholder) . '"' : '') . ' ' . (getDolGlobalString('CONTACT_SEARCH_AUTOFOCUS') ? 'autofocus' : '') . ' />';
$out .= ajax_event($htmlname, $events);
$out .= ajax_autocompleter($selected, $htmlname, DOL_URL_ROOT.'/contact/ajax/contact.php', $urloption, getDolGlobalString('CONTACT_USE_SEARCH_TO_SELECT'), 0, $events);
} else {
// Immediate load of all database
$out .= $this->selectcontacts($socid, $selected, $htmlname, $showempty, $exclude, $limitto, $showfunction, $morecss, $options_only, $showsoc, $forcecombo, $events, $moreparam, $htmlid, $multiple, $disableifempty);
}
return $out;
}
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
/**
* Output html form to select a third party.
* Note, you must use the select_company to get the component to select a third party. This function must only be called by select_company.
* Note: you must use the select_company() to get the component to select a third party. This function must only be called by select_company.
*
* @param string $selected Preselected type
* @param string $htmlname Name of field in form
* @param string $filter Optional filters criteras. WARNING: To avoid SQL injection, only few chars [.a-z0-9 =<>] are allowed here, example: 's.rowid <> x'
* If you need parenthesis, use the Universal Filter Syntax, example: '(s.client:in:1,3)'
* Do not use a filter coming from input of users.
* @param string|int<0,1> $showempty Add an empty field (Can be '1' or text to use on empty line like 'SelectThirdParty')
* @param int<0,1> $showtype Show third party type in combolist (customer, prospect or supplier)
* @param int $forcecombo Force to use standard HTML select component without beautification
* @param array<array{method:string,url:string,htmlname:string,params:array<string,string>}> $events Event options. Example: array(array('method'=>'getContacts', 'url'=>dol_buildpath('/core/ajax/contacts.php',1), 'htmlname'=>'contactid', 'params'=>array('add-customer-contact'=>'disabled')))
* @param string $filterkey Filter on key value
* @param int<0,1> $outputmode 0=HTML select string, 1=Array
* @param int $limit Limit number of answers
* @param string $morecss Add more css styles to the SELECT component
* @param string $moreparam Add more parameters onto the select tag. For example 'style="width: 95%"' to avoid select2 component to go over parent container
* @param bool $multiple add [] in the name of element and add 'multiple' attribute
* @param string[] $excludeids Exclude IDs from the select combo
* @param int<0,1> $showcode Show code in list
* @param string $selected Preselected type
* @param string $htmlname Name of field in form
* @param string $filter Optional filters criteras. WARNING: To avoid SQL injection, only few chars [.a-z0-9 =<>] are allowed here, example: 's.rowid <> x'
* If you need parenthesis, use the Universal Filter Syntax, example: '(s.client:in:1,3)'
* Do not use a filter coming from input of users.
* @param string|int<0,1> $showempty Add an empty field (Can be '1' or text to use on empty line like 'SelectThirdParty')
* @param int<0,1> $showtype Show third party type in combolist (customer, prospect or supplier)
* @param int $forcecombo Force to use standard HTML select component without beautification
* @param array<array{method:string,url:string,htmlname:string,params:array<string,string>}> $events Event options. Example: array(array('method'=>'getContacts', 'url'=>dol_buildpath('/core/ajax/contacts.php',1), 'htmlname'=>'contactid', 'params'=>array('add-customer-contact'=>'disabled')))
* @param string $filterkey Filter on key value
* @param int<0,1> $outputmode 0=HTML select string, 1=Array
* @param int $limit Limit number of answers
* @param string $morecss Add more css styles to the SELECT component
* @param string $moreparam Add more parameters onto the select tag. For example 'style="width: 95%"' to avoid select2 component to go over parent container
* @param bool $multiple add [] in the name of element and add 'multiple' attribute
* @param string[] $excludeids Exclude IDs from the select combo
* @param int<0,1> $showcode Show code in list
* @return array<int,array{key:int,value:string,label:string,labelhtml:string}>|string HTML string with
* @see select_company()
*/
public function select_thirdparty_list($selected = '', $htmlname = 'socid', $filter = '', $showempty = '', $showtype = 0, $forcecombo = 0, $events = array(), $filterkey = '', $outputmode = 0, $limit = 0, $morecss = 'minwidth100', $moreparam = '', $multiple = false, $excludeids = array(), $showcode = 0)
{
// phpcs:enable
global $conf, $user, $langs;
global $user, $langs;
global $hookmanager;
$out = '';
@ -1637,147 +1715,33 @@ class Form
}
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
/**
* Return HTML combo list of absolute discounts
*
* @param string $selected Id Fixed reduction preselected
* @param string $htmlname Name of the form field
* @param string $filter Optional filter critreria
* @param int $socid Id of thirdparty
* @param int $maxvalue Max value for lines that can be selected
* @return int Return number of qualifed lines in list
*/
public function select_remises($selected, $htmlname, $filter, $socid, $maxvalue = 0)
{
// phpcs:enable
global $langs, $conf;
// On recherche les remises
$sql = "SELECT re.rowid, re.amount_ht, re.amount_tva, re.amount_ttc,";
$sql .= " re.description, re.fk_facture_source";
$sql .= " FROM " . $this->db->prefix() . "societe_remise_except as re";
$sql .= " WHERE re.fk_soc = " . (int) $socid;
$sql .= " AND re.entity = " . $conf->entity;
if ($filter) {
$sql .= " AND " . $filter;
}
$sql .= " ORDER BY re.description ASC";
dol_syslog(get_class($this) . "::select_remises", LOG_DEBUG);
$resql = $this->db->query($sql);
if ($resql) {
print '<select id="select_' . $htmlname . '" class="flat maxwidthonsmartphone" name="' . $htmlname . '">';
$num = $this->db->num_rows($resql);
$qualifiedlines = $num;
$i = 0;
if ($num) {
print '<option value="0">&nbsp;</option>';
while ($i < $num) {
$obj = $this->db->fetch_object($resql);
$desc = dol_trunc($obj->description, 40);
if (preg_match('/\(CREDIT_NOTE\)/', $desc)) {
$desc = preg_replace('/\(CREDIT_NOTE\)/', $langs->trans("CreditNote"), $desc);
}
if (preg_match('/\(DEPOSIT\)/', $desc)) {
$desc = preg_replace('/\(DEPOSIT\)/', $langs->trans("Deposit"), $desc);
}
if (preg_match('/\(EXCESS RECEIVED\)/', $desc)) {
$desc = preg_replace('/\(EXCESS RECEIVED\)/', $langs->trans("ExcessReceived"), $desc);
}
if (preg_match('/\(EXCESS PAID\)/', $desc)) {
$desc = preg_replace('/\(EXCESS PAID\)/', $langs->trans("ExcessPaid"), $desc);
}
$selectstring = '';
if ($selected > 0 && $selected == $obj->rowid) {
$selectstring = ' selected';
}
$disabled = '';
if ($maxvalue > 0 && $obj->amount_ttc > $maxvalue) {
$qualifiedlines--;
$disabled = ' disabled';
}
if (getDolGlobalString('MAIN_SHOW_FACNUMBER_IN_DISCOUNT_LIST') && !empty($obj->fk_facture_source)) {
$tmpfac = new Facture($this->db);
if ($tmpfac->fetch($obj->fk_facture_source) > 0) {
$desc = $desc . ' - ' . $tmpfac->ref;
}
}
print '<option value="' . $obj->rowid . '"' . $selectstring . $disabled . '>' . $desc . ' (' . price($obj->amount_ht) . ' ' . $langs->trans("HT") . ' - ' . price($obj->amount_ttc) . ' ' . $langs->trans("TTC") . ')</option>';
$i++;
}
}
print '</select>';
print ajax_combobox('select_' . $htmlname);
return $qualifiedlines;
} else {
dol_print_error($this->db);
return -1;
}
}
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
/**
* Return list of all contacts (for a third party or all)
*
* @param int $socid Id of third party or 0 for all
* @param string $selected Id of preselected contact
* @param string $htmlname Name of HTML field ('none' for a not editable field)
* @param int<0,3> $showempty 0=no empty value, 1=add an empty value, 2=add line 'Internal' (used by user edit), 3=add an empty value only if more than one record into list
* @param string $exclude List of contacts id to exclude
* @param string $limitto Disable answers that are not id in this array list
* @param integer $showfunction Add function into label
* @param string $morecss Add more class to class style
* @param integer $showsoc Add company into label
* @param int $forcecombo Force to use combo box
* @param array<array{method:string,url:string,htmlname:string,params:array<string,string>}> $events Event options. Example: array(array('method'=>'getContacts', 'url'=>dol_buildpath('/core/ajax/contacts.php',1), 'htmlname'=>'contactid', 'params'=>array('add-customer-contact'=>'disabled')))
* @param bool $options_only Return options only (for ajax treatment)
* @param string $moreparam Add more parameters onto the select tag. For example 'style="width: 95%"' to avoid select2 component to go over parent container
* @param string $htmlid Html id to use instead of htmlname
* @return int Return integer <0 if KO, Nb of contact in list if OK
* @deprecated You can use selectcontacts directly (warning order of param was changed)
*/
public function select_contacts($socid, $selected = '', $htmlname = 'contactid', $showempty = 0, $exclude = '', $limitto = '', $showfunction = 0, $morecss = '', $showsoc = 0, $forcecombo = 0, $events = array(), $options_only = false, $moreparam = '', $htmlid = '')
{
// phpcs:enable
print $this->selectcontacts($socid, $selected, $htmlname, $showempty, $exclude, $limitto, $showfunction, $morecss, $options_only, $showsoc, $forcecombo, $events, $moreparam, $htmlid);
return $this->num;
}
/**
* Return HTML code of the SELECT of list of all contacts (for a third party or all).
* This also set the number of contacts found into $this->num
* Note: you must use the select_contact() to get the component to select a contact. This function must only be called by select_contact.
*
* @since 9.0 Add afterSelectContactOptions hook
*
* @param int $socid Id of third party or 0 for all or -1 for empty list
* @param array|int $selected Array of ID of preselected contact id
* @param string $htmlname Name of HTML field ('none' for a not editable field)
* @param int<0,3>|string $showempty 0=no empty value, 1=add an empty value, 2=add line 'Internal' (used by user edit), 3=add an empty value only if more than one record into list
* @param string $exclude List of contacts id to exclude
* @param string $limitto Disable answers that are not id in this array list
* @param integer $showfunction Add function into label
* @param string $morecss Add more class to class style
* @param bool $options_only Return options only (for ajax treatment)
* @param integer $showsoc Add company into label
* @param int $forcecombo Force to use combo box (so no ajax beautify effect)
* @param int $socid Id of third party or 0 for all or -1 for empty list
* @param array|int $selected Array of ID of preselected contact id
* @param string $htmlname Name of HTML field ('none' for a not editable field)
* @param int<0,3>|string $showempty 0=no empty value, 1=add an empty value, 2=add line 'Internal' (used by user edit), 3=add an empty value only if more than one record into list
* @param string $exclude List of contacts id to exclude
* @param string $limitto Disable answers that are not id in this array list
* @param integer $showfunction Add function into label
* @param string $morecss Add more class to class style
* @param int $options_only 1=Return options only (for ajax treatment), 2=Return array
* @param integer $showsoc Add company into label
* @param int $forcecombo Force to use combo box (so no ajax beautify effect)
* @param array<array{method:string,url:string,htmlname:string,params:array<string,string>}> $events Event options. Example: array(array('method'=>'getContacts', 'url'=>dol_buildpath('/core/ajax/contacts.php',1), 'htmlname'=>'contactid', 'params'=>array('add-customer-contact'=>'disabled')))
* @param string $moreparam Add more parameters onto the select tag. For example 'style="width: 95%"' to avoid select2 component to go over parent container
* @param string $htmlid Html id to use instead of htmlname
* @param bool $multiple add [] in the name of element and add 'multiple' attribute
* @param integer $disableifempty Set tag 'disabled' on select if there is no choice
* @return int|string Return integer <0 if KO, HTML with select string if OK.
* @param string $moreparam Add more parameters onto the select tag. For example 'style="width: 95%"' to avoid select2 component to go over parent container
* @param string $htmlid Html id to use instead of htmlname
* @param bool $multiple add [] in the name of element and add 'multiple' attribute
* @param integer $disableifempty Set tag 'disabled' on select if there is no choice
* @param string $filter Optional filters criteras. WARNING: To avoid SQL injection, only few chars [.a-z0-9 =<>] are allowed here, example: 's.rowid <> x'
* If you need parenthesis, use the Universal Filter Syntax, example: '(s.client:in:1,3)'
* Do not use a filter coming from input of users.
* @return int|string Return integer <0 if KO, HTML with select string if OK.
*/
public function selectcontacts($socid, $selected = array(), $htmlname = 'contactid', $showempty = 0, $exclude = '', $limitto = '', $showfunction = 0, $morecss = '', $options_only = false, $showsoc = 0, $forcecombo = 0, $events = array(), $moreparam = '', $htmlid = '', $multiple = false, $disableifempty = 0)
public function selectcontacts($socid, $selected = array(), $htmlname = 'contactid', $showempty = 0, $exclude = '', $limitto = '', $showfunction = 0, $morecss = '', $options_only = 0, $showsoc = 0, $forcecombo = 0, $events = array(), $moreparam = '', $htmlid = '', $multiple = false, $disableifempty = 0, $filter = array())
{
global $conf, $langs, $hookmanager, $action;
@ -1787,13 +1751,42 @@ class Form
$htmlid = $htmlname;
}
$num = 0;
$out = '';
$outarray = array();
if ($selected === '') {
$selected = array();
} elseif (!is_array($selected)) {
$selected = array($selected);
}
$out = '';
// Clean $filter that may contains sql conditions so sql code
if (function_exists('testSqlAndScriptInject')) {
if (testSqlAndScriptInject($filter, 3) > 0) {
$filter = '';
return 'SQLInjectionTryDetected';
}
}
if ($filter != '') { // If a filter was provided
if (preg_match('/[\(\)]/', $filter)) {
// If there is one parenthesis inside the criteria, we assume it is an Universal Filter Syntax.
$errormsg = '';
$filter = forgeSQLFromUniversalSearchCriteria($filter, $errormsg, 1);
// Redo clean $filter that may contains sql conditions so sql code
if (function_exists('testSqlAndScriptInject')) {
if (testSqlAndScriptInject($filter, 3) > 0) {
$filter = '';
return 'SQLInjectionTryDetected';
}
}
} else {
// If not, we do nothing. We already know that there is no parenthesis
// TODO Disallow this case in a future.
dol_syslog("Warning, select_thirdparty_list was called with a filter criteria not using the Universal Search Syntax.", LOG_WARNING);
}
}
if (!is_object($hookmanager)) {
include_once DOL_DOCUMENT_ROOT . '/core/class/hookmanager.class.php';
@ -1816,6 +1809,11 @@ class Form
if (getDolGlobalString('CONTACT_HIDE_INACTIVE_IN_COMBOBOX')) {
$sql .= " AND sp.statut <> 0";
}
if ($filter) {
// $filter is safe because, if it contains '(' or ')', it has been sanitized by testSqlAndScriptInject() and forgeSQLFromUniversalSearchCriteria()
// if not, by testSqlAndScriptInject() only.
$sql .= " AND (" . $filter . ")";
}
// Add where from hooks
$parameters = array();
$reshook = $hookmanager->executeHooks('selectContactListWhere', $parameters); // Note that $action and $object may have been modified by hook
@ -1903,13 +1901,16 @@ class Form
$out .= ' disabled';
}
$out .= ' selected>';
$out .= $contactstatic->getFullName($langs) . $extendedInfos;
$tmplabel = $contactstatic->getFullName($langs) . $extendedInfos;
if ($showfunction && $obj->poste) {
$out .= ' (' . $obj->poste . ')';
$tmplabel .= ' (' . $obj->poste . ')';
}
if (($showsoc > 0) && $obj->company) {
$out .= ' - (' . $obj->company . ')';
$tmplabel .= ' - (' . $obj->company . ')';
}
$out .= $tmplabel;
$out .= '</option>';
} else {
$out .= '<option value="' . $obj->rowid . '"';
@ -1917,26 +1918,33 @@ class Form
$out .= ' disabled';
}
$out .= '>';
$out .= $contactstatic->getFullName($langs) . $extendedInfos;
$tmplabel = $contactstatic->getFullName($langs) . $extendedInfos;
if ($showfunction && $obj->poste) {
$out .= ' (' . $obj->poste . ')';
$tmplabel .= ' (' . $obj->poste . ')';
}
if (($showsoc > 0) && $obj->company) {
$out .= ' - (' . $obj->company . ')';
$tmplabel .= ' - (' . $obj->company . ')';
}
$out .= $tmplabel;
$out .= '</option>';
}
} else {
if (in_array($obj->rowid, $selected)) {
$out .= $contactstatic->getFullName($langs) . $extendedInfos;
$tmplabel = $contactstatic->getFullName($langs) . $extendedInfos;
if ($showfunction && $obj->poste) {
$out .= ' (' . $obj->poste . ')';
$tmplabel .= ' (' . $obj->poste . ')';
}
if (($showsoc > 0) && $obj->company) {
$out .= ' - (' . $obj->company . ')';
$tmplabel .= ' - (' . $obj->company . ')';
}
$out .= $tmplabel;
}
}
array_push($outarray, array('key' => $obj->rowid, 'value' => $tmplabel, 'label' => $tmplabel, 'labelhtml' => $tmplabel));
}
$i++;
}
@ -1968,17 +1976,112 @@ class Form
}
$this->num = $num;
return $out;
if ($options_only === 2) {
// Return array of options
return $outarray;
} else {
return $out;
}
} else {
dol_print_error($this->db);
return -1;
}
}
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
/**
* Return the HTML select list of users
* Return HTML combo list of absolute discounts
*
* @param string $selected Id Fixed reduction preselected
* @param string $htmlname Name of the form field
* @param string $filter Optional filter critreria
* @param int $socid Id of thirdparty
* @param int $maxvalue Max value for lines that can be selected
* @return int Return number of qualifed lines in list
*/
public function select_remises($selected, $htmlname, $filter, $socid, $maxvalue = 0)
{
// phpcs:enable
global $langs, $conf;
// On recherche les remises
$sql = "SELECT re.rowid, re.amount_ht, re.amount_tva, re.amount_ttc,";
$sql .= " re.description, re.fk_facture_source";
$sql .= " FROM " . $this->db->prefix() . "societe_remise_except as re";
$sql .= " WHERE re.fk_soc = " . (int) $socid;
$sql .= " AND re.entity = " . $conf->entity;
if ($filter) {
$sql .= " AND " . $filter;
}
$sql .= " ORDER BY re.description ASC";
dol_syslog(get_class($this) . "::select_remises", LOG_DEBUG);
$resql = $this->db->query($sql);
if ($resql) {
print '<select id="select_' . $htmlname . '" class="flat maxwidthonsmartphone" name="' . $htmlname . '">';
$num = $this->db->num_rows($resql);
$qualifiedlines = $num;
$i = 0;
if ($num) {
print '<option value="0">&nbsp;</option>';
while ($i < $num) {
$obj = $this->db->fetch_object($resql);
$desc = dol_trunc($obj->description, 40);
if (preg_match('/\(CREDIT_NOTE\)/', $desc)) {
$desc = preg_replace('/\(CREDIT_NOTE\)/', $langs->trans("CreditNote"), $desc);
}
if (preg_match('/\(DEPOSIT\)/', $desc)) {
$desc = preg_replace('/\(DEPOSIT\)/', $langs->trans("Deposit"), $desc);
}
if (preg_match('/\(EXCESS RECEIVED\)/', $desc)) {
$desc = preg_replace('/\(EXCESS RECEIVED\)/', $langs->trans("ExcessReceived"), $desc);
}
if (preg_match('/\(EXCESS PAID\)/', $desc)) {
$desc = preg_replace('/\(EXCESS PAID\)/', $langs->trans("ExcessPaid"), $desc);
}
$selectstring = '';
if ($selected > 0 && $selected == $obj->rowid) {
$selectstring = ' selected';
}
$disabled = '';
if ($maxvalue > 0 && $obj->amount_ttc > $maxvalue) {
$qualifiedlines--;
$disabled = ' disabled';
}
if (getDolGlobalString('MAIN_SHOW_FACNUMBER_IN_DISCOUNT_LIST') && !empty($obj->fk_facture_source)) {
$tmpfac = new Facture($this->db);
if ($tmpfac->fetch($obj->fk_facture_source) > 0) {
$desc = $desc . ' - ' . $tmpfac->ref;
}
}
print '<option value="' . $obj->rowid . '"' . $selectstring . $disabled . '>' . $desc . ' (' . price($obj->amount_ht) . ' ' . $langs->trans("HT") . ' - ' . price($obj->amount_ttc) . ' ' . $langs->trans("TTC") . ')</option>';
$i++;
}
}
print '</select>';
print ajax_combobox('select_' . $htmlname);
return $qualifiedlines;
} else {
dol_print_error($this->db);
return -1;
}
}
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
/**
* Return the HTML select list of users
*
* @param string $selected Id user preselected
* @param string $htmlname Field name in form
@ -2001,7 +2104,7 @@ class Form
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
/**
* Return select list of users
* Return select list of users
*
* @param string|int|User $selected User id or user object of user preselected. If 0 or < -2, we use id of current user. If -1 or '', keep unselected (if empty is allowed)
* @param string $htmlname Field name in form
@ -2473,7 +2576,8 @@ class Form
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
/**
* Return list of products for customer in Ajax if Ajax activated or go to select_produits_list
* Return list of products for customer.
* Use Ajax if Ajax activated or go to select_produits_list
*
* @param int $selected Preselected products
* @param string $htmlname Name of HTML select field (must be unique in page).
@ -2661,7 +2765,7 @@ class Form
public function select_bom($selected = '', $htmlname = 'bom_id', $limit = 0, $status = 1, $type = 0, $showempty = '1', $morecss = '', $nooutput = '', $forcecombo = 0, $TProducts = [])
{
// phpcs:enable
global $conf, $user, $langs, $db;
global $db;
require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php';
@ -2724,8 +2828,8 @@ class Form
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
/**
* Return list of products for a customer.
* Called by select_produits.
* Return list of products for a customer.
* Called by select_produits.
*
* @param int $selected Preselected product
* @param string $htmlname Name of select html
@ -2745,7 +2849,7 @@ class Form
* 'warehouseopen' = count products from open warehouses,
* 'warehouseclosed' = count products from closed warehouses,
* 'warehouseinternal' = count products from warehouses for internal correct/transfer only
* @param int $status_purchase Purchase status -1=Return all products, 0=Products not on purchase, 1=Products on purchase
* @param int $status_purchase Purchase status -1=Return all products, 0=Products not on purchase, 1=Products on purchase
* @return array|string Array of keys for json
*/
public function select_produits_list($selected = 0, $htmlname = 'productid', $filtertype = '', $limit = 20, $price_level = 0, $filterkey = '', $status = 1, $finished = 2, $outputmode = 0, $socid = 0, $showempty = '1', $forcecombo = 0, $morecss = '', $hidepriceinlabel = 0, $warehouseStatus = '', $status_purchase = -1)
@ -4147,14 +4251,14 @@ class Form
}
/**
* Retourne la liste des types de delais de livraison possibles
* Return the list of type of delay available.
*
* @param string $selected Id du type de delais pre-selectionne
* @param string $htmlname Nom de la zone select
* @param string $filtertype To add a filter
* @param int $addempty Add empty entry
* @param string $morecss More CSS
* @return void
* @param string $selected Id du type de delais pre-selectionne
* @param string $htmlname Nom de la zone select
* @param string $filtertype To add a filter
* @param int $addempty Add empty entry
* @param string $morecss More CSS
* @return void
*/
public function selectAvailabilityDelay($selected = '', $htmlname = 'availid', $filtertype = '', $addempty = 0, $morecss = '')
{
@ -4185,7 +4289,7 @@ class Form
}
/**
* Load into cache cache_demand_reason, array of input reasons
* Load into cache cache_demand_reason, array of input reasons
*
* @return int Nb of lines loaded, <0 if KO
*/
@ -4236,16 +4340,16 @@ class Form
}
/**
* Return list of input reason (events that triggered an object creation, like after sending an emailing, making an advert, ...)
* List found into table c_input_reason loaded by loadCacheInputReason
* Return list of input reason (events that triggered an object creation, like after sending an emailing, making an advert, ...)
* List found into table c_input_reason loaded by loadCacheInputReason
*
* @param string $selected Id or code of type origin to select by default
* @param string $htmlname Nom de la zone select
* @param string $exclude To exclude a code value (Example: SRC_PROP)
* @param int $addempty Add an empty entry
* @param string $morecss Add more css to the HTML select component
* @param int $notooltip Do not show the tooltip for admin
* @return void
* @param string $selected Id or code of type origin to select by default
* @param string $htmlname Nom de la zone select
* @param string $exclude To exclude a code value (Example: SRC_PROP)
* @param int $addempty Add an empty entry
* @param string $morecss Add more css to the HTML select component
* @param int $notooltip Do not show the tooltip for admin
* @return void
*/
public function selectInputReason($selected = '', $htmlname = 'demandreasonid', $exclude = '', $addempty = 0, $morecss = '', $notooltip = 0)
{
@ -6196,16 +6300,16 @@ class Form
/**
* Show forms to select a contact
*
* @param string $page Page
* @param Societe $societe Filter on third party
* @param string $selected Id contact pre-selectionne
* @param string $htmlname Name of HTML select. If 'none', we just show contact link.
* @param string $page Page
* @param Societe $societe Filter on third party
* @param string $selected Id contact pre-selectionne
* @param string $htmlname Name of HTML select. If 'none', we just show contact link.
* @return void
*/
public function form_contacts($page, $societe, $selected = '', $htmlname = 'contactid')
{
// phpcs:enable
global $langs, $conf;
global $langs;
if ($htmlname != "none") {
print '<form method="post" action="' . $page . '">';
@ -6312,7 +6416,7 @@ class Form
*/
public function selectCurrency($selected = '', $htmlname = 'currency_id', $mode = 0, $useempty = '')
{
global $conf, $langs, $user;
global $langs, $user;
$langs->loadCacheCurrencies('');

View file

@ -17,7 +17,7 @@ if (isset($totalarray['pos'])) {
while ($i < $totalarray['nbfield']) {
$i++;
if (!empty($totalarray['pos'][$i])) {
printTotalValCell($totalarray['type'][$i] ?? '', $totalarray['val'][$totalarray['pos'][$i]]);
printTotalValCell($totalarray['type'][$i] ?? '', empty($totalarray['val'][$totalarray['pos'][$i]]) ? 0 : $totalarray['val'][$totalarray['pos'][$i]]);
} else {
if ($i == 1) {
if ((is_null($limit) || $num < $limit) && empty($offset)) {

View file

@ -288,10 +288,10 @@ class Project extends CommonObject
'title' => array('type' => 'varchar(255)', 'label' => 'ProjectLabel', 'enabled' => 1, 'visible' => 1, 'notnull' => 1, 'position' => 17, 'showoncombobox' => 2, 'searchall' => 1),
'entity' => array('type' => 'integer', 'label' => 'Entity', 'default' => '1', 'enabled' => 1, 'visible' => 3, 'notnull' => 1, 'position' => 19),
'fk_soc' => array('type' => 'integer', 'label' => 'Thirdparty', 'enabled' => 1, 'visible' => 0, 'position' => 20),
'dateo' => array('type' => 'date', 'label' => 'DateStart', 'enabled' => 1, 'visible' => 1, 'position' => 30),
'dateo' => array('type' => 'date', 'label' => 'DateStart', 'enabled' => 1, 'visible' => -1, 'position' => 30),
'datee' => array('type' => 'date', 'label' => 'DateEnd', 'enabled' => 1, 'visible' => 1, 'position' => 35),
'description' => array('type' => 'text', 'label' => 'Description', 'enabled' => 1, 'visible' => 3, 'position' => 55, 'searchall' => 1),
'public' => array('type' => 'integer', 'label' => 'Visibility', 'enabled' => 1, 'visible' => 1, 'position' => 65),
'public' => array('type' => 'integer', 'label' => 'Visibility', 'enabled' => 1, 'visible' => -1, 'position' => 65),
'fk_opp_status' => array('type' => 'integer', 'label' => 'OpportunityStatusShort', 'enabled' => 'getDolGlobalString("PROJECT_USE_OPPORTUNITIES")', 'visible' => 1, 'position' => 75),
'opp_percent' => array('type' => 'double(5,2)', 'label' => 'OpportunityProbabilityShort', 'enabled' => 'getDolGlobalString("PROJECT_USE_OPPORTUNITIES")', 'visible' => 1, 'position' => 80),
'note_private' => array('type' => 'html', 'label' => 'NotePrivate', 'enabled' => 1, 'visible' => 0, 'position' => 85, 'searchall' => 1),

View file

@ -260,6 +260,7 @@ if (GETPOST('search_usage_event_organization')) {
$arrayfields['p.usage_organize_event']['visible'] = 1;
$arrayfields['p.usage_organize_event']['checked'] = 1;
}
$arrayfields['p.fk_project']['enabled'] = 0;
$object->fields = dol_sort_array($object->fields, 'position');
$arrayfields = dol_sort_array($arrayfields, 'position');
@ -1111,7 +1112,8 @@ $moreforfilter .= '</div>';
$moreforfilter .= '<div class="divsearchfield">';
$tmptitle = $langs->trans('ProjectsWithThisContact');
$moreforfilter .= img_picto($tmptitle, 'contact', 'class="pictofixedwidth"').$form->selectcontacts(0, $search_project_contact ? $search_project_contact : '', 'search_project_contact', $tmptitle, '', '', 0, 'maxwidth300 widthcentpercentminusx');
$moreforfilter .= img_picto($tmptitle, 'contact', 'class="pictofixedwidth"').$form->select_contact(0, $search_project_contact ? $search_project_contact : '', 'search_project_contact', $tmptitle, '', '', 0, 'maxwidth300 widthcentpercentminusx');
$moreforfilter .= '</div>';
// If the user can view thirdparties other than his'
@ -1799,10 +1801,14 @@ while ($i < $imaxinloop) {
}
// Opp Status
if (!empty($arrayfields['p.fk_opp_status']['checked'])) {
print '<td class="center">';
if ($obj->opp_status_code) {
print $langs->trans("OppStatus".$obj->opp_status_code);
$s = $langs->trans("OppStatus".$obj->opp_status_code);
if (empty($arrayfields['p.opp_percent']['checked']) && $obj->opp_percent) {
$s .= ' ('.dol_escape_htmltag(price2num($obj->opp_percent, 1)).'%)';
}
}
print '<td class="center tdoverflowmax150" title="'.$s.'">';
print $s;
print '</td>';
if (!$i) {
$totalarray['nbfield']++;

View file

@ -121,6 +121,10 @@ if (!empty($action) && $action == 'fetch' && !empty($id)) {
$excludeids = array();
}
// FIXME
// If SOCIETE_USE_SEARCH_TO_SELECT is set, check that nb of chars in $filter is >= to avoid DOS attack
$arrayresult = $form->select_thirdparty_list(0, $htmlname, $filter, 1, $showtype, 0, null, $searchkey, $outjson, 0, 'minwidth100', '', false, $excludeids, $showcode);
if ($outjson) {