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

admin/actions/manufacturers.php (1 issue)

Severity
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 fabricants (dans l'administration)
24
 */
25
if (!defined('OLEDRION_ADMIN')) {
26
    exit();
27
}
28
switch ($action) {
29
    // ****************************************************************************************************************
30
    case 'default': // Liste des fabricants
31
32
        // ****************************************************************************************************************
33
        xoops_cp_header();
34
        $adminObject = \Xmf\Module\Admin::getInstance();
35
        $adminObject->displayNavigation('index.php?op=manufacturers');
36
37
        $vats = [];
38
        $form = "<form method='post' action='$baseurl' name='frmaddmanufacturer' id='frmaddmanufacturer'><input type='hidden' name='op' id='op' value='manufacturers'><input type='hidden' name='action' id='action' value='add'><input type='submit' name='btngo' id='btngo' value='"
39
                . _AM_OLEDRION_ADD_ITEM
40
                . "'></form>";
41
        echo $form;
42
        //        Oledrion\Utility::htitle(_MI_OLEDRION_ADMENU3, 4);
43
44
        $start    = \Xmf\Request::getInt('start', 0, 'GET');
45
        $criteria = new \CriteriaCompo();
46
        $criteria->add(new \Criteria('manu_id', 0, '<>'));
47
48
        $itemsCount = $manufacturerHandler->getCount($criteria); // Recherche du nombre total de fabricants
49
        if ($itemsCount > $limit) {
50
            $pagenav = new \XoopsPageNav($itemsCount, $limit, $start, 'start', 'op=manufacturers');
51
        }
52
53
        $criteria->setLimit($limit);
54
        $criteria->setStart($start);
55
        $criteria->setSort('manu_name, manu_commercialname');
56
57
        $manufacturers = $manufacturerHandler->getObjects($criteria);
58
        $class         = '';
59
        echo "<table width='100%' cellspacing='1' cellpadding='3' border='0' class='outer'>";
60
        if (isset($pagenav) && is_object($pagenav)) {
61
            echo "<tr><td colspan='2' align='left'>" . $pagenav->renderNav() . "</td><td align='right' colspan='3'>&nbsp;</td></tr>\n";
62
        }
63
        echo "<tr><th align='center'>" . _OLEDRION_LASTNAME . "</th><th align='center'>" . _OLEDRION_COMM_NAME . "</th><th align='center'>" . _OLEDRION_EMAIL . "</th><th align='center'>" . _AM_OLEDRION_ACTION . '</th></tr>';
64
        foreach ($manufacturers as $item) {
65
            $class     = ('even' === $class) ? 'odd' : 'even';
66
            $id        = $item->getVar('manu_id');
67
            $actions   = [];
68
            $actions[] = "<a href='$baseurl?op=manufacturers&action=edit&id=" . $id . "' title='" . _OLEDRION_EDIT . "'>" . $icons['edit'] . '</a>';
69
            $actions[] = "<a href='$baseurl?op=manufacturers&action=delete&id=" . $id . "' title='" . _OLEDRION_DELETE . "'" . $conf_msg . '>' . $icons['delete'] . '</a>';
70
            echo "<tr class='" . $class . "'>\n";
71
            echo "<td><a href='" . $item->getLink() . "'>" . $item->getVar('manu_name') . "</a></td><td align='left'>" . $item->getVar('manu_commercialname') . "</td><td align='center'>" . $item->getVar('manu_email') . "</td><td align='center'>" . implode(' ', $actions) . "</td>\n";
72
            echo "<tr>\n";
73
        }
74
        $class = ('even' === $class) ? 'odd' : 'even';
75
        echo "<tr class='" . $class . "'>\n";
76
        echo "<td colspan='4' align='center'>" . $form . "</td>\n";
77
        echo "</tr>\n";
78
        echo '</table>';
79
        if (isset($pagenav) && is_object($pagenav)) {
80
            echo "<div align='right'>" . $pagenav->renderNav() . '</div>';
81
        }
82
        require_once OLEDRION_ADMIN_PATH . 'admin_footer.php';
83
84
        break;
85
    // ****************************************************************************************************************
86
    case 'add': // Ajout d'un fabricant
87
88
    case 'edit': // Edition d'un fabricant
89
90
        // ****************************************************************************************************************
91
        xoops_cp_header();
92
        if ('edit' === $action) {
93
            $title = _AM_OLEDRION_EDIT_MANUFACTURER;
94
            $id    = \Xmf\Request::getInt('id', 0, 'GET');
95
            if (empty($id)) {
96
                Oledrion\Utility::redirect(_AM_OLEDRION_ERROR_1, $baseurl, 5);
97
            }
98
            // Item exits ?
99
            $item = null;
100
            $item = $manufacturerHandler->get($id);
101
            if (!is_object($item)) {
102
                Oledrion\Utility::redirect(_AM_OLEDRION_NOT_FOUND, $baseurl, 5);
103
            }
104
            $edit         = true;
105
            $label_submit = _AM_OLEDRION_MODIFY;
106
        } else {
107
            $title        = _AM_OLEDRION_ADD_MANUFACTURER;
108
            $item         = $manufacturerHandler->create(true);
109
            $label_submit = _AM_OLEDRION_ADD;
110
            $edit         = false;
111
        }
112
113
        $sform = new \XoopsThemeForm($title, 'frmmanufacturer', $baseurl);
114
        $sform->setExtra('enctype="multipart/form-data"');
115
        $sform->addElement(new \XoopsFormHidden('op', 'manufacturers'));
116
        $sform->addElement(new \XoopsFormHidden('action', 'saveedit'));
117
        $sform->addElement(new \XoopsFormHidden('manu_id', $item->getVar('manu_id')));
118
        $sform->addElement(new \XoopsFormText(_OLEDRION_LASTNAME, 'manu_name', 50, 255, $item->getVar('manu_name', 'e')), true);
119
        $sform->addElement(new \XoopsFormText(_OLEDRION_COMM_NAME, 'manu_commercialname', 50, 255, $item->getVar('manu_commercialname', 'e')), false);
120
        $sform->addElement(new \XoopsFormText(_OLEDRION_EMAIL, 'manu_email', 50, 255, $item->getVar('manu_email', 'e')), false);
121
        $sform->addElement(new \XoopsFormText(_OLEDRION_SITEURL, 'manu_url', 50, 255, $item->getVar('manu_url', 'e')), false);
122
123
        $editor = Oledrion\Utility::getWysiwygForm(_OLEDRION_MANUFACTURER_INF, 'manu_bio', $item->getVar('manu_bio', 'e'), 15, 60, 'bio_hidden');
124
        if ($editor) {
0 ignored issues
show
$editor is of type XoopsFormEditor, thus it always evaluated to true.
Loading history...
125
            $sform->addElement($editor, false);
126
        }
127
        // Les 5 images
128
        for ($i = 1; $i <= 5; ++$i) {
129
            if ('edit' === $action && $item->pictureExists($i)) {
130
                $pictureTray = new \XoopsFormElementTray(_AM_OLEDRION_CURRENT_PICTURE, '<br>');
131
                $pictureTray->addElement(new \XoopsFormLabel('', "<img src='" . $item->getPictureUrl($i) . "' alt='' border='0'>"));
132
                $deleteCheckbox = new \XoopsFormCheckBox('', 'delpicture' . $i);
133
                $deleteCheckbox->addOption(1, _DELETE);
134
                $pictureTray->addElement($deleteCheckbox);
135
                $sform->addElement($pictureTray);
136
                unset($pictureTray, $deleteCheckbox);
137
            }
138
            $sform->addElement(new \XoopsFormFile(_AM_OLEDRION_PICTURE . ' ' . $i, 'attachedfile' . $i, Oledrion\Utility::getModuleOption('maxuploadsize')), false);
139
        }
140
141
        $buttonTray = new \XoopsFormElementTray('', '');
142
        $submit_btn = new \XoopsFormButton('', 'post', $label_submit, 'submit');
143
        $buttonTray->addElement($submit_btn);
144
        $sform->addElement($buttonTray);
145
146
        $sform = Oledrion\Utility::formMarkRequiredFields($sform);
147
        $sform->display();
148
        require_once OLEDRION_ADMIN_PATH . 'admin_footer.php';
149
150
        break;
151
    // ****************************************************************************************************************
152
    case 'saveedit': // Sauvegarde d'un fabricant
153
154
        // ****************************************************************************************************************
155
        xoops_cp_header();
156
        $id = \Xmf\Request::getInt('manu_id', 0, 'POST');
157
        if (!empty($id)) {
158
            $edit = true;
159
            /** @var Oledrion\Manufacturer $item */
160
            $item = $manufacturerHandler->get($id);
161
            if (!is_object($item)) {
162
                Oledrion\Utility::redirect(_AM_OLEDRION_NOT_FOUND, $baseurl, 5);
163
            }
164
            $item->unsetNew();
165
        } else {
166
            $item = $manufacturerHandler->create(true);
167
        }
168
        $opRedirect = 'manufacturers';
169
        $item->setVars($_POST);
170
        for ($i = 1; $i <= 5; ++$i) {
171
            if (\Xmf\Request::hasVar('delpicture' . $i, 'POST') && 1 == \Xmf\Request::getInt('delpicture' . $i, 0, 'POST')) {
172
                $item->deletePicture($i);
173
            }
174
        }
175
176
        // Upload des fichiers
177
        for ($i = 1; $i <= 5; ++$i) {
178
            $res1 = Oledrion\Utility::uploadFile($i - 1, OLEDRION_PICTURES_PATH);
179
            if (true === $res1) {
180
                if (Oledrion\Utility::getModuleOption('resize_others')) {
181
                    // Eventuellement on redimensionne l'image
182
                    Oledrion\Utility::resizePicture(OLEDRION_PICTURES_PATH . '/' . $destname, OLEDRION_PICTURES_PATH . 'l' . $destname, Oledrion\Utility::getModuleOption('images_width'), Oledrion\Utility::getModuleOption('images_height'), true);
183
                }
184
                $item->setVar('manu_photo' . $i, basename($destname));
185
            } else {
186
                if (false !== $res1) {
187
                    echo $res1;
188
                }
189
            }
190
        }
191
192
        $res = $manufacturerHandler->insert($item);
193
        if ($res) {
194
            Oledrion\Utility::updateCache();
195
            Oledrion\Utility::redirect(_AM_OLEDRION_SAVE_OK, $baseurl . '?op=' . $opRedirect, 2);
196
        } else {
197
            Oledrion\Utility::redirect(_AM_OLEDRION_SAVE_PB, $baseurl . '?op=' . $opRedirect, 5);
198
        }
199
200
        break;
201
    // ****************************************************************************************************************
202
    case 'delete': // Suppression d'un fabricant
203
204
        // ****************************************************************************************************************
205
        xoops_cp_header();
206
        $id = \Xmf\Request::getInt('id', 0, 'GET');
207
        if (empty($id)) {
208
            Oledrion\Utility::redirect(_AM_OLEDRION_ERROR_1, $baseurl, 5);
209
        }
210
        $opRedirect = 'manufacturers';
211
        // On vérifie que ce fabriquant n'est pas relié à des produits
212
        $cnt = $manufacturerHandler->getManufacturerProductsCount($id);
213
        if (0 == $cnt) {
214
            $item = null;
215
            $item = $manufacturerHandler->get($id);
216
            if (is_object($item)) {
217
                $res = $manufacturerHandler->deleteManufacturer($item);
218
                if ($res) {
219
                    Oledrion\Utility::updateCache();
220
                    Oledrion\Utility::redirect(_AM_OLEDRION_SAVE_OK, $baseurl . '?op=' . $opRedirect, 2);
221
                } else {
222
                    Oledrion\Utility::redirect(_AM_OLEDRION_SAVE_PB, $baseurl . '?op=' . $opRedirect, 5);
223
                }
224
            } else {
225
                Oledrion\Utility::redirect(_AM_OLEDRION_NOT_FOUND, $baseurl . '?op=' . $opRedirect, 5);
226
            }
227
        } else {
228
            Oledrion\Utility::redirect(_AM_OLEDRION_ERROR_5, $baseurl . '?op=' . $opRedirect, 5);
229
        }
230
231
        break;
232
}
233