1: <?php
2:
3: /**
4: * This file is part of the Nette Framework (https://nette.org)
5: * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6: */
7:
8: namespace Nette\Application\UI;
9:
10: use Nette;
11:
12:
13: /**
14: * Web form adapted for Presenter.
15: */
16: class Form extends Nette\Forms\Form implements ISignalReceiver
17: {
18:
19: /**
20: * Application form constructor.
21: */
22: public function __construct(Nette\ComponentModel\IContainer $parent = NULL, $name = NULL)
23: {
24: parent::__construct();
25: if ($parent !== NULL) {
26: $parent->addComponent($this, $name);
27: }
28: }
29:
30:
31: /**
32: * @return void
33: */
34: protected function validateParent(Nette\ComponentModel\IContainer $parent)
35: {
36: parent::validateParent($parent);
37: $this->monitor('Nette\Application\UI\Presenter');
38: }
39:
40:
41: /**
42: * Returns the presenter where this component belongs to.
43: * @param bool throw exception if presenter doesn't exist?
44: * @return Presenter|NULL
45: */
46: public function getPresenter($need = TRUE)
47: {
48: return $this->lookup('Nette\Application\UI\Presenter', $need);
49: }
50:
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 Nette\ComponentModel\IComponent
56: * @return void
57: */
58: protected function attached($presenter)
59: {
60: if ($presenter instanceof Presenter) {
61: $name = $this->lookupPath('Nette\Application\UI\Presenter');
62:
63: if (!isset($this->getElementPrototype()->id)) {
64: $this->getElementPrototype()->id = 'frm-' . $name;
65: }
66:
67: if (iterator_count($this->getControls()) && $this->isSubmitted()) {
68: foreach ($this->getControls() as $control) {
69: if (!$control->isDisabled()) {
70: $control->loadHttpData();
71: }
72: }
73: }
74:
75: if (!$this->getAction()) {
76: $this->setAction(new Link($presenter, 'this', array()));
77: $signal = new Nette\Forms\Controls\HiddenField($name . self::NAME_SEPARATOR . 'submit');
78: $signal->setOmitted()->setHtmlId(FALSE);
79: $this[Presenter::SIGNAL_KEY] = $signal;
80: }
81: }
82: parent::attached($presenter);
83: }
84:
85:
86: /**
87: * Tells if the form is anchored.
88: * @return bool
89: */
90: public function isAnchored()
91: {
92: return (bool) $this->getPresenter(FALSE);
93: }
94:
95:
96: /**
97: * Internal: returns submitted HTTP data or NULL when form was not submitted.
98: * @return array|NULL
99: */
100: protected function receiveHttpData()
101: {
102: $presenter = $this->getPresenter();
103: if (!$presenter->isSignalReceiver($this, 'submit')) {
104: return;
105: }
106:
107: $isPost = $this->getMethod() === self::POST;
108: $request = $presenter->getRequest();
109: if ($request->isMethod('forward') || $request->isMethod('post') !== $isPost) {
110: return;
111: }
112:
113: if ($isPost) {
114: return Nette\Utils\Arrays::mergeTree($request->getPost(), $request->getFiles());
115: } else {
116: return $request->getParameters();
117: }
118: }
119:
120:
121: /********************* interface ISignalReceiver ****************d*g**/
122:
123:
124: /**
125: * This method is called by presenter.
126: * @param string
127: * @return void
128: */
129: public function signalReceived($signal)
130: {
131: if ($signal === 'submit') {
132: if (!$this->getPresenter()->getRequest()->hasFlag(Nette\Application\Request::RESTORED)) {
133: $this->fireEvents();
134: }
135: } else {
136: $class = get_class($this);
137: throw new BadSignalException("Missing handler for signal '$signal' in $class.");
138: }
139: }
140:
141: }
142: