2
0
forked from Wavyzz/dolibarr

Qual: Uniformize code

This commit is contained in:
Laurent Destailleur
2013-06-05 22:01:35 +02:00
parent 52407f88c4
commit 7e53f98baa
2 changed files with 264 additions and 75 deletions

View File

@@ -655,6 +655,72 @@ class Form
if ($user->admin) print info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionnarySetup"),1);
}
/**
* Return list of company for customer in Ajax if Ajax activated or go to select_thirparty_list
*
* @param int $selected Preselected products
* @param string $htmlname Name of HTML seletc field (must be unique in page)
* @param int $filtertype Filter on product type (''=nofilter, 0=product, 1=service)
* @param int $limit Limit on number of returned lines
* @param array $ajaxoptions Options for ajax_autocompleter
* @return void
*/
function select_thirdparty($selected='', $htmlname='productid', $filtertype='', $limit=20, $ajaxoptions=array())
{
global $langs,$conf;
if (! empty($conf->use_javascript_ajax) && ! empty($conf->global->COMPANY_USE_SEARCH_TO_SELECT))
{
$placeholder='';
if ($selected && empty($selected_input_value))
{
require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
$societe = new Societe($this->db);
$societe->fetch($selected);
$selected_input_value=$societe->ref;
}
// mode=1 means customers products
$urloption='htmlname='.$htmlname.'&outjson=1&price_level='.$price_level.'&type='.$filtertype.'&mode=1&status='.$status.'&finished='.$finished;
print ajax_autocompleter($selected, $htmlname, DOL_URL_ROOT.'/societe/ajax/company.php', $urloption, $conf->global->COMPANY_USE_SEARCH_TO_SELECT, 0, $ajaxoptions);
if (empty($hidelabel)) print $langs->trans("RefOrLabel").' : ';
else if ($hidelabel > 1) {
if (! empty($conf->global->MAIN_HTML5_PLACEHOLDER)) $placeholder=' placeholder="'.$langs->trans("RefOrLabel").'"';
else $placeholder=' title="'.$langs->trans("RefOrLabel").'"';
if ($hidelabel == 2) {
print img_picto($langs->trans("Search"), 'search');
}
}
print '<input type="text" size="20" name="search_'.$htmlname.'" id="search_'.$htmlname.'" value="'.$selected_input_value.'"'.$placeholder.' />';
if ($hidelabel == 3) {
print img_picto($langs->trans("Search"), 'search');
}
}
else
{
print $this->select_thirdparty_list($selected,$htmlname,$filtertype,1,0,1,array(),'',0,$limit);
}
}
/**
* Output html form to select a third party
*
* @param string $selected Preselected type
* @param string $htmlname Name of field in form
* @param string $filter Optionnal filters criteras (example: 's.rowid <> x')
* @param int $showempty Add an empty field
* @param int $showtype Show third party type in combolist (customer, prospect or supplier)
* @param int $forcecombo Force to use combo box
* @param array $event Event options. Example: array(array('method'=>'getContacts', 'url'=>dol_buildpath('/core/ajax/contacts.php',1), 'htmlname'=>'contactid', 'params'=>array('add-customer-contact'=>'disabled')))
* @return string HTML string with
* @deprecated Use select_thirdparty instead
*/
function select_company($selected='',$htmlname='socid',$filter='',$showempty=0, $showtype=0, $forcecombo=0, $event=array())
{
return $this->select_thirdparty_list($selected='',$htmlname='socid',$filter='',$showempty=0, $showtype=0, $forcecombo=0, $event=array());
}
/**
* Output html form to select a third party
*
@@ -665,13 +731,17 @@ class Form
* @param int $showtype Show third party type in combolist (customer, prospect or supplier)
* @param int $forcecombo Force to use combo box
* @param array $event 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 $outputmode 0=HTML select string, 1=Array
* @param int $limit Limit number of answers
* @return string HTML string with
*/
function select_company($selected='',$htmlname='socid',$filter='',$showempty=0, $showtype=0, $forcecombo=0, $event=array())
function select_thirdparty_list($selected='',$htmlname='socid',$filter='',$showempty=0, $showtype=0, $forcecombo=0, $event=array(), $filterkey='', $outputmode=0, $limit=20)
{
global $conf,$user,$langs;
$out='';
$outarray=array();
// On recherche les societes
$sql = "SELECT s.rowid, s.nom, s.client, s.fournisseur, s.code_client, s.code_fournisseur";
@@ -682,9 +752,33 @@ class Form
if ($filter) $sql.= " AND (".$filter.")";
if (!$user->rights->societe->client->voir && !$user->societe_id) $sql.= " AND s.rowid = sc.fk_soc AND sc.fk_user = " .$user->id;
if (! empty($conf->global->COMPANY_HIDE_INACTIVE_IN_COMBOBOX)) $sql.= " AND s.status<>0 ";
// Add criteria
if ($filterkey && $filterkey != '')
{
$sql.=" AND (";
if (! empty($conf->global->COMPANY_DONOTSEARCH_ANYWHERE)) // Can use index
{
$sql.="(s.name LIKE '".$filterkey."%'";
$sql.=")";
}
else
{
// For natural search
$scrit = explode(' ', $filterkey);
foreach ($scrit as $crit) {
$sql.=" AND (s.name LIKE '%".$crit."%'";
$sql.=")";
}
}
if (! empty($conf->barcode->enabled))
{
$sql .= " OR s.barcode LIKE '".$filterkey."'";
}
$sql.=")";
}
$sql.= " ORDER BY nom ASC";
dol_syslog(get_class($this)."::select_company sql=".$sql);
dol_syslog(get_class($this)."::select_thirdparty_list sql=".$sql);
$resql=$this->db->query($sql);
if ($resql)
{
@@ -692,32 +786,9 @@ class Form
{
//$minLength = (is_numeric($conf->global->COMPANY_USE_SEARCH_TO_SELECT)?$conf->global->COMPANY_USE_SEARCH_TO_SELECT:2);
$out.= ajax_combobox($htmlname, $event, $conf->global->COMPANY_USE_SEARCH_TO_SELECT);
/*
<<<<<<< HEAD
if ($selected && empty($selected_input_value))
{
require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
$product = new Product($this->db);
$product->fetch($selected);
$selected_input_value=$product->ref;
}
=======
if ($selected && empty($selected_input_value))
{
require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
$product = new Product($this->db);
$product->fetch($selected);
$selected_input_value=$product->ref;
}
>>>>>>> refs/remotes/origin/3.3
// mode=1 means customers products
$ajaxoptions=array();
$urloption='htmlname='.$htmlname.'&outjson=1&filter='.urlencode($filter).'&showtype='.$showtype;
$out.=ajax_autocompleter($selected, $htmlname, DOL_URL_ROOT.'/societe/ajax/company.php', $urloption, $conf->global->COMPANY_USE_SEARCH_TO_SELECT, 0, $ajaxoptions);
$out.='<input type="text" size="20" name="search_'.$htmlname.'" id="search_'.$htmlname.'" value="'.$selected_input_value.'" />';
*/
}
// Construct $out and $outarray
$out.= '<select id="'.$htmlname.'" class="flat" name="'.$htmlname.'">';
if ($showempty) $out.= '<option value="-1"></option>';
$num = $this->db->num_rows($resql);
@@ -741,9 +812,12 @@ class Form
$out.= '<option value="'.$obj->rowid.'" selected="selected">'.$label.'</option>';
}
else
{
{
$out.= '<option value="'.$obj->rowid.'">'.$label.'</option>';
}
array_push($outarray, array('key'=>$obj->rowid, 'value'=>$obj->name, 'label'=>$obj->name));
$i++;
}
}
@@ -754,6 +828,7 @@ class Form
dol_print_error($this->db);
}
if ($outputmode) return $outarray;
return $out;
}
@@ -1114,7 +1189,7 @@ class Form
/**
* Return list of products for customer in Ajax if Ajax activated or go to select_produits_do
* Return list of products for customer in Ajax if Ajax activated or go to select_produits_list
*
* @param int $selected Preselected products
* @param string $htmlname Name of HTML seletc field (must be unique in page)
@@ -1162,8 +1237,8 @@ class Form
}
}
else
{
$this->select_produits_do($selected,$htmlname,$filtertype,$limit,$price_level,'',$status,$finished,0);
{
print $this->select_produits_list($selected,$htmlname,$filtertype,$limit,$price_level,'',$status,$finished,0);
}
}
@@ -1178,13 +1253,16 @@ class Form
* @param string $filterkey Filter on product
* @param int $status -1=Return all products, 0=Products not on sell, 1=Products on sell
* @param int $finished Filter on finished field: 2=No filter
* @param int $disableout Disable print output
* @param int $outputmode 0=HTML select string, 1=Array
* @return array Array of keys for json
*/
function select_produits_do($selected='',$htmlname='productid',$filtertype='',$limit=20,$price_level=0,$filterkey='',$status=1,$finished=2,$disableout=0)
function select_produits_list($selected='',$htmlname='productid',$filtertype='',$limit=20,$price_level=0,$filterkey='',$status=1,$finished=2,$outputmode=0)
{
global $langs,$conf,$user,$db;
$out='';
$outarray=array();
$sql = "SELECT ";
$sql.= " p.rowid, p.label, p.ref, p.description, p.fk_product_type, p.price, p.price_ttc, p.price_base_type, p.tva_tx, p.duration, p.stock";
// Multilang : we add translation
@@ -1228,6 +1306,7 @@ class Form
// Add criteria on ref/label
if ($filterkey && $filterkey != '')
{
$sql.=" AND (";
if (! empty($conf->global->PRODUCT_DONOTSEARCH_ANYWHERE)) // Can use index
{
$sql.=" AND (p.ref LIKE '".$filterkey."%' OR p.label LIKE '".$filterkey."%'";
@@ -1249,22 +1328,20 @@ class Form
{
$sql .= " OR p.barcode LIKE '".$filterkey."'";
}
$sql.=")";
}
$sql.= $db->order("p.ref");
$sql.= $db->plimit($limit);
// Build output string
$outselect='';
$outjson=array();
dol_syslog(get_class($this)."::select_produits_do search product sql=".$sql, LOG_DEBUG);
dol_syslog(get_class($this)."::select_produits_list search product sql=".$sql, LOG_DEBUG);
$result=$this->db->query($sql);
if ($result)
{
$num = $this->db->num_rows($result);
$outselect.='<select class="flat" name="'.$htmlname.'" id="'.$htmlname.'">';
$outselect.='<option value="0" selected="selected">&nbsp;</option>';
$out.='<select class="flat" name="'.$htmlname.'" id="'.$htmlname.'">';
$out.='<option value="0" selected="selected">&nbsp;</option>';
$i = 0;
while ($num && $i < $num)
@@ -1273,13 +1350,14 @@ class Form
$optJson = array();
$objp = $this->db->fetch_object($result);
if(!empty($objp->price_by_qty) && $objp->price_by_qty == 1) { // Price by quantity will return many prices for the same product
if (!empty($objp->price_by_qty) && $objp->price_by_qty == 1)
{ // Price by quantity will return many prices for the same product
$sql = "SELECT rowid, quantity, price, unitprice, remise_percent, remise";
$sql.= " FROM ".MAIN_DB_PREFIX."product_price_by_qty";
$sql.= " WHERE fk_product_price=".$objp->price_rowid;
$sql.= " ORDER BY quantity ASC";
dol_syslog(get_class($this)."::select_produits_do search price by qty sql=".$sql);
dol_syslog(get_class($this)."::select_produits_list search price by qty sql=".$sql);
$result2 = $this->db->query($sql);
if ($result2)
{
@@ -1302,46 +1380,48 @@ class Form
// Add new entry
// "key" value of json key array is used by jQuery automatically as selected value
// "label" value of json key array is used by jQuery automatically as text for combo box
$outselect.=$opt;
array_push($outjson, $optJson);
$out.=$opt;
array_push($outarray, $optJson);
}
}
} else {
}
else
{
$this->_construct_product_list_option($objp, $opt, $optJson, $price_level, $selected);
// Add new entry
// "key" value of json key array is used by jQuery automatically as selected value
// "label" value of json key array is used by jQuery automatically as text for combo box
$outselect.=$opt;
array_push($outjson, $optJson);
$out.=$opt;
array_push($outarray, $optJson);
}
$i++;
}
$outselect.='</select>';
$out.='</select>';
$this->db->free($result);
if (empty($disableout)) print $outselect;
return $outjson;
if (empty($outputmode)) return $out;
return $outarray;
}
else
{
{
dol_print_error($db);
}
}
/**
* _construct_product_list_option
*
*
* @param resultset &$objp Resultset of fetch
* @param string $opt Option
* @param string $optJson Option
* @param int $price_level Price level
* @param string $selected Preselected value
* @return
* @return
*/
private function _construct_product_list_option(&$objp, &$opt, &$optJson, $price_level, $selected)
private function _construct_product_list_option(&$objp, &$opt, &$optJson, $price_level, $selected)
{
global $langs,$conf,$user,$db;
@@ -1399,7 +1479,7 @@ class Form
$sql.= " ORDER BY date_price";
$sql.= " DESC LIMIT 1";
dol_syslog(get_class($this)."::select_produits_do search price for level '.$price_level.' sql=".$sql);
dol_syslog(get_class($this)."::_construct_product_list_option search price for level '.$price_level.' sql=".$sql);
$result2 = $this->db->query($sql);
if ($result2)
{
@@ -1430,7 +1510,8 @@ class Form
}
// Price by quantity
if (!empty($objp->quantity) && $objp->quantity >= 1) {
if (!empty($objp->quantity) && $objp->quantity >= 1)
{
$found = 1;
$outqty=$objp->quantity;
$outdiscount=$objp->remise_percent;
@@ -1511,7 +1592,7 @@ class Form
}
/**
* Return list of products for customer (in Ajax if Ajax activated or go to select_produits_fournisseurs_do)
* Return list of products for customer (in Ajax if Ajax activated or go to select_produits_fournisseurs_list)
*
* @param int $socid Id third party
* @param string $selected Preselected product
@@ -1536,7 +1617,7 @@ class Form
}
else
{
$this->select_produits_fournisseurs_do($socid,$selected,$htmlname,$filtertype,$filtre,'',-1,0);
print $this->select_produits_fournisseurs_list($socid,$selected,$htmlname,$filtertype,$filtre,'',-1,0);
}
}
@@ -1550,13 +1631,16 @@ class Form
* @param string $filtre Pour filtre sql
* @param string $filterkey Filtre des produits
* @param int $statut -1=Return all products, 0=Products not on sell, 1=Products on sell
* @param int $disableout Disable print output
* @param int $outputmode 0=HTML select string, 1=Array
* @return array Array of keys for json
*/
function select_produits_fournisseurs_do($socid,$selected='',$htmlname='productid',$filtertype='',$filtre='',$filterkey='',$statut=-1,$disableout=0)
function select_produits_fournisseurs_list($socid,$selected='',$htmlname='productid',$filtertype='',$filtre='',$filterkey='',$statut=-1,$outputmode=0)
{
global $langs,$conf;
$out='';
$outarray=array();
$langs->load('stocks');
$sql = "SELECT p.rowid, p.label, p.ref, p.price, p.duration,";
@@ -1590,20 +1674,18 @@ class Form
$sql.= " ORDER BY pfp.ref_fourn DESC, pfp.quantity ASC";
// Build output string
$outselect='';
$outjson=array();
dol_syslog(get_class($this)."::select_produits_fournisseurs_do sql=".$sql,LOG_DEBUG);
dol_syslog(get_class($this)."::select_produits_fournisseurs_list sql=".$sql,LOG_DEBUG);
$result=$this->db->query($sql);
if ($result)
{
$num = $this->db->num_rows($result);
//$outselect.='<select class="flat" id="select'.$htmlname.'" name="'.$htmlname.'">'; // remove select to have id same with combo and ajax
$outselect.='<select class="flat" id="'.$htmlname.'" name="'.$htmlname.'">';
if (! $selected) $outselect.='<option value="0" selected="selected">&nbsp;</option>';
else $outselect.='<option value="0">&nbsp;</option>';
//$out.='<select class="flat" id="select'.$htmlname.'" name="'.$htmlname.'">'; // remove select to have id same with combo and ajax
$out.='<select class="flat" id="'.$htmlname.'" name="'.$htmlname.'">';
if (! $selected) $out.='<option value="0" selected="selected">&nbsp;</option>';
else $out.='<option value="0">&nbsp;</option>';
$i = 0;
while ($i < $num)
@@ -1694,9 +1776,9 @@ class Form
// Add new entry
// "key" value of json key array is used by jQuery automatically as selected value
// "label" value of json key array is used by jQuery automatically as text for combo box
$outselect.=$opt;
array_push($outjson, array('key'=>$outkey, 'value'=>$outref, 'label'=>$outval, 'qty'=>$outqty, 'discount'=>$outdiscount, 'disabled'=>(empty($objp->idprodfournprice)?true:false)));
// Exemple of var_dump $outjson
$out.=$opt;
array_push($outarray, array('key'=>$outkey, 'value'=>$outref, 'label'=>$outval, 'qty'=>$outqty, 'discount'=>$outdiscount, 'disabled'=>(empty($objp->idprodfournprice)?true:false)));
// Exemple of var_dump $outarray
// array(1) {[0]=>array(6) {[key"]=>string(1) "2" ["value"]=>string(3) "ppp"
// ["label"]=>string(76) "ppp (<strong>f</strong>ff2) - ppp - 20,00 Euros/1unité (20,00 Euros/unité)"
// ["qty"]=>string(1) "1" ["discount"]=>string(1) "0" ["disabled"]=>bool(false)
@@ -1707,12 +1789,12 @@ class Form
$i++;
}
$outselect.='</select>';
$out.='</select>';
$this->db->free($result);
if (empty($disableout)) print $outselect;
return $outjson;
if (empty($outputmode)) return $out;
return $outarray;
}
else
{
@@ -3524,7 +3606,7 @@ class Form
function select_duration($prefix,$iSecond='',$disabled=0,$typehour='select')
{
global $langs;
if ($iSecond)
{
require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
@@ -3532,8 +3614,8 @@ class Form
$hourSelected = convertSecondToTime($iSecond,'hour');
$minSelected = convertSecondToTime($iSecond,'min');
}
if ($typehour=='select')
if ($typehour=='select')
{
print '<select class="flat" name="'.$prefix.'hour"'.($disabled?' disabled="disabled"':'').'>';
for ($hour = 0; $hour < 24; $hour++)
@@ -3547,7 +3629,7 @@ class Form
}
print "</select>";
}
elseif ($typehour=='text')
elseif ($typehour=='text')
{
$fullhours=convertSecondToTime($iSecond,'fullhour');
print '<input type="text" size="3" name="'.$prefix.'hour" class="flat" value="'.$fullhours.'">';

View File

@@ -0,0 +1,107 @@
<?php
/* Copyright (C) 2006 Andre Cianfarani <acianfa@free.fr>
* Copyright (C) 2005-2012 Regis Houssin <regis.houssin@capnetworks.com>
* Copyright (C) 2007-2011 Laurent Destailleur <eldy@users.sourceforge.net>
*
* 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 <http://www.gnu.org/licenses/>.
*/
/**
* \file htdocs/product/ajax/products.php
* \brief File to return Ajax response on product list request
*/
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');
if (! defined('NOCSRFCHECK')) define('NOCSRFCHECK','1');
if (empty($_GET['keysearch']) && ! defined('NOREQUIREHTML')) define('NOREQUIREHTML','1');
require '../../main.inc.php';
$htmlname=GETPOST('htmlname','alpha');
$socid=GETPOST('socid','int');
$type=GETPOST('type','int');
$mode=GETPOST('mode','int');
$status=((GETPOST('status','int') >= 0) ? GETPOST('status','int') : -1);
$outjson=(GETPOST('outjson','int') ? GETPOST('outjson','int') : 0);
$price_level=GETPOST('price_level','int');
$action=GETPOST('action', 'alpha');
$id=GETPOST('id', 'int');
$price_by_qty_rowid=GETPOST('pbq', 'int');
/*
* View
*/
//print '<!-- Ajax page called with url '.$_SERVER["PHP_SELF"].'?'.$_SERVER["QUERY_STRING"].' -->'."\n";
dol_syslog(join(',',$_GET));
//print_r($_GET);
if (! empty($action) && $action == 'fetch' && ! empty($id))
{
require DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php';
$outjson=array();
$object = new Societe($db);
$ret=$object->fetch($id);
if ($ret > 0)
{
$outname=$object->name;
$outjson = array('name'=>$outname);
}
echo json_encode($outjson);
}
else
{
require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
$langs->load("products");
$langs->load("main");
top_httphead();
if (empty($htmlname)) return;
$match = preg_grep('/('.$htmlname.'[0-9]+)/',array_keys($_GET));
sort($match);
$id = (! empty($match[0]) ? $match[0] : '');
if (! GETPOST($htmlname) && ! GETPOST($id)) return;
// When used from jQuery, the search term is added as GET param "term".
$searchkey=(GETPOST($id)?GETPOST($id):(GETPOST($htmlname)?GETPOST($htmlname):''));
$form = new Form($db);
if (empty($mode) || $mode == 'customer')
{
$arrayresult=$form->select_company_html($socid,$htmlname,"client IN (1,3)",0,0,0,null,$searchkey,$outjson);
}
elseif ($mode == 'supplier')
{
$arrayresult=$form->select_company_html($socid,$htmlname,"fournisseur=1",0,0,0,null,$searchkey,$outjson);
}
$db->close();
if ($outjson) print json_encode($arrayresult);
}
?>