Completed
Push — master ( 9d3fbd...af269e )
by Michael
09:48
created

admin/actions/discounts.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
 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
 * Gestion des réductions (dans l'administration)
22
 */
23
if (!defined('OLEDRION_ADMIN')) {
24
    exit();
25
}
26
27
switch ($action) {
28
    // ****************************************************************************************************************
29
    case 'default': // Gestion des réductions
30
        // ****************************************************************************************************************
31
        xoops_cp_header();
32
        $adminObject = \Xmf\Module\Admin::getInstance();
33
        $adminObject->displayNavigation('index.php?op=discounts');
34
35
        $form = "<form method='post' action='$baseurl' name='frmadddiscount' id='frmadddiscount'><input type='hidden' name='op' id='op' value='discounts'><input type='hidden' name='action' id='action' value='add'><input type='submit' name='btngo' id='btngo' value='"
36
                . _AM_OLEDRION_ADD_ITEM
37
                . "'></form>";
38
        echo $form;
39
        //        OledrionUtility::htitle(_MI_OLEDRION_ADMENU6, 4);
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
40
41
        $discounts  = array();
42
        $itemsCount = 0;
43
        $class      = '';
44
        $start      = isset($_GET['start']) ? (int)$_GET['start'] : 0;
45
46
        $itemsCount = $h_oledrion_discounts->getCount(); // Recherche du nombre total de réductions
47
        if ($itemsCount > $limit) {
48
            $pagenav = new XoopsPageNav($itemsCount, $limit, $start, 'start', 'op=discounts');
49
        }
50
51
        $criteria = new Criteria('disc_id', 0, '<>');
52
        $criteria->setLimit($limit);
53
        $criteria->setStart($start);
54
        $discounts = $h_oledrion_discounts->getObjects($criteria);
55
56
        echo "<table width='100%' cellspacing='1' cellpadding='3' border='0' class='outer'>";
57
        echo "<tr><th align='center'>" . _AM_OLEDRION_ID . "</th><th align='center'>" . _TITLE . "</th><th align='center'>" . _AM_OLEDRION_ACTION . '</th></tr>';
58
        foreach ($discounts as $item) {
59
            $class     = ($class === 'even') ? 'odd' : 'even';
60
            $id        = $item->getVar('disc_id');
61
            $actions   = array();
62
            $actions[] = "<a href='$baseurl?op=discounts&action=edit&id=" . $id . "' title='" . _OLEDRION_EDIT . "'>" . $icones['edit'] . '</a>';
63
            $actions[] = "<a href='$baseurl?op=discounts&action=delete&id=" . $id . "' title='" . _OLEDRION_DELETE . "'" . $conf_msg . '>' . $icones['delete'] . '</a>';
64
            $actions[] = "<a href='$baseurl?op=discounts&action=copy&id=" . $id . "' title='" . _OLEDRION_DUPLICATE_DISCOUNT . "'>" . $icones['copy'] . '</a>';
65
            echo "<tr class='" . $class . "'>\n";
66
            echo '<td>' . $id . "</td><td align='center'>" . $item->getVar('disc_title') . "</td><td align='center'>" . implode(' ', $actions) . "</td>\n";
67
            echo "<tr>\n";
68
        }
69
        $class = ($class === 'even') ? 'odd' : 'even';
70
        echo "<tr class='" . $class . "'>\n";
71
        echo "<td colspan='3' align='center'>" . $form . "</td>\n";
72
        echo "</tr>\n";
73
        echo '</table>';
74
        if (isset($pagenav) && is_object($pagenav)) {
75
            echo "<div align='right'>" . $pagenav->renderNav() . '</div>';
76
        }
77
        $oledrion_reductions = new oledrion_reductions();
78
79
        require_once OLEDRION_ADMIN_PATH . 'admin_footer.php';
80
        break;
81
82
    // ****************************************************************************************************************
83
    case 'add': // Ajout d'une promotion
84
    case 'edit': // Edition d'une promo
85
        // ****************************************************************************************************************
86
87
        xoops_cp_header();
88
        //oledrion_adminMenu(7);
89 View Code Duplication
        if ($action === 'edit') {
90
            $title = _AM_OLEDRION_EDIT_DISCOUNT;
91
            $id    = isset($_GET['id']) ? (int)$_GET['id'] : 0;
92
            if (empty($id)) {
93
                OledrionUtility::redirect(_AM_OLEDRION_ERROR_1, $baseurl, 5);
94
            }
95
            // Item exits ?
96
            $item = null;
97
            $item = $h_oledrion_discounts->get($id);
98
            if (!is_object($item)) {
99
                OledrionUtility::redirect(_AM_OLEDRION_NOT_FOUND, $baseurl, 5);
100
            }
101
            $edit         = true;
102
            $label_submit = _AM_OLEDRION_MODIFY;
103
        } else {
104
            $title        = _AM_OLEDRION_ADD_DSICOUNT;
105
            $item         = $h_oledrion_discounts->create(true);
106
            $label_submit = _AM_OLEDRION_ADD;
107
            $edit         = false;
108
        }
109
110
        require_once XOOPS_ROOT_PATH . '/class/template.php';
111
        global $xoopsTpl;
112
        //      $xoopsTpl = new XoopsTpl();
113
        $xoopsTpl->assign('formTitle', $title);
114
        $xoopsTpl->assign('action', 'edit');
115
        $xoopsTpl->assign('baseurl', $baseurl);
116
        $xoopsTpl->assign('label_submit', $label_submit);
117
        $discountForTemplate                        = $item->toArray();
118
        $discountForTemplate['disc_pediod_checked'] = $item->getVar('disc_date_from') > 0
119
                                                      && $item->getVar('disc_date_to') > 0 ? 'checked' : '';
120
121
        $disc_date_from                        = new XoopsFormTextDateSelect(_AM_OLEDRION_DISCOUNT_PERFROM, 'disc_date_from', 15, $item->getVar('disc_date_from'));
122
        $discountForTemplate['disc_date_from'] = $disc_date_from->render();
123
        $disc_date_to                          = new XoopsFormTextDateSelect(_AM_OLEDRION_DISCOUNT_PERTO, 'disc_date_to', 15, $item->getVar('disc_date_to'));
124
        $discountForTemplate['disc_date_to']   = $disc_date_to->render();
125
126
        $reductionType0 = $reductionType1 = $reductionType2 = '';
127
        $checked        = 'checked';
128
129
        switch ($item->getVar('disc_price_type')) {
130
            case OLEDRION_DISCOUNT_PRICE_TYPE0:
131
                $reductionType0 = $checked;
132
                break;
133
            case OLEDRION_DISCOUNT_PRICE_TYPE1:
134
                $reductionType1 = $checked;
135
                break;
136
            case OLEDRION_DISCOUNT_PRICE_TYPE2:
137
                $reductionType2 = $checked;
138
                break;
139
        }
140
        $discountForTemplate['disc_price_type_checked0'] = $reductionType0;
141
        $discountForTemplate['disc_price_type_checked1'] = $reductionType1;
142
        $discountForTemplate['disc_price_type_checked2'] = $reductionType2;
143
        // ****
144
        $disc_price_amount_type1 = $disc_price_amount_type2 = '';
145
        if ($item->getVar('disc_price_amount_type') == OLEDRION_DISCOUNT_PRICE_REDUCE_PERCENT) {
146
            $disc_price_amount_type1 = $checked;
147
        } elseif ($item->getVar('disc_price_amount_type') == OLEDRION_DISCOUNT_PRICE_REDUCE_MONEY) {
148
            $disc_price_amount_type2 = $checked;
149
        }
150
        $discountForTemplate['disc_price_amount_type_checked1'] = $disc_price_amount_type1;
151
        $discountForTemplate['disc_price_amount_type_checked2'] = $disc_price_amount_type2;
152
        // ****
153
        $disc_price_amount_on_checked1 = $disc_price_amount_on_checked2 = '';
154
        if ($item->getVar('disc_price_amount_on') == OLEDRION_DISCOUNT_PRICE_AMOUNT_ON_PRODUCT) {
155
            $disc_price_amount_on_checked1 = $checked;
156
        } elseif ($item->getVar('disc_price_amount_on') == OLEDRION_DISCOUNT_PRICE_AMOUNT_ON_CART) {
157
            $disc_price_amount_on_checked2 = $checked;
158
        }
159
        $discountForTemplate['disc_price_amount_on_checked1'] = $disc_price_amount_on_checked1;
160
        $discountForTemplate['disc_price_amount_on_checked2'] = $disc_price_amount_on_checked2;
161
        // ****
162
        $disc_price_case_checked1 = $disc_price_case_checked2 = $disc_price_case_checked3 = $disc_price_case_checked4 = '';
163
        switch ($item->getVar('disc_price_case')) {
164
            case OLEDRION_DISCOUNT_PRICE_CASE_ALL:
165
                $disc_price_case_checked1 = $checked;
166
                break;
167
            case OLEDRION_DISCOUNT_PRICE_CASE_FIRST_BUY:
168
                $disc_price_case_checked2 = $checked;
169
                break;
170
            case OLEDRION_DISCOUNT_PRICE_CASE_PRODUCT_NEVER:
171
                $disc_price_case_checked3 = $checked;
172
                break;
173
            case OLEDRION_DISCOUNT_PRICE_CASE_QTY_IS:
174
                $disc_price_case_checked4 = $checked;
175
                break;
176
        }
177
        $discountForTemplate['disc_price_case_checked1'] = $disc_price_case_checked1;
178
        $discountForTemplate['disc_price_case_checked2'] = $disc_price_case_checked2;
179
        $discountForTemplate['disc_price_case_checked3'] = $disc_price_case_checked3;
180
        $discountForTemplate['disc_price_case_checked4'] = $disc_price_case_checked4;
181
182
        // ****
183
        $quantityConditions = array(
184
            OLEDRION_DISCOUNT_PRICE_QTY_COND1 => OLEDRION_DISCOUNT_PRICE_QTY_COND1_TEXT,
185
            OLEDRION_DISCOUNT_PRICE_QTY_COND2 => OLEDRION_DISCOUNT_PRICE_QTY_COND2_TEXT,
186
            OLEDRION_DISCOUNT_PRICE_QTY_COND3 => OLEDRION_DISCOUNT_PRICE_QTY_COND3_TEXT,
187
            OLEDRION_DISCOUNT_PRICE_QTY_COND4 => OLEDRION_DISCOUNT_PRICE_QTY_COND4_TEXT,
188
            OLEDRION_DISCOUNT_PRICE_QTY_COND5 => OLEDRION_DISCOUNT_PRICE_QTY_COND5_TEXT
189
        );
190
        $xoopsTpl->assign('disc_price_case_qty_cond_options', $quantityConditions);
191
        $xoopsTpl->assign('disc_price_case_qty_cond_selected', $item->getVar('disc_price_case_qty_cond'));
192
193
        // **** Réductions sur les frais de port ****
194
        $disc_shipping_type_checked1 = $disc_shipping_type_checked2 = $disc_shipping_type_checked3 = $disc_shipping_type_checked4 = '';
195
        switch ($item->getVar('disc_shipping_type')) {
196
            case OLEDRION_DISCOUNT_SHIPPING_TYPE1:
197
                $disc_shipping_type_checked1 = $checked;
198
                break;
199
            case OLEDRION_DISCOUNT_SHIPPING_TYPE2:
200
                $disc_shipping_type_checked2 = $checked;
201
                break;
202
            case OLEDRION_DISCOUNT_SHIPPING_TYPE3:
203
                $disc_shipping_type_checked3 = $checked;
204
                break;
205
            case OLEDRION_DISCOUNT_SHIPPING_TYPE4:
206
                $disc_shipping_type_checked4 = $checked;
207
                break;
208
        }
209
        $discountForTemplate['disc_shipping_type_checked1'] = $disc_shipping_type_checked1;
210
        $discountForTemplate['disc_shipping_type_checked2'] = $disc_shipping_type_checked2;
211
        $discountForTemplate['disc_shipping_type_checked3'] = $disc_shipping_type_checked3;
212
        $discountForTemplate['disc_shipping_type_checked4'] = $disc_shipping_type_checked4;
213
214
        // Groupes
215
        $xoopsTpl->assign('disc_groups_selected', $item->getVar('disc_group'));
216
        $memberHandler = xoops_getHandler('member');
217
        $groups        = array();
218
        $groups        = $memberHandler->getGroupList();
219
        $groups[0]     = _ALL;
220
        ksort($groups);
221
        $xoopsTpl->assign('disc_groups_options', $groups);
222
223
        // Catégories
224
        $categories = $h_oledrion_cat->getAllCategories(new Oledrion_parameters());
225
        $mytree     = new Oledrion_XoopsObjectTree($categories, 'cat_cid', 'cat_pid');
226
227 View Code Duplication
        if (OledrionUtility::checkVerXoops($module, '2.5.9')) {
0 ignored issues
show
The method checkVerXoops() does not seem to exist on object<OledrionUtility>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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...
228
            $categoriesSelect0 = $topic_tree->makeSelectElement('disc_cat_cid', 'cat_title', '-', $item->getVar('disc_cat_cid'), true, 0, '', '');
229
            $categoriesSelect  = $categoriesSelect0->render();
230
        } else {
231
            $categoriesSelect = $mytree->makeSelBox('disc_cat_cid', 'cat_title', '-', $item->getVar('disc_cat_cid'), _ALL);
0 ignored issues
show
Deprecated Code introduced by
The method Oledrion_XoopsObjectTree::makeSelBox() has been deprecated with message: since 2.5.9, please use makeSelectElement()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
232
        }
233
234
        $discountForTemplate['disc_cat_cid_select'] = $categoriesSelect;
235
236
        // Fabricants
237
        $vendors    = $h_oledrion_vendors->getList();
238
        $vendors[0] = _ALL;
239
        ksort($vendors);
240
241
        $xoopsTpl->assign('disc_vendor_id_options', $vendors);
242
        $xoopsTpl->assign('disc_vendor_id_selected', $item->getVar('disc_vendor_id'));
243
244
        // Catégorie
245
        $xoopsTpl->assign('disc_cat_cid_options', $categoriesSelect);
246
247
        // Produits
248
        $products    = $h_oledrion_products->getList();
249
        $products[0] = _ALL;
250
        ksort($products);
251
        $xoopsTpl->assign('disc_product_id_options', $products);
252
        $xoopsTpl->assign('disc_product_id_selected', $item->getVar('disc_product_id'));
253
254
        $productsSelect = $h_oledrion_products->productSelector(new Oledrion_parameters(array(
255
                                                                                            'caption'     => _AM_OLEDRION_DISCOUNT_PRODUCT,
256
                                                                                            'name'        => 'disc_product_id',
257
                                                                                            'value'       => $item->getVar('disc_product_id'),
258
                                                                                            'size'        => 1,
259
                                                                                            'multiple'    => false,
260
                                                                                            'values'      => null,
261
                                                                                            'showAll'     => true,
262
                                                                                            'sort'        => 'product_title',
263
                                                                                            'order'       => 'ASC',
264
                                                                                            'formName'    => 'frmdiscount',
265
                                                                                            'description' => _AM_OLEDRION_DISCOUNT_HELP1,
266
                                                                                            'withNull'    => _ALL
267
                                                                                        )));
268
        $xoopsTpl->assign('disc_product_id', $productsSelect->render());
269
270
        $xoopsTpl->assign('discount', $discountForTemplate);
271
        $xoopsTpl->assign('currencyName', OledrionUtility::getModuleOption('money_full'));
272
        //$editor = OledrionUtility::getWysiwygForm(_AM_OLEDRION_DISCOUNT_DESCR, 'disc_description', $item->getVar('disc_description','e'), 15, 60, 'description_hidden');
273
        //$xoopsTpl->assign('editor', $editor->render());
274
275
        $xoopsTpl->display('db:oledrion_admin_discounts.tpl');
276
        require_once OLEDRION_ADMIN_PATH . 'admin_footer.php';
277
278
        break;
279
280
    // ****************************************************************************************************************
281
    case 'copy': // Duplication d'une réduction
282
        // ****************************************************************************************************************
283
        xoops_cp_header();
284
        $id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
285
        if (empty($id)) {
286
            OledrionUtility::redirect(_AM_OLEDRION_ERROR_1, $baseurl, 5);
287
        }
288
        $opRedirect = 'discounts';
289
        $item       = null;
290
        $item       = $h_oledrion_discounts->get($id);
291
        if (is_object($item)) {
292
            $newDiscount = $item->xoopsClone();
293 View Code Duplication
            if (OLEDRION_DUPLICATED_PLACE === 'right') {
294
                $newDiscount->setVar('disc_title', xoops_trim($item->getVar('disc_title')) . ' ' . _AM_OLEDRION_DUPLICATED);
295
            } else {
296
                $newDiscount->setVar('disc_title', _AM_OLEDRION_DUPLICATED . ' ' . xoops_trim($item->getVar('disc_title')));
297
            }
298
            $newDiscount->setVar('disc_id', 0);
299
            $newDiscount->setNew();
300
            $res = $h_oledrion_discounts->insert($newDiscount, true);
301
            if ($res) {
302
                OledrionUtility::updateCache();
303
                OledrionUtility::redirect(_AM_OLEDRION_SAVE_OK, $baseurl . '?op=' . $opRedirect, 2);
304
            } else {
305
                OledrionUtility::redirect(_AM_OLEDRION_SAVE_PB, $baseurl . '?op=' . $opRedirect, 5);
306
            }
307
        } else {
308
            OledrionUtility::redirect(_AM_OLEDRION_NOT_FOUND, $baseurl . '?op=' . $opRedirect, 5);
309
        }
310
        break;
311
312
    // ****************************************************************************************************************
313
    case 'saveedit': // Enregistrement d'une réduction après modification ou ajout
314
        // ****************************************************************************************************************
315
        xoops_cp_header();
316
        $id = isset($_POST['disc_id']) ? (int)$_POST['disc_id'] : 0;
317
        if (!empty($id)) {
318
            $edit = true;
319
            $item = $h_oledrion_discounts->get($id);
320
            if (!is_object($item)) {
321
                OledrionUtility::redirect(_AM_OLEDRION_NOT_FOUND, $baseurl, 5);
322
            }
323
            $item->unsetNew();
324
        } else {
325
            $item = $h_oledrion_discounts->create(true);
326
        }
327
        $opRedirect = 'discounts';
328
        $item->setVars($_POST);
329
        if (isset($_POST['disc_pediod']) && (int)$_POST['disc_pediod'] == 1) {
330
            $item->setVar('disc_date_from', strtotime($_POST['disc_date_from']));
331
            $item->setVar('disc_date_to', strtotime($_POST['disc_date_to']));
332
        } else {
333
            $item->setVar('disc_date_from', 0);
334
            $item->setVar('disc_date_to', 0);
335
        }
336
        $res = $h_oledrion_discounts->insert($item);
337
        if ($res) {
338
            OledrionUtility::updateCache();
339
            OledrionUtility::redirect(_AM_OLEDRION_SAVE_OK, $baseurl . '?op=' . $opRedirect, 2);
340
        } else {
341
            OledrionUtility::redirect(_AM_OLEDRION_SAVE_PB, $baseurl . '?op=' . $opRedirect, 5);
342
        }
343
        break;
344
345
    // ****************************************************************************************************************
346 View Code Duplication
    case 'delete': // Suppression d'une réduction
347
        // ****************************************************************************************************************
348
        xoops_cp_header();
349
        $id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
350
        if (empty($id)) {
351
            OledrionUtility::redirect(_AM_OLEDRION_ERROR_1, $baseurl, 5);
352
        }
353
        $opRedirect = 'discounts';
354
        $item       = $h_oledrion_discounts->get($id);
355
        if (is_object($item)) {
356
            $res = $h_oledrion_discounts->delete($item, true);
357
            if ($res) {
358
                OledrionUtility::updateCache();
359
                OledrionUtility::redirect(_AM_OLEDRION_SAVE_OK, $baseurl . '?op=' . $opRedirect, 2);
360
            } else {
361
                OledrionUtility::redirect(_AM_OLEDRION_SAVE_PB, $baseurl . '?op=' . $opRedirect, 5);
362
            }
363
        } else {
364
            OledrionUtility::redirect(_AM_OLEDRION_NOT_FOUND, $baseurl . '?op=' . $opRedirect, 5);
365
        }
366
        break;
367
}
368