dolibarr/htdocs/core/class/extrafields.class.php

1638 lines
52 KiB
PHP
Raw Normal View History

<?php
/* 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>
* Copyright (C) 2009-2012 Regis Houssin <regis.houssin@capnetworks.com>
* 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>
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
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2008-11-09 13:21:20 +01:00
/**
* \file htdocs/core/class/extrafields.class.php
* \ingroup core
* \brief File of class to manage extra fields
*/
2008-11-09 13:21:20 +01:00
/**
2012-02-01 20:44:06 +01:00
* Class to manage standard extra fields
*/
class ExtraFields
{
var $db;
// Tableau contenant le nom des champs en clef et la definition de ces champs
2009-11-07 14:55:15 +01:00
var $attribute_type;
// Tableau contenant le nom des champs en clef et le label de ces champs en value
var $attribute_label;
2015-05-25 18:43:46 +02:00
// Tableau contenant le nom des champs en clef et la taille/longueur max de ces champs en value
2009-11-07 14:55:15 +01:00
var $attribute_size;
2013-02-14 23:34:44 +01:00
// Tableau contenant le nom des choix en clef et la valeur de ces choix en value
var $attribute_choice;
2012-10-25 13:11:55 +02:00
// Array to store if attribute is unique or not
2012-09-30 21:26:58 +02:00
var $attribute_unique;
// Array to store if attribute is required or not
var $attribute_required;
2013-02-18 00:40:49 +01:00
// Array to store parameters of attribute (used in select type)
var $attribute_param;
2015-03-10 18:33:14 +01:00
// Array to store position of attribute
2013-02-18 00:40:49 +01:00
var $attribute_pos;
2015-03-10 18:33:14 +01:00
// Array to store if attribute is editable regardless of the document status
var $attribute_alwayseditable;
2015-03-10 18:33:14 +01:00
// Array to store permission to check
var $attribute_perms;
// Array to store permission to check
var $attribute_list;
// Array to store if extra field is hidden
var $attribute_hidden; // warning, do not rely on this. If your module need a hidden data, it must use its own table.
var $error;
2012-03-03 13:42:27 +01:00
var $errno;
2014-11-15 16:20:03 +01:00
public static $type2label=array(
2013-04-21 17:36:23 +02:00
'varchar'=>'String',
'text'=>'TextLong',
'int'=>'Int',
'double'=>'Float',
'date'=>'Date',
'datetime'=>'DateAndTime',
'boolean'=>'Boolean',
'price'=>'ExtrafieldPrice',
'phone'=>'ExtrafieldPhone',
'mail'=>'ExtrafieldMail',
'select' => 'ExtrafieldSelect',
'sellist' => 'ExtrafieldSelectList',
'radio' => 'ExtrafieldRadio',
'checkbox' => 'ExtrafieldCheckBox',
'chkbxlst' => 'ExtrafieldCheckBoxFromList',
'link' => 'ExtrafieldLink',
'separate' => 'ExtrafieldSeparator',
2016-06-28 10:21:10 +02:00
'password' => 'ExtrafieldPassword',
);
/**
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
*/
function __construct($db)
{
2012-02-01 20:44:06 +01:00
$this->db = $db;
$this->error = array();
2009-11-07 14:55:15 +01:00
$this->attribute_type = array();
$this->attribute_label = array();
2009-11-07 14:55:15 +01:00
$this->attribute_size = array();
$this->attribute_elementtype = array();
$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();
$this->attribute_list = array();
2016-02-03 22:49:43 +01:00
$this->attribute_hidden = array();
}
2013-04-21 17:36:23 +02:00
/**
* Add a new extra field parameter
*
* @param string $attrname Code of attribute
* @param string $label label of attribute
* @param int $type Type of attribute ('int', 'text', 'varchar', 'date', 'datehour')
* @param int $pos Position of attribute
2015-04-30 00:07:30 +02:00
* @param string $size 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
2015-10-20 12:53:32 +02:00
* @param string $default_value Defaulted value (Example: '', '0', 'null', 'avalue')
2013-04-21 17:36:23 +02:00
* @param array $param Params for field
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
* @param int $list Into list view by default
* @param int $ishidden Is hidden extrafield (warning, do not rely on this. If your module need a hidden data, it must use its own table)
2013-04-21 17:36:23 +02:00
* @return int <=0 if KO, >0 if OK
*/
2016-02-03 22:49:43 +01:00
function addExtraField($attrname, $label, $type, $pos, $size, $elementtype, $unique=0, $required=0, $default_value='', $param=0, $alwayseditable=0, $perms='', $list=0, $ishidden=0)
{
2013-04-21 17:36:23 +02:00
if (empty($attrname)) return -1;
if (empty($label)) return -1;
2015-02-07 17:41:20 +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')
{
$result=$this->create($attrname, $type, $size, $elementtype, $unique, $required, $default_value, $param, $perms, $list);
2013-04-21 17:36:23 +02:00
}
$err1=$this->errno;
if ($result > 0 || $err1 == 'DB_ERROR_COLUMN_ALREADY_EXISTS' || $type == 'separate')
{
// Add declaration of field into table
2016-02-03 22:49:43 +01:00
$result2=$this->create_label($attrname,$label,$type,$pos,$size,$elementtype, $unique, $required, $param, $alwayseditable, $perms, $list, $ishidden);
2013-04-21 17:36:23 +02:00
$err2=$this->errno;
if ($result2 > 0 || ($err1 == 'DB_ERROR_COLUMN_ALREADY_EXISTS' && $err2 == 'DB_ERROR_RECORD_ALREADY_EXISTS'))
{
$this->error='';
$this->errno=0;
return 1;
}
else return -2;
}
else
{
return -1;
}
}
/**
2014-03-05 09:57:36 +01:00
* Add a new optional attribute.
* This is a private method. For public method, use addExtraField.
2012-02-01 20:44:06 +01:00
*
* @param string $attrname code of attribute
* @param int $type Type of attribute ('int', 'text', 'varchar', 'date', 'datehour')
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
* @param string $default_value Default value for field
* @param array $param Params for field (ex for select list : array('options'=>array('value'=>'label of option'))
* @param string $perms Permission
* @param int $list Into list view by default
2013-04-21 17:36:23 +02:00
* @return int <=0 if KO, >0 if OK
*/
private function create($attrname, $type='varchar', $length=255, $elementtype='member', $unique=0, $required=0, $default_value='',$param='', $perms='', $list=0)
{
2015-02-07 17:41:20 +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
$table=$elementtype.'_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';
2016-06-28 10:21:10 +02:00
} elseif($type=='mail') {
$typedb='varchar';
$lengthdb='128';
} elseif (($type=='select') || ($type=='sellist') || ($type=='radio') ||($type=='checkbox') ||($type=='chkbxlst')){
2013-02-14 23:34:44 +01:00
$typedb='text';
$lengthdb='';
} elseif ($type=='link') {
$typedb='int';
$lengthdb='11';
2016-06-28 10:21:10 +02:00
} elseif($type=='password') {
$typedb='varchar';
$lengthdb='50';
} else {
$typedb=$type;
$lengthdb=$length;
}
2013-02-14 23:34:44 +01:00
$field_desc = array(
2013-04-21 17:36:23 +02:00
'type'=>$typedb,
'value'=>$lengthdb,
'null'=>($required?'NOT NULL':'NULL'),
'default' => $default_value
2013-02-14 23:34:44 +01:00
);
2013-04-21 17:36:23 +02:00
$result=$this->db->DDLAddField(MAIN_DB_PREFIX.$table, $attrname, $field_desc);
if ($result > 0)
{
if ($unique)
{
$sql="ALTER TABLE ".MAIN_DB_PREFIX.$table." ADD UNIQUE INDEX uk_".$table."_".$attrname." (".$attrname.")";
$resql=$this->db->query($sql,1,'dml');
}
return 1;
}
else
{
$this->error=$this->db->lasterror();
2012-03-03 13:42:27 +01:00
$this->errno=$this->db->lasterrno();
2011-06-12 17:47:10 +02:00
return -1;
}
}
else
{
return 0;
}
}
/**
2014-03-05 09:57:36 +01:00
* Add description of a new optional attribute
2012-02-01 20:44:06 +01:00
*
* @param string $attrname code of attribute
* @param string $label label of attribute
* @param int $type Type of attribute ('int', 'text', 'varchar', 'date', 'datehour', 'float')
* @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', ...)
* @param int $unique Is field unique or not
* @param int $required Is field required or not
* @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
* @param int $list Into list view by default
* @param int $ishidden Is hidden extrafield (warning, do not rely on this. If your module need a hidden data, it must use its own table)
* @return int <=0 if KO, >0 if OK
2009-01-27 00:58:37 +01:00
*/
2016-02-03 22:49:43 +01:00
private function create_label($attrname, $label='', $type='', $pos=0, $size=0, $elementtype='member', $unique=0, $required=0, $param='', $alwayseditable=0, $perms='', $list=0, $ishidden=0)
{
global $conf;
2009-11-07 14:55:15 +01:00
2015-02-07 17:41:20 +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
if (empty($pos)) $pos=0;
if (empty($list)) $list=0;
2009-01-27 00:58:37 +01:00
if (! empty($attrname) && preg_match("/^\w[a-zA-Z0-9-_]*$/",$attrname) && ! is_numeric($attrname))
{
if(is_array($param) && count($param) > 0)
2013-02-14 23:34:44 +01:00
{
$params = $this->db->escape(serialize($param));
}
elseif (strlen($param) > 0)
{
$params = trim($param);
}
else
{
$params='';
}
2013-04-21 17:36:23 +02:00
2016-02-03 22:49:43 +01:00
$sql = "INSERT INTO ".MAIN_DB_PREFIX."extrafields(name, label, type, pos, size, entity, elementtype, fieldunique, fieldrequired, param, alwayseditable, perms, list, ishidden)";
2010-05-09 20:20:36 +02:00
$sql.= " VALUES('".$attrname."',";
$sql.= " '".$this->db->escape($label)."',";
2010-05-09 20:20:36 +02:00
$sql.= " '".$type."',";
$sql.= " '".$pos."',";
$sql.= " '".$size."',";
$sql.= " ".$conf->entity.",";
2013-04-21 17:36:23 +02:00
$sql.= " '".$elementtype."',";
$sql.= " '".$unique."',";
$sql.= " '".$required."',";
$sql.= " '".$params."',";
2015-03-10 18:33:14 +01:00
$sql.= " '".$alwayseditable."',";
$sql.= " ".($perms?"'".$this->db->escape($perms)."'":"null").",";
$sql.= " ".$list;
2016-02-03 22:49:43 +01:00
$sql.= ", ".$ishidden;
2013-04-21 17:36:23 +02:00
$sql.=')';
2009-01-27 00:58:37 +01:00
2014-06-12 11:31:53 +02:00
dol_syslog(get_class($this)."::create_label", LOG_DEBUG);
if ($this->db->query($sql))
{
return 1;
}
else
{
2012-03-03 13:42:27 +01:00
$this->error=$this->db->lasterror();
$this->errno=$this->db->lasterrno();
return -1;
}
}
}
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
*
* @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', ...)
* @return int < 0 if KO, 0 if nothing is done, 1 if OK
2009-01-27 00:58:37 +01:00
*/
function delete($attrname, $elementtype='member')
{
2015-02-07 17:41:20 +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
$table=$elementtype.'_extrafields';
if (! empty($attrname) && preg_match("/^\w[a-zA-Z0-9-_]*$/",$attrname))
2009-01-27 00:58:37 +01:00
{
2013-04-21 17:36:23 +02:00
$result=$this->db->DDLDropField(MAIN_DB_PREFIX.$table,$attrname); // This also drop the unique key
2009-01-27 00:58:37 +01:00
if ($result < 0)
{
2009-01-27 00:58:37 +01:00
$this->error=$this->db->lasterror();
}
2009-01-27 00:58:37 +01:00
$result=$this->delete_label($attrname,$elementtype);
2009-01-27 00:58:37 +01:00
return $result;
}
else
{
return 0;
}
}
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
*
* @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
*/
private function delete_label($attrname, $elementtype='member')
{
global $conf;
2009-11-07 14:55:15 +01:00
2015-02-07 17:41:20 +01:00
if ($elementtype == 'thirdparty') $elementtype='societe';
if ($elementtype == 'contact') $elementtype='socpeople';
2015-02-07 17:41:20 +01:00
if (isset($attrname) && $attrname != '' && preg_match("/^\w[a-zA-Z0-9-_]*$/",$attrname))
{
$sql = "DELETE FROM ".MAIN_DB_PREFIX."extrafields";
$sql.= " WHERE name = '".$attrname."'";
$sql.= " AND entity IN (0,".$conf->entity.')';
2013-04-21 17:36:23 +02:00
$sql.= " AND elementtype = '".$elementtype."'";
2014-06-12 11:31:53 +02:00
dol_syslog(get_class($this)."::delete_label", LOG_DEBUG);
$resql=$this->db->query($sql);
if ($resql)
{
return 1;
}
else
{
print dol_print_error($this->db);
return -1;
}
}
else
{
return 0;
}
}
2003-02-14 18:14:29 +01:00
2009-01-27 00:58:37 +01:00
/**
* Modify type of a personalized attribute
2012-02-01 20:44:06 +01:00
*
* @param string $attrname Name of attribute
* @param string $label Label of attribute
* @param string $type Type of attribute
* @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
* @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
* @param int $list Into list view by default
* @param int $ishidden Is hidden extrafield (warning, do not rely on this. If your module need a hidden data, it must use its own table)
* @return int >0 if OK, <=0 if KO
*/
2016-02-03 22:49:43 +01:00
function update($attrname,$label,$type,$length,$elementtype,$unique=0,$required=0,$pos=0,$param='',$alwayseditable=0, $perms='',$list='',$ishidden=0)
{
2015-02-07 17:41:20 +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
$table=$elementtype.'_extrafields';
2013-04-21 17:36:23 +02:00
if (isset($attrname) && $attrname != '' && preg_match("/^\w[a-zA-Z0-9-_]*$/",$attrname))
{
if ($type=='boolean') {
$typedb='int';
$lengthdb='1';
} elseif($type=='price') {
$typedb='double';
$lengthdb='24,8';
} elseif($type=='phone') {
$typedb='varchar';
$lengthdb='20';
2016-06-28 10:21:10 +02:00
} elseif($type=='mail') {
$typedb='varchar';
$lengthdb='128';
} elseif (($type=='select') || ($type=='sellist') || ($type=='radio') || ($type=='checkbox') || ($type=='chkbxlst')) {
$typedb='text';
$lengthdb='';
} elseif ($type=='link') {
$typedb='int';
$lengthdb='11';
2016-06-28 10:21:10 +02:00
} elseif($type=='password') {
$typedb='varchar';
$lengthdb='50';
} else {
$typedb=$type;
$lengthdb=$length;
}
$field_desc = array('type'=>$typedb, 'value'=>$lengthdb, 'null'=>($required?'NOT NULL':'NULL'));
2013-04-21 17:36:23 +02:00
if ($type != 'separate') // No table update when separate type
{
$result=$this->db->DDLUpdateField(MAIN_DB_PREFIX.$table, $attrname, $field_desc);
}
if ($result > 0 || $type == 'separate')
{
2012-09-30 21:26:58 +02:00
if ($label)
{
2016-02-03 22:49:43 +01:00
$result=$this->update_label($attrname,$label,$type,$length,$elementtype,$unique,$required,$pos,$param,$alwayseditable,$perms,$list,$ishidden);
2012-09-30 21:26:58 +02:00
}
if ($result > 0)
{
$sql='';
if ($unique)
{
$sql="ALTER TABLE ".MAIN_DB_PREFIX.$table." ADD UNIQUE INDEX uk_".$table."_".$attrname." (".$attrname.")";
}
else
{
2012-09-30 21:26:58 +02:00
$sql="ALTER TABLE ".MAIN_DB_PREFIX.$table." DROP INDEX uk_".$table."_".$attrname;
}
2014-06-13 01:34:39 +02:00
dol_syslog(get_class($this).'::update', LOG_DEBUG);
$resql=$this->db->query($sql,1,'dml');
return 1;
}
else
{
$this->error=$this->db->lasterror();
return -1;
}
}
else
{
$this->error=$this->db->lasterror();
2011-06-12 17:47:10 +02:00
return -1;
}
2009-11-07 14:55:15 +01:00
}
else
{
return 0;
}
2003-02-14 18:14:29 +01: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
* @param int $list Into list view by default
* @param int $ishidden Is hidden extrafield (warning, do not rely on this. If your module need a hidden data, it must use its own table)
2013-04-21 17:36:23 +02:00
* @return int <=0 if KO, >0 if OK
*/
2016-02-03 22:49:43 +01:00
private function update_label($attrname,$label,$type,$size,$elementtype,$unique=0,$required=0,$pos=0,$param='',$alwayseditable=0,$perms='',$list=0,$ishidden=0)
{
global $conf;
2016-02-03 22:49:43 +01:00
dol_syslog(get_class($this)."::update_label ".$attrname.", ".$label.", ".$type.", ".$size.", ".$elementtype.", ".$unique.", ".$required.", ".$pos.", ".$alwayseditable.", ".$perms.", ".$list.", ".$ishidden);
2009-11-07 14:55:15 +01:00
// Clean parameters
2015-02-07 17:41:20 +01:00
if ($elementtype == 'thirdparty') $elementtype='societe';
if ($elementtype == 'contact') $elementtype='socpeople';
if (empty($list)) $list=0;
2015-02-07 17:41:20 +01:00
if (isset($attrname) && $attrname != '' && preg_match("/^\w[a-zA-Z0-9-_]*$/",$attrname))
{
2009-11-07 14:55:15 +01:00
$this->db->begin();
2013-04-21 17:36:23 +02:00
if(is_array($param) && count($param) > 0)
{
2013-05-19 18:47:14 +02:00
$param = $this->db->escape(serialize($param));
}
2013-04-21 17:36:23 +02:00
$sql_del = "DELETE FROM ".MAIN_DB_PREFIX."extrafields";
$sql_del.= " WHERE name = '".$attrname."'";
$sql_del.= " AND entity = ".$conf->entity;
$sql_del.= " AND elementtype = '".$elementtype."'";
dol_syslog(get_class($this)."::update_label", LOG_DEBUG);
2009-11-07 14:55:15 +01:00
$resql1=$this->db->query($sql_del);
$sql = "INSERT INTO ".MAIN_DB_PREFIX."extrafields(";
$sql.= " name,"; // This is code
2009-11-07 14:55:15 +01:00
$sql.= " entity,";
$sql.= " label,";
$sql.= " type,";
$sql.= " size,";
$sql.= " elementtype,";
2012-10-25 13:11:55 +02:00
$sql.= " fieldunique,";
$sql.= " fieldrequired,";
2015-03-10 18:33:14 +01:00
$sql.= " perms,";
$sql.= " pos,";
$sql.= " alwayseditable,";
$sql.= " param,";
$sql.= " list";
2016-02-03 22:49:43 +01:00
$sql.= ", ishidden";
$sql.= ") VALUES (";
2009-11-07 14:55:15 +01:00
$sql.= "'".$attrname."',";
$sql.= " ".$conf->entity.",";
$sql.= " '".$this->db->escape($label)."',";
2009-11-07 14:55:15 +01:00
$sql.= " '".$type."',";
$sql.= " '".$size."',";
2013-04-21 17:36:23 +02:00
$sql.= " '".$elementtype."',";
$sql.= " '".$unique."',";
$sql.= " '".$required."',";
2015-03-10 18:33:14 +01:00
$sql.= " ".($perms?"'".$this->db->escape($perms)."'":"null").",";
2013-04-21 17:36:23 +02:00
$sql.= " '".$pos."',";
$sql.= " '".$alwayseditable."',";
2015-03-18 14:19:17 +01:00
$sql.= " '".$param."',";
$sql.= " ".$list;
2016-02-03 22:49:43 +01:00
$sql.= ", ".$ishidden;
2013-04-21 17:36:23 +02:00
$sql.= ")";
2014-06-12 11:31:53 +02:00
dol_syslog(get_class($this)."::update_label", LOG_DEBUG);
2009-11-07 14:55:15 +01:00
$resql2=$this->db->query($sql);
2009-11-07 14:55:15 +01:00
if ($resql1 && $resql2)
{
2009-11-07 14:55:15 +01:00
$this->db->commit();
return 1;
}
else
{
2009-11-07 14:55:15 +01:00
$this->db->rollback();
print dol_print_error($this->db);
return -1;
}
}
else
{
return 0;
}
}
2008-12-18 00:44:04 +01:00
/**
* Load array this->attribute_xxx like attribute_label, attribute_type, ...
2012-02-01 20:44:06 +01:00
*
2015-08-24 14:42:07 +02:00
* @param string $elementtype Type of element ('adherent', 'commande', 'thirdparty', 'facture', 'propal', 'product', ...)
2013-01-21 15:04:34 +01:00
* @param boolean $forceload Force load of extra fields whatever is option MAIN_EXTRAFIELDS_DISABLED
2012-02-01 20:44:06 +01:00
* @return array Array of attributes for all extra fields
2008-12-18 00:44:04 +01:00
*/
2014-11-20 13:06:05 +01:00
function fetch_name_optionals_label($elementtype,$forceload=false)
{
global $conf;
2009-11-07 14:55:15 +01:00
if ( empty($elementtype) ) return array();
2015-04-06 19:46:10 +02:00
2015-02-07 17:41:20 +01:00
if ($elementtype == 'thirdparty') $elementtype='societe';
if ($elementtype == 'contact') $elementtype='socpeople';
2015-02-07 17:41:20 +01:00
$array_name_label=array();
2015-04-06 19:46:10 +02:00
// For avoid conflicts with external modules
2014-11-17 15:09:52 +01:00
if (!$forceload && !empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) return $array_name_label;
2016-02-03 22:49:43 +01:00
$sql = "SELECT rowid,name,label,type,size,elementtype,fieldunique,fieldrequired,param,pos,alwayseditable,perms,list,ishidden";
$sql.= " FROM ".MAIN_DB_PREFIX."extrafields";
2013-04-25 00:09:47 +02:00
$sql.= " WHERE entity IN (0,".$conf->entity.")";
if ($elementtype) $sql.= " AND elementtype = '".$elementtype."'";
2008-12-18 00:44:04 +01:00
$sql.= " ORDER BY pos";
2009-01-27 00:58:37 +01:00
2014-06-12 11:31:53 +02:00
dol_syslog(get_class($this)."::fetch_name_optionals_label", LOG_DEBUG);
2008-12-18 00:44:04 +01:00
$resql=$this->db->query($sql);
if ($resql)
{
2008-12-18 00:44:04 +01:00
if ($this->db->num_rows($resql))
{
2008-12-18 00:44:04 +01:00
while ($tab = $this->db->fetch_object($resql))
{
2009-01-27 00:58:37 +01:00
// we can add this attribute to adherent object
if ($tab->type != 'separate')
{
$array_name_label[$tab->name]=$tab->label;
}
2009-11-07 14:55:15 +01:00
$this->attribute_type[$tab->name]=$tab->type;
2009-01-27 00:58:37 +01:00
$this->attribute_label[$tab->name]=$tab->label;
2009-11-07 14:55:15 +01:00
$this->attribute_size[$tab->name]=$tab->size;
2013-04-21 17:36:23 +02:00
$this->attribute_elementtype[$tab->name]=$tab->elementtype;
$this->attribute_unique[$tab->name]=$tab->fieldunique;
$this->attribute_required[$tab->name]=$tab->fieldrequired;
2015-04-06 19:46:10 +02:00
$this->attribute_param[$tab->name]=($tab->param ? unserialize($tab->param) : '');
2013-04-21 17:36:23 +02:00
$this->attribute_pos[$tab->name]=$tab->pos;
$this->attribute_alwayseditable[$tab->name]=$tab->alwayseditable;
2015-03-10 18:33:14 +01:00
$this->attribute_perms[$tab->name]=$tab->perms;
$this->attribute_list[$tab->name]=$tab->list;
2016-02-03 22:49:43 +01:00
$this->attribute_hidden[$tab->name]=$tab->ishidden;
}
}
}
else
{
$this->error=$this->db->lasterror();
dol_syslog(get_class($this)."::fetch_name_optionals_label ".$this->error, LOG_ERR);
}
2014-11-17 15:09:52 +01:00
return $array_name_label;
}
2009-11-07 14:55:15 +01:00
/**
* Return HTML string to put an input field into a page
*
2015-08-24 14:42:07 +02:00
* @param string $key Key of attribute
* @param string $value Value to show (for date type it must be in timestamp format)
* @param string $moreparam To add more parametes on html input tag
* @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)
2016-09-21 03:54:54 +02:00
* @param mixed $showsize Value for css to define size. May also be a numeric.
* @param int $objectid Current object id
2015-08-24 14:42:07 +02:00
* @return string
*/
function showInputField($key,$value,$moreparam='',$keyprefix='',$keysuffix='',$showsize=0, $objectid=0)
{
global $conf,$langs;
2013-04-21 17:36:23 +02:00
$label=$this->attribute_label[$key];
$type =$this->attribute_type[$key];
$size =$this->attribute_size[$key];
$elementtype=$this->attribute_elementtype[$key];
$unique=$this->attribute_unique[$key];
$required=$this->attribute_required[$key];
$param=$this->attribute_param[$key];
2015-03-10 18:33:14 +01:00
$perms=$this->attribute_perms[$key];
$list=$this->attribute_list[$key];
$hidden=$this->attribute_hidden[$key];
2015-03-10 18:33:14 +01:00
2015-08-24 14:42:07 +02:00
if (empty($showsize))
2013-04-21 17:36:23 +02:00
{
2015-08-24 14:42:07 +02:00
if ($type == 'date')
{
2016-09-21 03:54:54 +02:00
//$showsize=10;
$showsize = 'minwidth100imp';
2015-08-24 14:42:07 +02:00
}
elseif ($type == 'datetime')
{
2016-09-21 03:54:54 +02:00
//$showsize=19;
$showsize = 'minwidth200imp';
2015-08-24 14:42:07 +02:00
}
elseif (in_array($type,array('int','double')))
{
2016-09-21 03:54:54 +02:00
//$showsize=10;
$showsize = 'minwidth100imp';
2015-08-24 14:42:07 +02:00
}
else
{
2016-09-21 03:54:54 +02:00
if (round($size) < 12)
{
$showsize = 'minwidth100imp';
}
else if (round($size) <= 48)
{
$showsize = 'minwidth200imp';
}
else
{
//$showsize=48;
$showsize = 'minwidth400imp';
}
2015-08-24 14:42:07 +02:00
}
2013-04-21 17:36:23 +02:00
}
2016-09-21 03:54:54 +02:00
if (in_array($type,array('date','datetime')))
2013-04-21 17:36:23 +02:00
{
$tmp=explode(',',$size);
$newsize=$tmp[0];
$showtime = in_array($type,array('datetime')) ? 1 : 0;
// Do not show current date when field not required (see select_date() method)
if (!$required && $value == '') $value = '-1';
require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
global $form;
if (! is_object($form)) $form=new Form($this->db);
2013-04-21 17:36:23 +02:00
// TODO Must also support $moreparam
$out = $form->select_date($value, $keysuffix.'options_'.$key.$keyprefix, $showtime, $showtime, $required, '', 1, 1, 1, 0, 1);
2013-04-21 17:36:23 +02:00
}
2013-06-10 14:13:44 +02:00
elseif (in_array($type,array('int')))
2013-04-21 17:36:23 +02:00
{
$tmp=explode(',',$size);
$newsize=$tmp[0];
2016-09-21 03:54:54 +02:00
$out='<input type="text" class="flat '.$showsize.'" name="'.$keysuffix.'options_'.$key.$keyprefix.' maxwidthonsmartphone" " maxlength="'.$newsize.'" value="'.$value.'"'.($moreparam?$moreparam:'').'>';
2013-04-21 17:36:23 +02:00
}
elseif ($type == 'varchar')
{
2016-09-21 03:54:54 +02:00
$out='<input type="text" class="flat '.$showsize.'" name="'.$keysuffix.'options_'.$key.$keyprefix.' maxwidthonsmartphone" maxlength="'.$size.'" value="'.$value.'"'.($moreparam?$moreparam:'').'>';
2013-04-21 17:36:23 +02:00
}
elseif ($type == 'text')
{
require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
2015-08-24 14:42:07 +02:00
$doleditor=new DolEditor($keysuffix.'options_'.$key.$keyprefix,$value,'',200,'dolibarr_notes','In',false,false,! empty($conf->fckeditor->enabled) && $conf->global->FCKEDITOR_ENABLE_SOCIETE,5,100);
2013-04-21 17:36:23 +02:00
$out=$doleditor->Create(1);
}
elseif ($type == 'boolean')
{
$checked='';
if (!empty($value)) {
$checked=' checked value="1" ';
2013-04-21 17:36:23 +02:00
} else {
$checked=' value="1" ';
}
2016-09-21 03:54:54 +02:00
$out='<input type="checkbox" class="flat '.$showsize.'" name="'.$keysuffix.'options_'.$key.$keyprefix.'" '.$checked.' '.($moreparam?$moreparam:'').'>';
2013-04-21 17:36:23 +02:00
}
elseif ($type == 'mail')
{
2016-09-21 03:54:54 +02:00
$out='<input type="text" class="flat '.$showsize.'" name="'.$keysuffix.'options_'.$key.$keyprefix.' maxwidthonsmartphone" value="'.$value.'" '.($moreparam?$moreparam:'').'>';
2013-04-21 17:36:23 +02:00
}
elseif ($type == 'phone')
{
2016-09-21 03:54:54 +02:00
$out='<input type="text" class="flat '.$showsize.'" name="'.$keysuffix.'options_'.$key.$keyprefix.' maxwidthonsmartphone" value="'.$value.'" '.($moreparam?$moreparam:'').'>';
2013-04-21 17:36:23 +02:00
}
elseif ($type == 'price')
{
2016-09-21 03:54:54 +02:00
$out='<input type="text" class="flat '.$showsize.'" name="'.$keysuffix.'options_'.$key.$keyprefix.' maxwidthonsmartphone" value="'.price($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')
{
if (!empty($value)) {
$value=price($value);
}
2016-09-21 03:54:54 +02:00
$out='<input type="text" class="flat '.$showsize.'" name="'.$keysuffix.'options_'.$key.$keyprefix.' maxwidthonsmartphone" value="'.$value.'" '.($moreparam?$moreparam:'').'> ';
2013-06-10 14:13:44 +02:00
}
2013-04-21 17:36:23 +02:00
elseif ($type == 'select')
{
$out = '';
if (! empty($conf->use_javascript_ajax) && ! empty($conf->global->MAIN_EXTRAFIELDS_USE_SELECT2))
{
include_once DOL_DOCUMENT_ROOT . '/core/lib/ajax.lib.php';
$out.= ajax_combobox($keysuffix.'options_'.$key.$keyprefix, array(), 0);
}
2016-09-21 03:54:54 +02:00
$out.='<select class="flat '.$showsize.'" name="'.$keysuffix.'options_'.$key.$keyprefix.' maxwidthonsmartphone" id="options_'.$key.$keyprefix.'" '.($moreparam?$moreparam:'').'>';
2014-08-17 19:27:25 +02:00
$out.='<option value="0">&nbsp;</option>';
foreach ($param['options'] as $key => $val)
2013-04-21 17:36:23 +02:00
{
if ($key == '') continue;
list($val, $parent) = explode('|', $val);
2013-04-21 17:36:23 +02:00
$out.='<option value="'.$key.'"';
$out.= ($value==$key?' selected':'');
$out.= (!empty($parent)?' parent="'.$parent.'"':'');
2013-04-21 17:36:23 +02:00
$out.='>'.$val.'</option>';
}
$out.='</select>';
}
elseif ($type == 'sellist')
{
$out = '';
if (! empty($conf->use_javascript_ajax) && ! empty($conf->global->MAIN_EXTRAFIELDS_USE_SELECT2))
{
include_once DOL_DOCUMENT_ROOT . '/core/lib/ajax.lib.php';
$out.= ajax_combobox($keysuffix.'options_'.$key.$keyprefix, array(), 0);
}
2016-09-21 03:54:54 +02:00
$out.='<select class="flat '.$showsize.'" name="'.$keysuffix.'options_'.$key.$keyprefix.' maxwidthonsmartphone" id="options_'.$key.$keyprefix.'" '.($moreparam?$moreparam:'').'>';
2013-09-02 00:34:41 +02:00
if (is_array($param['options']))
{
$param_list=array_keys($param['options']);
$InfoFieldList = explode(":", $param_list[0]);
// 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
$keyList=(empty($InfoFieldList[2])?'rowid':$InfoFieldList[2].' as rowid');
2016-07-05 15:45:33 +02:00
if (count($InfoFieldList) > 4 && ! empty($InfoFieldList[4]))
{
if (strpos($InfoFieldList[4], 'extra.') !== false)
{
$keyList='main.'.$InfoFieldList[2].' as rowid';
} else {
$keyList=$InfoFieldList[2].' as rowid';
}
}
2016-07-05 15:45:33 +02:00
if (count($InfoFieldList) > 3 && ! empty($InfoFieldList[3]))
{
list($parentName, $parentField) = explode('|', $InfoFieldList[3]);
$keyList.= ', '.$parentField;
}
2013-09-02 00:34:41 +02:00
$fields_label = explode('|',$InfoFieldList[1]);
if (is_array($fields_label))
{
2013-09-02 00:34:41 +02:00
$keyList .=', ';
$keyList .= implode(', ', $fields_label);
}
$sqlwhere='';
2013-09-02 00:34:41 +02:00
$sql = 'SELECT '.$keyList;
$sql.= ' FROM '.MAIN_DB_PREFIX .$InfoFieldList[0];
if (!empty($InfoFieldList[4]))
{
// can use SELECT request
if (strpos($InfoFieldList[4], '$SEL$')!==false) {
$InfoFieldList[4]=str_replace('$SEL$','SELECT',$InfoFieldList[4]);
}
// current object id can be use into filter
if (strpos($InfoFieldList[4], '$ID$')!==false && !empty($objectid)) {
$InfoFieldList[4]=str_replace('$ID$',$objectid,$InfoFieldList[4]);
2015-11-20 13:59:58 +01:00
} else {
$InfoFieldList[4]=str_replace('$ID$','0',$InfoFieldList[4]);
}
//We have to join on extrafield table
if (strpos($InfoFieldList[4], 'extra')!==false)
{
$sql.= ' as main, '.MAIN_DB_PREFIX .$InfoFieldList[0].'_extrafields as extra';
$sqlwhere.= ' WHERE extra.fk_object=main.'.$InfoFieldList[2]. ' AND '.$InfoFieldList[4];
}
else
{
$sqlwhere.= ' WHERE '.$InfoFieldList[4];
}
}
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')))
{
$sqlwhere.= ' AND entity = '.$conf->entity;
}
2015-03-25 11:23:05 +01:00
$sql.=$sqlwhere;
2013-09-02 00:38:13 +02:00
//print $sql;
2013-04-21 17:36:23 +02:00
2015-04-21 20:21:24 +02:00
$sql .= ' ORDER BY ' . implode(', ', $fields_label);
2014-06-13 01:34:39 +02:00
dol_syslog(get_class($this).'::showInputField type=sellist', LOG_DEBUG);
$resql = $this->db->query($sql);
if ($resql)
2013-04-21 17:36:23 +02:00
{
$out.='<option value="0">&nbsp;</option>';
$num = $this->db->num_rows($resql);
$i = 0;
2013-09-02 00:34:41 +02:00
while ($i < $num)
2013-04-21 17:36:23 +02:00
{
$labeltoshow='';
2013-04-21 17:36:23 +02:00
$obj = $this->db->fetch_object($resql);
// 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)
{
$labeltoshow.= $obj->$field_toshow.' ';
}
}
else
{
$labeltoshow=$obj->{$InfoFieldList[1]};
}
$labeltoshow=dol_trunc($labeltoshow,45);
2013-04-21 17:36:23 +02:00
if ($value==$obj->rowid)
{
foreach ($fields_label as $field_toshow)
{
$translabel=$langs->trans($obj->$field_toshow);
if ($translabel!=$obj->$field_toshow) {
$labeltoshow=dol_trunc($translabel,18).' ';
}else {
$labeltoshow=dol_trunc($obj->$field_toshow,18).' ';
}
}
$out.='<option value="'.$obj->rowid.'" selected>'.$labeltoshow.'</option>';
2013-06-10 14:13:44 +02:00
}
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);
}
}
2013-09-02 00:34:41 +02:00
if (empty($labeltoshow)) $labeltoshow='(not defined)';
if ($value==$obj->rowid)
{
$out.='<option value="'.$obj->rowid.'" selected>'.$labeltoshow.'</option>';
}
2013-09-02 00:34:41 +02:00
if (!empty($InfoFieldList[3]))
{
$parent = $parentName.':'.$obj->{$parentField};
}
$out.='<option value="'.$obj->rowid.'"';
$out.= ($value==$obj->rowid?' selected':'');
$out.= (!empty($parent)?' parent="'.$parent.'"':'');
$out.='>'.$labeltoshow.'</option>';
2013-04-21 17:36:23 +02:00
}
2013-09-02 00:34:41 +02:00
$i++;
2013-04-21 17:36:23 +02:00
}
$this->db->free($resql);
2013-04-21 17:36:23 +02:00
}
else {
print 'Error in request '.$sql.' '.$this->db->lasterror().'. Check setup of extra parameters.<br>';
}
2013-04-21 17:36:23 +02:00
}
$out.='</select>';
}
elseif ($type == 'checkbox')
{
$out='';
$value_arr=explode(',',$value);
foreach ($param['options'] as $keyopt=>$val )
{
2015-08-24 14:42:07 +02:00
$out.='<input class="flat" type="checkbox" name="'.$keysuffix.'options_'.$key.$keyprefix.'[]" '.($moreparam?$moreparam:'');
2013-04-21 17:36:23 +02:00
$out.=' value="'.$keyopt.'"';
if ((is_array($value_arr)) && in_array($keyopt,$value_arr)) {
$out.= 'checked';
2013-04-21 17:36:23 +02:00
}else {
$out.='';
}
$out.='/>'.$val.'<br>';
}
}
elseif ($type == 'radio')
{
$out='';
foreach ($param['options'] as $keyopt=>$val )
{
2016-09-21 03:54:54 +02:00
$out.='<input class="flat '.$showsize.'" type="radio" name="'.$keysuffix.'options_'.$key.$keyprefix.'" '.($moreparam?$moreparam:'');
2013-04-21 17:36:23 +02:00
$out.=' value="'.$keyopt.'"';
$out.= ($value==$keyopt?'checked':'');
2013-04-21 17:36:23 +02:00
$out.='/>'.$val.'<br>';
}
}
elseif ($type == 'chkbxlst')
{
if (is_array($value)) {
$value_arr = $value;
}
else {
$value_arr = explode(',', $value);
}
2015-03-10 18:33:14 +01:00
if (is_array($param['options'])) {
$param_list = array_keys($param['options']);
$InfoFieldList = explode(":", $param_list[0]);
// 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
$keyList = (empty($InfoFieldList[2]) ? 'rowid' : $InfoFieldList[2] . ' as rowid');
2015-03-10 18:33:14 +01:00
if (count($InfoFieldList) > 3 && ! empty($InfoFieldList[3])) {
list ( $parentName, $parentField ) = explode('|', $InfoFieldList[3]);
$keyList .= ', ' . $parentField;
}
if (count($InfoFieldList) > 4 && ! empty($InfoFieldList[4])) {
if (strpos($InfoFieldList[4], 'extra.') !== false) {
$keyList = 'main.' . $InfoFieldList[2] . ' as rowid';
} else {
$keyList = $InfoFieldList[2] . ' as rowid';
}
}
2015-03-10 18:33:14 +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
$sqlwhere = '';
$sql = 'SELECT ' . $keyList;
$sql .= ' FROM ' . MAIN_DB_PREFIX . $InfoFieldList[0];
if (! empty($InfoFieldList[4])) {
// can use SELECT request
if (strpos($InfoFieldList[4], '$SEL$')!==false) {
$InfoFieldList[4]=str_replace('$SEL$','SELECT',$InfoFieldList[4]);
}
// current object id can be use into filter
if (strpos($InfoFieldList[4], '$ID$')!==false && !empty($objectid)) {
$InfoFieldList[4]=str_replace('$ID$',$objectid,$InfoFieldList[4]);
2015-11-20 13:59:58 +01:00
} else {
$InfoFieldList[4]=str_replace('$ID$','0',$InfoFieldList[4]);
}
// We have to join on extrafield table
if (strpos($InfoFieldList[4], 'extra') !== false) {
$sql .= ' as main, ' . MAIN_DB_PREFIX . $InfoFieldList[0] . '_extrafields as extra';
$sqlwhere .= ' WHERE extra.fk_object=main.' . $InfoFieldList[2] . ' AND ' . $InfoFieldList[4];
} else {
$sqlwhere .= ' WHERE ' . $InfoFieldList[4];
}
} else {
2015-12-03 11:52:41 +01:00
$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')))
{
$sqlwhere .= ' AND entity = ' . $conf->entity;
}
// $sql.=preg_replace('/^ AND /','',$sqlwhere);
// print $sql;
$sql .= $sqlwhere;
dol_syslog(get_class($this) . '::showInputField type=chkbxlst',LOG_DEBUG);
$resql = $this->db->query($sql);
if ($resql) {
$num = $this->db->num_rows($resql);
$i = 0;
while ( $i < $num ) {
$labeltoshow = '';
$obj = $this->db->fetch_object($resql);
2015-03-10 18:33:14 +01:00
// 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 ) {
$labeltoshow .= $obj->$field_toshow . ' ';
}
} else {
$labeltoshow = $obj->{$InfoFieldList[1]};
}
$labeltoshow = dol_trunc($labeltoshow, 45);
2015-03-10 18:33:14 +01:00
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) {
$labeltoshow = dol_trunc($translabel, 18) . ' ';
} else {
$labeltoshow = dol_trunc($obj->$field_toshow, 18) . ' ';
}
}
2015-08-24 14:42:07 +02:00
$out .= '<input class="flat" type="checkbox" name="'.$keysuffix.'options_' . $key . $keyprefix . '[]" ' . ($moreparam ? $moreparam : '');
$out .= ' value="' . $obj->rowid . '"';
2015-03-10 18:33:14 +01:00
$out .= 'checked';
2015-03-10 18:33:14 +01:00
$out .= '/>' . $labeltoshow . '<br>';
} 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)';
2015-03-10 18:33:14 +01:00
if (is_array($value_arr) && in_array($obj->rowid, $value_arr)) {
2015-08-24 14:42:07 +02:00
$out .= '<input class="flat" type="checkbox" name="'.$keysuffix.'options_' . $key . $keyprefix . '[]" ' . ($moreparam ? $moreparam : '');
$out .= ' value="' . $obj->rowid . '"';
2015-03-10 18:33:14 +01:00
$out .= 'checked';
$out .= '';
2015-03-10 18:33:14 +01:00
$out .= '/>' . $labeltoshow . '<br>';
}
2015-03-10 18:33:14 +01:00
if (! empty($InfoFieldList[3])) {
$parent = $parentName . ':' . $obj->{$parentField};
}
2015-03-10 18:33:14 +01:00
2015-08-24 14:42:07 +02:00
$out .= '<input class="flat" type="checkbox" name="'.$keysuffix.'options_' . $key . $keyprefix . '[]" ' . ($moreparam ? $moreparam : '');
$out .= ' value="' . $obj->rowid . '"';
2015-03-10 18:33:14 +01:00
$out .= ((is_array($value_arr) && in_array($obj->rowid, $value_arr)) ? ' checked ' : '');
;
$out .= '';
2015-03-10 18:33:14 +01:00
$out .= '/>' . $labeltoshow . '<br>';
}
2015-03-10 18:33:14 +01:00
$i ++;
}
$this->db->free($resql);
} else {
print 'Error in request ' . $sql . ' ' . $this->db->lasterror() . '. Check setup of extra parameters.<br>';
}
}
$out .= '</select>';
}
elseif ($type == 'link')
{
$out='';
2015-04-06 19:46:10 +02:00
$param_list=array_keys($param['options']);
// 0 : ObjectName
// 1 : classPath
$InfoFieldList = explode(":", $param_list[0]);
dol_include_once($InfoFieldList[1]);
if ($InfoFieldList[0] && class_exists($InfoFieldList[0]))
{
$object = new $InfoFieldList[0]($this->db);
if (!empty($value)) $object->fetch($value);
$valuetoshow=$object->ref;
if ($object->element == 'societe') $valuetoshow=$object->name; // Special case for thirdparty because ref is id because name is not unique
2016-09-21 03:54:54 +02:00
$out.='<input type="text" class="flat '.$showsize.'" name="'.$keysuffix.'options_'.$key.$keyprefix.'" value="'.$valuetoshow.'" >';
}
else
{
dol_syslog('Error bad setup of extrafield', LOG_WARNING);
$out.='Error bad setup of extrafield';
}
}
2016-06-28 10:21:10 +02:00
elseif ($type == 'password')
{
2016-09-21 03:54:54 +02:00
$out='<input type="password" class="flat '.$showsize.'" name="'.$keysuffix.'options_'.$key.$keyprefix.'" value="'.$value.'" '.($moreparam?$moreparam:'').'>';
2016-06-28 10:21:10 +02:00
}
if (!empty($hidden)) {
$out='<input type="hidden" value="'.$value.'" name="'.$keysuffix.'options_'.$key.$keyprefix.'" id="'.$keysuffix.'options_'.$key.$keyprefix.'"/>';
}
2013-04-21 17:36:23 +02:00
/* Add comments
if ($type == 'date') $out.=' (YYYY-MM-DD)';
elseif ($type == 'datetime') $out.=' (YYYY-MM-DD HH:MM:SS)';
*/
return $out;
}
2013-04-21 17:36:23 +02:00
/**
* Return HTML string to put an output field into a page
*
* @param string $key Key of attribute
* @param string $value Value to show
* @param string $moreparam To add more parametes on html input tag (only checkbox use html input for output rendering)
2013-04-21 17:36:23 +02:00
* @return string Formated value
*/
function showOutputField($key,$value,$moreparam='')
{
global $conf,$langs;
2013-04-21 17:36:23 +02:00
$label=$this->attribute_label[$key];
$type=$this->attribute_type[$key];
$size=$this->attribute_size[$key];
$elementtype=$this->attribute_elementtype[$key];
$unique=$this->attribute_unique[$key];
$required=$this->attribute_required[$key];
$params=$this->attribute_param[$key];
2015-03-10 18:33:14 +01:00
$perms=$this->attribute_perms[$key];
$list=$this->attribute_list[$key];
$hidden=$this->attribute_hidden[$key]; // warning, do not rely on this. If your module need a hidden data, it must use its own table.
$showsize=0;
2013-04-21 17:36:23 +02:00
if ($type == 'date')
{
$showsize=10;
$value=dol_print_date($value,'day');
}
elseif ($type == 'datetime')
{
$showsize=19;
$value=dol_print_date($value,'dayhour');
}
elseif ($type == 'int')
{
$showsize=10;
}
2013-06-10 14:13:44 +02:00
elseif ($type == 'double')
{
if (!empty($value)) {
$value=price($value);
}
}
2013-04-21 17:36:23 +02:00
elseif ($type == 'boolean')
{
$checked='';
if (!empty($value)) {
$checked=' checked ';
2013-04-21 17:36:23 +02:00
}
$value='<input type="checkbox" '.$checked.' '.($moreparam?$moreparam:'').' readonly disabled>';
2013-04-21 17:36:23 +02:00
}
elseif ($type == 'mail')
{
$value=dol_print_email($value);
}
elseif ($type == 'phone')
{
$value=dol_print_phone($value);
}
elseif ($type == 'price')
{
$value=price($value,0,$langs,0,0,-1,$conf->currency);
2013-04-21 17:36:23 +02:00
}
elseif ($type == 'select')
{
$value=$params['options'][$value];
}
elseif ($type == 'sellist')
{
2013-04-24 23:54:02 +02:00
$param_list=array_keys($params['options']);
$InfoFieldList = explode(":", $param_list[0]);
$selectkey="rowid";
2013-04-21 17:36:23 +02:00
$keyList='rowid';
2014-08-01 16:48:16 +02:00
if (count($InfoFieldList)>=3)
{
$selectkey = $InfoFieldList[2];
$keyList=$InfoFieldList[2].' as rowid';
}
2013-04-21 17:36:23 +02:00
$fields_label = explode('|',$InfoFieldList[1]);
if(is_array($fields_label)) {
$keyList .=', ';
$keyList .= implode(', ', $fields_label);
}
$sql = 'SELECT '.$keyList;
2013-04-21 17:36:23 +02:00
$sql.= ' FROM '.MAIN_DB_PREFIX .$InfoFieldList[0];
if (strpos($InfoFieldList[4], 'extra')!==false)
{
$sql.= ' as main';
}
2016-08-16 08:54:12 +02: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)."'";
}
//$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)
{
$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
// Several field into label (eq table:code|libelle:rowid)
$fields_label = explode('|',$InfoFieldList[1]);
2014-02-04 20:49:07 +01:00
2014-09-02 12:09:43 +02:00
if(is_array($fields_label) && count($fields_label)>1)
{
foreach ($fields_label as $field_toshow)
{
$translabel='';
if (!empty($obj->$field_toshow)) {
$translabel=$langs->trans($obj->$field_toshow);
}
if ($translabel!=$field_toshow) {
$value.=dol_trunc($translabel,18).' ';
}else {
$value.=$obj->$field_toshow.' ';
}
}
}
else
{
$translabel='';
if (!empty($obj->{$InfoFieldList[1]})) {
$translabel=$langs->trans($obj->{$InfoFieldList[1]});
}
if ($translabel!=$obj->{$InfoFieldList[1]}) {
$value=dol_trunc($translabel,18);
}else {
$value=$obj->{$InfoFieldList[1]};
}
}
2013-04-21 17:36:23 +02: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')
{
$value=$params['options'][$value];
}
elseif ($type == 'checkbox')
{
$value_arr=explode(',',$value);
$value='';
if (is_array($value_arr))
{
foreach ($value_arr as $keyval=>$valueval) {
$value.=$params['options'][$valueval].'<br>';
}
}
}
elseif ($type == 'chkbxlst')
{
$value_arr = explode(',', $value);
2015-03-10 18:33:14 +01:00
$param_list = array_keys($params['options']);
$InfoFieldList = explode(":", $param_list[0]);
2015-03-10 18:33:14 +01:00
$selectkey = "rowid";
$keyList = 'rowid';
2015-03-10 18:33:14 +01:00
if (count($InfoFieldList) >= 3) {
$selectkey = $InfoFieldList[2];
$keyList = $InfoFieldList[2] . ' as rowid';
}
2015-03-10 18:33:14 +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
$sql = 'SELECT ' . $keyList;
$sql .= ' FROM ' . MAIN_DB_PREFIX . $InfoFieldList[0];
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
dol_syslog(get_class($this) . ':showOutputField:$type=chkbxlst',LOG_DEBUG);
$resql = $this->db->query($sql);
if ($resql) {
$value = ''; // value was used, so now we reste it to use it to build final output
2015-03-10 18:33:14 +01:00
while ( $obj = $this->db->fetch_object($resql) ) {
2015-03-10 18:33:14 +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) {
foreach ( $fields_label as $field_toshow ) {
$translabel = '';
if (! empty($obj->$field_toshow)) {
$translabel = $langs->trans($obj->$field_toshow);
}
if ($translabel != $field_toshow) {
$value .= dol_trunc($translabel, 18) . '<BR>';
} else {
$value .= $obj->$field_toshow . '<BR>';
}
}
} else {
$translabel = '';
if (! empty($obj->{$InfoFieldList[1]})) {
$translabel = $langs->trans($obj->{$InfoFieldList[1]});
}
if ($translabel != $obj->{$InfoFieldList[1]}) {
$value .= dol_trunc($translabel, 18) . '<BR>';
} else {
$value .= $obj->{$InfoFieldList[1]} . '<BR>';
}
}
}
}
} else
dol_syslog(get_class($this) . '::showOutputField error ' . $this->db->lasterror(), LOG_WARNING);
}
elseif ($type == 'link')
{
$out='';
// only if something to display (perf)
if ($value)
{
$param_list=array_keys($params['options']);
// 0 : ObjectName
// 1 : classPath
$InfoFieldList = explode(":", $param_list[0]);
dol_include_once($InfoFieldList[1]);
if ($InfoFieldList[0] && class_exists($InfoFieldList[0]))
{
$object = new $InfoFieldList[0]($this->db);
$object->fetch($value);
$value=$object->getNomUrl(3);
}
else
{
dol_syslog('Error bad setup of extrafield', LOG_WARNING);
$out.='Error bad setup of extrafield';
}
}
}
elseif ($type == 'text')
2015-07-21 16:39:00 +02:00
{
2015-07-28 15:14:54 +02:00
$value=dol_htmlentitiesbr($value);
2015-07-21 16:39:00 +02:00
}
2016-06-28 10:21:10 +02:00
elseif ($type == 'password')
{
$value=preg_replace('/./i','*',$value);
}
2013-04-21 17:36:23 +02:00
else
{
$showsize=round($size);
if ($showsize > 48) $showsize=48;
}
2013-04-21 17:36:23 +02:00
//print $type.'-'.$size;
$out=$value;
if (!empty($hidden)) {
$out='';
}
2013-04-21 17:36:23 +02:00
return $out;
}
/**
* Return tag to describe alignement to use for this extrafield
*
* @param string $key Key of attribute
* @return string Formated value
*/
function getAlignFlag($key)
{
global $conf,$langs;
$type=$this->attribute_type[$key];
$align='';
if ($type == 'date')
{
$align="center";
}
elseif ($type == 'datetime')
{
$align="center";
}
elseif ($type == 'int')
{
$align="right";
}
elseif ($type == 'double')
{
$align="right";
}
elseif ($type == 'boolean')
{
$align="center";
}
elseif ($type == 'radio')
{
$align="center";
}
elseif ($type == 'checkbox')
{
$align="center";
}
return $align;
}
2013-04-21 17:36:23 +02:00
/**
* Return HTML string to print separator extrafield
*
* @param string $key Key of attribute
* @return string
*/
function showSeparator($key)
{
$out = '<tr class="liste_titre"><td colspan="4"><strong>'.$this->attribute_label[$key].'</strong></td></tr>';
return $out;
}
/**
* Fill array_options property of object by extrafields value (using for data sent by forms)
2013-04-21 17:36:23 +02:00
*
* @param array $extralabels $array of extrafields
* @param object $object Object
* @param string $onlykey Only following key is filled. When we make update of only one extrafield ($action = 'update_extras'), calling page must must set this to avoid to have other extrafields 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
*/
function setOptionalsFromPost($extralabels,&$object,$onlykey='')
2013-04-21 17:36:23 +02:00
{
global $_POST, $langs;
$nofillrequired='';// For error when required field left blank
$error_field_required = array();
2013-06-10 14:13:44 +02:00
2013-04-21 17:36:23 +02:00
if (is_array($extralabels))
{
// Get extra fields
foreach ($extralabels as $key => $value)
{
if (! empty($onlykey) && $key != $onlykey) continue;
2013-04-21 17:36:23 +02:00
$key_type = $this->attribute_type[$key];
if($this->attribute_required[$key] && !GETPOST("options_$key",2))
{
$nofillrequired++;
$error_field_required[] = $value;
}
2013-04-21 17:36:23 +02:00
if (in_array($key_type,array('date','datetime')))
{
// Clean parameters
$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"]);
}
else if (in_array($key_type,array('checkbox','chkbxlst')))
2013-04-21 17:36:23 +02:00
{
$value_arr=GETPOST("options_".$key);
2013-07-23 15:52:41 +02:00
if (!empty($value_arr)) {
$value_key=implode($value_arr,',');
}else {
$value_key='';
}
2013-04-21 17:36:23 +02:00
}
2013-06-10 14:13:44 +02:00
else if (in_array($key_type,array('price','double')))
{
$value_arr=GETPOST("options_".$key);
$value_key=price2num($value_arr);
}
2013-04-21 17:36:23 +02:00
else
{
$value_key=GETPOST("options_".$key);
}
$object->array_options["options_".$key]=$value_key;
}
if($nofillrequired) {
$langs->load('errors');
2015-11-19 13:59:58 +01:00
setEventMessages($langs->trans('ErrorFieldsRequired').' : '.implode(', ',$error_field_required), null, 'errors');
return -1;
}
else {
return 1;
}
2013-04-21 17:36:23 +02:00
}
else {
return 0;
}
}
2013-06-10 14:13:44 +02:00
/**
* return array_options array for object by extrafields value (using for data send by forms)
*
2015-08-24 16:32:43 +02:00
* @param array $extralabels $array of extrafields
* @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 int 1 if array_options set / 0 if no value
2013-06-10 14:13:44 +02:00
*/
2015-08-24 16:32:43 +02:00
function getOptionalsFromPost($extralabels,$keyprefix='',$keysuffix='')
2013-06-10 14:13:44 +02:00
{
global $_POST;
$array_options = array();
if (is_array($extralabels))
{
// Get extra fields
foreach ($extralabels as $key => $value)
{
$key_type = $this->attribute_type[$key];
if (in_array($key_type,array('date','datetime')))
{
// Clean parameters
2015-08-24 16:32:43 +02:00
$value_key=dol_mktime($_POST[$keysuffix."options_".$key.$keyprefix."hour"], $_POST[$keysuffix."options_".$key.$keyprefix."min"], 0, $_POST[$keysuffix."options_".$key.$keyprefix."month"], $_POST[$keysuffix."options_".$key.$keyprefix."day"], $_POST[$keysuffix."options_".$key.$keyprefix."year"]);
2013-06-10 14:13:44 +02:00
}
else if (in_array($key_type,array('checkbox')))
{
2015-08-24 16:32:43 +02: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
2015-12-03 10:04:13 +01:00
$value_arr=(array) $value_arr;
2015-11-30 18:06:15 +01:00
$value_key=implode(',', $value_arr);
2013-06-10 14:13:44 +02:00
}
else if (in_array($key_type,array('price','double')))
{
2015-08-24 16:32:43 +02:00
$value_arr=GETPOST($keysuffix."options_".$key.$keyprefix);
2013-06-10 14:13:44 +02:00
$value_key=price2num($value_arr);
}
else
{
2015-08-24 16:32:43 +02:00
$value_key=GETPOST($keysuffix."options_".$key.$keyprefix);
2013-06-10 14:13:44 +02:00
}
2015-08-24 16:32:43 +02: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;
}
else {
return 0;
}
}
}