Merge pull request #14202 from pstructures/PSdevelop

Added Hook to showOptionals Function
This commit is contained in:
Laurent Destailleur
2020-07-10 02:54:05 +02:00
committed by GitHub

View File

@@ -6711,237 +6711,242 @@ abstract class CommonObject
*/
public function showOptionals($extrafields, $mode = 'view', $params = null, $keysuffix = '', $keyprefix = '', $onetrtd = 0)
{
global $db, $conf, $langs, $action, $form;
global $db, $conf, $langs, $action, $form, $hookmanager;
if (!is_object($form)) $form = new Form($db);
$out = '';
if (is_array($extrafields->attributes[$this->table_element]['label']) && count($extrafields->attributes[$this->table_element]['label']) > 0)
$parameters=array();
$reshook=$hookmanager->executeHooks('showOptionals',$parameters,$this,$action); // Note that $action and $object may have been modified by hook
if (empty($reshook))
{
$out .= "\n";
$out .= '<!-- showOptionalsInput --> ';
$out .= "\n";
$out = '';
$extrafields_collapse_num = '';
$e = 0;
foreach ($extrafields->attributes[$this->table_element]['label'] as $key=>$label)
if (is_array($extrafields->attributes[$this->table_element]['label']) && count($extrafields->attributes[$this->table_element]['label']) > 0)
{
// Show only the key field in params
if (is_array($params) && array_key_exists('onlykey', $params) && $key != $params['onlykey']) continue;
$out .= "\n";
$out .= '<!-- showOptionalsInput --> ';
$out .= "\n";
// @todo Add test also on 'enabled' (different than 'list' that is 'visibility')
$enabled = 1;
if ($enabled && isset($extrafields->attributes[$this->table_element]['enabled'][$key]))
$extrafields_collapse_num = '';
$e = 0;
foreach ($extrafields->attributes[$this->table_element]['label'] as $key=>$label)
{
$enabled = dol_eval($extrafields->attributes[$this->table_element]['enabled'][$key], 1);
}
if (empty($enabled)) continue;
// Show only the key field in params
if (is_array($params) && array_key_exists('onlykey', $params) && $key != $params['onlykey']) continue;
$visibility = 1;
if ($visibility && isset($extrafields->attributes[$this->table_element]['list'][$key]))
{
$visibility = dol_eval($extrafields->attributes[$this->table_element]['list'][$key], 1);
}
// @todo Add test also on 'enabled' (different than 'list' that is 'visibility')
$enabled = 1;
if ($enabled && isset($extrafields->attributes[$this->table_element]['enabled'][$key]))
{
$enabled = dol_eval($extrafields->attributes[$this->table_element]['enabled'][$key], 1);
}
if (empty($enabled)) continue;
$perms = 1;
if ($perms && isset($extrafields->attributes[$this->table_element]['perms'][$key]))
{
$perms = dol_eval($extrafields->attributes[$this->table_element]['perms'][$key], 1);
}
if (($mode == 'create') && abs($visibility) != 1 && abs($visibility) != 3) continue; // <> -1 and <> 1 and <> 3 = not visible on forms, only on list
elseif (($mode == 'edit') && abs($visibility) != 1 && abs($visibility) != 3 && abs($visibility) != 4) continue; // <> -1 and <> 1 and <> 3 = not visible on forms, only on list and <> 4 = not visible at the creation
elseif ($mode == 'view' && empty($visibility)) continue;
if (empty($perms)) continue;
// Load language if required
if (!empty($extrafields->attributes[$this->table_element]['langfile'][$key])) {
$langs->load($extrafields->attributes[$this->table_element]['langfile'][$key]);
}
$colspan = '';
if (is_array($params) && count($params) > 0) {
if (array_key_exists('cols', $params)) {
$colspan = $params['cols'];
} elseif (array_key_exists('colspan', $params)) { // For backward compatibility. Use cols instead now.
$reg = array();
if (preg_match('/colspan="(\d+)"/', $params['colspan'], $reg)) {
$colspan = $reg[1];
} else {
$colspan = $params['colspan'];
}
$visibility = 1;
if ($visibility && isset($extrafields->attributes[$this->table_element]['list'][$key]))
{
$visibility = dol_eval($extrafields->attributes[$this->table_element]['list'][$key], 1);
}
}
switch ($mode) {
case "view":
$value = $this->array_options["options_".$key.$keysuffix];
break;
case "create":
case "edit":
$getposttemp = GETPOST($keyprefix.'options_'.$key.$keysuffix, 'none'); // GETPOST can get value from GET, POST or setup of default values.
// GETPOST("options_" . $key) can be 'abc' or array(0=>'abc')
if (is_array($getposttemp) || $getposttemp != '' || GETPOSTISSET($keyprefix.'options_'.$key.$keysuffix))
{
if (is_array($getposttemp)) {
// $getposttemp is an array but following code expects a comma separated string
$value = implode(",", $getposttemp);
} else {
$value = $getposttemp;
}
} else {
$value = $this->array_options["options_".$key]; // No GET, no POST, no default value, so we take value of object.
}
//var_dump($keyprefix.' - '.$key.' - '.$keysuffix.' - '.$keyprefix.'options_'.$key.$keysuffix.' - '.$this->array_options["options_".$key.$keysuffix].' - '.$getposttemp.' - '.$value);
break;
}
$perms = 1;
if ($perms && isset($extrafields->attributes[$this->table_element]['perms'][$key]))
{
$perms = dol_eval($extrafields->attributes[$this->table_element]['perms'][$key], 1);
}
if ($extrafields->attributes[$this->table_element]['type'][$key] == 'separate')
{
$extrafields_collapse_num = '';
$extrafield_param = $extrafields->attributes[$this->table_element]['param'][$key];
if (!empty($extrafield_param) && is_array($extrafield_param)) {
$extrafield_param_list = array_keys($extrafield_param['options']);
if (($mode == 'create') && abs($visibility) != 1 && abs($visibility) != 3) continue; // <> -1 and <> 1 and <> 3 = not visible on forms, only on list
elseif (($mode == 'edit') && abs($visibility) != 1 && abs($visibility) != 3 && abs($visibility) != 4) continue; // <> -1 and <> 1 and <> 3 = not visible on forms, only on list and <> 4 = not visible at the creation
elseif ($mode == 'view' && empty($visibility)) continue;
if (empty($perms)) continue;
// Load language if required
if (!empty($extrafields->attributes[$this->table_element]['langfile'][$key])) {
$langs->load($extrafields->attributes[$this->table_element]['langfile'][$key]);
}
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) {
$extrafields_collapse_num = $extrafields->attributes[$this->table_element]['pos'][$key];
}
}
}
$out .= $extrafields->showSeparator($key, $this, ($colspan + 1));
} else {
$class = (!empty($extrafields->attributes[$this->table_element]['hidden'][$key]) ? 'hideobject ' : '');
$csstyle = '';
$colspan = '';
if (is_array($params) && count($params) > 0) {
if (array_key_exists('class', $params)) {
$class .= $params['class'].' ';
}
if (array_key_exists('style', $params)) {
$csstyle = $params['style'];
if (array_key_exists('cols', $params)) {
$colspan = $params['cols'];
} elseif (array_key_exists('colspan', $params)) { // For backward compatibility. Use cols instead now.
$reg = array();
if (preg_match('/colspan="(\d+)"/', $params['colspan'], $reg)) {
$colspan = $reg[1];
} else {
$colspan = $params['colspan'];
}
}
}
// add html5 elements
$domData = ' data-element="extrafield"';
$domData .= ' data-targetelement="'.$this->element.'"';
$domData .= ' data-targetid="'.$this->id.'"';
$html_id = (empty($this->id) ? '' : 'extrarow-'.$this->element.'_'.$key.'_'.$this->id);
if (!empty($conf->global->MAIN_EXTRAFIELDS_USE_TWO_COLUMS) && ($e % 2) == 0) { $colspan = '0'; }
if ($action == 'selectlines') { $colspan++; }
// Convert date into timestamp format (value in memory must be a timestamp)
if (in_array($extrafields->attributes[$this->table_element]['type'][$key], array('date', 'datetime')))
{
$datenotinstring = $this->array_options['options_'.$key];
if (!is_numeric($this->array_options['options_'.$key])) // For backward compatibility
{
$datenotinstring = $this->db->jdate($datenotinstring);
}
$value = GETPOSTISSET($keyprefix.'options_'.$key.$keysuffix) ?dol_mktime(GETPOST($keyprefix.'options_'.$key.$keysuffix."hour", 'int', 3), GETPOST($keyprefix.'options_'.$key.$keysuffix."min", 'int', 3), 0, GETPOST($keyprefix.'options_'.$key.$keysuffix."month", 'int', 3), GETPOST($keyprefix.'options_'.$key.$keysuffix."day", 'int', 3), GETPOST($keyprefix.'options_'.$key.$keysuffix."year", 'int', 3)) : $datenotinstring;
}
// Convert float submited string into real php numeric (value in memory must be a php numeric)
if (in_array($extrafields->attributes[$this->table_element]['type'][$key], array('price', 'double')))
{
$value = GETPOSTISSET($keyprefix.'options_'.$key.$keysuffix) ?price2num(GETPOST($keyprefix.'options_'.$key.$keysuffix, 'alpha', 3)) : $this->array_options['options_'.$key];
}
// HTML, select, integer and text add default value
if (in_array($extrafields->attributes[$this->table_element]['type'][$key], array('html', 'text', 'select', 'int')))
{
if ($action == 'create') $value = $extrafields->attributes[$this->table_element]['default'][$key];
else $value = $this->array_options['options_'.$key];
}
$labeltoshow = $langs->trans($label);
$helptoshow = $langs->trans($extrafields->attributes[$this->table_element]['help'][$key]);
$out .= '<tr '.($html_id ? 'id="'.$html_id.'" ' : '').$csstyle.' class="'.$class.$this->element.'_extras_'.$key.' trextrafields_collapse'.$extrafields_collapse_num.'" '.$domData.' >';
$out .= '<td class="';
//$out .= "titlefield";
//if (GETPOST('action', 'none') == 'create') $out.='create';
// BUG #11554 : For public page, use red dot for required fields, instead of bold label
$tpl_context = isset($params["tpl_context"]) ? $params["tpl_context"] : "none";
if ($tpl_context == "public") { // Public page : red dot instead of fieldrequired characters
$out .= '">';
if (!empty($extrafields->attributes[$this->table_element]['help'][$key])) $out .= $form->textwithpicto($labeltoshow, $helptoshow);
else $out .= $labeltoshow;
if ($mode != 'view' && !empty($extrafields->attributes[$this->table_element]['required'][$key])) $out .= '&nbsp;<font color="red">*</font>';
} else {
if ($mode != 'view' && !empty($extrafields->attributes[$this->table_element]['required'][$key])) $out .= ' fieldrequired';
$out .= '">';
if (!empty($extrafields->attributes[$this->table_element]['help'][$key])) $out .= $form->textwithpicto($labeltoshow, $helptoshow);
else $out .= $labeltoshow;
}
$out .= '</td>';
$html_id = !empty($this->id) ? $this->element.'_extras_'.$key.'_'.$this->id : '';
$out .= '<td '.($html_id ? 'id="'.$html_id.'" ' : '').'class="'.$this->element.'_extras_'.$key.'" '.($colspan ? ' colspan="'.$colspan.'"' : '').'>';
switch ($mode) {
case "view":
$out .= $extrafields->showOutputField($key, $value);
$value = $this->array_options["options_".$key.$keysuffix];
break;
case "create":
case "edit":
$out .= $extrafields->showInputField($key, $value, '', $keysuffix, '', 0, $this->id, $this->table_element);
case "create":
case "edit":
$getposttemp = GETPOST($keyprefix.'options_'.$key.$keysuffix, 'none'); // GETPOST can get value from GET, POST or setup of default values.
// GETPOST("options_" . $key) can be 'abc' or array(0=>'abc')
if (is_array($getposttemp) || $getposttemp != '' || GETPOSTISSET($keyprefix.'options_'.$key.$keysuffix))
{
if (is_array($getposttemp)) {
// $getposttemp is an array but following code expects a comma separated string
$value = implode(",", $getposttemp);
} else {
$value = $getposttemp;
}
} else {
$value = $this->array_options["options_".$key]; // No GET, no POST, no default value, so we take value of object.
}
//var_dump($keyprefix.' - '.$key.' - '.$keysuffix.' - '.$keyprefix.'options_'.$key.$keysuffix.' - '.$this->array_options["options_".$key.$keysuffix].' - '.$getposttemp.' - '.$value);
break;
}
$out .= '</td>';
/*for($ii = 0; $ii < ($colspan - 1); $ii++)
if ($extrafields->attributes[$this->table_element]['type'][$key] == 'separate')
{
$out .='<td class="'.$this->element.'_extras_'.$key.'"></td>';
}*/
$extrafields_collapse_num = '';
$extrafield_param = $extrafields->attributes[$this->table_element]['param'][$key];
if (!empty($extrafield_param) && is_array($extrafield_param)) {
$extrafield_param_list = array_keys($extrafield_param['options']);
if (!empty($conf->global->MAIN_EXTRAFIELDS_USE_TWO_COLUMS) && (($e % 2) == 1)) $out .= '</tr>';
else $out .= '</tr>';
$e++;
}
}
$out .= "\n";
// Add code to manage list depending on others
if (!empty($conf->use_javascript_ajax)) {
$out .= '
<script>
jQuery(document).ready(function() {
function showOptions(child_list, parent_list)
{
var val = $("select[name=\""+parent_list+"\"]").val();
var parentVal = parent_list + ":" + val;
if(val > 0) {
$("select[name=\""+child_list+"\"] option[parent]").hide();
$("select[name=\""+child_list+"\"] option[parent=\""+parentVal+"\"]").show();
} else {
$("select[name=\""+child_list+"\"] option").show();
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) {
$extrafields_collapse_num = $extrafields->attributes[$this->table_element]['pos'][$key];
}
}
}
$out .= $extrafields->showSeparator($key, $this, ($colspan + 1));
} else {
$class = (!empty($extrafields->attributes[$this->table_element]['hidden'][$key]) ? 'hideobject ' : '');
$csstyle = '';
if (is_array($params) && count($params) > 0) {
if (array_key_exists('class', $params)) {
$class .= $params['class'].' ';
}
if (array_key_exists('style', $params)) {
$csstyle = $params['style'];
}
}
function setListDependencies() {
jQuery("select option[parent]").parent().each(function() {
var child_list = $(this).attr("name");
var parent = $(this).find("option[parent]:first").attr("parent");
var infos = parent.split(":");
var parent_list = infos[0];
$("select[name=\""+parent_list+"\"]").change(function() {
showOptions(child_list, parent_list);
});
});
}
setListDependencies();
});
</script>'."\n";
$out .= '<!-- /showOptionalsInput --> '."\n";
// add html5 elements
$domData = ' data-element="extrafield"';
$domData .= ' data-targetelement="'.$this->element.'"';
$domData .= ' data-targetid="'.$this->id.'"';
$html_id = (empty($this->id) ? '' : 'extrarow-'.$this->element.'_'.$key.'_'.$this->id);
if (!empty($conf->global->MAIN_EXTRAFIELDS_USE_TWO_COLUMS) && ($e % 2) == 0) { $colspan = '0'; }
if ($action == 'selectlines') { $colspan++; }
// Convert date into timestamp format (value in memory must be a timestamp)
if (in_array($extrafields->attributes[$this->table_element]['type'][$key], array('date', 'datetime')))
{
$datenotinstring = $this->array_options['options_'.$key];
if (!is_numeric($this->array_options['options_'.$key])) // For backward compatibility
{
$datenotinstring = $this->db->jdate($datenotinstring);
}
$value = GETPOSTISSET($keyprefix.'options_'.$key.$keysuffix) ?dol_mktime(GETPOST($keyprefix.'options_'.$key.$keysuffix."hour", 'int', 3), GETPOST($keyprefix.'options_'.$key.$keysuffix."min", 'int', 3), 0, GETPOST($keyprefix.'options_'.$key.$keysuffix."month", 'int', 3), GETPOST($keyprefix.'options_'.$key.$keysuffix."day", 'int', 3), GETPOST($keyprefix.'options_'.$key.$keysuffix."year", 'int', 3)) : $datenotinstring;
}
// Convert float submited string into real php numeric (value in memory must be a php numeric)
if (in_array($extrafields->attributes[$this->table_element]['type'][$key], array('price', 'double')))
{
$value = GETPOSTISSET($keyprefix.'options_'.$key.$keysuffix) ?price2num(GETPOST($keyprefix.'options_'.$key.$keysuffix, 'alpha', 3)) : $this->array_options['options_'.$key];
}
// HTML, select, integer and text add default value
if (in_array($extrafields->attributes[$this->table_element]['type'][$key], array('html', 'text', 'select', 'int')))
{
if ($action == 'create') $value = $extrafields->attributes[$this->table_element]['default'][$key];
else $value = $this->array_options['options_'.$key];
}
$labeltoshow = $langs->trans($label);
$helptoshow = $langs->trans($extrafields->attributes[$this->table_element]['help'][$key]);
$out .= '<tr '.($html_id ? 'id="'.$html_id.'" ' : '').$csstyle.' class="'.$class.$this->element.'_extras_'.$key.' trextrafields_collapse'.$extrafields_collapse_num.'" '.$domData.' >';
$out .= '<td class="';
//$out .= "titlefield";
//if (GETPOST('action', 'none') == 'create') $out.='create';
// BUG #11554 : For public page, use red dot for required fields, instead of bold label
$tpl_context = isset($params["tpl_context"]) ? $params["tpl_context"] : "none";
if ($tpl_context == "public") { // Public page : red dot instead of fieldrequired characters
$out .= '">';
if (!empty($extrafields->attributes[$this->table_element]['help'][$key])) $out .= $form->textwithpicto($labeltoshow, $helptoshow);
else $out .= $labeltoshow;
if ($mode != 'view' && !empty($extrafields->attributes[$this->table_element]['required'][$key])) $out .= '&nbsp;<font color="red">*</font>';
} else {
if ($mode != 'view' && !empty($extrafields->attributes[$this->table_element]['required'][$key])) $out .= ' fieldrequired';
$out .= '">';
if (!empty($extrafields->attributes[$this->table_element]['help'][$key])) $out .= $form->textwithpicto($labeltoshow, $helptoshow);
else $out .= $labeltoshow;
}
$out .= '</td>';
$html_id = !empty($this->id) ? $this->element.'_extras_'.$key.'_'.$this->id : '';
$out .= '<td '.($html_id ? 'id="'.$html_id.'" ' : '').'class="'.$this->element.'_extras_'.$key.'" '.($colspan ? ' colspan="'.$colspan.'"' : '').'>';
switch ($mode) {
case "view":
$out .= $extrafields->showOutputField($key, $value);
break;
case "create":
case "edit":
$out .= $extrafields->showInputField($key, $value, '', $keysuffix, '', 0, $this->id, $this->table_element);
break;
}
$out .= '</td>';
/*for($ii = 0; $ii < ($colspan - 1); $ii++)
{
$out .='<td class="'.$this->element.'_extras_'.$key.'"></td>';
}*/
if (!empty($conf->global->MAIN_EXTRAFIELDS_USE_TWO_COLUMS) && (($e % 2) == 1)) $out .= '</tr>';
else $out .= '</tr>';
$e++;
}
}
$out .= "\n";
// Add code to manage list depending on others
if (!empty($conf->use_javascript_ajax)) {
$out .= '
<script>
jQuery(document).ready(function() {
function showOptions(child_list, parent_list)
{
var val = $("select[name=\""+parent_list+"\"]").val();
var parentVal = parent_list + ":" + val;
if(val > 0) {
$("select[name=\""+child_list+"\"] option[parent]").hide();
$("select[name=\""+child_list+"\"] option[parent=\""+parentVal+"\"]").show();
} else {
$("select[name=\""+child_list+"\"] option").show();
}
}
function setListDependencies() {
jQuery("select option[parent]").parent().each(function() {
var child_list = $(this).attr("name");
var parent = $(this).find("option[parent]:first").attr("parent");
var infos = parent.split(":");
var parent_list = infos[0];
$("select[name=\""+parent_list+"\"]").change(function() {
showOptions(child_list, parent_list);
});
});
}
setListDependencies();
});
</script>'."\n";
$out .= '<!-- /showOptionalsInput --> '."\n";
}
}
}
return $out;
return $out;
} // end of hook manager
}