Passed
Pull Request — master (#14)
by Michael
04:13
created

admin/actions/products.php (3 issues)

Labels
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
use XoopsModules\Oledrion;
21
22
/**
23
 * Gestion des produits (dans l'administration)
24
 */
25
if (!defined('OLEDRION_ADMIN')) {
26
    exit();
27
}
28
29
switch ($action) {
30
    // ****************************************************************************************************************
31
    case 'default': // Gestion des produits
32
33
        // ****************************************************************************************************************
34
        xoops_cp_header();
35
        $adminObject = \Xmf\Module\Admin::getInstance();
36
        $adminObject->displayNavigation('index.php?op=products');
37
38
        $products = $categories = $vats = [];
39
40
        // Récupération des données uniques
41
        $categories = $categoryHandler->getAllCategories(new Oledrion\Parameters([
42
                                                                                     'start'   => 0,
43
                                                                                     'limit'   => 0,
44
                                                                                     'sort'    => 'cat_title',
45
                                                                                     'order'   => 'ASC',
46
                                                                                     'idaskey' => true,
47
                                                                                 ]));
48
49
        $form = "<form method='post' action='$baseurl' name='frmadddproduct' id='frmadddproduct'><input type='hidden' name='op' id='op' value='products'><input type='hidden' name='action' id='action' value='add'><input type='submit' name='btngo' id='btngo' value='"
50
                . _AM_OLEDRION_ADD_ITEM
51
                . "'></form>";
52
        echo $form;
53
        echo "<br><form method='get' action='$baseurl' name='frmaddeditproduct' id='frmaddeditproduct'>"
54
             . _OLEDRION_PRODUCT_ID
55
             . " <input type='text' name='id' id='id' value='' size='4'> <input type='hidden' name='op' id='op' value='products'><input type='radio' name='action' id='action' value='edit'>"
56
             . _OLEDRION_EDIT
57
             . " <input type='radio' name='action' id='action' value='confdelete'>"
58
             . _OLEDRION_DELETE
59
             . " <input type='submit' name='btngo' id='btngo' value='"
60
             . _GO
61
             . "'></form>";
62
63
        $vats = $vatHandler->getAllVats(new Oledrion\Parameters());
64
65
        $start = \Xmf\Request::getInt('start', 0, 'GET');
66
67
        $filter_product_id = $filter_product_cid = $filter_product_recommended = $filter_product_price = $filter_product_online = 0;
68
        // B.R. New
69
        // TODO: Code Reviewers Are these filters neccessary? Not sure what they do.
70
        $filter_skip_packing = $filter_skip_location = $filter_skip_delivery = 0;
71
        // B.R. End
72
        $filter_product_title = $filter_product_sku = '';
73
74
        $newFilter = false;
75
        $criteria  = new \CriteriaCompo();
76
        $criteria->add(new \Criteria('product_id', 0, '<>'));
77
78
        if (\Xmf\Request::hasVar('filter_product_id', 'POST')) {
79
            if (0 != \Xmf\Request::getInt('filter_product_id', 0, 'POST')) {
80
                $criteria->add(new \Criteria('product_id', \Xmf\Request::getInt('filter_product_id', 0, 'POST')), '=');
81
            }
82
            $filter_product_id = \Xmf\Request::getInt('filter_product_id', 0, 'POST');
83
            $newFilter         = true;
84
        }
85
        if (\Xmf\Request::hasVar('filter_product_cid', 'POST') && 0 != \Xmf\Request::getInt('filter_product_cid', 0, 'POST')) {
86
            $criteria->add(new \Criteria('product_cid', \Xmf\Request::getInt('filter_product_cid', 0, 'POST')), '=');
87
            $filter_product_cid = \Xmf\Request::getInt('filter_product_cid', 0, 'POST');
88
            $newFilter          = true;
89
        }
90
        if (\Xmf\Request::hasVar('filter_product_recommended', 'POST') && 0 != \Xmf\Request::getInt('filter_product_recommended', 0, 'POST')) {
91
            if (1 == \Xmf\Request::getInt('filter_product_recommended', 0, 'POST')) {
92
                $criteria->add(new \Criteria('product_recommended', '0000-00-00', '<>'));
93
            } else {
94
                $criteria->add(new \Criteria('product_recommended', '0000-00-00', '='));
95
            }
96
            $filter_product_recommended = \Xmf\Request::getInt('filter_product_recommended', 0, 'POST');
97
            $newFilter                  = true;
98
        }
99
        if (\Xmf\Request::hasVar('filter_product_title', 'POST') && '' !== xoops_trim($_POST['filter_product_title'])) {
100
            $criteria->add(new \Criteria('product_title', '%' . $_POST['filter_product_title'] . '%', 'LIKE'));
101
            $filter_product_title = $_POST['filter_product_title'];
102
            $newFilter            = true;
103
        }
104
        if (\Xmf\Request::hasVar('filter_product_sku', 'POST') && '' !== xoops_trim($_POST['filter_product_sku'])) {
105
            $criteria->add(new \Criteria('product_sku', '%' . $_POST['filter_product_sku'] . '%', 'LIKE'));
106
            $filter_product_sku = $_POST['filter_product_sku'];
107
            $newFilter          = true;
108
        }
109
        if (\Xmf\Request::hasVar('filter_product_online', 'POST') && 0 != \Xmf\Request::getInt('filter_product_online', 0, 'POST')) {
110
            $criteria->add(new \Criteria('product_online', \Xmf\Request::getInt('filter_product_online', 0, 'POST') - 1, '='));
111
            $filter_product_online = \Xmf\Request::getInt('filter_product_online', 0, 'POST');
112
            $newFilter             = true;
113
        }
114
        if (\Xmf\Request::hasVar('filter_product_price', 'POST') && 0 != \Xmf\Request::getInt('filter_product_price', 0, 'POST')) {
115
            $criteria->add(new \Criteria('product_price', \Xmf\Request::getInt('filter_product_price', 0, 'POST')), '>=');
116
            $filter_product_price = \Xmf\Request::getInt('filter_product_price', 0, 'POST');
117
            $newFilter            = true;
118
        }
119
        // B.R. Added: $filter_skip_packing $filter_skip_location $filter_skip_delivery
120
        if (0 == $filter_product_id && 0 == $filter_product_cid && 0 == $filter_product_recommended && 0 == $filter_product_price && 0 == $filter_product_online && '' === $filter_product_title && '' === $filter_product_sku && 0 == $filter_skip_packing && 0 == $filter_skip_location
121
            && 0 == $filter_skip_delivery) {
122
            $newFilter = true;
123
        }
124
125
        if (!$newFilter && isset($_SESSION['oledrion_filter'])) {
126
            $criteria                   = unserialize($_SESSION['oledrion_filter']);
127
            $filter_product_id          = $_SESSION['filter_product_id'];
128
            $filter_product_cid         = $_SESSION['filter_product_cid'];
129
            $filter_product_recommended = $_SESSION['filter_product_recommended'];
130
            $filter_product_title       = $_SESSION['filter_product_title'];
131
            $filter_product_sku         = $_SESSION['filter_product_sku'];
132
            $filter_product_online      = $_SESSION['filter_product_online'];
133
            $filter_product_price       = $_SESSION['filter_product_price'];
134
            // B.R. New
135
            $filter_skip_packing  = $_SESSION['filter_skip_packing'];
136
            $filter_skip_location = $_SESSION['filter_skip_location'];
137
            $filter_skip_delivery = $_SESSION['filter_skip_delivery'];
138
            // B.R. End
139
        }
140
141
        $_SESSION['oledrion_filter']            = serialize($criteria);
142
        $_SESSION['filter_product_id']          = $filter_product_id;
143
        $_SESSION['filter_product_cid']         = $filter_product_cid;
144
        $_SESSION['filter_product_recommended'] = $filter_product_recommended;
145
        $_SESSION['filter_product_title']       = $filter_product_title;
146
        $_SESSION['filter_product_sku']         = $filter_product_sku;
147
        $_SESSION['filter_product_online']      = $filter_product_online;
148
        $_SESSION['filter_product_price']       = $filter_product_price;
149
        // B.R. New
150
        $_SESSION['filter_skip_packing']  = $filter_skip_packing;
151
        $_SESSION['filter_skip_location'] = $filter_skip_location;
152
        $_SESSION['filter_skip_delivery'] = $filter_skip_delivery;
153
        // B.R. End
154
155
        $itemsCount = $productsHandler->getCount($criteria); // Recherche du nombre total de produits répondants aux critères
156
        Oledrion\Utility::htitle(_MI_OLEDRION_ADMENU4 . ' (' . $itemsCount . ')', 4);
157
158
        if ($itemsCount > $limit) {
159
            require_once XOOPS_ROOT_PATH . '/class/pagenav.php';
160
            $pagenav = new \XoopsPageNav($itemsCount, $limit, $start, 'start', 'op=products');
161
        }
162
        $mytree = new Oledrion\XoopsObjectTree($categories, 'cat_cid', 'cat_pid');
163
164
        $selectCateg0 = $mytree->makeSelectElement('filter_product_cid', 'cat_title', '-', $filter_product_cid, true, 0, '', '');
165
        $selectCateg  = $selectCateg0->render();
166
167
        $onlineSelect = Oledrion\Utility::htmlSelect('filter_product_online', [2 => _YES, 1 => _NO], $filter_product_online);
168
        // B.R. New
169
        $skipPackingSelect  = Oledrion\Utility::htmlSelect('filter_skip_packing', [2 => _YES, 1 => _NO], $filter_skip_packing);
170
        $skipLocationSelect = Oledrion\Utility::htmlSelect('filter_skip_location', [2 => _YES, 1 => _NO], $filter_skip_location);
171
        $skipDeliverySelect = Oledrion\Utility::htmlSelect('filter_skip_delivery', [2 => _YES, 1 => _NO], $filter_skip_delivery);
172
        // B.R. End
173
        $recommendedSelect = Oledrion\Utility::htmlSelect('filter_product_recommended', [1 => _YES, 2 => _NO], $filter_product_recommended);
174
175
        $criteria->setLimit($limit);
176
        $criteria->setStart($start);
177
        $criteria->setSort('product_id');
178
179
        $products = $productsHandler->getObjects($criteria);
180
        $class    = '';
181
        $span     = 8;
182
        if (isset($pagenav) && is_object($pagenav)) {
183
            echo "<div align='left'>" . $pagenav->renderNav() . '</div>';
184
        }
185
        echo "<table width='100%' cellspacing='1' cellpadding='3' border='0' class='outer'>";
186
        echo "<tr><th align='center'>"
187
             . _AM_OLEDRION_ID
188
             . "</th><th align='center'>"
189
             . _OLEDRION_TITLE
190
             . "</th><th align='center'>"
191
             . _OLEDRION_CATEGORY
192
             . "</th><th align='center'>"
193
             . _OLEDRION_NUMBER
194
             . '<br>'
195
             . _OLEDRION_EXTRA_ID
196
             . "</th><th align='center'>"
197
             . _OLEDRION_ONLINE
198
             . "</th><th align='center'>"
199
             . _AM_OLEDRION_RECOMMENDED
200
             . "</th><th align='center'>"
201
             . _OLEDRION_DATE
202
             . '</th>';
203
        if (Oledrion\Utility::getModuleOption('use_price')) {
204
            echo "<th align='center'>" . _OLEDRION_PRICE . '</th>';
205
            $span = 9;
206
        }
207
        echo "<th align='center'>" . _AM_OLEDRION_ACTION . '</th></tr>';
208
209
        echo "<tr><form method='post' action='$baseurl'><th align='center'><input type='text' size='3' name='filter_product_id' id='filter_product_id' value='$filter_product_id'></th>\n";
210
        echo "<th align='center'><input type='text' size='25' name='filter_product_title' id='filter_product_title' value='$filter_product_title'></th>\n";
211
        echo "<th align='center'>" . $selectCateg . "</th>\n";
212
        echo "<th align='center'><input type='text' size='25' name='filter_product_sku' id='filter_product_sku' value='$filter_product_sku'></th>\n";
213
        echo "<th align='center'>" . $onlineSelect . "</th>\n";
214
        // B.R. Start
215
        echo "<th align='center'>" . $skipPackingSelect . "</th>\n";
216
        echo "<th align='center'>" . $skipLocationSelect . "</th>\n";
217
        echo "<th align='center'>" . $skipDeliverySelect . "</th>\n";
218
        // B.R. End
219
        echo "<th align='center'>" . $recommendedSelect . "</th>\n";
220
        echo "<th align='center'>&nbsp;</th>\n";
221
        echo "<th align='center'><input type='text' size='5' name='filter_product_price' id='filter_product_price' value='$filter_product_price'></th>\n";
222
        echo "<th align='center'><input type='hidden' name='op' id='op' value='products'><input type='submit' name='btngo' id='btngo' value='" . _GO . "'></th></form></tr>\n";
223
        foreach ($products as $item) {
224
            $class       = ('even' === $class) ? 'odd' : 'even';
225
            $id          = $item->getVar('product_id');
226
            $recommended = '';
227
            if ($item->isRecommended()) {
228
                // If the product is recommended, the link is displayed to stop recommending it
229
                $recommended = "<a href='" . $baseurl . '?op=products&action=unrecommend&product_id=' . $id . "' title='" . _AM_OLEDRION_DONOTRECOMMEND_IT . "'><img alt='" . _AM_OLEDRION_DONOTRECOMMEND_IT . "' src='" . OLEDRION_IMAGES_URL . "heart_delete.png' alt=''></a>";
230
            } else {
231
                // Otherwise we will display the link that allows us to recommend it
232
                $recommended = "<a href='" . $baseurl . '?op=products&action=recommend&product_id=' . $id . "' title='" . _AM_OLEDRION_RECOMMEND_IT . "'><img alt='" . _AM_OLEDRION_RECOMMEND_IT . "' src='" . OLEDRION_IMAGES_URL . "heart_add.png' alt=''></a>";
233
            }
234
235
            $actions = [];
236
237
            $actions[] = "<a href='$baseurl?op=products&action=related&id=" . $id . "' title='" . _OLEDRION_RELATED_PRODUCTS . "'>" . $icons['details'] . '</a>';
238
239
            $actions[] = "<a href='$baseurl?op=products&action=edit&id=" . $id . "' title='" . _OLEDRION_EDIT . "'>" . $icons['edit'] . '</a>';
240
            $actions[] = "<a href='$baseurl?op=products&action=copy&id=" . $id . "' title='" . _OLEDRION_DUPLICATE_PRODUCT . "'>" . $icons['copy'] . '</a>';
241
            $actions[] = "<a href='$baseurl?op=products&action=confdelete&id=" . $id . "' title='" . _OLEDRION_DELETE . "'>" . $icons['delete'] . '</a>';
242
            $online    = 1 == $item->getVar('product_online') ? _YES : _NO;
243
            // B.R. New
244
            $skipPacking  = 1 == $item->getVar('skip_packing') ? _YES : _NO;
245
            $skipLocation = 1 == $item->getVar('skip_location') ? _YES : _NO;
246
            $skipDelivery = 1 == $item->getVar('skip_delivery') ? _YES : _NO;
247
            // B.R. End
248
            echo "<tr class='" . $class . "'>\n";
249
            if (isset($categories[$item->getVar('product_cid')])) {
250
                $productCategory = $categories[$item->getVar('product_cid')]->getVar('cat_title');
251
                $categoryUrl     = $categories[$item->getVar('product_cid')]->getLink();
252
            } else {
253
                $productCategory = '';
254
                $categoryUrl     = '#';
255
            }
256
            $productLink = "<a href='" . $item->getLink() . "' target='blank'>" . $item->getVar('product_title') . '</a>';
257
            if ((float)$item->getVar('product_discount_price') > 0) {
258
                $priceLine = '<s>' . $oledrionCurrency->amountForDisplay($item->getVar('product_price')) . '</s>  ' . $oledrionCurrency->amountForDisplay($item->getVar('product_discount_price'));
259
            } else {
260
                $priceLine = $oledrionCurrency->amountForDisplay($item->getVar('product_price'));
261
            }
262
263
            echo "<td align='center'>"
264
                 . $id
265
                 . "</td><td align ='left'>"
266
                 . $productLink
267
                 . "</td><td align='left'><a href='"
268
                 . $categoryUrl
269
                 . "' target='blank'>"
270
                 . $productCategory
271
                 . "</a></td><td align='center'>"
272
                 . $item->getVar('product_sku')
273
                 . ' / '
274
                 . $item->getVar('product_extraid')
275
                 . "</td><td align='center'>"
276
                 . $online
277
                 . "</td><td align='center'>"
278
                 . $recommended
279
                 . "</td><td align='center'>"
280
                 . $item->getVar('product_date')
281
                 . '</td>';
282
            if (Oledrion\Utility::getModuleOption('use_price')) {
283
                echo "<td align='right'>" . $priceLine . '</td>';
284
            }
285
            echo "<td align='center'>" . implode(' ', $actions) . "</td>\n";
286
            echo "<tr>\n";
287
        }
288
        $class = ('even' === $class) ? 'odd' : 'even';
289
        echo "<tr class='" . $class . "'>\n";
290
        echo "<td colspan='$span' align='center'>" . $form . "</td>\n";
291
        echo "</tr>\n";
292
        echo '</table>';
293
        if (isset($pagenav) && is_object($pagenav)) {
294
            echo "<div align='right'>" . $pagenav->renderNav() . '</div>';
295
        }
296
        //require_once OLEDRION_ADMIN_PATH . 'admin_footer.php';
297
298
        break;
299
    // ****************************************************************************************************************
300
    case 'unrecommend': // Arrêter de recommander un produit
301
302
        // ****************************************************************************************************************
303
        $opRedirect = '?op=products';
304
        if (\Xmf\Request::hasVar('product_id', 'GET')) {
305
            $product_id = \Xmf\Request::getInt('product_id', 0, 'GET');
306
            $product    = null;
307
            $product    = $productsHandler->get($product_id);
308
            if (is_object($product)) {
309
                $product->unsetRecommended();
310
                if ($productsHandler->insert($product, true)) {
311
                    Oledrion\Utility::redirect(_AM_OLEDRION_SAVE_OK, $baseurl . $opRedirect, 1);
312
                } else {
313
                    Oledrion\Utility::redirect(_AM_OLEDRION_SAVE_PB, $baseurl . $opRedirect, 4);
314
                }
315
            } else {
316
                Oledrion\Utility::redirect(_AM_OLEDRION_NOT_FOUND, $baseurl . $opRedirect, 4);
317
            }
318
        } else {
319
            Oledrion\Utility::redirect(_AM_OLEDRION_ERROR_1, $baseurl . $opRedirect, 4);
320
        }
321
322
        break;
323
    // ****************************************************************************************************************
324
    case 'recommend': // Recommander un produit
325
326
        // ****************************************************************************************************************
327
        $opRedirect = '?op=products';
328
        if (\Xmf\Request::hasVar('product_id', 'GET')) {
329
            $product_id = \Xmf\Request::getInt('product_id', 0, 'GET');
330
            $product    = null;
331
            $product    = $productsHandler->get($product_id);
332
            if (is_object($product)) {
333
                $product->setRecommended();
334
                if ($productsHandler->insert($product, true)) {
335
                    Oledrion\Utility::redirect(_AM_OLEDRION_SAVE_OK, $baseurl . $opRedirect, 1);
336
                } else {
337
                    Oledrion\Utility::redirect(_AM_OLEDRION_SAVE_PB, $baseurl . $opRedirect, 4);
338
                }
339
            } else {
340
                Oledrion\Utility::redirect(_AM_OLEDRION_NOT_FOUND, $baseurl . $opRedirect, 4);
341
            }
342
        } else {
343
            Oledrion\Utility::redirect(_AM_OLEDRION_ERROR_1, $baseurl . $opRedirect, 4);
344
        }
345
346
        break;
347
    // ****************************************************************************************************************
348
    case 'add': // Ajout d'un produit
349
350
    case 'edit': // Edition d'un produit
351
352
        // ****************************************************************************************************************
353
        xoops_cp_header();
354
        global $xoopsUser;
355
356
        if ('edit' === $action) {
357
            $title = _AM_OLEDRION_EDIT_PRODUCT;
358
            $id    = \Xmf\Request::getInt('id', 0, 'GET');
359
            if (empty($id)) {
360
                Oledrion\Utility::redirect(_AM_OLEDRION_ERROR_1, $baseurl, 5);
361
            }
362
            // Item exits ?
363
            $item = null;
364
            $item = $productsHandler->get($id);
365
            if (!is_object($item)) {
366
                Oledrion\Utility::redirect(_AM_OLEDRION_NOT_FOUND, $baseurl, 5);
367
            }
368
            $edit         = true;
369
            $label_submit = _AM_OLEDRION_MODIFY;
370
        } else {
371
            $title = _AM_OLEDRION_ADD_PRODUCT;
372
            $item  = $productsHandler->create(true);
373
            $item->setVar('product_online', 1);
374
            // B.R. Start
375
            $item->setVar('skip_packing', 0);
376
            $item->setVar('skip_location', 0);
377
            $item->setVar('skip_delivery', 0);
378
            // B.R. End
379
            if (OLEDRION_AUTO_FILL_MANUAL_DATE) {
380
                $item->setVar('product_date', formatTimestamp(time(), 's'));
381
            }
382
            $label_submit = _AM_OLEDRION_ADD;
383
            $edit         = false;
384
        }
385
386
        $categories = $categoryHandler->getAllCategories(new Oledrion\Parameters());
387
        if (0 === count($categories)) {
388
            Oledrion\Utility::redirect(_AM_OLEDRION_ERROR_8, $baseurl, 5);
389
        }
390
        // TVA
391
        $vats = $vatsForDisplay = [];
392
        $vats = $vatHandler->getAllVats(new Oledrion\Parameters());
393
        if (0 == count($vats) && Oledrion\Utility::getModuleOption('use_price')) {
394
            Oledrion\Utility::redirect(_AM_OLEDRION_ERROR_9, $baseurl, 5);
395
        }
396
        foreach ($vats as $onevat) {
397
            $vatsForDisplay[$onevat->getVar('vat_id')] = $onevat->getVar('vat_rate');
398
        }
399
400
        $mytree = new Oledrion\XoopsObjectTree($categories, 'cat_cid', 'cat_pid');
401
402
        $sform = new \XoopsThemeForm($title, 'frmproduct', $baseurl);
403
        $sform->setExtra('enctype="multipart/form-data"');
404
        $sform->addElement(new \XoopsFormHidden('op', 'products'));
405
        $sform->addElement(new \XoopsFormHidden('action', 'saveedit'));
406
        $sform->addElement(new \XoopsFormHidden('product_id', $item->getVar('product_id')));
407
        $sform->addElement(new \XoopsFormHidden('product_submitter', $xoopsUser->getVar('uid')));
408
409
        $sform->addElement(new \XoopsFormText(_OLEDRION_TITLE, 'product_title', 50, 255, $item->getVar('product_title', 'e')), true);
410
411
        // Vendeurs *************************************************************
412
        $vendors = $vendorsForDisplay = [];
413
        $vendors = $vendorsHandler->getAllVendors(new Oledrion\Parameters());
414
        foreach ($vendors as $oneVendor) {
415
            $vendorsForDisplay[$oneVendor->getVar('vendor_id')] = $oneVendor->getVar('vendor_name');
416
        }
417
        $vendorsSelect = new \XoopsFormSelect(_OLEDRION_VENDOR, 'product_vendor_id', $item->getVar('product_vendor_id'));
418
        $vendorsSelect->addOptionArray($vendorsForDisplay);
419
        $sform->addElement($vendorsSelect, true);
420
421
        $select_categ = $mytree->makeSelectElement('product_cid', 'cat_title', '-', $item->getVar('product_cid'), true, 0, '', _AM_OLEDRION_CATEG_HLP);
422
        $sform->addElement($select_categ);
423
424
        // B.R. New
425
        $sform->addElement(new \XoopsFormRadioYN(_OLEDRION_SKIP_PACKING, 'skip_packing', $item->getVar('skip_packing')), true);
426
        $sform->addElement(new \XoopsFormRadioYN(_OLEDRION_SKIP_LOCATION, 'skip_location', $item->getVar('skip_location')), true);
427
        $sform->addElement(new \XoopsFormRadioYN(_OLEDRION_SKIP_DELIVERY, 'skip_delivery', $item->getVar('skip_delivery')), true);
428
        // End New
429
430
        $deliveryTime = new \XoopsFormText(_OLEDRION_DELIVERY_TIME, 'product_delivery_time', 5, 5, $item->getVar('product_delivery_time', 'e'));
431
        $deliveryTime->setDescription(_OLEDRION_IN_DAYS);
432
        $sform->addElement($deliveryTime, false);
433
434
        $sform->addElement(new \XoopsFormText(_OLEDRION_NUMBER, 'product_sku', 10, 60, $item->getVar('product_sku', 'e')), false);
435
        $sform->addElement(new \XoopsFormText(_OLEDRION_EXTRA_ID, 'product_extraid', 10, 50, $item->getVar('product_extraid', 'e')), false);
436
        $sform->addElement(new \XoopsFormText(_OLEDRION_LENGTH, 'product_length', 10, 50, $item->getVar('product_length', 'e')), false);
437
        $sform->addElement(new \XoopsFormText(_OLEDRION_WIDTH, 'product_width', 10, 50, $item->getVar('product_width', 'e')), false);
438
439
        $sform->addElement(new \XoopsFormText(_OLEDRION_MEASURE1, 'product_unitmeasure1', 10, 20, $item->getVar('product_unitmeasure1', 'e')), false);
440
        $sform->addElement(new \XoopsFormText(_OLEDRION_WEIGHT, 'product_weight', 10, 20, $item->getVar('product_weight', 'e')), false);
441
        $sform->addElement(new \XoopsFormText(_OLEDRION_MEASURE2, 'product_unitmeasure2', 10, 20, $item->getVar('product_unitmeasure2', 'e')), false);
442
443
        $downloadUrl = new \XoopsFormText(_OLEDRION_DOWNLOAD_URL, 'product_download_url', 50, 255, $item->getVar('product_download_url', 'e'));
444
        $downloadUrl->setDescription(_AM_OLEDRION_DOWNLOAD_EXAMPLE . ' ' . XOOPS_UPLOAD_PATH . '/' . 'image.png');
445
        $sform->addElement($downloadUrl, false);
446
447
        $sform->addElement(new \XoopsFormText(_AM_OLEDRION_URL_HLP1, 'product_url', 50, 255, $item->getVar('product_url', 'e')), false);
448
        $sform->addElement(new \XoopsFormText(_AM_OLEDRION_URL_HLP2, 'product_url2', 50, 255, $item->getVar('product_url2', 'e')), false);
449
        $sform->addElement(new \XoopsFormText(_AM_OLEDRION_URL_HLP3, 'product_url3', 50, 255, $item->getVar('product_url3', 'e')), false);
450
451
        // Images *************************************************************
452
        if ('edit' === $action && $item->pictureExists()) {
453
            $pictureTray = new \XoopsFormElementTray(_AM_OLEDRION_IMAGE1_HELP, '<br>');
454
            $pictureTray->addElement(new \XoopsFormLabel('', "<img src='" . $item->getPictureUrl() . "' alt='' border='0'>"));
455
            $deleteCheckbox = new \XoopsFormCheckBox('', 'delpicture1');
456
            $deleteCheckbox->addOption(1, _DELETE);
457
            $pictureTray->addElement($deleteCheckbox);
458
            $sform->addElement($pictureTray);
459
            unset($pictureTray, $deleteCheckbox);
460
        }
461
        $sform->addElement(new \XoopsFormFile(_AM_OLEDRION_IMAGE1_CHANGE, 'attachedfile1', Oledrion\Utility::getModuleOption('maxuploadsize')), false);
462
463
        if (!Oledrion\Utility::getModuleOption('create_thumbs')) {
464
            // The user is responsible for creating the thumbnail itself
465
            if ('edit' === $action && $item->thumbExists()) {
466
                $pictureTray = new \XoopsFormElementTray(_AM_OLEDRION_IMAGE2_HELP, '<br>');
467
                $pictureTray->addElement(new \XoopsFormLabel('', "<img src='" . $item->getThumbUrl() . "' alt='' border='0'>"));
468
                $deleteCheckbox = new \XoopsFormCheckBox('', 'delpicture2');
469
                $deleteCheckbox->addOption(1, _DELETE);
470
                $pictureTray->addElement($deleteCheckbox);
471
                $sform->addElement($pictureTray);
472
                unset($pictureTray, $deleteCheckbox);
473
            }
474
            $sform->addElement(new \XoopsFormFile(_AM_OLEDRION_IMAGE2_CHANGE, 'attachedfile2', Oledrion\Utility::getModuleOption('maxuploadsize')), false);
475
        }
476
477
        // En ligne ? *********************************************************
478
        $sform->addElement(new \XoopsFormRadioYN(_OLEDRION_ONLINE_HLP, 'product_online', $item->getVar('product_online')), true);
479
        $sform->addElement(new \XoopsFormText(_OLEDRION_DATE, 'product_date', 50, 255, $item->getVar('product_date', 'e')), false);
480
481
        $date_submit = new \XoopsFormTextDateSelect(_OLEDRION_DATE_SUBMIT, 'product_submitted', 15, $item->getVar('product_submitted', 'e'));
482
        $date_submit->setDescription(_AM_OLEDRION_SUBDATE_HELP);
483
        $sform->addElement($date_submit, false);
484
485
        $sform->addElement(new \XoopsFormHidden('product_hits', $item->getVar('product_hits')));
486
        $sform->addElement(new \XoopsFormHidden('product_rating', $item->getVar('product_rating')));
487
        $sform->addElement(new \XoopsFormHidden('product_votes', $item->getVar('product_votes')));
488
        $sform->addElement(new \XoopsFormHidden('product_comments', $item->getVar('product_comments')));
489
490
        // manufacturers ************************************************************
491
        $manufacturers = $productsManufacturers = $manufacturers_d = $productsManufacturers_d = [];
492
        // Recherche de tous les fabricants
493
        $criteria = new \Criteria('manu_id', 0, '<>');
494
        $criteria->setSort('manu_name');
495
        $manufacturers = $manufacturerHandler->getObjects($criteria);
496
        foreach ($manufacturers as $oneitem) {
497
            $manufacturers_d[$oneitem->getVar('manu_id')] = xoops_trim($oneitem->getVar('manu_name')) . ' ' . xoops_trim($oneitem->getVar('manu_commercialname'));
498
        }
499
        // Recherche des fabricants de ce produit
500
        if ($edit) {
501
            $criteria = new \CriteriaCompo();
502
            $criteria->add(new \Criteria('pm_product_id', $item->getVar('product_id'), '='));
503
            $productsManufacturers = $productsmanuHandler->getObjects($criteria);
504
            foreach ($productsManufacturers as $oneproduct) {
505
                $productsManufacturers_d[] = $oneproduct->getVar('pm_manu_id');
506
            }
507
        }
508
        $manufacturersSelect = new \XoopsFormSelect(_OLEDRION_MANUFACTURER, 'manufacturers', $productsManufacturers_d, 5, true);
509
        $manufacturersSelect->addOptionArray($manufacturers_d);
510
        $manufacturersSelect->setDescription(_AM_OLEDRION_SELECT_HLP);
511
        $sform->addElement($manufacturersSelect, true);
512
513
        // Produits relatifs ****************************************************
514
        $relatedProducts = $productRelated = $relatedProducts_d = $productRelated_d = [];
515
        // Recherche de tous les produits sauf celui-là
516
        $criteria = new \Criteria('product_id', $item->getVar('product_id'), '<>');
517
        $criteria->setSort('product_title');
518
        $relatedProducts = $productsHandler->getObjects($criteria);
519
        foreach ($relatedProducts as $oneitem) {
520
            $relatedProducts_d[$oneitem->getVar('product_id')] = xoops_trim($oneitem->getVar('product_title'));
521
        }
522
        // Recherche des produits relatifs à ce produit
523
        if ($edit) {
524
            $criteria = new \CriteriaCompo();
525
            $criteria->add(new \Criteria('related_product_id', $item->getVar('product_id'), '='));
526
            $productRelated = $relatedHandler->getObjects($criteria);
527
            foreach ($productRelated as $oneproduct) {
528
                $productRelated_d[] = $oneproduct->getVar('related_product_related');
529
            }
530
        }
531
        $productsSelect = $productsHandler->productSelector(new Oledrion\Parameters([
532
                                                                                        'caption'  => _OLEDRION_RELATED_PRODUCTS,
533
                                                                                        'name'     => 'relatedproducts',
534
                                                                                        'value'    => $productRelated_d,
535
                                                                                        'size'     => 5,
536
                                                                                        'multiple' => true,
537
                                                                                        'values'   => $productRelated_d,
538
                                                                                        'showAll'  => true,
539
                                                                                        'sort'     => 'product_title',
540
                                                                                        'order'    => 'ASC',
541
                                                                                        'formName' => 'frmproduct',
542
                                                                                    ]));
543
        $sform->addElement($productsSelect);
544
        // ********************************************************************
545
546
        if (Oledrion\Utility::getModuleOption('use_price')) {
547
            // TVA ****************************************************************
548
            $vatSelect = new \XoopsFormSelect(_OLEDRION_VAT, 'product_vat_id', $item->getVar('product_vat_id'));
549
            $vatSelect->addOptionArray($vatsForDisplay);
550
            $sform->addElement($vatSelect, true);
551
552
            $sform->addElement(new \XoopsFormText(_OLEDRION_PRICE, 'product_price', 20, 20, $item->getVar('product_price', 'e')), true);
553
            $sform->addElement(new \XoopsFormText(_AM_OLEDRION_DISCOUNT_HLP, 'product_discount_price', 20, 20, $item->getVar('product_discount_price', 'e')), false);
554
555
            $sform->addElement(new \XoopsFormText(_OLEDRION_SHIPPING_PRICE, 'product_shipping_price', 20, 20, $item->getVar('product_shipping_price', 'e')), false);
556
            $sform->addElement(new \XoopsFormText(_OLEDRION_ECOTAXE, 'product_ecotaxe', 10, 10, $item->getVar('product_ecotaxe', 'e')), false);
557
        }
558
        $sform->addElement(new \XoopsFormText(_OLEDRION_STOCK_QUANTITY, 'product_stock', 10, 10, $item->getVar('product_stock', 'e')), false);
559
560
        $alertStock = new \XoopsFormText(_OLEDRION_STOCK_ALERT, 'product_alert_stock', 10, 10, $item->getVar('product_alert_stock', 'e'));
561
        $alertStock->setDescription(_AM_OLEDRION_STOCK_HLP);
562
        $sform->addElement($alertStock, false);
563
564
        $editor2 = Oledrion\Utility::getWysiwygForm(_OLEDRION_SUMMARY, 'product_summary', $item->getVar('product_summary', 'e'), 15, 60, 'summary_hidden');
565
        if ($editor2) {
0 ignored issues
show
$editor2 is of type XoopsFormEditor, thus it always evaluated to true.
Loading history...
566
            $sform->addElement($editor2, false);
567
        }
568
569
        $editor = Oledrion\Utility::getWysiwygForm(_OLEDRION_DESCRIPTION, 'product_description', $item->getVar('product_description', 'e'), 15, 60, 'description_hidden');
570
        if ($editor) {
0 ignored issues
show
$editor is of type XoopsFormEditor, thus it always evaluated to true.
Loading history...
571
            $sform->addElement($editor, false);
572
        }
573
574
        // Tags
575
        if (Oledrion\Utility::getModuleOption('use_tags')) {
576
            require_once XOOPS_ROOT_PATH . '/modules/tag/include/formtag.php';
577
            $sform->addElement(new \XoopsModules\Tag\FormTag('item_tag', 60, 255, $item->getVar('product_id'), 0));
578
        }
579
580
        // META Data
581
        if ($manual_meta) {
582
            $sform->addElement(new \XoopsFormText(_AM_OLEDRION_META_KEYWORDS, 'product_metakeywords', 50, 255, $item->getVar('product_metakeywords', 'e')), false);
583
            $sform->addElement(new \XoopsFormText(_AM_OLEDRION_META_DESCRIPTION, 'product_metadescription', 50, 255, $item->getVar('product_metadescription', 'e')), false);
584
            $sform->addElement(new \XoopsFormText(_AM_OLEDRION_META_PAGETITLE, 'product_metatitle', 50, 255, $item->getVar('product_metatitle', 'e')), false);
585
        }
586
        // Fichier attaché
587
        if ('edit' === $action && '' !== trim($item->getVar('product_attachment')) && file_exists(XOOPS_UPLOAD_PATH . '/' . trim($item->getVar('product_attachment')))) {
588
            $pictureTray = new \XoopsFormElementTray(_OLEDRION_ATTACHED_FILE, '<br>');
589
            $pictureTray->addElement(new \XoopsFormLabel('', "<a href='" . XOOPS_UPLOAD_URL . '/' . $item->getVar('product_attachment') . "' target='_blank'>" . XOOPS_UPLOAD_URL . '/' . $item->getVar('product_attachment') . '</a>'));
590
            $deleteCheckbox = new \XoopsFormCheckBox('', 'delpicture3');
591
            $deleteCheckbox->addOption(1, _DELETE);
592
            $pictureTray->addElement($deleteCheckbox);
593
            $sform->addElement($pictureTray);
594
            unset($pictureTray, $deleteCheckbox);
595
        }
596
        $downloadFile = new \XoopsFormFile(_OLEDRION_ATTACHED_FILE, 'attachedfile3', Oledrion\Utility::getModuleOption('maxuploadsize'));
597
        $downloadFile->setDescription(_AM_OLEDRION_ATTACHED_HLP);
598
        $sform->addElement($downloadFile, false);
599
600
        if (Oledrion\Utility::getModuleOption('product_property1')) {
601
            $property1select = new \XoopsFormSelect(Oledrion\Utility::getModuleOption('product_property1_title'), 'product_property1', $item->getVar('product_property1'));
602
            $property1Array  = explode('|', Oledrion\Utility::getModuleOption('product_property1'));
603
            foreach ($property1Array as $property1) {
604
                $property1select->addOption($property1);
605
            }
606
            $sform->addElement($property1select, false);
607
        }
608
609
        if (Oledrion\Utility::getModuleOption('product_property2')) {
610
            $property2select = new \XoopsFormSelect(Oledrion\Utility::getModuleOption('product_property2_title'), 'product_property2', $item->getVar('product_property2'));
611
            $property2Array  = explode('|', Oledrion\Utility::getModuleOption('product_property2'));
612
            foreach ($property2Array as $property2) {
613
                $property2select->addOption($property2);
614
            }
615
            $sform->addElement($property2select, false);
616
        }
617
618
        if (Oledrion\Utility::getModuleOption('product_property3')) {
619
            $property3select = new \XoopsFormSelect(Oledrion\Utility::getModuleOption('product_property3_title'), 'product_property3', $item->getVar('product_property3'));
620
            $property3Array  = explode('|', Oledrion\Utility::getModuleOption('product_property3'));
621
            foreach ($property3Array as $property3) {
622
                $property3select->addOption($property3);
623
            }
624
            $sform->addElement($property3select, false);
625
        }
626
627
        if (Oledrion\Utility::getModuleOption('product_property4')) {
628
            $property4select = new \XoopsFormSelect(Oledrion\Utility::getModuleOption('product_property4_title'), 'product_property4', $item->getVar('product_property4'));
629
            $property4Array  = explode('|', Oledrion\Utility::getModuleOption('product_property4'));
630
            foreach ($property4Array as $property4) {
631
                $property4select->addOption($property4);
632
            }
633
            $sform->addElement($property4select, false);
634
        }
635
636
        if (Oledrion\Utility::getModuleOption('product_property5')) {
637
            $property5select = new \XoopsFormSelect(Oledrion\Utility::getModuleOption('product_property5_title'), 'product_property5', $item->getVar('product_property5'));
638
            $property5Array  = explode('|', Oledrion\Utility::getModuleOption('product_property5'));
639
            foreach ($property5Array as $property5) {
640
                $property5select->addOption($property5);
641
            }
642
            $sform->addElement($property5select, false);
643
        }
644
645
        if (Oledrion\Utility::getModuleOption('product_property6')) {
646
            $property6select = new \XoopsFormSelect(Oledrion\Utility::getModuleOption('product_property6_title'), 'product_property6', $item->getVar('product_property6'));
647
            $property6Array  = explode('|', Oledrion\Utility::getModuleOption('product_property6'));
648
            foreach ($property6Array as $property6) {
649
                $property6select->addOption($property6);
650
            }
651
            $sform->addElement($property6select, false);
652
        }
653
654
        if (Oledrion\Utility::getModuleOption('product_property7')) {
655
            $property7select = new \XoopsFormSelect(Oledrion\Utility::getModuleOption('product_property7_title'), 'product_property7', $item->getVar('product_property7'));
656
            $property7Array  = explode('|', Oledrion\Utility::getModuleOption('product_property7'));
657
            foreach ($property7Array as $property7) {
658
                $property7select->addOption($property7);
659
            }
660
            $sform->addElement($property7select, false);
661
        }
662
663
        if (Oledrion\Utility::getModuleOption('product_property8')) {
664
            $property8select = new \XoopsFormSelect(Oledrion\Utility::getModuleOption('product_property8_title'), 'product_property8', $item->getVar('product_property8'));
665
            $property8Array  = explode('|', Oledrion\Utility::getModuleOption('product_property8'));
666
            foreach ($property8Array as $property8) {
667
                $property8select->addOption($property8);
668
            }
669
            $sform->addElement($property8select, false);
670
        }
671
672
        if (Oledrion\Utility::getModuleOption('product_property9')) {
673
            $property9select = new \XoopsFormSelect(Oledrion\Utility::getModuleOption('product_property9_title'), 'product_property9', $item->getVar('product_property9'));
674
            $property9Array  = explode('|', Oledrion\Utility::getModuleOption('product_property9'));
675
            foreach ($property9Array as $property9) {
676
                $property9select->addOption($property9);
677
            }
678
            $sform->addElement($property9select, false);
679
        }
680
681
        if (Oledrion\Utility::getModuleOption('product_property10')) {
682
            $property10select = new \XoopsFormSelect(Oledrion\Utility::getModuleOption('product_property10_title'), 'product_property10', $item->getVar('product_property10'));
683
            $property10Array  = explode('|', Oledrion\Utility::getModuleOption('product_property10'));
684
            foreach ($property10Array as $property10) {
685
                $property10select->addOption($property10);
686
            }
687
            $sform->addElement($property10select, false);
688
        }
689
690
        $buttonTray = new \XoopsFormElementTray('', '');
691
        $submit_btn = new \XoopsFormButton('', 'post', $label_submit, 'submit');
692
        $buttonTray->addElement($submit_btn);
693
        $sform->addElement($buttonTray);
694
695
        $sform = Oledrion\Utility::formMarkRequiredFields($sform);
696
        $sform->display();
697
        require_once OLEDRION_ADMIN_PATH . 'admin_footer.php';
698
699
        break;
700
    // ****************************************************************************************************************
701
    case 'saveedit': // Sauvegarde des informations d'un produit
702
703
        // ****************************************************************************************************************
704
        xoops_cp_header();
705
        $id = \Xmf\Request::getInt('product_id', 0, 'POST');
706
        if ($id > 0) {
707
            $edit = true;
708
            $item = $productsHandler->get($id);
709
            if (!is_object($item)) {
710
                Oledrion\Utility::redirect(_AM_OLEDRION_NOT_FOUND, $baseurl, 5);
711
            }
712
            $item->unsetNew();
713
            $add = false;
714
        } else {
715
            $item = $productsHandler->create(true);
716
            $edit = false;
717
            $add  = true;
718
        }
719
        $opRedirect = 'products';
720
        $item->setVars($_POST);
721
        $item->setVar('product_submitted', strtotime($_POST['product_submitted']));
722
723
        // Suppression de l'image principale
724
        if (\Xmf\Request::hasVar('delpicture1', 'POST') && 1 == \Xmf\Request::getInt('delpicture1', 0, 'POST')) {
725
            $item->deletePicture();
726
        }
727
728
        // Suppression de la vignette
729
        if (\Xmf\Request::hasVar('delpicture2', 'POST') && 1 == \Xmf\Request::getInt('delpicture2', 0, 'POST')) {
730
            $item->deleteThumb();
731
        }
732
        if (\Xmf\Request::hasVar('delpicture3', 'POST') && 1 == \Xmf\Request::getInt('delpicture3', 0, 'POST')) {
733
            $item->deleteAttachment();
734
        }
735
736
        // Upload de la grande image
737
        $destname    = '';
738
        $mainPicture = '';
739
        $res1        = Oledrion\Utility::uploadFile(0, OLEDRION_PICTURES_PATH);
740
        if (true === $res1) {
741
            $mainPicture = $destname;
742
            if (Oledrion\Utility::getModuleOption('resize_main')) {
743
                // On redimensionne l'image principale
744
                Oledrion\Utility::resizePicture(OLEDRION_PICTURES_PATH . '/' . $destname, OLEDRION_PICTURES_PATH . '/' . $destname, Oledrion\Utility::getModuleOption('images_width'), Oledrion\Utility::getModuleOption('images_height'), true);
745
            }
746
            $item->setVar('product_image_url', basename($destname));
747
        } else {
748
            if (false !== $res1) {
749
                echo $res1;
750
            }
751
        }
752
753
        $indiceAttached = 2;
754
        // Upload de la vignette
755
        if (!Oledrion\Utility::getModuleOption('create_thumbs')) {
756
            // L'utilisateur se charge de créer la vignette lui-même
757
            $destname = '';
758
            $res2     = Oledrion\Utility::uploadFile(1, OLEDRION_PICTURES_PATH);
759
            if (true === $res2) {
760
                $item->setVar('product_thumb_url', basename($destname));
761
            } else {
762
                if (false !== $res2) {
763
                    echo $res2;
764
                }
765
            }
766
        } else {
767
            // Il faut créer la vignette pour l'utilisateur
768
            $indiceAttached = 1;
769
            if ('' !== xoops_trim($mainPicture)) {
770
                $thumbName = OLEDRION_THUMBS_PREFIX . $mainPicture;
771
                Oledrion\Utility::resizePicture(OLEDRION_PICTURES_PATH . '/' . $mainPicture, OLEDRION_PICTURES_PATH . '/' . $thumbName, Oledrion\Utility::getModuleOption('thumbs_width'), Oledrion\Utility::getModuleOption('thumbs_height'), true);
772
                $item->setVar('product_thumb_url', $thumbName);
773
            }
774
        }
775
776
        // Téléchargement du fichier attaché
777
        $destname = '';
778
        $res3     = Oledrion\Utility::uploadFile($indiceAttached, OLEDRION_ATTACHED_FILES_PATH);
779
        if (true === $res3) {
780
            $item->setVar('product_attachment', basename($destname));
781
        } else {
782
            if (false !== $res3) {
783
                echo $res3;
784
            }
785
        }
786
787
        $res = $productsHandler->insert($item);
788
        if ($res) {
789
            if (Oledrion\Utility::getModuleOption('use_tags')) {
790
                $tagHandler = \XoopsModules\Tag\Helper::getInstance()->getHandler('Tag'); // xoops_getModuleHandler('tag', 'tag');
791
                $tagHandler->updateByItem($_POST['item_tag'], $item->getVar('product_id'), $xoopsModule->getVar('dirname'), 0);
0 ignored issues
show
The method updateByItem() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of XoopsObjectHandler such as XoopsPersistableObjectHandler. ( Ignorable by Annotation )

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

791
                $tagHandler->/** @scrutinizer ignore-call */ 
792
                             updateByItem($_POST['item_tag'], $item->getVar('product_id'), $xoopsModule->getVar('dirname'), 0);
Loading history...
792
            }
793
794
            $id = $item->getVar('product_id');
795
            // Notifications ******************************************************
796
            if (true === $add) {
797
                //$plugins = Plugin::getInstance();
798
                //$plugins->fireAction(Plugin::EVENT_ON_PRODUCT_CREATE, new Oledrion\Parameters(array('product' => $item)));
799
            }
800
            // Gestion des fabricants ************************************************
801
            if ($edit) {
802
                // Suppression préalable
803
                $criteria = new \CriteriaCompo();
804
                $criteria->add(new \Criteria('pm_product_id', $id, '='));
805
                $productsmanuHandler->deleteAll($criteria);
806
            }
807
            // Puis sauvegarde des données
808
            if (\Xmf\Request::hasVar('manufacturers', 'POST')) {
809
                foreach ($_POST['manufacturers'] as $id2) {
810
                    $item2 = $productsmanuHandler->create(true);
811
                    $item2->setVar('pm_product_id', $id);
812
                    $item2->setVar('pm_manu_id', (int)$id2);
813
                    $res = $productsmanuHandler->insert($item2);
814
                }
815
            }
816
817
            // Gestion des produits relatifs ****************************************
818
            if ($edit) {
819
                // Suppression préalable
820
                $criteria = new \CriteriaCompo();
821
                $criteria->add(new \Criteria('related_product_id', $id, '='));
822
                $relatedHandler->deleteAll($criteria);
823
            }
824
            // Puis sauvegarde des données
825
            if (\Xmf\Request::hasVar('relatedproducts', 'POST')) {
826
                foreach ($_POST['relatedproducts'] as $id2) {
827
                    $item2 = $relatedHandler->create(true);
828
                    $item2->setVar('related_product_id', $id);
829
                    $item2->setVar('related_product_related', (int)$id2);
830
                    $res = $relatedHandler->insert($item2);
831
                }
832
            }
833
            Oledrion\Utility::updateCache();
834
            Oledrion\Utility::redirect(_AM_OLEDRION_SAVE_OK, $baseurl . '?op=' . $opRedirect, 2);
835
        } else {
836
            Oledrion\Utility::redirect(_AM_OLEDRION_SAVE_PB, $baseurl . '?op=' . $opRedirect, 5);
837
        }
838
839
        break;
840
    // ****************************************************************************************************************
841
    case 'copy': // Copier un produit
842
843
        // ****************************************************************************************************************
844
        xoops_cp_header();
845
        $id = \Xmf\Request::getInt('id', 0, 'GET');
846
        if (empty($id)) {
847
            Oledrion\Utility::redirect(_AM_OLEDRION_ERROR_1, $baseurl, 5);
848
        }
849
        $opRedirect = 'products';
850
        $product    = null;
851
        $product    = $productsHandler->get($id);
852
        if (is_object($product)) {
853
            $newProduct   = $productsHandler->cloneProduct($product);
854
            $newProductId = $newProduct->product_id;
855
            if (false !== $newProduct) {
856
                Oledrion\Utility::redirect(_AM_OLEDRION_SAVE_OK, $baseurl . '?op=' . $opRedirect . '&action=edit&id=' . $newProductId, 2);
857
            } else {
858
                Oledrion\Utility::redirect(_AM_OLEDRION_SAVE_PB, $baseurl . '?op=' . $opRedirect, 5);
859
            }
860
        }
861
862
        break;
863
    // ****************************************************************************************************************
864
    case 'confdelete': // Confirmation de la suppression d'un produit
865
866
        // ****************************************************************************************************************
867
        xoops_cp_header();
868
869
        $id = \Xmf\Request::getInt('id', 0, 'GET');
870
        if (0 == $id) {
871
            Oledrion\Utility::redirect(_AM_OLEDRION_ERROR_1, $baseurl, 5);
872
        }
873
        $item = $productsHandler->get($id);
874
        if (!is_object($item)) {
875
            Oledrion\Utility::redirect(_AM_OLEDRION_NOT_FOUND, $baseurl, 5);
876
        }
877
        xoops_confirm(['op' => 'products', 'action' => 'delete', 'id' => $id], 'index.php', _AM_OLEDRION_CONF_DELITEM . '<br>' . $item->getVar('product_title'));
878
879
        break;
880
    // ****************************************************************************************************************
881
    case 'delete': // Suppression d'un produit
882
883
        // ****************************************************************************************************************
884
        xoops_cp_header();
885
        $id = \Xmf\Request::getInt('id', 0, 'POST');
886
        if (0 == $id) {
887
            Oledrion\Utility::redirect(_AM_OLEDRION_ERROR_1, $baseurl, 5);
888
        }
889
        $opRedirect = 'products';
890
        $tblTmp     = [];
891
        $tblTmp     = $caddyHandler->getCommandIdFromProduct($id);
892
        if (0 === count($tblTmp)) {
893
            $item = null;
894
            $item = $productsHandler->get($id);
895
            if (is_object($item)) {
896
                $res = $shelf->deleteProduct($item, true);
897
                if ($res) {
898
                    Oledrion\Utility::updateCache();
899
                    xoops_notification_deletebyitem($xoopsModule->getVar('mid'), 'new_product', $id);
900
                    Oledrion\Utility::redirect(_AM_OLEDRION_SAVE_OK, $baseurl . '?op=' . $opRedirect, 2);
901
                } else {
902
                    Oledrion\Utility::redirect(_AM_OLEDRION_SAVE_PB, $baseurl . '?op=' . $opRedirect, 5);
903
                }
904
            } else {
905
                Oledrion\Utility::redirect(_AM_OLEDRION_NOT_FOUND, $baseurl . '?op=' . $opRedirect, 5);
906
            }
907
        } else {
908
            Oledrion\Utility::htitle(_AM_OLEDRION_SORRY_NOREMOVE, 4);
909
            $tblTmp2 = [];
910
            $tblTmp2 = $commandsHandler->getObjects(new \Criteria('cmd_id', '(' . implode(',', $tblTmp) . ')', 'IN'), true);
911
            echo "<table width='100%' cellspacing='1' cellpadding='3' border='0' class='outer'>";
912
            $class = '';
913
            echo "<tr><th align='center'>" . _AM_OLEDRION_ID . "</th><th align='center'>" . _AM_OLEDRION_DATE . "</th><th align='center'>" . _AM_OLEDRION_CLIENT . "</th><th align='center'>" . _AM_OLEDRION_TOTAL_SHIPP . '</th></tr>';
914
            foreach ($tblTmp2 as $item) {
915
                $class = ('even' === $class) ? 'odd' : 'even';
916
                $date  = formatTimestamp(strtotime($item->getVar('cmd_date')), 's');
917
                echo "<tr class='" . $class . "'>\n";
918
                echo "<td align='right'>"
919
                     . $item->getVar('cmd_id')
920
                     . "</td><td align='center'>"
921
                     . $date
922
                     . "</td><td align='center'>"
923
                     . $item->getVar('cmd_lastname')
924
                     . ' '
925
                     . $item->getVar('cmd_firstname')
926
                     . "</td><td align='center'>"
927
                     . $item->getVar('cmd_total')
928
                     . ' '
929
                     . Oledrion\Utility::getModuleOption('money_short')
930
                     . ' / '
931
                     . $item->getVar('cmd_shipping')
932
                     . ' '
933
                     . Oledrion\Utility::getModuleOption('money_short')
934
                     . "</td>\n";
935
                echo "<tr>\n";
936
            }
937
            echo '</table>';
938
            require_once OLEDRION_ADMIN_PATH . 'admin_footer.php';
939
        }
940
941
        break;
942
    // **********************************************************************************************
943
    case 'related':
944
        // ******************************************************************************************
945
946
        xoops_cp_header();
947
        global $xoopsUser;
948
949
        $id = \Xmf\Request::getInt('id', 0, 'GET');
950
        if (empty($id)) {
951
            Oledrion\Utility::redirect(_AM_OLEDRION_ERROR_1, $baseurl, 5);
952
        }
953
        // Item exits ?
954
        $item = null;
955
        $item = $productsHandler->get($id);
956
        if (!is_object($item)) {
957
            Oledrion\Utility::redirect(_AM_OLEDRION_NOT_FOUND, $baseurl, 5);
958
        }
959
960
        //        $criteria = new \Criteria(/*'product_id', $item->getVar('product_id'), '<>'*/);
961
        $criteria = new \Criteria('product_id', $item->getVar('product_id'), '<>');
962
        $criteria->setSort('product_title');
963
        $relatedProducts = $productsHandler->getObjects($criteria);
964
        foreach ($relatedProducts as $oneitem) {
965
            $relatedProducts_d[$oneitem->getVar('product_id')] = xoops_trim($oneitem->getVar('product_title'));
966
        }
967
968
        $criteria = new \CriteriaCompo();
969
        $criteria->add(new \Criteria('related_product_id', $item->getVar('product_id'), '='));
970
        $productRelated = $relatedHandler->getObjects($criteria);
971
        foreach ($productRelated as $oneproduct) {
972
            $productRelated_d[$oneproduct->getVar('related_product_related')]['id']      = $oneproduct->getVar('related_product_related');
973
            $productRelated_d[$oneproduct->getVar('related_product_related')]['title']   = $relatedProducts_d[$oneproduct->getVar('related_product_related')];
974
            $productRelated_d[$oneproduct->getVar('related_product_related')]['percent'] = $oneproduct->getVar('related_product_percent');
975
        }
976
977
        $sform = new \XoopsThemeForm('related', 'frmproduct', $baseurl);
978
        $sform->setExtra('enctype="multipart/form-data"');
979
        $sform->addElement(new \XoopsFormHidden('op', 'products'));
980
        $sform->addElement(new \XoopsFormHidden('action', 'relatedsave'));
981
        $sform->addElement(new \XoopsFormHidden('product_id', $item->getVar('product_id')));
982
983
        foreach ($productRelated_d as $value) {
984
            $sform->addElement(new \XoopsFormText($value['title'], $value['id'], 5, 5, $value['percent']), false);
985
        }
986
987
        $buttonTray = new \XoopsFormElementTray('', '');
988
        $submit_btn = new \XoopsFormButton('', 'post', _SUBMIT, 'submit');
989
        $buttonTray->addElement($submit_btn);
990
        $sform->addElement($buttonTray);
991
        $sform = Oledrion\Utility::formMarkRequiredFields($sform);
992
        $sform->display();
993
994
        require_once OLEDRION_ADMIN_PATH . 'admin_footer.php';
995
996
        break;
997
    // **********************************************************************************************
998
    case 'relatedsave':
999
        // ******************************************************************************************
1000
1001
        xoops_cp_header();
1002
        $id   = \Xmf\Request::getInt('product_id', 0, 'POST');
1003
        $item = $productsHandler->get($id);
1004
        if (!is_object($item)) {
1005
            Oledrion\Utility::redirect(_AM_OLEDRION_NOT_FOUND, $baseurl, 5);
1006
        }
1007
1008
        foreach ($_POST as $related => $percent) {
1009
            if (is_numeric($related)) {
1010
                $relatedHandler->updatePercent($id, $related, $percent);
1011
            }
1012
        }
1013
1014
        $opRedirect = 'products';
1015
        Oledrion\Utility::redirect(_AM_OLEDRION_SAVE_OK, $baseurl . '?op=' . $opRedirect, 5);
1016
1017
        break;
1018
}
1019