From: Andrey Kutejko Date: Sun, 22 Apr 2018 19:58:45 +0000 (+0200) Subject: update filters X-Git-Tag: 0.6~4 X-Git-Url: https://git.andy128k.dev/?a=commitdiff_plain;h=bc9eb1a1a931660666860a6384b41fbe92baaff8;p=ipf-legacy-orm.git update filters --- diff --git a/src/adminmodel.php b/src/adminmodel.php index f656bb7..e8fcd26 100644 --- a/src/adminmodel.php +++ b/src/adminmodel.php @@ -383,16 +383,9 @@ abstract class IPF_Admin_Model_Filter implements IPF_Admin_ListFilter return $this->title; } - protected function link($params, $value, $title) + function paramName() { - if ($value !== null) - $params[$this->paramName] = $value; - else - unset($params[$this->paramName]); - - return Tag::a() - ->attr('href', '?'.IPF_HTTP_URL::generateParams($params, false)) - ->append($title); + return $this->paramName; } } @@ -420,14 +413,16 @@ class IPF_Admin_Model_RelationFilter extends IPF_Admin_Model_Filter $q->addWhere($this->relation['local'].' = ?', array($this->selected)); } - function render($extraParams) + function getItems() { - $ul = Tag::ul(); + $items = []; // reset filter - $ul->append(Tag::li() - ->toggleClass('selected', !$this->selected) - ->append($this->link($extraParams, null, __('All')))); + $items[] = array( + 'id' => null, + 'label' => __('All'), + 'selected' => !$this->selected, + ); // query related $table = IPF_ORM::getTable($this->relation['class']); @@ -442,14 +437,14 @@ class IPF_Admin_Model_RelationFilter extends IPF_Admin_Model_Filter $foreign = $this->relation['foreign']; foreach ($query->execute() as $val) { $id = $val[$foreign]; - $name = (string)$val; - - $ul->append(Tag::li() - ->toggleClass('selected', $this->selected == $id) - ->append($this->link($extraParams, $id, $name))); + $items[] = array( + 'id' => $id, + 'label' => (string)$val, + 'selected' => $this->selected == $id, + ); } - return $ul->html(); + return $items; } } @@ -476,25 +471,27 @@ class IPF_Admin_Model_OwnedFilter extends IPF_Admin_Model_Filter $q->addWhere($this->column.' = ?', array($this->selected)); } - function render($extraParams) + function getItems() { - $ul = Tag::ul(); + $items = []; // reset filter - $ul->append(Tag::li() - ->toggleClass('selected', !$this->selected) - ->append($this->link($extraParams, null, __('All')))); + $items[] = array( + 'id' => null, + 'label' => __('All'), + 'selected' => !$this->selected, + ); foreach (\PFF\Container::auth()->userQuery()->fetchAll() as $val) { $id = $val->id; - $name = (string)$val; - - $ul->append(Tag::li() - ->toggleClass('selected', $this->selected == $id) - ->append($this->link($extraParams, $id, $name))); + $items[] = array( + 'id' => $id, + 'label' => (string)$val, + 'selected' => $this->selected == $id, + ); } - return $ul->html(); + return $items; } } @@ -517,18 +514,25 @@ class BooleanFilter extends IPF_Admin_Model_Filter $this->selected = \PFF\Arr::get($params, $this->paramName); } - function render($extraParams) + function getItems() { - return Tag::ul(null, - Tag::li() - ->toggleClass('selected', $this->selected !== 'y' && $this->selected !== 'n') - ->append($this->link($extraParams, null, __('All'))), - Tag::li() - ->toggleClass('selected', $this->selected === 'y') - ->append($this->link($extraParams, 'y', $this->trueTitle)), - Tag::li() - ->toggleClass('selected', $this->selected === 'n') - ->append($this->link($extraParams, 'n', $this->falseTitle))); + return [ + [ + 'id' => null, + 'label' => __('All'), + 'selected' => $this->selected !== 'y' && $this->selected !== 'n', + ], + [ + 'id' => 'y', + 'label' => $this->trueTitle, + 'selected' => $this->selected === 'y', + ], + [ + 'id' => 'n', + 'label' => $this->falseTitle, + 'selected' => $this->selected === 'n', + ], + ]; } function applyToQuery($q) @@ -569,22 +573,25 @@ class DateHierarchyListFilter extends IPF_Admin_Model_Filter } } - function render($extraParams) + function getItems() { - $ul = Tag::ul(); - - $ul->append(Tag::li() - ->toggleClass('selected', !$this->year) - ->append($this->link($extraParams, null, __('All')))); - - if ($this->year) - $ul->append($this->renderChoice($extraParams, $this->year, 0, 0, $this->year)->addClass('selected')); + $items = []; - if ($this->month) - $ul->append($this->renderChoice($extraParams, $this->year, $this->month, 0, $this->monthName($this->month))->addClass('selected')); + $items[] = array( + 'id' => null, + 'label' => __('All'), + 'selected' => !$this->year, + ); - if ($this->day) - $ul->append($this->renderChoice($extraParams, $this->year, $this->month, $this->day, $this->day)->addClass('selected')); + if ($this->year) { + $items[] = ['id' => sprintf('%04d-00-00', $this->year), 'label' => $this->year, 'selected' => true]; + } + if ($this->month) { + $items[] = ['id' => sprintf('%04d-%02d-00', $this->year, $this->month), 'label' => $this->monthName($this->month), 'selected' => true]; + } + if ($this->day) { + $items[] = ['id' => sprintf('%04d-%02d-%02d', $this->year, $this->month, $this->day), 'label' => $this->day, 'selected' => true]; + } if ($this->day) { } elseif ($this->month) { @@ -592,22 +599,22 @@ class DateHierarchyListFilter extends IPF_Admin_Model_Filter 'YEAR(' . $this->name . ')', $this->year, 'MONTH(' . $this->name . ')', $this->month); foreach ($days as $day) { - $ul->append($this->renderChoice($extraParams, $this->year, $this->month, $day, $day)); + $items[] = ['id' => sprintf('%04d-%02d-%02d', $this->year, $this->month, $day), 'label' => $day, 'selected' => false]; } } elseif ($this->year) { $months = $this->choices('MONTH(' . $this->name . ')', 'YEAR(' . $this->name . ')', $this->year); foreach ($months as $month) { - $ul->append($this->renderChoice($extraParams, $this->year, $month, 0, $this->monthName($month))); + $items[] = ['id' => sprintf('%04d-%02d-00', $this->year, $month), 'label' => $this->monthName($month), 'selected' => false]; } } else { $years = $this->choices('YEAR(' . $this->name . ')'); foreach ($years as $year) { - $ul->append($this->renderChoice($extraParams, $year, 0, 0, $year)); + $items[] = ['id' => sprintf('%04d-00-00', $year), 'label' => $year, 'selected' => false]; } } - return $ul->html(); + return $items; } function applyToQuery($q) @@ -640,15 +647,8 @@ class DateHierarchyListFilter extends IPF_Admin_Model_Filter return \PFF\Arr::pluck($q->fetchArray(), 'value'); } - private function renderChoice($extraParams, $year, $month, $day, $label) - { - return Tag::li(null, - $this->link($extraParams, sprintf('%04d-%02d-%02d', $year, $month, $day), $label)); - } - protected function monthName($month) { return date('F', mktime(0, 0, 0, $month, 1)); } } -