1: <?php
2:
3: /**
4: * This file is part of the Nette Framework.
5: *
6: * Copyright (c) 2004, 2010 David Grudl (http://davidgrudl.com)
7: *
8: * This source file is subject to the "Nette license", and/or
9: * GPL license. For more information please see http://nette.org
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: */
22: class NAppForm extends NForm implements ISignalReceiver
23: {
24:
25: /**
26: * NApplication form constructor.
27: */
28: public function __construct(IComponentContainer $parent = NULL, $name = NULL)
29: {
30: parent::__construct();
31: $this->monitor('NPresenter');
32: if ($parent !== NULL) {
33: $parent->addComponent($this, $name);
34: }
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: /**
52: * This method will be called when the component (or component's parent)
53: * becomes attached to a monitored object. Do not call this method yourself.
54: * @param IComponent
55: * @return void
56: */
57: protected function attached($presenter)
58: {
59: if ($presenter instanceof NPresenter) {
60: $name = $this->lookupPath('NPresenter');
61:
62: if (!isset($this->getElementPrototype()->id)) {
63: $this->getElementPrototype()->id = 'frm-' . $name;
64: }
65:
66: $this->setAction(new NLink(
67: $presenter,
68: $name . self::NAME_SEPARATOR . 'submit!',
69: array()
70: ));
71:
72: // fill-in the form with HTTP data
73: if ($this->isSubmitted()) {
74: foreach ($this->getControls() as $control) {
75: $control->loadHttpData();
76: }
77: }
78: }
79: parent::attached($presenter);
80: }
81:
82:
83:
84: /**
85: * Tells if the form is anchored.
86: * @return bool
87: */
88: public function isAnchored()
89: {
90: return (bool) $this->getPresenter(FALSE);
91: }
92:
93:
94:
95: /**
96: * Internal: receives submitted HTTP data.
97: * @return array
98: */
99: protected function receiveHttpData()
100: {
101: $presenter = $this->getPresenter();
102: if (!$presenter->isSignalReceiver($this, 'submit')) {
103: return;
104: }
105:
106: $isPost = $this->getMethod() === self::POST;
107: $request = $presenter->getRequest();
108: if ($request->isMethod('forward') || $request->isMethod('post') !== $isPost) {
109: return;
110: }
111:
112: if ($isPost) {
113: return NArrayTools::mergeTree($request->getPost(), $request->getFiles());
114: } else {
115: return $request->getParams();
116: }
117: }
118:
119:
120:
121: /********************* interface ISignalReceiver ****************d*g**/
122:
123:
124:
125: /**
126: * This method is called by presenter.
127: * @param string
128: * @return void
129: */
130: public function signalReceived($signal)
131: {
132: if ($signal === 'submit') {
133: $this->fireEvents();
134:
135: } else {
136: throw new NBadSignalException("Missing handler for signal '$signal' in {$this->reflection->name}.");
137: }
138: }
139:
140: }
141: