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