|
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\App\Dialog\Library\DialogLibraryManager; |
|
24
|
|
|
use Jaxon\Plugin\Response\JQuery\DomSelector; |
|
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 a phrase to be displayed in js code |
|
75
|
|
|
* |
|
76
|
|
|
* @param array $aArgs |
|
|
|
|
|
|
77
|
|
|
* |
|
78
|
|
|
* @return string |
|
79
|
|
|
*/ |
|
80
|
|
|
private function makePhrase(array $aArgs): string |
|
81
|
|
|
{ |
|
82
|
|
|
if(empty($aArgs)) |
|
83
|
|
|
{ |
|
84
|
|
|
return ''; |
|
85
|
|
|
} |
|
86
|
|
|
// The first array entry is the message. |
|
87
|
|
|
$sPhrase = array_shift($aArgs); |
|
88
|
|
|
if(empty($aArgs)) |
|
89
|
|
|
{ |
|
90
|
|
|
return $sPhrase; |
|
91
|
|
|
} |
|
92
|
|
|
$nParamId = 1; |
|
93
|
|
|
foreach($aArgs as &$xParameter) |
|
94
|
|
|
{ |
|
95
|
|
|
$xParameter = "'$nParamId':" . $xParameter->getScript(); |
|
96
|
|
|
$nParamId++; |
|
97
|
|
|
} |
|
98
|
|
|
return $sPhrase . '.supplant({' . implode(',', $aArgs) . '})'; |
|
99
|
|
|
} |
|
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Make a phrase to be displayed in js code |
|
103
|
|
|
* |
|
104
|
|
|
* @return string |
|
105
|
|
|
*/ |
|
106
|
|
|
private function makeMessage(): string |
|
107
|
|
|
{ |
|
108
|
|
|
if(!($sPhrase = $this->makePhrase($this->aMessageArgs))) |
|
|
|
|
|
|
109
|
|
|
{ |
|
110
|
|
|
return ''; |
|
111
|
|
|
} |
|
112
|
|
|
$sMethod = $this->sMessageType; |
|
113
|
|
|
$xLibrary = $this->xDialogLibraryManager->getMessageLibrary(); |
|
114
|
|
|
$xLibrary->setReturnCode(true); |
|
115
|
|
|
return $xLibrary->$sMethod($sPhrase); |
|
116
|
|
|
} |
|
|
|
|
|
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Returns a string representation of the script output (javascript) from this request object |
|
120
|
|
|
* |
|
121
|
|
|
* @return string |
|
122
|
|
|
*/ |
|
123
|
|
|
public function getScript(): string |
|
124
|
|
|
{ |
|
125
|
|
|
$sMessageScript = $this->makeMessage(); |
|
126
|
|
|
$sScript = parent::getScript(); |
|
|
|
|
|
|
127
|
|
|
if($this->bConfirm) |
|
128
|
|
|
{ |
|
129
|
|
|
$sConfirmPhrase = $this->makePhrase($this->aConfirmArgs); |
|
130
|
|
|
$sScript = $this->xDialogLibraryManager->getQuestionLibrary() |
|
|
|
|
|
|
131
|
|
|
->confirm($sConfirmPhrase, $sScript, $sMessageScript); |
|
|
|
|
|
|
132
|
|
|
} |
|
133
|
|
|
if($this->sCondition !== '') |
|
134
|
|
|
{ |
|
135
|
|
|
$sScript = empty($sMessageScript) ? 'if(' . $this->sCondition . '){' . $sScript . ';}' : |
|
|
|
|
|
|
136
|
|
|
'if(' . $this->sCondition . '){' . $sScript . ';}else{' . $sMessageScript . ';}'; |
|
137
|
|
|
} |
|
138
|
|
|
return $this->sVars . $sScript; |
|
139
|
|
|
} |
|
|
|
|
|
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Check if the request has a parameter of type Parameter::PAGE_NUMBER |
|
143
|
|
|
* |
|
144
|
|
|
* @return bool |
|
145
|
|
|
*/ |
|
146
|
|
|
public function hasPageNumber(): bool |
|
147
|
|
|
{ |
|
148
|
|
|
foreach($this->aParameters as $xParameter) |
|
149
|
|
|
{ |
|
150
|
|
|
if($xParameter->getType() === Parameter::PAGE_NUMBER) |
|
151
|
|
|
{ |
|
152
|
|
|
return true; |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
return false; |
|
156
|
|
|
} |
|
|
|
|
|
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Set a value to the Parameter::PAGE_NUMBER parameter |
|
160
|
|
|
* |
|
161
|
|
|
* @param integer $nPageNumber The current page number |
|
|
|
|
|
|
162
|
|
|
* |
|
163
|
|
|
* @return Call |
|
164
|
|
|
*/ |
|
165
|
|
|
public function setPageNumber(int $nPageNumber): Call |
|
166
|
|
|
{ |
|
167
|
|
|
// Set the value of the Parameter::PAGE_NUMBER parameter |
|
168
|
|
|
foreach($this->aParameters as $xParameter) |
|
169
|
|
|
{ |
|
170
|
|
|
if($xParameter->getType() === Parameter::PAGE_NUMBER) |
|
171
|
|
|
{ |
|
172
|
|
|
$xParameter->setValue($nPageNumber); |
|
173
|
|
|
break; |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
return $this; |
|
177
|
|
|
} |
|
|
|
|
|
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Make the pagination links for this request |
|
181
|
|
|
* |
|
182
|
|
|
* @param integer $nCurrentPage The current page |
|
|
|
|
|
|
183
|
|
|
* @param integer $nItemsPerPage The number of items per page |
|
|
|
|
|
|
184
|
|
|
* @param integer $nItemsTotal The total number of items |
|
|
|
|
|
|
185
|
|
|
* |
|
186
|
|
|
* @return Paginator |
|
187
|
|
|
*/ |
|
188
|
|
|
public function pg(int $nCurrentPage, int $nItemsPerPage, int $nItemsTotal): Paginator |
|
189
|
|
|
{ |
|
190
|
|
|
// Append the page number to the parameter list, if not yet given. |
|
191
|
|
|
if(!$this->hasPageNumber()) |
|
192
|
|
|
{ |
|
193
|
|
|
$this->addParameter(Parameter::PAGE_NUMBER, 0); |
|
194
|
|
|
} |
|
195
|
|
|
return $this->xPaginator->setup($this, $nCurrentPage, $nItemsPerPage, $nItemsTotal); |
|
196
|
|
|
} |
|
|
|
|
|
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Make the pagination links for this request |
|
200
|
|
|
* |
|
201
|
|
|
* @param integer $nCurrentPage The current page |
|
|
|
|
|
|
202
|
|
|
* @param integer $nItemsPerPage The number of items per page |
|
|
|
|
|
|
203
|
|
|
* @param integer $nItemsTotal The total number of items |
|
|
|
|
|
|
204
|
|
|
* |
|
205
|
|
|
* @return Paginator |
|
206
|
|
|
*/ |
|
207
|
|
|
public function paginate(int $nCurrentPage, int $nItemsPerPage, int $nItemsTotal): Paginator |
|
208
|
|
|
{ |
|
209
|
|
|
return $this->pg($nCurrentPage, $nItemsPerPage, $nItemsTotal); |
|
210
|
|
|
} |
|
|
|
|
|
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Make the pagination links for this request |
|
214
|
|
|
* |
|
215
|
|
|
* @param integer $nCurrentPage The current page |
|
|
|
|
|
|
216
|
|
|
* @param integer $nItemsPerPage The number of items per page |
|
|
|
|
|
|
217
|
|
|
* @param integer $nItemsTotal The total number of items |
|
|
|
|
|
|
218
|
|
|
* |
|
219
|
|
|
* @return array |
|
220
|
|
|
*/ |
|
221
|
|
|
public function pages(int $nCurrentPage, int $nItemsPerPage, int $nItemsTotal): array |
|
222
|
|
|
{ |
|
223
|
|
|
return $this->pg($nCurrentPage, $nItemsPerPage, $nItemsTotal)->pages(); |
|
224
|
|
|
} |
|
|
|
|
|
|
225
|
|
|
} |
|
226
|
|
|
|