function form_select_tree($title, $input_name, $input_id, $input_value = FALSE, $array = FALSE, $db, $name_col, $id_col, $cat_col, $self_id = FALSE, $id = FALSE, $level = FALSE, $index = FALSE, $data = FALSE) { global $_POST, $locale; if (!defined("SELECT2")) { define("SELECT2", TRUE); add_to_footer("<script src='" . DYNAMICS . "assets/select2/select2.min.js' /></script>\n"); add_to_head("<link href='" . DYNAMICS . "assets/select2/select2.css' rel='stylesheet' />\n"); } $title2 = isset($title) && !empty($title) ? stripinput($title) : ucfirst(strtolower(str_replace("_", " ", $input_name))); $input_value = isset($input_value) ? stripinput($input_value) : ''; if (isset($name) && $name != "") { $name = stripinput($name); } else { $name = ""; } if (isset($id_col) && $id_col != "") { $id_col = stripinput($id_col); } else { $id_col = ""; } if (isset($cat_col) && $cat_col != "") { $cat_col = stripinput($cat_col); } else { $cat_col = ""; } if (!is_array($array)) { $array = array(); $state_validation = ""; $required = 0; $safemode = 0; $allowclear = ""; $placeholder = $locale['choose']; $deactivate = ""; $labeloff = ""; $multiple = ""; $stacking = 0; $width = "style='width:90%;'"; $add_parent_opts = 0; $no_root = 0; $inline = ''; $include_opts = ''; // for selective input. will not show items if value not in array. } else { $multiple = array_key_exists('is_multiple', $array) ? $array['is_multiple'] : ""; $placeholder = array_key_exists('placeholder', $array) ? $array['placeholder'] : $locale['choose']; $allowclear = !empty($placeholder) && $multiple !== 1 ? "allowClear:true" : ""; $deactivate = array_key_exists('deactivate', $array) ? $array['deactivate'] : ""; $labeloff = array_key_exists('labeloff', $array) ? $array['labeloff'] : ""; $helper_text = array_key_exists("helper", $array) ? $array['helper'] : ""; $required = array_key_exists('required', $array) && $array['required'] == 1 ? 1 : 0; $safemode = array_key_exists('safemode', $array) && $array['safemode'] == 1 ? 1 : 0; $stacking = array_key_exists('stacking', $array) && $array['stacking'] == 1 ? 1 : 0; $add_parent_opts = array_key_exists('add_parent_opts', $array) && $array['add_parent_opts'] == 1 ? 1 : 0; $no_root = array_key_exists('no_root', $array) && $array['no_root'] == 1 ? 1 : 0; $width = array_key_exists('width', $array) ? $array['width'] : ''; $multiple = $multiple == 1 ? "multiple" : ""; $inline = array_key_exists("rowstart", $array) ? 1 : 0; $include_opts = array_key_exists("include_opts", $array) ? $array['include_opts'] : ''; } // Patterns if (!$level) { $level = 0; $html = ""; $html .= "<div id='{$input_id}-field' class='form-group m-b-0'>\n"; $html .= "<label class='control-label " . ($inline ? "col-sm-3 col-md-3 col-lg-3" : '') . "' for='{$input_id}'>{$title} " . ($required == 1 ? "<span class='required'>*</span>" : '') . "</label>\n"; $html .= $inline ? "<div class='col-sm-9 col-md-9 col-lg-9'>\n" : ""; } $opt_pattern = str_repeat("—", $level); // no need to count here, it's cosmetics. if ($level == "0") { add_to_jquery("\n \$('#" . $input_id . "').select2({\n placeholder: '" . $placeholder . "',\n {$allowclear}\n });\n "); $html .= "<select name='{$input_name}' id='{$input_id}' " . ($width ? "style='width: {$width}'" : "style='min-width: 250px'") . " " . ($deactivate == "1" && isnum($deactivate) ? "readonly" : "") . " {$multiple}>"; if ($allowclear) { $html .= "<option value=''></option>"; } if ($no_root !== 1) { // api options to remove root from selector. used in items creation. $this_select = ''; if ($input_value !== NULL) { if ($input_value == '0') { $this_select = "selected"; } } $html .= $add_parent_opts == '1' ? "<option value='0' " . $this_select . ">{$opt_pattern} " . $locale['parent'] . "</option>\n" : "<option value='0' {$this_select}>{$opt_pattern} Root</option>\n"; } $index = dbquery_tree($db, $id_col, $cat_col); $data = dbquery_tree_data($db, $id_col, $cat_col); } if (!$id) { $id = 0; } if (isset($index[$id])) { foreach ($index[$id] as $key => $value) { $html =& $html; $name = $data[$value][$name_col]; $select = $input_value !== "" && $input_value == $value ? 'selected' : ''; if (isset($include_opts) && is_array($include_opts)) { $html .= in_array($value, $include_opts) ? "<option value='{$value}' " . $select . " " . ($self_id == $value ? 'disabled' : '') . ">{$opt_pattern} {$name} " . ($self_id == $value ? '(Current Item)' : '') . "</option>\n" : ''; } else { $html .= "<option value='{$value}' " . $select . " " . ($self_id == $value ? 'disabled' : '') . ">{$opt_pattern} {$name} " . ($self_id == $value ? '(Current Item)' : '') . "</option>\n"; } if (isset($index[$value])) { $html .= form_select_tree($title, $input_name, $input_id, $input_value, $array, $db, $name_col, $id_col, $cat_col, $self_id, $value, $level + 1, $index, $data); } } } if (!$level) { $html .= "</select>"; $html .= "<br/><div id='{$input_id}-help' style='display:inline-block !important;'></div>"; $html .= "<input type='hidden' name='def[{$input_name}]' value='[type=dropdown],[title={$title2}],[id={$input_id}],[required={$required}],[safemode={$safemode}]' readonly>"; $html .= "</div>\n"; } return $html; }
/** * Select2 hierarchy * Returns a full hierarchy nested dropdown. * @param $input_name * @param string $label * @param bool $input_value * @param array $options * @param $db - your db * @param $name_col - the option text to show * @param $id_col - unique id * @param $cat_col - parent id * ## The rest of the Params are used by the function itself -- no need to handle ## * @param bool $self_id - not required * @param bool $id - not required * @param bool $level - not required * @param bool $index - not required * @param bool $data - not required * @return string */ function form_select_tree($input_name, $label = "", $input_value = FALSE, array $options = array(), $db, $name_col, $id_col, $cat_col, $self_id = FALSE, $id = FALSE, $level = FALSE, $index = FALSE, $data = FALSE) { global $defender, $locale; if (!defined("SELECT2")) { define("SELECT2", TRUE); add_to_footer("<script src='" . DYNAMICS . "assets/select2/select2.min.js' /></script>\n"); add_to_head("<link href='" . DYNAMICS . "assets/select2/select2.css' rel='stylesheet' />\n"); } $title = $label ? stripinput($label) : ucfirst(strtolower(str_replace("_", " ", $input_name))); $default_options = array('required' => FALSE, 'regex' => '', 'input_id' => $input_name, 'placeholder' => $locale['choose'], 'deactivate' => FALSE, 'safemode' => FALSE, 'allowclear' => FALSE, 'multiple' => FALSE, 'width' => '250px', 'keyflip' => FALSE, 'tags' => FALSE, 'jsonmode' => FALSE, 'chainable' => FALSE, 'maxselect' => FALSE, 'error_text' => $locale['error_input_default'], 'class' => '', 'inline' => FALSE, 'tip' => '', 'delimiter' => ',', 'callback_check' => '', 'file' => '', 'parent_value' => $locale['root'], 'add_parent_opts' => FALSE, 'disable_opts' => '', 'hide_disabled' => FALSE, 'no_root' => FALSE, 'show_current' => FALSE, 'query' => ''); $options += $default_options; $options['input_id'] = trim($options['input_id'], "[]"); if ($options['multiple']) { if ($input_value) { $input_value = construct_array($input_value, 0, $options['delimiter']); } else { $input_value = array(); } } if (!$options['width']) { $options['width'] = $default_options['width']; } $allowclear = $options['placeholder'] && $options['multiple'] || $options['allowclear'] ? "allowClear:true" : ''; $disable_opts = ''; if ($options['disable_opts']) { $disable_opts = is_array($options['disable_opts']) ? $options['disable_opts'] : explode(',', $options['disable_opts']); } /* Child patern */ $opt_pattern = str_repeat("—", $level); if (!$level) { $level = 0; if (!isset($index[$id])) { $index[$id] = array('0' => $locale['no_opts']); } $error_class = ""; if ($defender->inputHasError($input_name)) { $error_class = "has-error "; if (!empty($options['error_text'])) { $new_error_text = $defender->getErrorText($input_name); if (!empty($new_error_text)) { $options['error_text'] = $new_error_text; } addNotice("danger", "<strong>{$title}</strong> - " . $options['error_text']); } } $html = "<div id='" . $options['input_id'] . "-field' class='form-group " . $error_class . $options['class'] . "' " . ($options['inline'] && $options['width'] && !$label ? "style='width: " . $options['width'] . "'" : '') . ">\n"; $html .= $label ? "<label class='control-label " . ($options['inline'] ? "col-xs-12 col-sm-3 p-l-0" : 'col-xs-12 p-l-0') . "' for='" . $options['input_id'] . "'>{$label} " . ($options['required'] == TRUE ? "<span class='required'>*</span>" : '') . " " . ($options['tip'] ? "<i class='pointer fa fa-question-circle' label=\"" . $options['tip'] . "\"></i>" : '') . "</label>\n" : ''; $html .= $options['inline'] ? "<div class='col-xs-12 " . ($label ? "col-sm-9 col-md-9 col-lg-9" : "col-sm-12") . "'>\n" : ""; } if ($level == 0) { $html =& $html; add_to_jquery("\n\t\t\$('#" . $options['input_id'] . "').select2({\n\t\tplaceholder: '" . $options['placeholder'] . "',\n\t\t{$allowclear}\n\t\t});\n\t\t"); if (is_array($input_value) && $options['multiple']) { // stores as value; $vals = ''; foreach ($input_value as $arr => $val) { $vals .= $arr == count($input_value) - 1 ? "'{$val}'" : "'{$val}',"; } add_to_jquery("\$('#" . $options['input_id'] . "').select2('val', [{$vals}]);"); } $html .= "<select name='{$input_name}' id='" . $options['input_id'] . "' style='width: " . ($options['width'] ? $options['width'] : $default_options['width']) . "' " . ($options['deactivate'] ? " disabled" : "") . ($options['multiple'] ? " multiple" : "") . ">"; $html .= $options['allowclear'] ? "<option value=''></option>" : ''; if ($options['no_root'] == FALSE) { // api options to remove root from selector. used in items creation. $this_select = ''; if ($input_value !== NULL) { if ($input_value !== '') { $this_select = 'selected'; } } $html .= $options['add_parent_opts'] == TRUE ? "<option value='0' " . $this_select . ">{$opt_pattern} " . $locale['parent'] . "</option>\n" : "<option value='0' " . $this_select . " >{$opt_pattern} " . $options['parent_value'] . "</option>\n"; } $index = dbquery_tree($db, $id_col, $cat_col, $options['query']); $data = dbquery_tree_data($db, $id_col, $cat_col, $options['query']); } if (!$id) { $id = 0; } if (isset($index[$id])) { foreach ($index[$id] as $key => $value) { //$hide = $disable_branch && $value == $self_id ? 1 : 0; $html =& $html; $name = $data[$value][$name_col]; $name = PHPFusion\QuantumFields::parse_label($name); $select = $input_value !== "" && $input_value == $value ? 'selected' : ''; $disabled = $disable_opts && in_array($value, $disable_opts) ? TRUE : FALSE; $hide = $disabled && $options['hide_disabled'] ? TRUE : FALSE; // do a disable for filter_opts item. $html .= !$hide ? "<option value='{$value}' " . $select . " " . ($disable_opts && in_array($value, $disable_opts) ? 'disabled' : '') . " >{$opt_pattern} {$name} " . ($options['show_current'] && $self_id == $value ? '(Current Item)' : '') . "</option>\n" : ''; if (isset($index[$value]) && !$hide) { $html .= form_select_tree($input_name, $label, $input_value, $options, $db, $name_col, $id_col, $cat_col, $self_id, $value, $level + TRUE, $index, $data); } } } if (!$level) { $html =& $html; $html .= "</select>"; $html .= $options['required'] == 1 && $defender->inputHasError($input_name) || $defender->inputHasError($input_name) ? "<div id='" . $options['input_id'] . "-help' class='label label-danger p-5 display-inline-block'>" . $options['error_text'] . "</div>" : ""; $html .= $options['inline'] ? "</div>\n" : ''; $html .= "</div>\n"; if ($options['required']) { $html .= "<input class='req' id='dummy-" . $options['input_id'] . "' type='hidden'>\n"; // for jscheck } $input_name = $options['multiple'] ? str_replace("[]", "", $input_name) : $input_name; $defender->add_field_session(array('input_name' => $input_name, 'title' => trim($title, '[]'), 'id' => $options['input_id'], 'type' => 'dropdown', 'regex' => $options['regex'], 'required' => $options['required'], 'safemode' => $options['safemode'], 'error_text' => $options['error_text'], 'callback_check' => $options['callback_check'], 'delimiter' => $options['delimiter'])); } return $html; }