OSDN Git Service

- added implementation of dynamic form definition helper via form helper.
authormumumu-org <mumumu-org@2ef88817-412d-0410-a32c-8029a115e976>
Fri, 10 Apr 2009 18:18:02 +0000 (18:18 +0000)
committermumumu-org <mumumu-org@2ef88817-412d-0410-a32c-8029a115e976>
Fri, 10 Apr 2009 18:18:02 +0000 (18:18 +0000)
class/Ethna_ActionForm.php
class/Ethna_ViewClass.php
class/Plugin/Smarty/block.form.php
class/Plugin/Smarty/function.form_input.php

index 5ca3a82..e412e4e 100644 (file)
@@ -1239,8 +1239,12 @@ class Ethna_ActionForm
     }
 
     /**
+     *  フォーム定義変更用、ユーザ定義ヘルパメソッド
+     *
+     *  Ethna_ActionForm#prepare() が実行される前に
      *  ユーザが動的にフォーム定義を変更したい場合に
      *  このメソッドをオーバーライドします。
+     *
      *  $this->backend も初期化済みのため、DBやセッション
      *  の値に基づいてフォーム定義を変更することができます。
      *
@@ -1252,6 +1256,26 @@ class Ethna_ActionForm
     }
 
     /**
+     *  フォーム定義変更用、ユーザ定義ヘルパメソッド
+     *
+     *  フォームヘルパを使うときに、フォーム定義を動的に
+     *  変更したい場合に、このメソッドをオーバーライドします。
+     *
+     *  以下の定義をテンプレートで行った場合に呼び出されます。
+     *  
+     *  {form ethna_action=...} (ethna_action がない場合は呼び出されません)
+     *  {form_input action=...} (action がない場合は呼び出されません)
+     *
+     *  @access public 
+     */
+    function setFormDef_ViewHelper()
+    {
+        //   TODO: デフォルト実装は Ethna_ActionClass#prepare 前に
+        //   呼び出されるものと同じ。異なる場合にオーバライドする
+        $this->setFormDef_PreHelper(); 
+    }
+
+    /**
      *  ヘルパオブジェクト(アプリケーションオブジェクト)
      *  経由でのフォーム値定義を設定する
      *
index b6970f0..3aa4964 100644 (file)
@@ -145,29 +145,40 @@ class Ethna_ViewClass
      *  helperアクションフォームオブジェクトを設定する
      *
      *  @param  string $action アクション名
+     *  @param  boolean $dynamic_helper 動的フォームヘルパを呼ぶか否か
      *  @access public
      */
-    function addActionFormHelper($action)
+    function addActionFormHelper($action, $dynamic_helper = false)
     {
+        //
+        //  既に追加されている場合は処理をしない
+        //
         if (isset($this->helper_action_form[$action])
             && is_object($this->helper_action_form[$action])) {
             return;
         }
 
+        //    現在のアクションと等しければ、対応する
+        //    アクションフォームを設定
         $ctl =& Ethna_Controller::getInstance();
         if ($action === $ctl->getCurrentActionName()) {
             $this->helper_action_form[$action] =& $this->af;
-            return;
+        } else {
+            //    アクションが異なる場合
+            $form_name = $ctl->getActionFormName($action);
+            if ($form_name === null) {
+                $this->logger->log(LOG_WARNING,
+                    'action form for the action [%s] not found.', $action);
+                return;
+            }
+            $this->helper_action_form[$action] =& new $form_name($ctl);
         }
 
-        $form_name = $ctl->getActionFormName($action);
-        if ($form_name === null) {
-            $this->logger->log(LOG_WARNING,
-                'action form for the action [%s] not found.', $action);
-            return;
+        //   動的フォームを設定するためのヘルパメソッドを呼ぶ
+        if ($dynamic_helper) {
+            $af =& $this->helper_action_form[$action];
+            $af->setFormDef_ViewHelper();
         }
-
-        $this->helper_action_form[$action] =& new $form_name($ctl);
     }
     // }}}
 
index 316c6b7..b3ae1b4 100644 (file)
@@ -32,6 +32,13 @@ function smarty_block_form($params, $content, &$smarty, &$repeat)
                 = 'default';
         }
 
+        // 動的フォームヘルパを呼ぶ
+        if (isset($params['ethna_action'])) {
+            $ethna_action = $params['ethna_action'];
+            $view =& $c->getView();
+            $view->addActionFormHelper($ethna_action, true);
+        }  
+
         // ここで返す値は出力されない
         return '';
 
index 3e8a77e..6d62936 100644 (file)
@@ -39,7 +39,7 @@ function smarty_function_form_input($params, &$smarty)
         $action = $block_params['ethna_action'];
     }
     if ($action !== null) {
-        $view->addActionFormHelper($action);
+        $view->addActionFormHelper($action, true);
     }
 
     // default