2004-10-19 20:58:50 +02:00
< ? php
2016-02-11 20:01:51 +01:00
/* Copyright ( C ) 2002 - 2003 Rodolphe Quiedeville < rodolphe @ quiedeville . org >
* Copyright ( C ) 2002 - 2003 Jean - Louis Bergamo < jlb @ j1b . org >
* Copyright ( C ) 2004 Sebastien Di Cintio < sdicintio @ ressource - toi . org >
* Copyright ( C ) 2004 Benoit Mortier < benoit . mortier @ opensides . be >
* Copyright ( C ) 2009 - 2012 Laurent Destailleur < eldy @ users . sourceforge . net >
2018-10-27 14:43:12 +02:00
* Copyright ( C ) 2009 - 2012 Regis Houssin < regis . houssin @ inodbox . com >
2016-02-11 20:01:51 +01:00
* Copyright ( C ) 2013 Florian Henry < forian . henry @ open - concept . pro >
* Copyright ( C ) 2015 Charles - Fr BENKE < charles . fr @ benke . fr >
* Copyright ( C ) 2016 Raphaël Doursenaud < rdoursenaud @ gpcsolutions . fr >
2017-09-25 16:14:14 +02:00
* Copyright ( C ) 2017 Nicolas ZABOURI < info @ inovea - conseil . com >
2019-02-24 09:17:45 +01:00
* Copyright ( C ) 2018 - 2019 Frédéric France < frederic . france @ netlogic . fr >
2014-03-05 09:57:36 +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 />.
2014-03-05 09:57:36 +01:00
*/
2003-02-13 19:14:57 +01:00
2008-11-09 13:21:20 +01:00
/**
2011-06-19 16:27:06 +02:00
* \file htdocs / core / class / extrafields . class . php
2013-07-27 14:21:47 +02:00
* \ingroup core
* \brief File of class to manage extra fields
*/
2004-07-28 00:46:33 +02:00
2014-05-01 16:26:57 +02:00
2008-11-09 13:21:20 +01:00
/**
2012-02-01 20:44:06 +01:00
* Class to manage standard extra fields
2013-07-27 14:21:47 +02:00
*/
2011-06-19 16:27:06 +02:00
class ExtraFields
2003-02-13 19:14:57 +01:00
{
2018-08-22 11:06:34 +02:00
/**
* @ var DoliDB Database handler .
*/
public $db ;
2017-05-24 22:48:44 +02:00
2019-02-24 09:17:45 +01:00
/**
2020-05-21 13:06:36 +02:00
* @ var array Array with type of element ( for what object is the extrafield )
2019-02-24 09:17:45 +01:00
* @ deprecated
*/
public $attribute_elementtype ;
/**
* @ var array Array with type of the extra field
* @ deprecated
*/
public $attribute_type ;
/**
* @ var array Array with label of extra field
* @ deprecated
*/
public $attribute_label ;
/**
* @ var array Array with size of extra field
* @ deprecated
*/
public $attribute_size ;
/**
* @ var array Array with list of possible values for some types of extra fields
* @ deprecated
*/
public $attribute_choice ;
/**
* @ var array Array to store compute formula for computed fields
* @ deprecated
*/
public $attribute_computed ;
/**
* @ var array Array to store default value
* @ deprecated
*/
public $attribute_default ;
/**
* @ var array Array to store if attribute is unique or not
* @ deprecated
*/
public $attribute_unique ;
/**
* @ var array Array to store if attribute is required or not
* @ deprecated
*/
public $attribute_required ;
/**
* @ var array Array to store parameters of attribute ( used in select type )
* @ deprecated
*/
public $attribute_param ;
/**
* @ var array Array to store position of attribute
* @ deprecated
*/
public $attribute_pos ;
/**
* @ var array Array to store if attribute is editable regardless of the document status
* @ deprecated
*/
public $attribute_alwayseditable ;
/**
* @ var array Array to store permission to check
* @ deprecated
*/
public $attribute_perms ;
/**
* @ var array Array to store language file to translate label of values
* @ deprecated
*/
public $attribute_langfile ;
/**
* @ var array Array to store if field is visible by default on list
* @ deprecated
*/
public $attribute_list ;
/**
* @ var array New array to store extrafields definition
*/
public $attributes ;
2017-06-07 16:44:04 +02:00
2018-08-22 10:37:16 +02:00
/**
* @ var string Error code ( or message )
*/
2019-11-13 19:35:39 +01:00
public $error = '' ;
2018-09-09 10:40:00 +02:00
2019-02-24 09:17:45 +01:00
/**
* @ var string [] Array of Error code ( or message )
*/
public $errors = array ();
/**
2019-02-25 21:39:45 +01:00
* @ var string DB Error number
2019-02-24 09:17:45 +01:00
*/
public $errno ;
2011-06-19 16:27:06 +02:00
2016-04-20 17:30:40 +02:00
2019-11-13 19:35:39 +01:00
public static $type2label = array (
2020-07-30 14:57:33 +02:00
'varchar' => 'String1Line' ,
'text' => 'TextLongNLines' ,
2019-02-24 09:17:45 +01:00
'html' => 'HtmlText' ,
'int' => 'Int' ,
'double' => 'Float' ,
'date' => 'Date' ,
'datetime' => 'DateAndTime' ,
'boolean' => 'Boolean' ,
'price' => 'ExtrafieldPrice' ,
'phone' => 'ExtrafieldPhone' ,
'mail' => 'ExtrafieldMail' ,
'url' => 'ExtrafieldUrl' ,
'password' => 'ExtrafieldPassword' ,
'select' => 'ExtrafieldSelect' ,
'sellist' => 'ExtrafieldSelectList' ,
'radio' => 'ExtrafieldRadio' ,
'checkbox' => 'ExtrafieldCheckBox' ,
'chkbxlst' => 'ExtrafieldCheckBoxFromList' ,
'link' => 'ExtrafieldLink' ,
'separate' => 'ExtrafieldSeparator' ,
2012-09-13 10:14:25 +02:00
);
2008-06-01 00:08:59 +02:00
2017-06-07 11:38:42 +02:00
2009-11-07 11:34:25 +01:00
/**
2011-09-11 20:35:38 +02:00
* Constructor
*
2012-02-01 20:44:06 +01:00
* @ param DoliDB $db Database handler
2013-04-21 17:36:23 +02:00
*/
2019-02-27 20:45:07 +01:00
public function __construct ( $db )
2008-06-01 00:08:59 +02:00
{
2012-02-01 20:44:06 +01:00
$this -> db = $db ;
2019-02-24 09:17:45 +01:00
$this -> error = '' ;
$this -> errors = array ();
2018-01-27 19:29:57 +01:00
$this -> attributes = array ();
// For old usage
2017-05-24 22:48:44 +02:00
$this -> attribute_elementtype = array ();
2009-11-07 14:55:15 +01:00
$this -> attribute_type = array ();
2008-06-01 00:08:59 +02:00
$this -> attribute_label = array ();
2009-11-07 14:55:15 +01:00
$this -> attribute_size = array ();
2017-05-24 22:48:44 +02:00
$this -> attribute_computed = array ();
$this -> attribute_default = array ();
2012-09-22 20:27:06 +02:00
$this -> attribute_unique = array ();
2012-10-25 13:11:55 +02:00
$this -> attribute_required = array ();
2015-03-10 18:33:14 +01:00
$this -> attribute_perms = array ();
2017-08-27 12:06:46 +02:00
$this -> attribute_langfile = array ();
2015-03-10 22:27:16 +01:00
$this -> attribute_list = array ();
2008-06-01 00:08:59 +02:00
}
2013-04-21 17:36:23 +02:00
/**
* Add a new extra field parameter
*
2017-08-27 12:06:46 +02:00
* @ param string $attrname Code of attribute
* @ param string $label label of attribute
2020-01-30 18:47:41 +01:00
* @ param string $type Type of attribute ( 'boolean' , 'int' , 'varchar' , 'text' , 'html' , 'date' , 'datehour' , 'price' , 'phone' , 'mail' , 'password' , 'url' , 'select' , 'checkbox' , 'separate' , ... )
2017-08-27 12:06:46 +02:00
* @ param int $pos Position of attribute
2019-01-25 12:03:07 +01:00
* @ param string $size Size / length definition of attribute ( '5' , '24,8' , ... ) . For float , it contains 2 numeric separated with a comma .
2018-04-12 23:16:23 +02:00
* @ param string $elementtype Element type . Same value than object -> table_element ( Example 'member' , 'product' , 'thirdparty' , ... )
2017-08-27 12:06:46 +02:00
* @ param int $unique Is field unique or not
* @ param int $required Is field required or not
* @ param string $default_value Defaulted value ( In database . use the default_value feature for default value on screen . Example : '' , '0' , 'null' , 'avalue' )
* @ param array | string $param Params for field ( ex for select list : array ( 'options' => array ( value '=>' label of option ' )) )
* @ param int $alwayseditable Is attribute always editable regardless of the document status
* @ param string $perms Permission to check
2018-04-10 15:39:11 +02:00
* @ param string $list Visibilty ( '0' = never visible , '1' = visible on list + forms , '2' = list only , '3' = form only or 'eval string' )
2018-06-28 19:13:23 +02:00
* @ param string $help Text with help tooltip
2017-08-27 12:06:46 +02:00
* @ param string $computed Computed value
2018-01-13 13:16:33 +01:00
* @ param string $entity Entity of extrafields ( for multicompany modules )
2017-08-27 12:06:46 +02:00
* @ param string $langfile Language file
2018-01-13 13:16:33 +01:00
* @ param string $enabled Condition to have the field enabled or not
2019-11-05 12:47:38 +01:00
* @ param int $totalizable Is a measure . Must show a total on lists
2020-02-23 23:03:17 +01:00
* @ param int $printable Is extrafield displayed on PDF
2017-12-09 23:19:29 +01:00
* @ return int <= 0 if KO , > 0 if OK
2013-04-21 17:36:23 +02:00
*/
2020-02-23 23:03:17 +01:00
public function addExtraField ( $attrname , $label , $type , $pos , $size , $elementtype , $unique = 0 , $required = 0 , $default_value = '' , $param = '' , $alwayseditable = 0 , $perms = '' , $list = '-1' , $help = '' , $computed = '' , $entity = '' , $langfile = '' , $enabled = '1' , $totalizable = 0 , $printable = 0 )
2011-06-19 20:09:41 +02:00
{
2013-04-21 17:36:23 +02:00
if ( empty ( $attrname )) return - 1 ;
if ( empty ( $label )) return - 1 ;
2020-01-30 18:47:41 +01:00
if ( $type == 'separate' ) { $unique = 0 ; $required = 0 ; } // Force unique and not required if this is a separator field to avoid troubles.
2020-01-30 01:48:28 +01:00
if ( $elementtype == 'thirdparty' ) $elementtype = 'societe' ;
if ( $elementtype == 'contact' ) $elementtype = 'socpeople' ;
2015-02-07 17:41:20 +01:00
2013-04-21 17:36:23 +02:00
// Create field into database except for separator type which is not stored in database
if ( $type != 'separate' )
{
2020-01-30 01:48:28 +01:00
$result = $this -> create ( $attrname , $type , $size , $elementtype , $unique , $required , $default_value , $param , $perms , $list , $computed , $help );
2013-04-21 17:36:23 +02:00
}
2020-01-30 01:48:28 +01:00
$err1 = $this -> errno ;
2013-04-21 17:36:23 +02:00
if ( $result > 0 || $err1 == 'DB_ERROR_COLUMN_ALREADY_EXISTS' || $type == 'separate' )
{
// Add declaration of field into table
2020-02-23 23:03:17 +01:00
$result2 = $this -> create_label ( $attrname , $label , $type , $pos , $size , $elementtype , $unique , $required , $param , $alwayseditable , $perms , $list , $help , $default_value , $computed , $entity , $langfile , $enabled , $totalizable , $printable );
2020-01-30 01:48:28 +01:00
$err2 = $this -> errno ;
2013-04-21 17:36:23 +02:00
if ( $result2 > 0 || ( $err1 == 'DB_ERROR_COLUMN_ALREADY_EXISTS' && $err2 == 'DB_ERROR_RECORD_ALREADY_EXISTS' ))
{
2020-01-30 01:48:28 +01:00
$this -> error = '' ;
$this -> errno = 0 ;
2013-04-21 17:36:23 +02:00
return 1 ;
}
else return - 2 ;
}
else
{
return - 1 ;
}
2011-06-19 20:09:41 +02:00
}
2009-11-07 11:34:25 +01:00
/**
2014-03-05 09:57:36 +01:00
* Add a new optional attribute .
2012-10-24 21:28:24 +02:00
* This is a private method . For public method , use addExtraField .
2012-02-01 20:44:06 +01:00
*
2012-02-20 10:09:28 +01:00
* @ param string $attrname code of attribute
2018-02-27 17:30:13 +01:00
* @ param int $type Type of attribute ( 'boolean' , 'int' , 'varchar' , 'text' , 'html' , 'date' , 'datehour' , 'price' , 'phone' , 'mail' , 'password' , 'url' , 'select' , 'checkbox' , ... )
2015-04-30 00:07:30 +02:00
* @ param string $length Size / length of attribute ( '5' , '24,8' , ... )
2015-02-07 17:41:20 +01:00
* @ param string $elementtype Element type ( 'member' , 'product' , 'thirdparty' , 'contact' , ... )
2013-04-21 17:36:23 +02:00
* @ param int $unique Is field unique or not
* @ param int $required Is field required or not
2017-05-24 22:48:44 +02:00
* @ param string $default_value Default value for field ( in database )
2013-04-21 17:36:23 +02:00
* @ param array $param Params for field ( ex for select list : array ( 'options' => array ( 'value' => 'label of option' ))
2015-03-10 22:27:16 +01:00
* @ param string $perms Permission
2018-04-10 15:39:11 +02:00
* @ param string $list Into list view by default
2017-05-24 22:48:44 +02:00
* @ param string $computed Computed value
2019-09-06 10:36:49 +02:00
* @ param string $help Help on tooltip
2013-04-21 17:36:23 +02:00
* @ return int <= 0 if KO , > 0 if OK
2009-11-07 11:34:25 +01:00
*/
2019-09-06 10:36:49 +02:00
private function create ( $attrname , $type = 'varchar' , $length = 255 , $elementtype = 'member' , $unique = 0 , $required = 0 , $default_value = '' , $param = '' , $perms = '' , $list = '0' , $computed = '' , $help = '' )
2011-06-19 16:27:06 +02:00
{
2019-11-13 19:35:39 +01:00
if ( $elementtype == 'thirdparty' ) $elementtype = 'societe' ;
if ( $elementtype == 'contact' ) $elementtype = 'socpeople' ;
$table = $elementtype . '_extrafields' ;
if ( $elementtype == 'categorie' ) $table = 'categories_extrafields' ;
if ( ! empty ( $attrname ) && preg_match ( " /^ \ w[a-zA-Z0-9_]* $ / " , $attrname ) && ! is_numeric ( $attrname ))
{
if ( $type == 'boolean' ) {
$typedb = 'int' ;
$lengthdb = '1' ;
} elseif ( $type == 'price' ) {
$typedb = 'double' ;
$lengthdb = '24,8' ;
} elseif ( $type == 'phone' ) {
$typedb = 'varchar' ;
$lengthdb = '20' ;
} elseif ( $type == 'mail' ) {
$typedb = 'varchar' ;
$lengthdb = '128' ;
} elseif ( $type == 'url' ) {
$typedb = 'varchar' ;
$lengthdb = '255' ;
} elseif (( $type == 'select' ) || ( $type == 'sellist' ) || ( $type == 'radio' ) || ( $type == 'checkbox' ) || ( $type == 'chkbxlst' )) {
$typedb = 'varchar' ;
$lengthdb = '255' ;
} elseif ( $type == 'link' ) {
$typedb = 'int' ;
$lengthdb = '11' ;
} elseif ( $type == 'html' ) {
$typedb = 'text' ;
$lengthdb = $length ;
} elseif ( $type == 'password' ) {
$typedb = 'varchar' ;
$lengthdb = '128' ;
2013-01-12 14:02:39 +01:00
} else {
2019-11-13 19:35:39 +01:00
$typedb = $type ;
$lengthdb = $length ;
if ( $type == 'varchar' && empty ( $lengthdb )) $lengthdb = '255' ;
2013-01-12 14:02:39 +01:00
}
2013-02-14 23:34:44 +01:00
$field_desc = array (
2018-07-09 13:02:01 +02:00
'type' => $typedb ,
'value' => $lengthdb ,
2019-11-13 19:35:39 +01:00
'null' => ( $required ? 'NOT NULL' : 'NULL' ),
2018-07-09 13:02:01 +02:00
'default' => $default_value
2013-02-14 23:34:44 +01:00
);
2013-04-21 17:36:23 +02:00
2019-11-13 19:35:39 +01:00
$result = $this -> db -> DDLAddField ( MAIN_DB_PREFIX . $table , $attrname , $field_desc );
2011-03-08 11:48:53 +01:00
if ( $result > 0 )
2008-06-01 00:08:59 +02:00
{
2012-09-22 20:27:06 +02:00
if ( $unique )
{
2019-11-13 19:35:39 +01:00
$sql = " ALTER TABLE " . MAIN_DB_PREFIX . $table . " ADD UNIQUE INDEX uk_ " . $table . " _ " . $attrname . " ( " . $attrname . " ) " ;
$resql = $this -> db -> query ( $sql , 1 , 'dml' );
2012-09-22 20:27:06 +02:00
}
2008-06-01 00:08:59 +02:00
return 1 ;
}
else
{
2019-11-13 19:35:39 +01:00
$this -> error = $this -> db -> lasterror ();
$this -> errno = $this -> db -> lasterrno ();
2011-06-12 17:47:10 +02:00
return - 1 ;
2008-06-01 00:08:59 +02:00
}
2011-03-08 11:48:53 +01:00
}
else
{
2008-06-01 00:08:59 +02:00
return 0 ;
}
2004-07-28 00:46:33 +02:00
}
2003-02-19 18:47:22 +01:00
2018-09-03 20:38:52 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2008-06-01 00:08:59 +02:00
/**
2014-03-05 09:57:36 +01:00
* Add description of a new optional attribute
2012-02-01 20:44:06 +01:00
*
2014-04-23 12:07:22 +02:00
* @ param string $attrname code of attribute
* @ param string $label label of attribute
2018-02-27 17:30:13 +01:00
* @ param int $type Type of attribute ( 'int' , 'varchar' , 'text' , 'html' , 'date' , 'datehour' , 'float' )
2014-04-23 12:07:22 +02:00
* @ param int $pos Position of attribute
2015-04-30 00:07:30 +02:00
* @ param string $size Size / length of attribute ( '5' , '24,8' , ... )
2015-02-07 17:41:20 +01:00
* @ param string $elementtype Element type ( 'member' , 'product' , 'thirdparty' , ... )
2014-04-23 12:07:22 +02:00
* @ param int $unique Is field unique or not
* @ param int $required Is field required or not
2017-08-27 12:06:46 +02:00
* @ param array | string $param Params for field ( ex for select list : array ( 'options' => array ( value '=>' label of option ' )) )
2014-10-16 13:28:39 +02:00
* @ param int $alwayseditable Is attribute always editable regardless of the document status
2015-03-10 18:33:14 +01:00
* @ param string $perms Permission to check
2018-04-10 15:39:11 +02:00
* @ param string $list Visibily
2018-06-28 19:13:23 +02:00
* @ param string $help Help on tooltip
2017-05-24 22:48:44 +02:00
* @ param string $default Default value ( in database . use the default_value feature for default value on screen ) .
* @ param string $computed Computed value
2017-08-04 09:00:11 +02:00
* @ param string $entity Entity of extrafields
2017-08-27 12:06:46 +02:00
* @ param string $langfile Language file
2018-01-13 13:16:33 +01:00
* @ param string $enabled Condition to have the field enabled or not
2019-11-05 12:47:38 +01:00
* @ param int $totalizable Is a measure . Must show a total on lists
2020-02-23 23:03:17 +01:00
* @ param int $printable Is extrafield displayed on PDF
2014-04-23 12:07:22 +02:00
* @ return int <= 0 if KO , > 0 if OK
2020-02-23 11:32:38 +01:00
* @ throws Exception
2009-01-27 00:58:37 +01:00
*/
2020-02-23 23:03:17 +01:00
private function create_label ( $attrname , $label = '' , $type = '' , $pos = 0 , $size = 0 , $elementtype = 'member' , $unique = 0 , $required = 0 , $param = '' , $alwayseditable = 0 , $perms = '' , $list = '-1' , $help = '' , $default = '' , $computed = '' , $entity = '' , $langfile = '' , $enabled = '1' , $totalizable = 0 , $printable = 0 )
2003-02-19 18:47:22 +01:00
{
2018-09-03 20:38:52 +02:00
// phpcs:enable
2019-11-13 19:35:39 +01:00
global $conf , $user ;
2009-11-07 14:55:15 +01:00
2019-11-13 19:35:39 +01:00
if ( $elementtype == 'thirdparty' ) $elementtype = 'societe' ;
if ( $elementtype == 'contact' ) $elementtype = 'socpeople' ;
2015-02-07 17:41:20 +01:00
2009-01-27 00:58:37 +01:00
// Clean parameters
2019-11-13 19:35:39 +01:00
if ( empty ( $pos )) $pos = 0 ;
if ( empty ( $list )) $list = '0' ;
if ( empty ( $required )) $required = 0 ;
if ( empty ( $unique )) $unique = 0 ;
2020-03-24 16:09:18 +01:00
if ( empty ( $printable )) $printable = 0 ;
2019-11-13 19:35:39 +01:00
if ( empty ( $alwayseditable )) $alwayseditable = 0 ;
2020-02-23 23:29:13 +01:00
if ( empty ( $totalizable )) $totalizable = 0 ;
2009-01-27 00:58:37 +01:00
2019-11-13 19:35:39 +01:00
if ( ! empty ( $attrname ) && preg_match ( " /^ \ w[a-zA-Z0-9-_]* $ / " , $attrname ) && ! is_numeric ( $attrname ))
2008-06-01 00:08:59 +02:00
{
2018-01-31 10:51:40 +01:00
if ( is_array ( $param ) && count ( $param ) > 0 )
2013-02-14 23:34:44 +01:00
{
2018-01-27 19:35:30 +01:00
$params = serialize ( $param );
2013-02-14 23:34:44 +01:00
}
elseif ( strlen ( $param ) > 0 )
{
$params = trim ( $param );
}
else
{
2020-01-30 01:48:28 +01:00
$params = '' ;
2013-02-14 23:34:44 +01:00
}
2013-04-21 17:36:23 +02:00
2017-07-25 11:57:36 +02:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " extrafields( " ;
2020-01-30 01:48:28 +01:00
$sql .= " name, " ;
$sql .= " label, " ;
$sql .= " type, " ;
$sql .= " pos, " ;
$sql .= " size, " ;
$sql .= " entity, " ;
$sql .= " elementtype, " ;
$sql .= " fieldunique, " ;
$sql .= " fieldrequired, " ;
$sql .= " param, " ;
$sql .= " alwayseditable, " ;
$sql .= " perms, " ;
$sql .= " langs, " ;
$sql .= " list, " ;
2020-02-23 23:03:17 +01:00
$sql .= " printable, " ;
2020-01-30 01:48:28 +01:00
$sql .= " fielddefault, " ;
$sql .= " fieldcomputed, " ;
$sql .= " fk_user_author, " ;
$sql .= " fk_user_modif, " ;
$sql .= " datec, " ;
$sql .= " enabled, " ;
$sql .= " help, " ;
$sql .= " totalizable " ;
$sql .= " ) " ;
$sql .= " VALUES(' " . $attrname . " ', " ;
$sql .= " ' " . $this -> db -> escape ( $label ) . " ', " ;
$sql .= " ' " . $this -> db -> escape ( $type ) . " ', " ;
$sql .= " " . $pos . " , " ;
$sql .= " ' " . $this -> db -> escape ( $size ) . " ', " ;
$sql .= " " . ( $entity === '' ? $conf -> entity : $entity ) . " , " ;
$sql .= " ' " . $this -> db -> escape ( $elementtype ) . " ', " ;
$sql .= " " . $unique . " , " ;
$sql .= " " . $required . " , " ;
$sql .= " ' " . $this -> db -> escape ( $params ) . " ', " ;
$sql .= " " . $alwayseditable . " , " ;
$sql .= " " . ( $perms ? " ' " . $this -> db -> escape ( $perms ) . " ' " : " null " ) . " , " ;
$sql .= " " . ( $langfile ? " ' " . $this -> db -> escape ( $langfile ) . " ' " : " null " ) . " , " ;
$sql .= " ' " . $this -> db -> escape ( $list ) . " ', " ;
2020-02-23 23:03:17 +01:00
$sql .= " ' " . $this -> db -> escape ( $printable ) . " ', " ;
2020-01-30 01:48:28 +01:00
$sql .= " " . ( $default ? " ' " . $this -> db -> escape ( $default ) . " ' " : " null " ) . " , " ;
$sql .= " " . ( $computed ? " ' " . $this -> db -> escape ( $computed ) . " ' " : " null " ) . " , " ;
$sql .= " " . ( is_object ( $user ) ? $user -> id : 0 ) . " , " ;
$sql .= " " . ( is_object ( $user ) ? $user -> id : 0 ) . " , " ;
$sql .= " ' " . $this -> db -> idate ( dol_now ()) . " ', " ;
$sql .= " " . ( $enabled ? " ' " . $this -> db -> escape ( $enabled ) . " ' " : " 1 " ) . " , " ;
$sql .= " " . ( $help ? " ' " . $this -> db -> escape ( $help ) . " ' " : " null " ) . " , " ;
2020-02-23 23:42:23 +01:00
$sql .= " " . ( $totalizable ? 'TRUE' : 'FALSE' );
2020-01-30 01:48:28 +01:00
$sql .= ')' ;
2017-08-24 13:23:15 +02:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::create_label " , LOG_DEBUG );
2008-06-01 00:08:59 +02:00
if ( $this -> db -> query ( $sql ))
{
return 1 ;
}
else
{
2019-11-13 19:35:39 +01:00
$this -> error = $this -> db -> lasterror ();
$this -> errno = $this -> db -> lasterrno ();
2012-03-03 13:42:27 +01:00
return - 1 ;
2008-06-01 00:08:59 +02:00
}
}
2003-02-19 18:47:22 +01:00
}
2008-06-01 00:08:59 +02:00
2009-01-27 00:58:37 +01:00
/**
2014-03-05 09:57:36 +01:00
* Delete an optional attribute
2012-02-01 20:44:06 +01:00
*
2012-02-20 10:09:28 +01:00
* @ param string $attrname Code of attribute to delete
2015-02-07 17:41:20 +01:00
* @ param string $elementtype Element type ( 'member' , 'product' , 'thirdparty' , 'contact' , ... )
2012-02-20 10:09:28 +01:00
* @ return int < 0 if KO , 0 if nothing is done , 1 if OK
2009-01-27 00:58:37 +01:00
*/
2019-02-27 20:45:07 +01:00
public function delete ( $attrname , $elementtype = 'member' )
2003-02-19 18:47:22 +01:00
{
2019-11-13 19:35:39 +01:00
if ( $elementtype == 'thirdparty' ) $elementtype = 'societe' ;
if ( $elementtype == 'contact' ) $elementtype = 'socpeople' ;
2015-02-07 17:41:20 +01:00
2019-11-13 19:35:39 +01:00
$table = $elementtype . '_extrafields' ;
if ( $elementtype == 'categorie' ) $table = 'categories_extrafields' ;
2012-10-25 10:06:29 +02:00
2019-11-13 19:35:39 +01:00
$error = 0 ;
2017-06-07 11:38:42 +02:00
2019-11-13 19:35:39 +01:00
if ( ! empty ( $attrname ) && preg_match ( " /^ \ w[a-zA-Z0-9-_]* $ / " , $attrname ))
2009-01-27 00:58:37 +01:00
{
2019-11-13 19:35:39 +01:00
$result = $this -> delete_label ( $attrname , $elementtype );
2009-01-27 00:58:37 +01:00
if ( $result < 0 )
2008-06-01 00:08:59 +02:00
{
2019-11-13 19:35:39 +01:00
$this -> error = $this -> db -> lasterror ();
$this -> errors [] = $this -> db -> lasterror ();
2017-01-04 15:05:13 +01:00
$error ++ ;
2008-06-01 00:08:59 +02:00
}
2009-01-27 00:58:37 +01:00
2019-11-13 19:35:39 +01:00
if ( ! $error )
2017-01-04 15:05:13 +01:00
{
$sql = " SELECT COUNT(rowid) as nb " ;
2019-11-13 19:35:39 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " extrafields " ;
$sql .= " WHERE elementtype = ' " . $elementtype . " ' " ;
$sql .= " AND name = ' " . $attrname . " ' " ;
2017-01-04 15:05:13 +01:00
//$sql.= " AND entity IN (0,".$conf->entity.")"; Do not test on entity here. We want to see if there is still on field remaning in other entities before deleting field in table
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$obj = $this -> db -> fetch_object ( $resql );
if ( $obj -> nb <= 0 )
{
2019-11-13 19:35:39 +01:00
$result = $this -> db -> DDLDropField ( MAIN_DB_PREFIX . $table , $attrname ); // This also drop the unique key
2017-01-04 15:05:13 +01:00
if ( $result < 0 )
{
2019-11-13 19:35:39 +01:00
$this -> error = $this -> db -> lasterror ();
$this -> errors [] = $this -> db -> lasterror ();
2017-01-04 15:05:13 +01:00
$error ++ ;
}
}
}
}
2017-06-07 11:38:42 +02:00
2009-01-27 00:58:37 +01:00
return $result ;
}
else
{
2008-06-01 00:08:59 +02:00
return 0 ;
}
}
2003-02-19 18:47:22 +01:00
2018-09-03 20:38:52 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2009-01-27 00:58:37 +01:00
/**
2014-03-05 09:57:36 +01:00
* Delete description of an optional attribute
2012-02-01 20:44:06 +01:00
*
2012-02-20 10:09:28 +01:00
* @ param string $attrname Code of attribute to delete
2015-02-07 17:41:20 +01:00
* @ param string $elementtype Element type ( 'member' , 'product' , 'thirdparty' , ... )
2013-04-21 17:36:23 +02:00
* @ return int < 0 if KO , 0 if nothing is done , 1 if OK
2009-01-27 00:58:37 +01:00
*/
2019-01-27 15:20:16 +01:00
private function delete_label ( $attrname , $elementtype = 'member' )
2003-02-19 18:47:22 +01:00
{
2018-09-03 20:38:52 +02:00
// phpcs:enable
2009-11-07 11:34:25 +01:00
global $conf ;
2009-11-07 14:55:15 +01:00
2019-11-13 19:35:39 +01:00
if ( $elementtype == 'thirdparty' ) $elementtype = 'societe' ;
if ( $elementtype == 'contact' ) $elementtype = 'socpeople' ;
2015-02-07 17:41:20 +01:00
2019-01-27 11:55:16 +01:00
if ( isset ( $attrname ) && $attrname != '' && preg_match ( " /^ \ w[a-zA-Z0-9-_]* $ / " , $attrname ))
2009-11-07 11:34:25 +01:00
{
2011-06-19 14:04:24 +02:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " extrafields " ;
2019-11-13 19:35:39 +01:00
$sql .= " WHERE name = ' " . $attrname . " ' " ;
$sql .= " AND entity IN (0, " . $conf -> entity . ')' ;
$sql .= " AND elementtype = ' " . $elementtype . " ' " ;
2008-06-01 00:08:59 +02:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::delete_label " , LOG_DEBUG );
2019-11-13 19:35:39 +01:00
$resql = $this -> db -> query ( $sql );
2012-02-20 10:09:28 +01:00
if ( $resql )
2008-06-01 00:08:59 +02:00
{
return 1 ;
}
else
{
2019-11-26 18:28:44 +01:00
dol_print_error ( $this -> db );
2011-06-19 16:27:06 +02:00
return - 1 ;
2008-06-01 00:08:59 +02:00
}
2009-11-07 11:34:25 +01:00
}
else
{
2008-06-01 00:08:59 +02:00
return 0 ;
}
}
2003-02-14 18:14:29 +01:00
2009-01-27 00:58:37 +01:00
/**
2011-03-08 11:48:53 +01:00
* Modify type of a personalized attribute
2012-02-01 20:44:06 +01:00
*
2012-02-20 10:09:28 +01:00
* @ param string $attrname Name of attribute
2012-09-22 20:27:06 +02:00
* @ param string $label Label of attribute
2018-02-27 17:30:13 +01:00
* @ param string $type Type of attribute ( 'boolean' , 'int' , 'varchar' , 'text' , 'html' , 'date' , 'datehour' , 'price' , 'phone' , 'mail' , 'password' , 'url' , 'select' , 'checkbox' , ... )
2012-02-20 10:09:28 +01:00
* @ param int $length Length of attribute
2015-02-07 17:41:20 +01:00
* @ param string $elementtype Element type ( 'member' , 'product' , 'thirdparty' , 'contact' , ... )
2013-04-21 17:36:23 +02:00
* @ param int $unique Is field unique or not
* @ param int $required Is field required or not
* @ param int $pos Position of attribute
2017-08-24 13:43:36 +02:00
* @ param array $param Params for field ( ex for select list : array ( 'options' => array ( value '=>' label of option ' )) )
2014-10-16 13:28:39 +02:00
* @ param int $alwayseditable Is attribute always editable regardless of the document status
2015-03-10 18:33:14 +01:00
* @ param string $perms Permission to check
2018-04-10 15:39:11 +02:00
* @ param string $list Visibility
2018-06-28 19:13:23 +02:00
* @ param string $help Help on tooltip
2017-05-24 22:48:44 +02:00
* @ param string $default Default value ( in database . use the default_value feature for default value on screen ) .
* @ param string $computed Computed value
2017-08-04 09:00:11 +02:00
* @ param string $entity Entity of extrafields
2017-08-27 12:06:46 +02:00
* @ param string $langfile Language file
2018-01-13 13:16:33 +01:00
* @ param string $enabled Condition to have the field enabled or not
2018-07-26 09:41:44 +02:00
* @ param int $totalizable Is extrafield totalizable on list
2020-02-23 23:03:17 +01:00
* @ param int $printable Is extrafield displayed on PDF
2012-02-20 10:09:28 +01:00
* @ return int > 0 if OK , <= 0 if KO
2020-02-23 11:32:38 +01:00
* @ throws Exception
2009-11-07 11:34:25 +01:00
*/
2020-02-23 23:03:17 +01:00
public function update ( $attrname , $label , $type , $length , $elementtype , $unique = 0 , $required = 0 , $pos = 0 , $param = '' , $alwayseditable = 0 , $perms = '' , $list = '' , $help = '' , $default = '' , $computed = '' , $entity = '' , $langfile = '' , $enabled = '1' , $totalizable = 0 , $printable = 0 )
2003-02-19 18:47:22 +01:00
{
2019-11-13 19:35:39 +01:00
if ( $elementtype == 'thirdparty' ) $elementtype = 'societe' ;
if ( $elementtype == 'contact' ) $elementtype = 'socpeople' ;
2015-02-07 17:41:20 +01:00
2019-11-13 19:35:39 +01:00
$table = $elementtype . '_extrafields' ;
if ( $elementtype == 'categorie' ) $table = 'categories_extrafields' ;
2011-06-19 16:27:06 +02:00
2019-01-27 11:55:16 +01:00
if ( isset ( $attrname ) && $attrname != '' && preg_match ( " /^ \ w[a-zA-Z0-9-_]* $ / " , $attrname ))
2009-11-07 11:34:25 +01:00
{
2019-11-13 19:35:39 +01:00
if ( $type == 'boolean' ) {
$typedb = 'int' ;
$lengthdb = '1' ;
} elseif ( $type == 'price' ) {
$typedb = 'double' ;
$lengthdb = '24,8' ;
} elseif ( $type == 'phone' ) {
$typedb = 'varchar' ;
$lengthdb = '20' ;
} elseif ( $type == 'mail' ) {
$typedb = 'varchar' ;
$lengthdb = '128' ;
} elseif ( $type == 'url' ) {
$typedb = 'varchar' ;
$lengthdb = '255' ;
} elseif (( $type == 'select' ) || ( $type == 'sellist' ) || ( $type == 'radio' ) || ( $type == 'checkbox' ) || ( $type == 'chkbxlst' )) {
$typedb = 'varchar' ;
$lengthdb = '255' ;
2018-02-27 17:30:13 +01:00
} elseif ( $type == 'html' ) {
2019-11-13 19:35:39 +01:00
$typedb = 'text' ;
} elseif ( $type == 'link' ) {
$typedb = 'int' ;
$lengthdb = '11' ;
} elseif ( $type == 'password' ) {
$typedb = 'varchar' ;
$lengthdb = '50' ;
2013-01-12 14:02:39 +01:00
} else {
2019-11-13 19:35:39 +01:00
$typedb = $type ;
$lengthdb = $length ;
2013-01-12 14:02:39 +01:00
}
2019-11-13 19:35:39 +01:00
$field_desc = array ( 'type' => $typedb , 'value' => $lengthdb , 'null' => ( $required ? 'NOT NULL' : 'NULL' ), 'default' => $default );
2013-04-21 17:36:23 +02:00
2013-03-16 00:00:57 +01:00
if ( $type != 'separate' ) // No table update when separate type
{
2019-11-13 19:35:39 +01:00
$result = $this -> db -> DDLUpdateField ( MAIN_DB_PREFIX . $table , $attrname , $field_desc );
2013-03-16 00:00:57 +01:00
}
if ( $result > 0 || $type == 'separate' )
2008-06-01 00:08:59 +02:00
{
2012-09-30 21:26:58 +02:00
if ( $label )
{
2020-02-23 23:03:17 +01:00
$result = $this -> update_label ( $attrname , $label , $type , $length , $elementtype , $unique , $required , $pos , $param , $alwayseditable , $perms , $list , $help , $default , $computed , $entity , $langfile , $enabled , $totalizable , $printable );
2012-09-30 21:26:58 +02:00
}
2012-09-22 20:27:06 +02:00
if ( $result > 0 )
{
2019-11-13 19:35:39 +01:00
$sql = '' ;
2012-09-22 20:27:06 +02:00
if ( $unique )
{
2019-11-13 19:35:39 +01:00
$sql = " ALTER TABLE " . MAIN_DB_PREFIX . $table . " ADD UNIQUE INDEX uk_ " . $table . " _ " . $attrname . " ( " . $attrname . " ) " ;
2012-09-22 20:27:06 +02:00
}
else
{
2019-11-13 19:35:39 +01:00
$sql = " ALTER TABLE " . MAIN_DB_PREFIX . $table . " DROP INDEX uk_ " . $table . " _ " . $attrname ;
2012-09-22 20:27:06 +02:00
}
2014-06-13 01:34:39 +02:00
dol_syslog ( get_class ( $this ) . '::update' , LOG_DEBUG );
2019-11-13 19:35:39 +01:00
$resql = $this -> db -> query ( $sql , 1 , 'dml' );
2012-09-22 20:27:06 +02:00
return 1 ;
}
else
{
2019-11-13 19:35:39 +01:00
$this -> error = $this -> db -> lasterror ();
2012-09-22 20:27:06 +02:00
return - 1 ;
}
2008-06-01 00:08:59 +02:00
}
else
{
2019-11-13 19:35:39 +01:00
$this -> error = $this -> db -> lasterror ();
2011-06-12 17:47:10 +02:00
return - 1 ;
2008-06-01 00:08:59 +02:00
}
2009-11-07 14:55:15 +01:00
}
else
2009-11-07 11:34:25 +01:00
{
2008-06-01 00:08:59 +02:00
return 0 ;
}
}
2003-02-19 18:47:22 +01:00
2018-09-03 20:38:52 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2009-11-07 11:34:25 +01:00
/**
2011-06-19 16:27:06 +02:00
* Modify description of personalized attribute
2012-02-01 20:44:06 +01:00
*
* @ param string $attrname Name of attribute
* @ param string $label Label of attribute
2013-04-21 17:36:23 +02:00
* @ param string $type Type of attribute
* @ param int $size Length of attribute
2015-02-07 17:41:20 +01:00
* @ param string $elementtype Element type ( 'member' , 'product' , 'thirdparty' , ... )
2013-04-21 17:36:23 +02:00
* @ param int $unique Is field unique or not
* @ param int $required Is field required or not
* @ param int $pos Position of attribute
* @ param array $param Params for field ( ex for select list : array ( 'options' => array ( value '=>' label of option ' )) )
2014-10-16 13:28:39 +02:00
* @ param int $alwayseditable Is attribute always editable regardless of the document status
2015-03-10 18:33:14 +01:00
* @ param string $perms Permission to check
2018-04-10 15:39:11 +02:00
* @ param string $list Visiblity
2018-06-28 19:13:23 +02:00
* @ param string $help Help on tooltip .
2017-05-24 22:48:44 +02:00
* @ param string $default Default value ( in database . use the default_value feature for default value on screen ) .
* @ param string $computed Computed value
2017-08-04 09:00:11 +02:00
* @ param string $entity Entity of extrafields
2017-08-27 12:06:46 +02:00
* @ param string $langfile Language file
2018-01-13 13:16:33 +01:00
* @ param string $enabled Condition to have the field enabled or not
2018-07-26 09:41:44 +02:00
* @ param int $totalizable Is extrafield totalizable on list
2020-02-23 23:03:17 +01:00
* @ param int $printable Is extrafield displayed on PDF
2018-07-26 09:41:44 +02:00
* @ return int <= 0 if KO , > 0 if OK
2020-02-23 11:32:38 +01:00
* @ throws Exception
2013-04-21 17:36:23 +02:00
*/
2020-02-23 23:03:17 +01:00
private function update_label ( $attrname , $label , $type , $size , $elementtype , $unique = 0 , $required = 0 , $pos = 0 , $param = '' , $alwayseditable = 0 , $perms = '' , $list = '0' , $help = '' , $default = '' , $computed = '' , $entity = '' , $langfile = '' , $enabled = '1' , $totalizable = 0 , $printable = 0 )
2008-06-01 00:08:59 +02:00
{
2018-09-03 20:38:52 +02:00
// phpcs:enable
2017-07-25 11:57:36 +02:00
global $conf , $user ;
2020-02-23 23:03:17 +01:00
dol_syslog ( get_class ( $this ) . " ::update_label " . $attrname . " , " . $label . " , " . $type . " , " . $size . " , " . $elementtype . " , " . $unique . " , " . $required . " , " . $pos . " , " . $alwayseditable . " , " . $perms . " , " . $list . " , " . $default . " , " . $computed . " , " . $entity . " , " . $langfile . " , " . $enabled . " , " . $totalizable . " , " . $printable );
2009-11-07 14:55:15 +01:00
2015-03-10 22:27:16 +01:00
// Clean parameters
2019-11-13 19:35:39 +01:00
if ( $elementtype == 'thirdparty' ) $elementtype = 'societe' ;
if ( $elementtype == 'contact' ) $elementtype = 'socpeople' ;
2015-05-25 17:19:22 +02:00
2019-11-13 19:35:39 +01:00
if ( empty ( $pos )) $pos = 0 ;
if ( empty ( $list )) $list = '0' ;
2018-07-26 09:41:44 +02:00
if ( empty ( $totalizable )) {
$totalizable = 0 ;
}
2019-11-13 19:35:39 +01:00
if ( empty ( $required )) $required = 0 ;
if ( empty ( $unique )) $unique = 0 ;
if ( empty ( $alwayseditable )) $alwayseditable = 0 ;
2015-02-07 17:41:20 +01:00
2019-01-27 11:55:16 +01:00
if ( isset ( $attrname ) && $attrname != '' && preg_match ( " /^ \ w[a-zA-Z0-9-_]* $ / " , $attrname ))
2009-11-07 11:34:25 +01:00
{
2009-11-07 14:55:15 +01:00
$this -> db -> begin ();
2013-04-21 17:36:23 +02:00
2018-01-31 10:51:40 +01:00
if ( is_array ( $param ) && count ( $param ) > 0 )
2013-02-15 11:19:49 +01:00
{
2018-01-31 10:51:40 +01:00
$params = serialize ( $param );
}
elseif ( strlen ( $param ) > 0 )
{
$params = trim ( $param );
}
else
{
2019-11-13 19:35:39 +01:00
$params = '' ;
2013-02-15 11:19:49 +01:00
}
2013-04-21 17:36:23 +02:00
2018-09-10 17:36:46 +02:00
if ( $entity === '' || $entity != '0' )
{
// We dont want on all entities, we delete all and current
$sql_del = " DELETE FROM " . MAIN_DB_PREFIX . " extrafields " ;
2019-11-13 19:35:39 +01:00
$sql_del .= " WHERE name = ' " . $attrname . " ' " ;
$sql_del .= " AND entity IN (0, " . ( $entity === '' ? $conf -> entity : $entity ) . " ) " ;
$sql_del .= " AND elementtype = ' " . $elementtype . " ' " ;
2018-09-10 17:36:46 +02:00
}
else
{
// We want on all entities ($entities = '0'), we delete on all only (we keep setup specific to each entity)
$sql_del = " DELETE FROM " . MAIN_DB_PREFIX . " extrafields " ;
2019-11-13 19:35:39 +01:00
$sql_del .= " WHERE name = ' " . $attrname . " ' " ;
$sql_del .= " AND entity = 0 " ;
$sql_del .= " AND elementtype = ' " . $elementtype . " ' " ;
2018-09-10 17:36:46 +02:00
}
2019-11-13 19:35:39 +01:00
$resql1 = $this -> db -> query ( $sql_del );
2009-11-07 14:55:15 +01:00
2011-06-19 14:04:24 +02:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " extrafields( " ;
2019-11-13 19:35:39 +01:00
$sql .= " name, " ; // This is code
$sql .= " entity, " ;
$sql .= " label, " ;
$sql .= " type, " ;
$sql .= " size, " ;
$sql .= " elementtype, " ;
$sql .= " fieldunique, " ;
$sql .= " fieldrequired, " ;
$sql .= " perms, " ;
$sql .= " langs, " ;
$sql .= " pos, " ;
$sql .= " alwayseditable, " ;
$sql .= " param, " ;
$sql .= " list, " ;
2020-02-23 23:03:17 +01:00
$sql .= " printable, " ;
2019-11-13 19:35:39 +01:00
$sql .= " totalizable, " ;
$sql .= " fielddefault, " ;
$sql .= " fieldcomputed, " ;
$sql .= " fk_user_author, " ;
$sql .= " fk_user_modif, " ;
$sql .= " datec, " ;
$sql .= " enabled, " ;
$sql .= " help " ;
$sql .= " ) VALUES ( " ;
$sql .= " ' " . $attrname . " ', " ;
$sql .= " " . ( $entity === '' ? $conf -> entity : $entity ) . " , " ;
$sql .= " ' " . $this -> db -> escape ( $label ) . " ', " ;
$sql .= " ' " . $this -> db -> escape ( $type ) . " ', " ;
$sql .= " ' " . $this -> db -> escape ( $size ) . " ', " ;
$sql .= " ' " . $this -> db -> escape ( $elementtype ) . " ', " ;
$sql .= " " . $unique . " , " ;
$sql .= " " . $required . " , " ;
$sql .= " " . ( $perms ? " ' " . $this -> db -> escape ( $perms ) . " ' " : " null " ) . " , " ;
$sql .= " " . ( $langfile ? " ' " . $this -> db -> escape ( $langfile ) . " ' " : " null " ) . " , " ;
$sql .= " " . $pos . " , " ;
$sql .= " ' " . $this -> db -> escape ( $alwayseditable ) . " ', " ;
$sql .= " ' " . $this -> db -> escape ( $params ) . " ', " ;
$sql .= " ' " . $this -> db -> escape ( $list ) . " ', " ;
2020-02-23 23:03:17 +01:00
$sql .= " ' " . $this -> db -> escape ( $printable ) . " ', " ;
2020-02-23 23:42:23 +01:00
$sql .= " " . ( $totalizable ? 'TRUE' : 'FALSE' ) . " , " ;
2019-11-13 19:35:39 +01:00
$sql .= " " . (( $default != '' ) ? " ' " . $this -> db -> escape ( $default ) . " ' " : " null " ) . " , " ;
$sql .= " " . ( $computed ? " ' " . $this -> db -> escape ( $computed ) . " ' " : " null " ) . " , " ;
$sql .= " " . $user -> id . " , " ;
$sql .= " " . $user -> id . " , " ;
$sql .= " ' " . $this -> db -> idate ( dol_now ()) . " ', " ;
$sql .= " ' " . $this -> db -> escape ( $enabled ) . " ', " ;
$sql .= " " . ( $help ? " ' " . $this -> db -> escape ( $help ) . " ' " : " null " );
$sql .= " ) " ;
$resql2 = $this -> db -> query ( $sql );
2008-06-01 00:08:59 +02:00
2009-11-07 14:55:15 +01:00
if ( $resql1 && $resql2 )
2008-06-01 00:08:59 +02:00
{
2009-11-07 14:55:15 +01:00
$this -> db -> commit ();
2008-06-01 00:08:59 +02:00
return 1 ;
}
else
{
2009-11-07 14:55:15 +01:00
$this -> db -> rollback ();
2019-11-26 18:28:44 +01:00
dol_print_error ( $this -> db );
2012-09-22 20:27:06 +02:00
return - 1 ;
2008-06-01 00:08:59 +02:00
}
2009-11-07 11:34:25 +01:00
}
else
{
2008-06-01 00:08:59 +02:00
return 0 ;
}
2003-02-19 18:47:22 +01:00
}
2004-07-28 00:46:33 +02:00
2004-09-25 13:02:10 +02:00
2019-02-27 20:45:07 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2008-12-18 00:44:04 +01:00
/**
2017-06-07 16:44:04 +02:00
* Load array this -> attributes , or old this -> attribute_xxx like attribute_label , attribute_type , ...
2012-02-01 20:44:06 +01:00
*
2020-04-01 23:15:53 +02:00
* @ param string $elementtype Type of element ( '' = all , 'adherent' , 'commande' , 'thirdparty' , 'facture' , 'propal' , 'product' , ... ) .
2019-10-06 14:41:52 +02:00
* @ param boolean $forceload Force load of extra fields whatever is status of cache .
2017-06-07 16:44:04 +02:00
* @ return array Array of attributes keys + label for all extra fields .
2008-12-18 00:44:04 +01:00
*/
2019-02-27 20:45:07 +01:00
public function fetch_name_optionals_label ( $elementtype , $forceload = false )
2003-02-19 18:47:22 +01:00
{
2018-09-03 20:38:52 +02:00
// phpcs:enable
2020-03-20 10:05:08 +01:00
global $conf ;
2009-11-07 14:55:15 +01:00
2018-04-17 13:52:50 +02:00
if ( empty ( $elementtype )) return array ();
2015-04-06 19:46:10 +02:00
2019-11-13 19:35:39 +01:00
if ( $elementtype == 'thirdparty' ) $elementtype = 'societe' ;
if ( $elementtype == 'contact' ) $elementtype = 'socpeople' ;
if ( $elementtype == 'order_supplier' ) $elementtype = 'commande_fournisseur' ;
2015-02-07 17:41:20 +01:00
2019-11-13 19:35:39 +01:00
$array_name_label = array ();
2015-04-06 19:46:10 +02:00
2019-11-10 17:48:50 +01:00
// We should not have several time this request. If we have, there is some optimization to do by calling a simple $extrafields->fetch_optionals() in top of code and not into subcode
2020-02-23 23:03:17 +01:00
$sql = " SELECT rowid,name,label,type,size,elementtype,fieldunique,fieldrequired,param,pos,alwayseditable,perms,langs,list,printable,totalizable,fielddefault,fieldcomputed,entity,enabled,help " ;
2019-11-13 19:35:39 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " extrafields " ;
2019-03-21 11:53:04 +01:00
//$sql.= " WHERE entity IN (0,".$conf->entity.")"; // Filter is done later
2019-11-13 19:35:39 +01:00
if ( $elementtype ) $sql .= " WHERE elementtype = ' " . $elementtype . " ' " ; // Filed with object->table_element
$sql .= " ORDER BY pos " ;
2009-01-27 00:58:37 +01:00
2019-11-13 19:35:39 +01:00
$resql = $this -> db -> query ( $sql );
2008-12-18 00:44:04 +01:00
if ( $resql )
2008-06-01 00:08:59 +02:00
{
2008-12-18 00:44:04 +01:00
if ( $this -> db -> num_rows ( $resql ))
2008-06-01 00:08:59 +02:00
{
2008-12-18 00:44:04 +01:00
while ( $tab = $this -> db -> fetch_object ( $resql ))
2008-06-01 00:08:59 +02:00
{
2019-03-21 11:53:04 +01:00
if ( $tab -> entity != 0 && $tab -> entity != $conf -> entity )
{
2019-03-21 12:33:39 +01:00
// This field is not in current entity. We discard but before we save it into the array of mandatory fields if it is a mandatory field without default value
2019-03-21 11:54:14 +01:00
if ( $tab -> fieldrequired && is_null ( $tab -> fielddefault ))
2019-03-21 11:53:04 +01:00
{
2019-11-13 19:35:39 +01:00
$this -> attributes [ $tab -> elementtype ][ 'mandatoryfieldsofotherentities' ][ $tab -> name ] = $tab -> type ;
2019-03-21 11:53:04 +01:00
}
continue ;
}
2017-06-07 16:44:04 +02:00
// We can add this attribute to object. TODO Remove this and return $this->attributes[$elementtype]['label']
2013-03-15 23:39:18 +01:00
if ( $tab -> type != 'separate' )
{
2020-01-30 01:48:28 +01:00
$array_name_label [ $tab -> name ] = $tab -> label ;
2013-03-15 23:39:18 +01:00
}
2017-08-24 13:23:15 +02:00
2017-06-07 16:44:04 +02:00
// Old usage
2020-01-30 01:48:28 +01:00
$this -> attribute_type [ $tab -> name ] = $tab -> type ;
$this -> attribute_label [ $tab -> name ] = $tab -> label ;
$this -> attribute_size [ $tab -> name ] = $tab -> size ;
$this -> attribute_elementtype [ $tab -> name ] = $tab -> elementtype ;
$this -> attribute_default [ $tab -> name ] = $tab -> fielddefault ;
$this -> attribute_computed [ $tab -> name ] = $tab -> fieldcomputed ;
$this -> attribute_unique [ $tab -> name ] = $tab -> fieldunique ;
$this -> attribute_required [ $tab -> name ] = $tab -> fieldrequired ;
$this -> attribute_param [ $tab -> name ] = ( $tab -> param ? unserialize ( $tab -> param ) : '' );
$this -> attribute_pos [ $tab -> name ] = $tab -> pos ;
$this -> attribute_alwayseditable [ $tab -> name ] = $tab -> alwayseditable ;
$this -> attribute_perms [ $tab -> name ] = ( strlen ( $tab -> perms ) == 0 ? 1 : $tab -> perms );
$this -> attribute_langfile [ $tab -> name ] = $tab -> langs ;
$this -> attribute_list [ $tab -> name ] = $tab -> list ;
$this -> attribute_totalizable [ $tab -> name ] = $tab -> totalizable ;
$this -> attribute_entityid [ $tab -> name ] = $tab -> entity ;
2017-08-24 13:23:15 +02:00
2017-06-07 16:44:04 +02:00
// New usage
2020-01-30 01:48:28 +01:00
$this -> attributes [ $tab -> elementtype ][ 'type' ][ $tab -> name ] = $tab -> type ;
$this -> attributes [ $tab -> elementtype ][ 'label' ][ $tab -> name ] = $tab -> label ;
$this -> attributes [ $tab -> elementtype ][ 'size' ][ $tab -> name ] = $tab -> size ;
$this -> attributes [ $tab -> elementtype ][ 'elementtype' ][ $tab -> name ] = $tab -> elementtype ;
$this -> attributes [ $tab -> elementtype ][ 'default' ][ $tab -> name ] = $tab -> fielddefault ;
$this -> attributes [ $tab -> elementtype ][ 'computed' ][ $tab -> name ] = $tab -> fieldcomputed ;
$this -> attributes [ $tab -> elementtype ][ 'unique' ][ $tab -> name ] = $tab -> fieldunique ;
$this -> attributes [ $tab -> elementtype ][ 'required' ][ $tab -> name ] = $tab -> fieldrequired ;
$this -> attributes [ $tab -> elementtype ][ 'param' ][ $tab -> name ] = ( $tab -> param ? unserialize ( $tab -> param ) : '' );
$this -> attributes [ $tab -> elementtype ][ 'pos' ][ $tab -> name ] = $tab -> pos ;
$this -> attributes [ $tab -> elementtype ][ 'alwayseditable' ][ $tab -> name ] = $tab -> alwayseditable ;
$this -> attributes [ $tab -> elementtype ][ 'perms' ][ $tab -> name ] = ( strlen ( $tab -> perms ) == 0 ? 1 : $tab -> perms );
$this -> attributes [ $tab -> elementtype ][ 'langfile' ][ $tab -> name ] = $tab -> langs ;
$this -> attributes [ $tab -> elementtype ][ 'list' ][ $tab -> name ] = $tab -> list ;
2020-02-23 23:03:17 +01:00
$this -> attributes [ $tab -> elementtype ][ 'printable' ][ $tab -> name ] = $tab -> printable ;
2020-02-23 23:42:23 +01:00
$this -> attributes [ $tab -> elementtype ][ 'totalizable' ][ $tab -> name ] = ( $tab -> totalizable ? 1 : 0 );
2020-01-30 01:48:28 +01:00
$this -> attributes [ $tab -> elementtype ][ 'entityid' ][ $tab -> name ] = $tab -> entity ;
$this -> attributes [ $tab -> elementtype ][ 'enabled' ][ $tab -> name ] = $tab -> enabled ;
$this -> attributes [ $tab -> elementtype ][ 'help' ][ $tab -> name ] = $tab -> help ;
$this -> attributes [ $tab -> elementtype ][ 'loaded' ] = 1 ;
2008-06-01 00:08:59 +02:00
}
}
2020-01-30 01:48:28 +01:00
if ( $elementtype ) $this -> attributes [ $elementtype ][ 'loaded' ] = 1 ; // If nothing found, we also save tag 'loaded'
2009-11-07 11:34:25 +01:00
}
else
{
2020-01-30 01:48:28 +01:00
$this -> error = $this -> db -> lasterror ();
2016-06-29 12:26:01 +02:00
dol_syslog ( get_class ( $this ) . " ::fetch_name_optionals_label " . $this -> error , LOG_ERR );
2008-06-01 00:08:59 +02:00
}
2014-11-17 15:09:52 +01:00
return $array_name_label ;
2008-06-01 00:08:59 +02:00
}
2009-11-07 14:55:15 +01:00
2011-06-22 13:41:37 +02:00
/**
2014-02-16 23:51:29 +01:00
* Return HTML string to put an input field into a page
2017-10-25 11:42:14 +02:00
* Code very similar with showInputField of common object
2012-02-20 10:09:28 +01:00
*
2018-04-17 13:52:50 +02:00
* @ param string $key Key of attribute
* @ param string $value Preselected value to show ( for date type it must be in timestamp format , for amount or price it must be a php numeric value )
* @ param string $moreparam To add more parametes on html input tag
* @ param string $keysuffix Prefix string to add after name and id of field ( can be used to avoid duplicate names )
* @ param string $keyprefix Suffix string to add before name and id of field ( can be used to avoid duplicate names )
* @ param string $morecss More css ( to defined size of field . Old behaviour : may also be a numeric )
* @ param int $objectid Current object id
* @ param string $extrafieldsobjectkey If defined ( for example $object -> table_element ), use the new method to get extrafields data
2019-09-24 16:26:15 +02:00
* @ param string $mode 1 = Used for search filters
2015-08-24 14:42:07 +02:00
* @ return string
2011-06-22 13:41:37 +02:00
*/
2019-09-24 16:26:15 +02:00
public function showInputField ( $key , $value , $moreparam = '' , $keysuffix = '' , $keyprefix = '' , $morecss = '' , $objectid = 0 , $extrafieldsobjectkey = '' , $mode = 0 )
2011-06-22 13:41:37 +02:00
{
2019-11-13 19:35:39 +01:00
global $conf , $langs , $form ;
2017-10-26 02:55:43 +02:00
2019-11-13 19:35:39 +01:00
if ( ! is_object ( $form ))
2017-10-26 02:55:43 +02:00
{
require_once DOL_DOCUMENT_ROOT . '/core/class/html.form.class.php' ;
2019-11-13 19:35:39 +01:00
$form = new Form ( $this -> db );
2017-10-26 02:55:43 +02:00
}
2013-01-18 15:57:11 +01:00
2019-11-13 19:35:39 +01:00
$out = '' ;
2017-11-19 16:55:28 +01:00
2019-11-13 19:35:39 +01:00
if ( ! preg_match ( '/options_$/' , $keyprefix )) // Because we work on extrafields, we add 'options_' to prefix if not already added
2019-06-19 15:48:04 +02:00
{
$keyprefix = $keyprefix . 'options_' ;
}
2017-10-25 11:42:14 +02:00
2019-11-13 19:35:39 +01:00
if ( ! empty ( $extrafieldsobjectkey ))
2018-04-17 13:52:50 +02:00
{
2019-11-13 19:35:39 +01:00
$label = $this -> attributes [ $extrafieldsobjectkey ][ 'label' ][ $key ];
$type = $this -> attributes [ $extrafieldsobjectkey ][ 'type' ][ $key ];
$size = $this -> attributes [ $extrafieldsobjectkey ][ 'size' ][ $key ];
$default = $this -> attributes [ $extrafieldsobjectkey ][ 'default' ][ $key ];
$computed = $this -> attributes [ $extrafieldsobjectkey ][ 'computed' ][ $key ];
$unique = $this -> attributes [ $extrafieldsobjectkey ][ 'unique' ][ $key ];
$required = $this -> attributes [ $extrafieldsobjectkey ][ 'required' ][ $key ];
$param = $this -> attributes [ $extrafieldsobjectkey ][ 'param' ][ $key ];
$perms = dol_eval ( $this -> attributes [ $extrafieldsobjectkey ][ 'perms' ][ $key ], 1 );
$langfile = $this -> attributes [ $extrafieldsobjectkey ][ 'langfile' ][ $key ];
$list = dol_eval ( $this -> attributes [ $extrafieldsobjectkey ][ 'list' ][ $key ], 1 );
$totalizable = $this -> attributes [ $extrafieldsobjectkey ][ 'totalizable' ][ $key ];
$help = $this -> attributes [ $extrafieldsobjectkey ][ 'help' ][ $key ];
$hidden = ( empty ( $list ) ? 1 : 0 ); // If empty, we are sure it is hidden, otherwise we show. If it depends on mode (view/create/edit form or list, this must be filtered by caller)
2018-04-17 13:52:50 +02:00
}
else // Old usage
{
2019-11-13 19:35:39 +01:00
$label = $this -> attribute_label [ $key ];
$type = $this -> attribute_type [ $key ];
$size = $this -> attribute_size [ $key ];
$elementtype = $this -> attribute_elementtype [ $key ]; // Seems not used
$default = $this -> attribute_default [ $key ];
$computed = $this -> attribute_computed [ $key ];
$unique = $this -> attribute_unique [ $key ];
$required = $this -> attribute_required [ $key ];
$param = $this -> attribute_param [ $key ];
$langfile = $this -> attribute_langfile [ $key ];
$list = $this -> attribute_list [ $key ];
$totalizable = $this -> attribute_totalizable [ $key ];
$hidden = ( empty ( $list ) ? 1 : 0 ); // If empty, we are sure it is hidden, otherwise we show. If it depends on mode (view/create/edit form or list, this must be filtered by caller)
2018-04-17 13:52:50 +02:00
}
2015-03-10 18:33:14 +01:00
2017-06-10 22:49:16 +02:00
if ( $computed )
{
2019-11-13 19:35:39 +01:00
if ( ! preg_match ( '/^search_/' , $keyprefix )) return '<span class="opacitymedium">' . $langs -> trans ( " AutomaticallyCalculated " ) . '</span>' ;
2017-10-16 21:05:12 +02:00
else return '' ;
2017-06-10 22:49:16 +02:00
}
2017-06-07 11:38:42 +02:00
2018-02-06 10:34:29 +01:00
if ( empty ( $morecss ))
2013-04-21 17:36:23 +02:00
{
2017-10-16 21:05:12 +02:00
if ( $type == 'date' )
{
2018-02-06 10:34:29 +01:00
$morecss = 'minwidth100imp' ;
2017-10-16 21:05:12 +02:00
}
2019-11-01 23:16:08 +01:00
elseif ( $type == 'datetime' || $type == 'link' )
2017-10-16 21:05:12 +02:00
{
2018-02-06 10:34:29 +01:00
$morecss = 'minwidth200imp' ;
2017-10-16 21:05:12 +02:00
}
2019-11-13 19:35:39 +01:00
elseif ( in_array ( $type , array ( 'int' , 'integer' , 'double' , 'price' )))
2017-10-16 21:05:12 +02:00
{
2018-02-06 10:34:29 +01:00
$morecss = 'maxwidth75' ;
2017-10-16 21:05:12 +02:00
}
2018-06-05 11:25:41 +02:00
elseif ( $type == 'password' )
{
2019-11-13 19:35:39 +01:00
$morecss = 'maxwidth100' ;
2018-06-05 11:25:41 +02:00
}
2017-10-16 21:05:12 +02:00
elseif ( $type == 'url' )
{
2019-11-13 19:35:39 +01:00
$morecss = 'minwidth400' ;
2017-10-16 21:05:12 +02:00
}
elseif ( $type == 'boolean' )
{
2019-11-13 19:35:39 +01:00
$morecss = '' ;
2017-10-16 21:05:12 +02:00
}
else
{
if ( round ( $size ) < 12 )
{
2018-02-06 10:34:29 +01:00
$morecss = 'minwidth100' ;
2017-10-16 21:05:12 +02:00
}
2019-01-27 10:49:34 +01:00
elseif ( round ( $size ) <= 48 )
2017-10-16 21:05:12 +02:00
{
2018-02-06 10:34:29 +01:00
$morecss = 'minwidth200' ;
2017-10-16 21:05:12 +02:00
}
else
{
2018-02-06 10:34:29 +01:00
$morecss = 'minwidth400' ;
2017-10-16 21:05:12 +02:00
}
}
2013-04-21 17:36:23 +02:00
}
2017-06-07 11:38:42 +02:00
2019-11-13 19:35:39 +01:00
if ( in_array ( $type , array ( 'date' , 'datetime' )))
2013-04-21 17:36:23 +02:00
{
2019-11-13 19:35:39 +01:00
$tmp = explode ( ',' , $size );
$newsize = $tmp [ 0 ];
2013-04-21 17:36:23 +02:00
2019-01-27 11:55:16 +01:00
$showtime = in_array ( $type , array ( 'datetime' )) ? 1 : 0 ;
2014-05-01 16:26:57 +02:00
2018-09-09 10:40:00 +02:00
// Do not show current date when field not required (see selectDate() method)
2014-05-01 16:26:57 +02:00
if ( ! $required && $value == '' ) $value = '-1' ;
// TODO Must also support $moreparam
2018-09-17 09:17:11 +02:00
$out = $form -> selectDate ( $value , $keyprefix . $key . $keysuffix , $showtime , $showtime , $required , '' , 1 , (( $keyprefix != 'search_' && $keyprefix != 'search_options_' ) ? 1 : 0 ), 0 , 1 );
2013-04-21 17:36:23 +02:00
}
2019-11-13 19:35:39 +01:00
elseif ( in_array ( $type , array ( 'int' , 'integer' )))
2013-04-21 17:36:23 +02:00
{
2019-11-13 19:35:39 +01:00
$tmp = explode ( ',' , $size );
$newsize = $tmp [ 0 ];
$out = '<input type="text" class="flat ' . $morecss . ' maxwidthonsmartphone" name="' . $keyprefix . $key . $keysuffix . '" id="' . $keyprefix . $key . $keysuffix . '" maxlength="' . $newsize . '" value="' . dol_escape_htmltag ( $value ) . '"' . ( $moreparam ? $moreparam : '' ) . '>' ;
2013-04-21 17:36:23 +02:00
}
2017-10-25 11:42:14 +02:00
elseif ( preg_match ( '/varchar/' , $type ))
2013-04-21 17:36:23 +02:00
{
2019-11-13 19:35:39 +01:00
$out = '<input type="text" class="flat ' . $morecss . ' maxwidthonsmartphone" name="' . $keyprefix . $key . $keysuffix . '" id="' . $keyprefix . $key . $keysuffix . '" maxlength="' . $size . '" value="' . dol_escape_htmltag ( $value ) . '"' . ( $moreparam ? $moreparam : '' ) . '>' ;
2013-04-21 17:36:23 +02:00
}
2016-11-08 17:21:26 +01:00
elseif ( in_array ( $type , array ( 'mail' , 'phone' , 'url' )))
{
2019-11-13 19:35:39 +01:00
$out = '<input type="text" class="flat ' . $morecss . ' maxwidthonsmartphone" name="' . $keyprefix . $key . $keysuffix . '" id="' . $keyprefix . $key . $keysuffix . '" value="' . dol_escape_htmltag ( $value ) . '" ' . ( $moreparam ? $moreparam : '' ) . '>' ;
2016-11-08 17:21:26 +01:00
}
2013-04-21 17:36:23 +02:00
elseif ( $type == 'text' )
2018-02-27 17:30:13 +01:00
{
2019-11-13 19:35:39 +01:00
if ( ! preg_match ( '/search_/' , $keyprefix )) // If keyprefix is search_ or search_options_, we must just use a simple text field
2018-02-27 17:30:13 +01:00
{
require_once DOL_DOCUMENT_ROOT . '/core/class/doleditor.class.php' ;
2019-11-13 19:35:39 +01:00
$doleditor = new DolEditor ( $keyprefix . $key . $keysuffix , $value , '' , 200 , 'dolibarr_notes' , 'In' , false , false , false , ROWS_5 , '90%' );
$out = $doleditor -> Create ( 1 );
2018-02-27 17:30:13 +01:00
}
else
{
2019-11-13 19:35:39 +01:00
$out = '<input type="text" class="flat ' . $morecss . ' maxwidthonsmartphone" name="' . $keyprefix . $key . $keysuffix . '" id="' . $keyprefix . $key . $keysuffix . '" value="' . dol_escape_htmltag ( $value ) . '" ' . ( $moreparam ? $moreparam : '' ) . '>' ;
2018-02-27 17:30:13 +01:00
}
}
elseif ( $type == 'html' )
2013-04-21 17:36:23 +02:00
{
2019-11-13 19:35:39 +01:00
if ( ! preg_match ( '/search_/' , $keyprefix )) // If keyprefix is search_ or search_options_, we must just use a simple text field
2017-12-01 12:55:28 +01:00
{
require_once DOL_DOCUMENT_ROOT . '/core/class/doleditor.class.php' ;
2019-11-13 19:35:39 +01:00
$doleditor = new DolEditor ( $keyprefix . $key . $keysuffix , $value , '' , 200 , 'dolibarr_notes' , 'In' , false , false , ! empty ( $conf -> fckeditor -> enabled ) && $conf -> global -> FCKEDITOR_ENABLE_SOCIETE , ROWS_5 , '90%' );
$out = $doleditor -> Create ( 1 );
2017-12-01 12:55:28 +01:00
}
else
{
2019-11-13 19:35:39 +01:00
$out = '<input type="text" class="flat ' . $morecss . ' maxwidthonsmartphone" name="' . $keyprefix . $key . $keysuffix . '" id="' . $keyprefix . $key . $keysuffix . '" value="' . dol_escape_htmltag ( $value ) . '" ' . ( $moreparam ? $moreparam : '' ) . '>' ;
2017-12-01 12:55:28 +01:00
}
2013-04-21 17:36:23 +02:00
}
elseif ( $type == 'boolean' )
{
2019-09-24 16:26:15 +02:00
if ( empty ( $mode ))
{
2019-11-13 19:35:39 +01:00
$checked = '' ;
2019-09-24 16:26:15 +02:00
if ( ! empty ( $value )) {
2019-11-13 19:35:39 +01:00
$checked = ' checked value="1" ' ;
2019-09-24 16:26:15 +02:00
} else {
2019-11-13 19:35:39 +01:00
$checked = ' value="1" ' ;
2019-09-24 16:26:15 +02:00
}
2019-11-13 19:35:39 +01:00
$out = '<input type="checkbox" class="flat ' . $morecss . ' maxwidthonsmartphone" name="' . $keyprefix . $key . $keysuffix . '" id="' . $keyprefix . $key . $keysuffix . '" ' . $checked . ' ' . ( $moreparam ? $moreparam : '' ) . '>' ;
2019-09-24 16:26:15 +02:00
}
else
{
2019-11-13 19:35:39 +01:00
$out .= $form -> selectyesno ( $keyprefix . $key . $keysuffix , $value , 1 , false , 1 );
2013-04-21 17:36:23 +02:00
}
}
elseif ( $type == 'price' )
{
2017-10-04 15:17:20 +02:00
if ( ! empty ( $value )) { // $value in memory is a php numeric, we format it into user number format.
2019-11-13 19:35:39 +01:00
$value = price ( $value );
2017-10-04 15:17:20 +02:00
}
2019-11-13 19:35:39 +01:00
$out = '<input type="text" class="flat ' . $morecss . ' maxwidthonsmartphone" name="' . $keyprefix . $key . $keysuffix . '" id="' . $keyprefix . $key . $keysuffix . '" value="' . $value . '" ' . ( $moreparam ? $moreparam : '' ) . '> ' . $langs -> getCurrencySymbol ( $conf -> currency );
2013-04-21 17:36:23 +02:00
}
2013-06-10 14:13:44 +02:00
elseif ( $type == 'double' )
{
2017-10-04 15:17:20 +02:00
if ( ! empty ( $value )) { // $value in memory is a php numeric, we format it into user number format.
2019-11-13 19:35:39 +01:00
$value = price ( $value );
2013-06-10 14:13:44 +02:00
}
2019-11-13 19:35:39 +01:00
$out = '<input type="text" class="flat ' . $morecss . ' maxwidthonsmartphone" name="' . $keyprefix . $key . $keysuffix . '" id="' . $keyprefix . $key . $keysuffix . '" value="' . $value . '" ' . ( $moreparam ? $moreparam : '' ) . '> ' ;
2013-06-10 14:13:44 +02:00
}
2013-04-21 17:36:23 +02:00
elseif ( $type == 'select' )
{
2014-01-09 02:10:03 +01:00
$out = '' ;
2019-11-13 19:35:39 +01:00
if ( ! empty ( $conf -> use_javascript_ajax ) && ! empty ( $conf -> global -> MAIN_EXTRAFIELDS_USE_SELECT2 ))
2014-01-09 02:10:03 +01:00
{
2019-11-13 19:35:39 +01:00
include_once DOL_DOCUMENT_ROOT . '/core/lib/ajax.lib.php' ;
$out .= ajax_combobox ( $keyprefix . $key . $keysuffix , array (), 0 );
2014-01-09 02:10:03 +01:00
}
2019-11-13 19:35:39 +01:00
$out .= '<select class="flat ' . $morecss . ' maxwidthonsmartphone" name="' . $keyprefix . $key . $keysuffix . '" id="' . $keyprefix . $key . $keysuffix . '" ' . ( $moreparam ? $moreparam : '' ) . '>' ;
$out .= '<option value="0"> </option>' ;
2016-01-27 21:10:42 +01:00
foreach ( $param [ 'options' ] as $key => $val )
2013-04-21 17:36:23 +02:00
{
2017-08-24 13:23:15 +02:00
if (( string ) $key == '' ) continue ;
2013-07-18 17:42:09 +02:00
list ( $val , $parent ) = explode ( '|' , $val );
2019-11-13 19:35:39 +01:00
$out .= '<option value="' . $key . '"' ;
$out .= ((( string ) $value == ( string ) $key ) ? ' selected' : '' );
$out .= ( ! empty ( $parent ) ? ' parent="' . $parent . '"' : '' );
$out .= '>' ;
if ( $langfile && $val ) $out .= $langs -> trans ( $val );
else $out .= $val ;
$out .= '</option>' ;
2013-04-21 17:36:23 +02:00
}
2019-11-13 19:35:39 +01:00
$out .= '</select>' ;
2013-04-21 17:36:23 +02:00
}
elseif ( $type == 'sellist' )
{
2014-01-09 02:10:03 +01:00
$out = '' ;
2019-11-13 19:35:39 +01:00
if ( ! empty ( $conf -> use_javascript_ajax ) && ! empty ( $conf -> global -> MAIN_EXTRAFIELDS_USE_SELECT2 ))
2014-01-09 02:10:03 +01:00
{
2019-11-13 19:35:39 +01:00
include_once DOL_DOCUMENT_ROOT . '/core/lib/ajax.lib.php' ;
$out .= ajax_combobox ( $keyprefix . $key . $keysuffix , array (), 0 );
2014-01-09 02:10:03 +01:00
}
2019-11-13 19:35:39 +01:00
$out .= '<select class="flat ' . $morecss . ' maxwidthonsmartphone" name="' . $keyprefix . $key . $keysuffix . '" id="' . $keyprefix . $key . $keysuffix . '" ' . ( $moreparam ? $moreparam : '' ) . '>' ;
2013-09-02 00:34:41 +02:00
if ( is_array ( $param [ 'options' ]))
{
2019-11-13 19:35:39 +01:00
$param_list = array_keys ( $param [ 'options' ]);
2013-07-25 20:18:45 +02:00
$InfoFieldList = explode ( " : " , $param_list [ 0 ]);
2019-11-13 19:35:39 +01:00
$parentName = '' ;
$parentField = '' ;
2013-11-05 13:11:36 +01:00
// 0 : tableName
// 1 : label field name
// 2 : key fields name (if differ of rowid)
// 3 : key field parent (for dependent lists)
// 4 : where clause filter on column or table extrafield, syntax field='value' or extra.field=value
2019-04-02 11:01:46 +02:00
// 5 : id category type
// 6 : ids categories list separated by comma for category root
2019-11-13 19:35:39 +01:00
$keyList = ( empty ( $InfoFieldList [ 2 ]) ? 'rowid' : $InfoFieldList [ 2 ] . ' as rowid' );
2013-11-05 13:11:36 +01:00
2016-07-05 15:45:33 +02:00
2019-11-13 19:35:39 +01:00
if ( count ( $InfoFieldList ) > 4 && ! empty ( $InfoFieldList [ 4 ]))
2013-11-05 13:11:36 +01:00
{
2013-11-05 19:53:49 +01:00
if ( strpos ( $InfoFieldList [ 4 ], 'extra.' ) !== false )
{
2019-11-13 19:35:39 +01:00
$keyList = 'main.' . $InfoFieldList [ 2 ] . ' as rowid' ;
2013-11-05 13:11:36 +01:00
} else {
2019-11-13 19:35:39 +01:00
$keyList = $InfoFieldList [ 2 ] . ' as rowid' ;
2013-11-04 19:21:13 +01:00
}
}
2019-11-13 19:35:39 +01:00
if ( count ( $InfoFieldList ) > 3 && ! empty ( $InfoFieldList [ 3 ]))
2016-07-05 15:45:33 +02:00
{
list ( $parentName , $parentField ) = explode ( '|' , $InfoFieldList [ 3 ]);
2019-11-13 19:35:39 +01:00
$keyList .= ', ' . $parentField ;
2016-07-05 15:45:33 +02:00
}
2013-06-27 13:35:28 +02:00
2019-04-02 11:01:46 +02:00
$filter_categorie = false ;
if ( count ( $InfoFieldList ) > 5 ) {
if ( $InfoFieldList [ 0 ] == 'categorie' ) {
$filter_categorie = true ;
}
}
2013-04-21 17:36:23 +02:00
2019-04-02 11:01:46 +02:00
if ( $filter_categorie === false ) {
$fields_label = explode ( '|' , $InfoFieldList [ 1 ]);
if ( is_array ( $fields_label )) {
$keyList .= ', ' ;
$keyList .= implode ( ', ' , $fields_label );
}
2015-04-21 20:21:24 +02:00
2019-04-02 11:01:46 +02:00
$sqlwhere = '' ;
2019-11-13 19:35:39 +01:00
$sql = 'SELECT ' . $keyList ;
$sql .= ' FROM ' . MAIN_DB_PREFIX . $InfoFieldList [ 0 ];
2019-04-02 11:01:46 +02:00
if ( ! empty ( $InfoFieldList [ 4 ])) {
// can use curent entity filter
if ( strpos ( $InfoFieldList [ 4 ], '$ENTITY$' ) !== false ) {
$InfoFieldList [ 4 ] = str_replace ( '$ENTITY$' , $conf -> entity , $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 ) {
2019-11-13 19:35:39 +01:00
$sql .= ' as main, ' . MAIN_DB_PREFIX . $InfoFieldList [ 0 ] . '_extrafields as extra' ;
$sqlwhere .= ' WHERE extra.fk_object=main.' . $InfoFieldList [ 2 ] . ' AND ' . $InfoFieldList [ 4 ];
2019-04-02 11:01:46 +02:00
} else {
2019-11-13 19:35:39 +01:00
$sqlwhere .= ' WHERE ' . $InfoFieldList [ 4 ];
2019-04-02 11:01:46 +02:00
}
} 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' ))) {
2019-11-13 19:35:39 +01:00
$sqlwhere .= ' AND entity = ' . $conf -> entity ;
2019-04-02 11:01:46 +02:00
}
$sql .= $sqlwhere ;
//print $sql;
2019-11-13 19:35:39 +01:00
$sql .= ' ORDER BY ' . implode ( ', ' , $fields_label );
2019-04-02 11:01:46 +02:00
2019-11-13 19:35:39 +01:00
dol_syslog ( get_class ( $this ) . '::showInputField type=sellist' , LOG_DEBUG );
2019-04-02 11:01:46 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql ) {
$out .= '<option value="0"> </option>' ;
$num = $this -> db -> num_rows ( $resql );
$i = 0 ;
while ( $i < $num ) {
$labeltoshow = '' ;
$obj = $this -> db -> fetch_object ( $resql );
// Several field into label (eq table:code|libelle:rowid)
$notrans = false ;
$fields_label = explode ( '|' , $InfoFieldList [ 1 ]);
2019-09-24 17:52:11 +02:00
if ( is_array ( $fields_label ) && count ( $fields_label ) > 1 ) {
2019-04-02 11:01:46 +02:00
$notrans = true ;
foreach ( $fields_label as $field_toshow ) {
2019-11-13 19:35:39 +01:00
$labeltoshow .= $obj -> $field_toshow . ' ' ;
2019-04-02 11:01:46 +02:00
}
} else {
$labeltoshow = $obj -> { $InfoFieldList [ 1 ]};
}
$labeltoshow = dol_trunc ( $labeltoshow , 45 );
if ( $value == $obj -> rowid ) {
2019-05-20 15:47:06 +02:00
if ( ! $notrans ) {
foreach ( $fields_label as $field_toshow ) {
$translabel = $langs -> trans ( $obj -> $field_toshow );
2019-11-13 19:35:39 +01:00
$labeltoshow = dol_trunc ( $translabel , 18 ) . ' ' ;
2019-05-20 15:47:06 +02:00
}
}
2019-11-13 19:35:39 +01:00
$out .= '<option value="' . $obj -> rowid . '" selected>' . $labeltoshow . '</option>' ;
2019-04-02 11:01:46 +02:00
} else {
if ( ! $notrans ) {
$translabel = $langs -> trans ( $obj -> { $InfoFieldList [ 1 ]});
2019-09-24 17:52:11 +02:00
$labeltoshow = dol_trunc ( $translabel , 18 );
2019-04-02 11:01:46 +02:00
}
if ( empty ( $labeltoshow )) $labeltoshow = '(not defined)' ;
if ( ! empty ( $InfoFieldList [ 3 ]) && $parentField ) {
2019-11-13 19:35:39 +01:00
$parent = $parentName . ':' . $obj -> { $parentField };
2019-04-02 11:01:46 +02:00
}
2019-11-13 19:35:39 +01:00
$out .= '<option value="' . $obj -> rowid . '"' ;
2019-04-02 11:01:46 +02:00
$out .= ( $value == $obj -> rowid ? ' selected' : '' );
2019-11-13 19:35:39 +01:00
$out .= ( ! empty ( $parent ) ? ' parent="' . $parent . '"' : '' );
$out .= '>' . $labeltoshow . '</option>' ;
2019-04-02 11:01:46 +02:00
}
$i ++ ;
}
$this -> db -> free ( $resql );
} else {
2019-11-13 19:35:39 +01:00
print 'Error in request ' . $sql . ' ' . $this -> db -> lasterror () . '. Check setup of extra parameters.<br>' ;
2019-04-02 11:01:46 +02:00
}
} else {
2020-04-10 10:59:32 +02:00
require_once DOL_DOCUMENT_ROOT . '/categories/class/categorie.class.php' ;
2019-04-02 11:01:46 +02:00
$data = $form -> select_all_categories ( Categorie :: $MAP_ID_TO_CODE [ $InfoFieldList [ 5 ]], '' , 'parent' , 64 , $InfoFieldList [ 6 ], 1 , 1 );
$out .= '<option value="0"> </option>' ;
foreach ( $data as $data_key => $data_value ) {
2019-11-13 19:35:39 +01:00
$out .= '<option value="' . $data_key . '"' ;
2019-04-02 11:01:46 +02:00
$out .= ( $value == $data_key ? ' selected' : '' );
2019-11-13 19:35:39 +01:00
$out .= '>' . $data_value . '</option>' ;
2019-04-02 11:01:46 +02:00
}
}
2013-04-21 17:36:23 +02:00
}
2019-11-13 19:35:39 +01:00
$out .= '</select>' ;
2013-04-21 17:36:23 +02:00
}
elseif ( $type == 'checkbox' )
{
2019-11-13 19:35:39 +01:00
$value_arr = explode ( ',' , $value );
$out = $form -> multiselectarray ( $keyprefix . $key . $keysuffix , ( empty ( $param [ 'options' ]) ? null : $param [ 'options' ]), $value_arr , '' , 0 , '' , 0 , '100%' );
2013-04-21 17:36:23 +02:00
}
elseif ( $type == 'radio' )
{
2019-11-13 19:35:39 +01:00
$out = '' ;
2017-09-03 15:21:17 +02:00
foreach ( $param [ 'options' ] as $keyopt => $val )
2013-04-21 17:36:23 +02:00
{
2019-11-13 19:35:39 +01:00
$out .= '<input class="flat ' . $morecss . '" type="radio" name="' . $keyprefix . $key . $keysuffix . '" id="' . $keyprefix . $key . $keysuffix . '" ' . ( $moreparam ? $moreparam : '' );
$out .= ' value="' . $keyopt . '"' ;
$out .= ' id="' . $keyprefix . $key . $keysuffix . '_' . $keyopt . '"' ;
$out .= ( $value == $keyopt ? 'checked' : '' );
$out .= '/><label for="' . $keyprefix . $key . $keysuffix . '_' . $keyopt . '">' . $val . '</label><br>' ;
2013-04-21 17:36:23 +02:00
}
}
2015-01-18 16:02:31 +01:00
elseif ( $type == 'chkbxlst' )
{
2015-05-18 13:13:50 +02:00
if ( is_array ( $value )) {
$value_arr = $value ;
}
else {
$value_arr = explode ( ',' , $value );
}
2015-03-10 18:33:14 +01:00
2015-01-18 16:02:31 +01:00
if ( is_array ( $param [ 'options' ])) {
$param_list = array_keys ( $param [ 'options' ]);
$InfoFieldList = explode ( " : " , $param_list [ 0 ]);
2019-11-13 19:35:39 +01:00
$parentName = '' ;
$parentField = '' ;
2015-01-18 16:02:31 +01:00
// 0 : tableName
// 1 : label field name
// 2 : key fields name (if differ of rowid)
// 3 : key field parent (for dependent lists)
// 4 : where clause filter on column or table extrafield, syntax field='value' or extra.field=value
2019-04-02 11:01:46 +02:00
// 5 : id category type
// 6 : ids categories list separated by comma for category root
2019-11-13 19:35:39 +01:00
$keyList = ( empty ( $InfoFieldList [ 2 ]) ? 'rowid' : $InfoFieldList [ 2 ] . ' as rowid' );
2015-03-10 18:33:14 +01:00
2019-11-13 19:35:39 +01:00
if ( count ( $InfoFieldList ) > 3 && ! empty ( $InfoFieldList [ 3 ])) {
list ( $parentName , $parentField ) = explode ( '|' , $InfoFieldList [ 3 ]);
$keyList .= ', ' . $parentField ;
2015-01-18 16:02:31 +01:00
}
2019-11-13 19:35:39 +01:00
if ( count ( $InfoFieldList ) > 4 && ! empty ( $InfoFieldList [ 4 ])) {
2015-01-18 16:02:31 +01:00
if ( strpos ( $InfoFieldList [ 4 ], 'extra.' ) !== false ) {
2019-11-13 19:35:39 +01:00
$keyList = 'main.' . $InfoFieldList [ 2 ] . ' as rowid' ;
2015-01-18 16:02:31 +01:00
} else {
2019-11-13 19:35:39 +01:00
$keyList = $InfoFieldList [ 2 ] . ' as rowid' ;
2015-01-18 16:02:31 +01:00
}
}
2015-03-10 18:33:14 +01:00
2019-04-02 11:01:46 +02:00
$filter_categorie = false ;
if ( count ( $InfoFieldList ) > 5 ) {
if ( $InfoFieldList [ 0 ] == 'categorie' ) {
$filter_categorie = true ;
}
}
2016-04-20 17:30:40 +02:00
2019-04-02 11:01:46 +02:00
if ( $filter_categorie === false ) {
$fields_label = explode ( '|' , $InfoFieldList [ 1 ]);
if ( is_array ( $fields_label )) {
$keyList .= ', ' ;
$keyList .= implode ( ', ' , $fields_label );
}
2017-06-07 11:38:42 +02:00
2019-04-02 11:01:46 +02:00
$sqlwhere = '' ;
2019-11-13 19:35:39 +01:00
$sql = 'SELECT ' . $keyList ;
$sql .= ' FROM ' . MAIN_DB_PREFIX . $InfoFieldList [ 0 ];
2019-04-02 11:01:46 +02:00
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 ]);
2019-07-28 00:32:29 +02:00
} elseif ( preg_match ( " #^.*list.php $ # " , $_SERVER [ " PHP_SELF " ])) {
2019-04-02 11:01:46 +02:00
// Pattern for word=$ID$
$word = '\b[a-zA-Z0-9-\.-_]+\b=\$ID\$' ;
// Removing space arount =, ( and )
$InfoFieldList [ 4 ] = preg_replace ( '# *(=|\(|\)) *#' , '$1' , $InfoFieldList [ 4 ]);
$nbPreg = 1 ;
// While we have parenthesis
while ( $nbPreg != 0 ) {
// Init des compteurs
$nbPregRepl = $nbPregSel = 0 ;
// On retire toutes les parenthèses sans = avant
2019-11-13 19:35:39 +01:00
$InfoFieldList [ 4 ] = preg_replace ( '#([^=])(\([^)^(]*(' . $word . ')[^)^(]*\))#' , '$1 $3 ' , $InfoFieldList [ 4 ], - 1 , $nbPregRepl );
2019-04-02 11:01:46 +02:00
// On retire les espaces autour des = et parenthèses
$InfoFieldList [ 4 ] = preg_replace ( '# *(=|\(|\)) *#' , '$1' , $InfoFieldList [ 4 ]);
// On retire toutes les parenthèses avec = avant
2019-11-13 19:35:39 +01:00
$InfoFieldList [ 4 ] = preg_replace ( '#\b[a-zA-Z0-9-\.-_]+\b=\([^)^(]*(' . $word . ')[^)^(]*\)#' , '$1 ' , $InfoFieldList [ 4 ], - 1 , $nbPregSel );
2019-04-02 11:01:46 +02:00
// On retire les espaces autour des = et parenthèses
$InfoFieldList [ 4 ] = preg_replace ( '# *(=|\(|\)) *#' , '$1' , $InfoFieldList [ 4 ]);
// Calcul du compteur général pour la boucle
$nbPreg = $nbPregRepl + $nbPregSel ;
}
// Si l'on a un AND ou un OR, avant ou après
2019-11-13 19:35:39 +01:00
preg_match ( '#(AND|OR|) *(' . $word . ') *(AND|OR|)#' , $InfoFieldList [ 4 ], $matchCondition );
2019-04-02 11:01:46 +02:00
while ( ! empty ( $matchCondition [ 0 ])) {
// If the two sides differ but are not empty
if ( ! empty ( $matchCondition [ 1 ]) && ! empty ( $matchCondition [ 3 ]) && $matchCondition [ 1 ] != $matchCondition [ 3 ]) {
// Nobody sain would do that without parentheses
$InfoFieldList [ 4 ] = str_replace ( '$ID$' , '0' , $InfoFieldList [ 4 ]);
} else {
if ( ! empty ( $matchCondition [ 1 ])) {
2019-07-28 17:51:15 +02:00
$boolCond = (( $matchCondition [ 1 ] == " AND " ) ? ' AND TRUE ' : ' OR FALSE ' );
2019-11-13 19:35:39 +01:00
$InfoFieldList [ 4 ] = str_replace ( $matchCondition [ 0 ], $boolCond . $matchCondition [ 3 ], $InfoFieldList [ 4 ]);
2019-04-02 11:01:46 +02:00
} elseif ( ! empty ( $matchCondition [ 3 ])) {
2019-07-28 17:51:15 +02:00
$boolCond = (( $matchCondition [ 3 ] == " AND " ) ? ' TRUE AND ' : ' FALSE OR' );
2019-04-02 11:01:46 +02:00
$InfoFieldList [ 4 ] = str_replace ( $matchCondition [ 0 ], $boolCond , $InfoFieldList [ 4 ]);
} else {
2019-07-28 17:51:15 +02:00
$InfoFieldList [ 4 ] = " TRUE " ;
2019-04-02 11:01:46 +02:00
}
}
// Si l'on a un AND ou un OR, avant ou après
2019-11-13 19:35:39 +01:00
preg_match ( '#(AND|OR|) *(' . $word . ') *(AND|OR|)#' , $InfoFieldList [ 4 ], $matchCondition );
2019-04-02 11:01:46 +02:00
}
} else {
$InfoFieldList [ 4 ] = str_replace ( '$ID$' , '0' , $InfoFieldList [ 4 ]);
}
// We have to join on extrafield table
2020-01-29 15:35:46 +01:00
if ( strpos ( $InfoFieldList [ 4 ], 'extra.' ) !== false ) {
2019-11-13 19:35:39 +01:00
$sql .= ' as main, ' . MAIN_DB_PREFIX . $InfoFieldList [ 0 ] . '_extrafields as extra' ;
$sqlwhere .= ' WHERE extra.fk_object=main.' . $InfoFieldList [ 2 ] . ' AND ' . $InfoFieldList [ 4 ];
2019-04-02 11:01:46 +02:00
} else {
2019-11-13 19:35:39 +01:00
$sqlwhere .= ' WHERE ' . $InfoFieldList [ 4 ];
2019-04-02 11:01:46 +02:00
}
} 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' ))) {
2019-11-13 19:35:39 +01:00
$sqlwhere .= ' AND entity = ' . $conf -> entity ;
2019-04-02 11:01:46 +02:00
}
// $sql.=preg_replace('/^ AND /','',$sqlwhere);
// print $sql;
$sql .= $sqlwhere ;
2019-11-13 19:35:39 +01:00
dol_syslog ( get_class ( $this ) . '::showInputField type=chkbxlst' , LOG_DEBUG );
2019-04-02 11:01:46 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql ) {
$num = $this -> db -> num_rows ( $resql );
$i = 0 ;
$data = array ();
while ( $i < $num ) {
$labeltoshow = '' ;
$obj = $this -> db -> fetch_object ( $resql );
$notrans = false ;
// Several field into label (eq table:code|libelle:rowid)
$fields_label = explode ( '|' , $InfoFieldList [ 1 ]);
if ( is_array ( $fields_label )) {
$notrans = true ;
foreach ( $fields_label as $field_toshow ) {
2019-11-13 19:35:39 +01:00
$labeltoshow .= $obj -> $field_toshow . ' ' ;
2019-04-02 11:01:46 +02:00
}
} else {
$labeltoshow = $obj -> { $InfoFieldList [ 1 ]};
}
$labeltoshow = dol_trunc ( $labeltoshow , 45 );
if ( is_array ( $value_arr ) && in_array ( $obj -> rowid , $value_arr )) {
foreach ( $fields_label as $field_toshow ) {
$translabel = $langs -> trans ( $obj -> $field_toshow );
if ( $translabel != $obj -> $field_toshow ) {
2019-11-13 19:35:39 +01:00
$labeltoshow = dol_trunc ( $translabel , 18 ) . ' ' ;
2019-04-02 11:01:46 +02:00
} else {
2019-11-13 19:35:39 +01:00
$labeltoshow = dol_trunc ( $obj -> $field_toshow , 18 ) . ' ' ;
2019-04-02 11:01:46 +02:00
}
}
$data [ $obj -> rowid ] = $labeltoshow ;
} else {
if ( ! $notrans ) {
$translabel = $langs -> trans ( $obj -> { $InfoFieldList [ 1 ]});
if ( $translabel != $obj -> { $InfoFieldList [ 1 ]}) {
$labeltoshow = dol_trunc ( $translabel , 18 );
} else {
$labeltoshow = dol_trunc ( $obj -> { $InfoFieldList [ 1 ]}, 18 );
}
}
if ( empty ( $labeltoshow ))
$labeltoshow = '(not defined)' ;
if ( is_array ( $value_arr ) && in_array ( $obj -> rowid , $value_arr )) {
$data [ $obj -> rowid ] = $labeltoshow ;
}
if ( ! empty ( $InfoFieldList [ 3 ]) && $parentField ) {
2019-11-13 19:35:39 +01:00
$parent = $parentName . ':' . $obj -> { $parentField };
2019-04-02 11:01:46 +02:00
}
$data [ $obj -> rowid ] = $labeltoshow ;
}
$i ++ ;
}
$this -> db -> free ( $resql );
2020-04-10 10:59:32 +02:00
$out = $form -> multiselectarray ( $keyprefix . $key . $keysuffix , $data , $value_arr , '' , 0 , '' , 0 , '100%' );
2019-04-02 11:01:46 +02:00
} else {
2020-04-10 10:59:32 +02:00
print 'Error in request ' . $sql . ' ' . $this -> db -> lasterror () . '. Check setup of extra parameters.<br>' ;
2019-04-02 11:01:46 +02:00
}
} else {
2020-04-10 10:59:32 +02:00
require_once DOL_DOCUMENT_ROOT . '/categories/class/categorie.class.php' ;
2019-04-02 11:01:46 +02:00
$data = $form -> select_all_categories ( Categorie :: $MAP_ID_TO_CODE [ $InfoFieldList [ 5 ]], '' , 'parent' , 64 , $InfoFieldList [ 6 ], 1 , 1 );
2020-04-10 10:59:32 +02:00
$out = $form -> multiselectarray ( $keyprefix . $key . $keysuffix , $data , $value_arr , '' , 0 , '' , 0 , '100%' );
2019-04-02 11:01:46 +02:00
}
2015-01-18 16:02:31 +01:00
}
}
2015-03-14 12:31:22 +01:00
elseif ( $type == 'link' )
{
2020-04-10 10:59:32 +02:00
$param_list = array_keys ( $param [ 'options' ]); // $param_list='ObjectName:classPath'
$showempty = (( $required && $default != '' ) ? 0 : 1 );
$out = $form -> selectForForms ( $param_list [ 0 ], $keyprefix . $key . $keysuffix , $value , $showempty , '' , '' , $morecss );
2015-03-14 12:31:22 +01:00
}
2016-06-28 10:21:10 +02:00
elseif ( $type == 'password' )
{
2017-09-30 18:52:33 +02:00
// If prefix is 'search_', field is used as a filter, we use a common text field.
2020-04-10 10:59:32 +02:00
$out = '<input style="display:none" type="text" name="fakeusernameremembered">' ; // Hidden field to reduce impact of evil Google Chrome autopopulate bug.
$out .= '<input autocomplete="new-password" type="' . ( $keyprefix == 'search_' ? 'text' : 'password' ) . '" class="flat ' . $morecss . '" name="' . $keyprefix . $key . $keysuffix . '" id="' . $keyprefix . $key . $keysuffix . '" value="' . $value . '" ' . ( $moreparam ? $moreparam : '' ) . '>' ;
2016-06-28 10:21:10 +02:00
}
2016-04-20 17:30:40 +02:00
if ( ! empty ( $hidden )) {
2020-04-10 10:59:32 +02:00
$out = '<input type="hidden" value="' . $value . '" name="' . $keyprefix . $key . $keysuffix . '" id="' . $keyprefix . $key . $keysuffix . '"/>' ;
2016-04-20 17:30:40 +02:00
}
2013-04-21 17:36:23 +02:00
/* Add comments
if ( $type == 'date' ) $out .= ' (YYYY-MM-DD)' ;
2017-10-16 21:05:12 +02:00
elseif ( $type == 'datetime' ) $out .= ' (YYYY-MM-DD HH:MM:SS)' ;
*/
2020-03-26 20:05:17 +01:00
/* if ( ! empty ( $help ) && $keyprefix != 'search_options_' ) {
2019-10-02 18:14:41 +02:00
$out .= $form -> textwithpicto ( '' , $help , 1 , 'help' , '' , 0 , 3 );
2020-03-26 20:05:17 +01:00
} */
2013-04-21 17:36:23 +02:00
return $out ;
2011-06-22 13:41:37 +02:00
}
2017-10-16 21:05:12 +02:00
2013-04-21 17:36:23 +02:00
/**
* Return HTML string to put an output field into a page
*
2018-01-31 14:01:58 +01:00
* @ param string $key Key of attribute
* @ param string $value Value to show
* @ param string $moreparam To add more parameters on html input tag ( only checkbox use html input for output rendering )
2019-11-02 17:26:26 +01:00
* @ param string $extrafieldsobjectkey If defined ( for example $object -> table_element ), function uses the new method to get extrafields data
2018-01-31 14:01:58 +01:00
* @ return string Formated value
2013-04-21 17:36:23 +02:00
*/
2019-02-27 20:45:07 +01:00
public function showOutputField ( $key , $value , $moreparam = '' , $extrafieldsobjectkey = '' )
2013-04-21 17:36:23 +02:00
{
2020-01-30 01:48:28 +01:00
global $conf , $langs ;
if ( ! empty ( $extrafieldsobjectkey ))
{
$label = $this -> attributes [ $extrafieldsobjectkey ][ 'label' ][ $key ];
$type = $this -> attributes [ $extrafieldsobjectkey ][ 'type' ][ $key ];
$size = $this -> attributes [ $extrafieldsobjectkey ][ 'size' ][ $key ];
$default = $this -> attributes [ $extrafieldsobjectkey ][ 'default' ][ $key ];
$computed = $this -> attributes [ $extrafieldsobjectkey ][ 'computed' ][ $key ];
$unique = $this -> attributes [ $extrafieldsobjectkey ][ 'unique' ][ $key ];
$required = $this -> attributes [ $extrafieldsobjectkey ][ 'required' ][ $key ];
$param = $this -> attributes [ $extrafieldsobjectkey ][ 'param' ][ $key ];
$perms = dol_eval ( $this -> attributes [ $extrafieldsobjectkey ][ 'perms' ][ $key ], 1 );
$langfile = $this -> attributes [ $extrafieldsobjectkey ][ 'langfile' ][ $key ];
$list = dol_eval ( $this -> attributes [ $extrafieldsobjectkey ][ 'list' ][ $key ], 1 );
$help = $this -> attributes [ $extrafieldsobjectkey ][ 'help' ][ $key ];
$hidden = ( empty ( $list ) ? 1 : 0 ); // If $list empty, we are sure it is hidden, otherwise we show. If it depends on mode (view/create/edit form or list, this must be filtered by caller)
2018-01-31 14:01:58 +01:00
}
2018-02-27 17:30:13 +01:00
else // Old usage
2018-01-31 14:01:58 +01:00
{
2019-11-02 17:26:26 +01:00
//dol_syslog("Warning: parameter 'extrafieldsobjectkey' is missing", LOG_WARNING);
2020-01-30 01:48:28 +01:00
$label = $this -> attribute_label [ $key ];
$type = $this -> attribute_type [ $key ];
$size = $this -> attribute_size [ $key ];
$default = $this -> attribute_default [ $key ];
$computed = $this -> attribute_computed [ $key ];
$unique = $this -> attribute_unique [ $key ];
$required = $this -> attribute_required [ $key ];
$param = $this -> attribute_param [ $key ];
$perms = dol_eval ( $this -> attribute_perms [ $key ], 1 );
$langfile = $this -> attribute_langfile [ $key ];
$list = dol_eval ( $this -> attribute_list [ $key ], 1 );
$help = '' ; // Not supported with old syntax
$hidden = ( empty ( $list ) ? 1 : 0 ); // If $list empty, we are sure it is hidden, otherwise we show. If it depends on mode (view/create/edit form or list, this must be filtered by caller)
}
if ( $hidden ) return '' ; // This is a protection. If field is hidden, we should just not call this method.
2015-03-10 22:27:16 +01:00
2019-07-11 22:18:44 +02:00
//if ($computed) $value = // $value is already calculated into $value before calling this method
2019-08-21 03:38:57 +02:00
2020-01-30 01:48:28 +01:00
$showsize = 0 ;
2013-04-21 17:36:23 +02:00
if ( $type == 'date' )
{
2020-01-30 01:48:28 +01:00
$showsize = 10 ;
$value = dol_print_date ( $value , 'day' );
2013-04-21 17:36:23 +02:00
}
elseif ( $type == 'datetime' )
{
2020-01-30 01:48:28 +01:00
$showsize = 19 ;
$value = dol_print_date ( $value , 'dayhour' );
2013-04-21 17:36:23 +02:00
}
elseif ( $type == 'int' )
{
2020-01-30 01:48:28 +01:00
$showsize = 10 ;
2013-04-21 17:36:23 +02:00
}
2013-06-10 14:13:44 +02:00
elseif ( $type == 'double' )
{
if ( ! empty ( $value )) {
2019-06-28 12:56:31 +02:00
//$value=price($value);
$sizeparts = explode ( " , " , $size );
2019-06-28 07:10:28 +02:00
$number_decimals = $sizeparts [ 1 ];
2019-11-13 19:35:39 +01:00
$value = price ( $value , 0 , $langs , 0 , 0 , $number_decimals , '' );
2013-06-10 14:13:44 +02:00
}
}
2013-04-21 17:36:23 +02:00
elseif ( $type == 'boolean' )
{
2019-11-13 19:35:39 +01:00
$checked = '' ;
2013-04-21 17:36:23 +02:00
if ( ! empty ( $value )) {
2019-11-13 19:35:39 +01:00
$checked = ' checked ' ;
2013-04-21 17:36:23 +02:00
}
2019-11-13 19:35:39 +01:00
$value = '<input type="checkbox" ' . $checked . ' ' . ( $moreparam ? $moreparam : '' ) . ' readonly disabled>' ;
2013-04-21 17:36:23 +02:00
}
elseif ( $type == 'mail' )
{
2019-11-13 19:35:39 +01:00
$value = dol_print_email ( $value , 0 , 0 , 0 , 64 , 1 , 1 );
2013-04-21 17:36:23 +02:00
}
2016-11-08 17:21:26 +01:00
elseif ( $type == 'url' )
{
2019-11-13 19:35:39 +01:00
$value = dol_print_url ( $value , '_blank' , 32 , 1 );
2016-11-08 17:21:26 +01:00
}
2013-04-21 17:36:23 +02:00
elseif ( $type == 'phone' )
{
2019-11-13 19:35:39 +01:00
$value = dol_print_phone ( $value , '' , 0 , 0 , '' , ' ' , 'phone' );
2013-04-21 17:36:23 +02:00
}
elseif ( $type == 'price' )
{
2020-04-28 14:22:52 +02:00
//$value = price($value, 0, $langs, 0, 0, -1, $conf->currency);
if ( $value || $value == '0' ) $value = price ( $value , 0 , $langs , 0 , 0 , - 1 );
2013-04-21 17:36:23 +02:00
}
elseif ( $type == 'select' )
{
2020-05-28 16:11:02 +02:00
$valstr = $param [ 'options' ][ $value ];
if (( $pos = strpos ( $valstr , " | " )) !== false )
{
2020-05-28 16:18:00 +02:00
$valstr = substr ( $valstr , 0 , $pos );
2020-05-28 16:11:02 +02:00
}
if ( $langfile && $valstr ) $value = $langs -> trans ( $valstr );
else $value = $valstr ;
2013-04-21 17:36:23 +02:00
}
elseif ( $type == 'sellist' )
{
2019-11-13 19:35:39 +01:00
$param_list = array_keys ( $param [ 'options' ]);
2013-04-24 23:54:02 +02:00
$InfoFieldList = explode ( " : " , $param_list [ 0 ]);
2013-06-27 13:35:28 +02:00
2019-11-13 19:35:39 +01:00
$selectkey = " rowid " ;
$keyList = 'rowid' ;
2013-06-27 13:35:28 +02:00
2019-11-13 19:35:39 +01:00
if ( count ( $InfoFieldList ) >= 3 )
2013-06-27 13:35:28 +02:00
{
$selectkey = $InfoFieldList [ 2 ];
2019-11-13 19:35:39 +01:00
$keyList = $InfoFieldList [ 2 ] . ' as rowid' ;
2013-06-27 13:35:28 +02:00
}
2013-04-21 17:36:23 +02:00
2019-01-27 11:55:16 +01:00
$fields_label = explode ( '|' , $InfoFieldList [ 1 ]);
2019-11-13 19:35:39 +01:00
if ( is_array ( $fields_label )) {
$keyList .= ', ' ;
2013-06-27 13:35:28 +02:00
$keyList .= implode ( ', ' , $fields_label );
}
$sql = 'SELECT ' . $keyList ;
2019-11-13 19:35:39 +01:00
$sql .= ' FROM ' . MAIN_DB_PREFIX . $InfoFieldList [ 0 ];
if ( strpos ( $InfoFieldList [ 4 ], 'extra' ) !== false )
2013-11-15 15:44:04 +01:00
{
2019-11-13 19:35:39 +01:00
$sql .= ' as main' ;
2013-11-15 15:44:04 +01:00
}
2019-11-13 19:35:39 +01:00
if ( $selectkey == 'rowid' && empty ( $value )) {
$sql .= " WHERE " . $selectkey . " =0 " ;
} elseif ( $selectkey == 'rowid' ) {
$sql .= " WHERE " . $selectkey . " = " . $this -> db -> escape ( $value );
} else {
$sql .= " WHERE " . $selectkey . " =' " . $this -> db -> escape ( $value ) . " ' " ;
2016-08-16 08:54:12 +02:00
}
2013-04-25 00:14:43 +02:00
//$sql.= ' AND entity = '.$conf->entity;
2014-02-04 20:49:07 +01:00
2014-06-13 01:34:39 +02:00
dol_syslog ( get_class ( $this ) . ':showOutputField:$type=sellist' , LOG_DEBUG );
2013-04-21 17:36:23 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
2019-11-13 19:35:39 +01:00
$value = '' ; // value was used, so now we reste it to use it to build final output
2014-02-04 20:49:07 +01:00
2013-04-21 17:36:23 +02:00
$obj = $this -> db -> fetch_object ( $resql );
2013-06-10 14:13:44 +02:00
2013-06-27 13:35:28 +02:00
// Several field into label (eq table:code|libelle:rowid)
2019-01-27 11:55:16 +01:00
$fields_label = explode ( '|' , $InfoFieldList [ 1 ]);
2014-02-04 20:49:07 +01:00
2019-11-13 19:35:39 +01:00
if ( is_array ( $fields_label ) && count ( $fields_label ) > 1 )
2013-06-27 13:35:28 +02:00
{
foreach ( $fields_label as $field_toshow )
{
2019-11-13 19:35:39 +01:00
$translabel = '' ;
2014-10-27 19:38:27 +01:00
if ( ! empty ( $obj -> $field_toshow )) {
2019-11-13 19:35:39 +01:00
$translabel = $langs -> trans ( $obj -> $field_toshow );
2014-10-27 19:38:27 +01:00
}
2019-11-13 19:35:39 +01:00
if ( $translabel != $field_toshow ) {
$value .= dol_trunc ( $translabel , 18 ) . ' ' ;
} else {
$value .= $obj -> $field_toshow . ' ' ;
2013-06-27 13:35:28 +02:00
}
}
}
else
{
2019-11-13 19:35:39 +01:00
$translabel = '' ;
2016-02-11 20:01:51 +01:00
if ( ! empty ( $obj -> { $InfoFieldList [ 1 ]})) {
2019-11-13 19:35:39 +01:00
$translabel = $langs -> trans ( $obj -> { $InfoFieldList [ 1 ]});
2014-10-27 19:38:27 +01:00
}
2019-11-13 19:35:39 +01:00
if ( $translabel != $obj -> { $InfoFieldList [ 1 ]}) {
$value = dol_trunc ( $translabel , 18 );
} else {
$value = $obj -> { $InfoFieldList [ 1 ]};
2013-06-27 13:35:28 +02:00
}
}
2013-04-21 17:36:23 +02:00
}
2014-01-30 10:48:10 +01:00
else dol_syslog ( get_class ( $this ) . '::showOutputField error ' . $this -> db -> lasterror (), LOG_WARNING );
2013-04-21 17:36:23 +02:00
}
elseif ( $type == 'radio' )
{
2019-11-13 19:35:39 +01:00
$value = $param [ 'options' ][ $value ];
2013-04-21 17:36:23 +02:00
}
elseif ( $type == 'checkbox' )
{
2019-11-13 19:35:39 +01:00
$value_arr = explode ( ',' , $value );
$value = '' ;
$toprint = array ();
2013-04-21 17:36:23 +02:00
if ( is_array ( $value_arr ))
{
foreach ( $value_arr as $keyval => $valueval ) {
2019-11-13 19:35:39 +01:00
$toprint [] = '<li class="select2-search-choice-dolibarr noborderoncategories" style="background: #aaa">' . $param [ 'options' ][ $valueval ] . '</li>' ;
2013-04-21 17:36:23 +02:00
}
}
2019-11-13 19:35:39 +01:00
$value = '<div class="select2-container-multi-dolibarr" style="width: 90%;"><ul class="select2-choices-dolibarr">' . implode ( ' ' , $toprint ) . '</ul></div>' ;
2013-04-21 17:36:23 +02:00
}
2015-01-18 16:02:31 +01:00
elseif ( $type == 'chkbxlst' )
{
$value_arr = explode ( ',' , $value );
2015-03-10 18:33:14 +01:00
2017-10-26 02:55:43 +02:00
$param_list = array_keys ( $param [ 'options' ]);
2015-01-18 16:02:31 +01:00
$InfoFieldList = explode ( " : " , $param_list [ 0 ]);
2015-03-10 18:33:14 +01:00
2015-01-18 16:02:31 +01:00
$selectkey = " rowid " ;
$keyList = 'rowid' ;
2015-03-10 18:33:14 +01:00
2015-01-18 16:02:31 +01:00
if ( count ( $InfoFieldList ) >= 3 ) {
$selectkey = $InfoFieldList [ 2 ];
2019-11-13 19:35:39 +01:00
$keyList = $InfoFieldList [ 2 ] . ' as rowid' ;
2015-01-18 16:02:31 +01:00
}
2015-03-10 18:33:14 +01:00
2015-01-18 16:02:31 +01:00
$fields_label = explode ( '|' , $InfoFieldList [ 1 ]);
if ( is_array ( $fields_label )) {
$keyList .= ', ' ;
$keyList .= implode ( ', ' , $fields_label );
}
2015-03-10 18:33:14 +01:00
2020-01-30 01:48:28 +01:00
$sql = 'SELECT ' . $keyList ;
$sql .= ' FROM ' . MAIN_DB_PREFIX . $InfoFieldList [ 0 ];
2015-01-18 16:02:31 +01:00
if ( strpos ( $InfoFieldList [ 4 ], 'extra' ) !== false ) {
$sql .= ' as main' ;
}
// $sql.= " WHERE ".$selectkey."='".$this->db->escape($value)."'";
// $sql.= ' AND entity = '.$conf->entity;
2015-03-10 18:33:14 +01:00
2020-01-30 01:48:28 +01:00
dol_syslog ( get_class ( $this ) . ':showOutputField:$type=chkbxlst' , LOG_DEBUG );
2015-01-18 16:02:31 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql ) {
$value = '' ; // value was used, so now we reste it to use it to build final output
2020-01-30 01:48:28 +01:00
$toprint = array ();
while ( $obj = $this -> db -> fetch_object ( $resql )) {
2015-01-18 16:02:31 +01:00
// Several field into label (eq table:code|libelle:rowid)
$fields_label = explode ( '|' , $InfoFieldList [ 1 ]);
if ( is_array ( $value_arr ) && in_array ( $obj -> rowid , $value_arr )) {
if ( is_array ( $fields_label ) && count ( $fields_label ) > 1 ) {
2019-02-02 18:25:01 +01:00
foreach ( $fields_label as $field_toshow ) {
2015-01-18 16:02:31 +01:00
$translabel = '' ;
2019-11-13 19:35:39 +01:00
if ( ! empty ( $obj -> $field_toshow )) {
2015-01-18 16:02:31 +01:00
$translabel = $langs -> trans ( $obj -> $field_toshow );
}
if ( $translabel != $field_toshow ) {
2019-11-13 19:35:39 +01:00
$toprint [] = '<li class="select2-search-choice-dolibarr noborderoncategories" style="background: #aaa">' . dol_trunc ( $translabel , 18 ) . '</li>' ;
2015-01-18 16:02:31 +01:00
} else {
2019-11-13 19:35:39 +01:00
$toprint [] = '<li class="select2-search-choice-dolibarr noborderoncategories" style="background: #aaa">' . $obj -> $field_toshow . '</li>' ;
2015-01-18 16:02:31 +01:00
}
}
} else {
$translabel = '' ;
2019-11-13 19:35:39 +01:00
if ( ! empty ( $obj -> { $InfoFieldList [ 1 ]})) {
2016-02-11 20:01:51 +01:00
$translabel = $langs -> trans ( $obj -> { $InfoFieldList [ 1 ]});
2015-01-18 16:02:31 +01:00
}
2016-02-11 20:01:51 +01:00
if ( $translabel != $obj -> { $InfoFieldList [ 1 ]}) {
2019-11-13 19:35:39 +01:00
$toprint [] = '<li class="select2-search-choice-dolibarr noborderoncategories" style="background: #aaa">' . dol_trunc ( $translabel , 18 ) . '</li>' ;
2015-01-18 16:02:31 +01:00
} else {
2019-11-13 19:35:39 +01:00
$toprint [] = '<li class="select2-search-choice-dolibarr noborderoncategories" style="background: #aaa">' . $obj -> { $InfoFieldList [ 1 ]} . '</li>' ;
2015-01-18 16:02:31 +01:00
}
}
}
}
2019-11-13 19:35:39 +01:00
$value = '<div class="select2-container-multi-dolibarr" style="width: 90%;"><ul class="select2-choices-dolibarr">' . implode ( ' ' , $toprint ) . '</ul></div>' ;
2017-02-03 09:25:26 +01:00
} else {
2019-11-13 19:35:39 +01:00
dol_syslog ( get_class ( $this ) . '::showOutputField error ' . $this -> db -> lasterror (), LOG_WARNING );
2017-02-03 09:25:26 +01:00
}
2015-01-18 16:02:31 +01:00
}
2015-03-14 12:31:22 +01:00
elseif ( $type == 'link' )
{
2019-11-13 19:35:39 +01:00
$out = '' ;
2018-01-31 14:01:58 +01:00
2018-02-01 19:03:21 +01:00
// Only if something to display (perf)
2019-04-25 01:16:29 +02:00
if ( $value ) // If we have -1 here, pb is into insert, not into ouptut (fix insert instead of changing code here to compensate)
2015-03-14 12:31:22 +01:00
{
2019-11-13 19:35:39 +01:00
$param_list = array_keys ( $param [ 'options' ]); // $param_list='ObjectName:classPath'
2017-10-26 02:55:43 +02:00
2015-03-18 13:23:14 +01:00
$InfoFieldList = explode ( " : " , $param_list [ 0 ]);
2019-11-13 19:35:39 +01:00
$classname = $InfoFieldList [ 0 ];
$classpath = $InfoFieldList [ 1 ];
if ( ! empty ( $classpath ))
2017-10-26 02:55:43 +02:00
{
dol_include_once ( $InfoFieldList [ 1 ]);
if ( $classname && class_exists ( $classname ))
{
$object = new $classname ( $this -> db );
$object -> fetch ( $value );
2019-11-13 19:35:39 +01:00
$value = $object -> getNomUrl ( 3 );
2017-10-26 02:55:43 +02:00
}
}
else
{
dol_syslog ( 'Error bad setup of extrafield' , LOG_WARNING );
return 'Error bad setup of extrafield' ;
}
2015-03-14 12:31:22 +01:00
}
}
2015-07-28 18:57:13 +02:00
elseif ( $type == 'text' )
2015-07-21 16:39:00 +02:00
{
2019-11-13 19:35:39 +01:00
$value = dol_htmlentitiesbr ( $value );
2015-07-21 16:39:00 +02:00
}
2018-02-27 17:30:13 +01:00
elseif ( $type == 'html' )
{
2019-11-13 19:35:39 +01:00
$value = dol_htmlentitiesbr ( $value );
2018-02-27 17:30:13 +01:00
}
2016-06-28 10:21:10 +02:00
elseif ( $type == 'password' )
{
2019-11-13 19:35:39 +01:00
$value = dol_trunc ( preg_replace ( '/./i' , '*' , $value ), 8 , 'right' , 'UTF-8' , 1 );
2016-06-28 10:21:10 +02:00
}
2013-04-21 17:36:23 +02:00
else
{
2019-11-13 19:35:39 +01:00
$showsize = round ( $size );
if ( $showsize > 48 ) $showsize = 48 ;
2013-04-21 17:36:23 +02:00
}
2016-04-20 17:30:40 +02:00
2013-04-21 17:36:23 +02:00
//print $type.'-'.$size;
2019-11-13 19:35:39 +01:00
$out = $value ;
2016-04-20 17:30:40 +02:00
2013-04-21 17:36:23 +02:00
return $out ;
}
2015-10-29 22:12:11 +01:00
/**
* Return tag to describe alignement to use for this extrafield
*
2018-01-31 14:01:58 +01:00
* @ param string $key Key of attribute
* @ param string $extrafieldsobjectkey If defined , use the new method to get extrafields data
* @ return string Formated value
2015-10-29 22:12:11 +01:00
*/
2019-02-27 20:45:07 +01:00
public function getAlignFlag ( $key , $extrafieldsobjectkey = '' )
2015-10-29 22:12:11 +01:00
{
2019-11-13 19:35:39 +01:00
global $conf , $langs ;
2015-10-29 22:12:11 +01:00
2019-11-13 19:35:39 +01:00
if ( ! empty ( $extrafieldsobjectkey )) $type = $this -> attributes [ $extrafieldsobjectkey ][ 'type' ][ $key ];
else $type = $this -> attribute_type [ $key ];
2015-10-29 22:12:11 +01:00
2019-11-13 19:35:39 +01:00
$align = '' ;
2016-04-20 17:30:40 +02:00
2018-07-26 09:41:44 +02:00
if ( $type == 'date' )
2015-10-29 22:12:11 +01:00
{
2019-11-13 19:35:39 +01:00
$align = " center " ;
2015-10-29 22:12:11 +01:00
}
elseif ( $type == 'datetime' )
{
2019-11-13 19:35:39 +01:00
$align = " center " ;
2015-10-29 22:12:11 +01:00
}
elseif ( $type == 'int' )
{
2019-11-13 19:35:39 +01:00
$align = " right " ;
2015-10-29 22:12:11 +01:00
}
2020-03-19 11:53:42 +01:00
elseif ( $type == 'price' )
{
2020-04-10 10:59:32 +02:00
$align = " right " ;
2020-03-19 11:53:42 +01:00
}
2015-10-29 22:12:11 +01:00
elseif ( $type == 'double' )
{
2019-11-13 19:35:39 +01:00
$align = " right " ;
2015-10-29 22:12:11 +01:00
}
elseif ( $type == 'boolean' )
{
2019-11-13 19:35:39 +01:00
$align = " center " ;
2015-10-29 22:12:11 +01:00
}
elseif ( $type == 'radio' )
{
2019-11-13 19:35:39 +01:00
$align = " center " ;
2015-10-29 22:12:11 +01:00
}
elseif ( $type == 'checkbox' )
{
2019-11-13 19:35:39 +01:00
$align = " center " ;
2015-10-29 22:12:11 +01:00
}
2018-07-26 09:41:44 +02:00
elseif ( $type == 'price' )
{
2019-11-13 19:35:39 +01:00
$align = " right " ;
2018-07-26 09:41:44 +02:00
}
2016-04-20 17:30:40 +02:00
2015-10-29 22:12:11 +01:00
return $align ;
}
2016-04-20 17:30:40 +02:00
2013-04-21 17:36:23 +02:00
/**
* Return HTML string to print separator extrafield
*
* @ param string $key Key of attribute
2018-04-13 10:48:29 +02:00
* @ param string $object Object
2019-11-16 14:38:05 +01:00
* @ param int $colspan Value of colspan to use ( it must includes the first column with title )
2018-04-13 10:48:29 +02:00
* @ return string HTML code with line for separator
2013-04-21 17:36:23 +02:00
*/
2019-11-16 14:38:05 +01:00
public function showSeparator ( $key , $object , $colspan = 2 )
2013-04-21 17:36:23 +02:00
{
2018-04-13 10:48:29 +02:00
global $langs ;
2019-11-16 14:38:05 +01:00
$out = '<tr id="trextrafieldseparator' . $key . '" class="trextrafieldseparator trextrafieldseparator' . $key . '"><td colspan="' . $colspan . '"><strong>' ;
2019-11-13 19:35:39 +01:00
$out .= $langs -> trans ( $this -> attributes [ $object -> table_element ][ 'label' ][ $key ]);
$out .= '</strong></td></tr>' ;
2019-05-03 16:14:44 +02:00
$extrafield_param = $this -> attributes [ $object -> table_element ][ 'param' ][ $key ];
if ( ! empty ( $extrafield_param ) && is_array ( $extrafield_param )) {
$extrafield_param_list = array_keys ( $extrafield_param [ 'options' ]);
if ( count ( $extrafield_param_list ) > 0 ) {
$extrafield_collapse_display_value = intval ( $extrafield_param_list [ 0 ]);
if ( $extrafield_collapse_display_value == 1 || $extrafield_collapse_display_value == 2 ) {
2019-08-21 03:38:57 +02:00
// Set the collapse_display status to cookie in priority or if ignorecollapsesetup is 1, if cookie and ignorecollapsesetup not defined, use the setup.
$collapse_display = (( isset ( $_COOKIE [ 'DOLCOLLAPSE_' . $object -> table_element . '_extrafields_' . $key ]) || GETPOST ( 'ignorecollapsesetup' , 'int' )) ? ( $_COOKIE [ 'DOLCOLLAPSE_' . $object -> table_element . '_extrafields_' . $key ] ? true : false ) : ( $extrafield_collapse_display_value == 2 ? false : true ));
2019-05-03 16:14:44 +02:00
$extrafields_collapse_num = $this -> attributes [ $object -> table_element ][ 'pos' ][ $key ];
2019-08-21 03:38:57 +02:00
$out .= '<!-- Add js script to manage the collpase/uncollapse of extrafields separators ' . $key . ' -->' ;
2019-05-03 16:14:44 +02:00
$out .= '<script type="text/javascript">' ;
$out .= 'jQuery(document).ready(function(){' ;
if ( $collapse_display === false ) {
2019-11-13 19:35:39 +01:00
$out .= ' jQuery("#trextrafieldseparator' . $key . ' td").prepend("<span class=\"cursorpointer fa fa-plus-square\"></span> ");' . " \n " ;
$out .= ' jQuery(".trextrafields_collapse' . $extrafields_collapse_num . '").hide();' . " \n " ;
2019-05-03 16:14:44 +02:00
} else {
2019-11-13 19:35:39 +01:00
$out .= ' jQuery("#trextrafieldseparator' . $key . ' td").prepend("<span class=\"cursorpointer fa fa-minus-square\"></span> ");' . " \n " ;
2019-08-21 03:38:57 +02:00
$out .= ' document.cookie = "DOLCOLLAPSE_' . $object -> table_element . '_extrafields_' . $key . '=1; path=' . $_SERVER [ " PHP_SELF " ] . '"' . " \n " ;
2019-05-03 16:14:44 +02:00
}
2019-11-13 19:35:39 +01:00
$out .= ' jQuery("#trextrafieldseparator' . $key . '").click(function(){' . " \n " ;
$out .= ' jQuery(".trextrafields_collapse' . $extrafields_collapse_num . '").toggle(300, function(){' . " \n " ;
$out .= ' if (jQuery(".trextrafields_collapse' . $extrafields_collapse_num . '").is(":hidden")) {' . " \n " ;
$out .= ' jQuery("#trextrafieldseparator' . $key . ' td span").addClass("fa-plus-square").removeClass("fa-minus-square");' . " \n " ;
2019-08-21 03:38:57 +02:00
$out .= ' document.cookie = "DOLCOLLAPSE_' . $object -> table_element . '_extrafields_' . $key . '=0; path=' . $_SERVER [ " PHP_SELF " ] . '"' . " \n " ;
$out .= ' } else {' . " \n " ;
2019-11-13 19:35:39 +01:00
$out .= ' jQuery("#trextrafieldseparator' . $key . ' td span").addClass("fa-minus-square").removeClass("fa-plus-square");' . " \n " ;
2019-08-21 03:38:57 +02:00
$out .= ' document.cookie = "DOLCOLLAPSE_' . $object -> table_element . '_extrafields_' . $key . '=1; path=' . $_SERVER [ " PHP_SELF " ] . '"' . " \n " ;
$out .= ' }' . " \n " ;
2019-05-03 16:14:44 +02:00
$out .= ' });' ;
$out .= ' });' ;
$out .= '});' ;
$out .= '</script>' ;
}
}
}
2013-04-21 17:36:23 +02:00
return $out ;
}
/**
2013-11-05 13:11:36 +01:00
* Fill array_options property of object by extrafields value ( using for data sent by forms )
2013-04-21 17:36:23 +02:00
*
2019-10-06 14:41:52 +02:00
* @ param array $extralabels Deprecated ( old $array of extrafields , now set this to null )
2015-02-22 17:41:02 +01:00
* @ param object $object Object
2020-07-04 17:09:20 +02:00
* @ param string $onlykey Only some keys are filled : $this
* 'string' => When we make update of only one extrafield ( $action = 'update_extras' ), calling page can set this to avoid to have other extrafields being reset .
* '@GETPOSTISSET' => When we make update of extrafields ( $action = 'update' ), calling page can set this to avoid to have fields not into POST being reset .
2015-02-26 14:25:30 +01:00
* @ return int 1 if array_options set , 0 if no value , - 1 if error ( field required missing for example )
2013-04-21 17:36:23 +02:00
*/
2019-02-27 20:45:07 +01:00
public function setOptionalsFromPost ( $extralabels , & $object , $onlykey = '' )
2013-04-21 17:36:23 +02:00
{
2013-07-26 10:46:45 +02:00
global $_POST , $langs ;
2019-10-06 14:41:52 +02:00
2019-11-13 19:35:39 +01:00
$nofillrequired = 0 ; // For error when required field left blank
2013-07-26 10:46:45 +02:00
$error_field_required = array ();
2013-06-10 14:13:44 +02:00
2019-11-13 19:35:39 +01:00
if ( is_array ( $this -> attributes [ $object -> table_element ][ 'label' ])) $extralabels = $this -> attributes [ $object -> table_element ][ 'label' ];
2018-04-12 23:16:23 +02:00
2013-04-21 17:36:23 +02:00
if ( is_array ( $extralabels ))
{
// Get extra fields
foreach ( $extralabels as $key => $value )
{
2020-07-04 17:09:20 +02:00
if ( ! empty ( $onlykey ) && $onlykey != '@GETPOSTISSET' && $key != $onlykey ) continue ;
if ( ! empty ( $onlykey ) && $onlykey == '@GETPOSTISSET' && ! GETPOSTISSET ( 'options_' . $key )) continue ;
2013-11-05 13:11:36 +01:00
2018-04-12 23:16:23 +02:00
$key_type = $this -> attributes [ $object -> table_element ][ 'type' ][ $key ];
if ( $key_type == 'separate' ) continue ;
$enabled = 1 ;
if ( isset ( $this -> attributes [ $object -> table_element ][ 'list' ][ $key ]))
{
$enabled = dol_eval ( $this -> attributes [ $object -> table_element ][ 'list' ][ $key ], 1 );
}
$perms = 1 ;
if ( isset ( $this -> attributes [ $object -> table_element ][ 'perms' ][ $key ]))
2013-07-26 10:46:45 +02:00
{
2018-04-12 23:16:23 +02:00
$perms = dol_eval ( $this -> attributes [ $object -> table_element ][ 'perms' ][ $key ], 1 );
}
if ( empty ( $enabled )) continue ;
if ( empty ( $perms )) continue ;
2018-10-08 14:26:18 +02:00
if ( $this -> attributes [ $object -> table_element ][ 'required' ][ $key ]) // Value is required
2018-04-12 23:16:23 +02:00
{
2018-10-08 14:26:18 +02:00
// Check if empty without using GETPOST, value can be alpha, int, array, etc...
2019-11-13 19:35:39 +01:00
if (( ! is_array ( $_POST [ " options_ " . $key ]) && empty ( $_POST [ " options_ " . $key ]) && $this -> attributes [ $object -> table_element ][ 'type' ][ $key ] != 'select' && $_POST [ " options_ " . $key ] != '0' )
|| ( ! is_array ( $_POST [ " options_ " . $key ]) && empty ( $_POST [ " options_ " . $key ]) && $this -> attributes [ $object -> table_element ][ 'type' ][ $key ] == 'select' )
2018-10-08 14:26:18 +02:00
|| ( is_array ( $_POST [ " options_ " . $key ]) && empty ( $_POST [ " options_ " . $key ])))
{
//print 'ccc'.$value.'-'.$this->attributes[$object->table_element]['required'][$key];
$nofillrequired ++ ;
$error_field_required [] = $langs -> transnoentitiesnoconv ( $value );
}
2013-07-26 10:46:45 +02:00
}
2013-04-21 17:36:23 +02:00
2019-01-27 11:55:16 +01:00
if ( in_array ( $key_type , array ( 'date' )))
2018-01-14 03:12:22 +01:00
{
// Clean parameters
// TODO GMT date in memory must be GMT so we should add gm=true in parameters
2020-01-30 01:48:28 +01:00
$value_key = dol_mktime ( 0 , 0 , 0 , $_POST [ " options_ " . $key . " month " ], $_POST [ " options_ " . $key . " day " ], $_POST [ " options_ " . $key . " year " ]);
2018-01-14 03:12:22 +01:00
}
2019-01-27 11:55:16 +01:00
elseif ( in_array ( $key_type , array ( 'datetime' )))
2013-04-21 17:36:23 +02:00
{
// Clean parameters
2018-01-14 03:12:22 +01:00
// TODO GMT date in memory must be GMT so we should add gm=true in parameters
2020-01-30 01:48:28 +01:00
$value_key = dol_mktime ( $_POST [ " options_ " . $key . " hour " ], $_POST [ " options_ " . $key . " min " ], 0 , $_POST [ " options_ " . $key . " month " ], $_POST [ " options_ " . $key . " day " ], $_POST [ " options_ " . $key . " year " ]);
2013-04-21 17:36:23 +02:00
}
2020-01-30 01:48:28 +01:00
elseif ( in_array ( $key_type , array ( 'checkbox' , 'chkbxlst' )))
2013-04-21 17:36:23 +02:00
{
2020-01-30 01:48:28 +01:00
$value_arr = GETPOST ( " options_ " . $key , 'array' ); // check if an array
2013-07-23 15:52:41 +02:00
if ( ! empty ( $value_arr )) {
2020-01-30 01:48:28 +01:00
$value_key = implode ( $value_arr , ',' );
} else {
$value_key = '' ;
2013-07-23 15:52:41 +02:00
}
2013-04-21 17:36:23 +02:00
}
2020-06-29 09:07:54 +02:00
elseif ( in_array ( $key_type , array ( 'price' , 'double' )))
2013-06-10 14:13:44 +02:00
{
2020-01-30 01:48:28 +01:00
$value_arr = GETPOST ( " options_ " . $key , 'alpha' );
$value_key = price2num ( $value_arr );
2013-06-10 14:13:44 +02:00
}
2020-06-29 09:07:54 +02:00
elseif ( in_array ( $key_type , array ( 'html' )))
{
$value_key = GETPOST ( " options_ " . $key , 'alpha' );
}
2020-07-30 14:47:51 +02:00
elseif ( in_array ( $key_type , array ( 'text' )))
{
$value_key = GETPOST ( " options_ " . $key , 'alphanohtml' );
}
2013-04-21 17:36:23 +02:00
else
{
2020-01-30 01:48:28 +01:00
$value_key = GETPOST ( " options_ " . $key );
2019-10-23 16:38:15 +02:00
if ( in_array ( $key_type , array ( 'link' )) && $value_key == '-1' ) $value_key = '' ;
2013-04-21 17:36:23 +02:00
}
2018-04-23 22:56:40 +02:00
2020-01-30 01:48:28 +01:00
$object -> array_options [ " options_ " . $key ] = $value_key ;
2013-04-21 17:36:23 +02:00
}
2017-09-20 14:46:51 +02:00
if ( $nofillrequired ) {
2013-07-26 10:46:45 +02:00
$langs -> load ( 'errors' );
2019-01-27 11:55:16 +01:00
setEventMessages ( $langs -> trans ( 'ErrorFieldsRequired' ) . ' : ' . implode ( ', ' , $error_field_required ), null , 'errors' );
2013-07-26 10:46:45 +02:00
return - 1 ;
}
else {
return 1 ;
}
2013-04-21 17:36:23 +02:00
}
else {
return 0 ;
}
}
2016-04-20 17:30:40 +02:00
2013-06-10 14:13:44 +02:00
/**
2018-01-14 03:12:22 +01:00
* return array_options array of data of extrafields value of object sent by a search form
2013-06-10 14:13:44 +02:00
*
2018-12-13 20:45:51 +01:00
* @ param array | string $extrafieldsobjectkey array of extrafields ( old usage ) or value of object -> table_element ( new usage )
* @ param string $keyprefix Prefix string to add into name and id of field ( can be used to avoid duplicate names )
* @ param string $keysuffix Suffix string to add into name and id of field ( can be used to avoid duplicate names )
* @ return array | int array_options set or 0 if no value
2013-06-10 14:13:44 +02:00
*/
2019-02-27 20:45:07 +01:00
public function getOptionalsFromPost ( $extrafieldsobjectkey , $keyprefix = '' , $keysuffix = '' )
2013-06-10 14:13:44 +02:00
{
global $_POST ;
2018-12-13 20:45:51 +01:00
if ( is_string ( $extrafieldsobjectkey ) && is_array ( $this -> attributes [ $extrafieldsobjectkey ][ 'label' ]))
{
$extralabels = $this -> attributes [ $extrafieldsobjectkey ][ 'label' ];
}
else
{
$extralabels = $extrafieldsobjectkey ;
}
2018-04-12 23:16:23 +02:00
2013-06-10 14:13:44 +02:00
if ( is_array ( $extralabels ))
{
2018-12-13 20:45:51 +01:00
$array_options = array ();
2013-06-10 14:13:44 +02:00
// Get extra fields
foreach ( $extralabels as $key => $value )
{
2018-12-13 20:45:51 +01:00
$key_type = '' ;
if ( is_string ( $extrafieldsobjectkey ))
{
$key_type = $this -> attributes [ $extrafieldsobjectkey ][ 'type' ][ $key ];
}
2013-06-10 14:13:44 +02:00
2019-11-13 19:35:39 +01:00
if ( in_array ( $key_type , array ( 'date' , 'datetime' )))
2013-06-10 14:13:44 +02:00
{
2020-03-30 19:42:53 +02:00
if ( ! GETPOSTISSET ( $keysuffix . " options_ " . $key . $keyprefix . " year " )) continue ; // Value was not provided, we should not set it.
2013-06-10 14:13:44 +02:00
// Clean parameters
2020-01-25 14:03:24 +01:00
$value_key = dol_mktime ( GETPOST ( $keysuffix . " options_ " . $key . $keyprefix . " hour " , 'int' ), GETPOST ( $keysuffix . " options_ " . $key . $keyprefix . " min " , 'int' ), 0 , GETPOST ( $keysuffix . " options_ " . $key . $keyprefix . " month " , 'int' ), GETPOST ( $keysuffix . " options_ " . $key . $keyprefix . " day " , 'int' ), GETPOST ( $keysuffix . " options_ " . $key . $keyprefix . " year " , 'int' ));
2013-06-10 14:13:44 +02:00
}
2019-01-27 23:31:37 +01:00
elseif ( in_array ( $key_type , array ( 'checkbox' , 'chkbxlst' )))
2013-06-10 14:13:44 +02:00
{
2020-01-30 01:48:28 +01:00
if ( ! GETPOSTISSET ( $keysuffix . " options_ " . $key . $keyprefix )) continue ; // Value was not provided, we should not set it.
2019-11-13 19:35:39 +01:00
$value_arr = GETPOST ( $keysuffix . " options_ " . $key . $keyprefix );
2015-11-30 18:06:15 +01:00
// Make sure we get an array even if there's only one checkbox
2019-11-13 19:35:39 +01:00
$value_arr = ( array ) $value_arr ;
$value_key = implode ( ',' , $value_arr );
2013-06-10 14:13:44 +02:00
}
2020-01-25 14:03:24 +01:00
elseif ( in_array ( $key_type , array ( 'price' , 'double' , 'int' )))
2013-06-10 14:13:44 +02:00
{
2020-01-30 01:48:28 +01:00
if ( ! GETPOSTISSET ( $keysuffix . " options_ " . $key . $keyprefix )) continue ; // Value was not provided, we should not set it.
2019-11-13 19:35:39 +01:00
$value_arr = GETPOST ( $keysuffix . " options_ " . $key . $keyprefix );
$value_key = price2num ( $value_arr );
2013-06-10 14:13:44 +02:00
}
else
{
2020-01-30 01:48:28 +01:00
if ( ! GETPOSTISSET ( $keysuffix . " options_ " . $key . $keyprefix )) continue ; // Value was not provided, we should not set it.
2019-11-13 19:35:39 +01:00
$value_key = GETPOST ( $keysuffix . " options_ " . $key . $keyprefix );
2013-06-10 14:13:44 +02:00
}
2014-02-16 23:51:29 +01:00
2019-11-13 19:35:39 +01:00
$array_options [ $keysuffix . " options_ " . $key ] = $value_key ; // No keyprefix here. keyprefix is used only for read.
2013-06-10 14:13:44 +02:00
}
return $array_options ;
}
2018-12-13 20:45:51 +01:00
return 0 ;
2013-06-10 14:13:44 +02:00
}
2003-02-13 19:14:57 +01:00
}