Система управления «Сайт PRO»
Версия 20240107

Класс Table

Объект $Table: Cms\Root\Table наследует Cms\Root\Base

Объект «Таблица»

Исходный код
class Table extends Base { … }

Свойства

$cols

$Table->cols = array();

Список колонок таблицы

Исходный код
    var $cols = array();

$rows

$Table->rows = array();

Список строк таблицы

Исходный код
    var $rows = array();

$request

$Table->request = array();

Пользовательские параметры фильтрации и сортировки

Исходный код
    var $request = array();

$item

$Table->item = true;

Строки таблицы соответствуют объектам системы управления

Исходный код
    var $item = true;

$sort

$Table->sort = null;

Поле сортировки по умолчанию

Исходный код
    var $sort = null;

$desc

$Table->desc = null;

Обратная сортировка по умолчанию

Исходный код
    var $desc = null;

$find

$Table->find = array();

Фильтр строк по умолчанию

Исходный код
    var $find = array();

$search

$Table->search = null;

Поиск строк по умолчанию

Исходный код
    var $search = null;

$stat

$Table->stat = array();

Статистика

Исходный код
    var $stat = array();

$panel

$Table->panel = array();

Панель таблицы

Исходный код
    var $panel = array();

$class

$Table->class = array('uk-table', 'uk-table-small', 'uk-table-divider', 'uk-table-responsive', 'uk-table-hover');

CSS-классы таблицы

Исходный код
    var $class = array('uk-table', 'uk-table-small', 'uk-table-divider', 'uk-table-responsive', 'uk-table-hover');

$style

$Table->style = array();

CSS-стили таблицы

Исходный код
    var $style = array();

$attrs

$Table->attrs = array();

HTML-атрибуты таблицы

Исходный код
    var $attrs = array();

Методы

init()

$Table->init($name=null, $data=array(), $request=null);

Создание таблицы

Параметры:

ИмяОписание
$name

имя таблицы

$data

свойства таблицы

$request

пользовательские параметры фильтрации и сортировки

Возвращает: null

Исходный код
    function init($name = null, $data = array(), $request = null) {
        $this->name = $name;
        if (is_array($data)) foreach ($data as $k => $v) $this->$k = $v;
        if ($_SESSION) {
            if (is_array($_request = $_SESSION['table'][$name])) $this->request = $_request;
        }
        if (is_array($request)) {
            foreach ($request as $k => $v) $this->request[$k] = $v;
        } else {
            if (isset($_REQUEST['sort']) && is_string($_REQUEST['sort'])) $this->request['sort'] = $_REQUEST['sort'];
            if (isset($_REQUEST['desc']) && is_string($_REQUEST['desc'])) $this->request['desc'] = $_REQUEST['desc'];
            if (isset($_REQUEST['find']) && is_array($_REQUEST['find'])) $this->request['find'] = $_REQUEST['find'];
            if (isset($_REQUEST['s']) && is_string($_REQUEST['s'])) $this->request['s'] = $_REQUEST['s'];
        }
    }

getCols()

$Table->getCols();

Список колонок

Возвращает: array

Исходный код
    function getCols() {
        return $this->cols;
    }

getRows()

$Table->getRows();

Список строк

Возвращает: array

Исходный код
    function getRows() {
        return $this->rows;
    }

addCol()

$Table->addCol($name, $title='', $data=array());

Добавление колонки

Параметры:

ИмяОписание
$name

имя колонки

$title

заголовок колонки

$data

свойства колонки

Возвращает: Cms\Root\Table\Col

Исходный код
    function addCol($name, $title = '', $data = array()) {
        $Main = $this->Main();
        $Col = $Main->create('table_col');
        $Col->init($name, $title, $data);
        return $this->cols[$name] = $Col;
    }

addRow()

$Table->addRow($row);

Добавление строки

Параметры:

ИмяОписание
$row

данные строки

Возвращает: Cms\Root\Table\Row

Исходный код
    function addRow($row) {
        $Main = $this->Main();
        $Row = $Main->create('table_row');
        if ($row) $Row->init($row, $this->item === false ? false : true);
        return $this->rows[] = $Row;
    }

addRows()

$Table->addRows($rows);

Добавление строк

Параметры:

ИмяОписание
$rows

запрос, результат запроса или список строк

Возвращает: null

Исходный код
    function addRows($rows) {
        $Data = $this->Data();
        if (is_string($rows)) $rows = $Data->query($rows);
        elseif (is_object($rows)) $rows = $Data->query("$rows");
        while ($row = $Data->fetch($rows)) $this->addRow($row);
    }

clear()

$Table->clear();

Удаление строк

Возвращает: null

Исходный код
    function clear() {
        $this->rows = array();
    }

isEmpty()

$Table->isEmpty();

Есть ли строки в таблице

Возвращает: bool

Исходный код
    function isEmpty() {
        return !count($this->rows);
    }

getClass()

$Table->getClass();

CSS-классы

Возвращает: array

Исходный код
    function getClass() {
        return is_array($this->class) ? $this->class : array($this->class);
    }

getStyle()

$Table->getStyle();

CSS-стили

Возвращает: array

Исходный код
    function getStyle() {
        return is_array($this->style) ? $this->style : array($this->style);
    }

getAttrs()

$Table->getAttrs();

HTML-атрибуты

Возвращает: array

Исходный код
    function getAttrs() {
        return array_merge(array('data-name' => $this->name), is_array($this->attrs) ? $this->attrs : array($this->attrs));
    }

getSort()

$Table->getSort();

Поле сортировки

Возвращает: string; null

Исходный код
    function getSort() {
        return !empty($this->request['sort']) ? $this->request['sort'] : $this->sort;
    }

getDesc()

$Table->getDesc();

Обратная сортировка

Возвращает: bool; null

Исходный код
    function getDesc() {
        return !empty($this->request['sort']) ? $this->request['desc'] : $this->desc;
    }

getFind()

$Table->getFind($name);

Фильтр строк

Параметры:

ИмяОписание
$name

имя колонки

Возвращает: string; null

Исходный код
    function getFind($name) {
        if (isset($this->request['find'][$name])) return $this->request['find'][$name];
        return $this->find[$name];
    }

getSearch()

$Table->getSearch();

Поиск по строкам

Возвращает: string; null

Исходный код
    function getSearch() {
        return strlen($this->request['s']) ? $this->request['s'] : $this->search;
    }

getPanel()

$Table->getPanel();

Панель

Возвращает: array

Исходный код
    function getPanel() {
        return $this->panel;
    }

sort()

$Table->sort();

Запуск сортировки строк

Возвращает: null

Исходный код
    function sort() {
        $sort = $this->getSort();
        if (empty($sort)) return;
        $desc = $this->getDesc();
        if (!$this->sortCol = $this->cols[$sort]) return;
        usort($this->rows, array( $this, $desc ? 'cmpDesc' : 'cmpAsc' ));
    }

cmpAsc()

$Table->cmpAsc($Row1, $Row2);

Сравнение строк для сортировки

Параметры:

ИмяОписание
$Row1

Cms\Root\Table\Row

$Row2

Cms\Root\Table\Row

Возвращает: 0; 1; -1

Исходный код
    function cmpAsc($Row1, $Row2) {
        return $this->sortCol->cmp($Row1, $Row2);
    }

cmpDesc()

$Table->cmpDesc($Row1, $Row2);

Сравнение строк для сортировки в обратном порядке

Параметры:

ИмяОписание
$Row1

Cms\Root\Table\Row

$Row2

Cms\Root\Table\Row

Возвращает: 0; 1; -1

Исходный код
    function cmpDesc($Row1, $Row2) {
        return $this->sortCol->cmp($Row2, $Row1);
    }

find()

$Table->find();

Запуск фильтрации строк и поиска

Возвращает: null

Исходный код
    function find() {
        if (!$this->find && !$this->request['find'] && !strlen($search = $this->getSearch())) return;
        $findCols = array();
        $findList = array();
        $searchCols = array();
        foreach ($this->cols as $name => $Col) if ($Col->canFind()) {
            $find = $this->getFind($name);
            if (is_array($find) || strlen($find)) {
                $find = $Col->normalize($find);
                $findCols[$name] = $Col;
                $findList[$name] = $find;
            }
            if (strlen($search)) {
                $searchCols[$name] = $Col;
            }
        }
        if (empty($findCols) && empty($searchCols)) return;
        $rows = array();
        foreach ($this->rows as $Row) {
            if (!empty($findCols)) {
                $match = true;
                foreach ($findCols as $name => $Col) if (!$Col->match($Row, $findList[$name])) {
                    $match = false;
                    break;
                }
                if (!$match) continue;
            }
            if (!empty($searchCols)) {
                $match = false;
                foreach ($searchCols as $name => $Col) if ($Col->match($Row, $search)) {
                    $match = true;
                    break;
                }
                if (!$match) continue;
            }
            $rows[] = $Row;
        }
        $this->rows = $rows;
    }

stat()

$Table->stat();

Запуск сбора статистики

Возвращает: null

Исходный код
    function stat() {
        $this->stat = array();
        foreach ($this->cols as $name => $Col) if ($Col->canStat()) {
            foreach ($this->rows as $Row) $Col->addStat($Row, $this->stat);
        }
    }

display()

$Table->display();

Запуск фильтрации, сортировки, сбора статистики и отображение таблицы

Возвращает: string

Исходный код
    function display() {
        $this->find();
        $this->sort();
        $this->stat();
        return $this->displayDetail();
    }

displayDetail()

$Table->displayDetail();

Отображение таблицы

Возвращает: string

Исходный код
    function displayDetail() {
        if ($_REQUEST['excel']) {
            return $this->Main()->callTemplate('table', 'excel', $this);
        } else {
            if ($this->pager !== false) $pager = $this->displayPager();
            return $this->Main()->callTemplate('detail', 'table', $this, array( 'pager' => $pager ));
        }
    }

displayTitle()

$Table->displayTitle();

Отображение заголовка таблицы

Возвращает: string

Исходный код
    function displayTitle() {
        return htmlspecialchars($this->title);
    }

displayClass()

$Table->displayClass();

Отображение CSS-классов

Возвращает: string

Исходный код
    function displayClass() {
        $html = $this->getClass();
        if (!empty($html)) return ' class="' . htmlspecialchars(implode(' ', $html)) . '"';
    }

displayStyle()

$Table->displayStyle();

Отображение CSS-стилей

Возвращает: string

Исходный код
    function displayStyle() {
        $html = array();
        foreach ($this->getStyle() as $k => $v) if (isset($v)) $html[] = (is_int($k) ? '' : $k . ':') . $v . ';';
        if (!empty($html)) return ' style="' . htmlspecialchars(implode('', $html)) . '"';
    }

displayAttrs()

$Table->displayAttrs();

Отображение HTML-атрибутов

Возвращает: string

Исходный код
    function displayAttrs() {
        $html = array();
        foreach ($this->getAttrs() as $k => $v) if (isset($v)) $html[] = is_int($k) ? htmlspecialchars($v) : htmlspecialchars($k) . '="' . htmlspecialchars($v) . '"';
        if (!empty($html)) return ' ' . implode(' ', $html);
    }

displayPager()

$Table->displayPager();

Отображение страниц

Возвращает: string; null

Исходный код
    function displayPager() {
        $limit = strlen($this->pager) && intval($this->pager) && $this->pager !== true ? intval($this->pager) : 100;
        return $this->Main()->displayPager($this->rows, $limit);
    }