Skip to content

Commit 841cf85

Browse files
committed
added search capabilities on admin tables + fixes
1 parent 22e7533 commit 841cf85

File tree

8 files changed

+132
-37
lines changed

8 files changed

+132
-37
lines changed

app/base/abstracts/AdminFormPage.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@
2222
*/
2323
abstract class AdminFormPage extends AdminPage
2424
{
25-
/**
26-
* @var array template data
27-
*/
28-
protected $templateData = [];
29-
3025
/**
3126
* {@inheritdocs}
3227
*

app/base/abstracts/AdminManageModelsPage.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,19 @@ public function __construct(ContainerInterface $container)
3333
parent::__construct($container);
3434
if ($this->templateData['action'] == 'list') {
3535
$this->addNewButton();
36-
$data = $this->getContainer()->call([$this->getObjectClass(), 'paginate'], ['order' => $this->getRequest()->query->get('order')]);
36+
37+
$paginate_params = [
38+
'order' => $this->getRequest()->query->get('order'),
39+
'condition' => $this->getRequest()->query->get('search')
40+
];
41+
if (is_array($paginate_params['condition'])) {
42+
foreach ($paginate_params['condition'] as $col => $search) {
43+
$paginate_params['condition'][$col . ' LIKE ?'] = ['%'.$search.'%'];
44+
unset($paginate_params['condition'][$col]);
45+
}
46+
}
47+
48+
$data = $this->getContainer()->call([$this->getObjectClass(), 'paginate'], $paginate_params);
3749
$this->templateData += [
3850
'table' => $this->getHtmlRenderer()->renderAdminTable($this->getTableElements($data['items']), $this->getTableHeader(), $this),
3951
'total' => $data['total'],
@@ -43,16 +55,6 @@ public function __construct(ContainerInterface $container)
4355
}
4456
}
4557

46-
/**
47-
* {@inheritdocs}
48-
*
49-
* @return array
50-
*/
51-
protected function getTemplateData()
52-
{
53-
return $this->templateData;
54-
}
55-
5658
/**
5759
* gets model table html
5860
*

app/base/abstracts/AdminPage.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ abstract class AdminPage extends BaseHtmlPage
3333
*/
3434
protected $locale = null;
3535

36+
/**
37+
* @var array template data
38+
*/
39+
protected $templateData = [];
40+
3641
/**
3742
* {@inheritdocs}
3843
*
@@ -138,6 +143,16 @@ public function addBackButton()
138143
$this->addActionLink('back-btn', 'back-btn', $this->getUtils()->getIcon('rewind').' '.$this->getUtils()->translate('Back', $this->getCurrentLocale()), $this->getControllerUrl());
139144
}
140145

146+
/**
147+
* {@inheritdocs}
148+
*
149+
* @return array
150+
*/
151+
protected function getTemplateData()
152+
{
153+
return $this->templateData;
154+
}
155+
141156
/**
142157
* gets access permission name
143158
*

app/base/abstracts/Model.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ public static function select(ContainerInterface $container, $options = [])
115115
*/
116116
protected static function getModelBasicWhere(ContainerInterface $container, $condition = [], $order = [])
117117
{
118+
if ($condition == null) {
119+
$condition = [];
120+
}
118121
$stmt = $container->get('db')->table(
119122
static::defaultTableName()
120123
)->where($condition);
@@ -169,6 +172,10 @@ public static function paginate(ContainerInterface $container, Request $request
169172
$request = Request::createFromGlobals();
170173
}
171174

175+
if ($condition == null) {
176+
$condition = [];
177+
}
178+
172179
$page = $request->get('page') ?? 0;
173180
$start = (int)$page * $page_size;
174181
$items = array_map(
@@ -178,10 +185,7 @@ function ($el) use ($container) {
178185
static::getModelBasicWhere($container, $condition, $order)->limit($page_size, $start)->fetchAll()
179186
);
180187

181-
$total = $container->get('db')->table(
182-
static::defaultTableName()
183-
)->where($condition, $order)->count();
184-
188+
$total = static::getModelBasicWhere($container, $condition, $order)->count();
185189
return ['items' => $items, 'page' => $page, 'total' => $total];
186190
}
187191

app/base/tools/Utils/HtmlPartsRenderer.php

Lines changed: 73 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -475,11 +475,15 @@ public function renderPaginator($current_page, $total, BasePage $controller, $pa
475475
*/
476476
public function renderAdminTable($elements, $header = null, BasePage $current_page = null)
477477
{
478+
$table_id = 'listing-table';
479+
480+
478481
$table = $this->getContainer()->make(
479482
TagElement::class,
480483
['options' => [
481484
'tag' => 'table',
482485
'width' => '100%',
486+
'id' => $table_id,
483487
'cellspacing' => '0',
484488
'cellpadding' => '0',
485489
'border' => '0',
@@ -518,6 +522,42 @@ public function renderAdminTable($elements, $header = null, BasePage $current_pa
518522
$header = array_flip($header);
519523
}
520524

525+
$add_searchrow = false;
526+
if ($current_page instanceof BasePage) {
527+
$search_row = $this->getContainer()->make(
528+
TagElement::class,
529+
['options' => [
530+
'tag' => 'tr',
531+
]]
532+
);
533+
$style="max-width:80%;font-size: 9px;line-height: 11px;max-width: 100%;padding: 3px 1px;margin: 0;border: 1px solid #555;border-radius: 2px;";
534+
foreach ($header as $k => $v) {
535+
if (is_array($v) && isset($v['search']) && boolval($v['search']) == true) {
536+
$td = $this->getContainer()->make(
537+
TagElement::class,
538+
['options' => [
539+
'tag' => 'td',
540+
'text' => '<input style="'.$style.'" name="search['.$v['search'].']" value="'.$current_page->getRequest()->query->get('search')[$v['search']].'"/>',
541+
]]
542+
);
543+
$add_searchrow = true;
544+
} else {
545+
$td = $this->getContainer()->make(
546+
TagElement::class,
547+
['options' => [
548+
'tag' => 'td',
549+
'text' => '&nbsp;',
550+
]]
551+
);
552+
}
553+
554+
$search_row->addChild($td);
555+
}
556+
if ($add_searchrow) {
557+
$tbody->addChild($search_row);
558+
}
559+
}
560+
521561
// tbody
522562
foreach ($elements as $key => $elem) {
523563
// ensure all header cols are in row cols
@@ -571,19 +611,29 @@ public function renderAdminTable($elements, $header = null, BasePage $current_pa
571611
]]
572612
);
573613

574-
foreach ($header as $th => $column_name) {
614+
foreach ($header as $th => $column) {
575615
$th = $this->getUtils()->translate($th, $current_page->getCurrentLocale());
576616
$request_params = [];
577617
if ($current_page instanceof BasePage) {
578618
$request_params = $current_page->getRequest()->query->all();
579619

580-
if (!empty($column_name)) {
581-
$val = 'DESC';
582-
if (isset($request_params['order'][$column_name])) {
583-
$val = ($request_params['order'][$column_name] == 'ASC') ? 'DESC' : 'ASC';
620+
if (!empty($column)) {
621+
$orderby = null;
622+
if (is_array($column)) {
623+
if (isset($column['order'])) {
624+
$orderby = $column['order'];
625+
}
626+
} else {
627+
$orderby = $column;
628+
}
629+
if (!empty($orderby)) {
630+
$val = 'DESC';
631+
if (isset($request_params['order'][$orderby])) {
632+
$val = ($request_params['order'][$orderby] == 'ASC') ? 'DESC' : 'ASC';
633+
}
634+
$request_params['order'][$orderby] = $val;
635+
$th = '<a class="ordering" href="'.($current_page->getControllerUrl().'?'.http_build_query($request_params)).'">'.$th.$this->getUtils()->getIcon($val == 'DESC' ? 'arrow-down' : 'arrow-up').'</a>';
584636
}
585-
$request_params['order'][$column_name] = $val;
586-
$th = '<a class="ordering" href="'.($current_page->getControllerUrl().'?'.http_build_query($request_params)).'">'.$th.$this->getUtils()->getIcon($val == 'DESC' ? 'arrow-down' : 'arrow-up').'</a>';
587637
}
588638
}
589639

@@ -604,6 +654,22 @@ public function renderAdminTable($elements, $header = null, BasePage $current_pa
604654
}
605655
$thead->addChild($row);
606656

657+
if (($current_page instanceof BasePage)) {
658+
$request_params = $current_page->getRequest()->query->all();
659+
if (isset($request_params['order']) || isset($request_params['search'])) {
660+
$current_page->addActionLink('reset-btn', 'reset-btn', $this->getUtils()->translate('Reset', $current_page->getCurrentLocale()), $current_page->getControllerUrl(), 'btn btn-sm btn-warning');
661+
}
662+
if ($add_searchrow) {
663+
$query_params = '';
664+
if (!empty($request_params)) {
665+
$query_params = (array) $request_params;
666+
unset($query_params['search']);
667+
$query_params = http_build_query($query_params);
668+
}
669+
$current_page->addActionLink('search-btn', 'search-btn', $this->getUtils()->getIcon('zoom-in').$this->getUtils()->translate('Search', $current_page->getCurrentLocale()), $current_page->getControllerUrl() . (!empty($query_params) ? '?':'') . $query_params, 'btn btn-sm btn-primary', ['data-target' => '#'.$table_id]);
670+
}
671+
}
672+
607673
return $table;
608674
}
609675

app/base/traits/AdminTrait.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,17 +157,20 @@ protected function addActionButton($key, $button_id, $button_text, $button_class
157157
* @param string $link_href
158158
* @param string $link_class
159159
*/
160-
protected function addActionLink($key, $link_id, $link_text, $link_href = '#', $link_class = 'btn btn-sm btn-light')
160+
public function addActionLink($key, $link_id, $link_text, $link_href = '#', $link_class = 'btn btn-sm btn-light', $attributes = [])
161161
{
162+
if (!is_array($attributes)) {
163+
$attributes = [];
164+
}
162165
$button = (string)(new TagElement(
163166
[
164167
'tag' => 'a',
165168
'id' => $link_id,
166169
'attributes' => [
167170
'class' => $link_class,
168171
'href' => $link_href,
169-
'title' => $link_text,
170-
],
172+
'title' => strip_tags($link_text),
173+
] + $attributes,
171174
'text' => $link_text,
172175
]
173176
));

app/site/controllers/Admin/Blocks.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,8 @@ public function formValidate(FAPI\Form $form, &$form_state)
269269
public function formSubmitted(FAPI\Form $form, &$form_state)
270270
{
271271
/**
272-
* @var Block $block
273-
*/
272+
* @var Block $block
273+
*/
274274
$block = $this->newEmptyObject();
275275
if ($this->getRequest()->get('block_id')) {
276276
$block = $this->loadObject($this->getRequest()->get('block_id'));
@@ -343,11 +343,11 @@ function ($el) {
343343
protected function getTableHeader()
344344
{
345345
return [
346-
'ID' => 'id',
347-
'Website' => 'website_id',
348-
'Region' => 'region',
349-
'Locale' => 'locale',
350-
'Title' => 'title',
346+
'ID' => ['order' => 'id'],
347+
'Website' => ['order' => 'website_id'],
348+
'Region' => ['order' => 'region', 'search' => 'region'],
349+
'Locale' => ['order' => 'locale', 'search' => 'locale'],
350+
'Title' => ['order' => 'title', 'search' => 'title'],
351351
'Where' => null,
352352
'Order' => null,
353353
'actions' => null,

js/src/admin.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@
6666
});
6767

6868
$elem.appAdmin('checkLoggedStatus');
69+
70+
$('#search-btn').click(function(evt){
71+
evt.preventDefault();
72+
$elem.appAdmin('searchTableColumns', this);
73+
})
6974
});
7075
},
7176
showOverlay: function() {
@@ -172,6 +177,11 @@
172177
$(that).appAdmin('checkLoggedStatus');
173178
}, 60000);
174179
},
180+
searchTableColumns: function(btn) {
181+
var query = $('input', $(btn).data('target')).serialize();
182+
var href = $(btn).attr('href');
183+
document.location = href + (href.indexOf('?') != -1 ? '&' : '?') + query;
184+
},
175185
show : function( ) { },// IS
176186
hide : function( ) { },// GOOD
177187
update : function( content ) { }// !!!

0 commit comments

Comments
 (0)