Source for file AppForm.php

Documentation is available at AppForm.php

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * Nette Framework
  5. 5:  *
  6. 6:  * @copyright  Copyright (c) 2004, 2010 David Grudl
  7. 7:  * @license    http://nettephp.com/license  Nette license
  8. 8:  * @link       http://nettephp.com
  9. 9:  * @category   Nette
  10. 10:  * @package    Nette\Application
  11. 11:  */
  12. 12:  
  13. 13:  
  14. 14:  
  15. 15: /**
  16. 16:  * Web form as presenter component.
  17. 17:  *
  18. 18:  * @copyright  Copyright (c) 2004, 2010 David Grudl
  19. 19:  * @package    Nette\Application
  20. 20:  *
  21. 21:  * @property-read Presenter $presenter 
  22. 22:  */
  23. 23: class AppForm extends Form implements ISignalReceiver
  24. 24: {
  25. 25:  
  26. 26:     /**
  27. 27:      * Application form constructor.
  28. 28:      */
  29. 29:     public function __construct(IComponentContainer $parent NULL$name NULL)
  30. 30:     {
  31. 31:         parent::__construct();
  32. 32:         $this->monitor('Nette\Application\Presenter');
  33. 33:         if ($parent !== NULL{
  34. 34:             $parent->addComponent($this$name);
  35. 35:         }
  36. 36:     }
  37. 37:  
  38. 38:  
  39. 39:  
  40. 40:     /**
  41. 41:      * Returns the presenter where this component belongs to.
  42. 42:      * @param  bool   throw exception if presenter doesn't exist?
  43. 43:      * @return Presenter|NULL
  44. 44:      */
  45. 45:     public function getPresenter($need TRUE)
  46. 46:     {
  47. 47:         return $this->lookup('Nette\Application\Presenter'$need);
  48. 48:     }
  49. 49:  
  50. 50:  
  51. 51:  
  52. 52:     /**
  53. 53:      * This method will be called when the component (or component's parent)
  54. 54:      * becomes attached to a monitored object. Do not call this method yourself.
  55. 55:      * @param  IComponent 
  56. 56:      * @return void 
  57. 57:      */
  58. 58:     protected function attached($presenter)
  59. 59:     {
  60. 60:         if ($presenter instanceof Presenter{
  61. 61:             $name $this->lookupPath('Nette\Application\Presenter');
  62. 62:  
  63. 63:             if (!isset($this->getElementPrototype()->id)) {
  64. 64:                 $this->getElementPrototype()->id 'frm-' $name;
  65. 65:             }
  66. 66:  
  67. 67:             $this->setAction(new Link(
  68. 68:                 $presenter,
  69. 69:                 $name self::NAME_SEPARATOR 'submit!',
  70. 70:                 array()
  71. 71:             ));
  72. 72:  
  73. 73:             // fill-in the form with HTTP data
  74. 74:             if ($this->isSubmitted()) {
  75. 75:                 foreach ($this->getControls(as $control{
  76. 76:                     $control->loadHttpData();
  77. 77:                 }
  78. 78:             }
  79. 79:         }
  80. 80:         parent::attached($presenter);
  81. 81:     }
  82. 82:  
  83. 83:  
  84. 84:  
  85. 85:     /**
  86. 86:      * Tells if the form is anchored.
  87. 87:      * @return bool 
  88. 88:      */
  89. 89:     public function isAnchored()
  90. 90:     {
  91. 91:         return (bool) $this->getPresenter(FALSE);
  92. 92:     }
  93. 93:  
  94. 94:  
  95. 95:  
  96. 96:     /**
  97. 97:      * Internal: receives submitted HTTP data.
  98. 98:      * @return array 
  99. 99:      */
  100. 100:     protected function receiveHttpData()
  101. 101:     {
  102. 102:         $presenter $this->getPresenter();
  103. 103:         if (!$presenter->isSignalReceiver($this'submit')) {
  104. 104:             return;
  105. 105:         }
  106. 106:  
  107. 107:         $isPost $this->getMethod(=== self::POST;
  108. 108:         $request $presenter->getRequest();
  109. 109:         if ($request->isMethod('forward'|| $request->isMethod('post'!== $isPost{
  110. 110:             return;
  111. 111:         }
  112. 112:  
  113. 113:         if ($isPost{
  114. 114:             return ArrayTools::mergeTree($request->getPost()$request->getFiles());
  115. 115:         else {
  116. 116:             return $request->getParams();
  117. 117:         }
  118. 118:     }
  119. 119:  
  120. 120:  
  121. 121:  
  122. 122:     /********************* interface ISignalReceiver ****************d*g**/
  123. 123:  
  124. 124:  
  125. 125:  
  126. 126:     /**
  127. 127:      * This method is called by presenter.
  128. 128:      * @param  string 
  129. 129:      * @return void 
  130. 130:      */
  131. 131:     public function signalReceived($signal)
  132. 132:     {
  133. 133:         if ($signal === 'submit'{
  134. 134:             $this->fireEvents();
  135. 135:  
  136. 136:         else {
  137. 137:             throw new BadSignalException("There is no handler for signal '$signal' in {$this->reflection->name}.");
  138. 138:         }
  139. 139:     }
  140. 140: