OSDN Git Service

- implement {form_input} helper for select list, check box, radio button.
authorichii386 <ichii386@2ef88817-412d-0410-a32c-8029a115e976>
Tue, 24 Oct 2006 20:17:32 +0000 (20:17 +0000)
committerichii386 <ichii386@2ef88817-412d-0410-a32c-8029a115e976>
Tue, 24 Oct 2006 20:17:32 +0000 (20:17 +0000)
- get the relevant action name of form definition within the block context.
- addActionFormHelper() is to be called in smarty function, so we need not do explicitly.

class/Ethna_SmartyPlugin.php
class/Ethna_ViewClass.php
class/Renderer/Ethna_Renderer_Smarty.php
skel/www.css.ethna.css
test/Ethna_ViewClass_Test.php

index cd0868c..d994267 100644 (file)
@@ -8,6 +8,7 @@
  *  @version    $Id$
  */
 
+// {{{ smarty_modifier_number_format
 /**
  *  smarty modifier:number_format()
  *
@@ -31,7 +32,9 @@ function smarty_modifier_number_format($string)
     }
     return number_format($string);
 }
+// }}}
 
+// {{{ smarty_modifier_strftime
 /**
  *  smarty modifier:strftime()
  *
@@ -56,7 +59,9 @@ function smarty_modifier_strftime($string, $format)
     }
     return strftime($format, strtotime($string));
 }
+// }}}
 
+// {{{ smarty_modifier_count
 /**
  *  smarty modifier:count()
  *
@@ -79,7 +84,9 @@ function smarty_modifier_count($array)
 {
     return count($array);
 }
+// }}}
 
+// {{{ smarty_modifier_join
 /**
  *  smarty modifier:join()
  *
@@ -106,7 +113,9 @@ function smarty_modifier_join($array, $glue)
     }
     return implode($glue, $array);
 }
+// }}}
 
+// {{{ smarty_modifier_filter
 /**
  *  smarty modifier:filter()
  *
@@ -144,7 +153,9 @@ function smarty_modifier_filter($array, $key)
     }
     return $tmp;
 }
+// }}}
 
+// {{{ smarty_modifier_unique
 /**
  *  smarty modifier:unique()
  *
@@ -192,7 +203,9 @@ function smarty_modifier_unique($array, $key = null)
         return array_unique($array);
     }
 }
+// }}}
 
+// {{{ smarty_modifier_wordwrap_i18n
 /**
  *  smarty modifier:ʸ»úÎó¤Îwordwrap½èÍý
  *
@@ -251,7 +264,9 @@ function smarty_modifier_wordwrap_i18n($string, $width, $break = "\n", $indent =
 
     return $r;
 }
+// }}}
 
+// {{{ smarty_modifier_truncate_i18n
 /**
  *  smarty modifier:ʸ»úÎóÀÚ¤êµÍ¤á½èÍý(i18nÂбþ)
  *
@@ -270,7 +285,9 @@ function smarty_modifier_truncate_i18n($string, $len = 80, $postfix = "...")
 {
     return mb_strimwidth($string, 0, $len, $postfix);
 }
+// }}}
 
+// {{{ smarty_modifier_i18n
 /**
  *  smarty modifier:i18n¥Õ¥£¥ë¥¿
  *
@@ -293,7 +310,9 @@ function smarty_modifier_i18n($string)
 
     return $i18n->get($string);
 }
+// }}}
 
+// {{{ smarty_modifier_checkbox
 /**
  *  smarty modifier:¥Á¥§¥Ã¥¯¥Ü¥Ã¥¯¥¹ÍÑ¥Õ¥£¥ë¥¿
  *
@@ -316,7 +335,9 @@ function smarty_modifier_checkbox($string)
         return "checked";
     }
 }
+// }}}
 
+// {{{ smarty_modifier_select
 /**
  *  smarty modifier:¥»¥ì¥¯¥È¥Ü¥Ã¥¯¥¹ÍÑ¥Õ¥£¥ë¥¿
  *
@@ -342,10 +363,12 @@ function smarty_modifier_checkbox($string)
 function smarty_modifier_select($string, $value)
 {
     if ($string == $value) {
-        print 'selected="true"';
+        return 'selected="true"';
     }
 }
+// }}}
 
+// {{{ smarty_modifier_form_value
 /**
  *  smarty modifier:¥Õ¥©¡¼¥àÃͽÐÎÏ¥Õ¥£¥ë¥¿
  *
@@ -377,30 +400,24 @@ function smarty_modifier_form_value($string)
 
     return htmlspecialchars($r, ENT_QUOTES);
 }
+// }}}
 
+// {{{ smarty_function_is_error
 /**
  *  smarty function:»ØÄꤵ¤ì¤¿¥Õ¥©¡¼¥à¹àÌܤǥ¨¥é¡¼¤¬È¯À¸¤·¤Æ¤¤¤ë¤«¤É¤¦¤«¤òÊÖ¤¹
- *
- *  sample:
- *  <code>
- *  {if is_error('name')}
- *  ¥¨¥é¡¼
- *  {/if}
- *  </code>
+ *  NOTE: {if is_error('name')} ¤Ï Ethna_Util.php ¤Î is_error() ¤Ç¤¢¤Ã¤Æ¡¢
+ *        smarty_function_is_error() ¤Ç¤Ï¤Ê¤¤¤³¤È¤ËÃí°Õ
  *
  *  @param  string  $name   ¥Õ¥©¡¼¥à¹àÌÜ̾
  */
 function smarty_function_is_error($params, &$smarty)
 {
-    $c =& Ethna_Controller::getInstance();
-
-    extract($params);
-
-    $action_error =& $c->getActionError();
-
-    return $action_error->isError($name);
+    $name = isset($params['name']) ? $params['name'] : null;
+    return is_error($name);
 }
+// }}}
 
+// {{{ smarty_function_message
 /**
  *  smarty function:»ØÄꤵ¤ì¤¿¥Õ¥©¡¼¥à¹àÌܤËÂбþ¤¹¤ë¥¨¥é¡¼¥á¥Ã¥»¡¼¥¸¤ò½ÐÎϤ¹¤ë
  *
@@ -416,15 +433,27 @@ function smarty_function_is_error($params, &$smarty)
  */
 function smarty_function_message($params, &$smarty)
 {
-    $c =& Ethna_Controller::getInstance();
-
-    extract($params);
+    if (isset($params['name']) === false) {
+        return '';
+    }
 
+    $c =& Ethna_Controller::getInstance();
     $action_error =& $c->getActionError();
 
-    print htmlspecialchars($action_error->getMessage($name));
+    $message = $action_error->getMessage($params['name']);
+    if ($message === null) {
+        return '';
+    }
+
+    $id = isset($params['id']) ? $params['id']
+        : str_replace("_", "-", "ethna-error-" . $params['name']);
+    $class = isset($params['class']) ? $params['class'] : "ethna-error";
+    return sprintf('<span class="%s" id="%s">%s</span>',
+        $class, $id, htmlspecialchars($message));
 }
+// }}}
 
+// {{{ smarty_function_uniqid
 /**
  *  smarty function:¥æ¥Ë¡¼¥¯ID¤òÀ¸À®¤¹¤ë(double post¥Á¥§¥Ã¥¯ÍÑ)
  *
@@ -441,16 +470,16 @@ function smarty_function_message($params, &$smarty)
  */
 function smarty_function_uniqid($params, &$smarty)
 {
-    extract($params);
-
     $uniqid = Ethna_Util::getRandom();
-    if (isset($type) && $type == 'get') {
-        print "uniqid=$uniqid";
+    if (isset($params['type']) && $params['type'] == 'get') {
+        return "uniqid=$uniqid";
     } else {
-        print "<input type=\"hidden\" name=\"uniqid\" value=\"$uniqid\" />\n";
+        return "<input type=\"hidden\" name=\"uniqid\" value=\"$uniqid\" />\n";
     }
 }
+// }}}
 
+// {{{ smarty_function_select
 /**
  *  smarty function:¥»¥ì¥¯¥È¥Õ¥£¡¼¥ë¥ÉÀ¸À®
  *
@@ -458,6 +487,7 @@ function smarty_function_uniqid($params, &$smarty)
  *  @param  string  $name   ¥Õ¥©¡¼¥à¹àÌÜ̾
  *  @param  string  $value  ¥»¥ì¥¯¥È¥Ü¥Ã¥¯¥¹¤ËÅϤµ¤ì¤¿¥Õ¥©¡¼¥àÃÍ
  *  @param  string  $empty  ¶õ¥¨¥ó¥È¥ê(¡Ö---ÁªÂò¤·¤Æ²¼¤µ¤¤---¡×Åù)
+ *  @deprecated
  */
 function smarty_function_select($params, &$smarty)
 {
@@ -472,13 +502,16 @@ function smarty_function_select($params, &$smarty)
     }
     print "</select>\n";
 }
+// }}}
 
+// {{{ smarty_function_checkbox_list
 /**
  *  smarty function:¥Á¥§¥Ã¥¯¥Ü¥Ã¥¯¥¹¥Õ¥£¥ë¥¿´Ø¿ô(ÇÛÎóÂбþ)
  *
  *  @param  string  $form   ¥Á¥§¥Ã¥¯¥Ü¥Ã¥¯¥¹¤ËÅϤµ¤ì¤¿¥Õ¥©¡¼¥àÃÍ
  *  @param  string  $key    É¾²ÁÂоݤÎÇÛÎ󥤥ó¥Ç¥Ã¥¯¥¹
  *  @param  string  $value  É¾²ÁÃÍ
+ *  @deprecated
  */
 function smarty_function_checkbox_list($params, &$smarty)
 {
@@ -516,7 +549,9 @@ function smarty_function_checkbox_list($params, &$smarty)
         }
     }
 }
+// }}}
 
+// {{{ smarty_function_url
 /**
  *  smarty function:urlÀ¸À®
  */
@@ -565,7 +600,9 @@ function smarty_function_url($params, &$smarty)
 
     return $url;
 }
+// }}}
 
+// {{{ smarty_function_form_name
 /**
  *  smarty function:¥Õ¥©¡¼¥àɽ¼¨Ì¾À¸À®
  *
@@ -573,20 +610,64 @@ function smarty_function_url($params, &$smarty)
  */
 function smarty_function_form_name($params, &$smarty)
 {
+    // name
     if (isset($params['name']) == false) {
         return null;
     }
     $name = $params['name'];
     unset($params['name']);
 
+    // action
+    $action = null;
+    if (isset($params['action'])) {
+        $action = $params['action'];
+        unset($params['action']);
+    } else {
+        for ($i = count($smarty->_tag_stack); $i >= 0; --$i) {
+            if ($smarty->_tag_stack[$i][0] === 'form') {
+                if (isset($smarty->_tag_stack[$i][1]['ethna_action'])) {
+                    $action = $smarty->_tag_stack[$i][1]['ethna_action'];
+                }
+                break;
+            }
+        }
+    }
+
     $c =& Ethna_Controller::getInstance();
     $view =& $c->getView();
     if ($view === null) {
         return null;
     }
-    print $view->getFormName($name, $params);
+    $view->addActionFormHelper($action);
+    return $view->getFormName($name, $action, $params);
 }
+// }}}
 
+// {{{ smarty_function_form_submit
+/**
+ *  smarty function:¥Õ¥©¡¼¥à¤Îsubmit¥Ü¥¿¥óÀ¸À®
+ *
+ *  @param  string  $submit   ¥Õ¥©¡¼¥à¹àÌÜ̾
+ */
+function smarty_function_form_submit($params, &$smarty)
+{
+    if (isset($params['name'])) {
+        $name = $params['name'];
+        unset($params['name']);
+    } else {
+        $name = 'submit';
+    }
+
+    $c =& Ethna_Controller::getInstance();
+    $view =& $c->getView();
+    if ($view === null) {
+        return null;
+    }
+    return $view->getFormSubmit($name, $params);
+}
+// }}}
+
+// {{{ smarty_function_form_input
 /**
  *  smarty function:¥Õ¥©¡¼¥à¥¿¥°À¸À®
  *
@@ -594,73 +675,95 @@ function smarty_function_form_name($params, &$smarty)
  */
 function smarty_function_form_input($params, &$smarty)
 {
-    if (isset($params['name']) == false) {
+    // name
+    if (isset($params['name'])) {
+        $name = $params['name'];
+        unset($params['name']);
+    } else {
         return null;
     }
-    $name = $params['name'];
-    unset($params['name']);
+
+    // ¸½ºß¤Î{form_input}¤ò°Ï¤àform block¤¬¤¢¤ì¤Ð¥Ñ¥é¥á¡¼¥¿¤ò¼èÆÀ¤·¤Æ¤ª¤¯
+    // c.f. http://smarty.php.net/manual/en/plugins.block.functions.php
+    $block_params = null;
+    for ($i = count($smarty->_tag_stack); $i >= 0; --$i) {
+        if ($smarty->_tag_stack[$i][0] === 'form') {
+            $block_params = $smarty->_tag_stack[$i][1];
+            break;
+        }
+    }
+
+    // action
+    $action = null;
+    if (isset($params['action'])) {
+        $action = $params['action'];
+        unset($params['action']);
+    } else if (isset($block_params['ethna_action'])) {
+        $action = $block_params['ethna_action'];
+    }
+
+    // default
+    if (isset($params['default']) === false) {
+        // {form_input default="foo"} ¤Î»ØÄ꤬¤Ê¤¤¤È¤­¤Ï
+        // ³°Â¦¤Î¥Ö¥í¥Ã¥¯¤«¤édefaultÃͤò¼èÆÀ
+        if (isset($block_params['default'][$name])) {
+            $params['default'] = $block_params['default'][$name];
+        }
+    }
 
     $c =& Ethna_Controller::getInstance();
     $view =& $c->getView();
     if ($view === null) {
         return null;
     }
-    print $view->getFormInput($name, $params);
+    $view->addActionFormHelper($action);
+    return $view->getFormInput($name, $action, $params);
 }
+// }}}
 
+// {{{ smarty_block_form
 /**
  *  smarty block:¥Õ¥©¡¼¥à¥¿¥°½ÐÎϥץ饰¥¤¥ó
  */
 function smarty_block_form($params, $content, &$smarty, &$repeat)
 {
-    extract($params);
-
-    $s = "";
     if ($repeat) {
-        $s = "<form";
-
-        // fundamentals
-        if ($action !== false) {
-            if (isset($action) == false) {
-                $action = basename($_SERVER['PHP_SELF']);
-            }
-            $s .= sprintf(' action="%s"', htmlspecialchars($action, ENT_QUOTES));
-        }
-        if ($method !== false) {
-            if (isset($method) == false) {
-                $method = "post";
-            }
-            $s .= sprintf(' method="%s"', htmlspecialchars($method, ENT_QUOTES));
+        return '';
+    } else {
+        $c =& Ethna_Controller::getInstance();
+        $view =& $c->getView();
+        if ($view === null) {
+            return null;
         }
 
-        // enctype¤Ïdefault off(-> "!=")
-        // + Ãͤ¬¤á¤ó¤É¤¤¤Î¤Çά¾ÎÂбþ("file"¤È¤«...¤¦¤¥¤à?)
-        if ($enctype != "") {
-            if ($enctype == "file" || $enctype == "multipart") {
-                $enctype = "multipart/form-data";
-            } else if ($enctype == "url") {
-                $enctype = "application/x-www-form-urlencoded";
-            }
-            $s .= sprintf(' enctype="%s"', htmlspecialchars($enctype, ENT_QUOTES));
+        // ethna_action
+        if (isset($params['ethna_action'])) {
+            $ethna_action = $params['ethna_action'];
+            unset($params['ethna_action']);
+
+            $view->addActionFormHelper($ethna_action);
+            $hidden = $c->getActionRequest($ethna_action, 'hidden');
+            $content = $hidden . $content;
         }
 
-        // additionals
-        foreach (array('accept', 'accept-charset', 'autocomplete', 'class', 'id', 'lang', 'name', 'style', 'target', 'title', 'urn') as $key) {
-            if (${$key} != "") {
-                $s .= sprintf(' %s="%s"', $key, htmlspecialchars(${$key}, ENT_QUOTES));
+        // enctype ¤Îά¾ÎÂбþ
+        if (isset($params['enctype'])) {
+            if ($params['enctype'] == 'file'
+                || $params['enctype'] == 'multipart') {
+                $params['enctype'] = 'multipart/form-data';
+            } else if ($params['enctype'] == 'url') {
+                $params['enctype'] = 'application/x-www-form-urlencoded';
             }
         }
 
-        $s .= ">";
-
-        if ($ethna_action != "") {
-            $c =& Ethna_Controller::getInstance();
-            $s .= $c->getActionRequest($ethna_action, "hidden");
+        // ¤³¤Îdefault¤ÏblockÆâ¤Îform_input¤Ç¤Î¤ß»È¤¦
+        // ($smarty->_tag_stack¤¬ÊÝ»ý)
+        if (isset($params['default'])) {
+            unset($params['default']);
         }
-    } else {
-        print $content;
-        $s = "</form>";
+
+        return $view->getFormBlock($content, $params);
     }
-    print $s;
 }
+// }}}
 ?>
index 4adfdfe..b19df07 100644 (file)
@@ -32,6 +32,9 @@ class Ethna_ViewClass
     /** @var    object  Ethna_I18N          i18n¥ª¥Ö¥¸¥§¥¯¥È */
     var $i18n;
 
+    /** @var    object  Ethna_Logger    ¥í¥°¥ª¥Ö¥¸¥§¥¯¥È */
+    var $logger;
+
     /** @var    object  Ethna_ActionError   ¥¢¥¯¥·¥ç¥ó¥¨¥é¡¼¥ª¥Ö¥¸¥§¥¯¥È */
     var $action_error;
 
@@ -58,6 +61,7 @@ class Ethna_ViewClass
 
     /**#@-*/
 
+    // {{{ Ethna_ViewClass
     /**
      *  Ethna_ViewClass¤Î¥³¥ó¥¹¥È¥é¥¯¥¿
      *
@@ -72,6 +76,7 @@ class Ethna_ViewClass
         $this->backend =& $backend;
         $this->config =& $this->backend->getConfig();
         $this->i18n =& $this->backend->getI18N();
+        $this->logger =& $this->backend->getLogger();
 
         $this->action_error =& $this->backend->getActionError();
         $this->ae =& $this->action_error;
@@ -91,7 +96,9 @@ class Ethna_ViewClass
             $this->helper_action_form[$key] =& $this->_getHelperActionForm($key);
         }
     }
+    // }}}
 
+    // {{{ preforward
     /**
      *  ²èÌÌɽ¼¨Á°½èÍý
      *
@@ -103,7 +110,9 @@ class Ethna_ViewClass
     function preforward()
     {
     }
+    // }}}
 
+    // {{{ forward
     /**
      *  Á«°Ü̾¤ËÂбþ¤¹¤ë²èÌ̤ò½ÐÎϤ¹¤ë
      *
@@ -118,7 +127,9 @@ class Ethna_ViewClass
         $this->_setDefault($renderer);
         $renderer->perform($this->forward_path);
     }
+    // }}}
 
+    // {{{ addActionFormHelper
     /**
      *  helper¥¢¥¯¥·¥ç¥ó¥Õ¥©¡¼¥à¥ª¥Ö¥¸¥§¥¯¥È¤òÀßÄꤹ¤ë
      *
@@ -132,7 +143,9 @@ class Ethna_ViewClass
         }
         $this->helper_action_form[$action] =& $this->_getHelperActionForm($action);
     }
+    // }}}
 
+    // {{{ clearActionFormHelper
     /**
      *  helper¥¢¥¯¥·¥ç¥ó¥Õ¥©¡¼¥à¥ª¥Ö¥¸¥§¥¯¥È¤òºï½ü¤¹¤ë
      *
@@ -142,15 +155,17 @@ class Ethna_ViewClass
     {
         unset($this->helper_action_form[$action]);
     }
+    // }}}
 
+    // {{{ getFormName
     /**
      *  »ØÄꤵ¤ì¤¿¥Õ¥©¡¼¥à¹àÌܤËÂбþ¤¹¤ë¥Õ¥©¡¼¥à̾(w/ ¥ì¥ó¥À¥ê¥ó¥°)¤ò¼èÆÀ¤¹¤ë
      *
      *  @access public
      */
-    function getFormName($name, $params)
+    function getFormName($name, $action, $params)
     {
-        $def = $this->_getHelperActionFormDef($name);
+        $def = $this->_getHelperActionFormDef($name, $action);
         $form_name = null;
         if (is_null($def) || isset($def['name']) == false) {
             $form_name = $name;
@@ -160,7 +175,24 @@ class Ethna_ViewClass
 
         return $form_name;
     }
+    // }}}
+
+    // {{{ getFormSubmit
+    /**
+     *  submit¥Ü¥¿¥ó¤ò¼èÆÀ¤¹¤ë(Á÷¿®À襢¥¯¥·¥ç¥ó¤Ç¼õ¤±¼è¤ë¤è¤¦
+     *  ÄêµÁ¤µ¤ì¤Æ¤¤¤Ê¤¤¤È¤­¤Ë¡¢¤¿¤ó¤Ësubmit¥Ü¥¿¥ó¤òºî¤ë¤Î¤Ë»È¤¦)
+     *
+     *  @access public
+     */
+    function getFormSubmit($name, $params)
+    {
+        $def = array('form_type' => FORM_TYPE_SUBMIT);
+        $input = $this->_getFormInput_Submit($name, $def, $params);
+        return $input;
+    }
+    // }}}
 
+    // {{{ getFormInput
     /**
      *  »ØÄꤵ¤ì¤¿¥Õ¥©¡¼¥à¹àÌܤËÂбþ¤¹¤ë¥Õ¥©¡¼¥à¥¿¥°¤ò¼èÆÀ¤¹¤ë
      *
@@ -169,9 +201,9 @@ class Ethna_ViewClass
      *  @access public
      *  @todo   form_type³Æ¼ïÂбþ/JavaScriptÂбþ...
      */
-    function getFormInput($name, $params)
+    function getFormInput($name, $action, $params)
     {
-        $def = $this->_getHelperActionFormDef($name);
+        $def = $this->_getHelperActionFormDef($name, $action);
         if (is_null($def)) {
             return "";
         }
@@ -179,13 +211,17 @@ class Ethna_ViewClass
         if (isset($def['form_type']) == false) {
             $def['form_type'] = FORM_TYPE_TEXT;
         }
+
+        if (is_array($def['type'])) {
+            $name .= '[]';
+        }
         
         switch ($def['form_type']) {
         case FORM_TYPE_BUTTON:
             $input = $this->_getFormInput_Button($name, $def, $params);
             break;
         case FORM_TYPE_CHECKBOX:
-            // T.B.D.
+            $input = $this->_getFormInput_Checkbox($name, $def, $params);
             break;
         case FORM_TYPE_FILE:
             $input = $this->_getFormInput_File($name, $def, $params);
@@ -197,10 +233,10 @@ class Ethna_ViewClass
             $input = $this->_getFormInput_Password($name, $def, $params);
             break;
         case FORM_TYPE_RADIO:
-            // T.B.D.
+            $input = $this->_getFormInput_Radio($name, $def, $params);
             break;
         case FORM_TYPE_SELECT:
-            // T.B.D.
+            $input = $this->_getFormInput_Select($name, $def, $params);
             break;
         case FORM_TYPE_SUBMIT:
             $input = $this->_getFormInput_Submit($name, $def, $params);
@@ -214,9 +250,47 @@ class Ethna_ViewClass
             break;
         }
 
-        print $input;
+        return $input;
+    }
+    // }}}
+
+    // {{{ getFormBlock
+    /**
+     *  ¥Õ¥©¡¼¥à¥¿¥°¤ò¼èÆÀ¤¹¤ë(type="form")
+     *
+     *  @access protected
+     */
+    function getFormBlock($content, $params)
+    {
+        $attr = array();
+
+        // action
+        if (isset($params['action'])) {
+            $attr['action'] = htmlspecialchars($params['action'], ENT_QUOTES);
+            unset($params['action']);
+        } else {
+            $action = basename($_SERVER['PHP_SELF']);
+        }
+
+        // method
+        if (isset($params['method'])) {
+            $attr['method'] = htmlspecialchars($params['method'], ENT_QUOTES);
+            unset($params['method']);
+        } else {
+            $attr['method'] = 'post';
+        }
+
+        // enctype
+        if (isset($params['enctype'])) {
+            $attr['enctype'] = htmlspecialchars($params['enctype'], ENT_QUOTES);
+            unset($params['enctype']);
+        }
+
+        return $this->_getFormInput_Html('form', $attr, $params, $content, false);
     }
+    // }}}
 
+    // {{{ _getHelperActionForm
     /**
      *  ¥¢¥¯¥·¥ç¥ó¥Õ¥©¡¼¥à¥ª¥Ö¥¸¥§¥¯¥È(helper)¤òÀ¸À®¤¹¤ë
      *
@@ -228,36 +302,54 @@ class Ethna_ViewClass
         $ctl =& Ethna_Controller::getInstance();
         $form_name = $ctl->getActionFormName($action);
         if ($form_name == null) {
-            // TODO: logging
+            $this->logger->log(LOG_WARNING,
+                'action form for the action [%s] not found.', $action);
             return null;
         }
         $af =& new $form_name($ctl);
 
         return $af;
     }
+    // }}}
 
+    // {{{ _getHelperActionFormDef
     /**
      *  ¥Õ¥©¡¼¥à¹àÌܤËÂбþ¤¹¤ë¥Õ¥©¡¼¥àÄêµÁ¤ò¼èÆÀ¤¹¤ë
      *
      *  @access protected
      */
-    function _getHelperActionFormDef($name)
+    function _getHelperActionFormDef($name, $action = null)
     {
-        $def = $this->af->getDef($name);
-        if (is_null($def)) {
-            foreach ($this->helper_action_form as $key => $value) {
-                if (is_object($value) == false) {
-                    continue;
-                }
-                $def = $value->getDef($name);
-                if (is_null($def) == false) {
-                    break;
+        $def = null;
+        if (is_null($action)) {
+            $def = $this->af->getDef($name);
+            if (is_null($def)) {
+                foreach ($this->helper_action_form as $key => $value) {
+                    if (is_object($value) == false) {
+                        continue;
+                    }
+                    $def = $value->getDef($name);
+                    if (is_null($def) == false) {
+                        break;
+                    }
                 }
             }
+        } else {
+            if (isset($this->helper_action_form[$action])
+                && is_object($this->helper_action_form[$action])) {
+                $af =& $this->helper_action_form[$action];
+                $def = $af->getDef($name);
+            }
+        }
+        if (is_null($def)) {
+            $this->logger->log(LOG_WARNING,
+                'form definition [%s] not found in action [%s]', $name, $action);
         }
         return $def;
     }
+    // }}}
 
+    // {{{ _getFormInput_Button
     /**
      *  ¥Õ¥©¡¼¥à¥¿¥°¤ò¼èÆÀ¤¹¤ë(type="button")
      *
@@ -265,13 +357,70 @@ class Ethna_ViewClass
      */
     function _getFormInput_Button($name, $def, $params)
     {
-        $r = array();
-        $r['type'] = "button";
-        $r['name'] = $name;
+        $attr = array();
+        $attr['type'] = "button";
+        $attr['name'] = $name;
+
+        return $this->_getFormInput_Html("input", $attr, $params);
+    }
+    // }}}
+
+    // {{{ _getFormInput_Checkbox
+    /**
+     *  ¥Á¥§¥Ã¥¯¥Ü¥Ã¥¯¥¹¥¿¥°¤ò¼èÆÀ¤¹¤ë(type="check")
+     *
+     *  @access protected
+     */
+    function _getFormInput_Checkbox($name, $def, $params)
+    {
+        $source = array();
+
+        // ¥ª¥×¥·¥ç¥ó¤Î°ìÍ÷(alist)¤ò¼èÆÀ
+        // XXX: experimental
+        if (isset($def['source'])) {
+            $source = $def['source'];
+        }
+
+        // defaultÃͤÎÀßÄê
+        if (isset($params['default'])) {
+            $current_value = $params['default'];
+            unset($params['default']);
+        }
+        $current_value = to_array($current_value);
+
+        // ¥¿¥°¤Î¥»¥Ñ¥ì¡¼¥¿
+        if (isset($params['separator'])) {
+            $separator = $params['separator'];
+            unset($params['separator']);
+        } else {
+            $separator = '';
+        }
+
+        $ret = array();
+        $i = 1;
+        foreach ($source as $key => $value) {
+            $attr = array();
+            $attr['type'] = 'checkbox';
+            $attr['name'] = $name;
+            $attr['value'] = $key;
+            $attr['id'] = $name . '_' . $i++;
+            if (in_array((string) $key, $current_value)) {
+                $attr['checked'] = 'checked';
+            }
+
+            // <input type="checkbox" />
+            $input_tag = $this->_getFormInput_Html('input', $attr, $params, $value);
+
+            // <label for="id">..</label>
+            $ret[] = $this->_getFormInput_Html('label', array('id' => $attr['id']),
+                                               $params, $input_tag, false);
+        }
 
-        return $this->_getFormInput_Html("input", $r, $params);
+        return implode($separator, $ret);
     }
+    // }}}
 
+    // {{{ _getFormInput_File
     /**
      *  ¥Õ¥©¡¼¥à¥¿¥°¤ò¼èÆÀ¤¹¤ë(type="file")
      *
@@ -279,14 +428,16 @@ class Ethna_ViewClass
      */
     function _getFormInput_File($name, $def, $params)
     {
-        $r = array();
-        $r['type'] = "file";
-        $r['name'] = $name;
-        $r['value'] = "";
+        $attr = array();
+        $attr['type'] = "file";
+        $attr['name'] = $name;
+        $attr['value'] = "";
 
-        return $this->_getFormInput_Html("input", $r, $params);
+        return $this->_getFormInput_Html("input", $attr, $params);
     }
+    // }}}
 
+    // {{{ _getFormInput_Hidden
     /**
      *  ¥Õ¥©¡¼¥à¥¿¥°¤ò¼èÆÀ¤¹¤ë(type="hidden")
      *
@@ -294,14 +445,22 @@ class Ethna_ViewClass
      */
     function _getFormInput_Hidden($name, $def, $params)
     {
-        $r = array();
-        $r['type'] = "hidden";
-        $r['name'] = $name;
-        $r['value'] = $this->af->get($name);
+        $attr = array();
+        $attr['type'] = "hidden";
+        $attr['name'] = $name;
+        if (isset($params['default'])) {
+            $attr['value'] = $params['default'];
+            unset($params['default']);
+        } else if (isset($params['value'])) {
+            $attr['value'] = $params['value'];
+            unset($params['value']);
+        }
 
-        return $this->_getFormInput_Html("input", $r, $params);
+        return $this->_getFormInput_Html("input", $attr, $params);
     }
+    // }}}
 
+    // {{{ _getFormInput_Password
     /**
      *  ¥Õ¥©¡¼¥à¥¿¥°¤ò¼èÆÀ¤¹¤ë(type="password")
      *
@@ -309,14 +468,134 @@ class Ethna_ViewClass
      */
     function _getFormInput_Password($name, $def, $params)
     {
-        $r = array();
-        $r['type'] = "password";
-        $r['name'] = $name;
-        $r['value'] = $this->af->get($name);
+        $attr = array();
+        $attr['type'] = "password";
+        $attr['name'] = $name;
+        if (isset($params['default'])) {
+            $attr['value'] = $params['default'];
+            unset($params['default']);
+        } else if (isset($params['value'])) {
+            $attr['value'] = $params['value'];
+            unset($params['value']);
+        }
+
+        return $this->_getFormInput_Html("input", $attr, $params);
+    }
+    // }}}
+
+    // {{{ _getFormInput_Radio
+    /**
+     *  ¥é¥¸¥ª¥Ü¥¿¥ó¥¿¥°¤ò¼èÆÀ¤¹¤ë(type="radio")
+     *
+     *  @access protected
+     */
+    function _getFormInput_Radio($name, $def, $params)
+    {
+        $source = array();
+
+        // ¥ª¥×¥·¥ç¥ó¤Î°ìÍ÷(alist)¤ò¼èÆÀ
+        // XXX: experimental
+        if (isset($def['source'])) {
+            $source = $def['source'];
+        }
+
+        // defaultÃͤÎÀßÄê
+        if (isset($params['default'])) {
+            $current_value = $params['default'];
+            unset($params['default']);
+        }
+
+        // ¥¿¥°¤Î¥»¥Ñ¥ì¡¼¥¿
+        if (isset($params['separator'])) {
+            $separator = $params['separator'];
+            unset($params['separator']);
+        } else {
+            $separator = '';
+        }
+
+        $ret = array();
+        $i = 1;
+        foreach ($source as $key => $value) {
+            $attr = array();
+            $attr['type'] = 'radio';
+            $attr['name'] = $name;
+            $attr['value'] = $key;
+            $attr['id'] = $name . '_' . $i++;
+            if ($current_value === (string) $key) {
+                $attr['checked'] = 'checked';
+            }
+
+            // <input type="radio" />
+            $input_tag = $this->_getFormInput_Html('input', $attr, $params, $value);
+
+            // <label for="id">..</label>
+            $ret[] = $this->_getFormInput_Html('label', array('id' => $attr['id']),
+                                               $params, $input_tag, false);
+        }
+
+        return implode($separator, $ret);
+    }
+    // }}}
+
+    // {{{ _getFormInput_Select
+    /**
+     *  ¥»¥ì¥¯¥È¥Ü¥Ã¥¯¥¹¥¿¥°¤ò¼èÆÀ¤¹¤ë(type="select")
+     *
+     *  @access protected
+     */
+    function _getFormInput_Select($name, $def, $params)
+    {
+        $source = array();
+
+        // ¥ª¥×¥·¥ç¥ó¤Î°ìÍ÷(alist)¤ò¼èÆÀ
+        if (isset($def['source'])) {
+            $source = $def['source'];
+        } else if (isset($params['actionform'])) {
+            $method = sprintf('list%s', str_replace('_', '', $params['action_form']));
+            $source = $this->af->$method();
+            unset($params['actionform']);
+        } else if (isset($params['manager']) && is_array($params['manager'])) {
+            list($manager_key, $manager_attr) = $params['manager'];
+            $manager =& $this->backend->getManager($manager_key);
+            $source = $manager->getAttrList($manager_attr);
+            unset($params['manager']);
+        } else if (isset($params['callback']) && is_callable($params['callback'])) {
+            $source = call_user_func($params['callback']);
+            unset($params['callback']);
+        }
+
+        // defaultÃͤÎÀßÄê
+        if (isset($params['default'])) {
+            $current_value = $params['default'];
+            unset($params['default']);
+        }
+
+        // ¥¿¥°¤Î¥»¥Ñ¥ì¡¼¥¿
+        if (isset($params['separator'])) {
+            $separator = $params['separator'];
+            unset($params['separator']);
+        } else {
+            $separator = '';
+        }
+
+        // select¥¿¥°¤ÎÃæ¿È¤òºî¤ë
+        $contents = array();
+        foreach ($source as $key => $value) {
+            if ($current_value === (string) $key) {
+                $attr = array('value' => $key, 'selected' => null);
+            } else {
+                $attr = array('value' => $key);
+            }
+            $contents[] = $this->_getFormInput_Html('option', $attr, $params, $value);
+        }
 
-        return $this->_getFormInput_Html("input", $r, $params);
+        $attr = array('name' => $name);
+        $element = $separator . implode($separator, $contents) . $separator;
+        return $this->_getFormInput_Html('select', $attr, $params, $element, false);
     }
+    // }}}
 
+    // {{{ _getFormInput_Submit
     /**
      *  ¥Õ¥©¡¼¥à¥¿¥°¤ò¼èÆÀ¤¹¤ë(type="submit")
      *
@@ -324,14 +603,16 @@ class Ethna_ViewClass
      */
     function _getFormInput_Submit($name, $def, $params)
     {
-        $r = array();
-        $r['type'] = "submit";
-        $r['name'] = $name;
-        $r['value'] = $def['name'];
+        $attr = array();
+        $attr['type'] = "submit";
+        $attr['name'] = $name;
+        $attr['value'] = $def['name'];
 
-        return $this->_getFormInput_Html("input", $r, $params);
+        return $this->_getFormInput_Html("input", $attr, $params);
     }
+    // }}}
 
+    // {{{ _getFormInput_Textarea
     /**
      *  ¥Õ¥©¡¼¥à¥¿¥°¤ò¼èÆÀ¤¹¤ë(textarea)
      *
@@ -339,12 +620,21 @@ class Ethna_ViewClass
      */
     function _getFormInput_Textarea($name, $def, $params)
     {
-        $r = array();
-        $r['name'] = $name;
+        $attr = array();
+        $attr['name'] = $name;
+        if (isset($params['default'])) {
+            $element = $params['default'];
+            unset($params['default']);
+        } else if (isset($params['value'])) {
+            $element = $params['value'];
+            unset($params['value']);
+        }
 
-        return $this->_getFormInput_Html("textarea", $r, $params, $this->af->get($name));
+        return $this->_getFormInput_Html("textarea", $attr, $params, $element);
     }
+    // }}}
 
+    // {{{ _getFormInput_Text
     /**
      *  ¥Õ¥©¡¼¥à¥¿¥°¤ò¼èÆÀ¤¹¤ë(type="text")
      *
@@ -352,27 +642,37 @@ class Ethna_ViewClass
      */
     function _getFormInput_Text($name, $def, $params)
     {
-        $r = array();
-        $r['type'] = "text";
-        $r['name'] = $name;
-        $r['value'] = $this->af->get($name);
+        $attr = array();
+        $attr['type'] = "text";
+        $attr['name'] = $name;
+        if (isset($params['default'])) {
+            $attr['value'] = $params['default'];
+            unset($params['default']);
+        } else if (isset($params['value'])) {
+            $attr['value'] = $params['value'];
+            unset($params['value']);
+        }
         if (isset($def['max']) && $def['max']) {
-            $r['maxlength'] = $def['max'];
+            $attr['maxlength'] = $def['max'];
         }
 
-        return $this->_getFormInput_Html("input", $r, $params);
+        return $this->_getFormInput_Html("input", $attr, $params);
     }
+    // }}}
 
+    // {{{ _getFormInput_Html
     /**
      *  HTML¥¿¥°¤ò¼èÆÀ¤¹¤ë
      *
      *  @access protected
      */
-    function _getFormInput_Html($tag, $attr, $user_attr, $element = false)
+    function _getFormInput_Html($tag, $attr, $user_attr,
+                                $element = null, $escape_elemet = true)
     {
         // user defs
         foreach ($user_attr as $key => $value) {
-            if ($key == "type" || $key == "name" || preg_match('/^[a-z0-9]+$/i', $key) == 0) {
+            if ($key == "type" || $key == "name"
+                || preg_match('/^[a-z0-9]+$/i', $key) == 0) {
                 continue;
             }
             $attr[$key] = $value;
@@ -381,18 +681,25 @@ class Ethna_ViewClass
         $r = "<$tag";
 
         foreach ($attr as $key => $value) {
-            $r .= sprintf(' %s="%s"', $key, htmlspecialchars($value, ENT_QUOTES));
+            if (is_null($value)) {
+                $r .= sprintf(' %s', $key);
+            } else {
+                $r .= sprintf(' %s="%s"', $key, htmlspecialchars($value, ENT_QUOTES));
+            }
         }
 
-        if ($element !== false) {
-            $r .= sprintf('>%s</%s>', htmlspecialchars($element, ENT_QUOTES), $tag);
-        } else {
+        if (is_null($element)) {
             $r .= " />";
+        } else {
+            $r .= sprintf('>%s</%s>', $escape_elemet
+                ? htmlspecialchars($element, ENT_QUOTES) : $element, $tag);
         }
 
         return $r;
     }
+    // }}}
 
+    // {{{ _getRenderer
     /**
      *  ¥ì¥ó¥À¥é¥ª¥Ö¥¸¥§¥¯¥È¤ò¼èÆÀ¤¹¤ë
      *
@@ -404,7 +711,9 @@ class Ethna_ViewClass
         $_ret_object =& $this->_getTemplateEngine();
         return $_ret_object;
     }
+    // }}}
 
+    // {{{ _getTemplateEngine
     /**
      *  ¥ì¥ó¥À¥é¥ª¥Ö¥¸¥§¥¯¥È¤ò¼èÆÀ¤¹¤ë(¤½¤Î¤¦¤Á_getRenderer()¤ËÅý¹ç¤µ¤ì¤ëͽÄê)
      *
@@ -429,13 +738,17 @@ class Ethna_ViewClass
             $tmp_session = Ethna_Util::escapeHtml($_SESSION);
             $renderer->setPropByRef('session', $tmp_session);
         }
-        $renderer->setProp('script', htmlspecialchars(basename($_SERVER['PHP_SELF']), ENT_QUOTES));
-        $renderer->setProp('request_uri', htmlspecialchars($_SERVER['REQUEST_URI'], ENT_QUOTES));
+        $renderer->setProp('script',
+            htmlspecialchars(basename($_SERVER['PHP_SELF']), ENT_QUOTES));
+        $renderer->setProp('request_uri',
+            htmlspecialchars($_SERVER['REQUEST_URI'], ENT_QUOTES));
         $renderer->setProp('config', $this->config->get());
 
         return $renderer;
     }
+    // }}}
 
+    // {{{ _setDefault
     /**
      *  ¶¦ÄÌÃͤòÀßÄꤹ¤ë
      *
@@ -445,6 +758,7 @@ class Ethna_ViewClass
     function _setDefault(&$renderer)
     {
     }
+    // }}}
 }
 // }}}
 ?>
index 8c555b0..cbc7ad4 100644 (file)
@@ -82,6 +82,7 @@ class Ethna_Renderer_Smarty extends Ethna_Renderer
         $this->setPlugin('checkbox_list','function','smarty_function_checkbox_list');
         $this->setPlugin('form_name','function','smarty_function_form_name');
         $this->setPlugin('form_input','function','smarty_function_form_input');
+        $this->setPlugin('form_submit','function','smarty_function_form_submit');
         $this->setPlugin('url','function','smarty_function_url');
         
         // default blocks
index 6f8688a..633f9e6 100644 (file)
@@ -49,3 +49,7 @@ div#main a:link {
 div#main a:visited {
     color: #f29578;
 }
+
+span.ethna-error {
+    color: #ff0000;
+}
index 8046122..e9e6280 100644 (file)
@@ -121,7 +121,7 @@ class Ethna_ViewClass_Test extends UnitTestCase
 
         $this->viewclass->af->form = $test_form;
 
-        $result = $this->viewclass->getFormName('test_text', $params);
+        $result = $this->viewclass->getFormName('test_text', null, $params);
         $this->assertEqual($result, $test_word);
      }