OSDN Git Service

20c329fb3c1b597488f999ca4a4e586632f5a37f
[ethna/ethna.git] / class / Ethna_ViewClass.php
1 <?php
2 // vim: foldmethod=marker
3 /**
4  *  Ethna_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$
10  */
11
12 // {{{ Ethna_ViewClass
13 /**
14  *  view¥¯¥é¥¹
15  *
16  *  @author     Masaki Fujimoto <fujimoto@php.net>
17  *  @access     public
18  *  @package    Ethna
19  */
20 class Ethna_ViewClass
21 {
22     /**#@+
23      *  @access private
24      */
25
26     /** @var    object  Ethna_Backend       backend¥ª¥Ö¥¸¥§¥¯¥È */
27     var $backend;
28
29     /** @var    object  Ethna_Config        ÀßÄꥪ¥Ö¥¸¥§¥¯¥È    */
30     var $config;
31
32     /** @var    object  Ethna_I18N          i18n¥ª¥Ö¥¸¥§¥¯¥È */
33     var $i18n;
34
35     /** @var    object  Ethna_Logger    ¥í¥°¥ª¥Ö¥¸¥§¥¯¥È */
36     var $logger;
37
38     /** @var    object  Ethna_Plugin    ¥×¥é¥°¥¤¥ó¥ª¥Ö¥¸¥§¥¯¥È */
39     var $plugin;
40
41     /** @var    object  Ethna_ActionError   ¥¢¥¯¥·¥ç¥ó¥¨¥é¡¼¥ª¥Ö¥¸¥§¥¯¥È */
42     var $action_error;
43
44     /** @var    object  Ethna_ActionError   ¥¢¥¯¥·¥ç¥ó¥¨¥é¡¼¥ª¥Ö¥¸¥§¥¯¥È(¾Êά·Á) */
45     var $ae;
46
47     /** @var    object  Ethna_ActionForm    ¥¢¥¯¥·¥ç¥ó¥Õ¥©¡¼¥à¥ª¥Ö¥¸¥§¥¯¥È */
48     var $action_form;
49
50     /** @var    object  Ethna_ActionForm    ¥¢¥¯¥·¥ç¥ó¥Õ¥©¡¼¥à¥ª¥Ö¥¸¥§¥¯¥È(¾Êά·Á) */
51     var $af;
52
53     /** @var    array   ¥¢¥¯¥·¥ç¥ó¥Õ¥©¡¼¥à¥ª¥Ö¥¸¥§¥¯¥È(helper) */
54     var $helper_action_form = array();
55
56     /** @var    array   helper¤Çhtml¤Îattribute¤Ë¤Ï¤·¤Ê¥Ñ¥é¥á¡¼¥¿¤Î°ìÍ÷ */
57     var $helper_parameter_keys = array('default', 'option', 'separator');
58
59     /** @var    object  Ethna_Session       ¥»¥Ã¥·¥ç¥ó¥ª¥Ö¥¸¥§¥¯¥È */
60     var $session;
61
62     /** @var    string  Á«°Ü̾ */
63     var $forward_name;
64
65     /** @var    string  Á«°ÜÀè¥Æ¥ó¥×¥ì¡¼¥È¥Õ¥¡¥¤¥ë̾ */
66     var $forward_path;
67
68     /**#@-*/
69
70     // {{{ Ethna_ViewClass
71     /**
72      *  Ethna_ViewClass¤Î¥³¥ó¥¹¥È¥é¥¯¥¿
73      *
74      *  @access public
75      *  @param  object  Ethna_Backend   $backend    backend¥ª¥Ö¥¸¥§¥¯¥È
76      *  @param  string  $forward_name   ¥Ó¥å¡¼¤Ë´ØÏ¢ÉÕ¤±¤é¤ì¤Æ¤¤¤ëÁ«°Ü̾
77      *  @param  string  $forward_path   ¥Ó¥å¡¼¤Ë´ØÏ¢ÉÕ¤±¤é¤ì¤Æ¤¤¤ë¥Æ¥ó¥×¥ì¡¼¥È¥Õ¥¡¥¤¥ë̾
78      */
79     function Ethna_ViewClass(&$backend, $forward_name, $forward_path)
80     {
81         $c =& $backend->getController();
82         $this->backend =& $backend;
83         $this->config =& $this->backend->getConfig();
84         $this->i18n =& $this->backend->getI18N();
85         $this->logger =& $this->backend->getLogger();
86         $this->plugin =& $this->backend->getPlugin();
87
88         $this->action_error =& $this->backend->getActionError();
89         $this->ae =& $this->action_error;
90
91         $this->action_form =& $this->backend->getActionForm();
92         $this->af =& $this->action_form;
93
94         $this->session =& $this->backend->getSession();
95
96         $this->forward_name = $forward_name;
97         $this->forward_path = $forward_path;
98
99         foreach (array_keys($this->helper_action_form) as $action) {
100             $this->addActionFormHelper($action);
101         }
102     }
103     // }}}
104
105     // {{{ preforward
106     /**
107      *  ²èÌÌɽ¼¨Á°½èÍý
108      *
109      *  ¥Æ¥ó¥×¥ì¡¼¥È¤ËÀßÄꤹ¤ëÃͤǥ³¥ó¥Æ¥­¥¹¥È¤Ë°Í¸¤·¤Ê¤¤¤â¤Î¤Ï
110      *  ¤³¤³¤ÇÀßÄꤹ¤ë(Îã:¥»¥ì¥¯¥È¥Ü¥Ã¥¯¥¹Åù)
111      *
112      *  @access public
113      */
114     function preforward()
115     {
116     }
117     // }}}
118
119     // {{{ forward
120     /**
121      *  Á«°Ü̾¤ËÂбþ¤¹¤ë²èÌ̤ò½ÐÎϤ¹¤ë
122      *
123      *  Æüì¤Ê²èÌ̤òɽ¼¨¤¹¤ë¾ì¹ç¤ò½ü¤¤¤ÆÆä˥ª¡¼¥Ð¡¼¥é¥¤¥É¤¹¤ëɬÍפÏ̵¤¤
124      *  (preforward()¤Î¤ß¥ª¡¼¥Ð¡¼¥é¥¤¥É¤¹¤ì¤ÐÎɤ¤)
125      *
126      *  @access public
127      */
128     function forward()
129     {
130         $renderer =& $this->_getRenderer();
131         $this->_setDefault($renderer);
132         $renderer->perform($this->forward_path);
133     }
134     // }}}
135
136     // {{{ addActionFormHelper
137     /**
138      *  helper¥¢¥¯¥·¥ç¥ó¥Õ¥©¡¼¥à¥ª¥Ö¥¸¥§¥¯¥È¤òÀßÄꤹ¤ë
139      *
140      *  @access public
141      */
142     function addActionFormHelper($action)
143     {
144         if (isset($this->helper_action_form[$action])
145             && is_object($this->helper_action_form[$action])) {
146             return;
147         }
148
149         $ctl =& Ethna_Controller::getInstance();
150         if ($action === $ctl->getCurrentActionName()) {
151             $this->helper_action_form[$action] =& $this->af;
152             return;
153         }
154
155         $form_name = $ctl->getActionFormName($action);
156         if ($form_name === null) {
157             $this->logger->log(LOG_WARNING,
158                 'action form for the action [%s] not found.', $action);
159             return;
160         }
161
162         $this->helper_action_form[$action] =& new $form_name($ctl);
163     }
164     // }}}
165
166     // {{{ clearActionFormHelper
167     /**
168      *  helper¥¢¥¯¥·¥ç¥ó¥Õ¥©¡¼¥à¥ª¥Ö¥¸¥§¥¯¥È¤òºï½ü¤¹¤ë
169      *
170      *  @access public
171      */
172     function clearActionFormHelper($action)
173     {
174         unset($this->helper_action_form[$action]);
175     }
176     // }}}
177
178     // {{{ _getHelperActionForm
179     /**
180      *  ¥¢¥¯¥·¥ç¥ó¥Õ¥©¡¼¥à¥ª¥Ö¥¸¥§¥¯¥È(helper)¤ò¼èÆÀ¤¹¤ë
181      *  $action === null ¤Ç $name ¤¬»ØÄꤵ¤ì¤Æ¤¤¤ë¤È¤­¤Ï¡¢$name¤ÎÄêµÁ¤ò
182      *  ´Þ¤à¤â¤Î¤òõ¤¹
183      *
184      *  @access protected
185      *  @param  string  action  ¼èÆÀ¤¹¤ë¥¢¥¯¥·¥ç¥ó̾
186      *  @param  string  name    ÄêµÁ¤µ¤ì¤Æ¤¤¤ë¤³¤È¤ò´üÂÔ¤¹¤ë¥Õ¥©¡¼¥à̾
187      *  @return object  Ethna_ActionForm¤Þ¤¿¤Ï·Ñ¾µ¥ª¥Ö¥¸¥§¥¯¥È
188      */
189     function &_getHelperActionForm($action = null, $name = null)
190     {
191         // $action ¤¬»ØÄꤵ¤ì¤Æ¤¤¤ë¾ì¹ç
192         if ($action !== null) {
193             if (isset($this->helper_action_form[$action])
194                 && is_object($this->helper_action_form[$action])) {
195                 return $this->helper_action_form[$action];
196             } else {
197                 $this->logger->log(LOG_WARNING,
198                     'helper action form for action [%s] not found',
199                     $action);
200                 return null;
201             }
202         }
203
204         // ºÇ½é¤Ë $this->af ¤òÄ´¤Ù¤ë
205         $def = $this->af->getDef($name);
206         if ($def !== null) {
207             return $this->af;
208         }
209
210         // $this->helper_action_form ¤ò½ç¤ËÄ´¤Ù¤ë
211         foreach (array_keys($this->helper_action_form) as $action) {
212             if (is_object($this->helper_action_form[$action]) === false) {
213                 continue;
214             }
215             $af =& $this->helper_action_form[$action];
216             $def = $af->getDef($name);
217             if (is_null($def) === false) {
218                 return $af;
219             }
220         }
221
222         // ¸«ÉÕ¤«¤é¤Ê¤«¤Ã¤¿
223         $this->logger->log(LOG_WARNING,
224             'action form defining form [%s] not found', $name);
225         return null;
226     }
227     // }}}
228
229     // {{{ getFormName
230     /**
231      *  »ØÄꤵ¤ì¤¿¥Õ¥©¡¼¥à¹àÌܤËÂбþ¤¹¤ë¥Õ¥©¡¼¥à̾(w/ ¥ì¥ó¥À¥ê¥ó¥°)¤ò¼èÆÀ¤¹¤ë
232      *
233      *  @access public
234      */
235     function getFormName($name, $action, $params)
236     {
237         $af =& $this->_getHelperActionForm($action, $name);
238         if ($af === null) {
239             return $name;
240         }
241
242         $def = $af->getDef($name);
243         if ($def === null || isset($def['name']) === false) {
244             return $name;
245         }
246
247         return $def['name'];
248     }
249     // }}}
250
251     // {{{ getFormSubmit
252     /**
253      *  submit¥Ü¥¿¥ó¤ò¼èÆÀ¤¹¤ë(Á÷¿®À襢¥¯¥·¥ç¥ó¤Ç¼õ¤±¼è¤ë¤è¤¦
254      *  ÄêµÁ¤µ¤ì¤Æ¤¤¤Ê¤¤¤È¤­¤Ë¡¢¤¿¤ó¤Ësubmit¥Ü¥¿¥ó¤òºî¤ë¤Î¤Ë»È¤¦)
255      *
256      *  @access public
257      */
258     function getFormSubmit($params)
259     {
260         if (isset($params['type']) === false) {
261             $params['type'] = 'submit';
262         }
263         return $this->_getFormInput_Html('input', $params);
264     }
265     // }}}
266
267     // {{{ getFormInput
268     /**
269      *  »ØÄꤵ¤ì¤¿¥Õ¥©¡¼¥à¹àÌܤËÂбþ¤¹¤ë¥Õ¥©¡¼¥à¥¿¥°¤ò¼èÆÀ¤¹¤ë
270      *
271      *  @access public
272      *  @todo   JavaScriptÂбþ
273      */
274     function getFormInput($name, $action, $params)
275     {
276         $af =& $this->_getHelperActionForm($action, $name);
277         if ($af === null) {
278             return '';
279         }
280
281         $def = $af->getDef($name);
282         if ($def === null) {
283             return '';
284         }
285
286         if (isset($def['form_type']) === false) {
287             $def['form_type'] = FORM_TYPE_TEXT;
288         }
289
290         // ÇÛÎó¥Õ¥©¡¼¥à¤¬²¿²ó¸Æ¤Ð¤ì¤¿¤«¤òÊݸ¤¹¤ë¥«¥¦¥ó¥¿
291         if (isset($def['type']) && is_array($def['type'])) {
292             static $form_counter = array();
293             if (isset($form_counter[$action]) === false) {
294                 $form_counter[$action] = array();
295             }
296             if (isset($form_counter[$action][$name]) === false) {
297                 $form_counter[$action][$name] = 0;
298             }
299             $def['_form_counter'] = $form_counter[$action][$name]++;
300         }
301
302         switch ($def['form_type']) {
303         case FORM_TYPE_BUTTON:
304             $input = $this->_getFormInput_Button($name, $def, $params);
305             break;
306
307         case FORM_TYPE_CHECKBOX:
308             $def['option'] = $this->_getSelectorOptions($af, $def, $params);
309             $input = $this->_getFormInput_Checkbox($name, $def, $params);
310             break;
311
312         case FORM_TYPE_FILE:
313             $input = $this->_getFormInput_File($name, $def, $params);
314             break;
315
316         case FORM_TYPE_HIDDEN:
317             $input = $this->_getFormInput_Hidden($name, $def, $params);
318             break;
319
320         case FORM_TYPE_PASSWORD:
321             $input = $this->_getFormInput_Password($name, $def, $params);
322             break;
323
324         case FORM_TYPE_RADIO:
325             $def['option'] = $this->_getSelectorOptions($af, $def, $params);
326             $input = $this->_getFormInput_Radio($name, $def, $params);
327             break;
328
329         case FORM_TYPE_SELECT:
330             $def['option'] = $this->_getSelectorOptions($af, $def, $params);
331             $input = $this->_getFormInput_Select($name, $def, $params);
332             break;
333
334         case FORM_TYPE_SUBMIT:
335             $input = $this->_getFormInput_Submit($name, $def, $params);
336             break;
337
338         case FORM_TYPE_TEXTAREA:
339             $input = $this->_getFormInput_Textarea($name, $def, $params);
340             break;
341
342         case FORM_TYPE_TEXT:
343         default:
344             $input = $this->_getFormInput_Text($name, $def, $params);
345             break;
346         }
347
348         return $input;
349     }
350     // }}}
351
352     // {{{ getFormBlock
353     /**
354      *  ¥Õ¥©¡¼¥à¥¿¥°¤ò¼èÆÀ¤¹¤ë(type="form")
355      *
356      *  @access protected
357      */
358     function getFormBlock($content, $params)
359     {
360         // method
361         if (isset($params['method']) === false) {
362             $params['method'] = 'post';
363         }
364
365         return $this->_getFormInput_Html('form', $params, $content, false);
366     }
367     // }}}
368
369     // {{{ _getSelectorOptions
370     /**
371      *  select, radio, checkbox ¤ÎÁªÂò»è¤ò¼èÆÀ¤¹¤ë
372      *
373      *  @access protected
374      */
375     function _getSelectorOptions(&$af, $def, $params)
376     {
377         // $params, $def ¤Î½ç¤ÇÄ´¤Ù¤ë
378         $source = null;
379         if (isset($params['option'])) {
380             $source = $params['option'];
381         } else if (isset($def['option'])) {
382             $source = $def['option'];
383         }
384
385         // Ì¤ÄêµÁ or ÄêµÁºÑ¤ß¤Î¾ì¹ç¤Ï¤½¤Î¤Þ¤Þ
386         if ($source === null) {
387             return null;
388         } else if (is_array($source)) {
389             return $source;
390         }
391         
392         // ÁªÂò»è¤ò¼èÆÀ
393         $options = null;
394         $split = array_map("trim", explode(',', $source));
395         if (count($split) === 1) {
396             // ¥¢¥¯¥·¥ç¥ó¥Õ¥©¡¼¥à¤«¤é¼èÆÀ
397             $method_or_property = $split[0];
398             if (method_exists($af, $method_or_property)) {
399                 $options = $af->$method_or_property();
400             } else {
401                 $options = $af->$method_or_property;
402             }
403         } else {
404             // ¥Þ¥Í¡¼¥¸¥ã¤«¤é¼èÆÀ
405             $mgr =& $this->backend->getManager($split[0]);
406             $attr_list = $mgr->getAttrList($split[1]);
407             if (is_array($attr_list)) {
408                 foreach ($attr_list as $key => $val) {
409                     $options[$key] = $val['name'];
410                 }
411             }
412         }
413
414         if (is_array($options) === false) {
415             $this->logger->log(LOG_WARNING,
416                 'selector option is not valid. [actionform=%s, option=%s]',
417                 get_class($af), $source);
418             return null;
419         }
420
421         return $options;
422     }
423     // }}}
424
425     // {{{ _getFormInput_Button
426     /**
427      *  ¥Õ¥©¡¼¥à¥¿¥°¤ò¼èÆÀ¤¹¤ë(type="button")
428      *
429      *  @access protected
430      */
431     function _getFormInput_Button($name, $def, $params)
432     {
433         $params['type'] = 'button';
434         
435         if (isset($def['type'])) {
436             $params['name'] = is_array($def['type']) ? $name . '[]' : $name;
437         } else {
438             $params['name'] = $name;
439         }
440         if (isset($params['value']) === false) {
441             if (isset($def['name'])) {
442                 $params['value'] = $def['name'];
443             }
444         }
445         if (isset($params['value']) && is_array($params['value'])) {
446             $params['value'] = $params['value'][0];
447         }
448
449         return $this->_getFormInput_Html('input', $params);
450     }
451     // }}}
452
453     // {{{ _getFormInput_Checkbox
454     /**
455      *  ¥Á¥§¥Ã¥¯¥Ü¥Ã¥¯¥¹¥¿¥°¤ò¼èÆÀ¤¹¤ë(type="check")
456      *
457      *  @access protected
458      */
459     function _getFormInput_Checkbox($name, $def, $params)
460     {
461         $params['type'] = 'checkbox';
462         if (isset($def['type'])) {
463             $params['name'] = is_array($def['type']) ? $name . '[]' : $name;
464         } else {
465             $params['name'] = $name;
466         }
467
468         // ¥ª¥×¥·¥ç¥ó¤Î°ìÍ÷(alist)¤ò¼èÆÀ
469         if (isset($def['option']) && is_array($def['option'])) {
470             $options = $def['option'];
471         } else {
472             $options = array();
473         }
474
475         // defaultÃͤÎÀßÄê
476         if (isset($params['default'])) {
477             $current_value = $params['default'];
478         } else if (isset($def['default'])) {
479             $current_value = $def['default'];
480         } else {
481             $current_value = array();
482         }
483         $current_value = array_map('strval', to_array($current_value));
484
485         // ¥¿¥°¤Î¥»¥Ñ¥ì¡¼¥¿
486         if (isset($params['separator'])) {
487             $separator = $params['separator'];
488         } else {
489             $separator = "\n";
490         }
491
492         $ret = array();
493         $i = 1;
494         foreach ($options as $key => $value) {
495             $params['value'] = $key;
496             $params['id'] = $name . '_' . $i++;
497
498             // checked
499             if (in_array((string) $key, $current_value, true)) {
500                 $params['checked'] = 'checked';
501             } else {
502                 unset($params['checked']);
503             }
504
505             // <input type="checkbox" />
506             $input_tag = $this->_getFormInput_Html('input', $params);
507
508             // <label for="id">..</label>
509             $ret[] = $this->_getFormInput_Html('label', array('for' => $params['id']),
510                                                $input_tag . $value, false);
511         }
512
513         return implode($separator, $ret);
514     }
515     // }}}
516
517     // {{{ _getFormInput_File
518     /**
519      *  ¥Õ¥©¡¼¥à¥¿¥°¤ò¼èÆÀ¤¹¤ë(type="file")
520      *
521      *  @access protected
522      */
523     function _getFormInput_File($name, $def, $params)
524     {
525         $params['type'] = 'file';
526         if (isset($def['type'])) {
527             $params['name'] = is_array($def['type']) ? $name . '[]' : $name;
528         } else {
529             $params['name'] = $name;
530         }
531         $params['value'] = '';
532
533         return $this->_getFormInput_Html('input', $params);
534     }
535     // }}}
536
537     // {{{ _getFormInput_Hidden
538     /**
539      *  ¥Õ¥©¡¼¥à¥¿¥°¤ò¼èÆÀ¤¹¤ë(type="hidden")
540      *
541      *  @access protected
542      */
543     function _getFormInput_Hidden($name, $def, $params)
544     {
545         $params['type'] = 'hidden';
546         if (isset($def['type'])) {
547             $params['name'] = is_array($def['type']) ? $name . '[]' : $name;
548         } else {
549             $params['name'] = $name;
550         }
551
552         // value
553         $value = '';
554         if (isset($params['value'])) {
555             $value = $params['value'];
556         } else if (isset($params['default'])) {
557             $value = $params['default'];
558         } else if (isset($def['default'])) {
559             $value = $def['default'];
560         }
561         if (is_array($value)) {
562             if ($def['_form_counter'] < count($value)) {
563                 $params['value'] = $value[$def['_form_counter']];
564             } else {
565                 $params['value'] = '';
566             }
567         } else {
568             $params['value'] = $value;
569         }
570
571         return $this->_getFormInput_Html('input', $params);
572     }
573     // }}}
574
575     // {{{ _getFormInput_Password
576     /**
577      *  ¥Õ¥©¡¼¥à¥¿¥°¤ò¼èÆÀ¤¹¤ë(type="password")
578      *
579      *  @access protected
580      */
581     function _getFormInput_Password($name, $def, $params)
582     {
583         $params['type'] = 'password';
584         if (isset($def['type'])) {
585             $params['name'] = is_array($def['type']) ? $name . '[]' : $name;
586         } else {
587             $params['name'] = $name;
588         }
589
590         // value
591         $value = '';
592         if (isset($params['value'])) {
593             $value = $params['value'];
594         } else if (isset($params['default'])) {
595             $value = $params['default'];
596         } else if (isset($def['default'])) {
597             $value = $def['default'];
598         }
599         if (is_array($value)) {
600             if ($def['_form_counter'] < count($value)) {
601                 $params['value'] = $value[$def['_form_counter']];
602             } else {
603                 $params['value'] = '';
604             }
605         } else {
606             $params['value'] = $value;
607         }
608
609         // maxlength
610         if (isset($def['max']) && $def['max']) {
611             $params['maxlength'] = $def['max'];
612         }
613
614         return $this->_getFormInput_Html('input', $params);
615     }
616     // }}}
617
618     // {{{ _getFormInput_Radio
619     /**
620      *  ¥é¥¸¥ª¥Ü¥¿¥ó¥¿¥°¤ò¼èÆÀ¤¹¤ë(type="radio")
621      *
622      *  @access protected
623      */
624     function _getFormInput_Radio($name, $def, $params)
625     {
626         $params['type'] = 'radio';
627         if (isset($def['type'])) {
628             $params['name'] = is_array($def['type']) ? $name . '[]' : $name;
629         } else {
630             $params['name'] = $name;
631         }
632
633         // ¥ª¥×¥·¥ç¥ó¤Î°ìÍ÷(alist)¤ò¼èÆÀ
634         if (isset($def['option']) && is_array($def['option'])) {
635             $options = $def['option'];
636         } else {
637             $options = array();
638         }
639
640         // defaultÃͤÎÀßÄê
641         if (isset($params['default'])) {
642             $current_value = $params['default'];
643         } else if (isset($def['default'])) {
644             $current_value = $def['default'];
645         } else {
646             $current_value = null;
647         }
648
649         // ¥¿¥°¤Î¥»¥Ñ¥ì¡¼¥¿
650         if (isset($params['separator'])) {
651             $separator = $params['separator'];
652         } else {
653             $separator = "\n";
654         }
655
656         $ret = array();
657         $i = 1;
658         foreach ($options as $key => $value) {
659             $params['value'] = $key;
660             $params['id'] = $name . '_' . $i++;
661
662             // checked
663             if (strcmp($current_value,$key) === 0) {
664                 $params['checked'] = 'checked';
665             } else {
666                 unset($params['checked']);
667             }
668
669             // <input type="radio" />
670             $input_tag = $this->_getFormInput_Html('input', $params);
671
672             // <label for="id">..</label>
673             $ret[] = $this->_getFormInput_Html('label', array('for' => $params['id']),
674                                                $input_tag . $value, false);
675         }
676
677         return implode($separator, $ret);
678     }
679     // }}}
680
681     // {{{ _getFormInput_Select
682     /**
683      *  ¥»¥ì¥¯¥È¥Ü¥Ã¥¯¥¹¥¿¥°¤ò¼èÆÀ¤¹¤ë(type="select")
684      *
685      *  @access protected
686      */
687     function _getFormInput_Select($name, $def, $params)
688     {
689         if (isset($def['type'])) {
690             $params['name'] = is_array($def['type']) ? $name . '[]' : $name;
691         } else {
692             $params['name'] = $name;
693         }
694
695         // ¥ª¥×¥·¥ç¥ó¤Î°ìÍ÷(alist)¤ò¼èÆÀ
696         if (isset($def['option']) && is_array($def['option'])) {
697             $options = $def['option'];
698         } else {
699             $options = array();
700         }
701
702         // defaultÃͤÎÀßÄê
703         if (isset($params['default'])) {
704             $current_value = $params['default'];
705         } else if (isset($def['default'])) {
706             $current_value = $def['default'];
707         } else {
708             $current_value = array();
709         }
710         $current_value = array_map('strval', to_array($current_value));
711
712         // ¥¿¥°¤Î¥»¥Ñ¥ì¡¼¥¿
713         if (isset($params['separator'])) {
714             $separator = $params['separator'];
715         } else {
716             $separator = "\n";
717         }
718
719         // select¥¿¥°¤ÎÃæ¿È¤òºî¤ë
720         $contents = array();
721         $selected = false;
722         foreach ($options as $key => $value) {
723             $attr = array('value' => $key);
724             if (in_array((string) $key, $current_value, true)) {
725                 $attr['selected'] = 'selected';
726                 $selected = true;
727             }
728             $contents[] = $this->_getFormInput_Html('option', $attr, $value);
729         }
730
731         // ¶õ¥¨¥ó¥È¥ê
732         if (isset($params['emptyoption'])) {
733             $attr = array('value' => '');
734             if ($selected === false) {
735                 $attr['selected'] = 'selected';
736             }
737             array_unshift($contents,
738                           $this->_getFormInput_Html('option',
739                                                     $attr,
740                                                     $params['emptyoption']));
741             unset($params['emptyoption']);
742         }
743
744         $element = $separator . implode($separator, $contents) . $separator;
745         return $this->_getFormInput_Html('select', $params, $element, false);
746     }
747     // }}}
748
749     // {{{ _getFormInput_Submit
750     /**
751      *  ¥Õ¥©¡¼¥à¥¿¥°¤ò¼èÆÀ¤¹¤ë(type="submit")
752      *
753      *  @access protected
754      */
755     function _getFormInput_Submit($name, $def, $params)
756     {
757         $params['type'] = 'submit';
758         if (isset($def['type'])) {
759             $params['name'] = is_array($def['type']) ? $name . '[]' : $name;
760         } else {
761             $params['name'] = $name;
762         }
763         if (isset($params['value']) === false) {
764             if (isset($def['name'])) {
765                 $params['value'] = $def['name'];
766             }
767         }
768         if (is_array($params['value'])) {
769             $params['value'] = $params['value'][0];
770         }
771
772         return $this->_getFormInput_Html('input', $params);
773     }
774     // }}}
775
776     // {{{ _getFormInput_Textarea
777     /**
778      *  ¥Õ¥©¡¼¥à¥¿¥°¤ò¼èÆÀ¤¹¤ë(textarea)
779      *
780      *  @access protected
781      */
782     function _getFormInput_Textarea($name, $def, $params)
783     {
784         if (isset($def['type'])) {
785             $params['name'] = is_array($def['type']) ? $name . '[]' : $name;
786         } else {
787             $params['name'] = $name;
788         }
789
790         // element
791         $element = '';
792         if (isset($params['value'])) {
793             $element = $params['value'];
794             unset($params['value']);
795         } else if (isset($params['default'])) {
796             $element = $params['default'];
797         } else if (isset($def['default'])) {
798             $element = $def['default'];
799         }
800         if (is_array($element)) {
801             if ($def['_form_counter'] < count($element)) {
802                 $element = $element[$def['_form_counter']];
803             } else {
804                 $element = '';
805             }
806         } else {
807             $params['value'] = $element;
808         }
809
810         return $this->_getFormInput_Html('textarea', $params, $element);
811     }
812     // }}}
813
814     // {{{ _getFormInput_Text
815     /**
816      *  ¥Õ¥©¡¼¥à¥¿¥°¤ò¼èÆÀ¤¹¤ë(type="text")
817      *
818      *  @access protected
819      */
820     function _getFormInput_Text($name, $def, $params)
821     {
822         // type
823         $params['type'] = 'text';
824
825         // name
826         if (isset($def['type'])) {
827             $params['name'] = is_array($def['type']) ? $name . '[]' : $name;
828         } else {
829             $params['name'] = $name;
830         }
831
832         // value
833         $value = '';
834         if (isset($params['value'])) {
835             $value = $params['value'];
836         } else if (isset($params['default'])) {
837             $value = $params['default'];
838         } else if (isset($def['default'])) {
839             $value = $def['default'];
840         }
841         if (is_array($value)) {
842             if ($def['_form_counter'] < count($value)) {
843                 $params['value'] = $value[$def['_form_counter']];
844             } else {
845                 $params['value'] = '';
846             }
847         } else {
848             $params['value'] = $value;
849         }
850
851         // maxlength
852         if (isset($def['max']) && $def['max']) {
853             $params['maxlength'] = $def['max'];
854         }
855
856         return $this->_getFormInput_Html('input', $params);
857     }
858     // }}}
859
860     // {{{ _getFormInput_Html
861     /**
862      *  HTML¥¿¥°¤ò¼èÆÀ¤¹¤ë
863      *
864      *  @access protected
865      */
866     function _getFormInput_Html($tag, $attr, $element = null, $escape_element = true)
867     {
868         // ÉÔÍפʥѥé¥á¡¼¥¿¤Ï¾Ã¤¹
869         foreach ($this->helper_parameter_keys as $key) {
870             unset($attr[$key]);
871         }
872
873         $r = "<$tag";
874
875         foreach ($attr as $key => $value) {
876             if ($value === null) {
877                 $r .= sprintf(' %s', $key);
878             } else {
879                 $r .= sprintf(' %s="%s"', $key, htmlspecialchars($value, ENT_QUOTES));
880             }
881         }
882
883         if ($element === null) {
884             $r .= ' />';
885         } else if ($escape_element) {
886             $r .= sprintf('>%s</%s>', htmlspecialchars($element, ENT_QUOTES), $tag);
887         } else {
888             $r .= sprintf('>%s</%s>', $element, $tag);
889         }
890
891         return $r;
892     }
893     // }}}
894
895     // {{{ _getRenderer
896     /**
897      *  ¥ì¥ó¥À¥é¥ª¥Ö¥¸¥§¥¯¥È¤ò¼èÆÀ¤¹¤ë
898      *
899      *  @access protected
900      *  @return object  Ethna_Renderer  ¥ì¥ó¥À¥é¥ª¥Ö¥¸¥§¥¯¥È
901      */
902     function &_getRenderer()
903     {
904         $_ret_object =& $this->_getTemplateEngine();
905         return $_ret_object;
906     }
907     // }}}
908
909     // {{{ _getTemplateEngine
910     /**
911      *  ¥ì¥ó¥À¥é¥ª¥Ö¥¸¥§¥¯¥È¤ò¼èÆÀ¤¹¤ë(¤½¤Î¤¦¤Á_getRenderer()¤ËÅý¹ç¤µ¤ì¤ëͽÄê)
912      *
913      *  @access protected
914      *  @return object  Ethna_Renderer  ¥ì¥ó¥À¥é¥ª¥Ö¥¸¥§¥¯¥È
915      *  @obsolete
916      */
917     function &_getTemplateEngine()
918     {
919         $c =& $this->backend->getController();
920         $renderer =& $c->getRenderer();
921
922         $form_array =& $this->af->getArray();
923         $app_array =& $this->af->getAppArray();
924         $app_ne_array =& $this->af->getAppNEArray();
925         $renderer->setPropByRef('form', $form_array);
926         $renderer->setPropByRef('app', $app_array);
927         $renderer->setPropByRef('app_ne', $app_ne_array);
928         $message_list = Ethna_Util::escapeHtml($this->ae->getMessageList());
929         $renderer->setPropByRef('errors', $message_list);
930         if (isset($_SESSION)) {
931             $tmp_session = Ethna_Util::escapeHtml($_SESSION);
932             $renderer->setPropByRef('session', $tmp_session);
933         }
934         $renderer->setProp('script',
935             htmlspecialchars(basename($_SERVER['SCRIPT_NAME']), ENT_QUOTES));
936         $renderer->setProp('request_uri',
937             isset($_SERVER['REQUEST_URI'])
938             ? htmlspecialchars($_SERVER['REQUEST_URI'], ENT_QUOTES)
939             : '');
940         $renderer->setProp('config', $this->config->get());
941
942         return $renderer;
943     }
944     // }}}
945
946     // {{{ _setDefault
947     /**
948      *  ¶¦ÄÌÃͤòÀßÄꤹ¤ë
949      *
950      *  @access protected
951      *  @param  object  Ethna_Renderer  ¥ì¥ó¥À¥é¥ª¥Ö¥¸¥§¥¯¥È
952      */
953     function _setDefault(&$renderer)
954     {
955     }
956     // }}}
957 }
958 // }}}
959 ?>