listForm()   C
last analyzed

Complexity

Conditions 11
Paths 81

Size

Total Lines 75
Code Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 57
dl 0
loc 75
rs 6.7915
c 0
b 0
f 0
cc 11
nc 81
nop 2

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
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
*/
11
12
/**
13
 * oledrion
14
 *
15
 * @copyright   {@link https://xoops.org/ XOOPS Project}
16
 * @license     {@link http://www.fsf.org/copyleft/gpl.html GNU public license}
17
 * @author      Hervé Thouzard (http://www.herve-thouzard.com/)
18
 */
19
20
/**
21
 * Liste des listes de l'utilisateur
22
 */
23
24
use XoopsModules\Oledrion;
25
use XoopsModules\Oledrion\Constants;
26
27
require_once __DIR__ . '/header.php';
28
$GLOBALS['current_category']             = -1;
29
$GLOBALS['xoopsOption']['template_main'] = 'oledrion_mylists.tpl';
30
require_once XOOPS_ROOT_PATH . '/header.php';
31
require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
32
33
$uid = Oledrion\Utility::getCurrentUserID();
34
if (0 == $uid) {
35
    Oledrion\Utility::redirect(_OLEDRION_ERROR23, XOOPS_URL . '/register.php', 4);
36
}
37
38
$baseurl = OLEDRION_URL . basename(__FILE__); // URL de ce script
39
//$handlers = HandlerManager::getInstance();
40
41
$op = \Xmf\Request::getCmd('op', 'default');
42
43
$xoopsTpl->assign('baseurl', $baseurl);
44
$helper->loadLanguage('modinfo');
45
$helper->loadLanguage('admin');
46
$breadcrumb = '';
47
48
/**
49
 * @param                        $op
50
 * @param  int                   $product_id
51
 * @return \XoopsThemeForm
52
 */
53
function listForm($op, $product_id = 0)
54
{
55
    global $baseurl;
56
57
    $db           = \XoopsDatabaseFactory::getDatabaseConnection();
58
    $listsHandler = new Oledrion\ListsHandler($db);
59
60
    if ('edit' === $op) {
61
        $title        = _OLEDRION_EDIT_LIST;
62
        $label_submit = _AM_OLEDRION_MODIFY;
0 ignored issues
show
Unused Code introduced by
The assignment to $label_submit is dead and can be removed.
Loading history...
63
        $list_id      = \Xmf\Request::getInt('list_id', 0, 'GET');
64
        if (empty($list_id)) {
65
            Oledrion\Utility::redirect(_AM_OLEDRION_ERROR_12, $baseurl, 5);
66
        }
67
        $item = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $item is dead and can be removed.
Loading history...
68
        $item = $listsHandler->get($list_id);
69
        if (!is_object($item)) {
70
            Oledrion\Utility::redirect(_AM_OLEDRION_NOT_FOUND, $baseurl, 5);
71
        }
72
        // Vérification, est-ce que l'utilisateur courant est bien le propriétaire de cette liste ?
73
        if (!$listsHandler->isThisMyList($list_id)) {
74
            Oledrion\Utility::redirect(_OLEDRION_ERROR25, $baseurl, 8);
75
        }
76
        $edit         = true;
77
        $label_submit = _AM_OLEDRION_MODIFY;
78
    } else {
79
        $title        = _OLEDRION_ADD_LIST;
80
        $label_submit = _AM_OLEDRION_ADD;
81
        $item         = $listsHandler->create(true);
82
        $edit         = false;
83
    }
84
85
    $sform = new \XoopsThemeForm($title, 'frmList', $baseurl);
86
    $sform->addElement(new \XoopsFormHidden('op', 'save'));
87
    $sform->addElement(new \XoopsFormHidden('list_id', $item->getVar('list_id')));
0 ignored issues
show
Bug introduced by
It seems like $item->getVar('list_id') can also be of type array and array; however, parameter $value of XoopsFormHidden::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

87
    $sform->addElement(new \XoopsFormHidden('list_id', /** @scrutinizer ignore-type */ $item->getVar('list_id')));
Loading history...
88
    $sform->addElement(new \XoopsFormText(_AM_OLEDRION_TITLE, 'list_title', 50, 255, $item->getVar('list_title', 'e')), true);
0 ignored issues
show
Bug introduced by
It seems like $item->getVar('list_title', 'e') can also be of type array and array; however, parameter $value of XoopsFormText::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

88
    $sform->addElement(new \XoopsFormText(_AM_OLEDRION_TITLE, 'list_title', 50, 255, /** @scrutinizer ignore-type */ $item->getVar('list_title', 'e')), true);
Loading history...
89
    //$sform->addElement(new \XoopsFormText(_OLEDRION_LIST_PASSWORD, 'list_password', 50, 50, $item->getVar('list_password','e')), false);
90
    $selectTypes = \XoopsModules\Oledrion\Lists::getTypesArray();
91
    $selectType  = new \XoopsFormSelect(_OLEDRION_LIST_TYPE, 'list_type', $item->getVar('list_type', 'e'));
92
    $selectType->addOptionArray($selectTypes);
93
    $sform->addElement($selectType, true);
94
    $sform->addElement(new \XoopsFormTextArea(_OLEDRION_DESCRIPTION, 'list_description', $item->getVar('list_description', 'e'), 7, 60), false);
0 ignored issues
show
Bug introduced by
It seems like $item->getVar('list_description', 'e') can also be of type array and array; however, parameter $value of XoopsFormTextArea::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

94
    $sform->addElement(new \XoopsFormTextArea(_OLEDRION_DESCRIPTION, 'list_description', /** @scrutinizer ignore-type */ $item->getVar('list_description', 'e'), 7, 60), false);
Loading history...
95
    $listProducts = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $listProducts is dead and can be removed.
Loading history...
96
    if ($edit) {
97
        $listProducts = $listsHandler->getListProducts($item);
98
        if (count($listProducts) > 0) {
99
            $productsTray = new \XoopsFormElementTray(_OLEDRION_PROD_IN_THIS_LIST, '<br>');
100
            $productsTray->addElement(new \XoopsFormLabel(_OLEDRION_CHECK_PRODUCTS), false);
101
            foreach ($listProducts as $product) {
102
                $caption  = "<a target='_blank' href='" . $product->getLink() . "'>" . $product->getVar('product_title') . '</a>';
103
                $checkbox = new \XoopsFormCheckBox($caption, 'productsList[]');
104
                $checkbox->addOption($product->getVar('product_id'), _DELETE);
105
                $productsTray->addElement($checkbox);
106
                unset($caption, $checkbox);
107
            }
108
            $sform->addElement($productsTray, false);
109
        }
110
    }
111
    if ($product_id > 0) {
112
        $product = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $product is dead and can be removed.
Loading history...
113
        $product = $productsHandler->get($product_id);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $productsHandler does not exist. Did you maybe mean $product?
Loading history...
114
        if (is_object($product) && $product->isProductVisible()) {
115
            $content = "<a target='_blank' href='" . $product->getLink() . "'>" . $product->getVar('product_title') . '</a>';
116
            $sform->addElement(new \XoopsFormLabel(_OLEDRION_PRODUCT_DO_ADD, $content));
117
            $sform->addElement(new \XoopsFormHidden('product_id', $product_id));
118
        }
119
    }
120
    $buttonTray = new \XoopsFormElementTray('', '');
121
    $submit_btn = new \XoopsFormButton('', 'post', $label_submit, 'submit');
122
    $buttonTray->addElement($submit_btn);
123
    $sform->addElement($buttonTray);
124
125
    $sform = Oledrion\Utility::formMarkRequiredFields($sform);
126
127
    return $sform;
128
}
129
130
switch ($op) {
131
    // ************************************************************************
132
    case 'default': // Liste de toutes les listes de l'utilisateur ************
133
134
        // ************************************************************************
135
        $xoopsTpl->assign('op', $op);
136
        $lists   = [];
137
        $start   = $limit = 0;
138
        $idAsKey = true;
139
        $lists   = $listsHandler->getRecentLists(new Oledrion\Parameters([
140
                                                                             'start'    => $start,
141
                                                                             'limit'    => $limit,
142
                                                                             'sort'     => 'list_title',
143
                                                                             'order'    => 'ASC',
144
                                                                             'idAsKey'  => $idAsKey,
145
                                                                             'listType' => Constants::OLEDRION_LISTS_ALL,
146
                                                                             'list_uid' => $uid,
147
                                                                         ]));
148
        if (count($lists) > 0) {
149
            foreach ($lists as $list) {
150
                $xoopsTpl->append('lists', $list->toArray());
151
            }
152
        }
153
        $breadcrumb = [
154
            OLEDRION_URL . 'all-lists.php'    => _MI_OLEDRION_SMNAME11,
155
            OLEDRION_URL . basename(__FILE__) => _MI_OLEDRION_SMNAME10,
156
        ];
157
158
        break;
159
    // ************************************************************************
160
    case 'addProduct': // Ajout d'un produit à une liste *********************
161
162
        // ************************************************************************
163
        $xoopsTpl->assign('op', $op);
164
        $product_id = \Xmf\Request::getInt('product_id', 0, 'GET');
165
        if (0 == $product_id) {
166
            Oledrion\Utility::redirect(_OLEDRION_ERROR14, $baseurl, 4);
167
        }
168
        $userListsCount = $listsHandler->getRecentListsCount(Constants::OLEDRION_LISTS_ALL, $uid);
169
        $xoopsTpl->assign('userListsCount', $userListsCount);
170
        $xoopsTpl->assign('product_id', $product_id);
171
        if ($userListsCount > 0) {
172
            $userLists = $listsHandler->getRecentLists(new Oledrion\Parameters([
173
                                                                                   'start'    => 0,
174
                                                                                   'limit'    => 0,
175
                                                                                   'sort'     => 'list_title',
176
                                                                                   'order'    => 'ASC',
177
                                                                                   'idAsKey'  => true,
178
                                                                                   'listType' => Constants::OLEDRION_LISTS_ALL,
179
                                                                                   'list_uid' => $uid,
180
                                                                               ]));
181
            foreach ($userLists as $list) {
182
                $xoopsTpl->append('lists', $list->toArray());
183
            }
184
            $breadcrumb = [
185
                OLEDRION_URL . 'all-lists.php'    => _MI_OLEDRION_SMNAME11,
186
                OLEDRION_URL . basename(__FILE__) => _MI_OLEDRION_SMNAME10,
187
                OLEDRION_URL                      => _OLEDRION_ADD_PRODUCT_LIST,
188
            ];
189
            $product    = null;
190
            $product    = $productsHandler->get($product_id);
191
            if (is_object($product) && $product->isProductVisible()) {
192
                $xoopsTpl->assign('product', $product->toArray());
193
            } else {
194
                Oledrion\Utility::redirect(_OLEDRION_ERROR1, $baseurl, 4);
195
            }
196
        } else {
197
            $sform      = listForm('addList', $product_id);
198
            $title      = _OLEDRION_ADD_LIST;
199
            $breadcrumb = [
200
                OLEDRION_URL . 'all-lists.php'    => _MI_OLEDRION_SMNAME11,
201
                OLEDRION_URL . basename(__FILE__) => _MI_OLEDRION_SMNAME10,
202
                OLEDRION_URL                      => $title,
203
            ];
204
            $xoopsTpl->assign('form', $sform->render());
205
        }
206
207
        break;
208
    // ************************************************************************
209
    case 'addProductToList': // Ajout d'un produit à une liste, sélection de la liste
210
211
        // ************************************************************************
212
        $xoopsTpl->assign('op', $op);
213
        $product_id = \Xmf\Request::getInt('product_id', 0, 'POST');
214
        if (0 == $product_id) {
215
            Oledrion\Utility::redirect(_OLEDRION_ERROR14, $baseurl, 4);
216
        }
217
        $product = null;
218
        $product = $productsHandler->get($product_id);
219
        if (is_object($product) && $product->isProductVisible()) {
220
            $xoopsTpl->assign('product', $product->toArray());
221
        } else {
222
            Oledrion\Utility::redirect(_OLEDRION_ERROR1, $baseurl, 4);
223
        }
224
225
        $list_id = \Xmf\Request::getInt('list_id', 0, 'POST');
226
        if (0 == $list_id) {
227
            // Ajouter à une nouvelle liste
228
            $sform      = listForm('addList', $product_id);
229
            $title      = _OLEDRION_ADD_LIST;
230
            $breadcrumb = [
231
                OLEDRION_URL . 'all-lists.php'    => _MI_OLEDRION_SMNAME11,
232
                OLEDRION_URL . basename(__FILE__) => _MI_OLEDRION_SMNAME10,
233
                OLEDRION_URL                      => $title,
234
            ];
235
            $xoopsTpl->assign('form', $sform->render());
236
            $xoopsTpl->assign('op', 'addList');
237
        } else {
238
            // Ajouter à une liste existante
239
            if (!$listsHandler->isThisMyList($list_id)) {
240
                Oledrion\Utility::redirect(_OLEDRION_ERROR25, $baseurl, 8);
241
            }
242
            if ($productsListHandler->isProductAlreadyInList($list_id, $product_id)) {
243
                Oledrion\Utility::redirect(_OLEDRION_ERROR26, $baseurl . '?op=addProduct&product_id=' . $product_id, 4);
244
            } else {
245
                $res = $productsListHandler->addProductToUserList($list_id, $product_id);
246
                if ($res) {
247
                    $list = null;
248
                    $list = $listsHandler->get($list_id);
249
                    if (is_object($list)) {
250
                        $listsHandler->incrementListProductsCount($list);
251
                    }
252
                    Oledrion\Utility::updateCache();
253
                    Oledrion\Utility::redirect(_OLEDRION_PRODUCT_LIST_ADD_OK, $product->getLink(), 2);
254
                } else {
255
                    Oledrion\Utility::redirect(_OLEDRION_ERROR27, $product->getLink(), 4);
256
                }
257
            }
258
        }
259
260
        break;
261
    // ************************************************************************
262
    case 'delete': // Suppression d'une liste ********************************
263
264
        // ************************************************************************
265
        $xoopsTpl->assign('op', $op);
266
        $list_id = \Xmf\Request::getInt('list_id', 0, 'GET');
267
        if (0 == $list_id) {
268
            Oledrion\Utility::redirect(_OLEDRION_ERROR21, $baseurl, 4);
269
        }
270
        // Vérification, est-ce que l'utilisateur courant est bien le propriétaire de cette liste ?
271
        if (!$listsHandler->isThisMyList($list_id)) {
272
            Oledrion\Utility::redirect(_OLEDRION_ERROR25, $baseurl, 8);
273
        }
274
        $item = $listsHandler->get($list_id);
275
        if (!is_object($item)) {
276
            Oledrion\Utility::redirect(_AM_OLEDRION_NOT_FOUND, $baseurl, 5);
277
        }
278
        xoops_confirm(['op' => 'reallyDelete', 'list_id' => $list_id], $baseurl, _OLEDRION_DELETE_LIST . '<br>' . $item->getVar('list_title'));
279
280
        break;
281
    // ************************************************************************
282
    case 'reallyDelete': // Suppression effective d'une liste **************
283
284
        // ************************************************************************
285
        $list_id = \Xmf\Request::getInt('list_id', 0, 'POST');
286
        if (0 == $list_id) {
287
            Oledrion\Utility::redirect(_OLEDRION_ERROR21, $baseurl, 4);
288
        }
289
        // Vérification, est-ce que l'utilisateur courant est bien le propriétaire de cette liste ?
290
        if (!$listsHandler->isThisMyList($list_id)) {
291
            Oledrion\Utility::redirect(_OLEDRION_ERROR25, $baseurl, 8);
292
        }
293
        $item = $listsHandler->get($list_id);
294
        if (!is_object($item)) {
295
            Oledrion\Utility::redirect(_AM_OLEDRION_NOT_FOUND, $baseurl, 5);
296
        }
297
        if ($listsHandler->deleteList($item)) {
298
            Oledrion\Utility::updateCache();
299
            Oledrion\Utility::redirect(_AM_OLEDRION_SAVE_OK, $baseurl, 2);
300
        } else {
301
            Oledrion\Utility::redirect(_AM_OLEDRION_SAVE_PB, $baseurl, 5);
302
        }
303
304
        break;
305
    // ************************************************************************
306
    case 'save': // Sauvegarde d'une liste *********************************
307
308
        // ************************************************************************
309
        $list_id = \Xmf\Request::getInt('list_id', 0, 'POST');
310
        if (!empty($list_id)) {
311
            // Vérification, est-ce que l'utilisateur courant est bien le propriétaire de cette liste ?
312
            if (!$listsHandler->isThisMyList($list_id)) {
313
                Oledrion\Utility::redirect(_OLEDRION_ERROR25, $baseurl, 8);
314
            }
315
            $edit = true;
316
            $item = $listsHandler->get($list_id);
317
            if (!is_object($item)) {
318
                Oledrion\Utility::redirect(_AM_OLEDRION_NOT_FOUND, $baseurl, 5);
319
            }
320
            $item->unsetNew();
321
            $edit = true;
322
        } else {
323
            $item = $listsHandler->create(true);
324
            $edit = false;
325
        }
326
        // Contrôle sur le titre
327
        if (!isset($_POST['list_title']) || (isset($_POST['list_title']) && '' === xoops_trim($_POST['list_title']))) {
328
            Oledrion\Utility::redirect(_OLEDRION_ERROR24, $baseurl, 5);
329
        }
330
        $item->setVars($_POST);
331
        if (!$edit) {
332
            $item->setVar('list_date', time());
333
            $item->setVar('list_uid', $uid);
334
        }
335
        if (\Xmf\Request::hasVar('productsList', 'POST')) {
336
            $productsDeletedCount = 0;
337
            foreach ($_POST['productsList'] as $productId) {
338
                $res = $productsListHandler->deleteProductFromList($list_id, (int)$productId);
339
                if ($res) {
340
                    ++$productsDeletedCount;
341
                }
342
            }
343
            if ($productsDeletedCount > 0) {
344
                $productsListHandler->decrementListProductsCount($productsDeletedCount);
345
            }
346
        }
347
        $res = $listsHandler->insert($item);
348
        if ($res) {
349
            if (\Xmf\Request::hasVar('product_id', 'POST')) {
350
                $product_id = \Xmf\Request::getInt('product_id', 0, 'POST');
351
                if ($product_id > 0) {
352
                    $product = null;
353
                    $product = $productsHandler->get($product_id);
354
                    if (is_object($product) && $product->isProductVisible()) {
355
                        // On peut ajouter le produit à cette nouvelle liste
356
                        $res = $productsListHandler->addProductToUserList($item->getVar('list_id'), $product_id);
357
                        if ($res) {
358
                            // Mise à jour du nombre de produits de la liste
359
                            $listsHandler->incrementListProductsCount($item);
360
                            Oledrion\Utility::updateCache();
361
                            Oledrion\Utility::redirect(_AM_OLEDRION_SAVE_OK, $product->getLink(), 2);
362
                        }
363
                    }
364
                }
365
            }
366
            Oledrion\Utility::updateCache();
367
            Oledrion\Utility::redirect(_AM_OLEDRION_SAVE_OK, $baseurl, 2);
368
        } else {
369
            Oledrion\Utility::redirect(_AM_OLEDRION_SAVE_PB, $baseurl, 5);
370
        }
371
372
        break;
373
    // ************************************************************************
374
    case 'edit': // Edition d'une liste ***************************************
375
376
    case 'addList': // Ajout d'une liste **************************************
377
378
        // ************************************************************************
379
        $xoopsTpl->assign('op', $op);
380
        $sform = listForm($op, 0);
381
        $title = _OLEDRION_ADD_LIST;
382
        if ('edit' === $op) {
383
            $title = _OLEDRION_EDIT_LIST;
384
        }
385
        $breadcrumb = [
386
            OLEDRION_URL . 'all-lists.php'    => _MI_OLEDRION_SMNAME11,
387
            OLEDRION_URL . basename(__FILE__) => _MI_OLEDRION_SMNAME10,
388
            OLEDRION_URL                      => $title,
389
        ];
390
391
        $xoopsTpl->assign('form', $sform->render());
392
393
        break;
394
}
395
396
Oledrion\Utility::setCSS();
397
Oledrion\Utility::setLocalCSS($xoopsConfig['language']);
398
399
$xoopsTpl->assign('mod_pref', $mod_pref);
400
$xoopsTpl->assign('breadcrumb', Oledrion\Utility::breadcrumb($breadcrumb));
0 ignored issues
show
Bug introduced by
It seems like $breadcrumb can also be of type string; however, parameter $path of XoopsModules\Oledrion\Utility::breadcrumb() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

400
$xoopsTpl->assign('breadcrumb', Oledrion\Utility::breadcrumb(/** @scrutinizer ignore-type */ $breadcrumb));
Loading history...
401
402
$title = _MI_OLEDRION_SMNAME10 . ' - ' . Oledrion\Utility::getModuleName();
403
Oledrion\Utility::setMetas($title, $title);
404
require_once XOOPS_ROOT_PATH . '/footer.php';
405