1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Request.php - The Jaxon Request |
5
|
|
|
* |
6
|
|
|
* This class is used to create client side requests to the Jaxon functions and callable objects. |
7
|
|
|
* |
8
|
|
|
* @package jaxon-core |
9
|
|
|
* @author Jared White |
10
|
|
|
* @author J. Max Wilson |
11
|
|
|
* @author Joseph Woolley |
12
|
|
|
* @author Steffen Konerow |
13
|
|
|
* @author Thierry Feuzeu <[email protected]> |
14
|
|
|
* @copyright Copyright (c) 2005-2007 by Jared White & J. Max Wilson |
15
|
|
|
* @copyright Copyright (c) 2008-2010 by Joseph Woolley, Steffen Konerow, Jared White & J. Max Wilson |
16
|
|
|
* @copyright 2016 Thierry Feuzeu <[email protected]> |
17
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License |
18
|
|
|
* @link https://github.com/jaxon-php/jaxon-core |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace Jaxon\Request; |
22
|
|
|
|
23
|
|
|
use JsonSerializable; |
24
|
|
|
use Jaxon\Jaxon; |
25
|
|
|
|
26
|
|
|
class Request extends JsCall |
27
|
|
|
{ |
28
|
|
|
use \Jaxon\Utils\Traits\Config; |
29
|
|
|
use \Jaxon\Utils\Traits\Manager; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The type of the request |
33
|
|
|
* |
34
|
|
|
* Can be one of "function" or "class". |
35
|
|
|
* |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
private $sType; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* A condition to check before sending this request |
42
|
|
|
* |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
protected $sCondition = null; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* The arguments of the confirm() call |
49
|
|
|
* |
50
|
|
|
* @var array |
51
|
|
|
*/ |
52
|
|
|
protected $aMessageArgs = null; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* The constructor. |
56
|
|
|
* |
57
|
|
|
* @param string $sName The javascript function or method name |
58
|
|
|
* @param string $sType The type (function or a method) |
59
|
|
|
*/ |
60
|
|
|
public function __construct($sName, $sType) |
61
|
|
|
{ |
62
|
|
|
parent::__construct($sName); |
63
|
|
|
$this->sType = $sType; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Check if the request has a parameter of type Jaxon::PAGE_NUMBER |
68
|
|
|
* |
69
|
|
|
* @return boolean |
70
|
|
|
*/ |
71
|
|
|
public function hasPageNumber() |
72
|
|
|
{ |
73
|
|
|
foreach($this->aParameters as $xParameter) |
74
|
|
|
{ |
75
|
|
|
if($xParameter->getType() == Jaxon::PAGE_NUMBER) |
76
|
|
|
{ |
77
|
|
|
return true; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
return false; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Set a value to the Jaxon::PAGE_NUMBER parameter |
85
|
|
|
* |
86
|
|
|
* @param integer $nPageNumber The current page number |
87
|
|
|
* |
88
|
|
|
* @return Request |
89
|
|
|
*/ |
90
|
|
|
public function setPageNumber($nPageNumber) |
91
|
|
|
{ |
92
|
|
|
// Set the value of the Jaxon::PAGE_NUMBER parameter |
93
|
|
|
foreach($this->aParameters as $xParameter) |
94
|
|
|
{ |
95
|
|
|
if($xParameter->getType() == Jaxon::PAGE_NUMBER) |
96
|
|
|
{ |
97
|
|
|
$xParameter->setValue(intval($nPageNumber)); |
98
|
|
|
break; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Add a confirmation question to the request |
106
|
|
|
* |
107
|
|
|
* @param string $sQuestion The question to ask |
108
|
|
|
* |
109
|
|
|
* @return Request |
110
|
|
|
*/ |
111
|
|
View Code Duplication |
public function confirm($sQuestion) |
|
|
|
|
112
|
|
|
{ |
113
|
|
|
$this->sCondition = '__confirm__'; |
114
|
|
|
$this->aMessageArgs = func_get_args(); |
115
|
|
|
array_walk($this->aMessageArgs, function (&$xParameter) { |
116
|
|
|
$xParameter = Parameter::make($xParameter); |
117
|
|
|
}); |
118
|
|
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Add a condition to the request |
123
|
|
|
* |
124
|
|
|
* The request is sent only if the condition is true. |
125
|
|
|
* |
126
|
|
|
* @param string $sCondition The condition to check |
127
|
|
|
* |
128
|
|
|
* @return Request |
129
|
|
|
*/ |
130
|
|
|
public function when($sCondition) |
131
|
|
|
{ |
132
|
|
|
$this->sCondition = Parameter::make($sCondition)->getScript(); |
133
|
|
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Add a condition to the request |
138
|
|
|
* |
139
|
|
|
* The request is sent only if the condition is false. |
140
|
|
|
* |
141
|
|
|
* @param string $sCondition The condition to check |
142
|
|
|
* |
143
|
|
|
* @return Request |
144
|
|
|
*/ |
145
|
|
|
public function unless($sCondition) |
146
|
|
|
{ |
147
|
|
|
$this->sCondition = '!(' . Parameter::make($sCondition)->getScript() . ')'; |
148
|
|
|
return $this; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Check if a value is equal to another before sending the request |
153
|
|
|
* |
154
|
|
|
* @param string $sValue1 The first value to compare |
155
|
|
|
* @param string $sValue2 The second value to compare |
156
|
|
|
* |
157
|
|
|
* @return Request |
158
|
|
|
*/ |
159
|
|
|
public function ifeq($sValue1, $sValue2) |
160
|
|
|
{ |
161
|
|
|
$this->sCondition = '(' . Parameter::make($sValue1) . '==' . Parameter::make($sValue2) . ')'; |
162
|
|
|
return $this; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Check if a value is not equal to another before sending the request |
167
|
|
|
* |
168
|
|
|
* @param string $sValue1 The first value to compare |
169
|
|
|
* @param string $sValue2 The second value to compare |
170
|
|
|
* |
171
|
|
|
* @return Request |
172
|
|
|
*/ |
173
|
|
|
public function ifne($sValue1, $sValue2) |
174
|
|
|
{ |
175
|
|
|
$this->sCondition = '(' . Parameter::make($sValue1) . '!=' . Parameter::make($sValue2) . ')'; |
176
|
|
|
return $this; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Check if a value is greater than another before sending the request |
181
|
|
|
* |
182
|
|
|
* @param string $sValue1 The first value to compare |
183
|
|
|
* @param string $sValue2 The second value to compare |
184
|
|
|
* |
185
|
|
|
* @return Request |
186
|
|
|
*/ |
187
|
|
|
public function ifgt($sValue1, $sValue2) |
188
|
|
|
{ |
189
|
|
|
$this->sCondition = '(' . Parameter::make($sValue1) . '>' . Parameter::make($sValue2) . ')'; |
190
|
|
|
return $this; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Check if a value is greater or equal to another before sending the request |
195
|
|
|
* |
196
|
|
|
* @param string $sValue1 The first value to compare |
197
|
|
|
* @param string $sValue2 The second value to compare |
198
|
|
|
* |
199
|
|
|
* @return Request |
200
|
|
|
*/ |
201
|
|
|
public function ifge($sValue1, $sValue2) |
202
|
|
|
{ |
203
|
|
|
$this->sCondition = '(' . Parameter::make($sValue1) . '>=' . Parameter::make($sValue2) . ')'; |
204
|
|
|
return $this; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Check if a value is lower than another before sending the request |
209
|
|
|
* |
210
|
|
|
* @param string $sValue1 The first value to compare |
211
|
|
|
* @param string $sValue2 The second value to compare |
212
|
|
|
* |
213
|
|
|
* @return Request |
214
|
|
|
*/ |
215
|
|
|
public function iflt($sValue1, $sValue2) |
216
|
|
|
{ |
217
|
|
|
$this->sCondition = '(' . Parameter::make($sValue1) . '<' . Parameter::make($sValue2) . ')'; |
218
|
|
|
return $this; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Check if a value is lower or equal to another before sending the request |
223
|
|
|
* |
224
|
|
|
* @param string $sValue1 The first value to compare |
225
|
|
|
* @param string $sValue2 The second value to compare |
226
|
|
|
* |
227
|
|
|
* @return Request |
228
|
|
|
*/ |
229
|
|
|
public function ifle($sValue1, $sValue2) |
230
|
|
|
{ |
231
|
|
|
$this->sCondition = '(' . Parameter::make($sValue1) . '<=' . Parameter::make($sValue2) . ')'; |
232
|
|
|
return $this; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Set the message to show if the condition to send the request is not met |
237
|
|
|
* |
238
|
|
|
* The first parameter is the message to show. The followin allow to insert data from |
239
|
|
|
* the webpage in the message using positional placeholders. |
240
|
|
|
* |
241
|
|
|
* @param string $sMessage The message to show if the request is not sent |
242
|
|
|
* |
243
|
|
|
* @return Request |
244
|
|
|
*/ |
245
|
|
View Code Duplication |
public function else($sMessage) |
|
|
|
|
246
|
|
|
{ |
247
|
|
|
$this->aMessageArgs = func_get_args(); |
248
|
|
|
array_walk($this->aMessageArgs, function (&$xParameter) { |
|
|
|
|
249
|
|
|
$xParameter = Parameter::make($xParameter); |
250
|
|
|
}); |
251
|
|
|
return $this; |
|
|
|
|
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Returns a string representation of the script output (javascript) from this request object |
256
|
|
|
* |
257
|
|
|
* @return string |
258
|
|
|
*/ |
259
|
|
|
public function getScript() |
260
|
|
|
{ |
261
|
|
|
/* |
262
|
|
|
* JQuery variables sometimes depend on the context where they are used, eg. when their value depends on $(this). |
263
|
|
|
* When a confirmation question is added, the Jaxon calls are made in a different context, |
264
|
|
|
* making those variables invalid. |
265
|
|
|
* To avoid issues related to these context changes, the JQuery selectors values are first saved into |
266
|
|
|
* local variables, which are then used in Jaxon function calls. |
267
|
|
|
*/ |
268
|
|
|
$sVars = ''; // Javascript code defining all the variables values. |
269
|
|
|
$nVarId = 1; // Position of the variables, starting from 1. |
270
|
|
|
// This array will avoid declaring multiple variables with the same value. |
271
|
|
|
// The array key is the variable value, while the array value is the variable name. |
272
|
|
|
$aVariables = array(); // Array of local variables. |
273
|
|
|
foreach($this->aParameters as &$xParameter) |
274
|
|
|
{ |
275
|
|
|
$sParameterStr = $xParameter->getScript(); |
276
|
|
View Code Duplication |
if($xParameter instanceof \Jaxon\JQuery\Dom\Element) |
|
|
|
|
277
|
|
|
{ |
278
|
|
|
if(!array_key_exists($sParameterStr, $aVariables)) |
279
|
|
|
{ |
280
|
|
|
// The value is not yet defined. A new variable is created. |
281
|
|
|
$sVarName = "jxnVar$nVarId"; |
282
|
|
|
$aVariables[$sParameterStr] = $sVarName; |
283
|
|
|
$sVars .= "$sVarName=$xParameter;"; |
284
|
|
|
$nVarId++; |
285
|
|
|
} |
286
|
|
|
else |
287
|
|
|
{ |
288
|
|
|
// The value is already defined. The corresponding variable is assigned. |
289
|
|
|
$sVarName = $aVariables[$sParameterStr]; |
290
|
|
|
} |
291
|
|
|
$xParameter = new Parameter(Jaxon::JS_VALUE, $sVarName); |
292
|
|
|
} |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
$sPhrase = ''; |
296
|
|
|
if(count($this->aMessageArgs) > 0) |
297
|
|
|
{ |
298
|
|
|
$sPhrase = array_shift($this->aMessageArgs); // The first array entry is the question. |
299
|
|
|
// $sPhrase = "'" . addslashes($sPhrase) . "'"; // Wrap the phrase with single quotes |
300
|
|
|
if(count($this->aMessageArgs) > 0) |
301
|
|
|
{ |
302
|
|
|
$nParamId = 1; |
303
|
|
|
foreach($this->aMessageArgs as &$xParameter) |
304
|
|
|
{ |
305
|
|
|
$sParameterStr = $xParameter->getScript(); |
306
|
|
View Code Duplication |
if($xParameter instanceof \Jaxon\JQuery\Dom\Element) |
|
|
|
|
307
|
|
|
{ |
308
|
|
|
if(!array_key_exists($sParameterStr, $aVariables)) |
309
|
|
|
{ |
310
|
|
|
// The value is not yet defined. A new variable is created. |
311
|
|
|
$sVarName = "jxnVar$nVarId"; |
312
|
|
|
$aVariables[$sParameterStr] = $sVarName; |
313
|
|
|
$sVars .= "$sVarName=$xParameter;"; |
314
|
|
|
$nVarId++; |
315
|
|
|
} |
316
|
|
|
else |
317
|
|
|
{ |
318
|
|
|
// The value is already defined. The corresponding variable is assigned. |
319
|
|
|
$sVarName = $aVariables[$sParameterStr]; |
320
|
|
|
} |
321
|
|
|
$xParameter = new Parameter(Jaxon::JS_VALUE, $sVarName); |
322
|
|
|
} |
323
|
|
|
$xParameter = "'$nParamId':" . $xParameter->getScript(); |
324
|
|
|
$nParamId++; |
325
|
|
|
} |
326
|
|
|
$sPhrase .= '.supplant({' . implode(',', $this->aMessageArgs) . '})'; |
327
|
|
|
} |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
$sScript = $this->getOption('core.prefix.' . $this->sType) . parent::getScript(); |
331
|
|
|
if($this->sCondition == '__confirm__') |
332
|
|
|
{ |
333
|
|
|
$xConfirm = $this->getPluginManager()->getConfirm(); |
334
|
|
|
$sScript = $xConfirm->confirm($sPhrase, $sScript, ''); |
335
|
|
|
} |
336
|
|
|
elseif($this->sCondition !== null) |
337
|
|
|
{ |
338
|
|
|
$sScript = 'if(' . $this->sCondition . '){' . $sScript . ';}'; |
339
|
|
|
if(($sPhrase)) |
340
|
|
|
{ |
341
|
|
|
$xAlert = $this->getPluginManager()->getAlert(); |
342
|
|
|
$xAlert->setReturn(true); |
343
|
|
|
$sScript .= 'else{' . $xAlert->warning($sPhrase) . ';}'; |
344
|
|
|
} |
345
|
|
|
} |
346
|
|
|
return $sVars . $sScript; |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
/** |
350
|
|
|
* Prints a string representation of the script output (javascript) from this request object |
351
|
|
|
* |
352
|
|
|
* @return void |
353
|
|
|
*/ |
354
|
|
|
public function printScript() |
355
|
|
|
{ |
356
|
|
|
print $this->getScript(); |
357
|
|
|
} |
358
|
|
|
} |
359
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.