<?php
/**
 * Smarty plugin.
 */

/**
 * Smarty {print_select} plugin.
 *
 * Type:     function<br>
 * Name:     url<br>
 * Purpose:  print select
 *
 * @param array                    $params parameters
 * @param Smarty_Internal_Template $smarty
 *
 * @return string
 */
function smarty_function_print_select($params, &$smarty)
{
    $name = '';
    $var = [];
    $selected = '';
    $param = '';
    $class = 'selecter';
    $keep_value = false;

    extract($params);

    if (!is_array($selected)) {
        $selected = [$selected => true];
    }

    if ($keep_value) {
        foreach ($selected as $key => $value) {
            if (!array_key_exists($key, $var)) {
                $var[$key] = $key;
            }
        }
    } ?>
    <select class="<?php echo $class; ?>" data-filter-type="<?php echo (strpos($param, 'multiple') !== false) ? 'multiselect' : 'select'; ?>" name="<?php echo $name; ?>" <?php echo $param; ?>>
       <?php
       foreach ($var as $index => $value) {
           ?>
          <option value="<?php echo $index; ?>"
              <?php echo (isset($selected[$index]) && ($selected[$index] || $selected[$index] == '0')) ? 'selected' : ''; ?>>
              <?php echo $value; ?>
          </option>
           <?php
       } ?>
    </select>
    <?php
}
