Completed
Push — master ( fff73d...f066e6 )
by Thierry
01:44
created

src/Request/Request.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\Manager;
29
30
    /**
31
     * A condition to check before sending this request
32
     *
33
     * @var string
34
     */
35
    protected $sCondition = null;
36
37
    /**
38
     * The arguments of the confirm() call
39
     *
40
     * @var array
41
     */
42
    protected $aMessageArgs = [];
43
44
    /**
45
     * The constructor.
46
     *
47
     * @param string        $sName            The javascript function or method name
48
     */
49
    public function __construct($sName)
50
    {
51
        parent::__construct($sName);
52
    }
53
54
    /**
55
     * Check if the request has a parameter of type Jaxon::PAGE_NUMBER
56
     *
57
     * @return boolean
58
     */
59
    public function hasPageNumber()
60
    {
61
        foreach($this->aParameters as $xParameter)
62
        {
63
            if($xParameter->getType() == Jaxon::PAGE_NUMBER)
64
            {
65
                return true;
66
            }
67
        }
68
        return false;
69
    }
70
71
    /**
72
     * Set a value to the Jaxon::PAGE_NUMBER parameter
73
     *
74
     * @param integer        $nPageNumber        The current page number
75
     *
76
     * @return Request
77
     */
78
    public function setPageNumber($nPageNumber)
79
    {
80
        // Set the value of the Jaxon::PAGE_NUMBER parameter
81
        foreach($this->aParameters as $xParameter)
82
        {
83
            if($xParameter->getType() == Jaxon::PAGE_NUMBER)
84
            {
85
                $xParameter->setValue(intval($nPageNumber));
86
                break;
87
            }
88
        }
89
        return $this;
90
    }
91
92
    /**
93
     * Add a confirmation question to the request
94
     *
95
     * @param string        $sQuestion                The question to ask
96
     *
97
     * @return Request
98
     */
99 View Code Duplication
    public function confirm($sQuestion)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
100
    {
101
        $this->sCondition = '__confirm__';
102
        $this->aMessageArgs = func_get_args();
103
        array_walk($this->aMessageArgs, function (&$xParameter) {
104
            $xParameter = Parameter::make($xParameter);
105
        });
106
        return $this;
107
    }
108
109
    /**
110
     * Add a condition to the request
111
     *
112
     * The request is sent only if the condition is true.
113
     *
114
     * @param string        $sCondition               The condition to check
115
     *
116
     * @return Request
117
     */
118
    public function when($sCondition)
119
    {
120
        $this->sCondition = Parameter::make($sCondition)->getScript();
121
        return $this;
122
    }
123
124
    /**
125
     * Add a condition to the request
126
     *
127
     * The request is sent only if the condition is false.
128
     *
129
     * @param string        $sCondition               The condition to check
130
     *
131
     * @return Request
132
     */
133
    public function unless($sCondition)
134
    {
135
        $this->sCondition = '!(' . Parameter::make($sCondition)->getScript() . ')';
136
        return $this;
137
    }
138
139
    /**
140
     * Check if a value is equal to another before sending the request
141
     *
142
     * @param string        $sValue1                  The first value to compare
143
     * @param string        $sValue2                  The second value to compare
144
     *
145
     * @return Request
146
     */
147
    public function ifeq($sValue1, $sValue2)
148
    {
149
        $this->sCondition = '(' . Parameter::make($sValue1) . '==' . Parameter::make($sValue2) . ')';
150
        return $this;
151
    }
152
153
    /**
154
     * Check if a value is not equal to another before sending the request
155
     *
156
     * @param string        $sValue1                  The first value to compare
157
     * @param string        $sValue2                  The second value to compare
158
     *
159
     * @return Request
160
     */
161
    public function ifne($sValue1, $sValue2)
162
    {
163
        $this->sCondition = '(' . Parameter::make($sValue1) . '!=' . Parameter::make($sValue2) . ')';
164
        return $this;
165
    }
166
167
    /**
168
     * Check if a value is greater than another before sending the request
169
     *
170
     * @param string        $sValue1                  The first value to compare
171
     * @param string        $sValue2                  The second value to compare
172
     *
173
     * @return Request
174
     */
175
    public function ifgt($sValue1, $sValue2)
176
    {
177
        $this->sCondition = '(' . Parameter::make($sValue1) . '>' . Parameter::make($sValue2) . ')';
178
        return $this;
179
    }
180
181
    /**
182
     * Check if a value is greater or equal to another before sending the request
183
     *
184
     * @param string        $sValue1                  The first value to compare
185
     * @param string        $sValue2                  The second value to compare
186
     *
187
     * @return Request
188
     */
189
    public function ifge($sValue1, $sValue2)
190
    {
191
        $this->sCondition = '(' . Parameter::make($sValue1) . '>=' . Parameter::make($sValue2) . ')';
192
        return $this;
193
    }
194
195
    /**
196
     * Check if a value is lower than another before sending the request
197
     *
198
     * @param string        $sValue1                  The first value to compare
199
     * @param string        $sValue2                  The second value to compare
200
     *
201
     * @return Request
202
     */
203
    public function iflt($sValue1, $sValue2)
204
    {
205
        $this->sCondition = '(' . Parameter::make($sValue1) . '<' . Parameter::make($sValue2) . ')';
206
        return $this;
207
    }
208
209
    /**
210
     * Check if a value is lower or equal to another before sending the request
211
     *
212
     * @param string        $sValue1                  The first value to compare
213
     * @param string        $sValue2                  The second value to compare
214
     *
215
     * @return Request
216
     */
217
    public function ifle($sValue1, $sValue2)
218
    {
219
        $this->sCondition = '(' . Parameter::make($sValue1) . '<=' . Parameter::make($sValue2) . ')';
220
        return $this;
221
    }
222
223
    /**
224
     * Set the message to show if the condition to send the request is not met
225
     *
226
     * The first parameter is the message to show. The followin allow to insert data from
227
     * the webpage in the message using positional placeholders.
228
     *
229
     * @param string        $sMessage                 The message to show if the request is not sent
230
     *
231
     * @return Request
232
     */
233 View Code Duplication
    public function elseShow($sMessage)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
234
    {
235
        $this->aMessageArgs = func_get_args();
236
        array_walk($this->aMessageArgs, function (&$xParameter) {
237
            $xParameter = Parameter::make($xParameter);
238
        });
239
        return $this;
240
    }
241
242
    /**
243
     * Returns a string representation of the script output (javascript) from this request object
244
     *
245
     * @return string
246
     */
247
    public function getScript()
248
    {
249
        /*
250
         * JQuery variables sometimes depend on the context where they are used, eg. when their value depends on $(this).
251
         * When a confirmation question is added, the Jaxon calls are made in a different context,
252
         * making those variables invalid.
253
         * To avoid issues related to these context changes, the JQuery selectors values are first saved into
254
         * local variables, which are then used in Jaxon function calls.
255
         */
256
        $sVars = ''; // Javascript code defining all the variables values.
257
        $nVarId = 1; // Position of the variables, starting from 1.
258
        // This array will avoid declaring multiple variables with the same value.
259
        // The array key is the variable value, while the array value is the variable name.
260
        $aVariables = []; // Array of local variables.
261
        foreach($this->aParameters as &$xParameter)
262
        {
263
            $sParameterStr = $xParameter->getScript();
264 View Code Duplication
            if($xParameter instanceof \Jaxon\Response\Plugin\JQuery\Dom\Element)
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
265
            {
266
                if(!array_key_exists($sParameterStr, $aVariables))
267
                {
268
                    // The value is not yet defined. A new variable is created.
269
                    $sVarName = "jxnVar$nVarId";
270
                    $aVariables[$sParameterStr] = $sVarName;
271
                    $sVars .= "$sVarName=$xParameter;";
272
                    $nVarId++;
273
                }
274
                else
275
                {
276
                    // The value is already defined. The corresponding variable is assigned.
277
                    $sVarName = $aVariables[$sParameterStr];
278
                }
279
                $xParameter = new Parameter(Jaxon::JS_VALUE, $sVarName);
280
            }
281
        }
282
283
        $sPhrase = '';
284
        if(count($this->aMessageArgs) > 0)
285
        {
286
            $sPhrase = array_shift($this->aMessageArgs); // The first array entry is the question.
287
            // $sPhrase = "'" . addslashes($sPhrase) . "'"; // Wrap the phrase with single quotes
288
            if(count($this->aMessageArgs) > 0)
289
            {
290
                $nParamId = 1;
291
                foreach($this->aMessageArgs as &$xParameter)
292
                {
293
                    $sParameterStr = $xParameter->getScript();
294 View Code Duplication
                    if($xParameter instanceof \Jaxon\Response\Plugin\JQuery\Dom\Element)
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
295
                    {
296
                        if(!array_key_exists($sParameterStr, $aVariables))
297
                        {
298
                            // The value is not yet defined. A new variable is created.
299
                            $sVarName = "jxnVar$nVarId";
300
                            $aVariables[$sParameterStr] = $sVarName;
301
                            $sVars .= "$sVarName=$xParameter;";
302
                            $nVarId++;
303
                        }
304
                        else
305
                        {
306
                            // The value is already defined. The corresponding variable is assigned.
307
                            $sVarName = $aVariables[$sParameterStr];
308
                        }
309
                        $xParameter = new Parameter(Jaxon::JS_VALUE, $sVarName);
310
                    }
311
                    $xParameter = "'$nParamId':" . $xParameter->getScript();
312
                    $nParamId++;
313
                }
314
                $sPhrase .= '.supplant({' . implode(',', $this->aMessageArgs) . '})';
315
            }
316
        }
317
318
        $sScript = parent::getScript();
319
        $xDialog = jaxon()->dialog();
320
        if($this->sCondition == '__confirm__')
321
        {
322
            $sScript = $xDialog->confirm($sPhrase, $sScript, '');
323
        }
324
        elseif($this->sCondition !== null)
325
        {
326
            $sScript = 'if(' . $this->sCondition . '){' . $sScript . ';}';
327
            if(($sPhrase))
328
            {
329
                $xDialog->getAlert()->setReturn(true);
330
                $sScript .= 'else{' . $xDialog->warning($sPhrase) . ';}';
331
            }
332
        }
333
        return $sVars . $sScript;
334
    }
335
336
    /**
337
     * Prints a string representation of the script output (javascript) from this request object
338
     *
339
     * @return void
340
     */
341
    public function printScript()
342
    {
343
        print $this->getScript();
344
    }
345
}
346