Classes

File builder/table/HTMLTable.class.php

File builder/table/HTMLTable.class.php

  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 
<?php
/**
 * This class allows you to manage easily html tables.
 * @package     Builder
 * @subpackage  Table
 * @copyright   &copy; 2005-2019 PHPBoost
 * @license     https://www.gnu.org/licenses/gpl-3.0.html GNU/GPL-3.0
 * @author      Loic ROUCHON <horn@phpboost.com>
 * @version     PHPBoost 5.2 - last update: 2018 10 23
 * @since       PHPBoost 3.0 - 2009 12 26
 * @contributor Julien BRISWALTER <j1.seth@phpboost.com>
*/

class HTMLTable extends AbstractHTMLElement
{
    private $arg_id = 1;
    private $nb_rows = 0;
    private $page_number = 1;

    /**
     * @var HTMLTableParameters
     */
    public $parameters;

    /**
     * @var Template
     */
    private $tpl;

    /**
     * @var HTMLTableModel
     */
    private $model;

    /**
     * @var HTMLTableColumn[]
     */
    private $columns = array();

    /**
     * @var HTMLTableRow[]
     */
    private $rows = array();

    private $filters_fieldset_class = 'FormFieldsetHorizontal';

    public function __construct(HTMLTableModel $model, $css_class = 'table', $tpl_path = '')
    {
        if ($tpl_path === '')
        {
            $tpl_path = 'framework/builder/table/HTMLTable.tpl';
        }
        $model->set_html_table($this);

        $this->css_class = $css_class;
        $this->tpl = new FileTemplate($tpl_path);
        $this->model = $model;
        $this->columns = $this->model->get_columns();
        $this->parameters = new HTMLTableParameters($this->model);
    }

    /**
     * @return Template
     */
    public function display()
    {
        $this->generate_filters_form();
        $this->generate_table_structure();
        $this->generate_headers();
        $this->generate_rows();
        $this->generate_rows_stats();
        return $this->tpl;
    }

    private function extract_parameters()
    {
        $nb_rows_per_page = $this->get_nb_rows_per_page();
        if ($nb_rows_per_page !== HTMLTableModel::NO_PAGINATION)
        {
            $last_page_number = ceil($this->nb_rows / $nb_rows_per_page);
            $this->page_number = max(1, min($this->parameters->get_page_number(), $last_page_number));
        }
        else
        {
            $this->page_number = 1;
        }
    }

    /**
     * @param $nb_rows
     * @param HTMLTableRow[] $rows
     */
    public function set_rows($nb_rows, array $rows)
    {
        $this->nb_rows = $nb_rows;
        $this->extract_parameters();
        $this->rows = $rows;
    }

    public function set_filters_fieldset_class_HTML()
    {
        $this->filters_fieldset_class = 'FormFieldsetHTML';
    }

    public function set_filters_fieldset_class_vertical()
    {
        $this->filters_fieldset_class = 'FormFieldsetVertical';
    }

    private function generate_filters_form()
    {
        $filters = $this->model->get_filters();
        $has_filters = !empty($filters);
        if ($filters)
        {
            $form = new HTMLForm('filters_form_' . $this->arg_id, '#', false);
            $filters_fieldset_class = $this->filters_fieldset_class;
            $fieldset = new $filters_fieldset_class('filters');
            $fieldset->set_description(LangLoader::get_message('filters', 'common'));
            $form->add_fieldset($fieldset);

            foreach ($filters as $filter)
            {
                $fieldset->add_field($filter->get_form_field());
                $this->tpl->assign_block_vars('filterElt', array(
                    'FORM_ID' => $filter->get_form_field()->get_html_id(),
                    'TABLE_ID' => $filter->get_id()
                ));
            }

            $submit_function = str_replace('-', '_', 'submit_filters_' . $this->arg_id);
            $form->add_button(new FormButtonButton(LangLoader::get_message('apply', 'common'), 'return ' . $submit_function . '()', 'submit'));

            $this->tpl->put_all(array(
                'C_FILTERS' => $has_filters,
                'SUBMIT_FUNCTION' => $submit_function,
                'SUBMIT_URL' => $this->parameters->get_js_submit_url(),
                'filters' => $form->display()
            ));
        }
    }

    private function generate_table_structure()
    {
        $has_nb_rows_options = $this->model->has_nb_rows_options();

        $this->tpl->put_all(array(
            'TABLE_ID' => $this->arg_id,
            'C_PAGINATION_ACTIVATED' => $this->is_pagination_activated(),
            'NUMBER_OF_COLUMNS' => count($this->columns),
            'C_CSS_CLASSES' => $this->has_css_class(),
            'CSS_CLASSES' => $this->get_css_class(),
            'C_CSS_STYLE' => $this->has_css_style(),
            'CSS_STYLE' => $this->get_css_style(),
            'C_ID' => $this->model->has_id(),
            'ID' => $this->model->get_id(),
            'C_CAPTION' => $this->model->has_caption(),
            'CAPTION' => $this->model->get_caption(),
            'U_TABLE_DEFAULT_OPIONS' => $this->parameters->get_default_table_url(),
            'C_NB_ROWS_OPTIONS' => $has_nb_rows_options,
            'C_HAS_ROWS' => !empty($this->rows),
            'C_DISPLAY_FOOTER' => $this->model->is_footer_displayed() && !empty($this->rows),
            'C_FOOTER_CSS_CLASSES' => $this->model->has_footer_css_class(),
            'FOOTER_CSS_CLASSES' => $this->model->get_footer_css_class()
        ));

        if ($has_nb_rows_options)
        {
            $first_row_index = $this->get_first_row_index();
            $nb_rows_per_page = $this->get_nb_rows_per_page();
            $nb_rows_options = $this->model->get_nb_rows_options();

            foreach ($nb_rows_options as $value)
            {
                $this->tpl->assign_block_vars('nbItemsOption', array(
                    'URL' => $this->parameters->get_nb_items_per_page_url($value, $first_row_index),
                    'VALUE' => $value,
                    'C_SELECTED' => $value == $nb_rows_per_page
                ));
            }
        }
    }

    private function generate_headers()
    {
        $sorting_rules = $this->parameters->get_sorting_rule();
        $sorted = $sorting_rules->get_order_way() . $sorting_rules->get_sort_parameter();
        foreach ($this->model->get_columns() as $column)
        {
            $sortable_parameter = $column->get_sortable_parameter();
            $values = array(
                'NAME' => $column->get_value(),
                'C_SORTABLE' => $column->is_sortable(),
                'C_SORT_ASC_SELECTED' => $sorted == HTMLTableSortingRule::ASC . $sortable_parameter,
                'C_SORT_DESC_SELECTED' => $sorted == HTMLTableSortingRule::DESC . $sortable_parameter,
                'U_SORT_ASC' => $this->parameters->get_ascending_sort_url($sortable_parameter),
                'U_SORT_DESC' => $this->parameters->get_descending_sort_url($sortable_parameter)
            );
            $this->add_css_vars($column, $values);
            $this->tpl->assign_block_vars('header_column', $values);
        }
    }

    private function generate_rows_stats()
    {
        $this->generate_stats();

        if ($this->is_pagination_activated())
        {
            $this->generate_pagination();
        }
    }

    private function is_pagination_activated()
    {
        return $this->model->is_pagination_activated() && $this->get_nb_pages() > 1;
    }

    private function generate_rows()
    {
        $row_number = 0;
        $last_displayed_row = ($this->get_first_row_index() + $this->get_nb_rows_per_page());
        if ($this->is_pagination_activated() && !($this->model instanceof SQLHTMLTableModel))
        {
            foreach ($this->rows as $row)
            {
                $row_number++;
                if ($row_number >= $this->get_first_row_index() && $row_number < $last_displayed_row)
                {
                    $this->generate_row($row);
                }
                else if ($row_number >= $last_displayed_row)
                    break;
            }
        }
        else
        {
            foreach ($this->rows as $row)
            {
                $this->generate_row($row);
            }
        }
    }

    protected final function generate_row(HTMLTableRow $row)
    {
        $row_values = array();
        $this->add_css_vars($row, $row_values);
        $this->add_id_vars($row, $row_values);
        $this->tpl->assign_block_vars('row', $row_values);

        foreach ($row->get_cells() as $cell)
        {
            if ($cell instanceof HTMLTableRowCell)
                $this->generate_cell($cell);
        }
    }

    private function generate_cell(HTMLTableRowCell $cell)
    {
        $cell_values = array(
            'VALUE' => $cell->get_value(),
            'C_COLSPAN' => $cell->is_multi_column(),
            'COLSPAN' => $cell->get_colspan()
        );
        $this->add_css_vars($cell, $cell_values);
        $this->add_id_vars($cell, $cell_values);
        $this->tpl->assign_block_vars('row.cell', $cell_values);
    }

    private function add_css_vars(HTMLElement $element, array &$tpl_vars)
    {
        $tpl_vars['C_CSS_CLASSES'] = $element->has_css_class();
        $tpl_vars['CSS_CLASSES'] = $element->get_css_class();
        $tpl_vars['C_CSS_STYLE'] = $element->has_css_style();
        $tpl_vars['CSS_STYLE'] = $element->get_css_style();
    }

    private function add_id_vars(HTMLElement $element, array &$tpl_vars)
    {
        $tpl_vars['C_ID'] = $element->has_id();
        $tpl_vars['ID'] = $element->get_id();
    }

    private function generate_stats()
    {
        $end = $this->get_first_row_index() + $this->get_nb_rows_per_page();
        $elements = StringVars::replace_vars(LangLoader::get_message('table_footer_stats', 'common'), array(
            'start' => $this->get_first_row_index() + 1,
            'end' => $end > $this->nb_rows || $this->get_nb_rows_per_page() == HTMLTableModel::NO_PAGINATION ? $this->nb_rows : $end,
            'total' => $this->nb_rows
        ));
        $this->tpl->put_all(array(
            'NUMBER_OF_ELEMENTS' => $elements
        ));
    }

    private function generate_pagination()
    {
        $nb_pages = $this->get_nb_pages();
        $pagination = new Pagination($nb_pages, $this->page_number);
        $pagination->set_url_builder_callback(array($this->parameters, 'get_pagination_url'));
        $this->tpl->put('pagination', $pagination->export());
    }

    private function get_nb_pages()
    {
        return ceil($this->nb_rows / $this->get_nb_rows_per_page());
    }

    public function get_page_number()
    {
        return $this->page_number;
    }

    public function get_nb_rows_per_page()
    {
        $nb_rows_per_page = $this->parameters->get_nb_items_per_page();
        if ($nb_rows_per_page < 1)
        {
            $nb_rows_per_page = $this->model->get_nb_rows_per_page();
        }
        return $nb_rows_per_page;
    }

    public function get_first_row_index()
    {
        return ($this->page_number - 1) * $this->get_nb_rows_per_page();
    }
}
?>