/**
 * Render button
 * 
 * Parameters:
 * 
 * - common anchor parameter
 * 
 * - method - if POST that this button will be send POST request. Method works 
 *   only if href parameter is present
 * - confirm - enforces confirmation dialog
 *   codes
 *
 * @param array $params
 * @param string $content
 * @param Smarty $smarty
 * @param boolean $repeat
 * @return string
 */
function smarty_block_link($params, $content, &$smarty, &$repeat)
{
    $href = '';
    if (isset($params['href'])) {
        $href = $params['href'];
        if (str_starts_with($href, '?')) {
            $href = assemble_from_string($params['href']);
        }
        // if
    }
    // if
    $params['href'] = $href;
    $confirm = '';
    if (array_key_exists('confirm', $params)) {
        $confirm = lang(trim($params['confirm']));
        unset($params['confirm']);
    }
    // if
    $post = false;
    if (array_key_exists('method', $params)) {
        if (strtolower($params['method']) == 'post') {
            $post = true;
        }
        // if
        unset($params['method']);
    }
    // if
    if ($post || $confirm) {
        $execution = $post ? 'App.postLink(' . var_export($href, true) . ')' : 'location.href = ' . var_export($href, true);
        if ($confirm) {
            $params['onclick'] = "if(confirm(" . var_export($confirm, true) . ")) { {$execution}; } return false;";
        } else {
            $params['onclick'] = "{$execution}; return false;";
        }
        // if
    }
    // if
    $not_lang = false;
    if (isset($params['not_lang'])) {
        $not_lang = (bool) $params['not_lang'];
        unset($params['not_lang']);
    }
    // if
    if (array_key_exists('id', $params) && strlen($params['id']) == 0) {
        unset($params['id']);
    }
    // if
    if (array_key_exists('title', $params)) {
        $params['title'] = lang($params['title']);
    }
    // if
    $text = $not_lang ? $content : lang($content);
    return open_html_tag('a', $params) . $text . '</a>';
}
/**
 * Render button
 * 
 * Parameters:
 * 
 * - common button parameter
 * - href - when button is clicked this link is opened
 * - method - if POST that this button will be send POST request. Method works 
 *   only if href parameter is present
 * - confirm - enforces confirmation dialog
 * - not_lang - if true content will not be matched agains registered language 
 *   codes
 *
 * @param array $params
 * @param string $content
 * @param Smarty $smarty
 * @param boolean $repeat
 * @return string
 */
function smarty_block_button($params, $content, &$smarty, &$repeat)
{
    if (!isset($params['type'])) {
        $params['type'] = 'button';
    }
    // if
    $href = '';
    if (isset($params['href'])) {
        $href = $params['href'];
        if (str_starts_with($href, '?')) {
            $href = assemble_from_string($params['href']);
        }
        // if
        unset($params['href']);
    }
    // if
    $confirm = '';
    if (isset($params['confirm'])) {
        $confirm = trim($params['confirm']);
        unset($params['confirm']);
    }
    // if
    $post = false;
    if (isset($params['method'])) {
        $post = strtolower($params['method']) == 'post';
        unset($params['method']);
    }
    // if
    if ($href) {
        $execution = $post ? 'App.postLink(' . var_export($href, true) . ')' : 'location.href = ' . var_export($href, true);
        if ($confirm) {
            $params['onclick'] = "if(confirm(" . var_export($confirm, true) . ")) { {$execution}; } return false;";
        } else {
            $params['onclick'] = "{$execution}; return false;";
        }
        // if
    } else {
        if ($confirm) {
            $params['onclick'] = "return confirm(" . var_export($confirm, true) . ")";
        }
        // if
    }
    // if
    $not_lang = false;
    if (isset($params['not_lang'])) {
        $not_lang = (bool) $params['not_lang'];
        unset($params['not_lang']);
    }
    // if
    $text = $not_lang ? $content : lang($content);
    return open_html_tag('button', $params) . '<span><span>' . clean($text) . '</span></span></button>';
}
/**
 * Render form block
 * 
 * Parameters:
 * 
 * - All FORM element attributes
 * - block_labels - Use block or inline labels
 * - autofocus - Automatically focus first field, true by default
 * - ask_on_leave - Ask users confirmation when form data is changed and user 
 *   tries to navigate of the page. This setting is off by default
 * - disable_submit - If true Submit button will be disabled until all values 
 *   in the form are valid. Off by default...
 * - show_errors - Display errors
 *
 * @param array $params
 * @param string $content
 * @param Smarty $smarty
 * @param boolean $repeat
 * @return string
 */
function smarty_block_form($params, $content, &$smarty, &$repeat)
{
    static $counter = 0;
    $action = array_var($params, 'action');
    if (str_starts_with($action, '?')) {
        $params['action'] = assemble_from_string($action);
    }
    // if
    $id = array_var($params, 'id');
    if (empty($id)) {
        $counter++;
        $id = 'system_form_' . $counter;
    }
    // if
    $params['id'] = $id;
    $is_uni = true;
    if (isset($params['uni'])) {
        $is_uni = (bool) $params['uni'];
        unset($params['uni']);
    }
    // if
    $autofocus = false;
    if (isset($params['autofocus'])) {
        $autofocus = (bool) $params['autofocus'];
        unset($params['autofocus']);
    }
    // if
    $ask_on_leave = false;
    if (isset($params['ask_on_leave'])) {
        $ask_on_leave = (bool) $params['ask_on_leave'];
        unset($params['ask_on_leave']);
    }
    // if
    $disable_submit = false;
    if (isset($params['disable_submit'])) {
        $disable_submit = (bool) $params['disable_submit'];
        unset($params['disable_submit']);
    }
    // if
    $show_errors = true;
    if (isset($params['show_errors'])) {
        $show_errors = (bool) $params['show_errors'];
        unset($params['show_errors']);
    }
    // if
    $classes = array();
    if (isset($params['class'])) {
        $classes = explode(' ', $params['class']);
    }
    // if
    if ($is_uni && !in_array('uniForm', $classes)) {
        $classes[] = 'uniForm';
    }
    // if
    if ($autofocus && !in_array('focusFirstField', $classes)) {
        $classes[] = 'focusFirstField';
    }
    // if
    if ($ask_on_leave && !in_array('askOnLeave', $classes)) {
        $classes[] = 'askOnLeave';
    }
    // if
    if ($disable_submit && !in_array('disableSubmit', $classes)) {
        $classes[] = 'disableSubmit';
    }
    // if
    if ($show_errors && !in_array('showErrors', $classes)) {
        $classes[] = 'showErrors';
    }
    // if
    $params['class'] = implode(' ', $classes);
    $block_labels = (bool) array_var($params, 'block_labels', true);
    $class_for_labels = $block_labels ? 'blockLabels' : 'inlineLabels';
    $errors_code = "";
    $errors = array_var($params, 'errors');
    if (empty($errors)) {
        $errors = $smarty->get_template_vars('errors');
        if (instance_of($errors, 'ValidationErrors') && is_foreachable($errors->getFieldErrors(ANY_FIELD))) {
            $errors_code = '<div id="errorMsg"><h3>' . lang('Oops! We found some errors that need to be corrected before we can proceed') . '</h3><ol>';
            foreach ($errors->getFieldErrors(ANY_FIELD) as $error_message) {
                $errors_code .= '<li>' . clean($error_message) . '</li>';
            }
            // foreach
            $errors_code .= '</ol></div>';
        }
        // if
    }
    // if
    $return = open_html_tag('form', $params) . "\n<div class=\"{$class_for_labels}\">\n{$errors_code}\n{$content}\n<input type=\"hidden\" name=\"submitted\" value=\"submitted\" style=\"display: none\" />\n</div>\n</form>\n";
    if ($is_uni) {
        $return .= "<script type=\"text/javascript\">\$('#{$id}').uniform();</script>\n";
    }
    // if
    return $return;
}