Example #1
0
/**
 * Generates form elements. The main use for print_search() and print_form(), see examples of this functions.
 *
 * Options tree:
 * textarea -\
 *     (string)id, (string)name, (bool)readonly, (bool)disabled, (string)width, (string)class,
 *     (int)rows, (int)cols,
 *     (string)value, (bool,string)placeholder, (bool)ajax, (array)ajax_vars
 * text, input, password -\
 *     (string)id, (string)name, (bool)readonly, (bool)disabled, (string)width, (string)class,
 *     (string)value, (bool,string)placeholder, (bool)ajax, (array)ajax_vars,
 *     (bool)show_password
 * hidden -\
 *     (string)id, (string)value
 * select, multiselect -\
 *     (string)id, (string)name, (bool)readonly, (bool)disabled, (string)onchange, (string)width,
 *     (string)title, (int)size, (bool)right, (bool)live-search, (bool)encode, (bool)subtext
 *     (string)value, (array)values, (string)icon,
 *     values items can be arrays, ie:
 *         value => array('name' => string, 'group' => string, 'icon' => string, 'class' => string, 'style' => string)
 * datetime -\
 *     (string)id, (string)name, (bool)readonly, (bool)disabled,
 *     (string|FALSE)from, (string|FALSE)to, (bool)presets, (string)min, (string)max
 *     (string)value (use it for single input)
 * checkbox, switch -\
 *     (string)id, (string)name, (bool)readonly, (bool)disabled, (string)onchange,
 *     (bool)revert, (int)width, (string)size, (string)off-color, (string)on-color, (string)off-text, (string)on-text
 *     (string)value, (string)placeholder, (string)title
 * submit -\
 *     (string)id, (string)name, (bool)readonly, (bool)disabled,
 *     (string)class, (bool)right, (string)tooltip,
 *     (string)value, (string)form_id, (string)icon
 * html, raw -\
 *     (string)id,
 *     (string)html
 * newline -\
 *     (string)id,
 *     (bool)hr
 *
 * @param array $item Options for current form element
 * @param string $type Type of form element, also can passed as $item['type']
 * @return string Generated form element
 */
function generate_form_element($item, $type = '')
{
    $value_isset = isset($item['value']);
    if (!$value_isset) {
        $item['value'] = '';
    }
    if (!isset($item['type'])) {
        $item['type'] = $type;
    }
    $string = '';
    $element_tooltip = '';
    switch ($item['type']) {
        case 'hidden':
            if (!$item['readonly'] && !$item['disabled']) {
                $string .= '    <input type="' . $item['type'] . '" name="' . $item['id'] . '" id="' . $item['id'] . '" value="' . $item['value'] . '" />' . PHP_EOL;
            }
            break;
        case 'password':
        case 'textarea':
        case 'text':
        case 'input':
            if ($item['type'] != 'textarea') {
                $item_begin = '    <input type="' . $item['type'] . '" ';
                // password specific options
                if ($item['type'] == 'password') {
                    // disable autocomplete for passwords
                    $item_begin .= ' autocomplete="off" ';
                    // mask password field for disabled/readonly by bullet
                    if (strlen($item['value']) && ($item['disabled'] || $item['readonly'])) {
                        if (!($item['show_password'] && $_SESSION['userlevel'] > 7)) {
                            $item['value'] = '&#8226;&#8226;&#8226;&#8226;&#8226;&#8226;&#8226;&#8226;';
                        }
                    }
                    // add icon for show/hide password
                    if ($item['show_password']) {
                        $item_begin .= ' data-toggle="password" ';
                        register_html_resource('js', 'bootstrap-show-password.min.js');
                        $GLOBALS['cache_html']['javascript'][] = "\$('[data-toggle=\"password\"]').password();";
                    }
                }
                $item_end = ' value="' . $item['value'] . '" />';
                $item_class = 'input';
            } else {
                $item_begin = '    <textarea ';
                // textarea specific options
                if (is_numeric($item['rows'])) {
                    $item_begin .= 'rows="' . $item['rows'] . '" ';
                }
                if (is_numeric($item['cols'])) {
                    $item_begin .= 'cols="' . $item['cols'] . '" ';
                }
                $item_end = '>' . $item['value'] . '</textarea>';
                $item_class = 'form-control';
            }
            if ($item['disabled']) {
                $item_end = ' disabled="1"' . $item_end;
            } else {
                if ($item['readonly']) {
                    $item_end = ' readonly="1"' . $item_end;
                }
            }
            if (isset($item['placeholder']) && $item['placeholder'] !== FALSE) {
                if ($item['placeholder'] === TRUE) {
                    $item['placeholder'] = $item['name'];
                }
                $string .= PHP_EOL;
                $string .= $item_begin . 'placeholder="' . $item['placeholder'] . '" ';
                $item['placeholder'] = TRUE;
                // Set to true for check at end
            } else {
                $string .= '  <div class="input-prepend">' . PHP_EOL;
                if (!$item['name']) {
                    $item['name'] = '<i class="icon-list"></i>';
                }
                $string .= '    <span class="add-on">' . $item['name'] . '</span>' . PHP_EOL;
                $string .= $item_begin;
            }
            if ($item['class']) {
                $item_class .= ' ' . $item['class'];
            }
            $string .= isset($item['width']) ? 'style="width:' . $item['width'] . '" ' : '';
            $string .= 'name="' . $item['id'] . '" id="' . $item['id'] . '" class="' . $item_class;
            if ($item['ajax'] === TRUE && is_array($item['ajax_vars'])) {
                $ajax_vars = array();
                if (!isset($item['ajax_vars']['field'])) {
                    // If query field not specified use item id as field
                    $item['ajax_vars']['field'] = $item['id'];
                }
                foreach ($item['ajax_vars'] as $k => $v) {
                    $ajax_vars[] = urlencode($k) . '=' . var_encode($v);
                }
                $string .= ' ajax-typeahead" autocomplete="off" data-link="/ajax/input.php?' . implode('&amp;', $ajax_vars);
                // Register scripts/css
                register_html_resource('js', 'typeahead.bundle.min.js');
                register_html_resource('css', 'typeaheadjs.css');
                // Ajax autocomplete for input
                // <input type='text' class='ajax-typeahead' data-link='your-json-link' />
                $item_id = $item['id'];
                $script = <<<SCRIPT
  var element_{$item_id} = \$('#{$item_id}.ajax-typeahead');
  var entries_{$item_id} = new Bloodhound({
    datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    remote: {
      url: element_{$item_id}.data('link') + '&query=%QUERY',
      wildcard: '%QUERY',
      filter: function(json) {
        return json.options;
      }
    }
  });
  element_{$item_id}.typeahead({
      hint: false,
      highlight: true,
      minLength: 1
    },
    {
      name: 'options',
      limit: 16,
      source: entries_{$item_id}
    }
  );
SCRIPT;
                register_html_resource('script', $script);
            }
            $string .= '" ' . $item_end . PHP_EOL;
            $string .= $item['placeholder'] ? PHP_EOL : '  </div>' . PHP_EOL;
            // End 'text' & 'input'
            break;
        case 'switch':
            // switch specific options
            if ($item['revert']) {
                $item_switch = ' data-toggle="switch-revert"';
            } else {
                $item_switch = ' data-toggle="switch"';
            }
            if ($item['size']) {
                $item_switch .= ' data-size="' . $item['size'] . '"';
            }
            if ($item['on-color']) {
                $item_switch .= ' data-on-color="' . $item['on-color'] . '"';
            }
            if ($item['off-color']) {
                $item_switch .= ' data-off-color="' . $item['off-color'] . '"';
            }
            if ($item['on-text']) {
                $item_switch .= ' data-on-text="' . $item['on-text'] . '"';
            }
            if ($item['off-text']) {
                $item_switch .= ' data-off-text="' . $item['off-text'] . '"';
            }
            if (is_numeric($item['width']) && $item['width'] > 10) {
                $item_switch .= ' data-handle-width="' . intval($item['width'] / 2) . '"';
            }
        case 'checkbox':
            $string = '    <input type="checkbox" ';
            $string .= ' name="' . $item['id'] . '" id="' . $item['id'] . '" ' . $item_switch;
            if ($item['title']) {
                $string .= ' data-rel="tooltip" data-tooltip="' . escape_html($item['title']) . '"';
            }
            if ($item['value'] == '1' || $item['value'] === 'on' || $item['value'] === 'yes' || $item['value'] === TRUE) {
                $string .= ' checked';
            }
            if ($item['disabled']) {
                $string .= ' disabled="1"';
            } else {
                if ($item['readonly']) {
                    $string .= ' readonly="1" onclick="return false"';
                } else {
                    if ($item['onchange']) {
                        $string .= ' onchange="' . $item['onchange'] . '"';
                    }
                }
            }
            $string .= ' value="1" />';
            if (is_string($item['placeholder'])) {
                // add placeholder text at right of the element
                $string .= '      <span class="help-inline" style="margin-top: 4px;">' . $item['placeholder'] . '</span>' . PHP_EOL;
            }
            // End 'switch' & 'checkbox'
            break;
        case 'datetime':
            register_html_resource('js', 'bootstrap-datetimepicker.min.js');
            // Enable DateTime JS
            $id_from = $item['id'] . '_from';
            $id_to = $item['id'] . '_to';
            if ($value_isset && !$item['from'] && !$item['to']) {
                // Single datetime input
                $item['from'] = $item['value'];
                $item['to'] = FALSE;
                $item['presets'] = FALSE;
                $id_from = $item['id'];
                $name_from = $item['name'];
            } else {
                $name_from = 'From';
            }
            // Presets
            if ($item['from'] === FALSE || $item['to'] === FALSE) {
                $item['presets'] = FALSE;
            }
            if (is_numeric($item['from'])) {
                $item['from'] = strftime("%F %T", $item['from']);
            }
            if (is_numeric($item['to'])) {
                $item['to'] = strftime("%F %T", $item['to']);
            }
            if ($item['presets']) {
                $presets = array('sixhours' => 'Last 6 hours', 'today' => 'Today', 'yesterday' => 'Yesterday', 'tweek' => 'This week', 'lweek' => 'Last week', 'tmonth' => 'This month', 'lmonth' => 'Last month', 'tquarter' => 'This quarter', 'lquarter' => 'Last quarter', 'tyear' => 'This year', 'lyear' => 'Last year');
                // Recursive call
                $preset_item = array('id' => $item['id'] . '_preset', 'type' => 'select', 'name' => 'Date presets', 'width' => '110px', 'values' => $presets);
                $string .= generate_form_element($preset_item) . PHP_EOL;
            }
            // Date/Time input fields
            if ($item['from'] !== FALSE) {
                $string .= '  <div id="' . $id_from . '_div" class="input-prepend" style="margin-bottom: 0;">' . PHP_EOL;
                $string .= '    <span class="add-on btn"><i data-time-icon="icon-time" data-date-icon="icon-calendar"></i> ' . $name_from . '</span>' . PHP_EOL;
                //$string .= '    <input type="text" class="input-medium" data-format="yyyy-MM-dd hh:mm:ss" ';
                $string .= '    <input type="text" data-format="yyyy-MM-dd hh:mm:ss" ';
                $string .= isset($item['width']) ? 'style="width:' . escape_html($item['width']) . '" ' : 'style="width: 130px;" ';
                if ($item['disabled']) {
                    $string .= 'disabled="1" ';
                } else {
                    if ($item['readonly']) {
                        $item['disabled'] = TRUE;
                        // for js
                        $string .= 'readonly="1" ';
                    }
                }
                $string .= 'name="' . $id_from . '" id="' . $id_from . '" value="' . escape_html($item['from']) . '"/>' . PHP_EOL;
                $string .= '  </div>' . PHP_EOL;
            }
            if ($item['to'] !== FALSE) {
                $string .= '  <div id="' . $id_to . '_div" class="input-prepend" style="margin-bottom: 0;">' . PHP_EOL;
                $string .= '    <span class="add-on btn"><i data-time-icon="icon-time" data-date-icon="icon-calendar"></i> To</span>' . PHP_EOL;
                //$string .= '    <input type="text" class="input-medium" data-format="yyyy-MM-dd hh:mm:ss" ';
                $string .= '    <input type="text" data-format="yyyy-MM-dd hh:mm:ss" ';
                $string .= isset($item['width']) ? 'style="width:' . escape_html($item['width']) . '" ' : 'style="width: 140px;" ';
                $string .= 'name="' . $id_to . '" id="' . $id_to . '" value="' . escape_html($item['to']) . '"/>' . PHP_EOL;
                $string .= '  </div>' . PHP_EOL;
            }
            // JS SCRIPT
            $min = '-Infinity';
            $max = 'Infinity';
            $pattern = '/^(\\d{4})-(\\d{2})-(\\d{2}) ([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$/';
            if (!empty($item['min'])) {
                if (preg_match($pattern, $item['min'], $matches)) {
                    $matches[2] = $matches[2] - 1;
                    array_shift($matches);
                    $min = 'new Date(' . implode(',', $matches) . ')';
                } else {
                    if ($item['min'] == 'now' || $item['min'] == 'current') {
                        $min = 'new Date()';
                    }
                }
            }
            if (!empty($item['max'])) {
                if (preg_match($pattern, $item['max'], $matches)) {
                    $matches[2] = $matches[2] - 1;
                    array_shift($matches);
                    $max = 'new Date(' . implode(',', $matches) . ')';
                } else {
                    if ($item['max'] == 'now' || $item['max'] == 'current') {
                        $max = 'new Date()';
                    }
                }
            }
            $script = '
      var startDate = ' . $min . ';
      var endDate   = ' . $max . ';
      $(document).ready(function() {
        $(\'[id=' . $id_from . '_div]\').datetimepicker({
          //pickSeconds: false,
          weekStart: 1,
          startDate: startDate,
          endDate: endDate
        });';
            if ($item['disabled']) {
                $script .= '
        $(\'[id=' . $id_from . '_div]\').datetimepicker(\'disable\');';
            }
            if ($item['to'] !== FALSE) {
                $script .= '
        $(\'[id=' . $id_to . '_div]\').datetimepicker({
          //pickSeconds: false,
          weekStart: 1,
          startDate: startDate,
          endDate: endDate
        });';
            }
            $script .= '
      });' . PHP_EOL;
            if ($item['presets']) {
                $script .= '
      $(\'select[id=' . $item['id'] . '_preset]\').change(function() {
        var input_from = $(\'input#' . $id_from . '\');
        var input_to   = $(\'input#' . $id_to . '\');
        switch ($(this).val()) {' . PHP_EOL;
                foreach ($presets as $k => $v) {
                    $preset = datetime_preset($k);
                    $script .= "          case '{$k}':\n";
                    $script .= "            input_from.val('" . $preset['from'] . "');\n";
                    $script .= "            input_to.val('" . $preset['to'] . "');\n";
                    $script .= "            break;\n";
                }
                $script .= '
          default:
            input_from.val("");
            input_to.val("");
            break;
        }
      });';
            }
            register_html_resource('script', $script);
            // End 'datetime'
            break;
        case 'tags':
            // Tags mostly same as multiselect, but used separate options and Bootstrap Tags Input JS
            register_html_resource('js', 'bootstrap-tagsinput.min.js');
            // Enable Tags Input JS
            //register_html_resource('js',  'bootstrap-tagsinput.js');      // Enable Tags Input JS
            register_html_resource('css', 'bootstrap-tagsinput.css');
            // Enable Tags Input CSS
            // defaults
            $delimiter = empty($item['delimiter']) ? ',' : $item['delimiter'];
            $script_begin = '';
            $script_options = array('trimValue' => 'true', 'tagClass' => 'function(item) {return "label label-default";}');
            //register_html_resource('script', '$("input[data-role=tagsinput], select[multiple][data-role=tagsinput]").tagsinput({trimValue: true, tagClass: function(item) {return "label label-default";} });');
            $string .= '    <select multiple data-toggle="tagsinput" name="' . $item['id'] . '[]" ' . $title;
            $string .= 'id="' . $item['id'] . '" ';
            if ($item['title']) {
                $string .= 'title="' . $item['title'] . '" ';
            } else {
                if (isset($item['name'])) {
                    $string .= 'title="' . $item['name'] . '" ';
                }
            }
            if (isset($item['placeholder']) && $item['placeholder'] !== FALSE) {
                if ($item['placeholder'] === TRUE) {
                    $item['placeholder'] = $item['name'];
                }
                //$string .= PHP_EOL;
                $string .= ' placeholder="' . $item['placeholder'] . '"';
                //$item['placeholder'] = TRUE; // Set to true for check at end
            }
            if ($item['disabled']) {
                $string .= ' disabled="1"';
            } else {
                if ($item['readonly']) {
                    $string .= ' disabled="1" readonly="1"';
                    // Bootstrap Tags Input not support readonly attribute, currently use disable
                }
            }
            if ($item['onchange']) {
                $string .= ' onchange="' . $item['onchange'] . '"';
            }
            $string .= '>' . PHP_EOL . '      ';
            // end <select>
            // Process values
            if (!is_array($item['value'])) {
                //$item['value'] = explode($delimiter, $item['value']);
                $item['value'] = array($item['value']);
            }
            //$item['value'] = array('test', 'hello');
            $suggest = array();
            foreach ($item['value'] as $entry) {
                $value = (string) $entry;
                if ($value == '[there is no data]' || $value === '') {
                    continue;
                }
                $suggest[] = $value;
                $string .= '<option value="' . $value . '"';
                $string .= '>' . escape_html($value) . '</option> ';
            }
            $string .= PHP_EOL . '    </select>' . PHP_EOL;
            // Generate typeahead from values
            $suggest = array_merge($suggest, (array) $item['values']);
            if (count($suggest)) {
                $option = '[{ hint: false, highlight: true, minLength: 1 },
                    { name: "suggest", limit: 16, source: suggest_' . $item['id'] . ' }]';
                $script_begin .= 'var suggest_' . $item['id'] . ' = new Bloodhound({ matchAnyQueryToken: true, queryTokenizer: Bloodhound.tokenizers.nonword, datumTokenizer: Bloodhound.tokenizers.nonword,
        local: [';
                $values = array();
                foreach (array_unique($suggest) as $k => $entry) {
                    if (is_array($entry)) {
                        $value = (string) $k;
                    } else {
                        $value = (string) $entry;
                    }
                    $values[] = "'" . str_replace("'", "\\'", $value) . "'";
                }
                $script_begin .= implode(',', $values);
                $script_begin .= ']});' . PHP_EOL;
                $script_options['typeaheadjs'] = $option;
                // Register scripts/css
                //register_html_resource('js', 'typeahead.bundle.js');
                register_html_resource('js', 'typeahead.bundle.min.js');
                register_html_resource('css', 'typeaheadjs.css');
            }
            if (count($script_options)) {
                $script = $script_begin;
                $script .= "\$('#" . $item['id'] . "').tagsinput({" . PHP_EOL;
                foreach ($script_options as $key => &$option) {
                    $option = '  ' . $key . ': ' . $option;
                }
                $script .= implode(',' . PHP_EOL, $script_options) . PHP_EOL;
                $script .= "});";
                register_html_resource('script', $script);
            }
            // End 'tags'
            break;
        case 'multiselect':
            unset($item['icon']);
            // For now not used icons in multiselect
        // For now not used icons in multiselect
        case 'select':
            $count_values = count($item['values']);
            if (empty($item['values'])) {
                $item['values'] = array(0 => '[there is no data]');
                $item['subtext'] = FALSE;
            }
            if ($item['type'] == 'multiselect') {
                $string .= '    <select multiple name="' . $item['id'] . '[]" ' . $title;
                // Enable Select/Deselect all (if select values count more than 4)
                if ($count_values > 4) {
                    $string .= ' data-actions-box="true" ';
                }
            } else {
                $string .= '    <select name="' . $item['id'] . '" ';
            }
            $string .= 'id="' . $item['id'] . '" ';
            if ($item['title']) {
                $string .= 'title="' . $item['title'] . '" ';
            } else {
                if (isset($item['name'])) {
                    $string .= 'title="' . $item['name'] . '" ';
                }
            }
            $data_width = $item['width'] ? ' data-width="' . $item['width'] . '"' : ' data-width="auto"';
            $data_size = is_numeric($item['size']) ? ' data-size="' . $item['size'] . '"' : ' data-size="15"';
            $string .= 'class="selectpicker show-tick';
            if ($item['right']) {
                $string .= ' pull-right';
            }
            $string .= '" data-selected-text-format="count>2"';
            if ($item['data-style']) {
                $string .= ' data-style="' . $item['data-style'] . '"';
            }
            // Enable Live search in values list (if select values count more than 12)
            if ($count_values > 12 && $item['live-search'] !== FALSE) {
                $string .= ' data-live-search="true"';
            }
            if ($item['disabled']) {
                $string .= ' disabled="1"';
            } else {
                if ($item['readonly']) {
                    $string .= ' disabled="1" readonly="1"';
                    // Bootstrap select not support readonly attribute, currently use disable
                }
            }
            if ($item['onchange']) {
                $string .= ' onchange="' . $item['onchange'] . '"';
            }
            $string .= $data_width . $data_size . '>' . PHP_EOL . '      ';
            // end <select>
            if (!is_array($item['value'])) {
                $item['value'] = array($item['value']);
            }
            // Prepare values for optgroups
            $values = array();
            $optgroup = array();
            foreach ($item['values'] as $k => $entry) {
                $k = (string) $k;
                $value = $item['encode'] ? var_encode($k) : $k;
                // Use base64+serialize encoding
                // Default group is '' (empty string), for allow to use 0 as group name!
                $group = '';
                if (!is_array($entry)) {
                    $entry = array('name' => $entry);
                } else {
                    if (isset($entry['group'])) {
                        $group = $entry['group'];
                    }
                }
                if ($item['subtext'] && !isset($entry['subtext'])) {
                    $entry['subtext'] = $k;
                }
                // Icons and empty name fix
                if ($item['icon'] && $item['value'] === array('')) {
                    // Only one main icon
                    $entry['icon'] = $item['icon'];
                    // Set value icon as global icon
                    unset($item['icon']);
                }
                if (in_array($k, $item['value'])) {
                    if (!($k === '' && $entry['name'] === '')) {
                        if ($item['icon']) {
                            $entry['icon'] = $item['icon'];
                            // Set value icon as global icon
                        }
                        // Element selected
                        $entry['selected'] = TRUE;
                    }
                } else {
                    if ($entry['name'] == '[there is no data]') {
                        $entry['disabled'] = TRUE;
                    }
                }
                if (strlen($entry['name']) == 0 && $k !== '') {
                    $entry['name'] = $k;
                }
                // if name still empty set it as value
                $values[$group][$value] = $entry;
            }
            // Generate optgroups for values
            foreach ($values as $group => $entries) {
                $optgroup[$group] = '';
                foreach ($entries as $value => $entry) {
                    $optgroup[$group] .= '<option value="' . $value . '"';
                    if (isset($entry['subtext']) && strlen($entry['subtext'])) {
                        $optgroup[$group] .= ' data-subtext="' . $entry['subtext'] . '"';
                    }
                    if ($entry['name'] == '[there is no data]') {
                        $optgroup[$group] .= ' disabled="1"';
                    }
                    if (isset($entry['class']) && $entry['class']) {
                        $optgroup[$group] .= ' class="' . $entry['class'] . '"';
                    } else {
                        if (isset($entry['style']) && $entry['style']) {
                            $optgroup[$group] .= ' style="' . $entry['style'] . '"';
                        } else {
                            if (isset($entry['color']) && $entry['color']) {
                                $optgroup[$group] .= ' style="color:' . $entry['color'] . ' !important;"';
                                //$optgroup[$group] .= ' data-content="<span style=\'color: ' . $entry['color'] . '\'>' . $entry['name'] . '</span>"';
                            }
                        }
                    }
                    // Icons
                    if (isset($entry['icon']) && $entry['icon']) {
                        $optgroup[$group] .= ' data-icon="' . $entry['icon'] . '"';
                    }
                    // Disabled, Selected
                    if (isset($entry['disabled']) && $entry['disabled']) {
                        $optgroup[$group] .= ' disabled="1"';
                    } else {
                        if (isset($entry['selected']) && $entry['selected']) {
                            $optgroup[$group] .= ' selected';
                        }
                    }
                    $optgroup[$group] .= '>' . escape_html($entry['name']) . '</option> ';
                }
            }
            // If item groups passed, use order passed from it
            $optgroups = array_keys($optgroup);
            if (isset($item['groups'])) {
                $groups = array_intersect((array) $item['groups'], $optgroups);
                $optgroups = array_diff($optgroups, $groups);
                $optgroups = array_merge($groups, $optgroups);
            }
            if (count($optgroups) === 1) {
                // Single optgroup, do not use optgroup tags
                $string .= array_shift($optgroup);
            } else {
                // Multiple optgroups implode
                foreach ($optgroups as $group) {
                    $entry = $optgroup[$group];
                    $label = $group !== '' ? ' label="' . $group . '"' : '';
                    $string .= '<optgroup' . $label . '>' . PHP_EOL;
                    $string .= $entry;
                    $string .= '</optgroup>' . PHP_EOL;
                }
            }
            $string .= PHP_EOL . '    </select>' . PHP_EOL;
            // End 'select' & 'multiselect'
            break;
        case 'submit':
            $button_type = 'submit';
            $button_onclick = '';
            $button_class = 'btn';
            if (!empty($item['class'])) {
                if (!preg_match('/btn-(default|primary|success|info|warning|danger)/', $item['class'])) {
                    // Add default class if custom class hot have it
                    $button_class .= ' btn-default';
                }
                $button_class .= ' ' . $item['class'];
            } else {
                $button_class .= ' btn-default';
            }
            if ($item['right']) {
                $button_class .= ' pull-right';
            }
            if ($item['form_id'] && $item['id'] == 'search') {
                // Note, used script form_to_path() stored in js/observium.js
                $button_type = 'button';
                $button_onclick = " onclick=\"form_to_path('" . $item['form_id'] . "');\"";
            }
            $button_disabled = $item['disabled'] || $item['readonly'];
            if ($button_disabled) {
                $button_class .= ' disabled';
            }
            $string .= '      <button id="' . $item['id'] . '" name="' . $item['id'] . '" type="' . $button_type . '"';
            // Add tooltip data
            if ($item['tooltip']) {
                $button_class .= ' tooltip-from-element';
                $string .= ' data-tooltip-id="tooltip-' . $item['id'] . '"';
                $element_tooltip .= '<div id="tooltip-' . $item['id'] . '" style="display: none;">' . $item['tooltip'] . '</div>' . PHP_EOL;
            }
            //$string .= ' class="'.$button_class.' text-nowrap" style="line-height: 20px;"'.$button_onclick;
            $string .= ' class="' . $button_class . ' text-nowrap"' . $button_onclick;
            if ($button_disabled) {
                $string .= ' disabled="1"';
            }
            if ($item['value']) {
                $string .= ' value="' . $item['value'] . '"';
            }
            $string .= '>';
            switch ($item['id']) {
                // Note. 'update' - use POST request, all other - use GET with generate url from js.
                case 'update':
                    $button_icon = 'icon-refresh';
                    $button_name = 'Update';
                    break;
                default:
                    $button_icon = 'icon-search';
                    $button_name = 'Search';
            }
            $nbsp = 0;
            if (array_key_exists('icon', $item)) {
                $button_icon = trim($item['icon']);
            }
            if (strlen($button_icon)) {
                $string .= '<i class="' . $button_icon . '"></i>';
                $nbsp++;
            }
            if (array_key_exists('name', $item)) {
                $button_name = trim($item['name']);
            }
            if (strlen($button_name)) {
                $nbsp++;
            }
            if ($nbsp == 2) {
                $string .= '&nbsp;';
            }
            $string .= $button_name . '</button>' . PHP_EOL;
            // End 'submit'
            break;
        case 'raw':
        case 'html':
            // Just add custom (raw) html element
            if (isset($item['html'])) {
                $string .= $item['html'];
            } else {
                $string .= '<span';
                if (isset($item['class'])) {
                    $string .= ' class="' . $item['class'] . '"';
                }
                $string .= '>' . $item['value'] . '</span>';
            }
            break;
        case 'newline':
            // Deprecated
            $string .= '<div class="clearfix" id="' . $item['id'] . '">';
            $string .= $item['hr'] ? '<hr />' : '<hr style="border-width: 0px;" />';
            $string .= '</div>' . PHP_EOL;
            // End 'newline'
            break;
    }
    return $string . $element_tooltip;
}
Example #2
0
            echo generate_box_close();
        }
        //echo('  <br />' . PHP_EOL);
    }
    echo '  </div> </div> <!-- END SECTION ' . $section . ' -->' . PHP_EOL;
}
?>
<div class="row">
<div class="col-sm-12">

  <div class="box box-solid">
  <div class="box-content no-padding">
  <div class="form-actions" style="margin: 0px;">
  <?php 
$item = array('id' => 'submit', 'name' => 'Save Changes', 'class' => 'btn-primary', 'right' => TRUE, 'icon' => 'icon-ok oicon-white', 'value' => 'save');
echo generate_form_element($item, 'submit');
?>
  </div>
  </div>
  </div>

</div>
</div>

</form>

<script>
  function switchDesc(form_id) {
    var selected = $('#'+form_id+' option:selected').val();
    //console.log(selected);
    $('[id^="param_'+form_id+'_"]').each( function( index, element ) {
Example #3
0
function pagination(&$vars, $total, $return_vars = FALSE)
{
    $pagesizes = array(10, 20, 50, 100, 500, 1000, 10000, 50000);
    // Permitted pagesizes
    if (is_numeric($vars['pagesize'])) {
        $per_page = (int) $vars['pagesize'];
    } else {
        if (isset($_SESSION['pagesize'])) {
            $per_page = $_SESSION['pagesize'];
        } else {
            $per_page = $GLOBALS['config']['web_pagesize'];
        }
    }
    if (!$vars['short']) {
        // Permit fixed pagesizes only (except $vars['short'] == TRUE)
        foreach ($pagesizes as $pagesize) {
            if ($per_page <= $pagesize) {
                $per_page = $pagesize;
                break;
            }
        }
        if (isset($vars['pagesize']) && $vars['pagesize'] != $_SESSION['pagesize']) {
            if ($vars['pagesize'] != $GLOBALS['config']['web_pagesize']) {
                $_SESSION['pagesize'] = $per_page;
                // Store pagesize in session only if changed default
            } else {
                if (isset($_SESSION['pagesize'])) {
                    unset($_SESSION['pagesize']);
                    // Reset pagesize from session
                }
            }
        }
    }
    $vars['pagesize'] = $per_page;
    // Return back current pagesize
    $page = (int) $vars['pageno'];
    $lastpage = ceil($total / $per_page);
    if ($page < 1) {
        $page = 1;
    } else {
        if (!$return_vars && $lastpage < $page) {
            $page = (int) $lastpage;
        }
    }
    $vars['pageno'] = $page;
    // Return back current pageno
    if ($return_vars) {
        return '';
    }
    // Silent exit (needed for detect default pagesize/pageno)
    $start = ($page - 1) * $per_page;
    $prev = $page - 1;
    $next = $page + 1;
    $lpm1 = $lastpage - 1;
    $adjacents = 3;
    $pagination = '';
    if ($total > 99 || $total > $per_page) {
        if ($total > 9999) {
            $total_text = format_si($total);
        } else {
            $total_text = $total;
        }
        $pagination .= '<div class="row">' . PHP_EOL . '  <div class="col-lg-1 col-md-2 col-sm-2" style="display: inline-block;">' . PHP_EOL . '    <div class="box box-solid" style="padding: 4px 12px;">' . $total_text . '&nbsp;Items</div>' . PHP_EOL . '  </div>' . PHP_EOL . '  <div class="col-lg-10 col-md-8 col-sm-8">' . PHP_EOL . '    <div class="pagination pagination-centered"><ul>' . PHP_EOL;
        if ($prev) {
            //$pagination .= '      <li><a href="'.generate_url($vars, array('pageno' => 1)).'">First</a></li>' . PHP_EOL;
            $pagination .= '      <li><a href="' . generate_url($vars, array('pageno' => $prev)) . '">Prev</a></li>' . PHP_EOL;
        }
        if ($lastpage < 7 + $adjacents * 2) {
            for ($counter = 1; $counter <= $lastpage; $counter++) {
                if ($counter == $page) {
                    $pagination .= "<li class='active'><a>{$counter}</a></li>";
                } else {
                    $pagination .= "<li><a href='" . generate_url($vars, array('pageno' => $counter)) . "'>{$counter}</a></li>";
                }
            }
        } elseif ($lastpage > 5 + $adjacents * 2) {
            if ($page < 1 + $adjacents * 2) {
                for ($counter = 1; $counter < 4 + $adjacents * 2; $counter++) {
                    if ($counter == $page) {
                        $pagination .= "<li class='active'><a>{$counter}</a></li>";
                    } else {
                        $class = '';
                        //if ($counter > 9)
                        //{
                        //  $class = ' class="hidden-md hidden-sm hidden-xs"';
                        //}
                        //else if ($counter > 6)
                        //{
                        //  $class = ' class="hidden-sm hidden-xs"';
                        //}
                        $pagination .= "<li{$class}><a href='" . generate_url($vars, array('pageno' => $counter)) . "'>{$counter}</a></li>";
                    }
                }
                $pagination .= "<li><a href='" . generate_url($vars, array('pageno' => $lpm1)) . "'>{$lpm1}</a></li>";
                $pagination .= "<li><a href='" . generate_url($vars, array('pageno' => $lastpage)) . "'>{$lastpage}</a></li>";
            } elseif ($lastpage - $adjacents * 2 > $page && $page > $adjacents * 2) {
                $pagination .= "<li><a href='" . generate_url($vars, array('pageno' => '1')) . "'>1</a></li>";
                $pagination .= "<li><a href='" . generate_url($vars, array('pageno' => '2')) . "'>2</a></li>";
                for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++) {
                    if ($counter == $page) {
                        $pagination .= "<li class='active'><a>{$counter}</a></li>";
                    } else {
                        $pagination .= "<li><a href='" . generate_url($vars, array('pageno' => $counter)) . "'>{$counter}</a></li>";
                    }
                }
                $pagination .= "<li><a href='" . generate_url($vars, array('pageno' => $lpm1)) . "'>{$lpm1}</a></li>";
                $pagination .= "<li><a href='" . generate_url($vars, array('pageno' => $lastpage)) . "'>{$lastpage}</a></li>";
            } else {
                $pagination .= "<li><a href='" . generate_url($vars, array('pageno' => '1')) . "'>1</a></li>";
                $pagination .= "<li><a href='" . generate_url($vars, array('pageno' => '2')) . "'>2</a></li>";
                for ($counter = $lastpage - (2 + $adjacents * 2); $counter <= $lastpage; $counter++) {
                    if ($counter == $page) {
                        $pagination .= "<li class='active'><a>{$counter}</a></li>";
                    } else {
                        $class = '';
                        //if ($lastpage - $counter > 9)
                        //{
                        //  $class = ' class="hidden-md hidden-sm hidden-xs"';
                        //}
                        //else if ($lastpage - $counter > 6)
                        //{
                        //  $class = ' class="hidden-sm hidden-xs"';
                        //}
                        $pagination .= "<li{$class}><a href='" . generate_url($vars, array('pageno' => $counter)) . "'>{$counter}</a></li>";
                    }
                }
            }
        }
        if ($page < $counter - 1) {
            $pagination .= "<li><a href='" . generate_url($vars, array('pageno' => $next)) . "'>Next</a></li>";
            # No need for "Last" as we don't have "First", 1, 2 and the 2 last pages are always in the list.
            #$pagination.= "<li><a href='".generate_url($vars, array('pageno' => $lastpage))."'>Last</a></li>";
        } else {
            if ($lastpage > 1) {
                $pagination .= "<li class='active'><a>Next</a></li>";
                #$pagination.= "<li class='active'><a>Last</a></li>";
            }
        }
        $pagination .= "</ul></div></div>";
        //$values = array('' => array('name'))
        foreach ($pagesizes as $pagesize) {
            $value = generate_url($vars, array('pagesize' => $pagesize, 'pageno' => floor($start / $pagesize)));
            $name = $pagesize == $GLOBALS['config']['web_pagesize'] ? "[ {$pagesize} ]" : $pagesize;
            $values[$value] = array('name' => $name, 'class' => 'text-center');
        }
        $element = array('type' => 'select', 'id' => 'pagesize', 'name' => '# ' . $per_page, 'width' => '90px', 'onchange' => "window.open(this.options[this.selectedIndex].value,'_top')", 'value' => $per_page, 'data-style' => 'box', 'values' => $values);
        $pagination .= '
       <div class="col-lg-1 col-md-2 col-sm-2">
       <form class="pull-right" action="#">';
        $pagination .= generate_form_element($element);
        $pagination .= '</form></div></div>';
    }
    return $pagination;
}
Example #4
0
    <div class="control-group">
      <label>Description</label>
      <div class="controls">
<?php 
            $item = array('id' => 'new_descr', 'type' => 'text', 'name' => 'Description', 'width' => '95%', 'placeholder' => TRUE, 'value' => escape_html($user_data['descr']));
            echo generate_form_element($item);
            ?>
      </div>
    </div>

    <div class="control-group">
      <div class="controls">
<?php 
            $item = array('id' => 'new_can_modify_passwd', 'type' => 'checkbox', 'placeholder' => 'Allow the user to change his password', 'value' => $user_data['can_modify_passwd']);
            echo generate_form_element($item);
            ?>
      </div>
    </div>
  </fieldset>

  </div>
  <div class="modal-footer">
    <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
    <button type="submit" class="btn btn-primary" name="submit" value="change_user"><i class="icon-ok icon-white"></i> Save Changes</button>
  </div>
 </form>
</div>
<?php 
        }
        // end edit user modal