PageRenderTime 55ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/Nette/Application/AppForm.php

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