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:                 $this->setAction(new Link(
  62. 62:                     $presenter,
  63. 63:                     $this->lookupPath('Nette\Application\Presenter'self::NAME_SEPARATOR 'submit!',
  64. 64:                     array()
  65. 65:                 ));
  66. 66:  
  67. 67:             // fill-in the form with HTTP data
  68. 68:             if ($this->isSubmitted()) {
  69. 69:                 foreach ($this->getControls(as $control{
  70. 70:                     $control->loadHttpData();
  71. 71:                 }
  72. 72:             }
  73. 73:         }
  74. 74:         parent::attached($presenter);
  75. 75:     }
  76. 76:  
  77. 77:  
  78. 78:  
  79. 79:     /**
  80. 80:      * Tells if the form is anchored.
  81. 81:      * @return bool 
  82. 82:      */
  83. 83:     public function isAnchored()
  84. 84:     {
  85. 85:         return (bool) $this->getPresenter(FALSE);
  86. 86:     }
  87. 87:  
  88. 88:  
  89. 89:  
  90. 90:     /**
  91. 91:      * Internal: receives submitted HTTP data.
  92. 92:      * @return array 
  93. 93:      */
  94. 94:     protected function receiveHttpData()
  95. 95:     {
  96. 96:         $presenter $this->getPresenter();
  97. 97:         if (!$presenter->isSignalReceiver($this'submit')) {
  98. 98:             return;
  99. 99:         }
  100. 100:  
  101. 101:         $isPost $this->getMethod(=== self::POST;
  102. 102:         $request $presenter->getRequest();
  103. 103:         if ($request->isMethod('forward'|| $request->isMethod('post'!== $isPost{
  104. 104:             return;
  105. 105:         }
  106. 106:  
  107. 107:         if ($isPost{
  108. 108:             return ArrayTools::mergeTree($request->getPost()$request->getFiles());
  109. 109:         else {
  110. 110:             return $request->getParams();
  111. 111:         }
  112. 112:     }
  113. 113:  
  114. 114:  
  115. 115:  
  116. 116:     /********************* interface ISignalReceiver ****************d*g**/
  117. 117:  
  118. 118:  
  119. 119:  
  120. 120:     /**
  121. 121:      * This method is called by presenter.
  122. 122:      * @param  string 
  123. 123:      * @return void 
  124. 124:      */
  125. 125:     public function signalReceived($signal)
  126. 126:     {
  127. 127:         if ($signal === 'submit'{
  128. 128:             $this->fireEvents();
  129. 129:  
  130. 130:         else {
  131. 131:             throw new BadSignalException("There is no handler for signal '$signal' in {$this->reflection->name}.");
  132. 132:         }
  133. 133:     }
  134. 134: