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