1: <?php
2:
3: /**
4: * This file is part of the Nette Framework (https://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: * @package Nette\Application
11: */
12:
13:
14:
15: /**
16: * Web form as presenter component.
17: *
18: * @author David Grudl
19: *
20: * @property-read NPresenter $presenter
21: * @package Nette\Application
22: */
23: class NAppForm extends NForm implements ISignalReceiver
24: {
25:
26: /**
27: * Application form constructor.
28: */
29: public function __construct(IComponentContainer $parent = NULL, $name = NULL)
30: {
31: parent::__construct();
32: $this->monitor('NPresenter');
33: if ($parent !== NULL) {
34: $parent->addComponent($this, $name);
35: }
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 NPresenter|NULL
44: */
45: public function getPresenter($need = TRUE)
46: {
47: return $this->lookup('NPresenter', $need);
48: }
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 IComponent
56: * @return void
57: */
58: protected function attached($presenter)
59: {
60: if ($presenter instanceof NPresenter) {
61: $name = $this->lookupPath('NPresenter');
62:
63: if (!isset($this->getElementPrototype()->id)) {
64: $this->getElementPrototype()->id = 'frm-' . $name;
65: }
66:
67: $this->setAction(new NLink(
68: $presenter,
69: $name . self::NAME_SEPARATOR . 'submit!',
70: array()
71: ));
72:
73: // fill-in the form with HTTP data
74: if ($this->isSubmitted()) {
75: foreach ($this->getControls() as $control) {
76: $control->loadHttpData();
77: }
78: }
79: }
80: parent::attached($presenter);
81: }
82:
83:
84:
85: /**
86: * Tells if the form is anchored.
87: * @return bool
88: */
89: public function isAnchored()
90: {
91: return (bool) $this->getPresenter(FALSE);
92: }
93:
94:
95:
96: /**
97: * Internal: receives submitted HTTP data.
98: * @return array
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 NArrayTools::mergeTree($request->getPost(), $request->getFiles());
115: } else {
116: return $request->getParams();
117: }
118: }
119:
120:
121:
122: /********************* interface ISignalReceiver ****************d*g**/
123:
124:
125:
126: /**
127: * This method is called by presenter.
128: * @param string
129: * @return void
130: */
131: public function signalReceived($signal)
132: {
133: if ($signal === 'submit') {
134: $this->fireEvents();
135:
136: } else {
137: throw new NBadSignalException("There is no handler for signal '$signal' in {$this->reflection->name}.");
138: }
139: }
140:
141: }
142: