From a8931d663b9565104546eb60500801820e23f419 Mon Sep 17 00:00:00 2001 From: Cameron Date: Mon, 3 Apr 2017 20:20:38 -0700 Subject: [PATCH] Admin-ui: Grid-listing (work-in-progress) --- e107_handlers/admin_ui.php | 68 ++++++- e107_handlers/form_handler.php | 313 ++++++++++++++++++++++++++------- 2 files changed, 313 insertions(+), 68 deletions(-) diff --git a/e107_handlers/admin_ui.php b/e107_handlers/admin_ui.php index bfc7fea18..c0f423dfc 100644 --- a/e107_handlers/admin_ui.php +++ b/e107_handlers/admin_ui.php @@ -2517,6 +2517,13 @@ class e_admin_controller_ui extends e_admin_controller * @var integer */ protected $perPage = 20; + + + /** + * Data for grid layout. + * @var array + */ + protected $grid = array(); /** * @var e_admin_model @@ -2784,7 +2791,13 @@ class e_admin_controller_ui extends e_admin_controller { return $this->perPage; } - + + public function getGrid() + { + return $this->grid; + } + + public function getFormQuery() { return $this->formQuery; @@ -2864,7 +2877,7 @@ class e_admin_controller_ui extends e_admin_controller return null; } - if($this->getAction() === 'list') + if($this->getAction() === 'list' || $this->getAction() === 'grid') { return $this->getListModel()->get($key); } @@ -5342,6 +5355,15 @@ class e_admin_ui extends e_admin_controller_ui $this->addTitle(); } + /** + * Grid action observer + */ + public function GridObserver() + { + $this->action = 'list'; + $this->ListObserver(); + } + /** * Filter response ajax page * @return string @@ -5565,6 +5587,21 @@ class e_admin_ui extends e_admin_controller_ui return $this->getUI()->getList(); } + /** + * Generic List action page + * @return string + */ + public function GridPage() + { + if($this->deleteConfirmScreen && !$this->getPosted('etrigger_delete_confirm') && $this->getPosted('delete_confirm_value')) + { + // 'edelete_confirm_data' set by single/batch delete trigger + return $this->getUI()->getConfirmDelete($this->getPosted('delete_confirm_value')); // User confirmation expected + } + + return $this->getUI()->getList(null,'grid'); + } + /** * List action observer * @return void @@ -5574,6 +5611,16 @@ class e_admin_ui extends e_admin_controller_ui $this->getTreeModel()->setParam('db_query', $this->_modifyListQry(false, false, 0, false, $this->listQry))->load(); } + + /** + * List action observer + * @return void + */ + public function GridAjaxObserver() + { + $this->ListAjaxObserver(); + } + /** * Generic List action page (Ajax) * @return string @@ -5583,6 +5630,12 @@ class e_admin_ui extends e_admin_controller_ui return $this->getUI()->getList(true); } + + public function GridAjaxPage() + { + return $this->getUI()->getList(true,'grid'); + } + /** * Generic Edit observer */ @@ -6279,6 +6332,8 @@ class e_admin_form_ui extends e_form + + /** * Create list view * Search for the following GET variables: @@ -6286,7 +6341,7 @@ class e_admin_form_ui extends e_form * * @return string */ - public function getList($ajax = false) + public function getList($ajax = false, $view='default') { $tp = e107::getParser(); $controller = $this->getController(); @@ -6386,13 +6441,16 @@ class e_admin_form_ui extends e_form 'fieldset_pre' => '', // markup to be added before opening fieldset element 'fieldset_post' => '', // markup to be added after closing fieldset element 'perPage' => $controller->getPerPage(), // if 0 - no next/prev navigation + 'grid' => $controller->getGrid(), 'from' => $controller->getQuery('from', 0), // current page, default 0 'field' => $controller->getQuery('field'), //current order field name, default - primary field 'asc' => $controller->getQuery('asc', 'desc'), //current 'order by' rule, default 'asc' ); - - + if($view === 'grid') + { + return $this->renderGridForm($options, $tree, $ajax); + } return $this->renderListForm($options, $tree, $ajax); } diff --git a/e107_handlers/form_handler.php b/e107_handlers/form_handler.php index 9a8a70ba2..f0f87a525 100644 --- a/e107_handlers/form_handler.php +++ b/e107_handlers/form_handler.php @@ -2911,6 +2911,13 @@ class e_form function submit_image($name, $value, $image, $title='', $options = array()) { $tp = e107::getParser(); + + if(!empty($options['icon'])) + { + $customIcon = $options['icon']; + unset($options['icon']); + } + $options = $this->format_options('submit_image', $name, $options); switch ($image) { @@ -2935,7 +2942,13 @@ class e_form $options['class'] = $options['class'] == 'action' ? 'btn btn-default action view' : $options['class']; break; } + $options['title'] = $title;//shorthand + + if(!empty($customIcon)) + { + $icon = $customIcon; + } return ""; @@ -4081,7 +4094,88 @@ class e_form } + private function renderOptions($parms, $value='', $id, $attributes) + { + $tp = e107::getParser(); + $cls = false; + $editIconDefault = deftrue('ADMIN_EDIT_ICON', $tp->toGlyph('fa-edit')); + $deleteIconDefault = deftrue('ADMIN_DELETE_ICON', $tp->toGlyph('fa-trash')); + + if($attributes['grid']) + { + $editIconDefault = $tp->toGlyph('fa-edit'); + $deleteIconDefault = $tp->toGlyph('fa-trash'); + } + + + $value = "
"; + + if(!empty($parms['sort']) && empty($attributes['grid']))//FIXME use a global variable such as $fieldpref + { + $mode = preg_replace('/[^\w]/', '', vartrue($_GET['mode'], '')); + $from = intval(vartrue($_GET['from'],0)); + $value .= "".ADMIN_SORT_ICON." "; + } + + + if(varset($parms['editClass'])) + { + $cls = (deftrue($parms['editClass'])) ? constant($parms['editClass']) : $parms['editClass']; + } + + if((false === $cls || check_class($cls)) && varset($parms['edit'],1) == 1) + { + + parse_str(str_replace('&', '&', e_QUERY), $query); //FIXME - FIX THIS + // keep other vars in tact + $query['action'] = 'edit'; + $query['id'] = $id; + + + if(!empty($parms['target']) && $parms['target']=='modal') + { + $eModal = " e-modal "; + $eModalCap = "data-modal-caption='#".$id."'"; + $query['iframe'] = 1; + } + else + { + $eModal = ""; + $eModalCap = ""; + } + + $query = http_build_query($query); + $value .= " + ".$editIconDefault.""; + } + + $delcls = !empty($attributes['noConfirm']) ? ' no-confirm' : ''; + if(varset($parms['deleteClass']) && varset($parms['delete'],1) == 1) + { + $cls = (deftrue($parms['deleteClass'])) ? constant($parms['deleteClass']) : $parms['deleteClass']; + + if(check_class($cls)) + { + $parms['class'] = 'action delete btn btn-default'.$delcls; + unset($parms['deleteClass']); + $parms['icon'] = $deleteIconDefault; + $value .= $this->submit_image('etrigger_delete['.$id.']', $id, 'delete', LAN_DELETE.' [ ID: '.$id.' ]', $parms); + } + } + else + { + $parms['class'] = 'action delete btn btn-default'.$delcls; + $parms['icon'] = $deleteIconDefault; + $value .= $this->submit_image('etrigger_delete['.$id.']', $id, 'delete', LAN_DELETE.' [ ID: '.$id.' ]', $parms); + } + + //$attributes['type'] = 'text'; + $value .= "
"; + + return $value; + + } /** * Render Field Value @@ -4092,6 +4186,8 @@ class e_form */ function renderValue($field, $value, $attributes, $id = 0) { + + if(!empty($attributes['multilan']) && is_array($value)) { $value = varset($value[e_LANGUAGE],''); @@ -4151,70 +4247,9 @@ class e_form if(!$value) { - - - $value = "
"; - - if(!empty($parms['sort']))//FIXME use a global variable such as $fieldpref - { - $mode = preg_replace('/[^\w]/', '', vartrue($_GET['mode'], '')); - $from = intval(vartrue($_GET['from'],0)); - $value .= "".ADMIN_SORT_ICON." "; - } - - $cls = false; - if(varset($parms['editClass'])) - { - $cls = (deftrue($parms['editClass'])) ? constant($parms['editClass']) : $parms['editClass']; - - } - if((false === $cls || check_class($cls)) && varset($parms['edit'],1) == 1) - { - - parse_str(str_replace('&', '&', e_QUERY), $query); //FIXME - FIX THIS - - // keep other vars in tact - $query['action'] = 'edit'; - $query['id'] = $id; - - - if(!empty($parms['target']) && $parms['target']=='modal') - { - $eModal = " e-modal "; - $eModalCap = "data-modal-caption='#".$id."'"; - $query['iframe'] = 1; - } - else - { - $eModal = ""; - $eModalCap = ""; - } - - $query = http_build_query($query); - - $value .= " - ".deftrue('ADMIN_EDIT_ICON', $tp->toGlyph('fa-edit')).""; - } - - $delcls = !empty($attributes['noConfirm']) ? ' no-confirm' : ''; - if(varset($parms['deleteClass']) && varset($parms['delete'],1) == 1) - { - $cls = (deftrue($parms['deleteClass'])) ? constant($parms['deleteClass']) : $parms['deleteClass']; - if(check_class($cls)) - { - $parms['class'] = 'action delete btn btn-default'.$delcls; - unset($parms['deleteClass']); - $value .= $this->submit_image('etrigger_delete['.$id.']', $id, 'delete', LAN_DELETE.' [ ID: '.$id.' ]', $parms); - } - } - else - { - $parms['class'] = 'action delete btn btn-default'.$delcls; - $value .= $this->submit_image('etrigger_delete['.$id.']', $id, 'delete', LAN_DELETE.' [ ID: '.$id.' ]', $parms); - } + $value = $this->renderOptions($parms, $value, $id, $attributes); } - //$attributes['type'] = 'text'; - $value .= "
"; + return $value; break; @@ -5756,6 +5791,7 @@ class e_form e107::setRegistry('core/adminUI/currentListModel', $model); $text .= $this->renderTableRow($fields, $current_fields, $model->getData(), $options['pid']); } + e107::setRegistry('core/adminUI/currentListModel', null); $text .= " @@ -5804,6 +5840,157 @@ class e_form return (vartrue($options['form_pre']).$text.vartrue($options['form_post'])); } + public function renderGridForm($form_options, $tree_models, $nocontainer = false) + { + $tp = e107::getParser(); + $text = ''; + + + // print_a($form_options); + + foreach ($form_options as $fid => $options) + { + $tree_model = $tree_models[$fid]; + $tree = $tree_model->getTree(); + $total = $tree_model->getTotal(); + + $amount = $options['perPage']; + $from = vartrue($options['from'], 0); + $field = vartrue($options['field'], $options['pid']); + $asc = strtoupper(vartrue($options['asc'], 'asc')); + $elid = $fid;//$options['id']; + $query = vartrue($options['query'],e_QUERY); // ? $options['query'] : ; + if(vartrue($_GET['action']) == 'list') + { + $query = e_QUERY; //XXX Quick fix for loss of pagination after 'delete'. + } + $url = (isset($options['url']) ? $tp->replaceConstants($options['url'], 'abs') : e_SELF); + $formurl = $url.($query ? '?'.$query : ''); + $fields = $options['fields']; + $current_fields = varset($options['fieldpref']) ? $options['fieldpref'] : array_keys($options['fields']); + $legend_class = vartrue($options['legend_class'], 'e-hideme'); + + + + $text .= " +
+
".$this->token()." + ".vartrue($options['fieldset_pre'])." +
+ ".$options['legend']." + ".vartrue($options['table_pre'])." +
+ "; + + + if(!$tree) + { + $text .= "
"; + $text .= "
".LAN_NO_RECORDS_FOUND."
"; // not prone to column-count issues. + } + else + { +/* +
+ +
*/ + + $template = '
'; + $template .= '{IMAGE}'; + $template .= (!empty($options['grid']['body'])) ? '
{BODY}
' : ''; + $template .= ' +
'; + + + + $cls = !empty($options['grid']['class']) ? $options['grid']['class'] : 'col-md-2'; + $height = !empty($options['grid']['height']) ? $options['grid']['height'] : '220px'; + $pid = $options['pid']; + $title = $options['grid']['title']; + $pic = $options['grid']['image']; + $body = $options['grid']['body']; + $fields['options']['grid'] = true; + + foreach($tree as $model) + { + e107::setRegistry('core/adminUI/currentListModel', $model); + + $data = $model->getData(); + + $id = $data[$pid]; + + $vars = array( + 'CHECKBOX' => $this->renderValue('checkboxes',$data['checkboxes'],$fields['checkboxes'],$id), + 'TITLE' => $this->renderValue($title,$data[$title],$fields[$title],$id), + 'IMAGE' => $this->renderValue($pic,$data[$pic],$fields[$pic],$id), + 'OPTIONS' => $this->renderValue('options',$data['options'], $fields['options'], $id) + ); + + if(!empty($options['grid']['body'])) + { + $vars['BODY'] = $this->renderValue($body,$data[$body],$fields[$body],$id); + } + + $text .= "
"; + + $text .= $tp->simpleParse($template,$vars); + + + // $text .= $this->renderTableRow($fields, $current_fields, $model->getData(), $options['pid']); + $text .= "
"; + + } + + e107::setRegistry('core/adminUI/currentListModel', null); + + $text .= "
+
"; + } + + + $text .= vartrue($options['table_post']); + + + if($tree && $amount) + { + // New nextprev SC parameters + $parms = 'total='.$total; + $parms .= '&amount='.$amount; + $parms .= '¤t='.$from; + if(ADMIN_AREA) + { + $parms .= '&tmpl_prefix=admin'; + } + + // NOTE - the whole url is double encoded - reason is to not break parms query string + // 'np_query' should be proper (urlencode'd) url query string + $url = rawurlencode($url.'?'.(varset($options['np_query']) ? str_replace(array('&', '&'), array('&', '&'), $options['np_query']).'&' : '').'from=[FROM]'); + $parms .= '&url='.$url; + //$parms = $total.",".$amount.",".$from.",".$url.'?'.($options['np_query'] ? $options['np_query'].'&' : '').'from=[FROM]'; + //$text .= $tp->parseTemplate("{NEXTPREV={$parms}}"); + $nextprev = $tp->parseTemplate("{NEXTPREV={$parms}}"); + if ($nextprev) + { + $text .= "
".$nextprev."
"; + } + } + + $text .= " + + ".vartrue($options['fieldset_post'])." + +
+ "; + } + if(!$nocontainer) + { + $text = '
'.$text.'
'; + } + return (vartrue($options['form_pre']).$text.vartrue($options['form_post'])); + } + /** * Generic DB Record Management Form. * TODO - lans