Classes

File builder/table/HTMLTableModel.class.php

File builder/table/HTMLTableModel.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: 
<?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: 2017 04 29
 * @since       PHPBoost 3.0 - 2010 02 25
 * @contributor Julien BRISWALTER <j1.seth@phpboost.com>
*/

class HTMLTableModel
{
    const NO_PAGINATION = 0;
    const DEFAULT_PAGINATION = 25;

    /**
     * @var HTMLTable
     */
    public $html_table;

    private $id;
    private $caption = '';
    private $rows_per_page;
    private $nb_rows_options = array(10, 25, 100);
    private $default_sorting_rule;
    private $allowed_sort_parameters = array();
    private $filters = array();
    private $permanent_filters = array();
    private $display_footer = true;
    private $footer_css_class = '';

    /**
     * @var HTMLTableColumn[]
     */
    private $columns;

    public function __construct($id, array $columns, HTMLTableSortingRule $default_sorting_rule, $rows_per_page = self::DEFAULT_PAGINATION)
    {
        foreach ($columns as $column)
        {
            if ($column instanceof HTMLTableColumn)
                $this->add_column($column);
        }

        $default_sorting_rule->set_is_default_sorting(true);
        $this->default_sorting_rule = $default_sorting_rule;
        $this->rows_per_page = $rows_per_page;
        $this->set_nb_rows_options($this->nb_rows_options);
        $this->id = $id;
    }

    public function set_html_table(HTMLTable $html_table)
    {
        $this->html_table = $html_table;
    }

    /**
     * {@inheritdoc}
     */
    public function has_id()
    {
        return !empty($this->id);
    }

    /**
     * {@inheritdoc}
     */
    public function get_id()
    {
        return $this->id;
    }

    /**
     * {@inheritdoc}
     */
    public function has_caption()
    {
        return !empty($this->caption);
    }

    /**
     * {@inheritdoc}
     */
    public function get_caption()
    {
        return $this->caption;
    }

    /**
     * {@inheritdoc}
     */
    public function is_footer_displayed()
    {
        return $this->display_footer;
    }

    /**
     * {@inheritdoc}
     */
    public function is_pagination_activated()
    {
        return $this->rows_per_page > self::NO_PAGINATION;
    }

    /**
     * {@inheritdoc}
     */
    public function get_nb_rows_per_page()
    {
        return $this->rows_per_page;
    }

    /**
     * {@inheritdoc}
     */
    public function has_nb_rows_options()
    {
        return !empty($this->nb_rows_options);
    }

    /**
     * {@inheritdoc}
     */
    public function get_nb_rows_options()
    {
        return $this->nb_rows_options;
    }

    /**
     * {@inheritdoc}
     */
    public function get_columns()
    {
        return $this->columns;
    }

    /**
     * {@inheritdoc}
     */
    public function default_sort_rule()
    {
        return $this->default_sorting_rule;
    }

    /**
     * {@inheritdoc}
     */
    public function is_sort_parameter_allowed($parameter)
    {
        return in_array($parameter, $this->allowed_sort_parameters);
    }

    /**
     * {@inheritdoc}
     */
    public function is_filter_allowed($id, $value)
    {
        if (array_key_exists($id, $this->filters))
        {
            return $this->filters[$id]->is_value_allowed($value);
        }
    }

    /**
     * {@inheritdoc}
     */
    public function get_filter($id)
    {
        return $this->filters[$id];
    }

    /**
     * {@inheritdoc}
     */
    public function get_filters()
    {
        return $this->filters;
    }

    /**
     * {@inheritdoc}
     */
    public function get_permanent_filter($id)
    {
        return $this->permanent_filters[$id];
    }

    /**
     * {@inheritdoc}
     */
    public function get_permanent_filters()
    {
        return $this->permanent_filters;
    }

    public function set_id($id)
    {
        $this->id = $id;
    }

    public function set_caption($caption)
    {
        $this->caption = $caption;
    }

    public function hide_footer()
    {
        $this->display_footer = false;
    }

    public function has_footer_css_class()
    {
        return !empty($this->footer_css_class);
    }

    public function get_footer_css_class()
    {
        return $this->footer_css_class;
    }

    public function set_footer_css_class($class)
    {
        $this->footer_css_class = $class;
    }

    public function add_footer_css_class($class)
    {
        $this->footer_css_class .= ' ' . $class;
    }

    public function set_nb_rows_options(array $nb_rows_options)
    {
        if ($this->is_pagination_activated())
        {
            if (!empty($nb_rows_options))
            {
                $nb_rows_options[] = $this->rows_per_page;
                $nb_rows_options = array_unique($nb_rows_options);
                sort($nb_rows_options);
            }
            $this->nb_rows_options = $nb_rows_options;
        }
    }

    public function add_filter(HTMLTableFilter $filter)
    {
        $this->filters[$filter->get_id()] = $filter;
    }

    public function add_permanent_filter($filter)
    {
        $this->permanent_filters[] = $filter;
    }

    private function add_column(HTMLTableColumn $column)
    {
        $this->columns[] = $column;
        if ($column->is_sortable())
        {
            $this->allowed_sort_parameters[] = $column->get_sortable_parameter();
        }
    }

    public function delete_last_column()
    {
        array_pop($this->columns);
    }
}
?>