1: <?php
2: /**
3: * This file contains the QControlProxy class
4: */
5:
6: /**
7: * Class QControlProxy is used to 'proxy' the actions for another control
8: */
9: class QControlProxy extends QControl {
10: /**
11: * @var string HTML element ID which is to be rendered/sent to the browser
12: * @deprecated This is not needed by newer implementation which uses HTML5 data tags.
13: */
14: protected $strTargetControlId;
15:
16: /** @var bool Overriding parent class */
17: protected $blnActionsMustTerminate = true;
18: /** @var bool Overriding parent class */
19: protected $blnScriptsOnly = true;
20: /** @var null Overriding parent class to turn off rendering of this control when auto-rendering */
21: protected $strPreferredRenderMethod = null;
22:
23: /**
24: * Constructor Method
25: *
26: * @param QControl|QControlBase|QForm $objParent Parent control
27: * @param null|string $strControlId Control ID for this control
28: *
29: * @throws Exception
30: * @throws QCallerException
31: */
32: public function __construct ($objParent, $strControlId = null) {
33: parent::__construct($objParent, $strControlId);
34: $this->mixActionParameter = new QJsClosure('return $j(this).data("qap")');
35: }
36:
37: /**
38: * @throws QCallerException
39: */
40: public function GetControlHtml() {
41: throw new QCallerException('QControlProxies cannot be rendered. Use RenderAsEvents() within an HTML tag.');
42: }
43:
44: /**
45: * Render as an HTML link (anchor tag)
46: *
47: * @param string $strLabel Text to link to
48: * @param string|null $strActionParameter Action parameter for this rendering of the control. Will be sent to the ActionParameter of the action.
49: * @param null|array $attributes Array of attributes to add to the tag for the link.
50: * @param string $strTag Tag to use. Defaults to 'a'.
51: *
52: * @return string
53: */
54: public function RenderAsLink($strLabel, $strActionParameter = null, $attributes = [], $strTag = 'a', $blnHtmlEntities = true) {
55: $defaults['href'] = 'javascript:;';
56: $defaults['data-qpxy'] = $this->strControlId;
57: if ($strActionParameter) {
58: $defaults['data-qap'] = $strActionParameter;
59: }
60: $attributes = array_merge($defaults, $attributes); // will only apply defaults that are not in attributes
61:
62: if ($blnHtmlEntities) {
63: $strLabel = QApplication::HtmlEntities($strLabel);
64: }
65:
66: return QHtml::RenderTag($strTag, $attributes, $strLabel);
67: }
68:
69: /**
70: * Render as an HTML button.
71: *
72: * @param string $strLabel Text to link to
73: * @param string|null $strActionParameter Action parameter for this rendering of the control. Will be sent to the ActionParameter of the action.
74: * @param array $attributes Array of attributes to add to the tag for the link.
75: * @param bool $blnHtmlEntities False to turn off html entities.
76: *
77: * @return string
78: */
79: public function RenderAsButton($strLabel, $strActionParameter = null, $attributes = [], $blnHtmlEntities = true) {
80: $defaults['onclick']='return false';
81: $defaults['type']='button';
82: $attributes = array_merge($defaults, $attributes); // will only apply defaults that are not in attributes
83: return $this->RenderAsLink($strLabel, $strActionParameter, $attributes, 'button', $blnHtmlEntities);
84: }
85:
86: /**
87: * Render just attributes that can be included in any html tag to attach the proxy to the tag.
88: *
89: * @param string|null $strActionParameter
90: * @return string
91: */
92: public function RenderAttributes($strActionParameter = null) {
93: $attributes['data-qpxy'] = $this->ControlId;
94: if ($strActionParameter) {
95: $attributes['data-qap'] = $strActionParameter;
96: }
97: return QHtml::RenderHtmlAttributes($attributes);
98: }
99:
100:
101: /**
102: * Renders only the id of this Proxy essentially embedding it into (disguising it as) another element.
103: * The template must contain the element separately. This function just renders/returns 'id="XYZ"'
104: * where XYZ is the target control ID.
105: *
106: * @param null|string $strActionParameter Action parameter against which the action will be taken $strActionParameter
107: * @param bool $blnDisplayOutput Should the output be sent to browser (true) or returned (false)
108: * @param null $strTargetControlId ID to be sent to the browser for this proxy's HTML element
109: *
110: * @deprecated Obsolete. Above methods generate less code and are easier to use. Also, do not mix the two.
111: *
112: * @param bool $blnRenderControlId Control ID to be rendered or not
113: *
114: * @return string
115: */
116: public function RenderAsEvents($strActionParameter = null, $blnDisplayOutput = true, $strTargetControlId = null, $blnRenderControlId = true) {
117: $this->RenderAttributes();
118: if ($strTargetControlId)
119: $this->strTargetControlId = $strTargetControlId;
120: else
121: $this->strTargetControlId = $this->objForm->GenerateControlId();
122:
123: $this->mixActionParameter = $strActionParameter;
124: $strToReturn = $this->RenderActionScripts();
125:
126: QApplication::ExecuteJavaScript($strToReturn);
127:
128: if ($blnRenderControlId && $blnDisplayOutput) {
129: echo sprintf("id='%s'", $this->strTargetControlId);
130: return '';
131: }
132: else if($blnRenderControlId)
133: return sprintf("id='%s'", $this->strTargetControlId);
134: else
135: return "";
136: }
137:
138: /**
139: * Renders the proxy as a clickable link (the link will not navigate)
140: *
141: * @param null|string $strActionParameter Action parameter against which the action will be taken
142: * @param bool $blnDisplayOutput Should the output be sent to browser (true) or returned (false)
143: * @param null|string $strTargetControlId ID to be sent to the browser for this proxy's HTML element
144: *
145: * @deprecated From v2. Above ways are more efficient.
146: * @return string
147: */
148: public function RenderAsHref($strActionParameter = null, $blnDisplayOutput = true, $strTargetControlId = null) {
149: if ($strTargetControlId)
150: $this->strTargetControlId = $strTargetControlId;
151: else
152: $this->strTargetControlId = $this->objForm->GenerateControlId();
153:
154: $this->mixActionParameter = $strActionParameter;
155: $strToReturn = $this->RenderAsScript('QClickEvent');
156: if ($strToReturn)
157: $strToReturn = 'javascript:' . $strToReturn;
158: else
159: $strToReturn = 'javascript: return false;';
160:
161: /* target id needs to be rendered outside the href
162: if ($blnRenderControlId && $blnDisplayOutput)
163: echo sprintf("id='%s'", $this->strTargetControlId);
164: */
165:
166: //$this->blnModified = false;
167:
168: // Output or Display
169: if ($blnDisplayOutput)
170: print($strToReturn);
171: else
172: return $strToReturn;
173: }
174:
175: /**
176: * Renders all the actions for a particular event as javascripts.
177: *
178: * @param string $strEventType
179: * @return string
180: */
181: public function RenderAsScript($strEventType='QClickEvent') {
182: $objActions = $this->GetAllActions($strEventType);
183: $strToReturn = '';
184: foreach ($objActions as $objAction) {
185: $strToReturn .= $objAction->RenderScript($this);
186: }
187: return $strToReturn;
188: }
189:
190: /**
191: * Parses postback data
192: *
193: * In this class, the method does nothing and is here because of the contraints (derived from an abstract class)
194: */
195: public function ParsePostData() {}
196:
197: /**
198: * Validates this control proxy
199: *
200: * @return bool Whether this control proxy is valid or not
201: */
202: public function Validate() {return true;}
203:
204: /////////////////////////
205: // Public Properties: GET
206: /////////////////////////
207: /**
208: * PHP magic method
209: *
210: * @param string $strName Name of the property
211: *
212: * @return mixed
213: * @throws Exception|QCallerException
214: */
215: public function __get($strName) {
216: switch ($strName) {
217:
218: // deprecated
219: case 'TargetControlId': return $this->strTargetControlId;
220:
221: default:
222: try {
223: return parent::__get($strName);
224: } catch (QCallerException $objExc) { $objExc->IncrementOffset(); throw $objExc; }
225: }
226: }
227:
228: /////////////////////////
229: // Public Properties: SET
230: /////////////////////////
231: /**
232: * PHP magic method
233: *
234: * @param string $strName Property name
235: * @param string $mixValue Property value
236: *
237: * @return mixed
238: * @throws Exception|QCallerException
239: */
240: public function __set($strName, $mixValue) {
241: $this->blnModified = true;
242:
243: switch ($strName) {
244:
245: // Deprecated
246: case 'TargetControlId':
247: try {
248: return ($this->strTargetControlId = QType::Cast($mixValue, QType::String));
249: } catch (QCallerException $objExc) { $objExc->IncrementOffset(); throw $objExc; }
250:
251: default:
252: try {
253: return (parent::__set($strName, $mixValue));
254: } catch (QCallerException $objExc) { $objExc->IncrementOffset(); throw $objExc; }
255: }
256: }
257: }