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