Completed
Pull Request — master (#29)
by Goffy
01:40
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
// set template
35
$templateMain = 'xnewsletter_admin_protocols.tpl';
36
37
// We recovered the value of the argument op in the URL$
38
$op = Request::getString('op', 'list');
39
40
$GLOBALS['xoopsTpl']->assign('xnewsletter_url', XNEWSLETTER_URL);
41
$GLOBALS['xoopsTpl']->assign('xnewsletter_icons_url', XNEWSLETTER_ICONS_URL);
42
43
switch ($op) {
44
    case 'list':
45
    case 'list_protocols':
46
        $GLOBALS['xoopsTpl']->assign('list_protocols', true);
47
        $adminObject->displayNavigation($currentFile);
48
        
49
        $limit = $helper->getConfig('adminperpage');
50
        $start = Request::getInt('start', 0);
51
52
        //first show misc protocol items
53
        $protocolCriteria = new \CriteriaCompo();
54
        $protocolCriteria->add(new \Criteria('protocol_letter_id', '0'));
55
        $protocolCriteria->setSort('protocol_id');
56
        $protocolCriteria->setOrder('DESC');
57
        $protocolCount = $helper->getHandler('Protocol')->getCount($protocolCriteria);
58
        $protocolCriteria->setLimit(2);
59
        $protocolsAll               = $helper->getHandler('Protocol')->getAll($protocolCriteria);
60
        $protocol_status            = '';
61
        $protocol_created           = '';
62
        $protocol_created_formatted = '';
63
        $p                          = 0;
64 View Code Duplication
        foreach ($protocolsAll as $id => $protocolObj) {
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...
65
            ++$p;
66
            if (count($protocolsAll) > 1) {
67
                $protocol_status .= "($p) ";
68
            }
69
            $protocol_status            .= $protocolObj->getVar('protocol_status') . '<br>';
70
            $protocol_created_formatted .= formatTimestamp($protocolObj->getVar('protocol_created'), 'M') . '<br>';
71
        }
72
        if ($protocolCount > 2) {
73
            $protocol_status .= '...';
74
        }
75
        $GLOBALS['xoopsTpl']->assign('protocol_status', $protocol_status);
76
        $GLOBALS['xoopsTpl']->assign('protocol_created_formatted', $protocol_created_formatted);
77
78
//        letter details
79
80
        $sql = 'SELECT protocol_letter_id FROM ' . $xoopsDB->prefix('xnewsletter_protocol') . ' GROUP BY protocol_letter_id';
81
        $prot_letters = $xoopsDB->query($sql);
82
        $protocol_letters_total = $prot_letters->num_rows;
83
84
        $sql = 'SELECT protocol_letter_id FROM ' . $xoopsDB->prefix('xnewsletter_protocol') . ' GROUP BY protocol_letter_id LIMIT ' . $start.', ' . $limit;
85
        $prot_letters = $xoopsDB->query($sql);
86
87
        while (false !== ($prot_letter = $xoopsDB->fetchArray($prot_letters))) {
88
            $protocol_letter_id = $prot_letter['protocol_letter_id'];
89
            $letterCriteria = new \CriteriaCompo();
90
            $letterCriteria->add(new \Criteria('letter_id', $protocol_letter_id));
91
            $letterCount = $helper->getHandler('Letter')->getCount();
92
            $letterObjs = $helper->getHandler('Letter')->getAll($letterCriteria);
93
94
            if ($letterCount > 0) {
95
                $GLOBALS['xoopsTpl']->assign('letters_count', $letterCount);
96
                foreach (array_keys($letterObjs) as $i) {
97
                    $protocolCriteria = new \CriteriaCompo();
98
                    $protocolCriteria->add(new \Criteria('protocol_letter_id', $letterObjs[$i]->getVar('letter_id')));
99
                    $protocolCriteria->setSort('protocol_id');
100
                    $protocolCriteria->setOrder('DESC');
101
                    $protocolCount = $helper->getHandler('Protocol')->getCount($protocolCriteria);
102
                    if ($protocolCount > 0) {
103
                        $protocolCriteria->setLimit(2);
104
                        $protocolsAll     = $helper->getHandler('Protocol')->getAll($protocolCriteria);
105
                        $protocol_status  = '';
106
                        $protocol_created = '';
107
108
                        $protocol_item['letter_title'] = $letterObjs[$i]->getVar('letter_title');
109
110
                        $p = 0;
111 View Code Duplication
                        foreach ($protocolsAll as $protocol) {
112
                            ++$p;
113
                            if (count($protocolsAll) > 1) {
114
                                $protocol_status .= "($p) ";
115
                            }
116
                            $protocol_status  .= $protocol->getVar('protocol_status') . '<br>';
117
                            $protocol_created .= formatTimestamp($protocol->getVar('protocol_created'), 'M') . '<br>';
118
                        }
119
                        if ($protocolCount > 2) {
120
                            $protocol_status .= '...';
121
                        }
122
                        $protocol_item['letter_id'] = $letterObjs[$i]->getVar('letter_id');
123
                        $protocol_item['status'] = $protocol_status;
124
                        $protocol_item['created'] = $protocol_created;
125
126
                        $GLOBALS['xoopsTpl']->append('protocols_list', $protocol_item);
127
                        unset($protocol);
128
                    }
129
                }
130
            }
131
        }
132 View Code Duplication
        if ($protocol_letters_total > $limit) {
133
            require_once XOOPS_ROOT_PATH . '/class/pagenav.php';
134
            $pagenav = new \XoopsPageNav($protocol_letters_total, $limit, $start, 'start', 'op=list');
135
            $GLOBALS['xoopsTpl']->assign('pagenav', $pagenav->renderNav(4));
136
        }
137
        break;
138
    case 'list_letter':
139
        $GLOBALS['xoopsTpl']->assign('list_letter', true);
140
        $letter_id = isset($_REQUEST['letter_id']) ? $_REQUEST['letter_id'] : '0';
141
        $adminObject->displayNavigation($currentFile);
142
        $adminObject->addItemButton(_AM_XNEWSLETTER_PROTOCOLLIST, '?op=list', 'list');
143
144
        if ($letter_id > '0') {
145
            $adminObject->addItemButton(_AM_XNEWSLETTER_LETTER_DELETE_ALL, '?op=delete_protocol_list&letter_id=' . $letter_id, 'delete');
146
        }
147
        $GLOBALS['xoopsTpl']->assign('buttons', $adminObject->renderButton('left'));
148
        $limit = $helper->getConfig('adminperpage');
149
150
        $protocolCriteria = new \CriteriaCompo();
151
        $protocolCriteria->add(new \Criteria('protocol_letter_id', $letter_id));
152
        $protocolCriteria->setSort('protocol_id');
153
        $protocolCriteria->setOrder('DESC');
154
        $protocolCount = $helper->getHandler('Protocol')->getCount($protocolCriteria);
155
        $start         = Request::getInt('start', 0);
156
        $protocolCriteria->setStart($start);
157
        $protocolCriteria->setLimit($limit);
158
        $protocolsAll = $helper->getHandler('Protocol')->getAll($protocolCriteria);
159 View Code Duplication
        if ($protocolCount > $limit) {
160
            require_once XOOPS_ROOT_PATH . '/class/pagenav.php';
161
            $pagenav = new \XoopsPageNav($protocolCount, $limit, $start, 'start', 'op=list_letter&letter_id=' . $letter_id);
162
            $GLOBALS['xoopsTpl']->assign('pagenav', $pagenav->renderNav(4));
163
        }
164
165
        // View Table
166
        $letterObj = $helper->getHandler('Letter')->get($letter_id);
167
        $GLOBALS['xoopsTpl']->assign('letter_title', $letterObj->getVar('letter_title'));
168
        if ($protocolCount > 0) {
169
            $GLOBALS['xoopsTpl']->assign('protocols_count', $protocolCount);
170
            $class = 'odd';
171
            foreach ($protocolsAll as $id => $protocolObj) {
172
                $protocol = $protocolObj->getValuesProtocol();
173
                $subscrObj  = $helper->getHandler('Subscr')->get($protocolObj->getVar('protocol_subscriber_id'));
174
                $subscriber = $subscrObj ? $subscrObj->getVar('subscr_email') : _AM_XNEWSLETTER_PROTOCOL_NO_SUBSCREMAIL;
175
                if ('' == $subscriber) {
176
                    $subscriber = '-';
177
                }
178
                $protocol['subscriber'] = $subscriber;
179
                $success_text = (true === (bool)$protocolObj->getVar('protocol_success')) ? XNEWSLETTER_IMG_OK : XNEWSLETTER_IMG_FAILED;
180
                $protocol['success_text'] = $success_text;
181
                $GLOBALS['xoopsTpl']->append('protocols_list2', $protocol);
182
                unset($protocol);
183
            }
184
        }
185
        break;
186
    case 'new_protocol':
187
        $adminObject->displayNavigation($currentFile);
188
        $adminObject->addItemButton(_AM_XNEWSLETTER_PROTOCOLLIST, '?op=list', 'list');
189
        $GLOBALS['xoopsTpl']->assign('buttons', $adminObject->renderButton('left'));
190
191
        $protocolObj = $helper->getHandler('Protocol')->create();
192
        $form        = $protocolObj->getForm();
193
        $GLOBALS['xoopsTpl']->assign('form', $form->render());
194
        break;
195
    case 'save_protocol':
196
        if (!$GLOBALS['xoopsSecurity']->check()) {
197
            redirect_header($currentFile, 3, implode(',', $GLOBALS['xoopsSecurity']->getErrors()));
198
        }
199 View Code Duplication
        if (Request::hasVar('protocol_id', 'REQUEST')) {
200
            $protocolObj = $helper->getHandler('Protocol')->get(Request::getInt('protocol_id', 0));
201
        } else {
202
            $protocolObj = $helper->getHandler('Protocol')->create();
203
        }
204
205
        $protocolObj->setVar('protocol_letter_id',     Request::getInt('protocol_letter_id', 0));
206
        $protocolObj->setVar('protocol_subscriber_id', Request::getInt('protocol_subscriber_id', 0));
207
        $protocolObj->setVar('protocol_status',        Request::getString('protocol_status', ''));
208
        $protocolObj->setVar('protocol_success',       Request::getString('protocol_success', ''));
209
        $protocolObj->setVar('protocol_submitter',     Request::getInt('protocol_submitter', 0));
210
        $protocolObj->setVar('protocol_created', strtotime(Request::getInt('protocol_created', 0)));
211
212
        if ($helper->getHandler('Protocol')->insert($protocolObj)) {
213
            redirect_header('?op=list', 3, _AM_XNEWSLETTER_FORMOK);
214
        }
215
216
        $GLOBALS['xoopsTpl']->assign('error', $protocolObj->getHtmlErrors());
217
        $form = $protocolObj->getForm();
218
        $GLOBALS['xoopsTpl']->assign('form', $form->render());
219
        break;
220 View Code Duplication
    case 'edit_protocol':
221
        $adminObject->displayNavigation($currentFile);
222
        $adminObject->addItemButton(_AM_XNEWSLETTER_NEWPROTOCOL, '?op=new_protocol', 'add');
223
        $adminObject->addItemButton(_AM_XNEWSLETTER_PROTOCOLLIST, '?op=list', 'list');
224
        $GLOBALS['xoopsTpl']->assign('buttons', $adminObject->renderButton('left'));
225
226
        $protocolObj = $helper->getHandler('Protocol')->get(Request::getInt('protocol_id', 0));
227
        $form        = $protocolObj->getForm();
228
        $GLOBALS['xoopsTpl']->assign('form', $form->render());
229
        break;
230 View Code Duplication
    case 'delete_protocol':
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...
231
        $protocolId = Request::getInt('protocol_id', 0);
232
        $protocolObj = $helper->getHandler('Protocol')->get($protocolId);
233
        if (true === Request::getBool('ok', false, 'POST')) {
234
            if (!$GLOBALS['xoopsSecurity']->check()) {
235
                redirect_header($currentFile, 3, implode(',', $GLOBALS['xoopsSecurity']->getErrors()));
236
            }
237
            if ($helper->getHandler('Protocol')->delete($protocolObj)) {
238
                redirect_header($currentFile, 3, _AM_XNEWSLETTER_FORMDELOK);
239
            } else {
240
                $GLOBALS['xoopsTpl']->assign('error', $protocolObj->getHtmlErrors());
241
            }
242
        } else {
243
            xoops_confirm(['ok' => true, 'protocol_id' => $protocolId, 'op' => 'delete_protocol'], $_SERVER['REQUEST_URI'], sprintf(_AM_XNEWSLETTER_FORMSUREDEL, $protocolId));
244
        }
245
        break;
246
    case 'delete_protocol_list':
247
        $letter_id = Request::getInt('letter_id', 0, 'REQUEST');
248
        if ($letter_id > 0) {
249
            $letterObj = $helper->getHandler('Letter')->get($letter_id);
250
            if (true === Request::getBool('ok', false, 'POST')) {
251
                if (!$GLOBALS['xoopsSecurity']->check()) {
252
                    redirect_header($currentFile, 3, implode(',', $GLOBALS['xoopsSecurity']->getErrors()));
253
                }
254
                $sql    = "DELETE FROM `{$xoopsDB->prefix('xnewsletter_protocol')}` WHERE `protocol_letter_id`={$letter_id}";
255
                $result = $xoopsDB->query($sql);
256
                if ($result) {
257
                    redirect_header($currentFile, 3, _AM_XNEWSLETTER_FORMDELOK);
258
                } else {
259
                    redirect_header($currentFile, 3, _AM_XNEWSLETTER_FORMDELNOTOK);
260
                }
261
            } else {
262
                xoops_confirm(['ok' => true, 'letter_id' => $letter_id, 'op' => 'delete_protocol_list'], $_SERVER['REQUEST_URI'], sprintf(_AM_XNEWSLETTER_FORMSUREDEL_LIST, $letterObj->getVar('letter_title')));
263
            }
264
        }
265
        break;
266
}
267
require_once __DIR__ . '/admin_footer.php';
268