Home > Zend_Form > 複数ページ対応追加/更新/削除/閲覧コントローラの作成

複数ページ対応追加/更新/削除/閲覧コントローラの作成

intro
以前に作成した追加/更新/削除/閲覧コントローラを複数ページフォーム対応にしてみます。ただし、リレーションは1:1のみ対応です。

1.コントローラの作成
以下のようにlibrary/My/Controller/MultiPage.phpファイルを作成します。

<?php
require_once 'My/Controller/Simple.php';
require_once 'My/Form/MultiPage.php';
require_once 'Zend/Session/Namespace.php';
 
class My_Controller_MultiPage extends My_Controller_Simple
{
    protected $_session_name = 'my_controller_multipage';
    protected $_table_form_classes = array();
 
    protected $_form = null;
 
    public function getForm()
    {
        if (!$this->_form)
        {
            $form = new My_Form_MultiPage($this->_session_name);
            foreach ($this->_table_form_classes as $table_class => $form_class)
            {
                $form->addForm($table_class, new $form_class());
            }
            $this->_form = $form;
        }
        return $this->_form;
    }
 
    protected function prepareForm($form, $label, $back)
    {
        if (method_exists($form, 'prepareForm'))
        {
            return $form->prepareForm($this->_getFormActionUrl(), $label);
        }
        return parent::prepareForm($form, $label, $back);
    }
 
 
    public function getTable()
    {
        if (!$this->_form)
        {
            $this->_form = $this->getForm();
        }
        $table_class = $this->_form->getCurrentFormName();
        if ($table_class)
        {
            return new $table_class();
        }
        return null;
    }
 
    protected function _clearSession()
    {
        parent::_clearSession();
        $this->getForm()->clear();
    }
 
    protected function _handleNextEvent()
    {
        $session = $this->getSession();
        $form = $this->getForm();
        $action_label = $this->_request->getActionName();
        if ($form->isValid($_POST))
        {
            if ($form->isConfirm())
            {
                $this->view->values = $form->getValues();
                $this->view->form = $form->prepareForm($this->_getFormActionUrl(), $action_label);
                return 'detail';
            }
        }
        $this->view->title = $form->getCurrentFormName();
        $this->view->form = $form->prepareForm($this->_getFormActionUrl(), $action_label);
        return 'form';
    }
 
    protected function _handleBackEvent()
    {
        $form = $this->getForm();
        $form->back();
        $this->view->form = $form->prepareForm($this->_getFormActionUrl(), 'next');
        return 'form';
    }
 
    protected function _executeFind($table, &$id)
    {
        $row = $table->find($id)->current();
        if ($row)
        {
            $id = array();
            return $this->_getValues($row, $id);
        }
        return null;
    }
 
    protected function _executeInsert($table, $values)
    {
        $dependant = false;
        foreach ($values as $table_name => $table_values)
        {
            $table = new $table_name();
            if ($dependant)
            {
                $this->_makeReference($table, $prev_table, $prev_id, $table_values, $prev_values);
            }
            $prev_id = $table->insert($table_values);
            $prev_table = $table_name;
            $prev_values = $table_values;
            $dependant = true;
        }
    }
 
    protected function _executeUpdate($table, $id, $values)
    {
        $dependant = false;
        foreach ($values as $table_name => $table_values)
        {
            $table = new $table_name();
            $current_id = $id[$table_name];
            $row = $table->find($current_id)->current();
            if (!$row)
            {
                $this->_makeReference($table, $prev_table, $prev_id, $table_values, $prev_values);
                $row = $table->createRow();
            }
            $row->setFromArray($table_values);
            $row->save();
            $prev_id = $current_id;
            $prev_table = $table_name;
            $prev_values = $table_values;
            $dependant = true;
        }
    }
 
    protected function _executeDelete($table, $id)
    {
         foreach ($id as $table_name => $current_id)
         {
             $table = new $table_name();
             $row = $table->find($current_id)->current();
             $row->delete();
         }
    }
 
    protected function _getValues($row, &$ids)
    {
        $values = array();
        $ids = array();
        $dependant = false;
        foreach ($this->_table_form_classes as $table_class => $form_name)
        {
            if ($depandant)
            {
                $row = $row->findDependentRowset($table_class)->current();
            }
            if ($row)
            {
                $values[$table_class] = $row->toArray();
                $ids[$table_class] = $this->_getRowId($table_class, $row);
                $depandant = true;
            }
            else
            {
                $depandant = false;
            }
        }
        return $values;
    }
 
    protected function _makeReference($table, $ref_table_name, $ref_row_id, &$values, &$ref_values)
    {
        $referenceMap = $table->getReference($ref_table_name);
        for ($i = 0; $i < count($referenceMap['columns']); $i++)
        {
            $column = $referenceMap['columns'][$i];
            $ref_column = $referenceMap['ref_columns'][$i];
            if (isset($ref_values[$ref_column]))
            {
                $values[$column] = $ref_values[$ref_column];
            }
            else
            {
                $values[$column] = $ref_row_id;
            }
        }
    }
 
    protected function _getRowId($table_class, $row)
    {
        if (isset($row->id))
        {
            return $row->id;
        }
        return null;
    }
}

次回
作成したコントローラを使います。
履歴
日付 内容
2008/4/20 公開
2008/4/26 My_Form_MultiPageの修正に伴いコントローラを修正しました。

Comments:0

Comment Form
Remember personal info

Trackbacks:0

Trackback URL for this entry
http://www.oplabo.jp/article/28/trackback
Listed below are links to weblogs that reference
複数ページ対応追加/更新/削除/閲覧コントローラの作成 from Open Programming Laboratory

Home > Zend_Form > 複数ページ対応追加/更新/削除/閲覧コントローラの作成

English
Search
Feeds

Return to page top