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

admin/actions/files.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      Hervé Thouzard (http://www.herve-thouzard.com/)
18
 */
19
20
/**
21
 * Gestion des fichiers attachés aux produits (dans l'administration)
22
 */
23
if (!defined('OLEDRION_ADMIN')) {
24
    exit();
25
}
26
$opRedirect = 'files';
27
28
switch ($action) {
29
    // ****************************************************************************************************************
30
    case 'default': // Gestion des fichiers attachés
31
        // ****************************************************************************************************************
32
        xoops_cp_header();
33
        $adminObject = \Xmf\Module\Admin::getInstance();
34
        $adminObject->displayNavigation('index.php?op=files');
35
36
        $start = isset($_GET['start']) ? (int)$_GET['start'] : 0;
37
        $form  = "<form method='post' action='$baseurl' name='frmadd' id='frmadd'><input type='hidden' name='op' id='op' value='files'><input type='hidden' name='action' id='action' value='add'><input type='submit' name='btngo' id='btngo' value='" . _AM_OLEDRION_ADD_ITEM . "'></form>";
38
        echo $form;
39
        //        OledrionUtility::htitle(_MI_OLEDRION_ADMENU11, 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
        $itemsCount = $h_oledrion_files->getCount(); // Recherche du nombre total d'éléments
41
        if ($itemsCount > $limit) {
42
            $pagenav = new XoopsPageNav($itemsCount, $limit, $start, 'start', 'op=files');
43
        }
44
        $items = $products = $productsIds = array();
45
        $items = $h_oledrion_files->getItems($start, $limit);
46
        foreach ($items as $item) {
47
            $productsIds[] = $item->getVar('file_product_id');
48
        }
49
        if (count($productsIds) > 0) {
50
            sort($productsIds);
51
            $productsIds = array_unique($productsIds);
52
            $products    = $h_oledrion_products->getProductsFromIDs($productsIds);
53
        }
54
55
        $class = '';
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'>" . _AM_OLEDRION_DESCRIPTION . "</th><th align='center'>" . _AM_OLEDRION_DISCOUNT_PRODUCT . "</th><th align='center'>" . _AM_OLEDRION_ACTION . '</th></tr>';
58
        foreach ($items as $item) {
59
            $class     = ($class === 'even') ? 'odd' : 'even';
60
            $id        = $item->getVar('file_id');
61
            $actions   = array();
62
            $actions[] = "<a href='$baseurl?op=files&action=edit&id=" . $id . "' title='" . _OLEDRION_EDIT . "'>" . $icones['edit'] . '</a>';
63
            $actions[] = "<a href='$baseurl?op=files&action=delete&id=" . $id . "' title='" . _OLEDRION_DELETE . "'" . $conf_msg . '>' . $icones['delete'] . '</a>';
64
            echo "<tr class='" . $class . "'>\n";
65
            $product = isset($products[$item->getVar('file_product_id')]) ? $products[$item->getVar('file_product_id')]->getVar('product_title') : '';
66
            echo "<td align='center'>" . $id . '</td>';
67
            echo '<td>' . $item->getVar('file_description') . "</td><td align='center'>" . $product . "</td><td align='center'>" . implode(' ', $actions) . "</td>\n";
68
            echo "<tr>\n";
69
        }
70
        $class = ($class === 'even') ? 'odd' : 'even';
71
        echo "<tr class='" . $class . "'>\n";
72
        echo "<td colspan='4' align='center'>" . $form . "</td>\n";
73
        echo "</tr>\n";
74
        echo '</table>';
75
        if (isset($pagenav) && is_object($pagenav)) {
76
            echo "<div align='right'>" . $pagenav->renderNav() . '</div>';
77
        }
78
        require_once OLEDRION_ADMIN_PATH . 'admin_footer.php';
79
        break;
80
81
    // ****************************************************************************************************************
82
    case 'add': // Ajout d'un fichier
83
    case 'edit': // Edition d'un fichier
84
        // ****************************************************************************************************************
85
        xoops_cp_header();
86 View Code Duplication
        if ($action === 'edit') {
87
            $title = _AM_OLEDRION_EDIT_FILE;
88
            $id    = isset($_GET['id']) ? (int)$_GET['id'] : 0;
89
            if (empty($id)) {
90
                OledrionUtility::redirect(_AM_OLEDRION_ERROR_1, $baseurl, 5);
91
            }
92
            // Item exits ?
93
            $item = null;
94
            $item = $h_oledrion_files->get($id);
95
            if (!is_object($item)) {
96
                OledrionUtility::redirect(_AM_OLEDRION_NOT_FOUND, $baseurl, 5);
97
            }
98
            $edit         = true;
99
            $label_submit = _AM_OLEDRION_MODIFY;
100
        } else {
101
            $title        = _AM_OLEDRION_ADD_FILE;
102
            $item         = $h_oledrion_files->create(true);
103
            $label_submit = _AM_OLEDRION_ADD;
104
            $edit         = false;
105
        }
106
        $products = array();
107
        $products = $h_oledrion_products->getList();
108
        $sform    = new XoopsThemeForm($title, 'frmaddfile', $baseurl);
109
        $sform->setExtra('enctype="multipart/form-data"');
110
        $sform->addElement(new XoopsFormHidden('op', 'files'));
111
        $sform->addElement(new XoopsFormHidden('action', 'saveedit'));
112
        $sform->addElement(new XoopsFormHidden('file_id', $item->getVar('file_id')));
113
        /*
114
                $file_product_id_select = new XoopsFormSelect(_OLEDRION_PRODUCT, 'file_product_id', $item->getVar('file_product_id', 'e'), 1, false);
115
                $file_product_id_select->addOptionArray($products);
116
                $sform->addElement($file_product_id_select, true);
117
        */
118
        $productsSelect = $h_oledrion_products->productSelector(new Oledrion_parameters(array(
119
                                                                                            'caption'  => _OLEDRION_PRODUCT,
120
                                                                                            'name'     => 'file_product_id',
121
                                                                                            'value'    => $item->getVar('file_product_id', 'e'),
122
                                                                                            'size'     => 1,
123
                                                                                            'multiple' => false,
124
                                                                                            'values'   => null,
125
                                                                                            'showAll'  => true,
126
                                                                                            'sort'     => 'product_title',
127
                                                                                            'order'    => 'ASC',
128
                                                                                            'formName' => 'frmaddfile'
129
                                                                                        )));
130
        $sform->addElement($productsSelect, true);
131
132
        $sform->addElement(new XoopsFormText(_AM_OLEDRION_DESCRIPTION, 'file_description', 50, 255, $item->getVar('file_description', 'e')), true);
133
134
        if ($action === 'edit' && trim($item->getVar('file_filename')) != '' && $item->fileExists()) {
135
            $pictureTray = new XoopsFormElementTray(_AM_OLEDRION_CURRENT_FILE, '<br>');
136
            $pictureTray->addElement(new XoopsFormLabel('', "<a href='" . $item->getURL() . "' target='_blank'>" . $item->getVar('file_filename') . '</a>'));
137
            $sform->addElement($pictureTray);
138
            unset($pictureTray);
139
        }
140
        $sform->addElement(new XoopsFormFile(_AM_OLEDRION_FILENAME, 'attachedfile', OledrionUtility::getModuleOption('maxuploadsize')), false);
141
142
        $button_tray = new XoopsFormElementTray('', '');
143
        $submit_btn  = new XoopsFormButton('', 'post', $label_submit, 'submit');
144
        $button_tray->addElement($submit_btn);
145
        $sform->addElement($button_tray);
146
        $sform =& OledrionUtility::formMarkRequiredFields($sform);
147
        $sform->display();
148
        require_once OLEDRION_ADMIN_PATH . 'admin_footer.php';
149
        break;
150
151
    // ****************************************************************************************************************
152
    case 'saveedit': // Sauvegarde d'un fichier attaché
153
        // ****************************************************************************************************************
154
        xoops_cp_header();
155
        $id = isset($_POST['file_id']) ? (int)$_POST['file_id'] : 0;
156
        if (!empty($id)) {
157
            $edit = true;
158
            $item = $h_oledrion_files->get($id);
159
            if (!is_object($item)) {
160
                OledrionUtility::redirect(_AM_OLEDRION_NOT_FOUND, $baseurl, 5);
161
            }
162
            $item->unsetNew();
163
        } else {
164
            $item = $h_oledrion_files->create(true);
165
        }
166
        $item->setVars($_POST);
167
        $destname = '';
168
        $result   = OledrionUtility::uploadFile(0, OLEDRION_ATTACHED_FILES_PATH);
169
        if ($result === true) {
170
            $item->setVar('file_filename', basename($destname));
171
            $item->setVar('file_mimetype', OledrionUtility::getMimeType(OLEDRION_ATTACHED_FILES_PATH . '/' . $destname));
172
        } else {
173
            if ($result !== false) {
174
                OledrionUtility::redirect(_AM_OLEDRION_SAVE_PB . '<br>' . $result, $baseurl . '?op=' . $opRedirect, 5);
175
            }
176
        }
177
        $res = $h_oledrion_files->insert($item);
178
        if ($res) {
179
            OledrionUtility::updateCache();
180
            OledrionUtility::redirect(_AM_OLEDRION_SAVE_OK, $baseurl . '?op=' . $opRedirect, 2);
181
        } else {
182
            OledrionUtility::redirect(_AM_OLEDRION_SAVE_PB, $baseurl . '?op=' . $opRedirect, 5);
183
        }
184
        break;
185
186
    // ****************************************************************************************************************
187 View Code Duplication
    case 'delete': // Suppression d'un fichier
188
        // ****************************************************************************************************************
189
        xoops_cp_header();
190
        $id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
191
        if (empty($id)) {
192
            OledrionUtility::redirect(_AM_OLEDRION_ERROR_1, $baseurl, 5);
193
        }
194
        $item = null;
195
        $item = $h_oledrion_files->get($id);
196
        if (is_object($item)) {
197
            $res = $h_oledrion_files->deleteAttachedFile($item);
198
            if ($res) {
199
                OledrionUtility::updateCache();
200
                OledrionUtility::redirect(_AM_OLEDRION_SAVE_OK, $baseurl . '?op=' . $opRedirect, 2);
201
            } else {
202
                OledrionUtility::redirect(_AM_OLEDRION_SAVE_PB, $baseurl . '?op=' . $opRedirect, 5);
203
            }
204
        } else {
205
            OledrionUtility::redirect(_AM_OLEDRION_NOT_FOUND, $baseurl . '?op=' . $opRedirect, 5);
206
        }
207
        break;
208
}
209