Passed
Branch master (48d769)
by Michael
02:30
created

CheckoutWizard::completeCallback()   F

Complexity

Conditions 13
Paths 1152

Size

Total Lines 160
Code Lines 127

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 127
nc 1152
nop 0
dl 0
loc 160
rs 2
c 0
b 0
f 0

How to fix   Long Method    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 (0 == $this->getValue('field')) { //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 Pedigree\Fields
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[] = ['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[] = ['value' => 'selectbox', 'description' => _MA_PEDIGREE_DROPDOWNFIELD];
96
        $this->fieldtype[] = ['value' => 'textbox', 'description' => _MA_PEDIGREE_TEXTBOXFIELD];
97
        $this->fieldtype[] = ['value' => 'textarea', 'description' => _MA_PEDIGREE_TEXTAREAFIELD];
98
        $this->fieldtype[] = ['value' => 'DateSelect', 'description' => _MA_PEDIGREE_DATEFIELD];
99
        $this->fieldtype[] = ['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[] = [
138
                'id'    => $this->getValue('id' . ($i + 1)),
139
                'value' => $this->getValue('lookup' . ($i + 1))
140
            ];
141
        }
142
        //print_r($radioarray); die();
143
        $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...
144
145
        return !$this->isError();
146
        //
147
    }
148
149
    public function prepareSettings()
150
    {
151
        if (0 == !$this->getValue('field')) {
152
            // field allready exists (editing mode)
153
154
            {
155
                $sql = 'SELECT * FROM ' . $GLOBALS['xoopsDB']->prefix('pedigree_fields') . " WHERE id='" . $this->getValue('field') . "'";
156
            }
157
            $result = $GLOBALS['xoopsDB']->query($sql);
158
            while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) {
159
                $hs = $row['hassearch'];
160
                if ('1' == $hs) {
161
                    $this->setValue('hassearch', 'hassearch');
162
                }
163
                $vip = $row['viewinpedigree'];
164
                if ('1' == $vip) {
165
                    $this->setValue('viewinpedigree', 'viewinpedigree');
166
                }
167
                $via = $row['viewinadvanced'];
168
                if ('1' == $via) {
169
                    $this->setValue('viewinadvanced', 'viewinadvanced');
170
                }
171
                $vipie = $row['viewinpie'];
172
                if ('1' == $vipie) {
173
                    $this->setValue('viewinpie', 'viewinpie');
174
                }
175
                $vil = $row['viewinlist'];
176
                if ('1' == $vil) {
177
                    $this->setValue('viewinlist', 'viewinlist');
178
                }
179
                $lit = $row['litter'];
180
                if ('1' == $lit) {
181
                    $this->setValue('litter', 'litter');
182
                }
183
                $glit = $row['generallitter'];
184
                if ('1' == $glit) {
185
                    $this->setValue('generallitter', 'generallitter');
186
                }
187
            }
188
        }
189
    }
190
191
    /**
192
     * @param $form
193
     *
194
     * @return bool
195
     */
196
    public function processSettings($form)
197
    {
198
        $this->setValue('hassearch', $this->coalesce($form['hasSearch']));
199
        $this->setValue('viewinpedigree', $this->coalesce($form['viewinpedigree']));
200
        $this->setValue('viewinadvanced', $this->coalesce($form['viewinadvanced']));
201
        $this->setValue('viewinpie', $this->coalesce($form['viewinpie']));
202
        $this->setValue('viewinlist', $this->coalesce($form['viewinlist']));
203
        $this->setValue('litter', $this->coalesce($form['litter']));
204
        $this->setValue('generallitter', $this->coalesce($form['generallitter']));
205
206
        //if both litter and general litter are set; unset generallitter
207
        if (('litter' === $this->getValue('litter')) && ('generallitter' === $this->getValue('generallitter'))) {
208
            $this->setValue('generallitter', 0);
209
        }
210
211
        return !$this->isError();
212
    }
213
214
    public function prepareSearch()
215
    {
216
        if (0 == !$this->getValue('field')) {
217
            // field allready exists (editing mode)
218
219
            $sql    = 'SELECT * FROM ' . $GLOBALS['xoopsDB']->prefix('pedigree_fields') . ' WHERE id=' . $this->getValue('field');
220
            $result = $GLOBALS['xoopsDB']->query($sql);
221
            while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) {
222
                if ('hasearch' === $this->getValue('hassearch')) {
223
                    $searchname = $row['searchname'];
224
                    $this->setValue('searchname', $searchname);
225
                    $searchexplain = $row['searchexplanation'];
226
                    $this->setValue('searchexplain', $searchexplain);
227
                }
228
            }
229
        }
230
    }
231
232
    /**
233
     * @param $form
234
     *
235
     * @return bool
236
     * @todo move language strings to language files
237
     */
238
    public function processSearch($form)
239
    {
240
        $searchname = $this->coalesce($form['searchname']);
241
        if (strlen($searchname) > 0) {
242
            $this->setValue('searchname', $searchname);
243
        } else {
244
            $this->addError('searchname', 'Please enter the searchname');
245
        }
246
247
        $fieldexplanation = $this->coalesce($form['searchexplain']);
248
        if (strlen($fieldexplanation) > 0) {
249
            $this->setValue('searchexplain', $fieldexplanation);
250
        } else {
251
            $this->addError('searchexplain', 'Please enter the search explanation for this field');
252
        }
253
254
        return !$this->isError();
255
    }
256
257
    /**
258
     * @return void
259
     */
260
    public function prepareDefaultvalue()
261
    {
262
        if (0 == !$this->getValue('field')) {
263
            // field allready exists (editing mode)
264
265
            $sql    = 'SELECT * FROM ' . $GLOBALS['xoopsDB']->prefix('pedigree_fields') . ' WHERE id=' . $this->getValue('field');
266
            $result = $GLOBALS['xoopsDB']->query($sql);
267
            while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) {
268
                $def = $row['DefaultValue'];
269
                $this->setValue('defaultvalue', $def);
270
                if ('1' == $row['LookupTable']) { //we have a lookup table; load values
271
                    $sql    = 'SELECT * FROM ' . $GLOBALS['xoopsDB']->prefix('pedigree_lookup' . $this->getValue('field')) . " ORDER BY 'order'";
272
                    $fc     = 0;
273
                    $result = $GLOBALS['xoopsDB']->query($sql);
274
                    while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) {
275
                        $radioarray[] = ['id' => $row['id'], 'value' => $row['value']];
276
                        ++$fc;
277
                    }
278
                    $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...
279
                    $this->setValue('fc', $fc);
280
                }
281
            }
282
        }
283
    }
284
285
    /**
286
     * @param $form
287
     *
288
     * @return bool
289
     * @todo move language string to language file
290
     */
291
    public function processDefaultValue($form)
292
    {
293
        $defaultvalue = $this->coalesce($form['defaultvalue']);
294
        if (strlen($defaultvalue) >= 0) {
295
            $this->setValue('defaultvalue', $defaultvalue);
296
        } else {
297
            $this->addError('defaultvalue', 'Please enter a defaultvalue');
298
        }
299
300
        return !$this->isError();
301
    }
302
303
    /**
304
     * @param $form
305
     *
306
     * @return bool
307
     */
308
    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

308
    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...
309
    {
310
        return !$this->isError();
311
    }
312
313
    public function completeCallback()
314
    {
315
316
        //can this field be searched
317
        $search = $this->getValue('hassearch');
318
        if ('hassearch' === $search) {
319
            $search        = '1';
320
            $searchname    = $this->getValue('searchname');
321
            $searchexplain = $this->getValue('searchexplain');
322
        } else {
323
            $search        = '0';
324
            $searchname    = '';
325
            $searchexplain = '';
326
        }
327
        //show in pedigree
328
        $viewinpedigree = $this->getValue('viewinpedigree');
329
        if ('viewinpedigree' === $viewinpedigree) {
330
            $viewinpedigree = '1';
331
        } else {
332
            $viewinpedigree = '0';
333
        }
334
        //show in advanced
335
        $viewinadvanced = $this->getValue('viewinadvanced');
336
        if ('viewinadvanced' === $viewinadvanced) {
337
            $viewinadvanced = '1';
338
        } else {
339
            $viewinadvanced = '0';
340
        }
341
        //show in pie
342
        $viewinpie = $this->getValue('viewinpie');
343
        if ('viewinpie' === $viewinpie) {
344
            $viewinpie = '1';
345
        } else {
346
            $viewinpie = '0';
347
        }
348
        //view in list
349
        $viewinlist = $this->getValue('viewinlist');
350
        if ('viewinlist' === $viewinlist) {
351
            $viewinlist = '1';
352
        } else {
353
            $viewinlist = '0';
354
        }
355
        //add a litter?
356
        $litter = ('litter' === $this->getValue('litter')) ? '1' : '0';
357
358
        //general litter
359
        $generallitter = ('generallitter' === $this->getValue('generalLitter')) ? '1' : '0';
360
361
        if (0 == !$this->getValue('field')) {
362
            // field allready exists (editing mode)
363
364
            //@todo refactor using class methods
365
            $sql = 'UPDATE '
366
                   . $GLOBALS['xoopsDB']->prefix('pedigree_fields')
367
                   . " SET fieldname = '"
368
                   . htmlspecialchars($this->getValue('name'), ENT_QUOTES | ENT_HTML5)
369
                   . "', fieldtype = '"
370
                   . $this->getValue('fieldtype')
371
                   . "', defaultvalue = '"
372
                   . $this->getValue('defaultvalue')
373
                   . "', fieldexplanation = '"
374
                   . $this->getValue('explain')
375
                   . "', hassearch = '"
376
                   . $search
377
                   . "', litter = '"
378
                   . $litter
379
                   . "', generallitter = '"
380
                   . $generallitter
381
                   . "', searchname = '"
382
                   . $searchname
383
                   . "', searchexplanation = '"
384
                   . $searchexplain
385
                   . "', viewinpedigree = '"
386
                   . $viewinpedigree
387
                   . "', viewinadvanced = '"
388
                   . $viewinadvanced
389
                   . "', viewinpie = '"
390
                   . $viewinpie
391
                   . "', viewinlist = '"
392
                   . $viewinlist
393
                   . "' WHERE id ='"
394
                   . $this->getValue('field')
395
                   . "'";
396
            $GLOBALS['xoopsDB']->queryF($sql);
397
            //possible change defaultvalue for userfield
398
            $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') . "'";
399
            $GLOBALS['xoopsDB']->queryF($sql);
400
            $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') . "'";
401
            $GLOBALS['xoopsDB']->queryF($sql);
402
            $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') . "'";
403
            $GLOBALS['xoopsDB']->queryF($sql);
404
        } else { //this is a new field
405
            $sql    = 'SELECT MAX(id) AS lid FROM ' . $GLOBALS['xoopsDB']->prefix('pedigree_fields') . ' LIMIT 1';
406
            $result = $GLOBALS['xoopsDB']->query($sql);
407
            while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) {
408
                $nextfieldnum = $row['lid'] + 1;
409
            }
410
            //add userfield to various tables as a new field.
411
            //always add at the end of the table
412
            $tables = ['pedigree_tree', 'pedigree_temp', 'pedigree_trash'];
413
            foreach ($tables as $table) {
414
                $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...
415
                $GLOBALS['xoopsDB']->queryF($SQL);
416
            }
417
            //is a lookup table present
418
            $lookup = $this->getValue('lookup1');
419
            if ('' == $lookup) {
420
                $lookup = '0';
421
            } else {
422
                $lookup = '1';
423
                //create table for lookupfield
424
                $createtable = 'CREATE TABLE ' . $GLOBALS['xoopsDB']->prefix('pedigree_lookup' . $nextfieldnum) . ' (`id` INT( 10 ) NOT NULL ,`value` VARCHAR( 255 ) NOT NULL, `order` INT( 10 )) ENGINE = MyISAM';
425
                $GLOBALS['xoopsDB']->queryF($createtable);
426
                //fill table
427
                $count = $this->getValue('fc');
428
                for ($x = 1; $x < $count + 1; ++$x) {
429
                    $y   = $x - 1;
430
                    $sql = 'INSERT INTO ' . $GLOBALS['xoopsDB']->prefix('pedigree_lookup' . $nextfieldnum) . " ( `id` , `value`, `order`) VALUES ('" . $y . "', '" . $this->getValue('lookup' . $x) . "','" . $y . "')";
431
                    $GLOBALS['xoopsDB']->queryF($sql);
432
                }
433
            }
434
435
            //Insert new record into pedigree_config
436
            //            $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 . "')";
437
            $sql = 'INSERT INTO '
438
                   . $GLOBALS['xoopsDB']->prefix('pedigree_fields')
439
                   . " VALUES ('"
440
                   . $nextfieldnum
441
                   . "', '1', '"
442
                   . $GLOBALS['xoopsDB']->escape(htmlspecialchars($this->getValue('name'), ENT_QUOTES | ENT_HTML5))
443
                   . "', '"
444
                   . $GLOBALS['xoopsDB']->escape($this->getValue('fieldtype'))
445
                   . "', '"
446
                   . $GLOBALS['xoopsDB']->escape($lookup)
447
                   . "', '"
448
                   . $GLOBALS['xoopsDB']->escape($this->getValue('defaultvalue'))
449
                   . "', '"
450
                   . $GLOBALS['xoopsDB']->escape($this->getValue('explain'))
451
                   . "', '"
452
                   . $GLOBALS['xoopsDB']->escape($search)
453
                   . "', '"
454
                   . $GLOBALS['xoopsDB']->escape($Litter)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $Litter seems to be never defined.
Loading history...
455
                   . "', '"
456
                   . $GLOBALS['xoopsDB']->escape($generallitter)
457
                   . "', '"
458
                   . $GLOBALS['xoopsDB']->escape($searchname)
459
                   . "', '"
460
                   . $GLOBALS['xoopsDB']->escape($searchexplain)
461
                   . "', '"
462
                   . $GLOBALS['xoopsDB']->escape($viewinpedigree)
463
                   . "', '"
464
                   . $GLOBALS['xoopsDB']->escape($viewinadvanced)
465
                   . "', '"
466
                   . $GLOBALS['xoopsDB']->escape($viewinpie)
467
                   . "', '"
468
                   . $GLOBALS['xoopsDB']->escape($viewinlist)
469
                   . "','','"
470
                   . $GLOBALS['xoopsDB']->escape($nextfieldnum)
471
                   . "')";
472
            $GLOBALS['xoopsDB']->queryF($sql);
473
        }
474
    }
475
476
    /**
477
     * Miscellaneous utility functions
478
     *
479
     * @param $email
480
     *
481
     * @return int
482
     */
483
484
    public function isValidEmail($email)
485
    {
486
        return preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$/i', $email);
487
    }
488
}
489