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