2007-11-15 19:07:24 +01:00
|
|
|
<?php
|
|
|
|
/*******************************************************************************
|
|
|
|
* Copyright (C) 2007 Easter-eggs
|
2021-04-13 18:04:19 +02:00
|
|
|
* https://ldapsaisie.org
|
2007-11-15 19:07:24 +01:00
|
|
|
*
|
|
|
|
* Author: See AUTHORS file in top-level directory.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License version 2
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* 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, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Element select d'un formulaire pour LdapSaisie
|
|
|
|
*
|
2008-04-25 16:09:27 +02:00
|
|
|
* Cette classe définis les éléments select des formulaires.
|
|
|
|
* Elle étant la classe basic LSformElement.
|
2007-11-15 19:07:24 +01:00
|
|
|
*
|
|
|
|
* @author Benjamin Renard <brenard@easter-eggs.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
class LSformElement_select extends LSformElement {
|
|
|
|
|
2008-10-16 15:03:45 +02:00
|
|
|
var $template = 'LSformElement_select.tpl';
|
|
|
|
var $fieldTemplate = 'LSformElement_select.tpl';
|
|
|
|
|
2008-05-15 12:56:55 +02:00
|
|
|
/**
|
2015-07-25 18:23:06 +02:00
|
|
|
* Return display data of this element
|
2020-04-29 15:54:21 +02:00
|
|
|
*
|
2015-07-25 18:23:06 +02:00
|
|
|
* This method return display data of this element
|
2007-11-15 19:07:24 +01:00
|
|
|
*
|
2022-12-31 05:52:31 +01:00
|
|
|
* @return array
|
2007-11-15 19:07:24 +01:00
|
|
|
*/
|
2019-03-12 11:42:53 +01:00
|
|
|
public function getDisplay(){
|
2008-02-08 18:39:24 +01:00
|
|
|
$return = $this -> getLabelInfos();
|
2008-10-16 15:03:45 +02:00
|
|
|
$params = array();
|
2008-02-08 18:39:24 +01:00
|
|
|
if (!$this -> isFreeze()) {
|
2020-06-04 19:04:48 +02:00
|
|
|
LStemplate :: addHelpInfo(
|
2008-11-10 04:14:13 +01:00
|
|
|
'LSformElement_select',
|
|
|
|
array(
|
2009-02-14 00:06:58 +01:00
|
|
|
'clear' => _("Reset selection.")
|
2008-11-10 04:14:13 +01:00
|
|
|
)
|
|
|
|
);
|
2020-05-28 16:56:36 +02:00
|
|
|
LStemplate :: addJSscript('LSformElement_select.js');
|
2008-02-08 18:39:24 +01:00
|
|
|
}
|
2008-10-16 15:03:45 +02:00
|
|
|
$params['possible_values'] = $this -> params['text_possible_values'];
|
2010-11-15 17:33:47 +01:00
|
|
|
$params['unrecognized_value_label_format'] = _("%{value} (unrecognized value)");
|
2008-10-16 15:03:45 +02:00
|
|
|
$return['html'] = $this -> fetchTemplate(NULL,$params);
|
2008-02-08 18:39:24 +01:00
|
|
|
return $return;
|
|
|
|
}
|
2008-02-05 17:11:21 +01:00
|
|
|
|
2015-07-25 18:23:06 +02:00
|
|
|
/**
|
2019-03-12 12:14:16 +01:00
|
|
|
* Check if a value is valid for the current form element
|
2015-07-25 18:23:06 +02:00
|
|
|
*
|
|
|
|
* This method check if a value is correct, that mean if it's one
|
|
|
|
* of the possible values.
|
|
|
|
*
|
2022-12-31 05:52:31 +01:00
|
|
|
* @param string $value The value to check
|
2015-07-25 18:23:06 +02:00
|
|
|
*
|
2022-12-31 05:52:31 +01:00
|
|
|
* @return string or False The value's label or False if this value is incorrect
|
2015-07-25 18:23:06 +02:00
|
|
|
*/
|
2019-03-12 12:14:16 +01:00
|
|
|
public function isValidValue($value) {
|
|
|
|
return self :: _isValidValue($value, $this -> getParam('text_possible_values'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a value is valid against specified possible values
|
|
|
|
*
|
|
|
|
* This method check if a value is correct, that mean if it's one
|
|
|
|
* of the possible values.
|
|
|
|
*
|
2022-12-31 05:52:31 +01:00
|
|
|
* @param string $value The value to check
|
|
|
|
* @param array $possible_values The possible values
|
2019-03-12 12:14:16 +01:00
|
|
|
*
|
2022-12-31 05:52:31 +01:00
|
|
|
* @return string or False The value's label or False if this value is incorrect
|
2019-03-12 12:14:16 +01:00
|
|
|
*/
|
|
|
|
public static function _isValidValue($value, $possible_values) {
|
2017-05-31 16:05:25 +02:00
|
|
|
if (!is_array($possible_values)) {
|
2019-03-12 12:14:16 +01:00
|
|
|
return False;
|
2017-05-31 16:05:25 +02:00
|
|
|
}
|
2015-07-25 18:23:06 +02:00
|
|
|
|
|
|
|
$ret=False;
|
|
|
|
if (is_array($possible_values) && isset($value)) {
|
|
|
|
foreach($possible_values as $key => $name) {
|
|
|
|
if (is_array($name)) {
|
|
|
|
if (!is_array($name['possible_values'])) continue;
|
|
|
|
foreach($name['possible_values'] as $k => $v) {
|
|
|
|
if ($k==$value) {
|
|
|
|
$ret=$v;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($ret) break;
|
|
|
|
}
|
|
|
|
elseif ($key==$value) {
|
|
|
|
$ret=$name;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ($ret) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
2020-07-07 12:43:55 +02:00
|
|
|
/**
|
|
|
|
* CLI autocompleter for form element attribute values
|
|
|
|
*
|
2022-12-31 05:52:31 +01:00
|
|
|
* @param array &$opts Array of avalaible autocomplete options
|
|
|
|
* @param string $comp_word The (unquoted) command word to autocomplete
|
|
|
|
* @param string $comp_word The current attribute value in command word to autocomplete (optional, default: empty string)
|
|
|
|
* @param string $comp_word The multiple value delimiter (optional, default: "|")
|
|
|
|
* @param string $comp_word The quote character detected (optional, default: empty string)
|
2020-07-07 12:43:55 +02:00
|
|
|
*
|
2022-12-31 05:52:31 +01:00
|
|
|
* @return void
|
2020-07-07 12:43:55 +02:00
|
|
|
*/
|
|
|
|
public function autocomplete_attr_values(&$opts, $comp_word, $attr_value="", $multiple_value_delimiter="|", $quote_char='') {
|
2021-08-25 18:02:37 +02:00
|
|
|
// Split attribute values and retrieved splited value in $attr_values and $last_attr_value
|
2020-07-07 12:43:55 +02:00
|
|
|
if (!$this -> split_autocomplete_attr_values($attr_value, $multiple_value_delimiter, $attr_values, $last_attr_value))
|
|
|
|
return;
|
|
|
|
|
|
|
|
foreach ($this -> params['text_possible_values'] as $value => $label) {
|
|
|
|
if (is_array($label)) {
|
|
|
|
foreach ($label['possible_values'] as $v => $l) {
|
|
|
|
$this -> add_autocomplete_attr_value_opts($opts, $attr_values, $v, $multiple_value_delimiter, $quote_char);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$this -> add_autocomplete_attr_value_opts($opts, $attr_values, $value, $multiple_value_delimiter, $quote_char);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-05 12:22:32 +01:00
|
|
|
|
|
|
|
/**
|
2021-08-25 18:02:37 +02:00
|
|
|
* Retrieve value as return in API response
|
2021-02-05 12:22:32 +01:00
|
|
|
*
|
2022-12-31 05:52:31 +01:00
|
|
|
* @param boolean $details If true, returned values will contain details if this field type
|
2021-02-05 12:22:32 +01:00
|
|
|
* support it (optional, default: false)
|
|
|
|
*
|
2022-12-31 05:52:31 +01:00
|
|
|
* @return mixed API value(s) or null/empty array if no value
|
2021-02-05 12:22:32 +01:00
|
|
|
*/
|
|
|
|
public function getApiValue($details=false) {
|
|
|
|
$values = array();
|
|
|
|
foreach(ensureIsArray($this -> values) as $value) {
|
|
|
|
$label = $this -> _isValidValue($value, $this -> params['text_possible_values']);
|
|
|
|
if ($label === false) continue;
|
|
|
|
if ($details)
|
|
|
|
$values[] = array(
|
|
|
|
'label' => $label,
|
|
|
|
'value' => $value,
|
|
|
|
);
|
|
|
|
else
|
|
|
|
$values[] = $value;
|
|
|
|
}
|
|
|
|
if ($this -> isMultiple()) {
|
|
|
|
return ensureIsArray($values);
|
|
|
|
}
|
|
|
|
if (!$values)
|
|
|
|
return null;
|
|
|
|
return $values[0];
|
|
|
|
}
|
|
|
|
|
2007-11-15 19:07:24 +01:00
|
|
|
}
|
|
|
|
|
2015-07-25 18:23:06 +02:00
|
|
|
/**
|
|
|
|
* LSformElement_select_checkIsValidValue template function
|
|
|
|
*
|
|
|
|
* This function permit to check during template processing
|
|
|
|
* if a value is valid. This function get as parameters
|
|
|
|
* (in $params) :
|
|
|
|
* - $value : the value to check
|
|
|
|
* - $possible_values : the possible values of the element
|
|
|
|
* As return, this function assign two template variables :
|
|
|
|
* - LSformElement_select_isValidValue :
|
|
|
|
* Boolean defining if the value is valid
|
|
|
|
* - LSformElement_select_isValidValue_label :
|
|
|
|
* The value's label
|
|
|
|
*
|
2022-12-31 05:52:31 +01:00
|
|
|
* @param array $params The template function parameters
|
|
|
|
* @param Smarty $template Smarty object
|
2015-07-25 18:23:06 +02:00
|
|
|
*
|
2022-12-31 05:52:31 +01:00
|
|
|
* @return void
|
2015-07-25 18:23:06 +02:00
|
|
|
**/
|
2022-12-31 04:09:56 +01:00
|
|
|
function LSformElement_select_checkIsValidValue($params, $template) {
|
|
|
|
$ret = LSformElement_select :: _isValidValue($params['value'], $params['possible_values']);
|
2015-07-25 18:23:06 +02:00
|
|
|
|
|
|
|
if ($ret===False) {
|
|
|
|
$label='';
|
|
|
|
$ret=false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$label=$ret;
|
|
|
|
$ret=true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$template -> assign('LSformElement_select_isValidValue',$ret);
|
|
|
|
$template -> assign('LSformElement_select_isValidValue_label',$label);
|
|
|
|
}
|
|
|
|
LStemplate :: registerFunction('LSformElement_select_checkIsValidValue','LSformElement_select_checkIsValidValue');
|