Source for file ViewClass.php

Documentation is available at ViewClass.php

  1. <?php
  2. // vim: foldmethod=marker
  3. /**
  4.  *  ViewClass.php
  5.  *
  6.  *  @author     Masaki Fujimoto <fujimoto@php.net>
  7.  *  @license    http://www.opensource.org/licenses/bsd-license.php The BSD License
  8.  *  @package    Ethna
  9.  *  @version    $Id: 4188accafabceee2638da3820d394bb0a50a45df $
  10.  */
  11.  
  12. // {{{ Ethna_ViewClass
  13. /**
  14.  *  viewクラス
  15.  *
  16.  *  @author     Masaki Fujimoto <fujimoto@php.net>
  17.  *  @access     public
  18.  *  @package    Ethna
  19.  */
  20. {
  21.     /**#@+
  22.      *  @access private
  23.      */
  24.  
  25.     /** @protected    object  Ethna_Controller    Controllerオブジェクト */
  26.     protected $ctl;
  27.  
  28.     /** @protected    object  Ethna_Backend       backendオブジェクト */
  29.     protected $backend;
  30.  
  31.     /** @protected    object  Ethna_Config        設定オブジェクト    */
  32.     protected $config;
  33.  
  34.     /** @protected    object  Ethna_I18N          i18nオブジェクト */
  35.     protected $i18n;
  36.  
  37.     /** @protected    object  Ethna_Logger    ログオブジェクト */
  38.     protected $logger;
  39.  
  40.     /** @protected    object  Ethna_Plugin    プラグインオブジェクト */
  41.     public $plugin;
  42.  
  43.     /** @protected    object  Ethna_ActionError   アクションエラーオブジェクト */
  44.     protected $action_error;
  45.  
  46.     /** @protected    object  Ethna_ActionError   アクションエラーオブジェクト(省略形) */
  47.     protected $ae;
  48.  
  49.     /** @protected    object  Ethna_ActionForm    アクションフォームオブジェクト */
  50.     protected $action_form;
  51.  
  52.     /** @protected    object  Ethna_ActionForm    アクションフォームオブジェクト(省略形) */
  53.     protected $af;
  54.  
  55.     /** @protected    array   アクションフォームオブジェクト(helper) */
  56.     protected $helper_action_form = array();
  57.  
  58.     /** @protected    array   helperでhtmlのattributeにはしないパラメータの一覧 */
  59.     protected $helper_parameter_keys = array('default''option''separator');
  60.  
  61.     /** @protected    object  Ethna_Session       セッションオブジェクト */
  62.     protected $session;
  63.  
  64.     /** @protected    string  遷移名 */
  65.     public $forward_name;
  66.  
  67.     /** @protected    string  遷移先テンプレートファイル名 */
  68.     protected $forward_path;
  69.  
  70.     /** @protected    boolean  配列フォームを呼んだカウンタをリセットするか否か */
  71.     protected $reset_counter = false;
  72.  
  73.     /**#@-*/
  74.  
  75.     /**#@+
  76.      *  @access protected
  77.      */
  78.  
  79.     /** @var  string レイアウト(HTMLの外枠を記述するファイル)のテンプレートファイルを指定(拡張子は除く)   */
  80.     protected $_layout_file = 'layout';
  81.  
  82.     /**#@-*/
  83.  
  84.     /**#@+
  85.      *  @access public
  86.      */
  87.  
  88.     /** @var boolean  レイアウトテンプレートの使用フラグ       */
  89.     public $use_layout = true;
  90.  
  91.     /** @var  boolean  デフォルトのヘッダ出力を使用するか否か  */
  92.     /**                ヘッダ出力を改造する場合はfalseにする   */
  93.     public $has_default_header = true;
  94.  
  95.     /** @var  array    default header */
  96.     public $default_header = array(
  97.         'Pragma' => 'no-cache',
  98.         'Cache-Control' => 'no-cache, no-store, must-revalidate',
  99.     );
  100.  
  101.     // {{{ Ethna_ViewClass
  102.     /**
  103.      *  Ethna_ViewClassのコンストラクタ
  104.      *
  105.      *  @access public
  106.      *  @param  object  Ethna_Backend   $backend    backendオブジェクト
  107.      *  @param  string  $forward_name   ビューに関連付けられている遷移名
  108.      *  @param  string  $forward_path   ビューに関連付けられているテンプレートファイル名
  109.      */
  110.     public function __construct($backend$forward_name$forward_path)
  111.     {
  112.         $c $backend->getController();
  113.         $this->ctl = $c;
  114.         $this->backend = $backend;
  115.         $this->config = $this->backend->getConfig();
  116.         $this->i18n = $this->backend->getI18N();
  117.         $this->logger = $this->backend->getLogger();
  118.         $this->plugin = $this->backend->getPlugin();
  119.  
  120.         $this->action_error = $this->backend->getActionError();
  121.         $this->ae = $this->action_error;
  122.  
  123.         $this->action_form = $this->backend->getActionForm();
  124.         $this->af = $this->action_form;
  125.  
  126.         $this->session = $this->backend->getSession();
  127.  
  128.         $this->forward_name = $forward_name;
  129.         $this->forward_path = $forward_path;
  130.  
  131.         foreach (array_keys($this->helper_action_formas $action{
  132.             $this->addActionFormHelper($action);
  133.         }
  134.     }
  135.     // }}}
  136.  
  137.     // {{{ preforward
  138.     /**
  139.      *  画面表示前処理
  140.      *
  141.      *  テンプレートに設定する値でコンテキストに依存しないものは
  142.      *  ここで設定する(例:セレクトボックス等)
  143.      *
  144.      *  @access public
  145.      *  @param  mixed  $params  アクションクラスから返された引数
  146.      *                           array('forward_name', $param) の形でアクション
  147.      *                           から値を返すことで、$params に値が渡されます。
  148.      */
  149.     public function preforward()
  150.     {
  151.     }
  152.     // }}}
  153.  
  154.     // {{{ forward
  155.     /**
  156.      *  遷移名に対応する画面を出力する
  157.      *
  158.      *  特殊な画面を表示する場合を除いて特にオーバーライドする必要は無い
  159.      *  (preforward()のみオーバーライドすれば良い)
  160.      *
  161.      *  @access public
  162.      */
  163.     public function forward()
  164.     {
  165.         $renderer $this->_getRenderer();
  166.         $this->_setDefault($renderer);
  167.  
  168.         if ($this->has_default_header{
  169.             $this->default_header['Content-Type''text/html; charset=' $this->ctl->getClientEncoding();
  170.             $this->header($this->default_header);
  171.         }
  172.  
  173.         // using layout.tpl flag
  174.         if ($this->use_layout{
  175.  
  176.             // check : layout file existance
  177.             $layout $this->getLayout();
  178.             if ($this->templateExists($layout)) {
  179.                 $content $renderer->perform($this->forward_pathtrue);
  180.  
  181.                 if (Ethna::isError($content)) {
  182.                     if ($content->getCode(== E_GENERAL{
  183.                         $error 404;
  184.                     }
  185.                     else {
  186.                         $error 500;
  187.                     }
  188.  
  189.                     $this->error($error);
  190.                     $content $renderer->perform($this->forward_pathtrue);
  191.                 }
  192.  
  193.                 $renderer->setProp('content'$content);
  194.                 if (isset($_SERVER['REQUEST_URI'])) {
  195.                     $uri_hash md5($_SERVER['REQUEST_URI']);
  196.                     $e $renderer->perform($layout$uri_hash);
  197.                 }
  198.                 else {
  199.                     $e $renderer->perform($layout);
  200.                 }
  201.             else {
  202.                 return Ethna::raiseWarning('file "'.$layout.'" not found');
  203.             }
  204.         else {
  205.             $e $renderer->perform($this->forward_path);
  206.         }
  207.  
  208.         if (Ethna::isError($e)) {
  209.             echo '<h1>Rendering error:</h1>';
  210.             echo '<h2>Message: ' $e->getMessage('</h2>';
  211.         }
  212.     }
  213.     // }}}
  214.  
  215.     // {{{ header
  216.     /**
  217.      *  HTTPヘッダを送信します。
  218.      *
  219.      *  @param  mixed   ヘッダを設定する値
  220.      *                   配列指定の場合、header => value の形式
  221.      *                   整数指定の場合は、HTTPステータスコード
  222.      *                   文字列で指定する場合は、ヘッダ出力をそのまま指定
  223.      *  @access public
  224.      */
  225.     public function header($status)
  226.     {
  227.         if (is_array($status)) {
  228.             foreach ($status as $key => $status{
  229.                 header ($key ": " $status);
  230.             }
  231.         else if (is_int($status)) {
  232.             $codes array(
  233.                 100 => "Continue",
  234.                 101 => "Switching Protocols",
  235.                 200 => "OK",
  236.                 201 => "Created",
  237.                 202 => "Accepted",
  238.                 203 => "Non-Authoritative Information",
  239.                 204 => "No Content",
  240.                 205 => "Reset Content",
  241.                 206 => "Partial Content",
  242.                 300 => "Multiple Choices",
  243.                 301 => "Moved Permanently",
  244.                 302 => "Found",
  245.                 303 => "See Other",
  246.                 304 => "Not Modified",
  247.                 305 => "Use Proxy",
  248.                 307 => "Temporary Redirect",
  249.                 400 => "Bad Request",
  250.                 401 => "Unauthorized",
  251.                 402 => "Payment Required",
  252.                 403 => "Forbidden",
  253.                 404 => "Not Found",
  254.                 405 => "Method Not Allowed",
  255.                 406 => "Not Acceptable",
  256.                 407 => "Proxy Authentication Required",
  257.                 408 => "Request Time-out",
  258.                 409 => "Conflict",
  259.                 410 => "Gone",
  260.                 411 => "Length Required",
  261.                 412 => "Precondition Failed",
  262.                 413 => "Request Entity Too Large",
  263.                 414 => "Request-URI Too Large",
  264.                 415 => "Unsupported Media Type",
  265.                 416 => "Requested range not satisfiable",
  266.                 417 => "Expectation Failed",
  267.                 500 => "Internal Server Error",
  268.                 501 => "Not Implemented",
  269.                 502 => "Bad Gateway",
  270.                 503 => "Service Unavailable",
  271.                 504 => "Gateway Time-out"
  272.             );
  273.  
  274.             if (array_key_exists($status$codes)) {
  275.                 header("HTTP/1.1: {$status} {$codes[$status]}");
  276.             }
  277.         else {
  278.             // check valid header
  279.             if (preg_match("/^.+\:\s.+$/"$status)) {
  280.                 header($status);
  281.             }
  282.         }
  283.     }
  284.     // }}}
  285.  
  286.     // {{{ redirect
  287.     /**
  288.      *  リダイレクト処理
  289.      *   - デフォルトのヘッダを送信しない
  290.      *   - レイアウトテンプレートの使用をしない
  291.      *
  292.      *  @param  string  リダイレクト先(URL)
  293.      *  @param  int     HTTPステータスコード (3xx)
  294.      *  @access public
  295.      */
  296.     public function redirect($url$staus_code 302)
  297.     {
  298.         $this->has_default_header = false;
  299.         $this->use_layout = false;
  300.  
  301.         $this->header($staus_code);
  302.         $this->header(array('Location' => $url));
  303.     }
  304.     // }}}
  305.  
  306.     // {{{ setLayout
  307.     /**
  308.      *  レイアウトテンプレートのファイル名を設定します。
  309.      *  レイアウトテンプレートは、HTML の外枠を設定するのに使用します。
  310.      *  
  311.      *  @param string $filename  レイアウトファイル名
  312.      *  @access public
  313.      */
  314.     public function setLayout($filename)
  315.     {
  316.         // check layout file existance
  317.         if ($this->templateExists($filename '.' $this->ctl->getExt('tpl'))) {
  318.             $this->_layout_file = $filename;
  319.             return true;
  320.         else {
  321.             return Ethna::raiseWarning('file "'$filename '.' $this->ctl->getExt('tpl''" not found');
  322.         }
  323.     }
  324.     // }}}
  325.  
  326.     // {{{ getLayout
  327.     /**
  328.      *  レイアウトテンプレートファイル名を取得します。
  329.      *  
  330.      *  @return string  レイアウトテンプレートのファイル名
  331.      *  @access public
  332.      */
  333.     public function getLayout()
  334.     {
  335.         return $this->_layout_file . '.' $this->ctl->getExt('tpl');
  336.     }
  337.     // }}}
  338.  
  339.     // {{{ getCurrentForwardName()
  340.     /**
  341.      *  getCurrentForwardName
  342.      *
  343.      *  @access public
  344.      */
  345.     public function getCurrentForwardName()
  346.     {
  347.         return $this->forward_name;
  348.     }
  349.     // }}}
  350.  
  351.     // {{{ templateExists
  352.     /**
  353.      *  テンプレートファイルが存在するか否かを返します。
  354.      *
  355.      * @param   string  $filename  チェック対象のテンプレートファイル
  356.      * @access  public
  357.      * @return  boolean 指定したテンプレートファイルが存在すればtrue
  358.      *                   存在しなければfalse
  359.      */
  360.     public function templateExists($filename)
  361.     {
  362.         $renderer $this->_getRenderer();
  363.         if ($renderer->templateExists($filename)) {
  364.             return true;
  365.         }
  366.         else {
  367.             return false;
  368.         }
  369.     }
  370.     // }}}
  371.  
  372.     // {{{ error 
  373.     /**
  374.      *  エラーページ出力用のHTTPステータスコードを指定します。
  375.      *
  376.      *  @param  int  HTTPステータスコード
  377.      *  @access public
  378.      */
  379.     public function error($code)
  380.     {
  381.         $this->header($code);
  382.  
  383.         // template 以下に error404.tpl とかがあれば, 
  384.         // preforward で $this->error(404); とかすればいい
  385.         $this->forward_path = "error{$code}.tpl";
  386.     }
  387.     // }}}
  388.  
  389.     // {{{ addActionFormHelper
  390.     /**
  391.      *  helperアクションフォームオブジェクトを設定する
  392.      *
  393.      *  @param  string $action アクション名
  394.      *  @param  boolean $dynamic_helper 動的フォームヘルパを呼ぶか否か
  395.      *  @access public
  396.      */
  397.     public function addActionFormHelper($action$dynamic_helper false)
  398.     {
  399.         //
  400.         //  既に追加されている場合は処理をしない
  401.         //
  402.         if (isset($this->helper_action_form[$action])
  403.             && is_object($this->helper_action_form[$action])) {
  404.             return;
  405.         }
  406.  
  407.         //    現在のアクションと等しければ、対応する
  408.         //    アクションフォームを設定
  409.         $ctl Ethna_Controller::getInstance();
  410.         if ($action === $ctl->getCurrentActionName()) {
  411.             $this->helper_action_form[$action$this->af;
  412.         else {
  413.             //    アクションが異なる場合
  414.             $form_name $ctl->getActionFormName($action);
  415.             if ($form_name === null{
  416.                 $this->logger->log(LOG_WARNING,
  417.                     'action form for the action [%s] not found.'$action);
  418.                 return;
  419.             }
  420.             $this->helper_action_form[$actionnew $form_name($ctl);
  421.         }
  422.  
  423.         //   動的フォームを設定するためのヘルパメソッドを呼ぶ
  424.         if ($dynamic_helper{
  425.             $af $this->helper_action_form[$action];
  426.             $af->setFormDef_ViewHelper();
  427.         }
  428.     }
  429.     // }}}
  430.  
  431.     // {{{ clearActionFormHelper
  432.     /**
  433.      *  helperアクションフォームオブジェクトを削除する
  434.      *
  435.      *  @access public
  436.      */
  437.     public function clearActionFormHelper($action)
  438.     {
  439.         unset($this->helper_action_form[$action]);
  440.     }
  441.     // }}}
  442.  
  443.     // {{{ _getHelperActionForm
  444.     /**
  445.      *  アクションフォームオブジェクト(helper)を取得する
  446.      *  $action === null で $name が指定されているときは、$nameの定義を
  447.      *  含むものを探す
  448.      *
  449.      *  @access protected
  450.      *  @param  string  action  取得するアクション名
  451.      *  @param  string  name    定義されていることを期待するフォーム名
  452.      *  @return object  Ethna_ActionFormまたは継承オブジェクト 
  453.      */
  454.     protected function _getHelperActionForm($action null$name null)
  455.     {
  456.         // $action が指定されている場合
  457.         if ($action !== null{
  458.             if (isset($this->helper_action_form[$action])
  459.                 && is_object($this->helper_action_form[$action])) {
  460.                 return $this->helper_action_form[$action];
  461.             else {
  462.                 $this->logger->log(LOG_WARNING,
  463.                     'helper action form for action [%s] not found',
  464.                     $action);
  465.                 return null;
  466.             }
  467.         }
  468.  
  469.         // 最初に $this->af を調べる
  470.         $def $this->af->getDef($name);
  471.         if ($def !== null{
  472.             return $this->af;
  473.         }
  474.  
  475.         // $this->helper_action_form を順に調べる
  476.         foreach (array_keys($this->helper_action_formas $action{
  477.             if (is_object($this->helper_action_form[$action]=== false{
  478.                 continue;
  479.             }
  480.             $af $this->helper_action_form[$action];
  481.             $def $af->getDef($name);
  482.             if (is_null($def=== false{
  483.                 return $af;
  484.             }
  485.         }
  486.  
  487.         // 見付からなかった
  488.         $this->logger->log(LOG_WARNING,
  489.             'action form defining form [%s] not found'$name);
  490.         return null;
  491.     }
  492.     // }}}
  493.  
  494.     // {{{ resetFormCounter
  495.     /**
  496.      *  フォームヘルパ用、内部フォームカウンタをリセットする
  497.      *
  498.      *  @access public
  499.      */
  500.     public function resetFormCounter()
  501.     {
  502.         $this->reset_counter = true;
  503.     }
  504.     // }}}
  505.  
  506.     // {{{ getFormName
  507.     /**
  508.      *  指定されたフォーム項目に対応するフォーム名(w/ レンダリング)を取得する
  509.      *
  510.      *  @access public
  511.      */
  512.     public function getFormName($name$action$params)
  513.     {
  514.         $af $this->_getHelperActionForm($action$name);
  515.         if ($af === null{
  516.             return $name;
  517.         }
  518.  
  519.         $def $af->getDef($name);
  520.         if ($def === null || isset($def['name']=== false{
  521.             return $name;
  522.         }
  523.  
  524.         return $def['name'];
  525.     }
  526.     // }}}
  527.  
  528.     // {{{ getFormSubmit
  529.     /**
  530.      *  submitボタンを取得する(送信先アクションで受け取るよう
  531.      *  定義されていないときに、たんにsubmitボタンを作るのに使う)
  532.      *
  533.      *  @access public
  534.      */
  535.     public function getFormSubmit($params)
  536.     {
  537.         if (isset($params['type']=== false{
  538.             $params['type''submit';
  539.         }
  540.         return $this->_getFormInput_Html('input'$params);
  541.     }
  542.     // }}}
  543.  
  544.     // {{{ getFormInput
  545.     /**
  546.      *  指定されたフォーム項目に対応するフォームタグを取得する
  547.      *
  548.      *  @access public
  549.      *  @todo   JavaScript対応
  550.      */
  551.     public function getFormInput($name$action$params)
  552.     {
  553.         $af $this->_getHelperActionForm($action$name);
  554.         if ($af === null{
  555.             return '';
  556.         }
  557.  
  558.         $def $af->getDef($name);
  559.         if ($def === null{
  560.             return '';
  561.         }
  562.  
  563.         if (isset($def['form_type']=== false{
  564.             $def['form_type'FORM_TYPE_TEXT;
  565.         }
  566.  
  567.         // 配列フォームが何回呼ばれたかを保存するカウンタ
  568.         if (isset($def['type']&& is_array($def['type'])) {
  569.             static $form_counter array();
  570.             if ($this->reset_counter{
  571.                 $form_counter array();
  572.                 $this->reset_counter false;
  573.             }
  574.  
  575.             if (isset($form_counter[$action]=== false{
  576.                 $form_counter[$actionarray();
  577.             }
  578.             if (isset($form_counter[$action][$name]=== false{
  579.                 $form_counter[$action][$name0;
  580.             }
  581.             $def['_form_counter'$form_counter[$action][$name]++;
  582.         }
  583.  
  584.         switch ($def['form_type']{
  585.         case FORM_TYPE_BUTTON:
  586.             $input $this->_getFormInput_Button($name$def$params);
  587.             break;
  588.  
  589.         case FORM_TYPE_CHECKBOX:
  590.             $def['option'$this->_getSelectorOptions($af$def$params);
  591.             $input $this->_getFormInput_Checkbox($name$def$params);
  592.             break;
  593.  
  594.         case FORM_TYPE_FILE:
  595.             $input $this->_getFormInput_File($name$def$params);
  596.             break;
  597.  
  598.         case FORM_TYPE_HIDDEN:
  599.             $input $this->_getFormInput_Hidden($name$def$params);
  600.             break;
  601.  
  602.         case FORM_TYPE_PASSWORD:
  603.             $input $this->_getFormInput_Password($name$def$params);
  604.             break;
  605.  
  606.         case FORM_TYPE_RADIO:
  607.             $def['option'$this->_getSelectorOptions($af$def$params);
  608.             $input $this->_getFormInput_Radio($name$def$params);
  609.             break;
  610.  
  611.         case FORM_TYPE_SELECT:
  612.             $def['option'$this->_getSelectorOptions($af$def$params);
  613.             $input $this->_getFormInput_Select($name$def$params);
  614.             break;
  615.  
  616.         case FORM_TYPE_SUBMIT:
  617.             $input $this->_getFormInput_Submit($name$def$params);
  618.             break;
  619.  
  620.         case FORM_TYPE_TEXTAREA:
  621.             $input $this->_getFormInput_Textarea($name$def$params);
  622.             break;
  623.  
  624.         case FORM_TYPE_TEXT:
  625.         default:
  626.             $input $this->_getFormInput_Text($name$def$params);
  627.             break;
  628.         }
  629.  
  630.         return $input;
  631.     }
  632.     // }}}
  633.  
  634.     // {{{ getFormBlock
  635.     /**
  636.      *  フォームタグを取得する(type="form")
  637.      *
  638.      *  @access protected
  639.      */
  640.     public function getFormBlock($content$params)
  641.     {
  642.         // method
  643.         if (isset($params['method']=== false{
  644.             $params['method''post';
  645.         }
  646.  
  647.         return $this->_getFormInput_Html('form'$params$contentfalse);
  648.     }
  649.     // }}}
  650.  
  651.     // {{{ _getSelectorOptions
  652.     /**
  653.      *  select, radio, checkbox の選択肢を取得する
  654.      *
  655.      *  @access protected
  656.      */
  657.     protected function _getSelectorOptions($af$def$params)
  658.     {
  659.         // $params, $def の順で調べる
  660.         $source null;
  661.         if (isset($params['option'])) {
  662.             $source $params['option'];
  663.         else if (isset($def['option'])) {
  664.             $source $def['option'];
  665.         }
  666.  
  667.         // 未定義 or 定義済みの場合はそのまま
  668.         if ($source === null{
  669.             return null;
  670.         else if (is_array($source)) {
  671.             return $source;
  672.         }
  673.         
  674.         // 選択肢を取得
  675.         $options null;
  676.         $split array_map("trim"explode(','$source));
  677.         if (count($split=== 1{
  678.             // アクションフォームから取得
  679.             $method_or_property $split[0];
  680.             if (method_exists($af$method_or_property)) {
  681.                 $options $af->$method_or_property();
  682.             else {
  683.                 $options $af->$method_or_property;
  684.             }
  685.         else {
  686.             // マネージャから取得
  687.             $mgr $this->backend->getManager($split[0]);
  688.             $attr_list $mgr->getAttrList($split[1]);
  689.             if (is_array($attr_list)) {
  690.                 foreach ($attr_list as $key => $val{
  691.                     $options[$key$val['name'];
  692.                 }
  693.             }
  694.         }
  695.  
  696.         if (is_array($options=== false{
  697.             $this->logger->log(LOG_WARNING,
  698.                 'selector option is not valid. [actionform=%s, option=%s]',
  699.                 get_class($af)$source);
  700.             return null;
  701.         }
  702.  
  703.         return $options;
  704.     }
  705.     // }}}
  706.  
  707.     // {{{ _getFormInput_Button
  708.     /**
  709.      *  フォームタグを取得する(type="button")
  710.      *
  711.      *  @access protected
  712.      */
  713.     protected function _getFormInput_Button($name$def$params)
  714.     {
  715.         $params['type''button';
  716.         
  717.         if (isset($def['type'])) {
  718.             $params['name'is_array($def['type']$name '[]' $name;
  719.         else {
  720.             $params['name'$name;
  721.         }
  722.         if (isset($params['value']=== false{
  723.             if (isset($def['name'])) {
  724.                 $params['value'$def['name'];
  725.             }
  726.         }
  727.         if (isset($params['value']&& is_array($params['value'])) {
  728.             $params['value'$params['value'][0];
  729.         }
  730.  
  731.         return $this->_getFormInput_Html('input'$params);
  732.     }
  733.     // }}}
  734.  
  735.     // {{{ _getFormInput_Checkbox
  736.     /**
  737.      *  チェックボックスタグを取得する(type="check")
  738.      *
  739.      *  @access protected
  740.      */
  741.     protected function _getFormInput_Checkbox($name$def$params)
  742.     {
  743.         $params['type''checkbox';
  744.         if (isset($def['type'])) {
  745.             $params['name'is_array($def['type']$name '[]' $name;
  746.         else {
  747.             $params['name'$name;
  748.         }
  749.  
  750.         // オプションの一覧(alist)を取得
  751.         if (isset($def['option']&& is_array($def['option'])) {
  752.             $options $def['option'];
  753.         else {
  754.             $options array();
  755.         }
  756.  
  757.         // default値の設定
  758.         if (isset($params['default'])) {
  759.             $current_value $params['default'];
  760.         else if (isset($def['default'])) {
  761.             $current_value $def['default'];
  762.         else {
  763.             $current_value array();
  764.         }
  765.         $current_value array_map('strval'to_array($current_value));
  766.  
  767.         // タグのセパレータ
  768.         if (isset($params['separator'])) {
  769.             $separator $params['separator'];
  770.         else {
  771.             $separator "\n";
  772.         }
  773.  
  774.         $ret array();
  775.         $i 1;
  776.         foreach ($options as $key => $value{
  777.             $params['value'$key;
  778.             $params['id'$name '_' $i++;
  779.  
  780.             // checked
  781.             if (in_array((string) $key$current_valuetrue)) {
  782.                 $params['checked''checked';
  783.             else {
  784.                 unset($params['checked']);
  785.             }
  786.  
  787.             // <input type="checkbox" />
  788.             $input_tag $this->_getFormInput_Html('input'$params);
  789.  
  790.             // <label for="id">..</label>
  791.             $ret[$this->_getFormInput_Html('label'array('for' => $params['id']),
  792.                                                $input_tag $valuefalse);
  793.         }
  794.  
  795.         return implode($separator$ret);
  796.     }
  797.     // }}}
  798.  
  799.     // {{{ _getFormInput_File
  800.     /**
  801.      *  フォームタグを取得する(type="file")
  802.      *
  803.      *  @access protected
  804.      */
  805.     function _getFormInput_File($name$def$params)
  806.     {
  807.         $params['type''file';
  808.         if (isset($def['type'])) {
  809.             $params['name'is_array($def['type']$name '[]' $name;
  810.         else {
  811.             $params['name'$name;
  812.         }
  813.         $params['value''';
  814.  
  815.         return $this->_getFormInput_Html('input'$params);
  816.     }
  817.     // }}}
  818.  
  819.     // {{{ _getFormInput_Hidden
  820.     /**
  821.      *  フォームタグを取得する(type="hidden")
  822.      *
  823.      *  @access protected
  824.      */
  825.     function _getFormInput_Hidden($name$def$params)
  826.     {
  827.         $params['type''hidden';
  828.         if (isset($def['type'])) {
  829.             $params['name'is_array($def['type']$name '[]' $name;
  830.         else {
  831.             $params['name'$name;
  832.         }
  833.  
  834.         // value
  835.         $value '';
  836.         if (isset($params['value'])) {
  837.             $value $params['value'];
  838.         else if (isset($params['default'])) {
  839.             $value $params['default'];
  840.         else if (isset($def['default'])) {
  841.             $value $def['default'];
  842.         }
  843.         if (is_array($value)) {
  844.             if ($def['_form_counter'count($value)) {
  845.                 $params['value'$value[$def['_form_counter']];
  846.             else {
  847.                 $params['value''';
  848.             }
  849.         else {
  850.             $params['value'$value;
  851.         }
  852.  
  853.         return $this->_getFormInput_Html('input'$params);
  854.     }
  855.     // }}}
  856.  
  857.     // {{{ _getFormInput_Password
  858.     /**
  859.      *  フォームタグを取得する(type="password")
  860.      *
  861.      *  @access protected
  862.      */
  863.     function _getFormInput_Password($name$def$params)
  864.     {
  865.         $params['type''password';
  866.         if (isset($def['type'])) {
  867.             $params['name'is_array($def['type']$name '[]' $name;
  868.         else {
  869.             $params['name'$name;
  870.         }
  871.  
  872.         // value
  873.         $value '';
  874.         if (isset($params['value'])) {
  875.             $value $params['value'];
  876.         else if (isset($params['default'])) {
  877.             $value $params['default'];
  878.         else if (isset($def['default'])) {
  879.             $value $def['default'];
  880.         }
  881.         if (is_array($value)) {
  882.             if ($def['_form_counter'count($value)) {
  883.                 $params['value'$value[$def['_form_counter']];
  884.             else {
  885.                 $params['value''';
  886.             }
  887.         else {
  888.             $params['value'$value;
  889.         }
  890.  
  891.         //   maxlength と フォーム定義のmax連携はサポートしない
  892.         //   @see http://sourceforge.jp/ticket/browse.php?group_id=1343&tid=16325
  893.  
  894.         return $this->_getFormInput_Html('input'$params);
  895.     }
  896.     // }}}
  897.  
  898.     // {{{ _getFormInput_Radio
  899.     /**
  900.      *  ラジオボタンタグを取得する(type="radio")
  901.      *
  902.      *  @access protected
  903.      */
  904.     function _getFormInput_Radio($name$def$params)
  905.     {
  906.         $params['type''radio';
  907.         if (isset($def['type'])) {
  908.             $params['name'is_array($def['type']$name '[]' $name;
  909.         else {
  910.             $params['name'$name;
  911.         }
  912.  
  913.         // オプションの一覧(alist)を取得
  914.         if (isset($def['option']&& is_array($def['option'])) {
  915.             $options $def['option'];
  916.         else {
  917.             $options array();
  918.         }
  919.  
  920.         // default値の設定
  921.         if (isset($params['default'])) {
  922.             $current_value $params['default'];
  923.         else if (isset($def['default'])) {
  924.             $current_value $def['default'];
  925.         else {
  926.             $current_value null;
  927.         }
  928.  
  929.         // タグのセパレータ
  930.         if (isset($params['separator'])) {
  931.             $separator $params['separator'];
  932.         else {
  933.             $separator "\n";
  934.         }
  935.  
  936.         $ret array();
  937.         $i 1;
  938.         foreach ($options as $key => $value{
  939.             $params['value'$key;
  940.             $params['id'$name '_' $i++;
  941.  
  942.             // checked
  943.             if (strcmp($current_value,$key=== 0{
  944.                 $params['checked''checked';
  945.             else {
  946.                 unset($params['checked']);
  947.             }
  948.  
  949.             // <input type="radio" />
  950.             $input_tag $this->_getFormInput_Html('input'$params);
  951.  
  952.             // <label for="id">..</label>
  953.             $ret[$this->_getFormInput_Html('label'array('for' => $params['id']),
  954.                                                $input_tag $valuefalse);
  955.         }
  956.  
  957.         return implode($separator$ret);
  958.     }
  959.     // }}}
  960.  
  961.     // {{{ _getFormInput_Select
  962.     /**
  963.      *  セレクトボックスタグを取得する(type="select")
  964.      *
  965.      *  @access protected
  966.      */
  967.     function _getFormInput_Select($name$def$params)
  968.     {
  969.         if (isset($def['type'])) {
  970.             $params['name'is_array($def['type']$name '[]' $name;
  971.         else {
  972.             $params['name'$name;
  973.         }
  974.  
  975.         // オプションの一覧(alist)を取得
  976.         if (isset($def['option']&& is_array($def['option'])) {
  977.             $options $def['option'];
  978.         else {
  979.             $options array();
  980.         }
  981.  
  982.         // default値の設定
  983.         if (isset($params['default'])) {
  984.             $current_value $params['default'];
  985.         else if (isset($def['default'])) {
  986.             $current_value $def['default'];
  987.         else {
  988.             $current_value array(=> 0,);
  989.         }
  990.         $current_value array_map('strval'to_array($current_value));
  991.  
  992.         // タグのセパレータ
  993.         if (isset($params['separator'])) {
  994.             $separator $params['separator'];
  995.         else {
  996.             $separator "\n";
  997.         }
  998.  
  999.         // selectタグの中身を作る
  1000.         $contents array();
  1001.         $selected false;
  1002.         foreach ($options as $key => $value{
  1003.             $attr array('value' => $key);
  1004.             $def['_form_counter'empty($def['_form_counter']$def['_form_counter'];
  1005.             if (isset($params['multiple']&&
  1006.                     in_array((string)$key$current_valuetrue||
  1007.                !isset($params['multiple']&& $selected === false &&
  1008.                     strcmp($current_value[$def['_form_counter']]$key=== 0{
  1009.                 $attr['selected''selected';
  1010.                 $selected true;
  1011.             }
  1012.             $contents[$this->_getFormInput_Html('option'$attr$value);
  1013.         }
  1014.  
  1015.         // 空エントリ
  1016.         if (isset($params['emptyoption'])) {
  1017.             $attr array('value' => '');
  1018.             if ($selected === false{
  1019.                 $attr['selected''selected';
  1020.             }
  1021.             array_unshift($contents,
  1022.                           $this->_getFormInput_Html('option',
  1023.                                                     $attr,
  1024.                                                     $params['emptyoption']));
  1025.             unset($params['emptyoption']);
  1026.         }
  1027.  
  1028.         $element $separator implode($separator$contents$separator;
  1029.         return $this->_getFormInput_Html('select'$params$elementfalse);
  1030.     }
  1031.     // }}}
  1032.  
  1033.     // {{{ _getFormInput_Submit
  1034.     /**
  1035.      *  フォームタグを取得する(type="submit")
  1036.      *
  1037.      *  @access protected
  1038.      */
  1039.     function _getFormInput_Submit($name$def$params)
  1040.     {
  1041.         $params['type''submit';
  1042.         if (isset($def['type'])) {
  1043.             $params['name'is_array($def['type']$name '[]' $name;
  1044.         else {
  1045.             $params['name'$name;
  1046.         }
  1047.         if (isset($params['value']=== false{
  1048.             if (isset($def['name'])) {
  1049.                 $params['value'$def['name'];
  1050.             }
  1051.         }
  1052.         if (is_array($params['value'])) {
  1053.             $params['value'$params['value'][0];
  1054.         }
  1055.  
  1056.         return $this->_getFormInput_Html('input'$params);
  1057.     }
  1058.     // }}}
  1059.  
  1060.     // {{{ _getFormInput_Textarea
  1061.     /**
  1062.      *  フォームタグを取得する(textarea)
  1063.      *
  1064.      *  @access protected
  1065.      */
  1066.     function _getFormInput_Textarea($name$def$params)
  1067.     {
  1068.         if (isset($def['type'])) {
  1069.             $params['name'is_array($def['type']$name '[]' $name;
  1070.         else {
  1071.             $params['name'$name;
  1072.         }
  1073.  
  1074.         // element
  1075.         $element '';
  1076.         if (isset($params['value'])) {
  1077.             $element $params['value'];
  1078.         else if (isset($params['default'])) {
  1079.             $element $params['default'];
  1080.         else if (isset($def['default'])) {
  1081.             $element $def['default'];
  1082.         }
  1083.         if (is_array($element)) {
  1084.             if ($def['_form_counter'count($element)) {
  1085.                 $element $element[$def['_form_counter']];
  1086.             else {
  1087.                 $element '';
  1088.             }
  1089.         }
  1090.  
  1091.         return $this->_getFormInput_Html('textarea'$params$element);
  1092.     }
  1093.     // }}}
  1094.  
  1095.     // {{{ _getFormInput_Text
  1096.     /**
  1097.      *  フォームタグを取得する(type="text")
  1098.      *
  1099.      *  @access protected
  1100.      */
  1101.     function _getFormInput_Text($name$def$params)
  1102.     {
  1103.         // type
  1104.         $params['type''text';
  1105.  
  1106.         // name
  1107.         if (isset($def['type'])) {
  1108.             $params['name'is_array($def['type']$name '[]' $name;
  1109.         else {
  1110.             $params['name'$name;
  1111.         }
  1112.  
  1113.         // value
  1114.         $value '';
  1115.         if (isset($params['value'])) {
  1116.             $value $params['value'];
  1117.         else if (isset($params['default'])) {
  1118.             $value $params['default'];
  1119.         else if (isset($def['default'])) {
  1120.             $value $def['default'];
  1121.         }
  1122.         if (is_array($value)) {
  1123.             if ($def['_form_counter'count($value)) {
  1124.                 $params['value'$value[$def['_form_counter']];
  1125.             else {
  1126.                 $params['value''';
  1127.             }
  1128.         else {
  1129.             $params['value'$value;
  1130.         }
  1131.  
  1132.         //   maxlength と フォーム定義のmax連携はサポートしない
  1133.         //   @see http://sourceforge.jp/ticket/browse.php?group_id=1343&tid=16325
  1134.  
  1135.         return $this->_getFormInput_Html('input'$params);
  1136.     }
  1137.     // }}}
  1138.  
  1139.     // {{{ _getFormInput_Html
  1140.     /**
  1141.      *  HTMLタグを取得する
  1142.      *
  1143.      *  @access protected
  1144.      */
  1145.     function _getFormInput_Html($tag$attr$element null$escape_element true)
  1146.     {
  1147.         // 不要なパラメータは消す
  1148.         foreach ($this->helper_parameter_keys as $key{
  1149.             unset($attr[$key]);
  1150.         }
  1151.  
  1152.         $r "<$tag";
  1153.  
  1154.         foreach ($attr as $key => $value{
  1155.             if ($value === null{
  1156.                 $r .= sprintf(' %s'$key);
  1157.             else {
  1158.                 $r .= sprintf(' %s="%s"'$keyhtmlspecialchars($valueENT_QUOTES));
  1159.             }
  1160.         }
  1161.  
  1162.         if ($element === null{
  1163.             $r .= ' />';
  1164.         else if ($escape_element{
  1165.             $r .= sprintf('>%s</%s>'htmlspecialchars($elementENT_QUOTES)$tag);
  1166.         else {
  1167.             $r .= sprintf('>%s</%s>'$element$tag);
  1168.         }
  1169.  
  1170.         return $r;
  1171.     }
  1172.     // }}}
  1173.  
  1174.     // {{{ _getRenderer
  1175.     /**
  1176.      *  レンダラオブジェクトを取得する
  1177.      *
  1178.      *  @access protected
  1179.      *  @return object  Ethna_Renderer  レンダラオブジェクト
  1180.      */
  1181.     function _getRenderer()
  1182.     {
  1183.         $c $this->backend->getController();
  1184.         $renderer $c->getRenderer();
  1185.  
  1186.         $form_array $this->af->getArray();
  1187.         $app_array $this->af->getAppArray();
  1188.         $app_ne_array $this->af->getAppNEArray();
  1189.         $renderer->setPropByRef('form'$form_array);
  1190.         $renderer->setPropByRef('app'$app_array);
  1191.         $renderer->setPropByRef('app_ne'$app_ne_array);
  1192.         $message_list Ethna_Util::escapeHtml($this->ae->getMessageList());
  1193.         $renderer->setPropByRef('errors'$message_list);
  1194.         if (isset($_SESSION)) {
  1195.             $tmp_session Ethna_Util::escapeHtml($_SESSION);
  1196.             $renderer->setPropByRef('session'$tmp_session);
  1197.         }
  1198.         $renderer->setProp('script',
  1199.             htmlspecialchars(basename($_SERVER['SCRIPT_NAME'])ENT_QUOTES));
  1200.         $renderer->setProp('request_uri',
  1201.             isset($_SERVER['REQUEST_URI'])
  1202.             ? htmlspecialchars($_SERVER['REQUEST_URI']ENT_QUOTES)
  1203.             : '');
  1204.         $renderer->setProp('config'$this->config->get());
  1205.  
  1206.         return $renderer;
  1207.     }
  1208.     // }}}
  1209.  
  1210.     // {{{ _setDefault
  1211.     /**
  1212.      *  共通値を設定する
  1213.      *
  1214.      *  @access protected
  1215.      *  @param  object  Ethna_Renderer  レンダラオブジェクト
  1216.      */
  1217.     protected function _setDefault($renderer)
  1218.     {
  1219.     }
  1220.     // }}}
  1221. }
  1222. // }}}

Documentation generated on Fri, 11 Nov 2011 03:59:25 +0900 by phpDocumentor 1.4.3