PageRenderTime 47ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/libs/Nette/Application/AppForm.php

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