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