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