<?php
/**
 * Smarty plugin
 *
 * @category Smarty
 * @license /_licenses/untermStrich/agb_normal.pdf
 * @author Christian Koller
 * @copyright 2009 untermStrich
 * @package Smarty
 * @subpackage plugins
 * @version $Id$
 * @deprecated Check the Smarty template tableeditpop.tpl
*/

/**
 * Smarty {tableeditpop} function plugin
 *
 * @param   string  $params     Parameters - See below
 * @param   object  &$smarty    The smarty object
 * @return  string
 *
 * @uses    _smarty_function_tableeditpop()
 * @deprecated Check the Smarty template tableeditpop.tpl
 */
function smarty_function_tableeditpop($params, US_Smarty &$smarty)
{
    $processor = new Smarty_class_tableeditpop(
            element('tabname', $params, null),
            element('header', $params, array()),
            element('whatview', $params, array()),
            element('list', $params, array()),
            element('no_rows_msg', $params, null),
            element('user_can_delete', $params, false),
            element('user_can_add', $params, false),
            element('user_can_edit', $params, null),
            element('hover_script', $params, null),
            element('order_by_def', $params, null),
            element('order_by_dir_def', $params, null)
    );

    return $processor->view($smarty);
}

/**
 * @deprecated Check the Smarty template tableeditpop.tpl
 */
class Smarty_class_tableeditpop
{
    protected $CI;

    protected $tabname;
    protected $header;
    protected $whatview;
    protected $list;
    protected $no_rows_msg;
    protected $user_can_delete;
    protected $user_can_add;
    protected $user_can_edit;
    protected $hover_script;
    protected $order_by_def;
    protected $order_by_dir_def;

    protected $reply;

    /**
     * Creates a table list with editable links.
     *
     * @param   string  $tabname            ID of the table and name of the corrosponding javascript action object.
     *                                      The javascript object has to provide the following methodes:
     *                                      <ul>
     *                                          <li>open            Opens the edit popup - parameter:
     *                                                              <ul>
     *                                                                 <li>mixed   Either 'add' or the item ID</li>
     *                                                                 <li>boolean Are the values NOT editable</li>
     *                                                                 <li>string  The message to show if it is not editable</li>
     *                                                              </ul>
     *                                          </li>
     *                                          <li>delete_entry    Delete entry - parameter:
     *                                                              <ul>
     *                                                                 <li>integer The item ID to delete</li>
     *                                                              </ul>
     *                                          </li>
     *                                          <li>order_by        Only active if the "format" parameter is an array holding the order by name - parameter:
     *                                                              <ul>
     *                                                                 <li>string  The name of the field in the database to order by</li>
     *                                                              </ul>
     *                                          </li>
     *                                          <li>hover_update    Only active if the "hover_script" parameter is set to TRUE - parameter:
     *                                                              <ul>
     *                                                                 <li>integer The item ID</li>
     *                                                              </ul>
     *                                          </li>
     *                                          <li>hover_stop      Only active if the "hover_script" parameter is set to TRUE.
     *                                                              Stops the hover script
     *                                          </li>
     *                                      </ul>
     * @param   array   $header             An assoc array holding the header infos.
     *                                      The key holds the name (key) of the translation.
     *                                      Each item is one of:
     *                                          Either an array:
     *                                          <ul>
     *                                              <li>0   string  A string with the formating CSS</li>
     *                                              <li>1   string  The name of the field in the database to order by (see order_by above)</li>
     *                                          </ul>
     *                                          Or it holds a string with the formating CSS
     * @param   array   $whatview           An assoc array holding the row infos.
     *                                      The key holds the field name in the "list" item object.
     *                                      Each item is one of:
     *                                          Either an array:
     *                                          <ul>
     *                                              <li>0   string  Format - Same as below at string only</li>
     *                                              <li>1   string  A string with the formating CSS for each cell</li>
     *                                          </ul>
     *                                          Or it holds a string with field formating infos.
     *                                          It can be either "bool" for boolean values or it holds the truncate value.
     * @param   array   $list               The values array. It holds the following objects:
     *                                      <ul>
     *                                          <li>rowinfo                 object  Optional Formatting infos:
     *                                                                              <ul>
     *                                                                                  <li>title   string  Title for the row</li>
     *                                                                                  <li>class   string  A CSS class for the row</li>
     *                                                                              </ul>
     *                                          </li>
     *                                          <li>object_is_editable      boolean Is the object editable</li>
     *                                          <li>object_is_editable_msg  string  Optional The message to show when the object is not editable</li>
     *                                          <li>id                      mixed   The item ID</li>
     *                                          <li>...                     mixed   Any type of fields (string, object) defined via whatview</li>
     *                                      </ul>
     * @param   string  $no_rows_msg        The message to show, if there are no results (One of $i.basic_no_result1, $i.basic_no_result2)
     * @param   boolean $user_can_delete    Optional Show the delete button
     * @param   boolean $user_can_add       Optional Show the add button
     * @param   boolean $user_can_edit      Optional Overwrite the object_is_editable setting of each item
     * @param   boolean $hover_script       Optional Activate the hover over script
     * @param   mixed   $order_by_def       Optional The default value for order by
     * @param   mixed   $order_by_dir_def   Optional The default value for order by direction
     */
    public function __construct($tabname, array $header, array $whatview, array $list, $no_rows_msg, $user_can_delete, $user_can_add, $user_can_edit, $hover_script, $order_by_def, $order_by_dir_def)
    {
        if (empty($tabname)) basic_error('Missing "tabname"', null, __FUNCTION__);
        if (empty($header) && !is_array($header)) basic_error('Missing "header"', null, __FUNCTION__);
        if (empty($whatview) && !is_array($whatview)) basic_error('Missing "whatview"', null, __FUNCTION__);
        if (empty($list) && !is_array($list)) basic_error('Missing "list"', null, __FUNCTION__);
        if (empty($no_rows_msg)) basic_error('Missing "no_rows_msg"', null, __FUNCTION__);

        $this->tabname = $tabname;
        $this->header = $header;
        $this->whatview = $whatview;
        $this->list = $list;
        $this->no_rows_msg = $no_rows_msg;
        $this->user_can_delete = $user_can_delete;
        $this->user_can_add = $user_can_add;
        $this->user_can_edit = $user_can_edit;
        $this->hover_script = $hover_script;
        $this->order_by_def = $order_by_def;
        $this->order_by_dir_def = $order_by_dir_def;
        $this->CI = CI_Controller::get_instance();
    }

    /**
     * Return the HTML as string
     *
     * @param   object  &$smarty    The main smarty object#
     * @return  string
     */
    public function view(US_Smarty &$smarty)
    {
        $this->reply = '';
        $this->create_header();

        if (!empty($this->hover_script)) {
            $hover_out = 'onmouseout="'.$this->tabname.'.hover_stop();"';
        } else {
            $hover_out = '';
        }
        return '<table id="edtp-table-'.$this->tabname.'" class="editablepop" '.$hover_out.'>'.$this->reply.'</table>';
    }

    /**
     * Create the header part of the table
     *
     * @return  void
     */
    protected function create_header()
    {
        $this->reply .= '<thead>';
        $this->reply .= '<tr>';

        $this->reply .= '<th class="'.$this->get_header_left_class().' noprint">';
        if ($this->user_can_add) {
            $this->reply .= sprintf(
                    '<span onclick="%s.open(\'add\', false, \'\');" class="dialog_make_btn16 add smallbtns ui-corner-all" title="%s"></span>',
                    $this->tabname,
                    $this->CI->lang->line('basic_add')
            );
        }
        $this->reply .= '</th>';

        foreach ($this->header as $item => $format) {
            if (is_array($format)) {
                $this->reply .= '<th style="'.$format[0].'">';
                $this->reply .= sprintf(
                        '<span onclick="%s.order_by(\'%s\')" class="order_by %s">%s</span>',
                        $this->tabname,
                        $format[1],
                        (isset($order_by_def) && $order_by_def==$format[1]) ? 'selected'.$order_by_dir_def : '',
                        $this->CI->lang->line($item)
                );
                $this->reply .= '</th>';
            } else {
                $this->reply .= '<th style="'.$format.'">'.$this->CI->lang->line($item).'</th>';
            }
        }

        $this->reply .= '</tr>';
        $this->reply .= '</thead>';
    }

    /**
     * Get the css class of the left header
     *
     * @return  string
     */
    protected function get_header_left_class()
    {
        $cssclass = 'edt';
        if ($this->user_can_delete) {
            $cssclass .= 'del';
        }
        if (!empty($this->hover_script)) {
            $cssclass .= 'hov';
        }
        return $cssclass.'p-btn';
    }

}

/* End of function.tableeditpop.php */