function smarty_function_dataform2($params, &$smarty)
{
    require_once $smarty->_get_plugin_filepath('shared', 'escape_special_chars');
    require_once $smarty->_get_plugin_filepath('shared', 'video_service');
    require_once $smarty->_get_plugin_filepath('function', 'html_options');
    require_once $smarty->_get_plugin_filepath('function', 'html_select_date');
    require_once $smarty->_get_plugin_filepath('function', 'html_select_time');
    require_once $smarty->_get_plugin_filepath('modifier', 'truncate');
    require_once $smarty->_get_plugin_filepath('modifier', 'nl2br');
    require_once $smarty->_get_plugin_filepath('modifier', 'wordwrap');
    require_once $smarty->_get_plugin_filepath('modifier', 'return_bytes');
    require_once $smarty->_get_plugin_filepath('modifier', 'fsize_format');
    $row = array();
    $dd = array();
    $options = array();
    $key = null;
    $key1 = null;
    $key2 = null;
    $type = 0;
    $name = 'dfform';
    $object = null;
    $edit = false;
    $table = null;
    $preset = null;
    $is_child = false;
    $extra = '';
    foreach ($params as $_key => $_val) {
        switch ($_key) {
            case 'name':
            case 'object':
            case 'title':
            case 'key':
            case 'key1':
            case 'key2':
            case 'preset':
                ${$_key} = (string) $_val;
                break;
            case 'options':
            case 'row':
            case 'dd':
                ${$_key} = (array) $_val;
                break;
            case 'paginate':
            case 'is_child':
            case 'edit':
                ${$_key} = (bool) $_val;
                break;
            default:
                if (!is_array($_val)) {
                    $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
                } else {
                    $smarty->trigger_error("dataform: extra attribute '{$_key}' cannot be an array", E_USER_NOTICE);
                }
                break;
        }
    }
    if (!$object) {
        $object = $name;
    }
    if (!$table) {
        $table = $object;
    }
    if (empty($row) && empty($dd)) {
        $smarty->trigger_error("dataform: either dd or row attribute must be present", E_USER_NOTICE);
        return '';
    }
    if (empty($row)) {
        $type = 2;
        /* new record */
    }
    if ($edit) {
        $type = 1;
    }
    if (empty($dd)) {
        $labels = null;
    } else {
        foreach ($dd as $_key => $_val) {
            $labels[$_key] = $_val['label'];
        }
    }
    if ($preset) {
        list($_field, $_text) = preg_split('/,/', $preset);
        list($_fieldname, $_fieldvalue) = preg_split('/=/', $_field);
        $_preset[$_fieldname] = $_fieldvalue;
        $_preset_text[$_fieldname] = $_text;
    }
    $_html_rows = '';
    $_html_result = '';
    $h_f = $key2 ? F2 : F;
    # Determinar el tamaño máximo de los archivo
    $upload = smarty_modifier_return_bytes(ini_get('upload_max_filesize'));
    $post = smarty_modifier_return_bytes(ini_get('post_max_size'));
    $max_size = 0;
    if ($upload < $post) {
        $max_size = $upload;
    } else {
        $max_size = $post;
    }
    $max_size = smarty_modifier_fsize_format($max_size);
    # Crea las filas del dataform
    $_i = 0;
    // Para los preset
    $_pre = '';
    if ($type) {
        foreach ((array) $dd as $_key => $_val) {
            if (!$dd[$_key]) {
                continue;
            }
            if ($type == 2) {
                $_val = '';
            }
            if ($type == 1) {
                $_val = $row[$_val['name']];
            }
            if ($dd[$_key]['references'] && $dd[$_key]['type'] != 'auto' && $dd[$_key]['type'] != 'hidden') {
                if ($_preset[$_key]) {
                    $_selected = $_preset[$_key];
                } else {
                    $_selected = $_val;
                    $_val = $row[$dd[$_key]['references']];
                }
                $dd[$_key]['type'] = 'references';
            }
            $_start_year = "-10";
            $_end_year = "+10";
            $_ispreset = false;
            switch ($dd[$_key]['type']) {
                //Add
                case 'video':
                    $_tmp = '';
                    $_options = smarty_function_video_service();
                    if ($_val) {
                        require_once $smarty->_get_plugin_filepath('shared', 'video_service');
                        $a_vs = smarty_function_video_service();
                        //      XML
                        require_once 'XML/Unserializer.php';
                        $options = array(XML_UNSERIALIZER_OPTION_ATTRIBUTES_PARSE => true, XML_UNSERIALIZER_OPTION_ATTRIBUTES_ARRAYKEY => false);
                        $unserializer = new XML_Unserializer($options);
                        $status = $unserializer->unserialize($_val, false);
                        if (PEAR::isError($status)) {
                            echo 'Error: ' . $status->getMessage();
                        } else {
                            $vs = $unserializer->getUnserializedData();
                        }
                        //  -----
                        $_tmp .= smarty_function_html_options(array('name' => $_key . '_type', 'options' => $_options, 'selected' => $vs['tipo']), $smarty) . '<input type="text" size="25" name="' . $_key . '_src" maxlength="500" value="' . $vs['src'] . '" />';
                    } else {
                        $_tmp .= smarty_function_html_options(array('name' => $_key . '_type', 'options' => $_options, 'selected' => $_val), $smarty) . '<input type="text" size="25" name="' . $_key . '_src" maxlength="500" />';
                    }
                    break;
                    //End Add
                //End Add
                case 'auto':
                    if ($dd[$_key]['extra']['default']) {
                        $_tmp = '<span class="auto">' . $dd[$_key]['extra']['default'] . '</span>';
                    } else {
                        $_tmp = !empty($_val) ? $_val : '';
                    }
                    break;
                case 'file':
                    $_tmp = '';
                    if ($_val) {
                        $_file = HOMEDIR . "/_" . $table . "/" . $_val;
                        $_icon = 'doc.png';
                        $_p = explode('.', $_file);
                        $_pc = count($_p);
                        $ext = $_p[$_pc - 1];
                        if (preg_match('/doc|rtf|swx|osd/i', $ext)) {
                            $_icon = 'doc.png';
                        }
                        if (preg_match('/pdf/i', $ext)) {
                            $_icon = 'pdf.png';
                        }
                        if (preg_match('/xls/i', $ext)) {
                            $_icon = 'excel.png';
                        }
                        if (preg_match('/jpg|gif|png/i', $ext)) {
                            $_icon = 'image.png';
                        }
                        $_tmp = '<input type="checkbox" checked name="' . $_key . '_keep" /> Conservar archivo actual (' . $_val . ')<br /><img src="' . URL . '/' . ALM_URI . '/themes/' . ALM_ADMIN_THEME . '/img/' . $_icon . '" alt="' . $_val . '" border="0" /><br />';
                    }
                    $_tmp .= '[' . $max_size . ']<input type="file" name="' . $_key . '" value="' . $_val . '" />';
                    break;
                case 'image':
                case 'img':
                    $_tmp = '';
                    $_icon = 'image.png';
                    $_tmp = '[' . $max_size . ']<input name="old_' . $_key . '" type="hidden" value="' . $_val . '" /><img src="' . URL . '/' . ALM_URI . '/themes/' . ALM_ADMIN_THEME . '/img/' . $_icon . '" border="0" alt="Imagen" title="Imagen" />';
                    if ($_val) {
                        $_tmp .= '<input type="checkbox" checked name="' . $_key . '_keep" /> Conservar archivo actual (' . $_val . ')<br /><img src="' . URL . '/' . ALM_URI . '/pic/50/' . $table . '/' . $_val . '" alt="' . $_val . '" width="50" border="0" /><br />';
                    }
                    $_tmp .= '<input type="file" name="' . $_key . '" value="' . $_val . '" />';
                    break;
                case 'boolean':
                case 'bool':
                    if (preg_match("/:/", $dd[$_key]['extra']['label_bool'])) {
                        list($_si, $_no) = preg_split('/:/', $dd[$_key]['extra']['label_bool']);
                        $_tchecked = $_val == 't' ? 'checked' : '';
                        $_fchecked = $_val == 'f' ? 'checked' : '';
                        $_tmp = $_si . '<input type="radio" name="' . $_key . '" ' . $_tchecked . ' value="on" />' . $_no . '<input type="radio" name="' . $_key . '" ' . $_fchecked . ' value="" />';
                    } else {
                        $_checked = $_val == 't' ? 'checked' : '';
                        $_tmp = '<input type="checkbox" name="' . $_key . '" ' . $_checked . ' />';
                    }
                    break;
                case 'datenull':
                    if (preg_match("/:/", $dd[$_key]['extra']['range'])) {
                        list($_start_year, $_end_year) = preg_split('/:/', $dd[$_key]['extra']['range']);
                    }
                    if (!isset($_val) || empty($_val)) {
                        $_val = '--';
                    }
                    $_tmp = smarty_function_html_select_date(array('prefix' => $_key . '_', 'time' => $_val, 'start_year' => $_start_year, 'end_year' => $_end_year, 'day_empty' => '--', 'month_empty' => '--', 'year_empty' => '--'), $smarty);
                    break;
                case 'date':
                    if (!$_val) {
                        $_val = $dd[$_key]['extra']['default'];
                    }
                    if (preg_match("/:/", $dd[$_key]['extra']['range'])) {
                        list($_start_year, $_end_year) = preg_split('/:/', $dd[$_key]['extra']['range']);
                    }
                    $_tmp = smarty_function_html_select_date(array('prefix' => $_key . '_', 'time' => $_val, 'start_year' => $_start_year, 'end_year' => $_end_year), $smarty);
                    break;
                case 'time':
                    $_tmp = smarty_function_html_select_time(array('prefix' => $_key . '_', 'time' => $_val, 'display_seconds' => false), $smarty);
                    break;
                case 'datetime':
                    if (!$_val) {
                        $_val = $dd[$_key]['extra']['default'];
                    }
                    if (preg_match("/:/", $dd[$_key]['extra']['range'])) {
                        list($_start_year, $_end_year) = preg_split('/:/', $dd[$_key]['extra']['range']);
                    }
                    $_tmp = smarty_function_html_select_date(array('prefix' => $_key . '_', 'time' => $_val, 'start_year' => $_start_year, 'end_year' => $_end_year), $smarty);
                    $_tmp .= smarty_function_html_select_time(array('prefix' => $_key . '_', 'time' => $_val, 'display_seconds' => false), $smarty);
                    break;
                case 'datetimenull':
                    if (!$_val) {
                        $_val = $dd[$_key]['extra']['default'];
                    }
                    if (preg_match("/:/", $dd[$_key]['extra']['range'])) {
                        list($_start_year, $_end_year) = preg_split('/:/', $dd[$_key]['extra']['range']);
                    }
                    $_tmp = smarty_function_html_select_date(array('prefix' => $_key . '_', 'time' => $_val, 'start_year' => $_start_year, 'end_year' => $_end_year, 'day_empty' => '--', 'month_empty' => '--', 'year_empty' => '--'), $smarty);
                    $_tmp .= smarty_function_html_select_time(array('prefix' => $_key . '_', 'time' => $_val, 'display_seconds' => false), $smarty);
                    break;
                case 'password':
                    $_tmp = '<input type="password" name="' . $_key . '" size="20" maxlength="16" />';
                    break;
                case 'text':
                    $_tmp = '<textarea rows="5" cols="40" name="' . $_key . '">' . $_val . '</textarea> <a href="javascript:edittext(\'' . $name . '\', \'' . $_key . '\', document.forms[\'' . $name . '\'].' . $_key . '.value);">' . ALM_MAX . '</a>';
                    break;
                case 'html':
                    $_tmp = '<textarea rows="5" cols="40" class="adm" id="' . $_key . '" name="' . $_key . '">' . $_val . '</textarea>';
                    $_tmp .= "<script language=\"JavaScript\">\ngenerate_wysiwyg('" . $_key . "');\n</script>\n";
                    break;
                case 'xhtml':
                    $_tmp = '<textarea rows="5" cols="40" class="adm" id="' . $_key . '" name="' . $_key . '"' . ($dd[$_key]['extra']['style'] ? ' style="' . $dd[$_key]['extra']['style'] . '"' : '') . '>' . $_val . '</textarea>';
                    $_tmp .= "<script language=\"JavaScript\">\ntinyMCE.execCommand(\"mceAddControl\", true,\"{$_key}\");\n</script>\n";
                    break;
                case 'varchar':
                case 'char':
                    if ($dd[$_key]['extra']['list_values']) {
                        $_options = $dd[$_key]['extra']['list_values'];
                        $_tmp = smarty_function_html_options(array('options' => $_options, 'selected' => $_val), $smarty);
                        $_tmp = preg_replace("/_REFERENCE_/", $_tmp, FCELLMODREF);
                        $_tmp = preg_replace("/_FIELD_/", $_key, $_tmp);
                    } else {
                        $_val = preg_replace("/\"/", "&quot;", $_val);
                        $_tmp = preg_replace("/_VALUE_/", qdollar($_val), FCELLMODSTR);
                        $_tmp = preg_replace("/_FIELD_/", $_key, $_tmp);
                        $_tmp = preg_replace("/_SIZE_/", $dd[$_key]['size'], $_tmp);
                        if ($dd[$_key]['extra']['source']) {
                            $_tmp .= '<script type="text/javascript">
                          addEvent(document.getElementById(\'' . $dd[$_key]['extra']['source'] . '\'), \'blur\', function () { if(document.getElementById(\'' . $_key . '\').value.replace(/(^\\s*)|(\\s*$)/g, \'\') == \'\')  document.getElementById(\'' . $_key . '\').value = ' . $dd[$_key]['extra']['apply_funct_js'] . '(document.getElementById(\'' . $dd[$_key]['extra']['source'] . '\').value) });
                        </script>';
                        }
                        if ($dd[$_key]['extra']['autocomplete']) {
                            $_tmp .= '<script type="text/javascript">
                          $(\'#' . $_key . '\').autocomplete({ serviceUrl:' . ($dd[$_key]['extra']['autocomplete_sr'] ? $dd[$_key]['extra']['autocomplete_sr'] : '\'' . URL . '/' . ALM_URI . '/js/autocomplete/autocomplete.php\'') . ', delimiter: /(,|;)\\s*/, params: { table:\'' . ($dd[$_key]['extra']['autocomplete_tb'] ? $dd[$_key]['extra']['autocomplete_tb'] : $table) . '\', field:\'' . ($dd[$_key]['extra']['autocomplete_fd'] ? $dd[$_key]['extra']['autocomplete_fd'] : ($dd[$_key]['extra']['autocomplete_tb'] ? $dd[$_key]['extra']['autocomplete_tb'] : $_key)) . '\' } });
                        </script>';
                        }
                    }
                    break;
                case 'numeric':
                case 'int':
                    if ($dd[$_key]['extra']['list_values']) {
                        $_options = $dd[$_key]['extra']['list_values'];
                        $_tmp = smarty_function_html_options(array('options' => $_options, 'selected' => $_val), $smarty);
                        $_tmp = preg_replace("/_REFERENCE_/", $_tmp, FCELLMODREF);
                        $_tmp = preg_replace("/_FIELD_/", $_key, $_tmp);
                    } else {
                        $_tmp = preg_replace("/_VALUE_/", $_val, FCELLMODSTR);
                        $_tmp = preg_replace("/_FIELD_/", $_key, $_tmp);
                        $_tmp = preg_replace("/_SIZE_/", 10, $_tmp);
                    }
                    break;
                case 'references':
                    if ($_preset[$_key]) {
                        $_tmp = '';
                        $_ispreset = true;
                        $_pre .= '<input type="hidden" name="' . $_key . '" value="' . $_selected . '" />';
                        //$_tmp = '<input type="hidden" name="' . $_key . '" value="' . $_selected . '" />';
                        //$_tmp .= $_selected;
                    } else {
                        if ($dd[$_key]['extra']['readonly']) {
                            $_tmp = preg_replace("/_FIELD_/", $_key, FCELLHIDEN);
                            $_tmp = preg_replace("/_VALUE_/", qdollar($_selected), $_tmp);
                            $_tmp = preg_replace("/_LABEL_/", $_val, $_tmp);
                        } else {
                            $_options = $options[$_key];
                            $_tmp = smarty_function_html_options(array('options' => $_options, 'selected' => $_selected), $smarty);
                            $_tmp = preg_replace("/_REFERENCE_/", $_tmp, FCELLMODREF);
                            $_tmp = preg_replace("/_FIELD_/", $_key, $_tmp);
                            if ($dd[$_key]['extra']['son']) {
                                $_tmp = preg_replace("/_CHANGE_/", "updateCombo('{$_key}', '" . $dd[$_key]['extra']['son'] . "', null)", $_tmp);
                            } else {
                                $_tmp = preg_replace("/_CHANGE_/", "", $_tmp);
                            }
                            if ($dd[$_key]['extra']['open_popup']) {
                                $_tmp .= '&nbsp;<a href="javascript:openPopUp(\'' . URL . '/' . ALM_URI . '/query.php?f=' . $dd[$_key]['references'] . '&action=record&' . $_key . '=\'+document.forms[\'new\'].' . $_key . '.value,' . $dd[$_key]['extra']['height'] . ',' . $dd[$_key]['extra']['width'] . ');">consultar</a>';
                            }
                            if ($dd[$_key]['extra']['depend']) {
                                $_tmp .= "<script>updateCombo('" . $dd[$_key]['extra']['depend'] . "', '{$_key}', '{$_selected}')</script>";
                            }
                        }
                    }
                    break;
                case 'hidden':
                case 'serial':
                    $hidden = true;
                    $_tmp = '';
                    break;
                default:
                    $_tmp = $_val;
            }
            if (!$_ispreset) {
                $_tmp = preg_replace("/_FCELL_/", qdollar($_tmp), FROW);
            }
            $_tmp = preg_replace("/_LABEL_/", $labels[$_key], $_tmp);
            if (!$hidden) {
                $_html_rows .= $_tmp;
            }
            $hidden = false;
        }
    } else {
        foreach ((array) $row as $_key => $_val) {
            $_tmp = '';
            if (!$dd[$_key]) {
                continue;
            }
            if ($dd[$_key]['references']) {
                $pos = strpos($dd[$_key]['references'], '.');
                if ($pos !== false) {
                    $r_table = substr($dd[$_key]['references'], 0, $pos);
                    $pos_2 = strpos($dd[$_key]['references'], '[');
                    if ($pos_2 !== false) {
                        $r_field = substr($dd[$_key]['references'], $pos_2 + 1, strlen($dd[$_key]['references']) - ($pos_2 + 2));
                    } else {
                        $r_field = $r_table;
                    }
                    $_val = $row[$r_field];
                } else {
                    $_val = $row[$dd[$_key]['references']];
                }
            }
            switch ($dd[$_key]['type']) {
                case 'char':
                    if ($dd[$_key]['extra']['list_values']) {
                        $_options = $dd[$_key]['extra']['list_values'];
                        $_tmp = $_options[$_val];
                    } else {
                        $_tmp = smarty_modifier_truncate($_val, 50);
                    }
                    break;
                case 'bool':
                case 'boolean':
                    $_si = ALM_YES;
                    $_no = ALM_NO;
                    if ($dd[$_key]['extra']['label_bool']) {
                        list($_si, $_no) = preg_split('/:/', $dd[$_key]['extra']['label_bool']);
                    }
                    $_tmp = $_val == 't' ? $_si : $_no;
                    break;
                case 'video':
                    if ($_val) {
                        require_once $smarty->_get_plugin_filepath('shared', 'video_service');
                        $a_vs = smarty_function_video_service();
                        //      XML
                        require_once 'XML/Unserializer.php';
                        $options = array(XML_UNSERIALIZER_OPTION_ATTRIBUTES_PARSE => true, XML_UNSERIALIZER_OPTION_ATTRIBUTES_ARRAYKEY => false);
                        $unserializer = new XML_Unserializer($options);
                        $status = $unserializer->unserialize($_val, false);
                        if (PEAR::isError($status)) {
                            echo 'Error: ' . $status->getMessage();
                        } else {
                            $vs = $unserializer->getUnserializedData();
                        }
                        //	-----
                        $_tmp = '<a href="javascript:openwindow(\'' . URL . '/' . ALM_URI . '/video.php?src=' . $vs['src'] . '&type=' . $vs['tipo'] . '\',400,333)"><img src="' . URL . '/' . ALM_URI . '/themes/' . ALM_ADMIN_THEME . '/img/' . $vs['tipo'] . '.png" alt="' . $a_vs[$vs['tipo']] . '" title="' . $a_vs[$vs['tipo']] . '" border="0" /></a>';
                    } else {
                        $_tmp = '--';
                    }
                    break;
                case 'file':
                    if ($_val) {
                        $_icon = $_val;
                        $_file = HOMEDIR . "/_" . $table . "/" . $_val;
                        $_icon = 'doc.png';
                        $_p = explode('.', $_file);
                        $_pc = count($_p);
                        if ($_p[$_pc - 1] == 'doc') {
                            $_icon = 'doc.png';
                        }
                        if ($_p[$_pc - 1] == 'pdf') {
                            $_icon = 'pdf.png';
                        }
                        if ($_p[$_pc - 1] == 'xls') {
                            $_icon = 'xls.png';
                        }
                        $_tmp = '<a title="' . $_val . '" href="' . URL . '/files/' . $table . '/' . $_val . '" target="_new"><img src="' . URL . '/' . ALM_URI . '/themes/' . ALM_ADMIN_THEME . '/img/' . $_icon . '" alt="' . $_val . '" border="0" /></a>';
                    } else {
                        $_tmp = '--';
                    }
                    break;
                case 'image':
                    if ($_val) {
                        if (THUMBNAILING) {
                            $_tmp = '<a href="javascript:openimage(\'' . URL . '/files/' . $table . '/' . $_val . '\',\'Imagen: ' . $_val . '\')"><img src="' . URL . '/' . ALM_URI . '/pic/50/' . $table . '/' . $_val . '" alt="' . $_val . '" width="50" border="0" /></a>';
                        } else {
                            $_tmp = '<a href="javascript:openimage(\'/files/' . $table . '/' . $_val . '\',\'Imagen: ' . $_val . '\')"><img src="' . URL . '/' . ALM_URI . '/pic/50/' . $table . '/' . $_val . '" alt="' . $_val . '" width="50" border="0" /></a>';
                        }
                    } else {
                        $_tmp = '--';
                    }
                    break;
                case 'hidden':
                case 'serial':
                    $hidden = true;
                    $_tmp = '';
                    break;
                default:
                    if ($dd[$_key]['extra']['list_values']) {
                        $_options = $dd[$_key]['extra']['list_values'];
                        $_val = $_options[trim($_val)];
                    }
                    if ($_preset[$_key]) {
                        $_tmp = '';
                        if (!empty($_pre)) {
                            $_pre .= ',';
                        }
                        $_pre .= $_key . '=' . $_preset[$_key];
                    } else {
                        $_tmp = smarty_modifier_wordwrap($_val);
                        $_tmp = smarty_modifier_nl2br($_tmp);
                    }
            }
            if (!$_preset[$_key]) {
                $_tmp = preg_replace('/_FCELL_/', qdollar($_tmp), FROW);
                $_tmp = preg_replace('/_LABEL_/', $labels[$_key], $_tmp);
            }
            if (!$hidden) {
                $_html_rows .= $_tmp;
            }
            $hidden = false;
        }
        $_html_cmd = preg_replace('/{_ID_}/', $row[$key], FCMD);
        if (!empty($_pre)) {
            $_pre = '<input type="hidden" name="preset" value="' . $_pre . '" />';
        }
    }
    if ($type == 2) {
        $_html_cmd = FCMDADD;
        $action = "add";
    }
    if ($type == 1) {
        $_html_cmd = FCMDMOD;
        $action = "save";
    }
    if ($type == 0) {
        $_html_cmd = FCMD;
        $action = "edit";
    }
    //if ($cmd)
    $_html_rows .= $_html_cmd;
    $_i++;
    $_tmp = FROW;
    if (!($_i % 2)) {
        $_tmp = preg_replace("/class=\"dgrow\"/", "class=\"dgrow2\"", $_tmp);
    }
    #$_html_rows .= preg_replace("/_FCELL_/", $_html_row, $_tmp);
    if ($paginate && $maxrows && $_i >= $maxrows * $pg) {
        $_need_paginate = true;
        break;
    }
    $_f = preg_replace('/_PRESET_/', $_pre, $h_f);
    //$_f = $h_f;
    $_html_result = preg_replace("/_FHEADER_/", $_html_labels, $_f);
    if ($cmd) {
        $_html_result = preg_replace("/_FHEADERCMD_/", FHEADERCMD, $_html_result);
    }
    $_html_result = preg_replace("/_FHEADERCMD_/", '', $_html_result);
    $_html_result = preg_replace("/_TITLE_/", $title, $_html_result);
    $_html_result = preg_replace('/_FROW_/', qdollar($_html_rows), $_html_result);
    #$_npgs = ceil(count($rows) / $maxrows);
    $_paginate = '';
    if ($paginate && $_npgs > 1) {
        if ($pg != 1) {
            $_paginate = PREV;
        }
        for ($_j = 1; $_j <= $_npgs; $_j++) {
            $npg = $_j == $pg ? CURRENTPG : NPG;
            $_paginate .= preg_replace("/_NPG_/", $_j, $npg);
        }
        if ($pg != $_npgs) {
            $_paginate .= NEXT;
        }
    }
    $_html_result = preg_replace("/_PAGINATE_/", $_paginate, $_html_result);
    # christian | a new way to know if this pages is the 404.php file
    $params = preg_split('/\\//', $_SERVER['PHP_SELF']);
    $page = $params[count($params) - 1];
    # end
    if ($page == '404.php') {
        $_html_result = preg_replace("/_SELF_/", SELF, $_html_result);
    } else {
        $_html_result = preg_replace("/_SELF_/", $_SERVER['PHP_SELF'], $_html_result);
    }
    $_referer = preg_replace("/\\//", "\\/", $_SERVER['PHP_SELF']);
    if (preg_match("/{$_referer}/", $_SERVER['HTTP_REFERER']) || $is_child || $_SERVER['PHP_SELF'] == '/cms/query.php' || $_SERVER['PHP_SELF'] == '/' . ALM_URI . '/query.php') {
        $_referer = $_SERVER['PHP_SELF'];
    } else {
        if (defined('SELF')) {
            $_referer = SELF;
        } else {
            $_referer = $_SERVER['PHP_SELF'];
        }
    }
    if ($is_child) {
        $_referer .= '?action=close';
    }
    // Si es el popup de consulta
    if ($_SERVER['PHP_SELF'] == '/cms/query.php' || $_SERVER['PHP_SELF'] == '/' . ALM_URI . '/query.php') {
        $_referer .= '?action=close';
    }
    $_html_result = preg_replace("/_REFERER_/", $_referer, $_html_result);
    $_html_result = preg_replace("/_KEY_/", $key, $_html_result);
    $_html_result = preg_replace("/_KEY1_/", $key1, $_html_result);
    $_html_result = preg_replace("/_KEY2_/", $key2, $_html_result);
    $_html_result = preg_replace("/{_ID_}/", $_REQUEST[$key], $_html_result);
    $_html_result = preg_replace("/_ID1_/", $_REQUEST[$key1], $_html_result);
    $_html_result = preg_replace("/_ID2_/", $_REQUEST[$key2], $_html_result);
    $_html_result = preg_replace("/_SORT_/", $_REQUEST['sort'], $_html_result);
    $_html_result = preg_replace("/_PG_/", $_REQUEST[$object . 'pg'], $_html_result);
    $_html_result = preg_replace("/_PGPREV_/", $pg - 1, $_html_result);
    $_html_result = preg_replace("/_PGNEXT_/", $pg + 1, $_html_result);
    $_html_result = preg_replace("/_FORM_/", $name, $_html_result);
    $_html_result = preg_replace("/_OBJECT_/", $object, $_html_result);
    $_html_result = preg_replace("/_ACTION_/", $action, $_html_result);
    if ($type == 0) {
        $_html_result = preg_replace('/old_/', '', $_html_result);
    }
    if (strpos($_html_rows, 'value="Agregar"') != '') {
        $_SESSION['accion'] = 'add';
    } elseif (isset($_SESSION['accion'])) {
        unset($_SESSION['accion']);
    }
    return $_html_result;
}
function smarty_function_dataform($params, &$smarty)
{
    require_once $smarty->_get_plugin_filepath('shared', 'escape_special_chars');
    require_once $smarty->_get_plugin_filepath('function', 'html_options');
    require_once $smarty->_get_plugin_filepath('function', 'html_select_date');
    require_once $smarty->_get_plugin_filepath('function', 'html_select_time');
    require_once $smarty->_get_plugin_filepath('modifier', 'truncate');
    require_once $smarty->_get_plugin_filepath('modifier', 'nl2br');
    require_once $smarty->_get_plugin_filepath('modifier', 'wordwrap');
    require_once $smarty->_get_plugin_filepath('modifier', 'return_bytes');
    require_once $smarty->_get_plugin_filepath('modifier', 'fsize_format');
    $row = array();
    $dd = array();
    $options = array();
    $keys = array();
    $type = 0;
    $name = 'dfform';
    $object = null;
    $edit = false;
    $table = null;
    $preset = null;
    $is_child = false;
    $extra = '';
    foreach ($params as $_key => $_val) {
        switch ($_key) {
            case 'name':
            case 'object':
            case 'title':
            case 'preset':
                ${$_key} = (string) $_val;
                break;
            case 'options':
            case 'row':
            case 'dd':
            case 'keys':
                ${$_key} = (array) $_val;
                break;
            case 'paginate':
            case 'is_child':
            case 'edit':
                ${$_key} = (bool) $_val;
                break;
            default:
                if (!is_array($_val)) {
                    $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
                } else {
                    $smarty->trigger_error("dataform: extra attribute '{$_key}' cannot be an array", E_USER_NOTICE);
                }
                break;
        }
    }
    if (!$object) {
        $object = $name;
    }
    if (!$table) {
        $table = $object;
    }
    if (empty($row) && empty($dd)) {
        $smarty->trigger_error("dataform: either dd or row attribute must be present", E_USER_NOTICE);
        return '';
    }
    if (empty($row)) {
        $type = 2;
        /* new record */
    }
    if ($edit) {
        $type = 1;
    }
    if (empty($dd)) {
        $labels = null;
    } else {
        foreach ($dd as $_key => $_val) {
            $labels[$_key] = $_val['label'];
        }
    }
    if ($preset) {
        list($_field, $_text) = preg_split('/,/', $preset);
        list($_fieldname, $_fieldvalue) = preg_split('/=/', $_field);
        $_preset[$_fieldname] = $_fieldvalue;
        $_preset_text[$_fieldname] = $_text;
    }
    $_html_rows = '';
    $_html_result = '';
    # Determinar el tamaño máximo de los archivo
    $upload = smarty_modifier_return_bytes(ini_get('upload_max_filesize'));
    $post = smarty_modifier_return_bytes(ini_get('post_max_size'));
    $max_size = 0;
    if ($upload < $post) {
        $max_size = $upload;
    } else {
        $max_size = $post;
    }
    $max_size = smarty_modifier_fsize_format($max_size);
    #
    # Crea las filas del dataform en modo edicion
    #
    $_i = 0;
    if ($type) {
        foreach ((array) $dd as $_key => $_val) {
            if (!$dd[$_key]) {
                continue;
            }
            if ($type == 2) {
                $_val = '';
            }
            if ($type == 1) {
                $_val = $row[$_val['name']];
            }
            # Tenemos permiso para mostrar este campo?
            if (isset($dd[$_key]['extra']['role']) && $dd[$_key]['extra']['role'] !== $_SESSION['idalm_role']) {
                $dd[$_key]['type'] = 'hidden';
            }
            if ($dd[$_key]['references'] && !in_array($dd[$_key]['type'], array('hidden', 'auto'))) {
                if ($_preset[$_key]) {
                    $_selected = $_preset[$_key];
                } else {
                    $_selected = $_val;
                    if (!isset($references[$dd[$_key]['references']])) {
                        $references[$dd[$_key]['references']] = 0;
                    }
                    $references[$dd[$_key]['references']]++;
                    $n = $references[$dd[$_key]['references']] == 1 ? '' : $references[$dd[$_key]['references']];
                    $_val = $row[$dd[$_key]['references'] . $n];
                }
                $dd[$_key]['type'] = 'references';
            }
            $_start_year = "-10";
            $_end_year = "+10";
            switch ($dd[$_key]['type']) {
                case 'auto':
                    if ($dd[$_key]['extra']['default']) {
                        if (!empty($options[$_key])) {
                            $_val = $options[$_key][trim($dd[$_key]['extra']['default'])];
                        } else {
                            $_val = $dd[$_key]['extra']['default'];
                        }
                        $_tmp = '<span class="auto">' . $_val . '</span>';
                    } else {
                        $_tmp = !empty($_val) ? $_val : '';
                    }
                    break;
                case 'file':
                    $_tmp = '';
                    if ($_val) {
                        $_file = HOMEDIR . "/_" . $table . "/" . $_val;
                        $_icon = 'doc.png';
                        $_p = explode('.', $_file);
                        $_pc = count($_p);
                        $ext = $_p[$_pc - 1];
                        if (preg_match('/doc|rtf|swx|osd/i', $ext)) {
                            $_icon = 'doc.png';
                        }
                        if (preg_match('/pdf/i', $ext)) {
                            $_icon = 'pdf.png';
                        }
                        if (preg_match('/xls/i', $ext)) {
                            $_icon = 'excel.png';
                        }
                        if (preg_match('/jpg|gif|png/i', $ext)) {
                            $_icon = 'image.png';
                        }
                        $_tmp = '<input type="checkbox" checked name="' . $_key . '_keep" /> Conservar archivo actual (' . $_val . ')<br /><img src="' . URL . '/' . ALM_URI . '/themes/' . ALM_ADMIN_THEME . '/img/' . $_icon . '" alt="' . $_val . '" border="0" /><br />';
                    }
                    $_tmp .= '[' . ALM_MAXIMUM . ' ' . $max_size . '] <input type="file" name="' . $_key . '" value="' . $_val . '" />';
                    break;
                case 'image':
                case 'img':
                    $_tmp = '';
                    $_icon = 'image.png';
                    $_tmp = '[' . ALM_MAXIMUM . ' ' . $max_size . '] <img src="' . URL . '/' . ALM_URI . '/themes/' . ALM_ADMIN_THEME . '/img/' . $_icon . '" border="0" alt="Imagen" title="Imagen" />';
                    if ($_val) {
                        $_tmp .= '<input type="checkbox" checked name="' . $_key . '_keep" /> Conservar archivo actual (' . $_val . ')<br /><img src="' . URL . '/' . ALM_URI . '/pic/50/' . $table . '/' . $_val . '" alt="' . $_val . '" width="50" border="0" /><br />';
                    }
                    $_tmp .= '<input type="file" name="' . $_key . '" value="' . $_val . '" />';
                    break;
                case 'boolean':
                case 'bool':
                    if (preg_match("/:/", $dd[$_key]['extra']['label_bool'])) {
                        list($_si, $_no) = preg_split('/:/', $dd[$_key]['extra']['label_bool']);
                        $_tchecked = $_val == 't' ? 'checked' : '';
                        $_fchecked = $_val == 'f' ? 'checked' : '';
                        $_tmp = $_si . '<input type="radio" name="' . $_key . '" ' . $_tchecked . ' value="on" />' . $_no . '<input type="radio" name="' . $_key . '" ' . $_fchecked . ' value="" />';
                    } else {
                        $_checked = $_val == 't' ? 'checked' : '';
                        $_tmp = '<input type="checkbox" name="' . $_key . '" ' . $_checked . ' />';
                    }
                    break;
                case 'datenull':
                    if (preg_match("/:/", $dd[$_key]['extra']['range'])) {
                        list($_start_year, $_end_year) = preg_split('/:/', $dd[$_key]['extra']['range']);
                    }
                    if (!isset($_val) || empty($_val)) {
                        $_val = '--';
                    }
                    $_tmp = smarty_function_html_select_date(array('prefix' => $_key . '_', 'time' => $_val, 'start_year' => $_start_year, 'end_year' => $_end_year, 'day_empty' => '--', 'month_empty' => '--', 'year_empty' => '--'), $smarty);
                    break;
                case 'date':
                    if (preg_match("/:/", $dd[$_key]['extra']['range'])) {
                        list($_start_year, $_end_year) = preg_split('/:/', $dd[$_key]['extra']['range']);
                    }
                    $_tmp = smarty_function_html_select_date(array('prefix' => $_key . '_', 'time' => $_val, 'start_year' => $_start_year, 'end_year' => $_end_year), $smarty);
                    break;
                case 'time':
                    $_tmp = smarty_function_html_select_time(array('prefix' => $_key . '_', 'time' => $_val, 'display_seconds' => false), $smarty);
                    break;
                case 'datetime':
                    if (preg_match("/:/", $dd[$_key]['extra']['range'])) {
                        list($_start_year, $_end_year) = preg_split('/:/', $dd[$_key]['extra']['range']);
                    }
                    $_tmp = smarty_function_html_select_date(array('prefix' => $_key . '_', 'time' => $_val, 'start_year' => $_start_year, 'end_year' => $_end_year), $smarty);
                    $_tmp .= smarty_function_html_select_time(array('prefix' => $_key . '_', 'time' => $_val, 'display_seconds' => false), $smarty);
                    break;
                case 'datetimenull':
                    if (preg_match("/:/", $dd[$_key]['extra']['range'])) {
                        list($_start_year, $_end_year) = preg_split('/:/', $dd[$_key]['extra']['range']);
                    }
                    $_tmp = smarty_function_html_select_date(array('prefix' => $_key . '_', 'time' => $_val, 'start_year' => $_start_year, 'end_year' => $_end_year, 'day_empty' => '--', 'month_empty' => '--', 'year_empty' => '--'), $smarty);
                    $_tmp .= smarty_function_html_select_time(array('prefix' => $_key . '_', 'time' => $_val, 'display_seconds' => false), $smarty);
                    break;
                case 'password':
                    $_tmp = '<input type="password" name="' . $_key . '" size="20" maxlength="16" />';
                    break;
                case 'text':
                    $_tmp = '<textarea rows="5" cols="40" name="' . $_key . '">' . $_val . '</textarea> <a href="javascript:edittext(\'' . $name . '\', \'' . $_key . '\', document.forms[\'' . $name . '\'].' . $_key . '.value);">' . ALM_MAX . '</a>';
                    break;
                case 'html':
                    $_tmp = '<textarea rows="5" cols="40" id="' . $_key . '" name="' . $_key . '">' . $_val . '</textarea> <a href="javascript:edithtml(\'' . $name . '\', \'' . $_key . '\', document.forms[\'' . $name . '\'].' . $_key . '.value);">' . ALM_MAX . '</a>';
                    $_tmp .= "<script language=\"JavaScript\">\ngenerate_wysiwyg('" . $_key . "');\n</script>\n";
                    break;
                case 'xhtml':
                    $_tmp = '<textarea rows="5" cols="40" class="adm" id="' . $_key . '" name="' . $_key . '"' . ($dd[$_key]['extra']['style'] ? ' style="' . $dd[$_key]['extra']['style'] . '"' : '') . '>' . $_val . '</textarea>';
                    $_tmp .= "<script language=\"JavaScript\">\ntinyMCE.execCommand(\"mceAddControl\", true,\"{$_key}\");\n</script>\n";
                    break;
                case 'varchar':
                case 'char':
                    if ($dd[$_key]['extra']['list_values']) {
                        $_options = $dd[$_key]['extra']['list_values'];
                        $_tmp = smarty_function_html_options(array('options' => $_options, 'selected' => $_val), $smarty);
                        $_tmp = preg_replace("/_REFERENCE_/", $_tmp, FCELLMODREF);
                        $_tmp = preg_replace("/_FIELD_/", $_key, $_tmp);
                    } else {
                        $_val = preg_replace("/\"/", "&quot;", $_val);
                        $_tmp = preg_replace("/_VALUE_/", qdollar($_val), FCELLMODSTR);
                        $_tmp = preg_replace("/_FIELD_/", $_key, $_tmp);
                        $_tmp = preg_replace("/_SIZE_/", $dd[$_key]['size'], $_tmp);
                    }
                    break;
                case 'numeric':
                case 'int':
                    if ($dd[$_key]['extra']['list_values']) {
                        $_options = $dd[$_key]['extra']['list_values'];
                        $_tmp = smarty_function_html_options(array('options' => $_options, 'selected' => $_val), $smarty);
                        $_tmp = preg_replace("/_REFERENCE_/", $_tmp, FCELLMODREF);
                        $_tmp = preg_replace("/_FIELD_/", $_key, $_tmp);
                    } else {
                        $_tmp = preg_replace("/_VALUE_/", $_val, FCELLMODSTR);
                        $_tmp = preg_replace("/_FIELD_/", $_key, $_tmp);
                        $_tmp = preg_replace("/_SIZE_/", 10, $_tmp);
                    }
                    break;
                case 'references':
                    if ($_preset[$_key]) {
                        $_tmp = '<input type="hidden" name="' . $_key . '" value="' . $_selected . '" />';
                        if ($options[$_key]) {
                            $_tmp .= $options[$_key][$_selected];
                        } else {
                            $_tmp .= $_selected;
                        }
                    } else {
                        $_options = $options[$_key];
                        $_tmp = smarty_function_html_options(array('options' => $_options, 'selected' => $_selected), $smarty);
                        $_tmp = preg_replace("/_REFERENCE_/", qdollar($_tmp), FCELLMODREF);
                        $_tmp = preg_replace("/_FIELD_/", $_key, $_tmp);
                    }
                    break;
                case 'hidden':
                case 'serial':
                    $hidden = true;
                    $_tmp = '';
                    break;
                default:
                    $_tmp = $_val;
            }
            if (!empty($dd[$_key]['extra']['help'])) {
                $_tmp .= '<sup title="' . $dd[$_key]['extra']['help'] . '">&nbsp;?&nbsp;</sup>';
            }
            $_tmp = preg_replace("/_FCELL_/", qdollar($_tmp), FROW);
            $_tmp = preg_replace("/_LABEL_/", $labels[$_key], $_tmp);
            if (!$hidden) {
                $_html_rows .= $_tmp;
            }
            $hidden = false;
        }
    } else {
        #
        # Crea las filas del dataform en modo lectura
        #
        foreach ((array) $row as $_key => $_val) {
            $_tmp = '';
            if (!$dd[$_key]) {
                continue;
            }
            # Tenemos permiso para mostrar este campo?
            if (isset($dd[$_key]['extra']['role']) && $dd[$_key]['extra']['role'] !== $_SESSION['idalm_role']) {
                $dd[$_key]['type'] = 'hidden';
            }
            if ($dd[$_key]['references']) {
                if (!isset($references[$dd[$_key]['references']])) {
                    $references[$dd[$_key]['references']] = 0;
                }
                $references[$dd[$_key]['references']]++;
                $n = $references[$dd[$_key]['references']] == 1 ? '' : $references[$dd[$_key]['references']];
                $_val = $row[$dd[$_key]['references'] . $n];
            }
            switch ($dd[$_key]['type']) {
                case 'char':
                    if ($dd[$_key]['extra']['list_values']) {
                        $_options = $dd[$_key]['extra']['list_values'];
                        $_tmp = $_options[$_val];
                    } else {
                        $_tmp = smarty_modifier_truncate($_val, 50);
                    }
                    break;
                case 'bool':
                case 'boolean':
                    $_si = ALM_YES;
                    $_no = ALM_NO;
                    if ($dd[$_key]['extra']['label_bool']) {
                        list($_si, $_no) = preg_split('/:/', $dd[$_key]['extra']['label_bool']);
                    }
                    $_tmp = $_val == 't' ? $_si : $_no;
                    break;
                case 'file':
                    if ($_val) {
                        $_icon = $_val;
                        $_file = HOMEDIR . "/_" . $table . "/" . $_val;
                        $_icon = 'doc.png';
                        $_p = explode('.', $_file);
                        $_pc = count($_p);
                        if ($_p[$_pc - 1] == 'doc') {
                            $_icon = 'doc.png';
                        }
                        if ($_p[$_pc - 1] == 'pdf') {
                            $_icon = 'pdf.png';
                        }
                        if ($_p[$_pc - 1] == 'xls') {
                            $_icon = 'xls.png';
                        }
                        $_tmp = '<a title="' . $_val . '" href="' . URL . '/files/' . $table . '/' . $_val . '" target="_new"><img src="' . URL . '/' . ALM_URI . '/themes/' . ALM_ADMIN_THEME . '/img/' . $_icon . '" alt="' . $_val . '" border="0" /></a>';
                    } else {
                        $_tmp = '--';
                    }
                    break;
                case 'image':
                    if ($_val) {
                        if (THUMBNAILING) {
                            $_tmp = '<a href="javascript:openimage(\'' . URL . '/files/' . $table . '/' . $_val . '\',\'Imagen: ' . $_val . '\')"><img src="' . URL . '/' . ALM_URI . '/pic/50/' . $table . '/' . $_val . '" alt="' . $_val . '" width="50" border="0" /></a>';
                        } else {
                            $_tmp = '<a href="javascript:openimage(\'' . URL . '/files/' . $table . '/' . $_val . '\',\'Imagen: ' . $_val . '\')"><img src="' . URL . '/' . ALM_URI . '/pic/50/' . $table . '/' . $_val . '" alt="' . $_val . '" width="50" border="0" /></a>';
                        }
                    } else {
                        $_tmp = '--';
                    }
                    break;
                case 'hidden':
                case 'serial':
                    $hidden = true;
                    $_tmp = '';
                    break;
                default:
                    if ($dd[$_key]['extra']['list_values']) {
                        $_options = $dd[$_key]['extra']['list_values'];
                        $_val = $_options[trim($_val)];
                    }
                    if ($_preset[$_key]) {
                        $_tmp = $_val;
                        if (!empty($_pre)) {
                            $_pre .= ',';
                        }
                        $_pre .= $_key . '=' . $_preset[$_key];
                    } else {
                        $_tmp = smarty_modifier_wordwrap($_val);
                        $_tmp = smarty_modifier_nl2br($_tmp);
                    }
            }
            $_tmp = preg_replace('/_FCELL_/', qdollar($_tmp), FROW);
            $_tmp = preg_replace('/_LABEL_/', $labels[$_key], $_tmp);
            if (!$hidden) {
                $_html_rows .= $_tmp;
            }
            $hidden = false;
        }
        $_html_cmd = preg_replace("/{_ID_}/", $row[$key], FCMD);
        if (!empty($_pre)) {
            $_pre = '<input type="hidden" name="preset" value="' . $_pre . '" />';
        }
    }
    if ($type == 2) {
        $_html_cmd = FCMDADD;
        $action = "add";
    }
    if ($type == 1) {
        $_html_cmd = FCMDMOD;
        $action = "save";
    }
    if ($type == 0) {
        $_html_cmd = FCMD;
        $action = "edit";
    }
    //if ($cmd)
    $_html_rows .= $_html_cmd;
    $_i++;
    $_tmp = FROW;
    if (!($_i % 2)) {
        $_tmp = preg_replace("/class=\"dgrow\"/", "class=\"dgrow2\"", $_tmp);
    }
    #$_html_rows .= preg_replace("/_FCELL_/", $_html_row, $_tmp);
    if ($paginate && $maxrows && $_i >= $maxrows * $pg) {
        $_need_paginate = true;
        break;
    }
    $_f = F;
    $oldkeys = null;
    foreach ($keys as $val) {
        $oldkeys .= '<input type="hidden" name="alm_old_' . $val . '" value="' . $row[$val] . '" />';
    }
    $_f = preg_replace("/{_OLDKEYS_}/", $oldkeys, $_f);
    $_f = preg_replace('/{_PRESET_}/', $_pre, $_f);
    $_html_result = preg_replace("/_FHEADER_/", $_html_labels, $_f);
    if ($cmd) {
        $_html_result = preg_replace("/_FHEADERCMD_/", FHEADERCMD, $_html_result);
    }
    $_html_result = preg_replace("/_FHEADERCMD_/", '', $_html_result);
    $_html_result = preg_replace("/_TITLE_/", $title, $_html_result);
    $_html_result = preg_replace("/_FROW_/", qdollar($_html_rows), $_html_result);
    #$_npgs = ceil(count($rows) / $maxrows);
    $_paginate = '';
    if ($paginate && $_npgs > 1) {
        if ($pg != 1) {
            $_paginate = PREV;
        }
        for ($_j = 1; $_j <= $_npgs; $_j++) {
            $npg = $_j == $pg ? CURRENTPG : NPG;
            $_paginate .= preg_replace("/_NPG_/", $_j, $npg);
        }
        if ($pg != $_npgs) {
            $_paginate .= NEXT;
        }
    }
    $_html_result = preg_replace("/_PAGINATE_/", $_paginate, $_html_result);
    $params = preg_split('/\\//', $_SERVER['PHP_SELF']);
    $page = $params[count($params) - 1];
    if ($page == '404.php') {
        $_html_result = preg_replace("/_SELF_/", SELF, $_html_result);
    } else {
        $_html_result = preg_replace("/_SELF_/", $_SERVER['PHP_SELF'], $_html_result);
    }
    $_referer = preg_replace("/\\//", "\\/", $_SERVER['PHP_SELF']);
    if (preg_match("/{$_referer}/", $_SERVER['HTTP_REFERER']) || $is_child) {
        $_referer = $_SERVER['PHP_SELF'];
    } else {
        if (defined('SELF')) {
            $_referer = SELF;
        } else {
            $_referer = $_SERVER['HTTP_REFERER'];
        }
    }
    if ($is_child) {
        $_referer .= '?action=close';
    }
    $_html_result = preg_replace("/_REFERER_/", $_referer, $_html_result);
    $_html_result = preg_replace("/_KEY_/", $key, $_html_result);
    $_html_result = preg_replace("/_KEY1_/", $key1, $_html_result);
    $_html_result = preg_replace("/_KEY2_/", $key2, $_html_result);
    $_html_result = preg_replace("/{_ID_}/", $_REQUEST[$key], $_html_result);
    $_html_result = preg_replace("/_ID1_/", $_REQUEST[$key1], $_html_result);
    $_html_result = preg_replace("/_ID2_/", $_REQUEST[$key2], $_html_result);
    $_html_result = preg_replace("/_SORT_/", $_REQUEST['sort'], $_html_result);
    $_html_result = preg_replace("/_PG_/", $_REQUEST[$object . 'pg'], $_html_result);
    $_html_result = preg_replace("/_PGPREV_/", $pg - 1, $_html_result);
    $_html_result = preg_replace("/_PGNEXT_/", $pg + 1, $_html_result);
    $_html_result = preg_replace("/_FORM_/", $name, $_html_result);
    $_html_result = preg_replace("/_OBJECT_/", $object, $_html_result);
    $_html_result = preg_replace("/_ACTION_/", $action, $_html_result);
    if ($type == 0) {
        $_html_result = preg_replace('/alm_old_/', '', $_html_result);
    }
    if (strpos($_html_rows, 'value="Agregar"') != '') {
        $_SESSION['accion'] = 'add';
    } elseif (isset($_SESSION['accion'])) {
        unset($_SESSION['accion']);
    }
    return $_html_result;
}
function smarty_function_datagrid($params, &$smarty)
{
    require_once $smarty->_get_plugin_filepath('shared', 'escape_special_chars');
    require_once $smarty->_get_plugin_filepath('function', 'html_options');
    require_once $smarty->_get_plugin_filepath('function', 'html_select_date');
    require_once $smarty->_get_plugin_filepath('function', 'html_select_time');
    require_once $smarty->_get_plugin_filepath('modifier', 'truncate');
    require_once $smarty->_get_plugin_filepath('modifier', 'wordwrap');
    require_once $smarty->_get_plugin_filepath('modifier', 'url');
    $rows = array();
    $dd = array();
    $options = array();
    $paginate = false;
    $selected = null;
    $keys = array();
    $maxrows = defined('MAXROWS') ? MAXROWS : 5;
    $maxcols = defined('MAXCOLS') ? MAXCOLS : 5;
    $name = 'dgform';
    $table = null;
    $parent = null;
    $truncate = true;
    $is_child = null;
    $have_child = null;
    $extra = '';
    foreach ($params as $_key => $_val) {
        switch ($_key) {
            case 'name':
            case 'title':
            case 'parent':
                ${$_key} = (string) $_val;
                break;
            case 'options':
            case 'rows':
            case 'dd':
            case 'keys':
                ${$_key} = (array) $_val;
                break;
            case 'paginate':
            case 'search':
            case 'cmd':
            case 'truncate':
            case 'have_child':
            case 'is_child':
                ${$_key} = (bool) $_val;
                break;
            case 'selected':
                if (is_array($_val)) {
                    $smarty->trigger_error('datagrid: the "' . $_key . '" attribute cannot be an array', E_USER_WARNING);
                } else {
                    $selected = (string) $_val;
                }
                break;
            case 'num_rows':
            case 'maxcols':
            case 'maxrows':
                ${$_key} = (int) $_val;
                break;
            default:
                if (!is_array($_val)) {
                    $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
                } else {
                    $smarty->trigger_error("datagrid: extra attribute '{$_key}' cannot be an array", E_USER_NOTICE);
                }
                break;
        }
    }
    if (empty($rows)) {
        $smarty->trigger_error("datagrid: rows attribute must be present", E_USER_NOTICE);
        return '';
        /* raise error here? */
    }
    if (empty($dd)) {
        $headers = null;
    } else {
        foreach ($dd as $_key => $_val) {
            $headers[$_key] = $_val['label'];
        }
    }
    if (!$table) {
        $table = $name;
    }
    $_html_search = '';
    $_html_headers = '';
    $_html_rows = '';
    $_html_result = '';
    #
    # Crea los encabezados del datagrid
    #
    $_cols = 0;
    if (!empty($headers)) {
        foreach ($headers as $_key => $_val) {
            if ($maxcols && $_cols >= $maxcols) {
                break;
            }
            # Tenemos permiso para mostrar este campo?
            if (isset($dd[$_key]['extra']['role']) && $dd[$_key]['extra']['role'] !== $_SESSION['idalm_role']) {
                $dd[$_key]['type'] = 'hidden';
            }
            if ($parent == $_key || $dd[$_key]['type'] == 'external' || $dd[$_key]['type'] == 'hidden') {
                continue;
            }
            $_html_header = DGHEADERCELL;
            # Para permitir referencias multiples a la misma tabla
            if ($dd[$_key]['references']) {
                if (!isset($references[$dd[$_key]['references']])) {
                    $references[$dd[$_key]['references']] = 0;
                }
                if ($dd[$_key]['references'] == $name && !$references[$dd[$_key]['references']]) {
                    $references[$dd[$_key]['references']] += 2;
                } else {
                    $references[$dd[$_key]['references']]++;
                }
                $n = $references[$dd[$_key]['references']] == 1 ? '' : $references[$dd[$_key]['references']];
                $_field = $dd[$_key]['references'] . $n;
            } else {
                $_field = $_key;
            }
            if ($_SESSION[$name . 'sort'] == $_field) {
                $_img = '<img src="' . URL . '/' . ALM_URI . '/themes/' . ALM_ADMIN_THEME . '/img/up.gif" border="0" />';
                $_html_header = preg_replace("/_DESC_/", ' desc', $_html_header);
                $_html_header = preg_replace("/_SORTIMG_/", $_img, $_html_header);
            } else {
                $_html_header = preg_replace("/_DESC_/", '', $_html_header);
            }
            if ($_SESSION[$name . 'sort'] == $_field . ' desc') {
                $_img = '<img src="' . URL . '/' . ALM_URI . '/themes/' . ALM_ADMIN_THEME . '/img/down.gif" border="0" />';
                $_html_header = preg_replace("/_SORTIMG_/", $_img, $_html_header);
            } else {
                $_html_header = preg_replace("/_SORTIMG_/", '', $_html_header);
            }
            $_html_header = preg_replace("/_LABEL_/", $_val, $_html_header);
            $_html_header = preg_replace("/_FIELD_/", $_field, $_html_header);
            $_html_headers .= $_html_header;
            ++$_cols;
        }
        unset($references);
    } else {
        foreach ($rows[0] as $_key => $_val) {
            if ($maxcols && $_cols >= $maxcols) {
                break;
            }
            $_html_header = preg_replace("/_LABEL_|_FIELD_/", $_key, DGHEADERCELL);
            $_html_headers .= $_html_header;
            ++$_cols;
        }
    }
    $_i = 0;
    $pg = $_SESSION[$name . 'pg'] ? $_SESSION[$name . 'pg'] : 1;
    foreach ((array) $rows as $row) {
        unset($references);
        #
        # Crea las filas del datagrid en modo edicion
        #
        $_html_row = '';
        $_chosen = true;
        foreach ($keys as $val) {
            if ($_REQUEST[$val] !== $row[$val]) {
                $_chosen = false;
                break;
            }
        }
        if ($_REQUEST['f'] == $name && $_REQUEST['action'] == 'mod' && $_chosen) {
            $selected_row = $row;
            $_cols = 0;
            foreach ($row as $_key => $_val) {
                if ($maxcols && $_cols >= $maxcols) {
                    break;
                }
                if (!$dd[$_key]) {
                    continue;
                }
                # Tenemos permiso para mostrar este campo?
                if (isset($dd[$_key]['extra']['role']) && $dd[$_key]['extra']['role'] !== $_SESSION['idalm_role']) {
                    $dd[$_key]['type'] = 'hidden';
                }
                if ($parent == $_key) {
                    $parentid = $_val;
                    $dd[$_key]['type'] = 'hidden';
                } elseif ($dd[$_key]['references']) {
                    $_selected = $_val;
                    if (!isset($references[$dd[$_key]['references']])) {
                        $references[$dd[$_key]['references']] = 0;
                    }
                    if ($dd[$_key]['references'] == $name && !$references[$dd[$_key]['references']]) {
                        $references[$dd[$_key]['references']] += 2;
                    } else {
                        $references[$dd[$_key]['references']]++;
                    }
                    $n = $references[$dd[$_key]['references']] == 1 ? '' : $references[$dd[$_key]['references']];
                    $_val = $row[$dd[$_key]['references'] . $n];
                    $dd[$_key]['type'] = 'references';
                }
                switch ($dd[$_key]['type']) {
                    case 'file':
                    case 'image':
                    case 'img':
                        $_tmp = '';
                        if ($_val) {
                            $_tmp = '<input type="checkbox" checked name="' . $_key . '_keep" /> ' . ALM_KEEP_FILE . ' (' . $_val . ')<br /><img src="' . URL . '/' . ALM_URI . '/pic/50/' . $table . '/' . $_val . '" alt="' . $_val . '" width="50" border="0" /><br />';
                        }
                        $_tmp .= '<input type="file" name="' . $_key . '" value="' . $_val . '" />';
                        break;
                    case 'time':
                        $_tmp = smarty_function_html_select_time(array('prefix' => $_key . '_', 'time' => $_val, 'display_seconds' => false), $smarty);
                        break;
                    case 'datetime':
                        $_tmp = '<input type="hidden" name="' . $_key . '" value="' . $_val . '" />';
                        $_tmp .= $_val;
                        break;
                    case 'date':
                        $_tmp = smarty_function_html_select_date(array('prefix' => $_key . '_', 'time' => $_val, 'start_year' => "-10", 'end_year' => "+10"), $smarty);
                        break;
                    case 'boolean':
                    case 'bool':
                        if ($dd[$_key]['extra']['label_bool']) {
                            list($_si, $_no) = preg_split('/:/', $dd[$_key]['extra']['label_bool']);
                            $_tchecked = $_val == 't' ? 'checked' : '';
                            $_fchecked = $_val == 'f' ? 'checked' : '';
                            $_tmp = $_si . '<input type="radio" name="' . $_key . '" ' . $_tchecked . ' value="on">' . $_no . '<input type="radio" name="' . $_key . '" ' . $_fchecked . ' value="">';
                        } else {
                            $_checked = $_val == 't' ? 'checked' : '';
                            $_tmp = '<input type="checkbox" name="' . $_key . '" ' . $_checked . ' />';
                        }
                        break;
                    case 'text':
                        $_tmp = preg_replace("/_VALUE_/", qdollar(htmlentities($_val, ENT_COMPAT, 'UTF-8')), DGCELLMODSTR);
                        $_tmp = preg_replace("/_FIELD_/", $_key, $_tmp);
                        break;
                    case 'xhtml':
                        $_tmp = '<textarea rows="5" cols="40" class="adm" id="grid' . $_key . '" name="' . $_key . '"' . ($dd[$_key]['extra']['style'] ? ' style="' . $dd[$_key]['extra']['style'] . '"' : '') . '>' . $_val . '</textarea>';
                        $_tmp .= "<script language=\"JavaScript\">\ntinyMCE.execCommand(\"mceAddControl\", true,\"grid{$_key}\");\n</script>\n";
                        break;
                    case 'varchar':
                    case 'char':
                        if ($dd[$_key]['extra']['list_values']) {
                            $_options = $dd[$_key]['extra']['list_values'];
                            $_tmp = smarty_function_html_options(array('options' => $_options, 'selected' => trim($_val)), $smarty);
                            $_tmp = preg_replace("/_REFERENCE_/", qdollar($_tmp), DGCELLMODREF);
                            $_tmp = preg_replace("/_FIELD_/", $_key, $_tmp);
                        } else {
                            $_tmp = preg_replace("/_VALUE_/", qdollar(htmlentities($_val, ENT_COMPAT, 'UTF-8')), DGCELLMODSTR);
                            $_tmp = preg_replace("/_FIELD_/", $_key, $_tmp);
                            $_tmp = preg_replace("/_SIZE_/", $dd[$_key]['size'], $_tmp);
                        }
                        break;
                    case 'int':
                    case 'numeric':
                        if ($dd[$_key]['extra']['list_values']) {
                            $_options = $dd[$_key]['extra']['list_values'];
                            $_tmp = smarty_function_html_options(array('options' => $_options, 'selected' => trim($_val)), $smarty);
                            $_tmp = preg_replace("/_REFERENCE_/", qdollar($_tmp), DGCELLMODREF);
                            $_tmp = preg_replace("/_FIELD_/", $_key, $_tmp);
                        } else {
                            $_tmp = preg_replace("/_VALUE_/", $_val, DGCELLMODSTR);
                            $_tmp = preg_replace("/_FIELD_/", $_key, $_tmp);
                            $_tmp = preg_replace("/_SIZE_/", 10, $_tmp);
                        }
                        break;
                    case 'references':
                        $_options = $options[$_key];
                        $_tmp = smarty_function_html_options(array('options' => $_options, 'selected' => $_selected), $smarty);
                        $_tmp = preg_replace("/_REFERENCE_/", qdollar($_tmp), DGCELLMODREF);
                        $_tmp = preg_replace("/_FIELD_/", $_key, $_tmp);
                        break;
                    case 'order':
                        $_tmp = '- +';
                        break;
                        #case 'hidden':
                        #  $_tmp = '<input type="hidden" name="'.$_key.'" value="'.$_val.'"  />';
                        #  break;
                    #case 'hidden':
                    #  $_tmp = '<input type="hidden" name="'.$_key.'" value="'.$_val.'"  />';
                    #  break;
                    case 'hidden':
                        $hidden = true;
                        $_tmp = '';
                        break;
                    default:
                        $_tmp = $_val;
                }
                if ($dd[$_key]['type'] != 'hidden') {
                    $_tmp = preg_replace("/_VALUE_/", qdollar($_tmp), DGCELL);
                    $_cols++;
                }
                $_html_row .= $_tmp;
            }
            if ($_SESSION['credentials'][$table] == 'full' || $_SESSION['credentials'][$table] == 'edit' || $_SESSION['idalm_user'] === 'admin') {
                $_dgcmdmod = DGCMDMOD;
            }
            /*
            $key_id = null;
            foreach($keys as $val)
              $key_id[] = $val .'='. $row[$val];
            $_html_cmd = preg_replace("/{_IDS_}/", 'array('. join(',',$key_id) .')', $_dgcmdmod);
            */
            $_html_cmd = $_dgcmdmod;
        } else {
            #
            # Crea las filas del datagrid en modo lectura
            #
            $_cols = 0;
            foreach ($row as $_key => $_val) {
                if ($maxcols && $_cols >= $maxcols) {
                    break;
                }
                if (!$dd[$_key] || $dd[$_key]['type'] == 'external') {
                    continue;
                }
                # Tenemos permiso para mostrar este campo?
                if (isset($dd[$_key]['extra']['role']) && $dd[$_key]['extra']['role'] !== $_SESSION['idalm_role']) {
                    $dd[$_key]['type'] = 'hidden';
                }
                if ($parent == $_key) {
                    $parentid = $_val;
                    continue;
                }
                if ($dd[$_key]['references']) {
                    if (!isset($references[$dd[$_key]['references']])) {
                        $references[$dd[$_key]['references']] = 0;
                    }
                    if ($dd[$_key]['references'] == $name && !$references[$dd[$_key]['references']]) {
                        $references[$dd[$_key]['references']] += 2;
                    } else {
                        $references[$dd[$_key]['references']]++;
                    }
                    $n = $references[$dd[$_key]['references']] == 1 ? '' : $references[$dd[$_key]['references']];
                    $_val = empty($_val) ? '--' : $row[$dd[$_key]['references'] . $n];
                }
                switch ($dd[$_key]['type']) {
                    case 'varchar':
                    case 'char':
                        if ($dd[$_key]['extra']['list_values']) {
                            $_options = $dd[$_key]['extra']['list_values'];
                            $_tmp = $_options[trim($_val)];
                        } else {
                            $_tmp = smarty_modifier_truncate($_val, 50);
                            $_tmp = smarty_modifier_url($_tmp);
                            $_tmp = preg_replace("/_SIZE_/", $dd[$_key]['size'], $_tmp);
                        }
                        break;
                    case 'bool':
                    case 'boolean':
                        $_si = ALM_YES;
                        $_no = ALM_NO;
                        if ($dd[$_key]['extra']['label_bool']) {
                            list($_si, $_no) = preg_split('/:/', $dd[$_key]['extra']['label_bool']);
                        }
                        $_tmp = $_val == 't' ? $_si : $_no;
                        break;
                    case 'file':
                        if ($_val) {
                            $_file = HOMEDIR . "/_" . $table . "/" . $_val;
                            $_icon = 'doc.png';
                            $_p = explode('.', $_file);
                            $_pc = count($_p);
                            $ext = $_p[$_pc - 1];
                            if (preg_match('/doc|rtf|swx|osd/i', $ext)) {
                                $_icon = 'doc.png';
                            }
                            if (preg_match('/pdf/i', $ext)) {
                                $_icon = 'pdf.png';
                            }
                            if (preg_match('/xls/i', $ext)) {
                                $_icon = 'excel.png';
                            }
                            if (preg_match('/jpg|gif|png/i', $ext)) {
                                $_icon = 'image.png';
                            }
                            $_tmp = '<a href="' . URL . '/files/' . $table . '/' . $_val . '" target="_new"><img src="' . URL . '/' . ALM_URI . '/themes/' . ALM_ADMIN_THEME . '/img/' . $_icon . '" alt="' . $_val . '" border="0" /></a>';
                        } else {
                            $_tmp = '--';
                        }
                        break;
                    case 'image':
                        if ($_val) {
                            if (THUMBNAILING) {
                                $_tmp = '<a href="javascript:openimage(\'' . URL . '/files/' . $table . '/' . $_val . '\',\'Imagen: ' . $_val . '\')"><img src="' . URL . '/' . ALM_URI . '/pic/50/' . $table . '/' . $_val . '" alt="' . $_val . '" width="50" border="0" /></a>';
                            } else {
                                $_tmp = '<a href="javascript:openimage(\'/files/' . $table . '/' . $_val . '\',\'Imagen: ' . $_val . '\')"><img src="/_' . $table . '/' . $_val . '" alt="' . $_val . '" width="50" height="20" border="0" /></a>';
                            }
                        } else {
                            $_tmp = '--';
                        }
                        break;
                    case 'order':
                        $_tmp = '';
                        if ($_SESSION[$name . 'first'] != $row[$key]) {
                            $_tmp .= '<a href="_SELF_?action=move&' . $key . '=' . $row[$key] . '&sense=up&key=' . $_key . '"><img src="' . URI . '/' . ALM_URI . '/themes/' . ALM_ADMIN_THEME . '/img/up.gif" border="0"/></a>';
                        }
                        if ($_SESSION[$name . 'last'] != $row[$key]) {
                            if (!empty($_tmp)) {
                                $tmp = ' ';
                            }
                            $_tmp .= '<a href="_SELF_?action=move&' . $key . '=' . $row[$key] . '&sense=down&key=' . $_key . '"><img src="' . URL . '/' . ALM_URI . '/themes/' . ALM_ADMIN_THEME . '/img/down.gif" border="0"/></a>';
                        }
                        if (empty($_tmp)) {
                            $_tmp = '--';
                        }
                        break;
                    case 'hidden':
                        $hidden = true;
                        $_tmp = '';
                        break;
                    case 'text':
                    case 'html':
                    case 'xhtml':
                        # strip_tags quita los tags [x]html, preg_replace reemplaza los &nbsp; por espacio en blanco, el otro preg_replace quita mas de un espacio en blanco conjunto y lo reemplaza por un solo espacio y trim quita los espacios en blanco al final e inicio de la cadena.
                        $_val = trim(preg_replace('/\\s\\s+/', ' ', preg_replace('/&nbsp;/', ' ', strip_tags($_val))));
                    default:
                        $_val = $_val === '' ? "&nbsp;" : $_val;
                        if ($truncate) {
                            $_tmp = smarty_modifier_truncate($_val, 50);
                        } else {
                            $_tmp = smarty_modifier_wordwrap($_val, 50, "<br/>");
                        }
                        $_tmp = smarty_modifier_url($_tmp);
                }
                if ($dd[$_key]['type'] != 'hidden') {
                    $_tmp = preg_replace("/_VALUE_/", qdollar($_tmp), DGCELL);
                    $_cols++;
                }
                $_html_row .= $_tmp;
            }
            if ($_cols <= 3 || $parent) {
                if ($_SESSION['credentials'][$table] == 'full' || $_SESSION['idalm_user'] === 'admin') {
                    if ($is_child) {
                        $_dgcmd = DGCMD_det;
                    } else {
                        $_dgcmd = DGCMDR;
                    }
                } elseif ($_SESSION['credentials'][$table] == 'edit') {
                    $_dgcmd = DGCMDREDIT;
                } elseif ($_SESSION['credentials'][$table] == 'delete') {
                    $_dgcmd = DGCMDRDEL;
                } elseif ($_SESSION['credentials'][$table] == 'read') {
                    $_dgcmd = DGCMDRVER;
                }
            } else {
                if ($_SESSION['credentials'][$table] == 'full' || $_SESSION['idalm_user'] === 'admin') {
                    $_dgcmd = DGCMD;
                } elseif ($_SESSION['credentials'][$table] == 'edit') {
                    $_dgcmd = DGCMDEDIT;
                } elseif ($_SESSION['credentials'][$table] == 'delete') {
                    $_dgcmd = DGCMDDEL;
                } elseif ($_SESSION['credentials'][$table] == 'read') {
                    $_dgcmd = DGCMDVER;
                }
            }
            $ids = null;
            $key_id_url = null;
            $key_id_js = null;
            foreach ($keys as $val) {
                $key_id_url[] = $val . '=' . $row[$val];
                $key_id_js[] = "'{$val}':'" . $row[$val] . "'";
                $ids[] = "'" . $row[$val] . "'";
            }
            /* If it has a child, set parent keys */
            if (!empty($parent)) {
                $key_id_js[] = "'{$parent}':'" . $row[$parent] . "'";
            }
            $_html_cmd = preg_replace("/{_KEY=ID_}/", join('&amp;', $key_id_url), $_dgcmd);
            $_html_cmd = preg_replace("/{_KEY:ID_}/", '{' . join(',', $key_id_js) . '}', $_html_cmd);
            $_html_cmd = preg_replace("/{_IDS_}/", '[' . join(',', $ids) . ']', $_html_cmd);
        }
        if ($cmd) {
            $_html_row .= $_html_cmd;
        }
        $_i++;
        $_tmp = DGROW;
        if (!($_i % 2)) {
            $_tmp = preg_replace("/class=\"dgrow\"/", "class=\"dgrow2\"", $_tmp);
        }
        $_html_rows .= preg_replace("/_DGCELL_/", qdollar($_html_row), $_tmp);
        if ($paginate && $maxrows && $_i >= $maxrows * $pg) {
            $_need_paginate = true;
            break;
        }
    }
    $_dg = DG;
    $oldkeys = null;
    foreach ($keys as $val) {
        $oldkeys .= '<input type="hidden" name="alm_old_' . $val . '" value="' . $selected_row[$val] . '" />';
    }
    $_dg = preg_replace("/{_OLDKEYS_}/", $oldkeys, $_dg);
    $_html_result = preg_replace("/_DGHEADER_/", $_html_headers, $_dg);
    if ($search === true) {
        $_dgsearch = preg_replace("/{_Q_}/", htmlentities($_REQUEST['q']), DGSEARCH);
        $_html_result = preg_replace("/{_DGSEARCH_}/", $_dgsearch, $_html_result);
    } else {
        $_html_result = preg_replace("/{_DGSEARCH_}/", '', $_html_result);
    }
    if ($cmd) {
        $_html_result = preg_replace("/_DGHEADERCMD_/", DGHEADERCMD, $_html_result);
    }
    $_html_result = preg_replace("/_DGHEADERCMD_/", '', $_html_result);
    $_html_result = preg_replace("/_TITLE_/", $title, $_html_result);
    $_html_result = preg_replace("/_ROWS_/", $num_rows, $_html_result);
    $_html_result = preg_replace("/_DGROW_/", qdollar($_html_rows), $_html_result);
    # Paginacion del datagrid
    $_npgs = ceil($num_rows / $maxrows);
    $_paginate = '';
    if ($paginate && $_npgs > 1) {
        if ($pg != 1) {
            $_paginate = PREV;
        }
        for ($_j = 1; $_j <= $_npgs; $_j++) {
            if ($_j > 2 && $_npgs > 10 && $_npgs - $_j >= 2 && abs($pg - $_j) > 3) {
                $npg = $npg != '...' && $npg ? '...' : '';
            } else {
                $npg = $_j == $pg ? CURRENTPG : NPG;
            }
            $_paginate .= preg_replace("/_NPG_/", $_j, $npg);
        }
        if ($pg != $_npgs) {
            $_paginate .= NEXT;
        }
    }
    $_html_result = preg_replace("/_PARENT_/", $parent, $_html_result);
    $_html_result = preg_replace("/_PARENTID_/", $parentid, $_html_result);
    $_html_result = preg_replace("/_PAGINATE_/", $_paginate, $_html_result);
    $params = preg_split('/\\//', $_SERVER['PHP_SELF']);
    $page = $params[count($params) - 1];
    if ($page == '404.php' || $page == '404c.php') {
        $_html_result = preg_replace("/_SELF_/", SELF, $_html_result);
    } else {
        $_html_result = preg_replace("/_SELF_/", $_SERVER['PHP_SELF'], $_html_result);
    }
    $akeys = null;
    foreach ($keys as $val) {
        $akeys[] = "'" . $val . "'";
    }
    $_html_result = preg_replace("/{_KEYS_}/", '[' . join(',', $akeys) . ']', $_html_result);
    #$_html_result = preg_replace("/{_ID_}/", $_REQUEST[$key], $_html_result);
    $_html_result = preg_replace("/_SORT_/", $_SESSION[$name . 'sort'], $_html_result);
    $_html_result = preg_replace("/_PG_/", $_SESSION[$name . 'pg'], $_html_result);
    $_html_result = preg_replace("/_PGPREV_/", $pg - 1, $_html_result);
    $_html_result = preg_replace("/_PGNEXT_/", $pg + 1, $_html_result);
    $_html_result = preg_replace("/_MAXCOLS_/", $maxcols, $_html_result);
    $_html_result = preg_replace("/_FORM_/", $name, $_html_result);
    $_html_result = preg_replace("/_Q_/", $_REQUEST['q'], $_html_result);
    return $_html_result;
}
function smarty_function_datagrid2($params, &$smarty)
{
    require_once $smarty->_get_plugin_filepath('shared', 'escape_special_chars');
    require_once $smarty->_get_plugin_filepath('function', 'html_options');
    require_once $smarty->_get_plugin_filepath('function', 'html_select_date');
    require_once $smarty->_get_plugin_filepath('function', 'html_select_time');
    require_once $smarty->_get_plugin_filepath('modifier', 'truncate');
    require_once $smarty->_get_plugin_filepath('modifier', 'wordwrap');
    require_once $smarty->_get_plugin_filepath('modifier', 'url');
    $rows = array();
    $dd = array();
    $options = array();
    $paginate = false;
    $selected = null;
    $key = null;
    $key1 = null;
    $key2 = null;
    $maxrows = defined('MAXROWS') ? MAXROWS : 5;
    $maxcols = defined('MAXCOLS') ? MAXCOLS : 5;
    $name = 'dgform';
    $table = null;
    $parent = null;
    $truncate = true;
    $is_child = null;
    $have_child = null;
    $extra = '';
    foreach ($params as $_key => $_val) {
        switch ($_key) {
            case 'name':
            case 'title':
            case 'key':
            case 'key1':
            case 'key2':
            case 'parent':
            case 'search':
                ${$_key} = (string) $_val;
                break;
            case 'options':
            case 'rows':
            case 'dd':
                ${$_key} = (array) $_val;
                break;
            case 'shortEdit':
                if (isset($params[$_key])) {
                    ${$_key} = (bool) $_val;
                }
                break;
            case 'paginate':
            case 'cmd':
            case 'truncate':
            case 'have_child':
            case 'is_child':
                ${$_key} = (bool) $_val;
                break;
            case 'selected':
                if (is_array($_val)) {
                    $smarty->trigger_error('datagrid: the "' . $_key . '" attribute cannot be an array', E_USER_WARNING);
                } else {
                    $selected = (string) $_val;
                }
                break;
            case 'num_rows':
            case 'maxcols':
            case 'maxrows':
                ${$_key} = (int) $_val;
                break;
            default:
                if (!is_array($_val)) {
                    $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
                } else {
                    $smarty->trigger_error("datagrid: extra attribute '{$_key}' cannot be an array", E_USER_NOTICE);
                }
                break;
        }
    }
    if (empty($rows) && empty($search)) {
        #$smarty->trigger_error("datagrid: rows attribute must be present", E_USER_NOTICE);
        return ALM_NODATA;
        /* raise error here? */
    }
    if (empty($dd)) {
        $headers = null;
    } else {
        foreach ($dd as $_key => $_val) {
            $headers[$_key] = $_val['label'];
        }
    }
    if (!$table) {
        $table = $name;
    }
    $_html_search = '';
    $_html_headers = '';
    $_html_rows = '';
    $_html_result = '';
    # Crea el Buscador del datagrid
    if (!empty($search)) {
        $_html_search = preg_replace("/_FIELDS_/", 'Filtro', DS);
        $fields = preg_split('/,/', trim($search));
        $_html_sch = '';
        if ($fields) {
            foreach ($fields as $field) {
                $_tmp = '';
                if (!empty($_html_sch)) {
                    $_html_sch .= ' | ';
                }
                switch ($dd[$field]['type']) {
                    case 'varchar':
                        $_tmp = preg_replace("/_LABEL_/", $dd[$field]['label'], DSLABEL);
                        $_tmp .= '&nbsp;' . preg_replace("/_VALUE_/", '', DGCELLMODSTR);
                        $_tmp = preg_replace("/_FIELD_/", $field . 'search', $_tmp);
                        $_tmp = preg_replace("/_SIZE_/", $dd[$field]['size'], $_tmp);
                        if ($dd[$field]['extra']['autocomplete']) {
                            $_tmp .= '<script type="text/javascript">
                          $(\'#' . $field . 'search\').autocomplete({ serviceUrl:' . ($dd[$field]['extra']['autocomplete_sr'] ? $dd[$field]['extra']['autocomplete_sr'] : '\'' . URL . '/' . ALM_URI . '/js/autocomplete/autocomplete.php\'') . ', delimiter: /(,|;)\\s*/, params: { table:\'' . ($dd[$field]['extra']['autocomplete_tb'] ? $dd[$field]['extra']['autocomplete_tb'] : $table) . '\', field:\'' . ($dd[$field]['extra']['autocomplete_fd'] ? $dd[$field]['extra']['autocomplete_fd'] : ($dd[$field]['extra']['autocomplete_tb'] ? $dd[$field]['extra']['autocomplete_tb'] : $field)) . '\' } });
                        </script>';
                        }
                        break;
                }
                $_html_sch .= $_tmp;
            }
        }
        $_html_search = preg_replace("/_DSFIELDS_/", $_html_sch, $_html_search);
    }
    # Crea los encabezados del datagrid
    if (!empty($rows)) {
        $_cols = 0;
        if (!empty($headers)) {
            foreach ($headers as $_key => $_val) {
                if ($maxcols && $_cols >= $maxcols) {
                    break;
                }
                if ($parent == $_key || $dd[$_key]['type'] == 'external') {
                    continue;
                }
                $_html_header = DGHEADERCELL;
                $_field = $dd[$_key]['references'] ? $dd[$_key]['references'] : $_key;
                if ($_SESSION[$name . 'sort'] == $_field) {
                    $_img = '<img src="' . URL . '/' . ALM_URI . '/themes/' . ALM_ADMIN_THEME . '/img/up.gif" border="0" />';
                    $_html_header = preg_replace("/_DESC_/", ' desc', $_html_header);
                    $_html_header = preg_replace("/_SORTIMG_/", $_img, $_html_header);
                } else {
                    $_html_header = preg_replace("/_DESC_/", '', $_html_header);
                }
                if ($_SESSION[$name . 'sort'] == $_field . ' desc') {
                    $_img = '<img src="' . URL . '/' . ALM_URI . '/themes/' . ALM_ADMIN_THEME . '/img/down.gif" border="0" />';
                    $_html_header = preg_replace("/_SORTIMG_/", $_img, $_html_header);
                } else {
                    $_html_header = preg_replace("/_SORTIMG_/", '', $_html_header);
                }
                $_html_header = preg_replace("/_LABEL_/", $_val, $_html_header);
                $_html_header = preg_replace("/_FIELD_/", $_field, $_html_header);
                $_html_headers .= $_html_header;
                ++$_cols;
            }
        } else {
            foreach ($rows[0] as $_key => $_val) {
                if ($maxcols && $_cols >= $maxcols) {
                    break;
                }
                $_html_header = preg_replace("/_LABEL_|_FIELD_/", $_key, DGHEADERCELL);
                $_html_headers .= $_html_header;
                ++$_cols;
            }
        }
        # Crea las filas del datagrid
        $_i = 0;
        $pg = $_SESSION[$name . 'pg'] ? $_SESSION[$name . 'pg'] : 1;
        foreach ((array) $rows as $row) {
            $_html_row = '';
            $_chosen = $key2 ? $_REQUEST[$key1] == $row[$key1] && $_REQUEST[$key2] == $row[$key2] : $_REQUEST[$key] == $row[$key];
            if ($_REQUEST['f'] == $name && $_REQUEST['action'] == 'mod' && $_chosen) {
                $_cols = 0;
                foreach ($row as $_key => $_val) {
                    if ($maxcols && $_cols >= $maxcols) {
                        break;
                    }
                    if (!$dd[$_key]) {
                        continue;
                    }
                    if ($parent == $_key) {
                        $parentid = $_val;
                        $dd[$_key]['type'] = 'hidden';
                    } elseif ($dd[$_key]['references']) {
                        $_selected = $_val;
                        $_val = $row[$dd[$_key]['references']];
                        $dd[$_key]['type'] = 'references';
                    }
                    switch ($dd[$_key]['type']) {
                        case 'hidden':
                            $_tmp = '<input type="hidden" name="' . $_key . '" value="' . $_val . '"  />';
                            break;
                        case 'file':
                        case 'image':
                        case 'img':
                            $_tmp = '';
                            if ($_val) {
                                $_tmp = '<input type="checkbox" checked name="' . $_key . '_keep" /> Conservar archivo actual (' . $_val . ')<br /><img src="' . URL . '/' . ALM_URI . '/pic/50/' . $table . '/' . $_val . '" alt="' . $_val . '" width="50" border="0" /><br />';
                            }
                            $_tmp .= '<input type="file" name="' . $_key . '" value="' . $_val . '" />';
                            break;
                        case 'time':
                            $_tmp = smarty_function_html_select_time(array('prefix' => $_key . '_', 'time' => $_val, 'display_seconds' => false), $smarty);
                            break;
                        case 'datetime':
                            $_tmp = '<input type="hidden" name="' . $_key . '" value="' . $_val . '" />';
                            $_tmp .= $_val;
                            break;
                        case 'date':
                            $_tmp = smarty_function_html_select_date(array('prefix' => $_key . '_', 'time' => $_val, 'start_year' => "-10", 'end_year' => "+10"), $smarty);
                            break;
                        case 'boolean':
                        case 'bool':
                            if ($dd[$_key]['extra']['label_bool']) {
                                list($_si, $_no) = preg_split('/:/', $dd[$_key]['extra']['label_bool']);
                                $_tchecked = $_val == 't' ? 'checked' : '';
                                $_fchecked = $_val == 'f' ? 'checked' : '';
                                $_tmp = $_si . '<input type="radio" name="' . $_key . '" ' . $_tchecked . ' value="on">' . $_no . '<input type="radio" name="' . $_key . '" ' . $_fchecked . ' value="">';
                            } else {
                                $_checked = $_val == 't' ? 'checked' : '';
                                $_tmp = '<input type="checkbox" name="' . $_key . '" ' . $_checked . ' />';
                            }
                            break;
                        case 'text':
                            $_tmp = preg_replace("/_VALUE_/", qdollar($_val), DGCELLMODTXT);
                            $_tmp = preg_replace("/_FIELD_/", $_key, $_tmp);
                            break;
                        case 'xhtml':
                            $_tmp = '<textarea rows="5" cols="40" class="adm" id="grid' . $_key . '" name="' . $_key . '"' . ($dd[$_key]['extra']['style'] ? ' style="' . $dd[$_key]['extra']['style'] . '"' : '') . '>' . $_val . '</textarea>';
                            $_tmp .= "<script language=\"JavaScript\">\ntinyMCE.execCommand(\"mceAddControl\", true,\"grid{$_key}\");\n</script>\n";
                            break;
                        case 'varchar':
                        case 'char':
                            if ($dd[$_key]['extra']['list_values']) {
                                $_options = $dd[$_key]['extra']['list_values'];
                                $_tmp = smarty_function_html_options(array('options' => $_options, 'selected' => trim($_val)), $smarty);
                                $_tmp = preg_replace("/_REFERENCE_/", qdollar($_tmp), DGCELLMODREF);
                                $_tmp = preg_replace("/_FIELD_/", $_key, $_tmp);
                            } else {
                                $_tmp = preg_replace("/_VALUE_/", qdollar($_val), DGCELLMODSTR);
                                $_tmp = preg_replace("/_FIELD_/", $_key, $_tmp);
                                $_tmp = preg_replace("/_SIZE_/", $dd[$_key]['size'], $_tmp);
                            }
                            break;
                        case 'int':
                        case 'numeric':
                            $_tmp = preg_replace("/_VALUE_/", $_val, DGCELLMODSTR);
                            $_tmp = preg_replace("/_FIELD_/", $_key, $_tmp);
                            $_tmp = preg_replace("/_SIZE_/", 10, $_tmp);
                            break;
                        case 'references':
                            $_options = $options[$_key];
                            $_tmp = smarty_function_html_options(array('options' => $_options, 'selected' => $_selected), $smarty);
                            $_tmp = preg_replace("/_REFERENCE_/", $_tmp, DGCELLMODREF);
                            $_tmp = preg_replace("/_FIELD_/", $_key, $_tmp);
                            break;
                        case 'order':
                            $_tmp = '- +';
                            break;
                        default:
                            $_tmp = $_val;
                    }
                    if ($dd[$_key]['type'] != 'hidden') {
                        $_tmp = preg_replace("/_VALUE_/", qdollar($_tmp), DGCELL);
                        $_cols++;
                    }
                    $_html_row .= $_tmp;
                }
                $_dgcmdmod = $key2 ? DGCMD2MOD : DGCMDMOD;
                $_html_cmd = preg_replace("/{_ID_}/", $row[$key], $_dgcmdmod);
            } else {
                $_cols = 0;
                foreach ($row as $_key => $_val) {
                    if ($maxcols && $_cols >= $maxcols) {
                        break;
                    }
                    if (!$dd[$_key] || $dd[$_key]['type'] == 'external') {
                        continue;
                    }
                    if ($parent == $_key) {
                        $parentid = $_val;
                        continue;
                    }
                    if ($dd[$_key]['references']) {
                        $pos = strpos($dd[$_key]['references'], '.');
                        if ($pos !== false) {
                            $r_table = substr($dd[$_key]['references'], 0, $pos);
                            $pos_2 = strpos($dd[$_key]['references'], '[');
                            if ($pos_2 !== false) {
                                $r_field = substr($dd[$_key]['references'], $pos_2 + 1, strlen($dd[$_key]['references']) - ($pos_2 + 2));
                            } else {
                                $r_field = $r_table;
                            }
                            $_val = $row[$r_field];
                        } else {
                            $_val = $row[$dd[$_key]['references']];
                        }
                    }
                    switch ($dd[$_key]['type']) {
                        case 'char':
                            if ($dd[$_key]['extra']['list_values']) {
                                $_options = $dd[$_key]['extra']['list_values'];
                                $_tmp = $_options[trim($_val)];
                            } else {
                                $_tmp = smarty_modifier_truncate($_val, 50);
                                $_tmp = smarty_modifier_url($_tmp);
                                $_tmp = preg_replace("/_SIZE_/", $dd[$_key]['size'], $_tmp);
                            }
                            break;
                        case 'bool':
                        case 'boolean':
                            $_si = ALM_YES;
                            $_no = ALM_NO;
                            if ($dd[$_key]['extra']['label_bool']) {
                                list($_si, $_no) = preg_split('/:/', $dd[$_key]['extra']['label_bool']);
                            }
                            $_tmp = $_val == 't' ? $_si : $_no;
                            break;
                        case 'video':
                            if ($_val) {
                                require_once $smarty->_get_plugin_filepath('shared', 'video_service');
                                $a_vs = smarty_function_video_service();
                                //      XML
                                require_once 'XML/Unserializer.php';
                                $options = array(XML_UNSERIALIZER_OPTION_ATTRIBUTES_PARSE => true, XML_UNSERIALIZER_OPTION_ATTRIBUTES_ARRAYKEY => false);
                                $unserializer = new XML_Unserializer($options);
                                $status = $unserializer->unserialize($_val, false);
                                if (PEAR::isError($status)) {
                                    echo 'Error: ' . $status->getMessage();
                                } else {
                                    $vs = $unserializer->getUnserializedData();
                                }
                                $_tmp = '<a href="javascript:openwindow(\'' . URL . '/' . ALM_URI . '/video.php?src=' . $vs['src'] . '&type=' . $vs['tipo'] . '\',400,333)"><img src="' . URL . '/' . ALM_URI . '/themes/' . ALM_ADMIN_THEME . '/img/' . $vs['tipo'] . '.png" alt="' . $a_vs[$vs['tipo']] . '" title="' . $a_vs[$vs['tipo']] . '" border="0" /></a>';
                            } else {
                                $_tmp = '--';
                            }
                            break;
                        case 'file':
                            if ($_val) {
                                $_file = HOMEDIR . "/_" . $table . "/" . $_val;
                                $_icon = 'doc.png';
                                $_p = explode('.', $_file);
                                $_pc = count($_p);
                                $ext = $_p[$_pc - 1];
                                if (preg_match('/doc|rtf|swx|osd/i', $ext)) {
                                    $_icon = 'doc.png';
                                }
                                if (preg_match('/pdf/i', $ext)) {
                                    $_icon = 'pdf.png';
                                }
                                if (preg_match('/xls/i', $ext)) {
                                    $_icon = 'excel.png';
                                }
                                if (preg_match('/jpg|gif|png/i', $ext)) {
                                    $_icon = 'image.png';
                                }
                                $_tmp = '<a href="' . URL . '/files/' . $table . '/' . $_val . '" target="_new"><img src="' . URL . '/' . ALM_URI . '/themes/' . ALM_ADMIN_THEME . '/img/' . $_icon . '" alt="' . $_val . '" border="0" /></a>';
                            } else {
                                $_tmp = '--';
                            }
                            break;
                        case 'image':
                            if ($_val) {
                                if (THUMBNAILING) {
                                    $_tmp = '<a href="javascript:openimage(\'' . URL . '/files/' . $table . '/' . $_val . '\',\'Imagen: ' . $_val . '\')"><img src="' . URL . '/' . ALM_URI . '/pic/50/' . $table . '/' . $_val . '" alt="' . $_val . '" width="50" border="0" /></a>';
                                } else {
                                    $_tmp = '<a href="javascript:openimage(\'' . URL . '/files/' . $table . '/' . $_val . '\',\'Imagen: ' . $_val . '\')"><img src="/_' . $table . '/' . $_val . '" alt="' . $_val . '" width="50" height="20" border="0" /></a>';
                                }
                            } else {
                                $_tmp = '--';
                            }
                            break;
                        case 'order':
                            $_tmp = '';
                            if ($_SESSION[$name . 'first'] != $row[$key]) {
                                $_tmp .= '<a href="_SELF_?action=move&' . $key . '=' . $row[$key] . '&sense=up&key=' . $_key . '"><img src="' . URL . '/' . ALM_URI . '/themes/' . ALM_ADMIN_THEME . '/img/up.gif" border="0"/></a>';
                            }
                            if ($_SESSION[$name . 'last'] != $row[$key]) {
                                if (!empty($_tmp)) {
                                    $tmp = ' ';
                                }
                                $_tmp .= '<a href="_SELF_?action=move&' . $key . '=' . $row[$key] . '&sense=down&key=' . $_key . '"><img src="' . URL . '/' . ALM_URI . '/themes/' . ALM_ADMIN_THEME . '/img/down.gif" border="0"/></a>';
                            }
                            if (empty($_tmp)) {
                                $_tmp = '--';
                            }
                            break;
                        case 'text':
                        case 'html':
                        case 'xhtml':
                            # strip_tags quita los tags [x]html, preg_replace reemplaza los &nbsp; por espacio en blanco, el otro preg_replace quita mas de un espacio en blanco conjunto y lo reemplaza por un solo espacio y trim quita los espacios en blanco al final e inicio de la cadena.
                            $_val = trim(preg_replace('/\\s\\s+/', ' ', preg_replace('/&nbsp;/', ' ', strip_tags($_val))));
                        case 'varchar':
                            if ($dd[$_key]['extra']['list_values']) {
                                $_options = $dd[$_key]['extra']['list_values'];
                                $_tmp = $_options[trim($_val)];
                                break;
                            }
                        default:
                            if ($truncate) {
                                $_tmp = smarty_modifier_truncate($_val, 50);
                            } else {
                                $_tmp = smarty_modifier_wordwrap($_val, 50, "<br/>");
                            }
                            $_tmp = smarty_modifier_url($_tmp);
                    }
                    $_html_row .= preg_replace("/_VALUE_/", qdollar($_tmp), DGCELL);
                    $_cols++;
                }
                if ($key2) {
                    $_dgcmd = $_cols <= 3 || $parent ? DGCMD2R : DGCMD2;
                } elseif ($is_child) {
                    $_dgcmd = DGCMD_det;
                } else {
                    $_dgcmd = ($_cols <= 3 || $parent) && !$have_child ? $shortEdit === false ? DGCMD_NOSHORT_EDIT : DGCMDR : DGCMD;
                }
                $_html_cmd = preg_replace("/{_ID_}/", $row[$key], $_dgcmd);
                $_html_cmd = preg_replace("/_ID1_/", $row[$key1], $_html_cmd);
                $_html_cmd = preg_replace("/_ID2_/", $row[$key2], $_html_cmd);
            }
            if ($cmd) {
                $_html_row .= $_html_cmd;
            }
            $_i++;
            $_tmp = DGROW;
            if (!($_i % 2)) {
                $_tmp = preg_replace("/class=\"dgrow\"/", "class=\"dgrow2\"", $_tmp);
            }
            $_html_rows .= preg_replace("/_DGCELL_/", qdollar($_html_row), $_tmp);
            if ($paginate && $maxrows && $_i >= $maxrows * $pg) {
                $_need_paginate = true;
                break;
            }
        }
        $_dg = $key2 ? DG2 : DG;
        $_html_result = preg_replace("/_DGHEADER_/", $_html_headers, $_dg);
        if (!empty($search)) {
            $_html_result = $_html_search . $_html_result;
        }
        if ($cmd) {
            $_html_result = preg_replace("/_DGHEADERCMD_/", DGHEADERCMD, $_html_result);
        }
        $_html_result = preg_replace("/_DGHEADERCMD_/", '', $_html_result);
        $_html_result = preg_replace("/_TITLE_/", $title, $_html_result);
        $_html_result = preg_replace("/_ROWS_/", $num_rows, $_html_result);
        $_html_result = preg_replace("/_DGROW_/", qdollar($_html_rows), $_html_result);
        # Paginacion del datagrid
        $_npgs = ceil($num_rows / $maxrows);
        $_paginate = '';
        if ($paginate && $_npgs > 1) {
            if ($pg != 1) {
                $_paginate = PREV;
            }
            for ($_j = 1; $_j <= $_npgs; $_j++) {
                if ($_j > 2 && $_npgs > 10 && $_npgs - $_j >= 2 && abs($pg - $_j) > 3) {
                    $npg = $npg != '...' && $npg ? '...' : '';
                } else {
                    $npg = $_j == $pg ? CURRENTPG : NPG;
                }
                $_paginate .= preg_replace("/_NPG_/", $_j, $npg);
            }
            if ($pg != $_npgs) {
                $_paginate .= NEXT;
            }
        }
        $_html_result = preg_replace("/_PARENT_/", $parent, $_html_result);
        $_html_result = preg_replace("/_PARENTID_/", $parentid, $_html_result);
        $_html_result = preg_replace("/_PAGINATE_/", $_paginate, $_html_result);
        # christian | a new way to know if this pages is the 404.php file
        $params = preg_split('/\\//', $_SERVER['PHP_SELF']);
        $page = $params[count($params) - 1];
        # end
        if ($page == '404.php' || $page == '404c.php') {
            $_html_result = preg_replace("/_SELF_/", SELF, $_html_result);
        } else {
            $_html_result = preg_replace("/_SELF_/", $_SERVER['PHP_SELF'], $_html_result);
        }
        $_html_result = preg_replace("/_KEY_/", $key, $_html_result);
        $_html_result = preg_replace("/_KEY1_/", $key1, $_html_result);
        $_html_result = preg_replace("/_KEY2_/", $key2, $_html_result);
        $_html_result = preg_replace("/{_ID_}/", $_REQUEST[$key], $_html_result);
        $_html_result = preg_replace("/_ID1_/", $_REQUEST[$key1], $_html_result);
        $_html_result = preg_replace("/_ID2_/", $_REQUEST[$key2], $_html_result);
        $_html_result = preg_replace("/_SORT_/", $_SESSION[$name . 'sort'], $_html_result);
        $_html_result = preg_replace("/_PG_/", $_SESSION[$name . 'pg'], $_html_result);
        $_html_result = preg_replace("/_PGPREV_/", $pg - 1, $_html_result);
        $_html_result = preg_replace("/_PGNEXT_/", $pg + 1, $_html_result);
        $_html_result = preg_replace("/_MAXCOLS_/", $maxcols, $_html_result);
        $_html_result = preg_replace("/_FORM_/", $name, $_html_result);
        $_html_result = preg_replace("/_Q_/", $_REQUEST['q'], $_html_result);
    } else {
        if (!empty($search)) {
            $_html_result = $_html_search;
            if ($_SESSION[$table . 'query']) {
                $_html_result .= DSL . "<br />";
            }
        }
        # christian | a new way to know if this pages is the 404.php file
        $params = preg_split('/\\//', $_SERVER['PHP_SELF']);
        $page = $params[count($params) - 1];
        # end
        if ($page == '404.php' || $page == '404c.php') {
            $_html_result = preg_replace("/_SELF_/", SELF, $_html_result);
        } else {
            $_html_result = preg_replace("/_SELF_/", $_SERVER['PHP_SELF'], $_html_result);
        }
        $_html_result = preg_replace("/_DSQUERY_/", $_SESSION[$table . 'ssearch'], $_html_result);
        $_html_result .= ALM_NODATA;
    }
    return $_html_result;
}