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

admin/actions/delivery.php (1 issue)

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      Hossein Azizabadi ([email protected])
18
 */
19
20
/**
21
 * Check is admin
22
 */
23
if (!defined('OLEDRION_ADMIN')) {
24
    exit();
25
}
26
27
switch ($action) {
28
    case 'default':
29
        xoops_cp_header();
30
        $adminObject = \Xmf\Module\Admin::getInstance();
31
        $adminObject->displayNavigation('index.php?op=delivery');
32
33
        $start    = isset($_GET['start']) ? (int)$_GET['start'] : 0;
34
        $delivery = array();
35
        $form     = "<form method='post' action='$baseurl' name='frmadddelivery' id='frmadddelivery'><input type='hidden' name='op' id='op' value='delivery'><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_ADMENU20, 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
        $delivery = $h_oledrion_delivery->getAllDelivery(new Oledrion_parameters(array(
41
                                                                                     'start' => $start,
42
                                                                                     'limit' => $limit
43
                                                                                 )));
44
45
        $class = '';
46
        echo "<table width='100%' cellspacing='1' cellpadding='3' border='0' class='outer'>";
47
        echo "<tr><th align='center'>" . _AM_OLEDRION_ID . "</th><th align='center'>" . _AM_OLEDRION_DELIVERY_TITLE . "</th><th align='center'>" . _OLEDRION_ONLINE . "</th><th align='center'>" . _AM_OLEDRION_ACTION . '</th></tr>';
48
        foreach ($delivery as $item) {
49
            $id        = $item->getVar('delivery_id');
50
            $class     = ($class === 'even') ? 'odd' : 'even';
51
            $actions   = array();
52
            $actions[] = "<a href='$baseurl?op=delivery&action=edit&id=" . $id . "' title='" . _OLEDRION_EDIT . "'>" . $icones['edit'] . '</a>';
53
            $actions[] = "<a href='$baseurl?op=delivery&action=delete&id=" . $id . "' title='" . _OLEDRION_DELETE . "'" . $conf_msg . '>' . $icones['delete'] . '</a>';
54
            $online    = $item->getVar('delivery_online') == 1 ? _YES : _NO;
55
            echo "<tr class='" . $class . "'>\n";
56
            echo "<td align='center'>" . $id . "</td><td align='center'>" . $item->getVar('delivery_title') . "</td><td align='center'>" . $online . "</td><td align='center'>" . implode(' ', $actions) . "</td>\n";
57
            echo "<tr>\n";
58
        }
59
        $class = ($class === 'even') ? 'odd' : 'even';
60
        echo "<tr class='" . $class . "'>\n";
61
        echo "<td colspan='4' align='center'>" . $form . "</td>\n";
62
        echo "</tr>\n";
63
        echo '</table>';
64
        require_once OLEDRION_ADMIN_PATH . 'admin_footer.php';
65
        break;
66
67
    case 'add':
68
    case 'edit':
69
        xoops_cp_header();
70 View Code Duplication
        if ($action === 'edit') {
71
            $title = _AM_OLEDRION_DELIVERY_EDIT;
72
            $id    = isset($_GET['id']) ? (int)$_GET['id'] : 0;
73
            if (empty($id)) {
74
                OledrionUtility::redirect(_AM_OLEDRION_ERROR_1, $baseurl, 5);
75
            }
76
            // Item exits ?
77
            $item = null;
78
            $item = $h_oledrion_delivery->get($id);
79
            if (!is_object($item)) {
80
                OledrionUtility::redirect(_AM_OLEDRION_NOT_FOUND, $baseurl, 5);
81
            }
82
            $edit         = true;
83
            $label_submit = _AM_OLEDRION_MODIFY;
84
        } else {
85
            $title        = _AM_OLEDRION_DELIVERY_ADD;
86
            $item         = $h_oledrion_delivery->create(true);
87
            $label_submit = _AM_OLEDRION_ADD;
88
            $edit         = false;
89
        }
90
        $sform = new XoopsThemeForm($title, 'frmadddelivery', $baseurl);
91
        $sform->addElement(new XoopsFormHidden('op', 'delivery'));
92
        $sform->addElement(new XoopsFormHidden('action', 'save'));
93
        $sform->addElement(new XoopsFormHidden('delivery_id', $item->getVar('delivery_id')));
94
        $sform->addElement(new XoopsFormText(_AM_OLEDRION_DELIVERY_TITLE, 'delivery_title', 50, 150, $item->getVar('delivery_title', 'e')), true);
95
96
        // Add payment options ************************************************************
97
        $payments = $deliveryPayments = $payments_d = $deliveryPayments_d = array();
98
99
        $criteria = new Criteria('payment_id', 0, '<>');
100
        $criteria->setSort('payment_title');
101
        $payments = $h_oledrion_payment->getObjects($criteria);
102
        foreach ($payments as $oneitem) {
103
            $payments_d[$oneitem->getVar('payment_id')] = xoops_trim($oneitem->getVar('payment_title'));
104
        }
105
106
        if (empty($payments_d)) {
107
            OledrionUtility::redirect(_AM_OLEDRION_DELIVERY_PAYMENTADD, $baseurl, 5);
108
        }
109
110
        if ($edit) {
111
            $criteria = new CriteriaCompo();
112
            $criteria->add(new Criteria('dp_delivery', $item->getVar('delivery_id'), '='));
113
            $deliveryPayments = $h_oledrion_delivery_payment->getObjects($criteria);
114
            foreach ($deliveryPayments as $oneproduct) {
115
                $deliveryPayments_d[] = $oneproduct->getVar('dp_payment');
116
            }
117
        }
118
        $paymentSelect = new XoopsFormSelect(_AM_OLEDRION_DELIVERY_PAYMENT, 'payments', $deliveryPayments_d, 5, true);
119
        $paymentSelect->addOptionArray($payments_d);
120
        $paymentSelect->setDescription(_AM_OLEDRION_SELECT_HLP);
121
        $sform->addElement($paymentSelect, true);
122
123 View Code Duplication
        if ($action === 'edit' && $item->pictureExists()) {
124
            $pictureTray = new XoopsFormElementTray(_AM_OLEDRION_CURRENT_PICTURE, '<br>');
125
            $pictureTray->addElement(new XoopsFormLabel('', "<img src='" . $item->getPictureUrl() . "' alt='' border='0'>"));
126
            $deleteCheckbox = new XoopsFormCheckBox('', 'delpicture');
127
            $deleteCheckbox->addOption(1, _DELETE);
128
            $pictureTray->addElement($deleteCheckbox);
129
            $sform->addElement($pictureTray);
130
            unset($pictureTray, $deleteCheckbox);
131
        }
132
        $sform->addElement(new XoopsFormFile(_AM_OLEDRION_PICTURE, 'attachedfile', OledrionUtility::getModuleOption('maxuploadsize')), false);
133
        $editor = OledrionUtility::getWysiwygForm(_AM_OLEDRION_DESCRIPTION, 'delivery_description', $item->getVar('delivery_description', 'e'), 15, 60, 'description_hidden');
134
        if ($editor) {
135
            $sform->addElement($editor, false);
136
        }
137
        $sform->addElement(new XoopsFormRadioYN(_OLEDRION_ONLINE_HLP, 'delivery_online', $item->getVar('delivery_online')), true);
138
        $button_tray = new XoopsFormElementTray('', '');
139
        $submit_btn  = new XoopsFormButton('', 'post', $label_submit, 'submit');
140
        $button_tray->addElement($submit_btn);
141
        $sform->addElement($button_tray);
142
        $sform =& OledrionUtility::formMarkRequiredFields($sform);
143
        $sform->display();
144
        require_once OLEDRION_ADMIN_PATH . 'admin_footer.php';
145
        break;
146
147
    case 'save':
148
        xoops_cp_header();
149
        $id = isset($_POST['delivery_id']) ? (int)$_POST['delivery_id'] : 0;
150
        if (!empty($id)) {
151
            $edit = true;
152
            $item = $h_oledrion_delivery->get($id);
153
            if (!is_object($item)) {
154
                OledrionUtility::redirect(_AM_OLEDRION_NOT_FOUND, $baseurl, 5);
155
            }
156
            $item->unsetNew();
157
        } else {
158
            $item = $h_oledrion_delivery->create(true);
159
        }
160
        $opRedirect = 'delivery';
161
        $item->setVars($_POST);
162
        if (isset($_POST['delpicture']) && (int)$_POST['delpicture'] == 1) {
163
            $item->deletePicture();
164
        }
165
        $destname = '';
166
        $res1     = OledrionUtility::uploadFile(0, OLEDRION_PICTURES_PATH);
167 View Code Duplication
        if ($res1) {
168
            if (OledrionUtility::getModuleOption('resize_others')) { // Eventuellement on redimensionne l'image
169
                OledrionUtility::resizePicture(OLEDRION_PICTURES_PATH . '/' . $destname, OLEDRION_PICTURES_PATH . '/' . $destname, OledrionUtility::getModuleOption('images_width'), OledrionUtility::getModuleOption('images_height'), true);
170
            }
171
            $item->setVar('delivery_image', basename($destname));
172
        } else {
173
            if ($res1 !== false) {
174
                echo $res1;
175
            }
176
        }
177
        $res = $h_oledrion_delivery->insert($item);
178
179
        $delivery_id = $item->getVar('delivery_id');
180
181
        // Save payments for each delivery type
182
        if ($edit) {
183
            // Suppression préalable
184
            $criteria = new CriteriaCompo();
185
            $criteria->add(new Criteria('dp_delivery', $delivery_id, '='));
186
            $h_oledrion_delivery_payment->deleteAll($criteria);
187
        }
188
        if (isset($_POST['payments'])) {
189
            foreach ($_POST['payments'] as $id2) {
190
                $item2 = $h_oledrion_delivery_payment->create(true);
191
                $item2->setVar('dp_delivery', $delivery_id);
192
                $item2->setVar('dp_payment', (int)$id2);
193
                $res1 = $h_oledrion_delivery_payment->insert($item2);
194
            }
195
        }
196
197
        if ($res) {
198
            OledrionUtility::updateCache();
199
            OledrionUtility::redirect(_AM_OLEDRION_SAVE_OK, $baseurl . '?op=' . $opRedirect, 2);
200
        } else {
201
            OledrionUtility::redirect(_AM_OLEDRION_SAVE_PB, $baseurl . '?op=' . $opRedirect, 5);
202
        }
203
        break;
204
205 View Code Duplication
    case 'delete':
206
        xoops_cp_header();
207
        $id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
208
        if ($id == 0) {
209
            OledrionUtility::redirect(_AM_OLEDRION_ERROR_1, $baseurl, 5);
210
        }
211
        $delivery = null;
212
        $delivery = $h_oledrion_delivery->get($id);
213
        if (!is_object($delivery)) {
214
            OledrionUtility::redirect(_AM_OLEDRION_ERROR_10, $baseurl, 5);
215
        }
216
        $msg = sprintf(_AM_OLEDRION_CONF_DEL_ITEM, $delivery->getVar('delivery_title'));
217
        xoops_confirm(array('op' => 'delivery', 'action' => 'confdelete', 'id' => $id), 'index.php', $msg);
218
        break;
219
220 View Code Duplication
    case 'confdelete':
221
222
        xoops_cp_header();
223
        $id = isset($_POST['id']) ? (int)$_POST['id'] : 0;
224
        if (empty($id)) {
225
            OledrionUtility::redirect(_AM_OLEDRION_ERROR_1, $baseurl, 5);
226
        }
227
        $opRedirect = 'delivery';
228
229
        $item = null;
230
        $item = $h_oledrion_delivery->get($id);
231
        if (is_object($item)) {
232
            //Delete delivery payment info
233
            $criteria = new CriteriaCompo();
234
            $criteria->add(new Criteria('dp_delivery', $item->getVar('delivery_id')));
235
            $h_oledrion_delivery_payment->deleteAll($criteria);
236
            // Delete delivery
237
            $res = $h_oledrion_delivery->delete($item);
238
            if ($res) {
239
                OledrionUtility::updateCache();
240
                OledrionUtility::redirect(_AM_OLEDRION_SAVE_OK, $baseurl . '?op=' . $opRedirect, 2);
241
            } else {
242
                OledrionUtility::redirect(_AM_OLEDRION_SAVE_PB, $baseurl . '?op=' . $opRedirect, 5);
243
            }
244
        } else {
245
            OledrionUtility::redirect(_AM_OLEDRION_NOT_FOUND, $baseurl . '?op=' . $opRedirect, 5);
246
        }
247
        break;
248
}
249