From 7170a692d4d47a69164e384c17057318d293d8af Mon Sep 17 00:00:00 2001 From: Andrey Kutejko Date: Sun, 4 Jan 2015 01:02:02 +0200 Subject: [PATCH] separate component context and rendering --- ipf/admin/component.php | 70 ++++++++++++++-------------- ipf/admin/controllers/components.php | 16 +++++-- 2 files changed, 47 insertions(+), 39 deletions(-) diff --git a/ipf/admin/component.php b/ipf/admin/component.php index e9f8d58..48b47a5 100644 --- a/ipf/admin/component.php +++ b/ipf/admin/component.php @@ -47,16 +47,15 @@ abstract class IPF_Admin_Component return array(); } - protected function renderToResponse($template, $context, $request) + protected function fullContext($context) { - $context = array_merge( + return array_merge( array( 'component' => $this, 'app' => $this->app, ), - $this->context($request), + $this->context($this->request), $context); - return IPF_Admin_App::RenderToResponse($template, $context, $request); } protected function _setupEditForm($form) @@ -110,21 +109,26 @@ abstract class IPF_Admin_Component return $this->_getForm($obj, $data); } - protected function _getListTemplate() + public function listTemplate() { return 'admin/items.html'; } - protected function _getAddTemplate() + public function addTemplate() { return 'admin/change.html'; } - protected function _getChangeTemplate() + public function editTemplate() { return 'admin/change.html'; } + public function deleteTemplate() + { + return 'admin/delete.html'; + } + protected function extraMedia($form) { return array( @@ -139,11 +143,11 @@ abstract class IPF_Admin_Component } // Views Function - public function AddItem($request) + public function addItem() { $errors = false; - if ($request->method == 'POST') { - $form = $this->_getAddForm(null, $request->POST + $request->FILES); + if ($this->request->method == 'POST') { + $form = $this->_getAddForm(null, $this->request->POST + $this->request->FILES); $this->_setupAddForm($form); if ($form->isValid()) { @@ -151,7 +155,7 @@ abstract class IPF_Admin_Component IPF_Admin_Log::logObject($this, 'add', $object, $id); - $url = @$request->POST['ipf_referrer']; + $url = @$this->request->POST['ipf_referrer']; if (!$url) $url = IPF_HTTP_URL::urlForView(array('IPF_Admin_Controller', 'listItems'), array($this->app->slug(), $this->slug())); return new IPF_HTTP_Response_Redirect($url); @@ -164,7 +168,7 @@ abstract class IPF_Admin_Component $extraMedia = $this->extraMedia($form); - $context = array( + return $this->fullContext(array( 'mode'=>'add', 'page_title'=>$this->titleAdd(), 'classname'=>$this->verbose_name(), @@ -174,46 +178,44 @@ abstract class IPF_Admin_Component 'extra_css' => $extraMedia['css'], 'errors' => $errors, 'objecttools' => array(), - ); - return $this->renderToResponse($this->_getAddTemplate(), $context, $request); + )); } - public function DeleteItem($request, $id) + public function deleteItem($id) { $object = $this->getObjectByID($id); if (!$object) throw new IPF_HTTP_Error404; - if ($request->method == 'POST') { + if ($this->request->method == 'POST') { IPF_Admin_Log::logObject($this, 'delete', $object); $this->deleteObject($object); - $url = @$request->POST['ipf_referrer']; + $url = @$this->request->POST['ipf_referrer']; if (!$url) $url = IPF_HTTP_URL::urlForView(array('IPF_Admin_Controller', 'listItems'), array($this->app->slug(), $this->slug())); return new IPF_HTTP_Response_Redirect($url); } - $context = array( + return $this->fullContext(array( 'page_title' => $this->titleDelete(), 'classname' => $this->verbose_name(), 'object' => $object, 'object_id' => $id, - 'ipf_referrer' => @$request->GET['ipf_referrer'], - ); - return $this->renderToResponse('admin/delete.html', $context, $request); + 'ipf_referrer' => @$this->request->GET['ipf_referrer'], + )); } - public function EditItem($request, $id) + public function editItem($id) { $object = $this->getObjectByID($id); if (!$object) throw new IPF_HTTP_Error404; $errors = false; - if ($request->method == 'POST') { - $form = $this->_getEditForm($object, $request->POST + $request->FILES); + if ($this->request->method == 'POST') { + $form = $this->_getEditForm($object, $this->request->POST + $this->request->FILES); $this->_setupEditForm($form); if ($form->isValid()) { @@ -221,7 +223,7 @@ abstract class IPF_Admin_Component IPF_Admin_Log::logObject($this, 'change', $object, $id); - $url = @$request->POST['ipf_referrer']; + $url = @$this->request->POST['ipf_referrer']; if (!$url) $url = IPF_HTTP_URL::urlForView(array('IPF_Admin_Controller', 'listItems'), array($this->app->slug(), $this->slug())); @@ -236,7 +238,7 @@ abstract class IPF_Admin_Component $objecttools = $this->objectTools($object); $extraMedia = $this->extraMedia($form); - $context = array( + return $this->fullContext(array( 'mode'=>'change', 'page_title'=>$this->titleEdit(), 'classname'=>$this->verbose_name(), @@ -248,19 +250,18 @@ abstract class IPF_Admin_Component 'extra_css' => $extraMedia['css'], 'errors' => $errors, 'objecttools' => $objecttools, - ); - return $this->renderToResponse($this->_getChangeTemplate(), $context, $request); + )); } - public function ListItems($request) + public function listItems() { - $searchValue = @$request->GET['q']; + $searchValue = @$this->request->GET['q']; $filters = $this->listFilters(); foreach ($filters as $f) - $f->setParams($request->GET); + $f->setParams($this->request->GET); - $currentPage = (int)@$request->GET['page']; + $currentPage = (int)@$this->request->GET['page']; if (!$currentPage) $currentPage = 1; @@ -312,7 +313,7 @@ abstract class IPF_Admin_Component $pagerLayout = new IPF_Pager_Layout; $pages = $pagerLayout->layout($currentPage, ceil($count / $this->perPage)); - $context = array( + return $this->fullContext(array( 'orderable'=>$this->_orderable(), 'page_title'=>$this->titleList(), 'header' => $header, @@ -325,8 +326,7 @@ abstract class IPF_Admin_Component 'filters' => $filters, 'is_search' => $this->searcheable(), 'search_value' => $searchValue, - ); - return $this->renderToResponse($this->_getListTemplate(), $context, $request); + )); } public function listFilters() diff --git a/ipf/admin/controllers/components.php b/ipf/admin/controllers/components.php index ff705a9..34a0299 100644 --- a/ipf/admin/controllers/components.php +++ b/ipf/admin/controllers/components.php @@ -22,22 +22,30 @@ class IPF_Admin_Controller extends IPF_Controller function listItems() { - return $this->getComponent(array('view'))->ListItems($this->request); + $component = $this->getComponent(array('view')); + $context = $component->listItems(); + return IPF_Admin_App::RenderToResponse($component->listTemplate(), $context, $this->request); } function addItem() { - return $this->getComponent(array('view', 'add'))->AddItem($this->request); + $component = $this->getComponent(array('view', 'add')); + $context = $component->addItem(); + return IPF_Admin_App::RenderToResponse($component->addTemplate(), $context, $this->request); } function editItem() { - return $this->getComponent(array('view', 'change'))->EditItem($this->request, $this->params[3]); + $component = $this->getComponent(array('view', 'change')); + $context = $component->editItem($this->params[3]); + return IPF_Admin_App::RenderToResponse($component->editTemplate(), $context, $this->request); } function deleteItem() { - return $this->getComponent(array('view', 'delete'))->DeleteItem($this->request, $this->params[3]); + $component = $this->getComponent(array('view', 'delete')); + $context = $component->deleteItem($this->params[3]); + return IPF_Admin_App::RenderToResponse($component->deleteTemplate(), $context, $this->request); } function reorder() -- 2.49.0