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