]> git.andy128k.dev Git - ipf.git/commitdiff
separate component context and rendering
authorAndrey Kutejko <andy128k@gmail.com>
Sat, 3 Jan 2015 23:02:02 +0000 (01:02 +0200)
committerAndrey Kutejko <andy128k@gmail.com>
Sat, 3 Jan 2015 23:02:02 +0000 (01:02 +0200)
ipf/admin/component.php
ipf/admin/controllers/components.php

index e9f8d58a78a43611fd3fca7c0f0c39575af31805..48b47a58e55776ec41e2ec9f79046a463703d73f 100644 (file)
@@ -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()
index ff705a95eaa7af7e8a9be191cfcc6bf5f77ef0f9..34a0299a0b8ac6fc88055dc503cb78e9e02227d5 100644 (file)
@@ -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()