Passed
Branch master (465698)
by Michael
05:38
created

CheckoutWizard::prepareSettings()   D

Complexity

Conditions 10
Paths 130

Size

Total Lines 37
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 25
nc 130
nop 0
dl 0
loc 37
rs 4.606
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
require_once __DIR__ . '/wizard.php';
3
4
/**
5
 * Class CheckoutWizard
6
 */
7
class CheckoutWizard extends ZervWizard
8
{
9
    /**
10
     * checkoutwizard constructor.
11
     */
12
    public function __construct()
13
    {
14
        global $field;
15
        // start the session and initialize the wizard
16
        if (!isset($_SESSION)) {
17
            session_start();
18
        }
19
        parent::__construct($_SESSION, __CLASS__);
20
21
        $this->addStep('fieldname', _MA_PEDIGREE_ENTER_FIELD);
22
        if ($this->getValue('field') == 0) { //only for a new field
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $this->getValue('field') of type null|mixed to 0; this is ambiguous as not only 0 == 0 is true, but null == 0 is true, too. Consider using a strict comparison ===.
Loading history...
23
            $this->addStep('fieldtype', _MA_PEDIGREE_FIELD_TYP_SEL);
24
            if (('selectbox' === $this->getValue('fieldtype')) || ('radiobutton' === $this->getValue('fieldtype'))) {
25
                $this->addStep('lookup', _MA_PEDIGREE_FIELD_ADD_VALUE);
26
            }
27
        }
28
29
        $this->addStep('Settings', _MA_PEDIGREE_FIELD_PARAM);
30
        if ('hassearch' === $this->getValue('hassearch')) {
31
            $this->addStep('search', _MA_PEDIGREE_SEARCH_PARAMFIELD);
32
        }
33
        if ('picture' !== $this->getValue('fieldtype')) {
34
            $this->addStep('defaultvalue', _MA_PEDIGREE_FIELD_DEFAUT);
35
        }
36
        $this->addStep('confirm', _MA_PEDIGREE_FIELDCONFIRM);
37
    }
38
39
    /**
40
     * @todo change access to fields using PedigreeFields
41
     * @return void
42
     */
43
    public function prepareFieldname()
44
    {
45
        global $field;
46
        if (0 == !$field) {
47
            // field already exists (editing mode)
48
49
            $sql    = 'SELECT * from ' . $GLOBALS['xoopsDB']->prefix('pedigree_fields') . ' WHERE Id=' . $field;
50
            $result = $GLOBALS['xoopsDB']->query($sql);
51
            while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) {
52
                $name             = $row['fieldname'];
53
                $fieldexplanation = $row['fieldexplanation'];
54
                $fieldtype        = $row['fieldtype'];
55
            }
56
            $this->setValue('name', $name);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $name does not seem to be defined for all execution paths leading up to this point.
Loading history...
57
            $this->setValue('explain', $fieldexplanation);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $fieldexplanation does not seem to be defined for all execution paths leading up to this point.
Loading history...
58
            //set the fieldtype because we wont allow it to be edited
59
            $this->setValue('fieldtype', $fieldtype);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $fieldtype does not seem to be defined for all execution paths leading up to this point.
Loading history...
60
        }
61
        $this->setValue('field', $field); //is it a new field or are we editing a field
62
    }
63
64
    /**
65
     * @param $form
66
     *
67
     * @return bool
68
     */
69
    public function processFieldname(&$form)
70
    {
71
        $name = $this->coalesce($form['name']);
72
        if (strlen($name) > 0) {
73
            $this->setValue('name', $name);
74
        } else {
75
            $this->addError('name', _MA_PEDIGREE_FIELD_NAM);
76
        }
77
78
        $fieldexplanation = $this->coalesce($form['explain']);
79
        if (strlen($fieldexplanation) > 0) {
80
            $this->setValue('explain', $fieldexplanation);
81
        } else {
82
            $this->addError('explain', _MA_PEDIGREE_FIELD_EXPLAN1);
83
        }
84
85
        return !$this->isError();
86
    }
87
88
    /**
89
     * Setup this class' fieldtype array
90
     * @return void
91
     */
92
    public function prepareFieldtype()
93
    {
94
        $this->fieldtype[] = array('value' => 'radiobutton', 'description' => _MA_PEDIGREE_RADIOBUTTONFIELD);
0 ignored issues
show
Bug Best Practice introduced by
The property fieldtype does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
95
        $this->fieldtype[] = array('value' => 'selectbox', 'description' => _MA_PEDIGREE_DROPDOWNFIELD);
96
        $this->fieldtype[] = array('value' => 'textbox', 'description' => _MA_PEDIGREE_TEXTBOXFIELD);
97
        $this->fieldtype[] = array('value' => 'textarea', 'description' => _MA_PEDIGREE_TEXTAREAFIELD);
98
        $this->fieldtype[] = array('value' => 'DateSelect', 'description' => _MA_PEDIGREE_DATEFIELD);
99
        $this->fieldtype[] = array('value' => 'urlfield', 'description' => _MA_PEDIGREE_URLFIELD);
100
    }
101
102
    /**
103
     * @param $form
104
     *
105
     * @return bool
106
     */
107
    public function processFieldtype($form)
108
    {
109
        $this->prepareFieldtype();
110
        $fieldtype = $this->coalesce($form['fieldtype']);
111
        $this->setValue('fieldtype', $fieldtype);
112
113
        return !$this->isError();
114
    }
115
116
    /**
117
     * @param $form
118
     *
119
     * @return bool
120
     */
121
    public function processLookup($form)
122
    {
123
        $fc = $this->coalesce($form['fc']);
124
        $this->setValue('fc', $fc);
125
        $lookup   = $this->coalesce($form['lookup' . $fc]);
126
        $lookupid = $this->coalesce($form['id' . $fc]);
127
        if (strlen($lookup) > 0) {
128
            $this->setValue('lookup' . $fc, $lookup);
129
            $this->setValue('id' . $fc, $lookupid);
130
        }
131
        $lastlookup = $this->getValue('lookup' . $fc);
132
        if ($lastlookup == '') {
133
            $this->setValue('fc', $fc - 1);
134
        }
135
136
        for ($i = 0; $i < $fc; ++$i) {
137
            $radioarray[] = array('id' => $this->getValue('id' . ($i + 1)), 'value' => $this->getValue('lookup' . ($i + 1)));
138
        }
139
        //print_r($radioarray); die();
140
        $this->setValue('radioarray', $radioarray);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $radioarray does not seem to be defined for all execution paths leading up to this point.
Loading history...
141
142
        return !$this->isError();
143
        //
144
    }
145
146
    public function prepareSettings()
147
    {
148
        if (0 == !$this->getValue('field')) {
149
            // field allready exists (editing mode)
150
151
            {
152
                $sql = 'SELECT * FROM ' . $GLOBALS['xoopsDB']->prefix('pedigree_fields') . " WHERE Id='" . $this->getValue('field') . "'";
153
            }
154
            $result = $GLOBALS['xoopsDB']->query($sql);
155
            while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) {
156
                $hs = $row['hassearch'];
157
                if ('1' == $hs) {
158
                    $this->setValue('hassearch', 'hassearch');
159
                }
160
                $vip = $row['viewinpedigree'];
161
                if ('1' == $vip) {
162
                    $this->setValue('viewinpedigree', 'viewinpedigree');
163
                }
164
                $via = $row['viewinadvanced'];
165
                if ('1' == $via) {
166
                    $this->setValue('viewinadvanced', 'viewinadvanced');
167
                }
168
                $vipie = $row['viewinpie'];
169
                if ('1' == $vipie) {
170
                    $this->setValue('viewinpie', 'viewinpie');
171
                }
172
                $vil = $row['viewinlist'];
173
                if ('1' == $vil) {
174
                    $this->setValue('viewinlist', 'viewinlist');
175
                }
176
                $lit = $row['litter'];
177
                if ('1' == $lit) {
178
                    $this->setValue('litter', 'litter');
179
                }
180
                $glit = $row['generallitter'];
181
                if ('1' == $glit) {
182
                    $this->setValue('generallitter', 'generallitter');
183
                }
184
            }
185
        }
186
    }
187
188
    /**
189
     * @param $form
190
     *
191
     * @return bool
192
     */
193
    public function processSettings($form)
194
    {
195
        $this->setValue('hassearch', $this->coalesce($form['hasSearch']));
196
        $this->setValue('viewinpedigree', $this->coalesce($form['viewInPedigree']));
197
        $this->setValue('viewinadvanced', $this->coalesce($form['viewInAdvanced']));
198
        $this->setValue('viewinpie', $this->coalesce($form['viewInPie']));
199
        $this->setValue('viewinlist', $this->coalesce($form['viewInList']));
200
        $this->setValue('litter', $this->coalesce($form['litter']));
201
        $this->setValue('generallitter', $this->coalesce($form['generalLitter']));
202
203
        //if both litter and general litter are set; unset generallitter
204
        if (('litter' === $this->getValue('litter')) && ('generallitter' === $this->getValue('generallitter'))) {
205
            $this->setValue('generallitter', 0);
206
        }
207
208
        return !$this->isError();
209
    }
210
211
    public function prepareSearch()
212
    {
213
        if (0 == !$this->getValue('field')) {
214
            // field allready exists (editing mode)
215
216
            $sql    = 'SELECT * FROM ' . $GLOBALS['xoopsDB']->prefix('pedigree_fields') . ' WHERE Id=' . $this->getValue('field');
217
            $result = $GLOBALS['xoopsDB']->query($sql);
218
            while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) {
219
                if ('hasearch' === $this->getValue('hassearch')) {
220
                    $searchname = $row['searchname'];
221
                    $this->setValue('searchname', $searchname);
222
                    $searchexplain = $row['searchexplanation'];
223
                    $this->setValue('searchexplain', $searchexplain);
224
                }
225
            }
226
        }
227
    }
228
229
    /**
230
     * @param $form
231
     *
232
     * @return bool
233
     * @todo move language strings to language files
234
     */
235
    public function processSearch($form)
236
    {
237
        $searchname = $this->coalesce($form['searchname']);
238
        if (strlen($searchname) > 0) {
239
            $this->setValue('searchname', $searchname);
240
        } else {
241
            $this->addError('searchname', 'Please enter the searchname');
242
        }
243
244
        $fieldexplanation = $this->coalesce($form['searchexplain']);
245
        if (strlen($fieldexplanation) > 0) {
246
            $this->setValue('searchexplain', $fieldexplanation);
247
        } else {
248
            $this->addError('searchexplain', 'Please enter the search explanation for this field');
249
        }
250
251
        return !$this->isError();
252
    }
253
254
    /**
255
     * @return void
256
     */
257
    public function prepareDefaultvalue()
258
    {
259
        if (0 == !$this->getValue('field')) {
260
            // field allready exists (editing mode)
261
262
            $sql    = 'SELECT * FROM ' . $GLOBALS['xoopsDB']->prefix('pedigree_fields') . ' WHERE Id=' . $this->getValue('field');
263
            $result = $GLOBALS['xoopsDB']->query($sql);
264
            while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) {
265
                $def = $row['DefaultValue'];
266
                $this->setValue('defaultvalue', $def);
267
                if ($row['LookupTable'] == '1') { //we have a lookup table; load values
268
                    $sql    = 'SELECT * from ' . $GLOBALS['xoopsDB']->prefix('pedigree_lookup' . $this->getValue('field')) . " order by 'order'";
269
                    $fc     = 0;
270
                    $result = $GLOBALS['xoopsDB']->query($sql);
271
                    while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) {
272
                        $radioarray[] = array('id' => $row['Id'], 'value' => $row['value']);
273
                        ++$fc;
274
                    }
275
                    $this->setValue('radioarray', $radioarray);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $radioarray does not seem to be defined for all execution paths leading up to this point.
Loading history...
276
                    $this->setValue('fc', $fc);
277
                }
278
            }
279
        }
280
    }
281
282
    /**
283
     * @param $form
284
     *
285
     * @return bool
286
     * @todo move language string to language file
287
     */
288
    public function processDefaultValue($form)
289
    {
290
        $defaultvalue = $this->coalesce($form['defaultvalue']);
291
        if (strlen($defaultvalue) >= 0) {
292
            $this->setValue('defaultvalue', $defaultvalue);
293
        } else {
294
            $this->addError('defaultvalue', 'Please enter a defaultvalue');
295
        }
296
297
        return !$this->isError();
298
    }
299
300
    /**
301
     * @param $form
302
     *
303
     * @return bool
304
     */
305
    public function processConfirm($form)
0 ignored issues
show
Unused Code introduced by
The parameter $form is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

305
    public function processConfirm(/** @scrutinizer ignore-unused */ $form)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
306
    {
307
        return !$this->isError();
308
    }
309
310
    public function completeCallback()
311
    {
312
313
        //can this field be searched
314
        $search = $this->getValue('hassearch');
315
        if ($search === 'hassearch') {
316
            $search        = '1';
317
            $searchname    = $this->getValue('searchname');
318
            $searchexplain = $this->getValue('searchexplain');
319
        } else {
320
            $search        = '0';
321
            $searchname    = '';
322
            $searchexplain = '';
323
        }
324
        //show in pedigree
325
        $viewinpedigree = $this->getValue('viewinpedigree');
326
        if ($viewinpedigree === 'viewinpedigree') {
327
            $viewinpedigree = '1';
328
        } else {
329
            $viewinpedigree = '0';
330
        }
331
        //show in advanced
332
        $viewinadvanced = $this->getValue('viewinadvanced');
333
        if ($viewinadvanced === 'viewinadvanced') {
334
            $viewinadvanced = '1';
335
        } else {
336
            $viewinadvanced = '0';
337
        }
338
        //show in pie
339
        $viewinpie = $this->getValue('viewinpie');
340
        if ($viewinpie === 'viewinpie') {
341
            $viewinpie = '1';
342
        } else {
343
            $viewinpie = '0';
344
        }
345
        //view in list
346
        $viewinlist = $this->getValue('viewinlist');
347
        if ($viewinlist === 'viewinlist') {
348
            $viewinlist = '1';
349
        } else {
350
            $viewinlist = '0';
351
        }
352
        //add a litter?
353
        $litter = ('litter' === $this->getValue('litter')) ? '1' : '0';
354
355
        //general litter
356
        $generallitter = ('generallitter' === $this->getValue('generalLitter')) ? '1' : '0';
357
358
        if (0 == !$this->getValue('field')) {
359
            // field allready exists (editing mode)
360
361
            //@todo refactor using class methods
362
            $sql = 'UPDATE ' . $GLOBALS['xoopsDB']->prefix('pedigree_fields') . " SET fieldname = '" . htmlspecialchars($this->getValue('name')) . "', fieldtype = '" . $this->getValue('fieldtype') . "', defaultvalue = '" . $this->getValue('defaultvalue') . "', fieldexplanation = '" . $this->getValue('explain') . "', hassearch = '" . $search . "', litter = '" . $litter . "', generallitter = '" . $generallitter . "', searchname = '" . $searchname . "', searchexplanation = '" . $searchexplain . "', viewinpedigree = '" . $viewinpedigree . "', viewinadvanced = '" . $viewinadvanced . "', viewinpie = '" . $viewinpie . "', viewinlist = '" . $viewinlist . "' WHERE Id ='" . $this->getValue('field') . "'";
363
            $GLOBALS['xoopsDB']->queryF($sql);
364
            //possible change defaultvalue for userfield
365
            $sql = 'ALTER TABLE ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . ' CHANGE `user' . $this->getValue('field') . '` `user' . $this->getValue('field') . "` VARCHAR( 255 ) NOT NULL DEFAULT '" . $this->getValue('defaultvalue') . "'";
366
            $GLOBALS['xoopsDB']->queryF($sql);
367
            $sql = 'ALTER TABLE ' . $GLOBALS['xoopsDB']->prefix('pedigree_temp') . ' CHANGE `user' . $this->getValue('field') . '` `user' . $this->getValue('field') . "` VARCHAR( 1024 ) NOT NULL DEFAULT '" . $this->getValue('defaultvalue') . "'";
368
            $GLOBALS['xoopsDB']->queryF($sql);
369
            $sql = 'ALTER TABLE ' . $GLOBALS['xoopsDB']->prefix('pedigree_trash') . ' CHANGE `user' . $this->getValue('field') . '` `user' . $this->getValue('field') . "` VARCHAR( 255 ) NOT NULL DEFAULT '" . $this->getValue('defaultvalue') . "'";
370
            $GLOBALS['xoopsDB']->queryF($sql);
371
        } else { //this is a new field
372
            $sql    = 'SELECT MAX(Id) AS lid FROM ' . $GLOBALS['xoopsDB']->prefix('pedigree_fields') . ' LIMIT 1';
373
            $result = $GLOBALS['xoopsDB']->query($sql);
374
            while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) {
375
                $nextfieldnum = $row['lid'] + 1;
376
            }
377
            //add userfield to various tables as a new field.
378
            //always add at the end of the table
379
            $tables = array('pedigree_tree', 'pedigree_temp', 'pedigree_trash');
380
            foreach ($tables as $table) {
381
                $SQL = 'ALTER TABLE ' . $GLOBALS['xoopsDB']->prefix($table) . ' ADD `user' . $nextfieldnum . "` VARCHAR( 255 ) NOT NULL DEFAULT '" . $this->getValue('defaultvalue') . "'";
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $nextfieldnum does not seem to be defined for all execution paths leading up to this point.
Loading history...
382
                $GLOBALS['xoopsDB']->queryF($SQL);
383
            }
384
            //is a lookup table present
385
            $lookup = $this->getValue('lookup1');
386
            if ('' == $lookup) {
387
                $lookup = '0';
388
            } else {
389
                $lookup = '1';
390
                //create table for lookupfield
391
                $createtable = 'CREATE TABLE ' . $GLOBALS['xoopsDB']->prefix('pedigree_lookup' . $nextfieldnum) . ' (`Id` INT( 10 ) NOT NULL ,`value` VARCHAR( 255 ) NOT NULL, `order` INT( 10 )) ENGINE = MyISAM';
392
                $GLOBALS['xoopsDB']->queryF($createtable);
393
                //fill table
394
                $count = $this->getValue('fc');
395
                for ($x = 1; $x < $count + 1; ++$x) {
396
                    $y   = $x - 1;
397
                    $sql = 'INSERT INTO ' . $GLOBALS['xoopsDB']->prefix('pedigree_lookup' . $nextfieldnum) . " ( `Id` , `value`, `order`) VALUES ('" . $y . "', '" . $this->getValue('lookup' . $x) . "','" . $y . "')";
398
                    $GLOBALS['xoopsDB']->queryF($sql);
399
                }
400
            }
401
402
            //Insert new record into pedigree_config
403
            //            $sql = 'INSERT INTO ' . $GLOBALS['xoopsDB']->prefix('pedigree_fields') . " VALUES ('" . $nextfieldnum . "', '1', '" . htmlspecialchars($this->getValue('name')) . "', '" . $this->getValue('fieldtype') . "', '" . $lookup . "', '" . $this->getValue('defaultvalue') . "', '" . $this->getValue('explain') . "', '" . $search . "', '" . $Litter . "', '" . $generalLitter . "', '" . $searchname . "', '" . $searchexplain . "', '" . $viewinpedigree . "', '" . $viewinadvanced . "', '" . $viewinpie . "', '" . $viewinlist . "','','" . $nextfieldnum . "')";
404
            $sql = 'INSERT INTO ' . $GLOBALS['xoopsDB']->prefix('pedigree_fields') . " VALUES ('" . $nextfieldnum . "', '1', '" . $GLOBALS['xoopsDB']->escape(htmlspecialchars($this->getValue('name'))) . "', '" . $GLOBALS['xoopsDB']->escape($this->getValue('fieldtype')) . "', '" . $GLOBALS['xoopsDB']->escape($lookup) . "', '" . $GLOBALS['xoopsDB']->escape($this->getValue('defaultvalue')) . "', '" . $GLOBALS['xoopsDB']->escape($this->getValue('explain')) . "', '" . $GLOBALS['xoopsDB']->escape($search) . "', '" . $GLOBALS['xoopsDB']->escape($Litter) . "', '" . $GLOBALS['xoopsDB']->escape($generalLitter) . "', '" . $GLOBALS['xoopsDB']->escape($searchname) . "', '" . $GLOBALS['xoopsDB']->escape($searchexplain) . "', '" . $GLOBALS['xoopsDB']->escape($viewinpedigree) . "', '" . $GLOBALS['xoopsDB']->escape($viewinadvanced) . "', '" . $GLOBALS['xoopsDB']->escape($viewinpie) . "', '" . $GLOBALS['xoopsDB']->escape($viewinlist) . "','','" . $GLOBALS['xoopsDB']->escape($nextfieldnum) . "')";
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $Litter seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $generalLitter does not exist. Did you maybe mean $generallitter?
Loading history...
405
            $GLOBALS['xoopsDB']->queryF($sql);
406
        }
407
    }
408
409
    /**
410
     * Miscellaneous utility functions
411
     *
412
     * @param $email
413
     *
414
     * @return int
415
     */
416
417
    public function isValidEmail($email)
418
    {
419
        return preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$/i', $email);
420
    }
421
}
422