Changeset 1147:2e5cb79e4782 for inc/admin/class.dc.form.php
- Timestamp:
- 03/08/13 15:37:34 (12 years ago)
- Branch:
- twig
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
inc/admin/class.dc.form.php
r1090 r1147 13 13 14 14 /** 15 * Template form node 16 */ 15 * dcFormNode 16 * 17 * @uses Twig_Node 18 * 19 */ 17 20 class dcFormNode extends Twig_Node 18 21 { … … 30 33 { 31 34 $compiler 32 ->addDebugInfo($this) 33 ->write("\$context['dc_form']->beginForm('". 34 $this->getAttribute('name')."');\n") 35 ->addDebugInfo($this); 36 $compiler 37 ->write("\$context['dc_form']->beginForm(") 38 ->subcompile($this->getAttribute('name')) 39 ->write(");\n"); 40 $compiler 35 41 ->subcompile($this->getNode('body')) 36 42 ->write("\$context['dc_form']->renderHiddenWidgets();\n") … … 49 55 $lineno = $token->getLine(); 50 56 $stream = $this->parser->getStream(); 51 $name = $ stream->expect(Twig_Token::NAME_TYPE)->getValue();57 $name = $this->parser->getExpressionParser()->parseExpression(); 52 58 $stream->expect(Twig_Token::BLOCK_END_TYPE); 53 59 $body = $this->parser->subparse(array($this,'decideBlockEnd'),true); … … 77 83 protected $core; 78 84 protected $twig; 79 protected $blocks;80 85 protected $forms; 81 86 protected $currentForm; 87 protected $blocks; 82 88 83 89 public function __construct($core) … … 96 102 } 97 103 104 public function addTemplate($tpl) { 105 $t = $this->twig->loadTemplate($tpl); 106 $this->blocks = array_merge($this->blocks,$t->getBlocks()); 107 } 108 98 109 public function getGlobals() 99 110 { … … 137 148 } 138 149 139 public function renderWidget($name,$attributes=array()) 150 public function renderBlock($name,$attr) { 151 echo $this->template->renderBlock( 152 $name, 153 $attr, 154 $this->blocks 155 ); 156 } 157 158 public function getCurrentForm() { 159 return $this->currentForm; 160 } 161 162 public function renderWidget($name,$attributes=array(),$extra=array()) 140 163 { 141 164 $field = $this->currentForm->$name; 142 165 if ($field) { 143 echo $this->template->renderBlock( 166 $attr = $field->getAttributes(); 167 if (isset($attr['attr'])) { 168 $attr['attr'] = array_merge($attr['attr'],$attributes); 169 } else { 170 $attr['attr'] = $attributes; 171 } 172 $this->renderBlock( 144 173 $field->getWidgetBlock(), 145 174 array_merge( 146 $field->getAttributes(), 147 array('attr' => $attributes) 148 ), 149 $this->blocks 175 $attr, 176 $extra 177 ) 150 178 ); 151 179 } … … 197 225 198 226 /** 199 * Template form 200 */ 227 * dcForm - Template form 228 * 229 */ 201 230 class dcForm 202 231 { … … 211 240 protected $errors; 212 241 213 private function addNonce() 242 243 /** 244 * addNonce -- adds dc nonce to form fields 245 * 246 * @access protected 247 * 248 * @return nothing 249 */ 250 protected function addNonce() 214 251 { 215 252 $this->addField( … … 219 256 } 220 257 221 protected function getNID($nid) 258 259 /** 260 * Defines Name & ID from field 261 * 262 * @param mixed $nid either an array (name, id) or a string (name only, id will be set to null). 263 * 264 * @access protected 265 * 266 * @return nothing. 267 */ 268 protected function setNID($nid) 222 269 { 223 270 if (is_array($nid)) { … … 231 278 } 232 279 280 public function getContext() { 281 return array(); 282 } 283 284 /** 285 * Class constructor 286 * 287 * @param mixed $core dotclear core 288 * @param mixed $name form name 289 * @param mixed $action form action 290 * @param string $method form method ('GET' or 'POST') 291 * 292 * @access public 293 * 294 * @return mixed Value. 295 */ 233 296 public function __construct($core,$name,$action,$method='POST') 234 297 { 235 298 $this->core = $core; 236 $this-> getNID($name);299 $this->setNID($name); 237 300 $this->method = $method; 238 301 $this->action = $action; … … 247 310 } 248 311 312 313 /** 314 * Returns form name 315 * 316 * @access public 317 * 318 * @return mixed Value. 319 */ 249 320 public function getName() 250 321 { … … 270 341 } 271 342 343 public function removeField(dcField $f) { 344 $n = $f->getName(); 345 if (isset($this->fields[$n])){ 346 unset($this->fields[$n]); 347 } 348 349 } 350 public function renameField($field,$newname) { 351 $oldname = $field->getName(); 352 if (isset($this->fields[$oldname])) { 353 unset($this->fields[$oldname]); 354 $field->setName($newname); 355 $this->fields[$newname] = $field; 356 } 357 //print_r($this->fields); 358 } 272 359 public function begin() 273 360 { … … 292 379 public function __get($name) 293 380 { 294 return isset($this->fields[$name]) ? 381 return isset($this->fields[$name]) ? 295 382 $this->fields[$name] : null; 296 383 } 297 384 298 385 public function __set($name,$value) 299 386 { … … 302 389 } 303 390 } 304 391 305 392 public function isSubmitted() 306 393 { 307 394 $from = $this->method == 'POST' ? $_POST : $_GET; 308 echo "form fields :\n"; 309 } 310 311 public function setup() 312 { 395 } 396 397 protected function setupFields() { 313 398 $from = $this->method == 'POST' ? $_POST : $_GET; 314 399 foreach ($this->fields as $f) { 315 400 $f->setup($from); 316 401 } 402 } 403 404 protected function handleActions($submitted) { 405 foreach ($submitted as $f) { 406 $action = $f->getAction(); 407 if ($action != NULL) { 408 $ret = call_user_func($action,$this); 409 } 410 } 411 } 412 413 protected function getSubmittedFields() { 414 $s = array(); 317 415 foreach ($this->submitfields as $f) { 318 416 if ($f->isDefined()) { 319 $ret = call_user_func($f->getAction(),$this); 320 return; 417 $s[$f->getName()] = $f; 321 418 } 322 419 } 323 } 324 420 return $s; 421 } 422 423 public function setup() 424 { 425 $this->setupFields(); 426 $submitted = $this->getSubmittedFields(); 427 $this->handleActions($submitted); 428 } 429 325 430 public function check() 326 431 { … … 352 457 protected $defined; 353 458 354 protected function getNID($nid)459 protected function setNID($nid) 355 460 { 356 461 if (is_array($nid)) { … … 365 470 public function __construct($name,$value,$attributes=array()) 366 471 { 367 $this-> getNID($name);472 $this->setNID($name); 368 473 $this->attributes = $attributes; 369 $this-> value = $value;474 $this->setValue($value); 370 475 $this->attributes['name'] = $this->name; 371 476 $this->attributes['id'] = $this->id; 477 $this->defined = false; 478 } 479 480 public function setValue($value) { 481 $this->value = $value; 372 482 $this->attributes['value'] = $this->value; 373 $this->defined = false; 374 } 375 483 } 484 485 public function getValue() { 486 return $this->value; 487 } 488 376 489 public function __toString() 377 490 { … … 395 508 return $this->name; 396 509 } 397 510 public function setName($name) { 511 $this->setNID($name); 512 $this->attributes['name'] = $this->name; 513 $this->attributes['id'] = $this->id; 514 } 515 398 516 public function check() 399 517 { … … 406 524 } 407 525 526 public function parseValue($from) { 527 if (isset($from[$this->id])) { 528 return $from[$this->id]; 529 } 530 return null; 531 } 532 408 533 public function setup($from) 409 534 { 410 if (isset($from[$this->id])) { 411 $this->value = $from[$this->id]; 535 $value = $this->parseValue($from); 536 if ($value !== null) { 537 $this->setValue($value); 412 538 $this->defined = true; 413 539 } … … 488 614 if (isset($attributes['action'])) { 489 615 $this->action = $attributes['action']; 616 } else { 617 $this->action = NULL; 490 618 } 491 619 } … … 527 655 } 528 656 657 public function parseValue($from) { 658 659 $v = parent::parseValue($from); 660 if (!isset($this->options[$v])) { 661 return $this->value; 662 } else { 663 return $v; 664 } 665 } 529 666 } 530 667 ?>
Note: See TracChangeset
for help on using the changeset viewer.