Home > General > Creating Add/Update/Delete/View Action Controller

Creating Add/Update/Delete/View Action Controller

intro
This article shows simple add/update/delete/view Action Controller derived by Zend_Controller_Action.

1.Create Controller
Create the file library/My/Controller/Simple.php with the following content.

<?php
require_once 'Zend/Controller/Action.php';
require_once 'Zend/Form.php';
require_once 'Zend/Session/Namespace.php';
 
class My_Controller_Simple extends Zend_Controller_Action
{
    protected $_session_name = "my_controller_simple";
    protected $_table_class;
    protected $_form_class;
    protected $_events = array(
        'next', 'update', 'add', 'delete', 'back', 'cancel'
    );
    protected $_submit_decorators = array(
        'ViewHelper',
        array('HtmlTag', array('tag' => 'dt', 'class' => 'submit'))
    );
 
    public function listAction()
    {
        $table = $this->getTable();
        $this->view->list = $this->_executeList($table);
        return $this->render('list');
    }
 
    public function detailAction()
    {
        $this->_editable = false;
        return $this->_executeEventHandler();
    }
 
    public function addAction()
    {
        $this->_editable = true;
        return $this->_executeEventHandler();
    }
 
    public function updateAction()
    {
        $this->_editable = true;
        return $this->_executeEventHandler();
    }
 
    public function deleteAction()
    {
        $this->_editable = false;
        return $this->_executeEventHandler();
    }
 
    public function getForm()
    {
        return new $this->_form_class();
    }
 
    public function getTable()
    {
        return new $this->_table_class();
    }
 
    public function getSession()
    {
        return new Zend_Session_Namespace($this->_session_name);
    }
 
    public function addEvent($event)
    {
        if (array_search($event, $this->_event) !== FALSE)
        {
            $this->_events[] = $event;
        }
    }
 
    public function removeEvent($event)
    {
        $index = array_search($event, $this->_event);
        if ($index !== FALSE)
        {
            unset($this->_events[$index]);
        }
    }
 
    public function setEvents($events)
    {
        $this->_events = $events;
    }
 
    protected function _getFormActionUrl()
    {
        $request = $this->_request;
        $options = array('action' => $request->getActionName());
        return $this->_helper->url->url($options);
    }
 
    protected function prepareForm($form, $label, $back)
    {
        $form->setAction($this->_getFormActionUrl())
             ->setMethod('post');
        if ($back)
        {
            $back = $form->createElement('submit', 'back');
            $back->setLabel('back');
            $back->setDecorators($this->_submit_decorators);
            $form->addElement($back);
        }
 
        if ($label)
        {
            $submit = $form->createElement('submit', $label);
            $submit->setLabel($label);
            $submit->setDecorators($this->_submit_decorators);
            $form->addElement($submit);
        }
 
        $cancel = $form->createElement('submit', 'cancel');
        $cancel->setLabel('cancel');
        $cancel->setDecorators($this->_submit_decorators);
        $form->addElement($cancel);
 
        return $form;
    }
 
    protected function _fetchId()
    {
        if ($this->getRequest()->isGet() && isset($_GET['id']))
        {
            return intval($_GET['id']);
        }
        return null;
    }
 
    protected function _executeList($table)
    {
        return $table->fetchAll();
    }
 
    protected function _executeFind($table, $id)
    {
        $row =$table->find($id)->current();
        if ($row)
        {
            return $row->toArray();
        }
        return null;
    }
 
    protected function _executeInsert($table, $values)
    {
        $table->insert($values);
    }
 
    protected function _executeUpdate($table, $id, $values)
    {
        $row = $table->find($id)->current();
        $row->setFromArray($values);
        $row->save();
    }
 
    protected function _executeDelete($table, $id)
    {
        $row = $table->find($id)->current();
        $row->delete();
    }
 
    protected function _clearSession()
    {
        $session = $this->getSession();
        unset($session->current_id);
        unset($session->values);
    }
 
    protected function _executeEventHandler()
    {
        $this->view->action = $this->_request->getActionName();
        $events = array_intersect($this->_events, array_keys($_POST));
        if (count($events) > 0)
        {
            foreach ($events as $event)
            {
                $handler = '_handle' . ucfirst($event) . 'Event';
                if (method_exists($this, $handler))
                {
                    $view = $this->$handler();
                    break;
                }
            }
        }
        else
        {
            $view = $this->_handleEventDefault();
        }
        if ($view)
        {
            return $this->render($view);
        }
        $this->_clearSession();
        return $this->_helper->redirector('index');
    }
 
    protected function _handleEventDefault()
    {
        $session = $this->getSession();
        $table = $this->getTable();
        $form = $this->getForm();
        $editable = $this->_editable;
        $id = $this->_fetchId();
        if ($id)
        {
            $values = $this->_executeFind($table, $id);
            if ($values)
            {
                $session->current_id = $id;
                if ($editable)
                {
                    $form->setDefaults($values);
                }
                else
                {
                    $this->view->values = $values;
                    $form = new Zend_Form();
                    $action_label = $this->_request->getActionName();
                    if ($action_label == 'detail')
                    {
                        $action_label = null;
                    }
                    $this->view->form =
                        $this->prepareForm($form, $action_label, false);
                    return 'detail';
                }
            }
        }
        if ($editable)
        {
            $this->view->form = $this->prepareForm($form, 'next', false);
            return 'form';
        }
        return null;
    }
 
    protected function _handleNextEvent()
    {
        $session = $this->getSession();
        $form = $this->getForm();
        if ($form->isValid($_POST))
        {
            $session->values = $form->getValues();
            $this->view->values = $form->getValues();
            $form = new Zend_Form();
            $action_label = $this->_request->getActionName();
            $this->view->form = $this->prepareForm($form, $action_label, true);
            return 'detail';
        }
        $this->view->form = $this->prepareForm($form, 'next', true);
        return 'form';
    }
 
    protected function _handleAddEvent()
    {
        $session = $this->getSession();
        $table = $this->getTable();
        if (isset($session->values))
        {
            $values = $session->values;
            $this->_clearSession();
            $this->_executeInsert($table, $values);
            return 'finish';
        }
        return null;
    }
 
    protected function _handleUpdateEvent()
    {
        $session = $this->getSession();
        $table = $this->getTable();
        if (isset($session->values) && isset($session->current_id))
        {
            $current_id = $session->current_id;
            $values = $session->values;
            $this->_clearSession();
            $this->_executeUpdate($table, $current_id, $values);
            return 'finish';
        }
        return null;
    }
 
    protected function _handleDeleteEvent()
    {
        $session = $this->getSession();
        $table = $this->getTable();
        if (isset($session->current_id))
        {
            $current_id = $session->current_id;
            $this->_clearSession();
            $this->_executeDelete($table, $current_id);
            return 'finish';
        }
        return null;
    }
 
    protected function _handleBackEvent()
    {
        $session = $this->getSession();
        $form = $this->getForm();
        if (isset($session->values))
        {
            $form->setDefaults($session->values);
            unset($session->values);
            $this->view->form = $this->prepareForm($form, 'next', false);
            return 'form';
        }
        return null;
    }
 
    protected function _handleCancelEvent()
    {
        $this->_clearSession();
        return $this->_helper->redirector('list');
    }
}

Next
Next article will show how to use this controller.
History
Date Content
2008/4/15 Published
2008/4/18 The forward process when the action was canceled was changed to the redirect process.
The method getFormAction was modified to use the url helper.
The decorators of submit buttons was changed.
2008/4/26 Methods for DB processing were added.

Comments:0

Comment Form
Remember personal info

Trackbacks:0

Trackback URL for this entry
http://www.oplabo.com/article/24/trackback
Listed below are links to weblogs that reference
Creating Add/Update/Delete/View Action Controller from Open Programming Laboratory

Home > General > Creating Add/Update/Delete/View Action Controller

Japanese
Search
Feeds

Return to page top