Completed
Push — master ( 75c398...3c71f0 )
by Thierry
01:47
created

Request::confirm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
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\Factory;
22
23
use JsonSerializable;
24
use Jaxon\Jaxon;
25
26
class Request extends JsCall
27
{
28
    use Traits\Condition;
29
30
    /**
31
     * The arguments of the confirm() call
32
     *
33
     * @var array
34
     */
35
    protected $aMessageArgs = [];
36
37
    /**
38
     * The constructor.
39
     *
40
     * @param string        $sName            The javascript function or method name
41
     */
42
    public function __construct($sName)
43
    {
44
        parent::__construct($sName);
45
    }
46
47
    /**
48
     * Check if the request has a parameter of type Jaxon::PAGE_NUMBER
49
     *
50
     * @return boolean
51
     */
52
    public function hasPageNumber()
53
    {
54
        foreach($this->aParameters as $xParameter)
55
        {
56
            if($xParameter->getType() == Jaxon::PAGE_NUMBER)
57
            {
58
                return true;
59
            }
60
        }
61
        return false;
62
    }
63
64
    /**
65
     * Set a value to the Jaxon::PAGE_NUMBER parameter
66
     *
67
     * @param integer        $nPageNumber        The current page number
68
     *
69
     * @return Request
70
     */
71
    public function setPageNumber($nPageNumber)
72
    {
73
        // Set the value of the Jaxon::PAGE_NUMBER parameter
74
        foreach($this->aParameters as $xParameter)
75
        {
76
            if($xParameter->getType() == Jaxon::PAGE_NUMBER)
77
            {
78
                $xParameter->setValue(intval($nPageNumber));
79
                break;
80
            }
81
        }
82
        return $this;
83
    }
84
85
    /**
86
     * Create parameters for message arguments
87
     *
88
     * @param   $aArgs          The arguments
89
     *
90
     * @return void
91
     */
92
    private function setMessageArgs(array $aArgs)
93
    {
94
        array_walk($aArgs, function (&$xParameter) {
95
            $xParameter = Parameter::make($xParameter);
96
        });
97
        $this->aMessageArgs = $aArgs;
98
    }
99
100
    /**
101
     * Add a confirmation question to the request
102
     *
103
     * @param string        $sQuestion                The question to ask
104
     *
105
     * @return Request
106
     */
107
    public function confirm($sQuestion)
108
    {
109
        $this->sCondition = '__confirm__';
110
        $this->setMessageArgs(func_get_args());
111
        return $this;
112
    }
113
114
    /**
115
     * Set the message to show if the condition to send the request is not met
116
     *
117
     * The first parameter is the message to show. The followin allow to insert data from
118
     * the webpage in the message using positional placeholders.
119
     *
120
     * @param string        $sMessage                 The message to show if the request is not sent
121
     *
122
     * @return Request
123
     */
124
    public function elseShow($sMessage)
125
    {
126
        $this->setMessageArgs(func_get_args());
127
        return $this;
128
    }
129
130
    /**
131
     * Returns a string representation of the script output (javascript) from this request object
132
     *
133
     * @return string
134
     */
135
    public function getScript()
136
    {
137
        /*
138
         * JQuery variables sometimes depend on the context where they are used, eg. when their value depends on $(this).
139
         * When a confirmation question is added, the Jaxon calls are made in a different context,
140
         * making those variables invalid.
141
         * To avoid issues related to these context changes, the JQuery selectors values are first saved into
142
         * local variables, which are then used in Jaxon function calls.
143
         */
144
        $sVars = ''; // Javascript code defining all the variables values.
145
        $nVarId = 1; // Position of the variables, starting from 1.
146
        // This array will avoid declaring multiple variables with the same value.
147
        // The array key is the variable value, while the array value is the variable name.
148
        $aVariables = []; // Array of local variables.
149
        foreach($this->aParameters as &$xParameter)
150
        {
151
            $sParameterStr = $xParameter->getScript();
152 View Code Duplication
            if($xParameter instanceof \Jaxon\Response\Plugin\JQuery\Dom\Element)
0 ignored issues
show
Duplication introduced by
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...
153
            {
154
                if(!array_key_exists($sParameterStr, $aVariables))
155
                {
156
                    // The value is not yet defined. A new variable is created.
157
                    $sVarName = "jxnVar$nVarId";
158
                    $aVariables[$sParameterStr] = $sVarName;
159
                    $sVars .= "$sVarName=$xParameter;";
160
                    $nVarId++;
161
                }
162
                else
163
                {
164
                    // The value is already defined. The corresponding variable is assigned.
165
                    $sVarName = $aVariables[$sParameterStr];
166
                }
167
                $xParameter = new Parameter(Jaxon::JS_VALUE, $sVarName);
168
            }
169
        }
170
171
        $sPhrase = '';
172
        if(count($this->aMessageArgs) > 0)
173
        {
174
            $sPhrase = array_shift($this->aMessageArgs); // The first array entry is the question.
175
            // $sPhrase = "'" . addslashes($sPhrase) . "'"; // Wrap the phrase with single quotes
176
            if(count($this->aMessageArgs) > 0)
177
            {
178
                $nParamId = 1;
179
                foreach($this->aMessageArgs as &$xParameter)
180
                {
181
                    $sParameterStr = $xParameter->getScript();
182 View Code Duplication
                    if($xParameter instanceof \Jaxon\Response\Plugin\JQuery\Dom\Element)
0 ignored issues
show
Duplication introduced by
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...
183
                    {
184
                        if(!array_key_exists($sParameterStr, $aVariables))
185
                        {
186
                            // The value is not yet defined. A new variable is created.
187
                            $sVarName = "jxnVar$nVarId";
188
                            $aVariables[$sParameterStr] = $sVarName;
189
                            $sVars .= "$sVarName=$xParameter;";
190
                            $nVarId++;
191
                        }
192
                        else
193
                        {
194
                            // The value is already defined. The corresponding variable is assigned.
195
                            $sVarName = $aVariables[$sParameterStr];
196
                        }
197
                        $xParameter = new Parameter(Jaxon::JS_VALUE, $sVarName);
198
                    }
199
                    $xParameter = "'$nParamId':" . $xParameter->getScript();
200
                    $nParamId++;
201
                }
202
                $sPhrase .= '.supplant({' . implode(',', $this->aMessageArgs) . '})';
203
            }
204
        }
205
206
        $sScript = parent::getScript();
207
        $xDialog = jaxon()->dialog();
208
        if($this->sCondition == '__confirm__')
209
        {
210
            $sScript = $xDialog->confirm($sPhrase, $sScript, '');
211
        }
212
        elseif($this->sCondition !== null)
213
        {
214
            $sScript = 'if(' . $this->sCondition . '){' . $sScript . ';}';
215
            if(($sPhrase))
216
            {
217
                $xDialog->getAlert()->setReturn(true);
218
                $sScript .= 'else{' . $xDialog->warning($sPhrase) . ';}';
219
            }
220
        }
221
        return $sVars . $sScript;
222
    }
223
224
    /**
225
     * Prints a string representation of the script output (javascript) from this request object
226
     *
227
     * @return void
228
     */
229
    public function printScript()
230
    {
231
        print $this->getScript();
232
    }
233
}
234