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