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

Класс Col

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

Объект «Колонка таблицы»

Исходный код
class Col extends \Cms\Site\Base { … }

Свойства

$name

$Col->name
Исходный код
    var $name;

$title

$Col->title
Исходный код
    var $title;

$width

$Col->width
Исходный код
    var $width;

$align

$Col->align
Исходный код
    var $align;

$empty

$Col->empty
Исходный код
    var $empty;

$display

$Col->display
Исходный код
    var $display;

$get

$Col->get
Исходный код
    var $get;

$href

$Col->href
Исходный код
    var $href;

$panel

$Col->panel
Исходный код
    var $panel;

$color

$Col->color
Исходный код
    var $color;

$background

$Col->background
Исходный код
    var $background;

$class

$Col->class = array();
Исходный код
    var $class = array();

$style

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

$attrs

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

Методы

init()

$Col->init($name=null, $title=null, $data=array());
Исходный код
    function init($name = null, $title = null, $data = array()) {
        $this->name = $name;
        $this->title = $title;
        if (is_array($data)) foreach ($data as $k => $v) $this->$k = $v;
        if ($this->name === 'panel') {
            if (!isset($data['width'])) $this->width = 0;
            if (!isset($data['align'])) $this->align = 'right';
            if (!isset($data['class'])) $this->class = array('uk-padding-remove', 'uk-table-middle');
            if (!isset($data['display'])) $this->display = array($this, 'displayPanel');
        }
        if (!isset($this->display) && isset($this->type)) {
            $method = 'display' . preg_replace('~\\W~', '', $this->type);
            if (is_callable(array($this, $method))) $this->display = array($this, $method);
        }
        foreach (array( 'get', 'href', 'panel', 'color', 'background' ) as $k) if (is_string($code = $this->$k)) {
            $code = '$root = $Row->root(); $App = $Row->App();'
             . ' $Conf = $Row->Conf(); $Data = $Row->Data(); $Auth = $Row->Auth(); $Main = $Row->Main(); $Form = $Row->Form();'
             . ' $Request = $Main->Request(); $Display = $Main->Display(); $Storage = $Main->Storage();'
             . (strpos($code, 'return') === false ? ' return ' : '') . "\n"
             . $code
             . ';';
            $this->$k = create_function('$Row, $Col', $code);
        }
        foreach (array( 'display' ) as $k) if (is_string($code = $this->$k)) {
            $code = '$root = $Row->root(); $App = $Row->App();'
             . ' $Conf = $Row->Conf(); $Data = $Row->Data(); $Auth = $Row->Auth(); $Main = $Row->Main(); $Form = $Row->Form();'
             . ' $Request = $Main->Request(); $Display = $Main->Display(); $Storage = $Main->Storage();'
             . (strpos($code, 'return') === false ? ' return ' : '') . "\n"
             . $code
             . ';';
            $this->$k = create_function('$value, $Row, $Col', $code);
        }
    }

getAlign()

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

getWidth()

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

getClass()

$Col->getClass($Row=null);
Исходный код
    function getClass($Row = null) {
        $r = is_array($this->class) ? $this->class : array($this->class);
        if ($align = $this->getAlign()) if (!in_array($class = 'uk-text-' . $align, $r)) $r[] = $class;
        if ($this->nowrap) if (!in_array($class = 'uk-text-nowrap', $r)) $r[] = $class;
        return $r;
    }

getStyle()

$Col->getStyle($Row=null);
Исходный код
    function getStyle($Row = null) {
        $r = is_array($this->style) ? $this->style : array($this->style);
        if ($Row === null) {
            if ($width = $this->getWidth()) $r['width'] = strval($width) === strval(doubleval($width)) ? $width . 'px' : $width;
        }
        return $r;
    }

getAttrs()

$Col->getAttrs($Row=null);
Исходный код
    function getAttrs($Row = null) {
        $r = is_array($this->attrs) ? $this->attrs : array($this->attrs);

        return array_merge(array('data-name' => $this->name), $this->attrs);
    }

getPanel()

$Col->getPanel($Row=null);
Исходный код
    function getPanel($Row = null) {
        if (is_callable($function = $this->panel)) return call_user_func($function, $Row, $this);
        else return $Row->getPanel();
    }

canSort()

$Col->canSort();
Исходный код
    function canSort() {
        return $this->sort !== false;
    }

canFind()

$Col->canFind();
Исходный код
    function canFind() {
        return !empty($this->find);
    }

canStat()

$Col->canStat();
Исходный код
    function canStat() {
        return !empty($this->stat);
    }

get()

$Col->get($Row);
Исходный код
    function get($Row) {
        if (is_callable($function = $this->get)) return call_user_func($function, $Row, $this);
        return $Row->get($this->name);
    }

href()

$Col->href($Row);
Исходный код
    function href($Row) {
        if (empty($this->href)) return;
        if ($this->href === true) return $Row->href();
        if (is_callable($function = $this->href)) return call_user_func($function, $Row, $this);
    }

color()

$Col->color($Row);
Исходный код
    function color($Row) {
        if (empty($this->color)) return;
        if (is_callable($function = $this->color)) return call_user_func($function, $Row, $this);
    }

background()

$Col->background($Row);
Исходный код
    function background($Row) {
        if (!$this->background) return;
        if (is_callable($function = $this->background)) return call_user_func($function, $Row, $this);
    }

normalize()

$Col->normalize($find);
Исходный код
    function normalize($find) {
        if ($this->find === 'checkbox') {
            if ($find === 'Y') return 'Y';
            if ($find === 'N') return 'N';
            return $find ? 'Y' : 'N';
        } elseif ($this->find === 'date' || $this->find === 'datetime') {
            $Form = $this->Form();
            if (is_array($find)) {
                $r = array();
                if ($value = $Form->requestDate(strval($find[0]))) $r[0] = $value;
                if ($value = $Form->requestDate(strval($find[1]))) $r[1] = $value;
                return $r;
            } elseif (is_string($find) && strlen($find) > 20) {
                # Дата - Дата
                $r = array();
                if ($value = $Form->requestDate(trim(substr($find, 0, floor(strlen($find) / 2))))) $r[0] = $value;
                if ($value = $Form->requestDate(trim(substr($find, ceil(strlen($find) / 2))))) $r[1] = $value;
                return $r;
            } else {
                if ($value = $Form->requestDate(strval($find))) return $value;
                else return;
            }
        } elseif ($this->find === 'select') {
            return strval($find);
        } else {
            return mb_strtolower(strval($find));
        }
    }

match()

$Col->match($Row, $match);
Исходный код
    function match($Row, $match) {
        if ($this->find === 'checkbox') {
            $value = $this->get($Row);
            if ($match === 'Y') return !!$value;
            if ($match === 'N') return !$value;
            return true;
        } elseif ($this->find === 'date' || $this->find === 'datetime') {
            $value = substr($this->get($Row), 0, 10);
            if (is_array($match)) {
                if ($match[0] && $match[0] > $value) return false;
                if ($match[1] && $match[1] < $value) return false;
                return true;
            } elseif ($match) {
                if ($match !== $value) return false;
                return true;
            } else {
                return true;
            }
        } elseif ($this->find === 'multiple') {
            $value = mb_strtolower($this->get($Row));
            if (is_array($match)) {
                foreach ($match as $match) if (is_string($match))
                if (strpos($value, $match) !== false) return true;
            } else {
                if (strpos($value, $match) !== false) return true;
            }
        } elseif ($this->find === 'select') {
            $value = $this->get($Row);
            if (empty($match) || $match === '*') return true;
            if (is_string($value)) return strpos($value, $match) !== false;
            if (is_array($value)) return in_array($match, $value);
        } else {
            $value = mb_strtolower($this->get($Row));
            if (strpos($value, $match) !== false) return true;
        }
    }

cmp()

$Col->cmp($Row1, $Row2);
Исходный код
    function cmp($Row1, $Row2) {
        $value1 = $this->get($Row1);
        $value2 = $this->get($Row2);
        if ($value1 === $value2) return 0;
        $length1 = strlen($value1);
        $length2 = strlen($value2);
        if (!$length1 && !$length2) return 0;
        if (!$length1) return -1;
        if (!$length2) return 1;
        if ($value1 > $value2) return 1;
        if ($value1 < $value2) return -1;
        return 0;
    }

addStat()

$Col->addStat($Row, &$stat);
Исходный код
    function addStat($Row, &$stat) {
        if ($this->stat === null) return;
        $name = $this->name;
        $value = $this->get($Row);
        if ($this->stat === 'sum' || $this->stat === 'Y' || $this->stat === true) {
            $stat[$name] += $value;
        } elseif ($this->stat === 'avg') {
            $stat[$name . '_sum'] += $value;
            $stat[$name . '_count'] ++;
            $stat[$name] = $stat[$name . '_sum'] / $stat[$name . '_count'];
        } elseif ($this->stat === 'count') {
            $stat[$name] ++;
        } elseif ($this->stat === 'min') {
            if (!isset($stat[$name]) || $value < $stat[$name]) $stat[$name] = $value;
        } elseif ($this->stat === 'max') {
            if (!isset($stat[$name]) || $value > $stat[$name]) $stat[$name] = $value;
        } elseif ($this->stat === 'first') {
            if (!isset($stat[$name])) $stat[$name] = $value;
        } elseif ($this->stat === 'last') {
            $stat[$name] = $value;
        }
    }

displayDate()

$Col->displayDate($value);
Исходный код
    function displayDate($value) {
        if (intval($value)) return date('d.m.Y', strtotime($value));
    }

displayDateTime()

$Col->displayDateTime($value);
Исходный код
    function displayDateTime($value) {
        if (intval($value)) return date('d.m.Y H:i', strtotime($value));
    }

displayCheckbox()

$Col->displayCheckbox($value, $Row);
Исходный код
    function displayCheckbox($value, $Row) {
        if (!isset($value)) if (is_object($Row) && $Row->stat) return;
        if (strlen($value)) return $this->Main()->translate('Да');
        else return $this->Main()->translate('Нет');
    }

displayMoney()

$Col->displayMoney($value);
Исходный код
    function displayMoney($value) {
        if (strlen($value)) return number_format($value, 2, ',', '');
    }

displayPercent()

$Col->displayPercent($value);
Исходный код
    function displayPercent($value) {
        if (strlen($value)) return number_format($value * 100, (floor($value * 100) === ceil($value * 100) ? 0 : 2), ',', '') . '%';
    }

displayPercent100()

$Col->displayPercent100($value);
Исходный код
    function displayPercent100($value) {
        if (strlen($value)) return number_format($value, (floor($value) === ceil($value) ? 0 : 2), ',', '') . '%';
    }

displayNumber()

$Col->displayNumber($value);
Исходный код
    function displayNumber($value) {
        if (strlen($value)) return number_format($value, (floor($value) === ceil($value) ? 0 : 2), ',', '');
    }

displayPanel()

$Col->displayPanel($value=null, $Row=null);
Исходный код
    function displayPanel($value = null, $Row = null) {
        if ($panel = $this->getPanel($Row)) return $this->Main()->callTemplate('display', 'table_panel', $Row->Item, array( 'panel' => $panel ));
    }

displaySelect()

$Col->displaySelect($value);
Исходный код
    function displaySelect($value) {
        if (!is_array($this->options)) return htmlspecialchars($value);
        return '<select data-name="' . $this->name . '[]">' . $this->displayOptions($value) . '</select>';
    }

displayOptions()

$Col->displayOptions($value);
Исходный код
    function displayOptions($value) {
        $html = '';
        if (!isset($this->options[$value])) $html .= '<option value="' . htmlspecialchars($value) . '" selected="selected">' . (strlen($value) ? htmlspecialchars($value) : '&nbsp;') . '</option>';
        foreach ($this->options as $k => $v) $html .= '<option value="' . htmlspecialchars($k) . '"' . (strval($k) === strval($value) ? ' selected="selected"' : '') . '>' . htmlspecialchars($v) . '</option>';
        return $html;
    }

displayText()

$Col->displayText($value);
Исходный код
    function displayText($value) {
        return htmlspecialchars($value);
    }

displayHtml()

$Col->displayHtml($value);
Исходный код
    function displayHtml($value) {
        return $value;
    }

displayInputText()

$Col->displayInputText($value);
Исходный код
    function displayInputText($value) {
        return '<input type="text" data-name="' . $this->name . '" value="' . $this->displayText($value) . '"' . (isset($this->empty) ? ' placeholder="' . $this->empty . '"' : '') . '>';
    }

displayInputDate()

$Col->displayInputDate($value);
Исходный код
    function displayInputDate($value) {
        return '<input type="text" data-name="' . $this->name . '" value="' . $this->displayDate($value) . '"' . (isset($this->empty) ? ' placeholder="' . $this->empty . '"' : '') . '>';
    }

displayInputMoney()

$Col->displayInputMoney($value);
Исходный код
    function displayInputMoney($value) {
        return '<input type="text" data-name="' . $this->name . '" value="' . $this->displayMoney($value) . '"' . (isset($this->empty) ? ' placeholder="' . $this->empty . '"' : '') . '>';
    }

displayInputNumber()

$Col->displayInputNumber($value);
Исходный код
    function displayInputNumber($value) {
        return '<input type="text" data-name="' . $this->name . '" value="' . $this->displayNumber($value) . '"' . (isset($this->empty) ? ' placeholder="' . $this->empty . '"' : '') . '>';
    }

displayButton()

$Col->displayButton($value);
Исходный код
    function displayButton($value) {
        return '<button data-name="' . $this->name . '">' . htmlspecialchars($value) . '</button>';
    }

displayValue()

$Col->displayValue($Row);
Исходный код
    function displayValue($Row) {
        $value = $this->get($Row);
        if (is_callable($function = $this->display)) {
            $html = call_user_func($function, $value, $Row, $this);
            if (!strlen($html)) $html = $this->empty;
        } elseif (strlen($value)) {
            $html = $this->html ? $value : htmlspecialchars($value);
        } else {
            $html = isset($this->empty) ? $this->empty : '&nbsp;';
        }
        return $html;
    }

displayTitle()

$Col->displayTitle();
Исходный код
    function displayTitle() {
        if ($this->title[0] === '<') return $this->title;
        elseif (strlen($this->title)) return nl2br(htmlspecialchars($this->title));
        else return '&nbsp;';
    }

displayClass()

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

displayStyle()

$Col->displayStyle();
Исходный код
    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()

$Col->displayAttrs();
Исходный код
    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);
    }