1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Call.php - The Jaxon Call |
5
|
|
|
* |
6
|
|
|
* This class is used to create js ajax requests to callable classes and functions. |
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\Js; |
22
|
|
|
|
23
|
|
|
use Jaxon\App\Dialog\DialogManager; |
24
|
|
|
use JsonSerializable; |
25
|
|
|
use Stringable; |
26
|
|
|
|
27
|
|
|
use function array_map; |
28
|
|
|
use function array_shift; |
29
|
|
|
use function func_get_args; |
30
|
|
|
|
31
|
|
|
class Call |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var DialogManager |
35
|
|
|
*/ |
36
|
|
|
protected $xDialogManager; |
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The name of the javascript function |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
private $sFunction; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var array<ParameterInterface> |
47
|
|
|
*/ |
48
|
|
|
protected $aParameters = []; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Convert the parameter value to integer |
52
|
|
|
* |
53
|
|
|
* @var bool |
|
|
|
|
54
|
|
|
*/ |
55
|
|
|
protected $bToInt = false; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* The arguments of the else() calls |
59
|
|
|
* |
60
|
|
|
* @var array |
61
|
|
|
*/ |
62
|
|
|
protected $aMessage = []; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* A condition to check before making the call |
66
|
|
|
* |
67
|
|
|
* @var array |
68
|
|
|
*/ |
69
|
|
|
protected $aCondition = []; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* The arguments of the confirm() call |
73
|
|
|
* |
74
|
|
|
* @var array |
75
|
|
|
*/ |
76
|
|
|
protected $aConfirm = []; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* The constructor. |
80
|
|
|
* |
81
|
|
|
* @param string $sFunction The javascript function |
|
|
|
|
82
|
|
|
*/ |
83
|
|
|
public function __construct(string $sFunction) |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
$this->sFunction = $sFunction; |
86
|
|
|
} |
|
|
|
|
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return Call |
90
|
|
|
*/ |
91
|
|
|
public function toInt(): Call |
92
|
|
|
{ |
93
|
|
|
$this->bToInt = true; |
94
|
|
|
return $this; |
95
|
|
|
} |
|
|
|
|
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Clear the parameter list associated with this request |
99
|
|
|
* |
100
|
|
|
* @return Call |
101
|
|
|
*/ |
102
|
|
|
public function clearParameters(): Call |
103
|
|
|
{ |
104
|
|
|
$this->aParameters = []; |
105
|
|
|
return $this; |
106
|
|
|
} |
|
|
|
|
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Set the value of the parameter at the given position |
110
|
|
|
* |
111
|
|
|
* @param ParameterInterface $xParameter The value to be used |
|
|
|
|
112
|
|
|
* |
113
|
|
|
* @return Call |
114
|
|
|
*/ |
115
|
|
|
public function pushParameter(ParameterInterface $xParameter): Call |
116
|
|
|
{ |
117
|
|
|
$this->aParameters[] = $xParameter; |
118
|
|
|
return $this; |
119
|
|
|
} |
|
|
|
|
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Add a parameter value to the parameter list for this request |
123
|
|
|
* |
124
|
|
|
* @param string $sType The type of the value to be used |
|
|
|
|
125
|
|
|
* @param string $sValue The value to be used |
|
|
|
|
126
|
|
|
* |
127
|
|
|
* Types should be one of the following <Parameter::FORM_VALUES>, <Parameter::QUOTED_VALUE>, <Parameter::NUMERIC_VALUE>, |
|
|
|
|
128
|
|
|
* <Parameter::JS_VALUE>, <Parameter::INPUT_VALUE>, <Parameter::CHECKED_VALUE>, <Parameter::PAGE_NUMBER>. |
|
|
|
|
129
|
|
|
* The value should be as follows: |
|
|
|
|
130
|
|
|
* - <Parameter::FORM_VALUES> - Use the ID of the form you want to process. |
|
|
|
|
131
|
|
|
* - <Parameter::QUOTED_VALUE> - The string data to be passed. |
|
|
|
|
132
|
|
|
* - <Parameter::JS_VALUE> - A string containing valid javascript |
|
|
|
|
133
|
|
|
* (either a javascript variable name that will be in scope at the time of the call or |
|
|
|
|
134
|
|
|
* a javascript function call whose return value will become the parameter). |
|
|
|
|
135
|
|
|
* |
136
|
|
|
* @return Call |
137
|
|
|
*/ |
138
|
|
|
public function addParameter(string $sType, string $sValue): Call |
139
|
|
|
{ |
140
|
|
|
$this->pushParameter(new Parameter($sType, $sValue)); |
141
|
|
|
return $this; |
142
|
|
|
} |
|
|
|
|
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Add a set of parameters to this request |
146
|
|
|
* |
147
|
|
|
* @param array $aParameters The parameters |
|
|
|
|
148
|
|
|
* |
149
|
|
|
* @return Call |
150
|
|
|
*/ |
151
|
|
|
public function addParameters(array $aParameters): Call |
152
|
|
|
{ |
153
|
|
|
foreach($aParameters as $xParameter) |
154
|
|
|
{ |
155
|
|
|
$this->pushParameter(Parameter::make($xParameter)); |
156
|
|
|
} |
157
|
|
|
return $this; |
158
|
|
|
} |
|
|
|
|
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param array $aArgs |
|
|
|
|
162
|
|
|
* |
163
|
|
|
* @return array |
164
|
|
|
*/ |
165
|
|
|
private function getArgs(array $aArgs): array |
166
|
|
|
{ |
167
|
|
|
array_shift($aArgs); |
168
|
|
|
return $aArgs; |
169
|
|
|
} |
|
|
|
|
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param DialogManager $xDialogManager |
|
|
|
|
173
|
|
|
* |
174
|
|
|
* @return void |
175
|
|
|
*/ |
176
|
|
|
public function setDialogManager(DialogManager $xDialogManager) |
177
|
|
|
{ |
178
|
|
|
$this->xDialogManager = $xDialogManager; |
179
|
|
|
} |
|
|
|
|
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Show a message if the condition to the call is not met |
183
|
|
|
* |
184
|
|
|
* @param string $sMessage The message to show |
|
|
|
|
185
|
|
|
* |
186
|
|
|
* @return Call |
187
|
|
|
*/ |
188
|
|
|
public function elseShow(string $sMessage): Call |
189
|
|
|
{ |
190
|
|
|
$this->aMessage = $this->xDialogManager->warning($sMessage, $this->getArgs(func_get_args())); |
191
|
|
|
return $this; |
192
|
|
|
} |
|
|
|
|
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Show an information message if the condition to the call is not met |
196
|
|
|
* |
197
|
|
|
* @param string $sMessage The message to show |
|
|
|
|
198
|
|
|
* |
199
|
|
|
* @return Call |
200
|
|
|
*/ |
201
|
|
|
public function elseInfo(string $sMessage): Call |
202
|
|
|
{ |
203
|
|
|
$this->aMessage = $this->xDialogManager->info($sMessage, $this->getArgs(func_get_args())); |
204
|
|
|
return $this; |
205
|
|
|
} |
|
|
|
|
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Show a success message if the condition to the call is not met |
209
|
|
|
* |
210
|
|
|
* @param string $sMessage The message to show |
|
|
|
|
211
|
|
|
* |
212
|
|
|
* @return Call |
213
|
|
|
*/ |
214
|
|
|
public function elseSuccess(string $sMessage): Call |
215
|
|
|
{ |
216
|
|
|
$this->aMessage = $this->xDialogManager->success($sMessage, $this->getArgs(func_get_args())); |
217
|
|
|
return $this; |
218
|
|
|
} |
|
|
|
|
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Show a warning message if the condition to the call is not met |
222
|
|
|
* |
223
|
|
|
* @param string $sMessage The message to show |
|
|
|
|
224
|
|
|
* |
225
|
|
|
* @return Call |
226
|
|
|
*/ |
227
|
|
|
public function elseWarning(string $sMessage): Call |
228
|
|
|
{ |
229
|
|
|
$this->aMessage = $this->xDialogManager->warning($sMessage, $this->getArgs(func_get_args())); |
230
|
|
|
return $this; |
231
|
|
|
} |
|
|
|
|
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Show an error message if the condition to the call is not met |
235
|
|
|
* |
236
|
|
|
* @param string $sMessage The message to show |
|
|
|
|
237
|
|
|
* |
238
|
|
|
* @return Call |
239
|
|
|
*/ |
240
|
|
|
public function elseError(string $sMessage): Call |
241
|
|
|
{ |
242
|
|
|
$this->aMessage = $this->xDialogManager->error($sMessage, $this->getArgs(func_get_args())); |
243
|
|
|
return $this; |
244
|
|
|
} |
|
|
|
|
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Add a confirmation question to the request |
248
|
|
|
* |
249
|
|
|
* @param string $sQuestion The question to ask |
|
|
|
|
250
|
|
|
* |
251
|
|
|
* @return Call |
252
|
|
|
*/ |
253
|
|
|
public function confirm(string $sQuestion): Call |
254
|
|
|
{ |
255
|
|
|
$this->aConfirm = $this->xDialogManager->confirm($sQuestion, $this->getArgs(func_get_args())); |
256
|
|
|
return $this; |
257
|
|
|
} |
|
|
|
|
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Check if a value is equal to another before sending the request |
261
|
|
|
* |
262
|
|
|
* @param mixed $xValue1 The first value to compare |
|
|
|
|
263
|
|
|
* @param mixed $xValue2 The second value to compare |
|
|
|
|
264
|
|
|
* |
265
|
|
|
* @return Call |
266
|
|
|
*/ |
267
|
|
|
public function ifeq($xValue1, $xValue2): Call |
268
|
|
|
{ |
269
|
|
|
$this->aCondition = ['eq', Parameter::make($xValue1), Parameter::make($xValue2)]; |
270
|
|
|
return $this; |
271
|
|
|
} |
|
|
|
|
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Check if a value is equal to another before sending the request |
275
|
|
|
* |
276
|
|
|
* @param mixed $xValue1 The first value to compare |
|
|
|
|
277
|
|
|
* @param mixed $xValue2 The second value to compare |
|
|
|
|
278
|
|
|
* |
279
|
|
|
* @return Call |
280
|
|
|
*/ |
281
|
|
|
public function ifteq($xValue1, $xValue2): Call |
282
|
|
|
{ |
283
|
|
|
$this->aCondition = ['teq', Parameter::make($xValue1), Parameter::make($xValue2)]; |
284
|
|
|
return $this; |
285
|
|
|
} |
|
|
|
|
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Check if a value is not equal to another before sending the request |
289
|
|
|
* |
290
|
|
|
* @param mixed $xValue1 The first value to compare |
|
|
|
|
291
|
|
|
* @param mixed $xValue2 The second value to compare |
|
|
|
|
292
|
|
|
* |
293
|
|
|
* @return Call |
294
|
|
|
*/ |
295
|
|
|
public function ifne($xValue1, $xValue2): Call |
296
|
|
|
{ |
297
|
|
|
$this->aCondition = ['ne', Parameter::make($xValue1), Parameter::make($xValue2)]; |
298
|
|
|
return $this; |
299
|
|
|
} |
|
|
|
|
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Check if a value is not equal to another before sending the request |
303
|
|
|
* |
304
|
|
|
* @param mixed $xValue1 The first value to compare |
|
|
|
|
305
|
|
|
* @param mixed $xValue2 The second value to compare |
|
|
|
|
306
|
|
|
* |
307
|
|
|
* @return Call |
308
|
|
|
*/ |
309
|
|
|
public function ifnte($xValue1, $xValue2): Call |
310
|
|
|
{ |
311
|
|
|
$this->aCondition = ['nte', Parameter::make($xValue1), Parameter::make($xValue2)]; |
312
|
|
|
return $this; |
313
|
|
|
} |
|
|
|
|
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* Check if a value is greater than another before sending the request |
317
|
|
|
* |
318
|
|
|
* @param mixed $xValue1 The first value to compare |
|
|
|
|
319
|
|
|
* @param mixed $xValue2 The second value to compare |
|
|
|
|
320
|
|
|
* |
321
|
|
|
* @return Call |
322
|
|
|
*/ |
323
|
|
|
public function ifgt($xValue1, $xValue2): Call |
324
|
|
|
{ |
325
|
|
|
$this->aCondition = ['gt', Parameter::make($xValue1), Parameter::make($xValue2)]; |
326
|
|
|
return $this; |
327
|
|
|
} |
|
|
|
|
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* Check if a value is greater or equal to another before sending the request |
331
|
|
|
* |
332
|
|
|
* @param mixed $xValue1 The first value to compare |
|
|
|
|
333
|
|
|
* @param mixed $xValue2 The second value to compare |
|
|
|
|
334
|
|
|
* |
335
|
|
|
* @return Call |
336
|
|
|
*/ |
337
|
|
|
public function ifge($xValue1, $xValue2): Call |
338
|
|
|
{ |
339
|
|
|
$this->aCondition = ['ge', Parameter::make($xValue1), Parameter::make($xValue2)]; |
340
|
|
|
return $this; |
341
|
|
|
} |
|
|
|
|
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* Check if a value is lower than another before sending the request |
345
|
|
|
* |
346
|
|
|
* @param mixed $xValue1 The first value to compare |
|
|
|
|
347
|
|
|
* @param mixed $xValue2 The second value to compare |
|
|
|
|
348
|
|
|
* |
349
|
|
|
* @return Call |
350
|
|
|
*/ |
351
|
|
|
public function iflt($xValue1, $xValue2): Call |
352
|
|
|
{ |
353
|
|
|
$this->aCondition = ['lt', Parameter::make($xValue1), Parameter::make($xValue2)]; |
354
|
|
|
return $this; |
355
|
|
|
} |
|
|
|
|
356
|
|
|
|
357
|
|
|
/** |
358
|
|
|
* Check if a value is lower or equal to another before sending the request |
359
|
|
|
* |
360
|
|
|
* @param mixed $xValue1 The first value to compare |
|
|
|
|
361
|
|
|
* @param mixed $xValue2 The second value to compare |
|
|
|
|
362
|
|
|
* |
363
|
|
|
* @return Call |
364
|
|
|
*/ |
365
|
|
|
public function ifle($xValue1, $xValue2): Call |
366
|
|
|
{ |
367
|
|
|
$this->aCondition = ['le', Parameter::make($xValue1), Parameter::make($xValue2)]; |
368
|
|
|
return $this; |
369
|
|
|
} |
|
|
|
|
370
|
|
|
|
371
|
|
|
/** |
372
|
|
|
* Add a condition to the request |
373
|
|
|
* |
374
|
|
|
* The request is sent only if the condition is true. |
375
|
|
|
* |
376
|
|
|
* @param mixed $xCondition The condition to check |
|
|
|
|
377
|
|
|
* |
378
|
|
|
* @return Call |
379
|
|
|
*/ |
380
|
|
|
public function when($xCondition): Call |
381
|
|
|
{ |
382
|
|
|
return $this->ifeq(true, $xCondition); |
383
|
|
|
} |
|
|
|
|
384
|
|
|
|
385
|
|
|
/** |
386
|
|
|
* Add a condition to the request |
387
|
|
|
* |
388
|
|
|
* The request is sent only if the condition is false. |
389
|
|
|
* |
390
|
|
|
* @param mixed $xCondition The condition to check |
|
|
|
|
391
|
|
|
* |
392
|
|
|
* @return Call |
393
|
|
|
*/ |
394
|
|
|
public function unless($xCondition): Call |
395
|
|
|
{ |
396
|
|
|
return $this->ifeq(false, $xCondition); |
397
|
|
|
} |
|
|
|
|
398
|
|
|
|
399
|
|
|
/** |
400
|
|
|
* Check if the request has a parameter of type Parameter::PAGE_NUMBER |
401
|
|
|
* |
402
|
|
|
* @return ParameterInterface|null |
403
|
|
|
*/ |
404
|
|
|
private function findPageNumber(): ?ParameterInterface |
405
|
|
|
{ |
406
|
|
|
foreach($this->aParameters as $xParameter) |
407
|
|
|
{ |
408
|
|
|
if($xParameter->getType() === Parameter::PAGE_NUMBER) |
409
|
|
|
{ |
410
|
|
|
return $xParameter; |
411
|
|
|
} |
412
|
|
|
} |
413
|
|
|
return null; |
414
|
|
|
} |
|
|
|
|
415
|
|
|
|
416
|
|
|
/** |
417
|
|
|
* Check if the request has a parameter of type Parameter::PAGE_NUMBER |
418
|
|
|
* |
419
|
|
|
* @return bool |
420
|
|
|
*/ |
421
|
|
|
public function hasPageNumber(): bool |
422
|
|
|
{ |
423
|
|
|
return $this->findPageNumber() !== null; |
424
|
|
|
} |
|
|
|
|
425
|
|
|
|
426
|
|
|
/** |
427
|
|
|
* Set a value to the Parameter::PAGE_NUMBER parameter |
428
|
|
|
* |
429
|
|
|
* @param integer $nPageNumber The current page number |
|
|
|
|
430
|
|
|
* |
431
|
|
|
* @return Call |
432
|
|
|
*/ |
433
|
|
|
public function setPageNumber(int $nPageNumber): Call |
434
|
|
|
{ |
435
|
|
|
/** @var Parameter */ |
|
|
|
|
436
|
|
|
$xParameter = $this->findPageNumber(); |
437
|
|
|
if($xParameter !== null) |
438
|
|
|
{ |
439
|
|
|
$xParameter->setValue($nPageNumber); |
|
|
|
|
440
|
|
|
} |
441
|
|
|
return $this; |
442
|
|
|
} |
|
|
|
|
443
|
|
|
|
444
|
|
|
/** |
445
|
|
|
* Convert this call to array |
446
|
|
|
* |
447
|
|
|
* @return array |
448
|
|
|
*/ |
449
|
|
|
public function toArray(): array |
450
|
|
|
{ |
451
|
|
|
$aCalls = [[ |
452
|
|
|
'_type' => 'func', |
453
|
|
|
'_name' => $this->sFunction, |
454
|
|
|
'args' => array_map(function(JsonSerializable $xParam) { |
455
|
|
|
return $xParam->jsonSerialize(); |
456
|
|
|
}, $this->aParameters), |
457
|
|
|
]]; |
|
|
|
|
458
|
|
|
if($this->bToInt) |
459
|
|
|
{ |
460
|
|
|
$aCalls[] = [ |
461
|
|
|
'_type' => 'func', |
462
|
|
|
'_name' => 'toInt', |
463
|
|
|
'args' => [[ '_type' => '_', '_name' => 'this' ]], |
464
|
|
|
]; |
465
|
|
|
} |
466
|
|
|
|
467
|
|
|
$aCall = ['_type' => 'expr', 'calls' => $aCalls]; |
468
|
|
|
if(($this->aConfirm)) |
|
|
|
|
469
|
|
|
{ |
470
|
|
|
$aCall['question'] = $this->aConfirm; |
471
|
|
|
} |
472
|
|
|
if(($this->aCondition)) |
|
|
|
|
473
|
|
|
{ |
474
|
|
|
$aCall['condition'] = $this->aCondition; |
475
|
|
|
} |
476
|
|
|
if(($this->aMessage)) |
|
|
|
|
477
|
|
|
{ |
478
|
|
|
$aCall['message'] = $this->aMessage; |
479
|
|
|
} |
480
|
|
|
|
481
|
|
|
return $aCall; |
482
|
|
|
} |
|
|
|
|
483
|
|
|
|
484
|
|
|
/** |
485
|
|
|
* Convert this call to array, when converting the response into json. |
486
|
|
|
* |
487
|
|
|
* This is a method of the JsonSerializable interface. |
488
|
|
|
* |
489
|
|
|
* @return array |
490
|
|
|
*/ |
491
|
|
|
public function jsonSerialize(): array |
492
|
|
|
{ |
493
|
|
|
return $this->toArray(); |
494
|
|
|
} |
|
|
|
|
495
|
|
|
|
496
|
|
|
/** |
497
|
|
|
* Returns a string representation of the script output (javascript) from this request object |
498
|
|
|
* |
499
|
|
|
* @return string |
500
|
|
|
*/ |
501
|
|
|
public function __toString() |
502
|
|
|
{ |
503
|
|
|
$aParameters = array_map(function(Stringable $xParam) { |
504
|
|
|
return $xParam->__toString(); |
505
|
|
|
}, $this->aParameters); |
506
|
|
|
$sScript = $this->sFunction . '(' . implode(', ', $aParameters) . ')'; |
|
|
|
|
507
|
|
|
return $this->bToInt ? "parseInt($sScript)" : $sScript; |
508
|
|
|
} |
|
|
|
|
509
|
|
|
} |
510
|
|
|
|