OSDN Git Service

- merged changes from 2009-01-30 to 2009-04-29.
[ethna/ethna.git] / class / Plugin / Smarty / block.form.php
1 <?php
2 /**
3  *  smarty block:フォームタグ出力プラグイン
4  */
5 function smarty_block_form($params, $content, &$smarty, &$repeat)
6 {
7     if ($repeat) {
8         // {form}: ブロック内部に進む前の処理
9
10         // 配列指定のフォームヘルパ用カウンタをリセットする
11         $c =& Ethna_Controller::getInstance();
12         $view =& $c->getView();
13         $view->resetFormCounter();
14
15         // {form default=... }
16         if (isset($params['default']) === false) {
17             // 指定なしのときは $form を使う
18             // 1テンプレートに複数 {form} を指定する場合は、
19             // default を指定することが必要
20             $af =& $c->getActionForm();
21
22             // c.f. http://smarty.net/manual/en/plugins.block.functions.php
23             $smarty->_tag_stack[count($smarty->_tag_stack)-1][1]['default']
24                 =& $af->getArray(false);
25         }
26
27         // {form name=... }
28         // 複数 {form} が置かれた場合に、それぞれを識別する役割を果たす
29         if (isset($params['name']) === false) {
30             // c.f. http://smarty.php.net/manual/en/plugins.block.functions.php
31             $smarty->_tag_stack[count($smarty->_tag_stack)-1][1]['name']
32                 = 'default';
33         }
34
35         // 動的フォームヘルパを呼ぶ
36         if (isset($params['ethna_action'])) {
37             $ethna_action = $params['ethna_action'];
38             $view =& $c->getView();
39             $view->addActionFormHelper($ethna_action, true);
40         }  
41
42         // ここで返す値は出力されない
43         return '';
44
45     } else {
46         // {/form}: ブロック全体を出力
47
48         $c =& Ethna_Controller::getInstance();
49         $view =& $c->getView();
50         if ($view === null) {
51             return null;
52         }
53
54         // {form ethna_action=... }
55         if (isset($params['ethna_action'])) {
56             $ethna_action = $params['ethna_action'];
57             unset($params['ethna_action']);
58
59             $view->addActionFormHelper($ethna_action);
60             $hidden = $c->getActionRequest($ethna_action, 'hidden');
61             $content = $hidden . $content;
62         }
63
64         //  {form name=... }
65         //  指定された場合は、submitされた {form}を識別する
66         //  id をhiddenタグで指定する
67         $name = $params['name'];
68         unset($params['name']);
69         if ($name != 'default') {
70             $name_hidden = sprintf('<input type="hidden" name="ethna_fid" value="%s" />',
71                                    htmlspecialchars($name, ENT_QUOTES)
72                            );
73             $content = $name_hidden . $content;
74         }
75
76         // enctype の略称対応
77         if (isset($params['enctype'])) {
78             if ($params['enctype'] == 'file'
79                 || $params['enctype'] == 'multipart') {
80                 $params['enctype'] = 'multipart/form-data';
81             } else if ($params['enctype'] == 'url') {
82                 $params['enctype'] = 'application/x-www-form-urlencoded';
83             }
84         }
85
86         // defaultはもう不要
87         if (isset($params['default'])) {
88             unset($params['default']);
89         }
90
91         // $contentを囲む<form>ブロック全体を出力
92         return $view->getFormBlock($content, $params);
93     }
94 }
95