<?php
namespace App\Mvc\View\Utils\Types; class ListingColumns { protected $columns = []; public static function create() { return new self(); } public function append(\App\Mvc\View\Utils\Types\ListingColumn $column) { $this->columns[] = $column; return $this; } public function insert(\App\Mvc\View\Utils\Types\ListingColumn $column, $index) { $columns = array_merge( array_slice($this->columns, 0, $index), [$column], array_slice($this->columns, $index) ); $this->columns = $columns; return $this; } public function getAll($filters = []) { $ret = $this->columns; if ($filters) { $ret = []; foreach($this->columns as $column) { $matched = false; if (isset($filters['sortable'])) { if ($filters['sortable'] == $column->getSortable()) { $matched = true; } } if (isset($filters['visible'])) { if ($filters['visible'] == !$column->isInvisible()) { $matched = true; } } if ($matched) { $ret[] = $column; } } } return $ret; } public function getAllSortable() { return $this->getAll(['sortable' => true,]); } public function getAllVisible() { return $this->getAll(['visible' => true,]); } public function loadLabels($lang) { foreach($this->columns as $column){ if(!$column->getLabel()){ $column->loadLabel($lang); } } return $this; } public function loadSettings(Array $settings) { if ($settings) { $dynamicOptionsOrder = array_keys($settings); usort($this->columns, function ($a, $b) use($dynamicOptionsOrder) { return array_search($a->getId(), $dynamicOptionsOrder) - array_search($b->getId(), $dynamicOptionsOrder); }); foreach($this->columns as $column) { $id = $column->getId(); $col = $settings[$id]; if ($col) { if (isset($col['visible'])) { $column->setInvisible(!$col['visible']); } } } } return $this; } } 