class ExtraFields (View source)

Class to manage standard extra fields

Properties

DoliDB $db
array<string, array{label: array<string, string>, type: array<string, string>, size: array<string, string>, default: array<string, string>, computed: array<string, string>, unique: array<string, int>, required: array<string, int>, param: array<string, mixed>, perms: array<string, mixed>, list: array<string, int|string>, pos: array<string, int>, totalizable: array<string, int>, help: array<string, string>, printable: array<string, int>, enabled: array<string, int>, langfile: array<string, string>, css: array<string, string>, csslist: array<string, string>, cssview: array<string, string>, hidden: array<string, int>, mandatoryfieldsofotherentities: array<string, string>, alwayseditable: array<string, int<0, 1>>, emptyonclone: array<string, int<0, 1>>, loaded?: int, count: int, aiprompt: array<string, string>}> $attributes
array<string, bool|int<0, 1>>|null $expand_display
string $error
string[] $errors
string $errno
static array<string, string> $type2label
static array<string, array<string, string>> $geoDataTypes

Methods

__construct($db)

Constructor

addExtraField($attrname, string $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, $moreparams = array(), $aiprompt = "", $emptyonclone = 0)

Add a new extra field parameter

updateExtraField($attrname, string $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, $moreparams = array(), int<0, 1> $emptyonclone = 0)

Update an existing extra field parameter

delete(string $attrname, $elementtype = '')

Delete an optional attribute

update($attrname, string $label, $type, $length, $elementtype, $unique = 0, $required = 0, $pos = 0, $param = array(), $alwayseditable = 0, $perms = '', $list = '', $help = '', $default = '', $computed = '', $entity = '', $langfile = '', $enabled = '1', $totalizable = 0, $printable = 0, $moreparams = array(), $aiprompt = "", int<0, 1> $emptyonclone = 0)

Modify type of a personalized attribute

fetch_name_optionals_label($elementtype, bool $forceload = false, $attrname = '')

Load the array of extrafields definition $this->attributes

string
showInputField(string $key, string|array{start: int, end: int} $value, string $moreparam = '', string $keysuffix = '', string $keyprefix = '', string $morecss = '', int|CommonObject $object = 0, string $extrafieldsobjectkey = '', int $mode = 0)

Return HTML string to put an input field into a page Code very similar with showInputField of common object

string
showOutputField(string $key, string $value, string $moreparam = '', string $extrafieldsobjectkey = '', Translate|null $outputlangs = null, CommonObject|null $object = null, string $mode = '')

Return HTML string to put an output field into a page

string
getAlignFlag(string $key, string $extrafieldsobjectkey = '') deprecated

Return the CSS to use for this extrafield into list

string
getCSSClass(string $key, string $extrafieldsobjectkey = '', string $mode = 'csslist')

Return the CSS to use for this extrafield into list

string
showSeparator(string $key, object $object, int $colspan = 2, string $display_type = 'card', string $mode = '')

Return HTML string to print separator extrafield

int
setOptionalsFromPost(null $extralabels, CommonObject $object, string $onlykey = '', int $todefaultifmissing = 0)

Fill array_options property of object by extrafields value (using for data sent by forms)

array<string, mixed>|int<0, 0>
getOptionalsFromPost(array<string, mixed>|string $extrafieldsobjectkey, string $keysuffix = '', string $keyprefix = '')

return array_options array of data of extrafields value of object sent by a search form

static string[]
getListOfTypesLabels()

Return array with all possible types and labels of extrafields

static bool
isEmptyValue(null|int|float|string|array<int|string, mixed> $v, string $type)

Return if a value is "empty" for a mandatory vision.

Details

__construct($db)

Constructor

@param DoliDB $db Database handler

Parameters

$db

addExtraField($attrname, string $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, $moreparams = array(), $aiprompt = "", $emptyonclone = 0)

Add a new extra field parameter

@param string $attrname Code of attribute

Parameters

$attrname
string $label

label of attribute @param string $type Type of attribute ('boolean', 'int', 'varchar', 'text', 'html', 'date', 'datetime', 'duration', 'price', 'pricecy', 'phone', 'email', 'password', 'url', 'select', 'checkbox', 'separate',...) @param int $pos Position of attribute @param string $size Size/length definition of attribute ('5', '24,8', ...). For float, it contains 2 numeric separated with a comma. @param string $elementtype Element type. Same value than object->table_element (Example 'member', 'product', 'thirdparty', ...) @param int<0,1> $unique Is field unique or not @param int<0,1> $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,mixed|mixed[]>|string $param Params for field (ex for select list : array('options' => array(value'=>'label of option')) ) @param int<0,1> $alwayseditable Is attribute always editable regardless of the document status @param string $perms Permission to check @param string $list Visibility ('0'=never visible, '1'=visible on list+forms, '2'=list only, '3'=form only or 'eval string') @param string $help Text with help tooltip @param string $computed Computed value @param string $entity Entity of extrafields (for multicompany modules) @param string $langfile Language file @param string $enabled Condition to have the field enabled or not @param int<0,1> $totalizable Is a measure. Must show a total on lists @param int<0,1> $printable Is extrafield displayed on PDF @param array<string,mixed> $moreparams More parameters. Example: array('css'=>, 'csslist'=>Css on list, 'cssview'=>...) @param string $aiprompt Ai prompt value @param int<0,1> $emptyonclone Is attribute to be emptied after object clone @return int Return integer <=0 if KO, >0 if OK

$type
$pos
$size
$elementtype
$unique
$required
$default_value
$param
$alwayseditable
$perms
$list
$help
$computed
$entity
$langfile
$enabled
$totalizable
$printable
$moreparams
$aiprompt
$emptyonclone

updateExtraField($attrname, string $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, $moreparams = array(), int<0, 1> $emptyonclone = 0)

Update an existing extra field parameter

@param string $attrname Code of attribute

Parameters

$attrname
string $label

label of attribute @param string $type Type of attribute ('boolean', 'int', 'varchar', 'text', 'html', 'date', 'datetime', 'duration', 'price', 'pricecy', 'phone', 'email', 'password', 'url', 'select', 'checkbox', 'separate',...) @param int $pos Position of attribute @param string $size Size/length definition of attribute ('5', '24,8', ...). For float, it contains 2 numeric separated with a comma. @param string $elementtype Element type. Same value than object->table_element (Example 'member', 'product', 'thirdparty', ...) @param int<0,1> $unique Is field unique or not @param int<0,1> $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,mixed|mixed[]>|string $param Params for field (ex for select list : array('options' => array(value'=>'label of option')) ) @param int<0,1> $alwayseditable Is attribute always editable regardless of the document status @param string $perms Permission to check @param string $list Visibility ('0'=never visible, '1'=visible on list+forms, '2'=list only, '3'=form only or 'eval string') @param string $help Text with help tooltip @param string $computed Computed value @param string $entity Entity of extrafields (for multicompany modules) @param string $langfile Language file @param string $enabled Condition to have the field enabled or not @param int<0,1> $totalizable Is a measure. Must show a total on lists @param int<0,1> $printable Is extrafield displayed on PDF @param array<string,mixed> $moreparams More parameters. Example: array('css'=>, 'csslist'=>Css on list, 'cssview'=>...)

$type
$pos
$size
$elementtype
$unique
$required
$default_value
$param
$alwayseditable
$perms
$list
$help
$computed
$entity
$langfile
$enabled
$totalizable
$printable
$moreparams
int<0, 1> $emptyonclone

Is attribute to be emptied after object clone @return int Return integer <=0 if KO, >0 if OK

delete(string $attrname, $elementtype = '')

Delete an optional attribute

Parameters

string $attrname

Code of attribute to delete @param string $elementtype Element type ('member', 'product', 'thirdparty', 'contact', ...) @return int Return integer < 0 if KO, 0 if nothing is done, 1 if OK

$elementtype

update($attrname, string $label, $type, $length, $elementtype, $unique = 0, $required = 0, $pos = 0, $param = array(), $alwayseditable = 0, $perms = '', $list = '', $help = '', $default = '', $computed = '', $entity = '', $langfile = '', $enabled = '1', $totalizable = 0, $printable = 0, $moreparams = array(), $aiprompt = "", int<0, 1> $emptyonclone = 0)

Modify type of a personalized attribute

@param string $attrname Name of attribute

Parameters

$attrname
string $label

Label of attribute @param string $type Type of attribute ('boolean', 'int', 'varchar', 'text', 'html', 'date', 'datetime', 'duration', 'price', 'phone', 'email', 'password', 'url', 'select', 'checkbox', ...) @param string $length Size/length of attribute ('5', '24,8', ...) @param string $elementtype Element type ('member', 'product', 'thirdparty', 'contact', ...) @param int<0,1> $unique Is field unique or not @param int<0,1> $required Is field required or not @param int<0,1> $pos Position of attribute @param array<string,mixed|mixed[]> $param Params for field (ex for select list : array('options' => array(value'=>'label of option')) ) @param int<0,1> $alwayseditable Is attribute always editable regardless of the document status @param string $perms Permission to check @param string $list Visibility @param string $help Help on tooltip @param string $default Default value (in database. use the default_value feature for default value on screen). @param string $computed Computed value @param string $entity Entity of extrafields @param string $langfile Language file @param string $enabled Condition to have the field enabled or not @param int<0,1> $totalizable Is extrafield totalizable on list @param int<0,1> $printable Is extrafield displayed on PDF @param array<string,mixed> $moreparams More parameters. Example: array('css'=>, 'csslist'=>, 'cssview'=>...) @param string $aiprompt Ai prompt value

$type
$length
$elementtype
$unique
$required
$pos
$param
$alwayseditable
$perms
$list
$help
$default
$computed
$entity
$langfile
$enabled
$totalizable
$printable
$moreparams
$aiprompt
int<0, 1> $emptyonclone

Is attribute to be emptied after object clone @return int >0 if OK, <=0 if KO @throws Exception

fetch_name_optionals_label($elementtype, bool $forceload = false, $attrname = '')

Load the array of extrafields definition $this->attributes

@param  string      $elementtype        Type of element ('all' = all or $object->table_element like 'adherent', 'commande', 'thirdparty', 'facture', 'propal', 'product', ...).

Parameters

$elementtype
bool $forceload

Force load of extra fields whatever is status of cache. @param string $attrname The name of the attribute. @return array<string,string> Array of attributes keys+label for all extra fields. in addition $this->attributes will be completed with array{label:array<string,string>,type:array<string,string>,size:array<string,string>,default:array<string,string>,computed:array<string,string>,unique:array<string,int>,required:array<string,int>,param:array<string,mixed>,perms:array<string,mixed[]>,list:array<string,int>|array<string,string>,pos:array<string,int>,totalizable:array<string,int>,help:array<string,string>,printable:array<string,int>,enabled:array<string,int>,langfile:array<string,string>,css:array<string,string>,csslist:array<string,string>,hidden:array<string,int>,mandatoryfieldsofotherentities?:array<string,string>,loaded?:int,count:int} Note: count set as present to avoid static analysis notices

$attrname

string showInputField(string $key, string|array{start: int, end: int} $value, string $moreparam = '', string $keysuffix = '', string $keyprefix = '', string $morecss = '', int|CommonObject $object = 0, string $extrafieldsobjectkey = '', int $mode = 0)

Return HTML string to put an input field into a page Code very similar with showInputField of common object

Parameters

string $key

Key of attribute

string|array{start: int, end: int} $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); for dates in filter mode, a range array('start'=>, 'end'=>) should be provided

string $moreparam

To add more parameters on html input tag

string $keysuffix

Suffix string to add after name and id of field (can be used to avoid duplicate names)

string $keyprefix

Prefix string to add before name and id of field (can be used to avoid duplicate names)

string $morecss

More css (to defined size of field. Old behaviour: may also be a numeric)

int|CommonObject $object

Current object or object ID. Preferably, pass the object itself.

string $extrafieldsobjectkey

The key to use to store retrieved data (commonly $object->table_element)

int $mode

1=Used for search filters

Return Value

string

string showOutputField(string $key, string $value, string $moreparam = '', string $extrafieldsobjectkey = '', Translate|null $outputlangs = null, CommonObject|null $object = null, string $mode = '')

Return HTML string to put an output field into a page

Parameters

string $key

Key of attribute

string $value

Value to show

string $moreparam

To add more parameters on html input tag (only checkbox use html input for output rendering)

string $extrafieldsobjectkey

Required (for example $object->table_element).

Translate|null $outputlangs Output
CommonObject|null $object

The parent object of field to show

string $mode

'' or 'list'

Return Value

string

Formatted value

string getAlignFlag(string $key, string $extrafieldsobjectkey = '') deprecated

deprecated Use getCSSClass()

Return the CSS to use for this extrafield into list

Parameters

string $key

Key of attribute

string $extrafieldsobjectkey

If defined, use the new method to get extrafields data

Return Value

string

Formatted value

string getCSSClass(string $key, string $extrafieldsobjectkey = '', string $mode = 'csslist')

Return the CSS to use for this extrafield into list

Parameters

string $key

Key of attribute

string $extrafieldsobjectkey

If defined, use the new method to get extrafields data

string $mode

'csslist' (used on td into table list), 'css' (used on create/update), 'cssview' (used on view)

Return Value

string

Formatted value

string showSeparator(string $key, object $object, int $colspan = 2, string $display_type = 'card', string $mode = '')

Return HTML string to print separator extrafield

Parameters

string $key

Key of attribute

object $object Object
int $colspan

Value of colspan to use (it must includes the first column with title)

string $display_type

"card" for form display, "line" for document line display (extrafields on propal line, order line, etc...)

string $mode

Show output ('view') or input ('create' or 'edit') for extrafield

Return Value

string

HTML code with line for separator

int setOptionalsFromPost(null $extralabels, CommonObject $object, string $onlykey = '', int $todefaultifmissing = 0)

Fill array_options property of object by extrafields value (using for data sent by forms)

Parameters

null $extralabels

Deprecated (old $array of extrafields, now set this to null)

CommonObject $object Object
string $onlykey

Only some keys are filled: '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 several extrafields ($action = 'update'), calling page can set this to avoid to have fields not into POST being reset.

int $todefaultifmissing

1=Set value to the default value in database if value is mandatory and missing

Return Value

int

1 if array_options set, 0 if no value, -1 if error (field required missing for example)

array<string, mixed>|int<0, 0> getOptionalsFromPost(array<string, mixed>|string $extrafieldsobjectkey, string $keysuffix = '', string $keyprefix = '')

return array_options array of data of extrafields value of object sent by a search form

Parameters

array<string, mixed>|string $extrafieldsobjectkey

array of extrafields (old usage) or value of object->table_element (new usage)

string $keysuffix

Suffix string to add into name and id of field (can be used to avoid duplicate names)

string $keyprefix

Prefix string to add into name and id of field (can be used to avoid duplicate names)

Return Value

array<string, mixed>|int<0, 0>

array_options set or 0 if no value

static string[] getListOfTypesLabels()

Return array with all possible types and labels of extrafields

Return Value

string[]

static bool isEmptyValue(null|int|float|string|array<int|string, mixed> $v, string $type)

Return if a value is "empty" for a mandatory vision.

Parameters

null|int|float|string|array<int|string, mixed> $v

Value to test

string $type

Type of extrafield 'sellist', 'link', 'select', ...

Return Value

bool

True is value is an empty value, not allowed for a mandatory field.