|
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\Call; |
|
22
|
|
|
|
|
23
|
|
|
use Jaxon\Plugin\Response\JQuery\DomSelector; |
|
24
|
|
|
use Jaxon\Ui\Dialog\Library\DialogLibraryManager; |
|
25
|
|
|
|
|
26
|
|
|
use function array_shift; |
|
27
|
|
|
use function implode; |
|
28
|
|
|
|
|
29
|
|
|
class Call extends JsCall |
|
|
|
|
|
|
30
|
|
|
{ |
|
31
|
|
|
use Traits\CallConditionTrait; |
|
32
|
|
|
use Traits\CallMessageTrait; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var DialogLibraryManager |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $xDialogLibraryManager; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var Paginator |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $xPaginator; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var array |
|
46
|
|
|
*/ |
|
47
|
|
|
private $aVariables; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var string |
|
51
|
|
|
*/ |
|
52
|
|
|
private $sVars; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var int |
|
|
|
|
|
|
56
|
|
|
*/ |
|
57
|
|
|
private $nVarId; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* The constructor. |
|
61
|
|
|
* |
|
62
|
|
|
* @param string $sName The javascript function or method name |
|
|
|
|
|
|
63
|
|
|
* @param DialogLibraryManager $xDialogLibraryManager |
|
|
|
|
|
|
64
|
|
|
* @param Paginator $xPaginator |
|
|
|
|
|
|
65
|
|
|
*/ |
|
66
|
|
|
public function __construct(string $sName, DialogLibraryManager $xDialogLibraryManager, Paginator $xPaginator) |
|
|
|
|
|
|
67
|
|
|
{ |
|
68
|
|
|
parent::__construct($sName); |
|
69
|
|
|
$this->xDialogLibraryManager = $xDialogLibraryManager; |
|
70
|
|
|
$this->xPaginator = $xPaginator; |
|
|
|
|
|
|
71
|
|
|
} |
|
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Make unique js vars for parameters of type DomSelector |
|
75
|
|
|
* |
|
76
|
|
|
* @param ParameterInterface $xParameter |
|
|
|
|
|
|
77
|
|
|
* |
|
78
|
|
|
* @return ParameterInterface |
|
79
|
|
|
*/ |
|
80
|
|
|
private function _makeUniqueJsVar(ParameterInterface $xParameter): ParameterInterface |
|
81
|
|
|
{ |
|
82
|
|
|
if($xParameter instanceof DomSelector) |
|
83
|
|
|
{ |
|
84
|
|
|
$sParameterStr = $xParameter->getScript(); |
|
85
|
|
|
if(!isset($this->aVariables[$sParameterStr])) |
|
86
|
|
|
{ |
|
87
|
|
|
// The value is not yet defined. A new variable is created. |
|
88
|
|
|
$sVarName = 'jxnVar' . $this->nVarId; |
|
|
|
|
|
|
89
|
|
|
$this->aVariables[$sParameterStr] = $sVarName; |
|
90
|
|
|
$this->sVars .= "$sVarName=$xParameter;"; |
|
|
|
|
|
|
91
|
|
|
$this->nVarId++; |
|
92
|
|
|
} |
|
93
|
|
|
else |
|
94
|
|
|
{ |
|
95
|
|
|
// The value is already defined. The corresponding variable is assigned. |
|
96
|
|
|
$sVarName = $this->aVariables[$sParameterStr]; |
|
97
|
|
|
} |
|
98
|
|
|
$xParameter = new Parameter(Parameter::JS_VALUE, $sVarName); |
|
99
|
|
|
} |
|
100
|
|
|
return $xParameter; |
|
101
|
|
|
} |
|
|
|
|
|
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Make a phrase to be displayed in js code |
|
105
|
|
|
* |
|
106
|
|
|
* @param array $aArgs |
|
|
|
|
|
|
107
|
|
|
* |
|
108
|
|
|
* @return string |
|
109
|
|
|
*/ |
|
110
|
|
|
private function makePhrase(array $aArgs): string |
|
111
|
|
|
{ |
|
112
|
|
|
if(empty($aArgs)) |
|
113
|
|
|
{ |
|
114
|
|
|
return ''; |
|
115
|
|
|
} |
|
116
|
|
|
// The first array entry is the message. |
|
117
|
|
|
$sPhrase = array_shift($aArgs); |
|
118
|
|
|
if(empty($aArgs)) |
|
119
|
|
|
{ |
|
120
|
|
|
return $sPhrase; |
|
121
|
|
|
} |
|
122
|
|
|
$nParamId = 1; |
|
123
|
|
|
foreach($aArgs as &$xParameter) |
|
124
|
|
|
{ |
|
125
|
|
|
$xParameter = $this->_makeUniqueJsVar($xParameter); |
|
126
|
|
|
$xParameter = "'$nParamId':" . $xParameter->getScript(); |
|
127
|
|
|
$nParamId++; |
|
128
|
|
|
} |
|
129
|
|
|
$sPhrase .= '.supplant({' . implode(',', $aArgs) . '})'; |
|
130
|
|
|
return $sPhrase; |
|
131
|
|
|
} |
|
|
|
|
|
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Make a phrase to be displayed in js code |
|
135
|
|
|
* |
|
136
|
|
|
* @return string |
|
137
|
|
|
*/ |
|
138
|
|
|
private function makeMessage(): string |
|
139
|
|
|
{ |
|
140
|
|
|
if(!($sPhrase = $this->makePhrase($this->aMessageArgs))) |
|
|
|
|
|
|
141
|
|
|
{ |
|
142
|
|
|
return ''; |
|
143
|
|
|
} |
|
144
|
|
|
$sMethod = $this->sMessageType; |
|
145
|
|
|
$xLibrary = $this->xDialogLibraryManager->getMessageLibrary(); |
|
146
|
|
|
$xLibrary->setReturnCode(true); |
|
147
|
|
|
return $xLibrary->$sMethod($sPhrase); |
|
148
|
|
|
} |
|
|
|
|
|
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Returns a string representation of the script output (javascript) from this request object |
|
152
|
|
|
* |
|
153
|
|
|
* @return string |
|
154
|
|
|
*/ |
|
155
|
|
|
public function getScript(): string |
|
156
|
|
|
{ |
|
157
|
|
|
/* |
|
158
|
|
|
* JQuery variables sometimes depend on the context where they are used, eg. when their value depends on $(this). |
|
159
|
|
|
* When a confirmation question is added, the Jaxon calls are made in a different context, |
|
160
|
|
|
* making those variables invalid. |
|
161
|
|
|
* To avoid issues related to these context changes, the JQuery selectors values are first saved into |
|
162
|
|
|
* local variables, which are then used in Jaxon function calls. |
|
163
|
|
|
*/ |
|
|
|
|
|
|
164
|
|
|
$this->sVars = ''; // Javascript code defining all the variables values. |
|
165
|
|
|
$this->nVarId = 1; // Position of the variables, starting from 1. |
|
166
|
|
|
// This array will avoid declaring multiple variables with the same value. |
|
167
|
|
|
// The array key is the variable value, while the array value is the variable name. |
|
168
|
|
|
$this->aVariables = []; // Array of local variables. |
|
169
|
|
|
foreach($this->aParameters as &$xParameter) |
|
170
|
|
|
{ |
|
171
|
|
|
$xParameter = $this->_makeUniqueJsVar($xParameter); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
$sMessageScript = $this->makeMessage(); |
|
175
|
|
|
$sScript = parent::getScript(); |
|
|
|
|
|
|
176
|
|
|
if($this->sCondition === '__confirm__') |
|
177
|
|
|
{ |
|
178
|
|
|
$sConfirmPhrase = $this->makePhrase($this->aConfirmArgs); |
|
179
|
|
|
$sScript = $this->xDialogLibraryManager->getQuestionLibrary() |
|
|
|
|
|
|
180
|
|
|
->confirm($sConfirmPhrase, $sScript, $sMessageScript); |
|
|
|
|
|
|
181
|
|
|
} |
|
182
|
|
|
elseif($this->sCondition !== '') |
|
183
|
|
|
{ |
|
184
|
|
|
$sScript = empty($sMessageScript) ? 'if(' . $this->sCondition . '){' . $sScript . ';}' : |
|
|
|
|
|
|
185
|
|
|
'if(' . $this->sCondition . '){' . $sScript . ';}else{' . $sMessageScript . ';}'; |
|
186
|
|
|
} |
|
187
|
|
|
return $this->sVars . $sScript; |
|
188
|
|
|
} |
|
|
|
|
|
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Check if the request has a parameter of type Parameter::PAGE_NUMBER |
|
192
|
|
|
* |
|
193
|
|
|
* @return bool |
|
194
|
|
|
*/ |
|
195
|
|
|
public function hasPageNumber(): bool |
|
196
|
|
|
{ |
|
197
|
|
|
foreach($this->aParameters as $xParameter) |
|
198
|
|
|
{ |
|
199
|
|
|
if($xParameter->getType() === Parameter::PAGE_NUMBER) |
|
200
|
|
|
{ |
|
201
|
|
|
return true; |
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
|
|
return false; |
|
205
|
|
|
} |
|
|
|
|
|
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* Set a value to the Parameter::PAGE_NUMBER parameter |
|
209
|
|
|
* |
|
210
|
|
|
* @param integer $nPageNumber The current page number |
|
|
|
|
|
|
211
|
|
|
* |
|
212
|
|
|
* @return Call |
|
213
|
|
|
*/ |
|
214
|
|
|
public function setPageNumber(int $nPageNumber): Call |
|
215
|
|
|
{ |
|
216
|
|
|
// Set the value of the Parameter::PAGE_NUMBER parameter |
|
217
|
|
|
foreach($this->aParameters as $xParameter) |
|
218
|
|
|
{ |
|
219
|
|
|
if($xParameter->getType() === Parameter::PAGE_NUMBER) |
|
220
|
|
|
{ |
|
221
|
|
|
$xParameter->setValue($nPageNumber); |
|
222
|
|
|
break; |
|
223
|
|
|
} |
|
224
|
|
|
} |
|
225
|
|
|
return $this; |
|
226
|
|
|
} |
|
|
|
|
|
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* Make the pagination links for this request |
|
230
|
|
|
* |
|
231
|
|
|
* @param integer $nCurrentPage The current page |
|
|
|
|
|
|
232
|
|
|
* @param integer $nItemsPerPage The number of items per page |
|
|
|
|
|
|
233
|
|
|
* @param integer $nItemsTotal The total number of items |
|
|
|
|
|
|
234
|
|
|
* |
|
235
|
|
|
* @return array |
|
236
|
|
|
*/ |
|
237
|
|
|
public function pages(int $nCurrentPage, int $nItemsPerPage, int $nItemsTotal): array |
|
238
|
|
|
{ |
|
239
|
|
|
// Append the page number to the parameter list, if not yet given. |
|
240
|
|
|
if(!$this->hasPageNumber()) |
|
241
|
|
|
{ |
|
242
|
|
|
$this->addParameter(Parameter::PAGE_NUMBER, 0); |
|
243
|
|
|
} |
|
244
|
|
|
return $this->xPaginator->setup($this, $nCurrentPage, $nItemsPerPage, $nItemsTotal)->pages(); |
|
245
|
|
|
} |
|
|
|
|
|
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* Make the pagination links for this request |
|
249
|
|
|
* |
|
250
|
|
|
* @param integer $nCurrentPage The current page |
|
|
|
|
|
|
251
|
|
|
* @param integer $nItemsPerPage The number of items per page |
|
|
|
|
|
|
252
|
|
|
* @param integer $nItemsTotal The total number of items |
|
|
|
|
|
|
253
|
|
|
* |
|
254
|
|
|
* @return Paginator |
|
255
|
|
|
*/ |
|
256
|
|
|
public function paginate(int $nCurrentPage, int $nItemsPerPage, int $nItemsTotal): Paginator |
|
257
|
|
|
{ |
|
258
|
|
|
// Append the page number to the parameter list, if not yet given. |
|
259
|
|
|
if(!$this->hasPageNumber()) |
|
260
|
|
|
{ |
|
261
|
|
|
$this->addParameter(Parameter::PAGE_NUMBER, 0); |
|
262
|
|
|
} |
|
263
|
|
|
return $this->xPaginator->setup($this, $nCurrentPage, $nItemsPerPage, $nItemsTotal); |
|
264
|
|
|
} |
|
|
|
|
|
|
265
|
|
|
|
|
266
|
|
|
/** |
|
267
|
|
|
* Make the pagination links for this request |
|
268
|
|
|
* |
|
269
|
|
|
* @param integer $nCurrentPage The current page |
|
|
|
|
|
|
270
|
|
|
* @param integer $nItemsPerPage The number of items per page |
|
|
|
|
|
|
271
|
|
|
* @param integer $nItemsTotal The total number of items |
|
|
|
|
|
|
272
|
|
|
* |
|
273
|
|
|
* @return Paginator |
|
274
|
|
|
*/ |
|
275
|
|
|
public function pg(int $nCurrentPage, int $nItemsPerPage, int $nItemsTotal): Paginator |
|
276
|
|
|
{ |
|
277
|
|
|
return $this->paginate($nCurrentPage, $nItemsPerPage, $nItemsTotal); |
|
278
|
|
|
} |
|
|
|
|
|
|
279
|
|
|
} |
|
280
|
|
|
|