[992] | 1 | <?php |
---|
| 2 | |
---|
| 3 | |
---|
| 4 | class dcFormNode extends Twig_Node { |
---|
| 5 | public function __construct($name, Twig_NodeInterface $body, $lineno, $tag = null) |
---|
| 6 | { |
---|
| 7 | parent::__construct(array('body' => $body), array('name' => $name), $lineno, $tag); |
---|
| 8 | } |
---|
| 9 | |
---|
| 10 | /** |
---|
| 11 | * Compiles the node to PHP. |
---|
| 12 | * |
---|
| 13 | * @param Twig_Compiler A Twig_Compiler instance |
---|
| 14 | */ |
---|
| 15 | public function compile(Twig_Compiler $compiler) |
---|
| 16 | { |
---|
| 17 | $compiler |
---|
| 18 | ->addDebugInfo($this) |
---|
| 19 | ->write("\$context['dc_form']->beginForm('". |
---|
| 20 | $this->getAttribute('name')."');\n") |
---|
| 21 | ->subcompile($this->getNode('body')) |
---|
| 22 | ->write("\$context['dc_form']->endForm();\n") |
---|
| 23 | ; |
---|
| 24 | } |
---|
| 25 | |
---|
| 26 | } |
---|
| 27 | |
---|
| 28 | class dcFormTokenParser extends Twig_TokenParser { |
---|
| 29 | |
---|
| 30 | public function parse(Twig_Token $token) { |
---|
| 31 | $lineno = $token->getLine(); |
---|
| 32 | $stream = $this->parser->getStream(); |
---|
| 33 | $name = $stream->expect(Twig_Token::NAME_TYPE)->getValue(); |
---|
| 34 | $stream->expect(Twig_Token::BLOCK_END_TYPE); |
---|
| 35 | $body = $this->parser->subparse(array($this, 'decideBlockEnd'), true); |
---|
| 36 | $stream->expect(Twig_Token::BLOCK_END_TYPE); |
---|
| 37 | return new dcFormNode ($name, $body, $token->getLine(), $this->getTag()); |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | public function decideBlockEnd(Twig_Token $token) |
---|
| 41 | { |
---|
| 42 | return $token->test('endform'); |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | |
---|
| 46 | public function getTag() |
---|
| 47 | { |
---|
| 48 | return 'form'; |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | class dcFormExtension extends Twig_Extension { |
---|
| 54 | protected $template; |
---|
| 55 | protected $tpl; |
---|
| 56 | protected $core; |
---|
| 57 | protected $twig; |
---|
| 58 | protected $blocks; |
---|
| 59 | protected $forms; |
---|
| 60 | protected $currentForm; |
---|
| 61 | |
---|
| 62 | public function __construct($core){ |
---|
| 63 | $this->core = $core; |
---|
| 64 | $this->tpl = 'form_layout.html.twig'; |
---|
| 65 | $this->forms = array(); |
---|
| 66 | $this->currentForm = null; |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | public function initRuntime(Twig_Environment $environment) |
---|
| 70 | { |
---|
| 71 | $this->twig = $environment; |
---|
| 72 | $this->template = $this->twig->loadTemplate($this->tpl); |
---|
| 73 | $this->blocks = $this->template->getBlocks(); |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | public function getGlobals() { |
---|
| 77 | return array('dc_form' => $this); |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | public function getFunctions() { |
---|
| 81 | return array( |
---|
| 82 | 'form_field' => new Twig_Function_Method($this, 'renderWidget', array('is_safe' => array('html'))), |
---|
| 83 | '_form_is_choice_group' => new \Twig_Function_Method($this, 'isChoiceGroup', array('is_safe' => array('html'))), |
---|
| 84 | '_form_is_choice_selected' => new \Twig_Function_Method($this, 'isChoiceSelected', array('is_safe' => array('html'))) |
---|
| 85 | |
---|
| 86 | ); |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | public function isChoiceGroup($choice) { |
---|
| 90 | return is_array($choice); |
---|
| 91 | } |
---|
| 92 | public function isChoiceSelected($choice,$value) { |
---|
| 93 | return $choice == $value; |
---|
| 94 | |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | public function getTokenParsers() { |
---|
| 98 | return array(new dcFormTokenParser()); |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | public function renderWidget($name,$attributes=array()) { |
---|
| 102 | $field = $this->currentForm->$name; |
---|
| 103 | if ($field) |
---|
| 104 | echo $this->template->renderBlock( |
---|
| 105 | $field->getWidgetBlock(), |
---|
| 106 | array_merge($field->getAttributes(),array('attr'=>$attributes)), |
---|
| 107 | $this->blocks); |
---|
| 108 | |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | public function getName() { |
---|
| 112 | return 'dc_form'; |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | public function addForm(dcForm $form) { |
---|
| 116 | $this->forms[$form->getName()]=$form; |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | public function beginForm($name) { |
---|
| 120 | if (isset($this->forms[$name])) { |
---|
| 121 | $this->currentForm = $this->forms[$name]; |
---|
| 122 | $this->currentForm->begin(); |
---|
| 123 | } else { |
---|
| 124 | throw new Twig_Error_Runtime("Form '".$name."' does not exist"); |
---|
| 125 | } |
---|
| 126 | } |
---|
| 127 | public function endForm() { |
---|
| 128 | $this->currentForm->end(); |
---|
| 129 | $this->currentForm = null; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | |
---|
| 135 | class dcForm { |
---|
| 136 | protected $name; |
---|
| 137 | protected $core; |
---|
| 138 | protected $action; |
---|
| 139 | protected $fields; |
---|
| 140 | |
---|
| 141 | public function __construct($core,$name,$action, $method='POST') { |
---|
| 142 | $this->core = $core; |
---|
| 143 | $this->name = $name; |
---|
| 144 | $this->method = $method; |
---|
| 145 | $this->action = $action; |
---|
| 146 | $this->fields = array(); |
---|
[1014] | 147 | $this->core->page->getExtension('dc_form')->addForm($this); |
---|
[992] | 148 | } |
---|
| 149 | |
---|
| 150 | public function getName() { |
---|
| 151 | return $this->name; |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | public function addField(dcField $f) { |
---|
| 155 | $this->fields[$f->getName()]=$f; |
---|
| 156 | return $this; |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | public function begin() { |
---|
| 160 | echo '<form method="'.$this->method.'" action="'.$this->action.'">'; |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | public function end() { |
---|
| 164 | echo '</form>'; |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | |
---|
| 168 | public function __isset($name) { |
---|
| 169 | return isset($this->fields[$name]); |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | public function __get($name) { |
---|
| 173 | if (isset($this->fields[$name])) { |
---|
| 174 | return $this->fields[$name]; |
---|
| 175 | } else { |
---|
| 176 | return null; |
---|
| 177 | } |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | public function __set($name,$value) { |
---|
| 181 | if (isset($this->fields[$name])) { |
---|
| 182 | $this->fields[$name]->setAttribute('value',$value); |
---|
| 183 | } |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | abstract class dcField { |
---|
| 189 | protected $attributes; |
---|
| 190 | protected $name; |
---|
| 191 | protected $value; |
---|
| 192 | protected $id; |
---|
| 193 | |
---|
| 194 | protected function getNID($nid) { |
---|
| 195 | if (is_array($nid)) { |
---|
| 196 | $this->name = $nid[0]; |
---|
| 197 | $this->id = !empty($nid[1]) ? $nid[1] : null; |
---|
| 198 | } else { |
---|
| 199 | $this->id = $this->name = $nid; |
---|
| 200 | } |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | public function __construct($name, $value, $attributes = array()) { |
---|
| 204 | $this->getNID($name); |
---|
| 205 | $this->attributes = $attributes; |
---|
| 206 | $this->value = $value; |
---|
| 207 | $this->attributes['name'] = $this->name; |
---|
| 208 | $this->attributes['id'] = $this->id; |
---|
| 209 | $this->attributes['value'] = $this->value; |
---|
| 210 | |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | abstract public function getWidgetBlock(); |
---|
| 214 | |
---|
| 215 | public function setAttribute ($name,$value) { |
---|
| 216 | $this->attributes[$name] = $value; |
---|
| 217 | } |
---|
| 218 | |
---|
| 219 | public function getAttributes() { |
---|
| 220 | return $this->attributes; |
---|
| 221 | |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | public function getName(){ |
---|
| 225 | return $this->name; |
---|
| 226 | } |
---|
| 227 | |
---|
| 228 | public function check() { |
---|
| 229 | |
---|
| 230 | } |
---|
| 231 | } |
---|
| 232 | |
---|
| 233 | |
---|
| 234 | class dcFieldText extends dcField { |
---|
| 235 | |
---|
| 236 | public function getWidgetBlock() { |
---|
| 237 | return "field_text"; |
---|
| 238 | } |
---|
| 239 | |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | class dcFieldTextArea extends dcField { |
---|
| 243 | |
---|
| 244 | public function getWidgetBlock() { |
---|
| 245 | return "field_textarea"; |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | } |
---|
| 249 | |
---|
| 250 | class dcFieldHidden extends dcField { |
---|
| 251 | |
---|
| 252 | public function getWidgetBlock() { |
---|
| 253 | return "field_hidden"; |
---|
| 254 | } |
---|
| 255 | |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | class dcFieldCheckbox extends dcField { |
---|
| 259 | |
---|
| 260 | public function getWidgetBlock() { |
---|
| 261 | return "field_checkbox"; |
---|
| 262 | } |
---|
| 263 | |
---|
| 264 | } |
---|
| 265 | |
---|
| 266 | class dcFieldSubmit extends dcField { |
---|
| 267 | |
---|
| 268 | public function getWidgetBlock() { |
---|
| 269 | return "field_submit"; |
---|
| 270 | } |
---|
| 271 | |
---|
| 272 | } |
---|
| 273 | |
---|
| 274 | class dcFieldCombo extends dcField { |
---|
| 275 | protected $options; |
---|
| 276 | |
---|
| 277 | public function __construct($name, $value, $options, $attributes = array()) { |
---|
| 278 | $this->options = $options; |
---|
| 279 | parent::__construct($name,$value,$attributes); |
---|
| 280 | $this->attributes['options']=$options; |
---|
| 281 | } |
---|
| 282 | |
---|
| 283 | public function getWidgetBlock() { |
---|
| 284 | return "field_combo"; |
---|
| 285 | } |
---|
| 286 | |
---|
| 287 | } |
---|
| 288 | |
---|
[1014] | 289 | ?> |
---|