Completed
Push — master ( 592643...f454c2 )
by Michael
03:05 queued 01:21
created

admin/protocol.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * ****************************************************************************
4
 *  - A Project by Developers TEAM For Xoops - ( https://xoops.org )
5
 * ****************************************************************************
6
 *  XNEWSLETTER - MODULE FOR XOOPS
7
 *  Copyright (c) 2007 - 2012
8
 *  Goffy ( wedega.com )
9
 *
10
 *  You may not change or alter any portion of this comment or credits
11
 *  of supporting developers from this source code or any supporting
12
 *  source code which is considered copyrighted (c) material of the
13
 *  original comment or credit authors.
14
 *
15
 *  This program is distributed in the hope that it will be useful,
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 *  GNU General Public License for more details.
19
 *  ---------------------------------------------------------------------------
20
 * @copyright  Goffy ( wedega.com )
21
 * @license    GPL 2.0
22
 * @package    xnewsletter
23
 * @author     Goffy ( [email protected] )
24
 *
25
 * ****************************************************************************
26
 */
27
28
use Xmf\Request;
29
30
$currentFile = basename(__FILE__);
31
require_once __DIR__ . '/admin_header.php';
32
xoops_cp_header();
33
34
// We recovered the value of the argument op in the URL$
35
$op = \Xmf\Request::getString('op', 'list');
36
37
$adminObject = \Xmf\Module\Admin::getInstance();
38
$letterAdmin = \Xmf\Module\Admin::getInstance();
39
40
switch ($op) {
41
    case 'list':
42
    case 'list_protocols':
43
        echo $letterAdmin->displayNavigation($currentFile);
44
        
45
        $limit = $helper->getConfig('adminperpage');
46
        $start = \Xmf\Request::getInt('start', 0);
47
        
48
        $sql = 'SELECT protocol_letter_id FROM ' . $xoopsDB->prefix('xnewsletter_protocol') . ' GROUP BY protocol_letter_id';
49
        $prot_letters = $xoopsDB->query($sql);
50
        $protocol_letters_total = $prot_letters->num_rows;
51
        
52
        $sql = 'SELECT protocol_letter_id FROM ' . $xoopsDB->prefix('xnewsletter_protocol') . ' GROUP BY protocol_letter_id LIMIT ' . $start.', ' . $limit;
53
        $prot_letters = $xoopsDB->query($sql);
54
        
55
        // View Table
56
        echo "
57
            <table class='outer width100' cellspacing='1'>
58
                <tr>
59
                    <th>" . _AM_XNEWSLETTER_LETTER_ID . '</th>
60
                    <th>' . _AM_XNEWSLETTER_LETTER_TITLE . '</th>
61
                    <th>' . _AM_XNEWSLETTER_PROTOCOL_LAST_STATUS . '</th>
62
                    <th>' . _AM_XNEWSLETTER_PROTOCOL_CREATED . '</th>
63
                    <th>' . _AM_XNEWSLETTER_FORMACTION . '</th>
64
                </tr>';
65
                                $class = 'odd';
66
        //first show misc protocol items
67
        echo "<tr class='{$class}'>";
68
        $class = ('even' === $class) ? 'odd' : 'even';
69
        echo '<td> - </td>';
70
        echo '<td>' . _AM_XNEWSLETTER_PROTOCOL_MISC . '</td>';
71
72
        $protocolCriteria = new \CriteriaCompo();
73
        $protocolCriteria->add(new \Criteria('protocol_letter_id', '0'));
74
        $protocolCriteria->setSort('protocol_id');
75
        $protocolCriteria->setOrder('DESC');
76
        $protocolCount = $helper->getHandler('Protocol')->getCount($protocolCriteria);
77
        $protocolCriteria->setLimit(2);
78
        $protocolObjs               = $helper->getHandler('Protocol')->getAll($protocolCriteria);
79
        $protocol_status            = '';
80
        $protocol_created           = '';
81
        $protocol_created_formatted = '';
82
        $p                          = 0;
83 View Code Duplication
        foreach ($protocolObjs as $protocol_id => $protocolObj) {
84
            ++$p;
85
            if (count($protocolObjs) > 1) {
86
                $protocol_status .= "($p) ";
87
            }
88
            $protocol_status            .= $protocolObj->getVar('protocol_status') . '<br>';
89
            $protocol_created_formatted .= formatTimestamp($protocolObj->getVar('protocol_created'), 'M') . '<br>';
90
        }
91
        if ($protocolCount > 2) {
92
            $protocol_status .= '...';
93
        }
94
        echo "
95
                <td class='center'>
96
                    <a href='?op=list_letter&letter_id=0'>" . $protocol_status . "</a>
97
                </td>
98
                <td class='center'>{$protocol_created_formatted}</td>
99
                <td class='center'>
100
                    <a href='?op=list_letter&letter_id=0'><img src=" . XNEWSLETTER_ICONS_URL . "/xn_details.png alt='" . _AM_XNEWSLETTER_DETAILS . "' title='" . _AM_XNEWSLETTER_DETAILS . "'></a>
101
                </td>
102
            </tr>";
103
        while (false !== ($prot_letter = $xoopsDB->fetchArray($prot_letters))) {
104
            $protocol_letter_id = $prot_letter['protocol_letter_id'];
105
            $letterCriteria = new \CriteriaCompo();
106
            $letterCriteria->add(new \Criteria('letter_id', $protocol_letter_id));
107
            $letterCount = $helper->getHandler('Letter')->getCount();
108
            $letterObjs = $helper->getHandler('Letter')->getAll($letterCriteria);
109
110
            if ($letterCount > 0) {
111
                foreach (array_keys($letterObjs) as $i) {
112
                    $protocolCriteria = new \CriteriaCompo();
113
                    $protocolCriteria->add(new \Criteria('protocol_letter_id', $letterObjs[$i]->getVar('letter_id')));
114
                    $protocolCriteria->setSort('protocol_id');
115
                    $protocolCriteria->setOrder('DESC');
116
                    $protocolCount = $helper->getHandler('Protocol')->getCount($protocolCriteria);
117
                    if ($protocolCount > 0) {
118
                        $protocolCriteria->setLimit(2);
119
                        $protocolObjs     = $helper->getHandler('Protocol')->getAll($protocolCriteria);
120
                        $protocol_status  = '';
121
                        $protocol_created = '';
122
123
                        echo "<tr class='{$class}'>";
124
                        $class = ('even' === $class) ? 'odd' : 'even';
125
                        echo '<td>' . $i . '</td>';
126
                        echo '<td>' . $letterObjs[$i]->getVar('letter_title') . '</td>';
127
128
                        $p = 0;
129 View Code Duplication
                        foreach ($protocolObjs as $protocol) {
130
                            ++$p;
131
                            if (count($protocolObjs) > 1) {
132
                                $protocol_status .= "($p) ";
133
                            }
134
                            $protocol_status  .= $protocol->getVar('protocol_status') . '<br>';
135
                            $protocol_created .= formatTimestamp($protocol->getVar('protocol_created'), 'M') . '<br>';
136
                        }
137
                        if ($protocolCount > 2) {
138
                            $protocol_status .= '...';
139
                        }
140
                        echo "
141
                                <td class='center'>
142
                                    <a href='?op=list_letter&letter_id=" . $i . "'>" . $protocol_status . "</a>
143
                                </td>
144
                                <td class='center'>" . $protocol_created . "</td>
145
                                <td class='center'>
146
                                    <a href='?op=list_letter&letter_id=" . $i . "'><img src=" . XNEWSLETTER_ICONS_URL . "/xn_details.png alt='" . _AM_XNEWSLETTER_DETAILS . "' title='" . _AM_XNEWSLETTER_DETAILS . "'></a>
147
                                </td>
148
                            </tr>";
149
                    }
150
                }
151
            }
152
        }
153
        echo '</table>';
154 View Code Duplication
        if ($protocol_letters_total > $limit) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
155
            require_once XOOPS_ROOT_PATH . '/class/pagenav.php';
156
            $pagenav = new \XoopsPageNav($protocol_letters_total, $limit, $start, 'start', 'op=list');
157
            $pagenav = $pagenav->renderNav(4);
158
        } else {
159
            $pagenav = '';
160
        }
161
        echo '<br>';
162
        echo '<div>' . $pagenav . '</div>';
163
        echo '<br>';
164
        break;
165
    case 'list_letter':
166
        $letter_id = isset($_REQUEST['letter_id']) ? $_REQUEST['letter_id'] : '0';
167
        $adminObject->displayNavigation($currentFile);
168
        $adminObject->addItemButton(_AM_XNEWSLETTER_PROTOCOLLIST, '?op=list', 'list');
169
170
        if ($letter_id > '0') {
171
            $adminObject->addItemButton(_AM_XNEWSLETTER_LETTER_DELETE_ALL, '?op=delete_protocol_list&letter_id=' . $letter_id, 'delete');
172
        }
173
        $adminObject->displayButton('left');
174
        $limit = $helper->getConfig('adminperpage');
175
176
        $protocolCriteria = new \CriteriaCompo();
177
        $protocolCriteria->add(new \Criteria('protocol_letter_id', $letter_id));
178
        $protocolCriteria->setSort('protocol_id');
179
        $protocolCriteria->setOrder('DESC');
180
        $protocolCount = $helper->getHandler('Protocol')->getCount($protocolCriteria);
181
        $start         = \Xmf\Request::getInt('start', 0);
182
        $protocolCriteria->setStart($start);
183
        $protocolCriteria->setLimit($limit);
184
        $protocolObjs = $helper->getHandler('Protocol')->getAll($protocolCriteria);
185 View Code Duplication
        if ($protocolCount > $limit) {
186
            require_once XOOPS_ROOT_PATH . '/class/pagenav.php';
187
            $pagenav = new \XoopsPageNav($protocolCount, $limit, $start, 'start', 'op=list_letter&letter_id=' . $letter_id);
188
            $pagenav = $pagenav->renderNav(4);
189
        } else {
190
            $pagenav = '';
191
        }
192
193
        // View Table
194
        $letterObj = $helper->getHandler('Letter')->get($letter_id);
195
        echo '<h2>' . $letterObj->getVar('letter_title') . '</h2>';
196
        echo "
197
            <table class='outer width100' cellspacing='1'>
198
                <tr>
199
                    <th>" . _AM_XNEWSLETTER_PROTOCOL_ID . '</th>
200
                    <th>' . _AM_XNEWSLETTER_PROTOCOL_SUBSCRIBER_ID . '</th>
201
                    <th>' . _AM_XNEWSLETTER_PROTOCOL_STATUS . '</th>
202
                    <th>' . _AM_XNEWSLETTER_PROTOCOL_SUCCESS . '</th>
203
                    <th>' . _AM_XNEWSLETTER_PROTOCOL_SUBMITTER . '</th>
204
                    <th>' . _AM_XNEWSLETTER_PROTOCOL_CREATED . "</th>
205
                    <th class='center width10'>" . _AM_XNEWSLETTER_FORMACTION . '</th>
206
                </tr>';
207
        if ($protocolCount > 0) {
208
            $class = 'odd';
209
            foreach ($protocolObjs as $protocol_id => $protocolObj) {
210
                echo "<tr class='{$class}'>";
211
                $class = ('even' === $class) ? 'odd' : 'even';
212
                echo "<td class='center'>" . $protocol_id . '</td>';
213
                $subscrObj  = $helper->getHandler('Subscr')->get($protocolObj->getVar('protocol_subscriber_id'));
214
                $subscriber = $subscrObj ? $subscrObj->getVar('subscr_email') : _AM_XNEWSLETTER_PROTOCOL_NO_SUBSCREMAIL;
215
                if ('' == $subscriber) {
216
                    $subscriber = '-';
217
                }
218
                $success = (true === (bool)$protocolObj->getVar('protocol_success')) ? XNEWSLETTER_IMG_OK : XNEWSLETTER_IMG_FAILED;
219
                echo '<td>' . $subscriber . '</td>';
220
                echo '<td>' . $protocolObj->getVar('protocol_status') . '</td>';
221
                echo "<td class='center'>" . $success . '</td>';
222
                echo "<td class='center'>" . \XoopsUser::getUnameFromId($protocolObj->getVar('protocol_submitter'), 'S') . '</td>';
223
                echo "<td class='center'>" . formatTimestamp($protocolObj->getVar('protocol_created'), 'L') . '</td>';
224
225
                echo "
226
                    <td  class='center'>
227
                        <a href='?op=delete_protocol&protocol_id=" . $protocol_id . "'><img src=" . XNEWSLETTER_ICONS_URL . "/xn_delete.png alt='" . _DELETE . "' title='" . _DELETE . "'></a>
228
                    </td>";
229
                echo '</tr>';
230
            }
231
        }
232
        echo '</table>';
233
        echo '<br>';
234
        echo '<div>' . $pagenav . '</div>';
235
        echo '<br>';
236
        break;
237
    case 'new_protocol':
238
        $adminObject->displayNavigation($currentFile);
239
        $adminObject->addItemButton(_AM_XNEWSLETTER_PROTOCOLLIST, '?op=list', 'list');
240
        $adminObject->displayButton('left');
241
242
        $protocolObj = $helper->getHandler('Protocol')->create();
243
        $form        = $protocolObj->getForm();
244
        $form->display();
245
        break;
246
    case 'save_protocol':
247
        if (!$GLOBALS['xoopsSecurity']->check()) {
248
            redirect_header($currentFile, 3, implode(',', $GLOBALS['xoopsSecurity']->getErrors()));
249
        }
250 View Code Duplication
        if (\Xmf\Request::hasVar('protocol_id', 'REQUEST')) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
251
            $protocolObj = $helper->getHandler('Protocol')->get($_REQUEST['protocol_id']);
252
        } else {
253
            $protocolObj = $helper->getHandler('Protocol')->create();
254
        }
255
256
        $protocolObj->setVar('protocol_letter_id', $_REQUEST['protocol_letter_id']);
257
        $protocolObj->setVar('protocol_subscriber_id', $_REQUEST['protocol_subscriber_id']);
258
        $protocolObj->setVar('protocol_status', $_REQUEST['protocol_status']);
259
        $protocolObj->setVar('protocol_success', $_REQUEST['protocol_success']);
260
        $protocolObj->setVar('protocol_submitter', $_REQUEST['protocol_submitter']);
261
        $protocolObj->setVar('protocol_created', strtotime($_REQUEST['protocol_created']));
262
263
        if ($helper->getHandler('Protocol')->insert($protocolObj)) {
264
            redirect_header('?op=list', 3, _AM_XNEWSLETTER_FORMOK);
265
        }
266
267
        echo $protocolObj->getHtmlErrors();
268
        $form = $protocolObj->getForm();
269
        $form->display();
270
        break;
271 View Code Duplication
    case 'edit_protocol':
272
        $adminObject->displayNavigation($currentFile);
273
        $adminObject->addItemButton(_AM_XNEWSLETTER_NEWPROTOCOL, '?op=new_protocol', 'add');
274
        $adminObject->addItemButton(_AM_XNEWSLETTER_PROTOCOLLIST, '?op=list', 'list');
275
        $adminObject->displayButton('left');
276
277
        $protocolObj = $helper->getHandler('Protocol')->get($_REQUEST['protocol_id']);
278
        $form        = $protocolObj->getForm();
279
        $form->display();
280
        break;
281 View Code Duplication
    case 'delete_protocol':
282
        $protocolObj = $helper->getHandler('Protocol')->get($_REQUEST['protocol_id']);
283
        if (true === \Xmf\Request::getBool('ok', false, 'POST')) {
284
            if (!$GLOBALS['xoopsSecurity']->check()) {
285
                redirect_header($currentFile, 3, implode(',', $GLOBALS['xoopsSecurity']->getErrors()));
286
            }
287
            if ($helper->getHandler('Protocol')->delete($protocolObj)) {
288
                redirect_header($currentFile, 3, _AM_XNEWSLETTER_FORMDELOK);
289
            } else {
290
                echo $protocolObj->getHtmlErrors();
291
            }
292
        } else {
293
            xoops_confirm(['ok' => true, 'protocol_id' => $_REQUEST['protocol_id'], 'op' => 'delete_protocol'], $_SERVER['REQUEST_URI'], sprintf(_AM_XNEWSLETTER_FORMSUREDEL, $protocolObj->getVar('protocol_id')));
294
        }
295
        break;
296
    case 'delete_protocol_list':
297
        $letter_id = \Xmf\Request::getInt('letter_id', 0, 'REQUEST');
298
        if ($letter_id > 0) {
299
            $letterObj = $helper->getHandler('Letter')->get($_REQUEST['letter_id']);
300
            if (true === \Xmf\Request::getBool('ok', false, 'POST')) {
301
                if (!$GLOBALS['xoopsSecurity']->check()) {
302
                    redirect_header($currentFile, 3, implode(',', $GLOBALS['xoopsSecurity']->getErrors()));
303
                }
304
                $sql    = "DELETE FROM `{$xoopsDB->prefix('xnewsletter_protocol')}` WHERE `protocol_letter_id`={$letter_id}";
305
                $result = $xoopsDB->query($sql);
306
                if ($result) {
307
                    redirect_header($currentFile, 3, _AM_XNEWSLETTER_FORMDELOK);
308
                } else {
309
                    redirect_header($currentFile, 3, _AM_XNEWSLETTER_FORMDELNOTOK);
310
                }
311
            } else {
312
                xoops_confirm(['ok' => true, 'letter_id' => $letter_id, 'op' => 'delete_protocol_list'], $_SERVER['REQUEST_URI'], sprintf(_AM_XNEWSLETTER_FORMSUREDEL_LIST, $letterObj->getVar('letter_title')));
313
            }
314
        }
315
        break;
316
}
317
require_once __DIR__ . '/admin_footer.php';
318