Passed
Push — master ( 81ba93...c6c854 )
by Michael
03:30
created

admin/question.php (1 issue)

Labels
Severity
1
<?php
2
3
/**
4
 * Module: SmartFAQ
5
 * Author: The SmartFactory <www.smartfactory.ca>
6
 * Licence: GNU
7
 */
8
9
use XoopsModules\Smartfaq;
10
use XoopsModules\Smartfaq\Constants;
11
12
require_once __DIR__ . '/admin_header.php';
13
14
/** @var Smartfaq\Helper $helper */
15
$helper = Smartfaq\Helper::getInstance();
16
$smartModuleConfig = $helper->getConfig();
17
18
19
global $xoopsUser;
20
21
// Creating the faq handler object
22
/** @var Smartfaq\FaqHandler $faqHandler */
23
$faqHandler = Smartfaq\Helper::getInstance()->getHandler('Faq');
24
25
// Creating the category handler object
26
/** @var Smartfaq\CategoryHandler $categoryHandler */
27
$categoryHandler = Smartfaq\Helper::getInstance()->getHandler('Category');
28
29
$op = '';
30
if (\Xmf\Request::hasVar('op', 'GET')) {
31
    $op = $_GET['op'];
32
}
33
if (\Xmf\Request::hasVar('op', 'POST')) {
34
    $op = $_POST['op'];
35
}
36
37
// Where shall we start?
38
$startfaq = \Xmf\Request::getInt('startfaq', 0, 'GET');
39
40
/**
41
 * @param bool $showmenu
42
 * @param int  $faqid
43
 */
44
function editfaq($showmenu = false, $faqid = -1)
45
{
46
    global $faqHandler, $categoryHandler, $xoopsUser, $xoopsConfig, $xoopsDB, $modify, $xoopsModule, $XOOPS_URL, $myts;
47
    /** @var Smartfaq\Helper $helper */
48
    $helper = Smartfaq\Helper::getInstance();
49
50
    require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
51
    // If there is a parameter, and the id exists, retrieve data: we're editing a faq
52
    if (-1 != $faqid) {
53
        // Creating the FAQ object
54
        $faqObj = new Smartfaq\Faq($faqid);
0 ignored issues
show
It seems like $faqid can also be of type integer; however, parameter $id of XoopsModules\Smartfaq\Faq::__construct() does only seem to accept null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

54
        $faqObj = new Smartfaq\Faq(/** @scrutinizer ignore-type */ $faqid);
Loading history...
55
56
        if ($faqObj->notLoaded()) {
57
            redirect_header('faq.php', 1, _AM_SF_NOARTTOEDIT);
58
        }
59
        switch ($faqObj->status()) {
60
            case Constants::SF_STATUS_ASKED:
61
                $breadcrumb_action    = _AM_SF_APPROVING;
62
                $collapsableBar_title = _AM_SF_QUESTION_APPROVING;
63
                $collapsableBar_info  = _AM_SF_QUESTION_APPROVING_INFO;
64
                $button_caption       = _AM_SF_QUEUE;
65
                break;
66
            case 'default':
67
            default:
68
                $breadcrumb_action    = _AM_SF_EDITING;
69
                $collapsableBar_title = _AM_SF_EDITQUES;
70
                $collapsableBar_info  = _AM_SF_EDITING_INFO;
71
                $button_caption       = _AM_SF_MODIFY;
72
                break;
73
        }
74
75
        // Creating the category of this FAQ
76
        $categoryObj = $categoryHandler->get($faqObj->categoryid());
77
78
        echo "<br>\n";
79
        Smartfaq\Utility::collapsableBar('bottomtable', 'bottomtableicon');
80
        echo "<img id='bottomtableicon' src=" . XOOPS_URL . '/modules/' . $xoopsModule->dirname() . "/assets/images/icon/close12.gif alt=''></a>&nbsp;" . $collapsableBar_title . '</h3>';
81
        echo "<div id='bottomtable'>";
82
        echo '<span style="color: #567; margin: 3px 0 12px 0; font-size: small; display: block; ">' . $collapsableBar_info . '</span>';
83
    } else {
84
        // there's no parameter, so we're adding a faq
85
        $faqObj = $faqHandler->create();
86
        $faqObj->setVar('uid', $xoopsUser->getVar('uid'));
87
        $categoryObj = $categoryHandler->create();
88
89
        $breadcrumb_action = _AM_SF_CREATINGNEW;
90
        $button_caption    = _AM_SF_CREATE;
91
92
        Smartfaq\Utility::collapsableBar('bottomtable', 'bottomtableicon');
93
        echo "<img id='bottomtableicon' src=" . XOOPS_URL . '/modules/' . $xoopsModule->dirname() . "/assets/images/icon/close12.gif alt=''></a>&nbsp;" . _AM_SF_CREATEQUESTION . '</h3>';
94
        echo "<div id='bottomtable'>";
95
    }
96
    $sform = new \XoopsThemeForm(_AM_SF_OPEN_QUESTION, 'op', xoops_getenv('PHP_SELF'), 'post', true);
97
    $sform->setExtra('enctype="multipart/form-data"');
98
99
    // faq requester
100
    $sform->addElement(new \XoopsFormLabel(_AM_SF_REQUESTED_BY, Smartfaq\Utility::getLinkedUnameFromId($faqObj->uid(), $helper->getConfig('userealname'))));
101
102
    // CATEGORY
103
    /*
104
    * Get information for pulldown menu using XoopsTree.
105
    * First var is the database table
106
    * Second var is the unique field ID for the categories
107
    * Last one is not set as we do not have sub menus in Smartfaq
108
    */
109
110
    $mytree = new Smartfaq\Tree($xoopsDB->prefix('smartfaq_categories'), 'categoryid', 'parentid');
111
    ob_start();
112
    $mytree->makeMySelBox('name', 'weight', $categoryObj->categoryid());
113
    $sform->addElement(new \XoopsFormLabel(_AM_SF_CATEGORY_QUESTION, ob_get_clean()));
114
115
    // faq QUESTION
116
    $sform->addElement(new \XoopsFormTextArea(_AM_SF_QUESTION, 'question', $faqObj->question(), 7, 60));
117
118
    // PER ITEM PERMISSIONS
119
    $memberHandler   = xoops_getHandler('member');
120
    $group_list      = $memberHandler->getGroupList();
121
    $groups_checkbox = new \XoopsFormCheckBox(_AM_SF_PERMISSIONS_QUESTION, 'groups[]', $faqObj->getGroups_read());
122
    foreach ($group_list as $group_id => $group_name) {
123
        if (XOOPS_GROUP_ADMIN != $group_id) {
124
            $groups_checkbox->addOption($group_id, $group_name);
125
        }
126
    }
127
    $sform->addElement($groups_checkbox);
128
129
    // faq ID
130
    $sform->addElement(new \XoopsFormHidden('faqid', $faqObj->faqid()));
131
132
    $buttonTray = new \XoopsFormElementTray('', '');
133
    $hidden     = new \XoopsFormHidden('op', 'addfaq');
134
    $buttonTray->addElement($hidden);
135
136
    $sform->addElement(new \XoopsFormHidden('status', $faqObj->status()));
137
    // Setting the FAQ Status
138
    /*  $status_select = new \XoopsFormSelect('', 'status', $status);
139
    $status_select->addOptionArray(Smartfaq\Utility::getStatusArray());
140
    $status_tray = new \XoopsFormElementTray(_AM_SF_STATUS_EXP , '&nbsp;');
141
    $status_tray->addElement($status_select);
142
    $sform->addElement($status_tray);
143
    */
144
    if (-1 == $faqid) {
145
        // there's no faqid? Then it's a new faq
146
        // $buttonTray -> addElement( new \XoopsFormButton( '', 'mod', _AM_SF_CREATE, 'submit' ) );
147
        $butt_create = new \XoopsFormButton('', '', _AM_SF_CREATE, 'submit');
148
        $butt_create->setExtra('onclick="this.form.elements.op.value=\'addfaq\'"');
149
        $buttonTray->addElement($butt_create);
150
151
        $butt_clear = new \XoopsFormButton('', '', _AM_SF_CLEAR, 'reset');
152
        $buttonTray->addElement($butt_clear);
153
154
        $butt_cancel = new \XoopsFormButton('', '', _AM_SF_CANCEL, 'button');
155
        $butt_cancel->setExtra('onclick="history.go(-1)"');
156
        $buttonTray->addElement($butt_cancel);
157
    } else {
158
        // else, we're editing an existing faq
159
        // $buttonTray -> addElement( new \XoopsFormButton( '', 'mod', _AM_SF_MODIFY, 'submit' ) );
160
        $butt_create = new \XoopsFormButton('', '', $button_caption, 'submit');
161
        $butt_create->setExtra('onclick="this.form.elements.op.value=\'addfaq\'"');
162
        $buttonTray->addElement($butt_create);
163
164
        $butt_edit = new \XoopsFormButton('', '', _AM_SF_OPEN_QUESTION_EDIT, 'button');
165
        $butt_edit->setExtra("onclick=\"location='faq.php?op=mod&amp;faqid=" . $faqid . "'\"");
166
        $buttonTray->addElement($butt_edit);
167
168
        $butt_cancel = new \XoopsFormButton('', '', _AM_SF_CANCEL, 'button');
169
        $butt_cancel->setExtra('onclick="history.go(-1)"');
170
        $buttonTray->addElement($butt_cancel);
171
    }
172
173
    $sform->addElement($buttonTray);
174
    $sform->display();
175
    echo '</div>';
176
    unset($hidden);
177
}
178
179
/* -- Available operations -- */
180
switch ($op) {
181
    case 'mod':
182
183
        global $xoopsConfig, $xoopsDB, $xoopsModule, $modify, $myts;
184
        $faqid = isset($_GET['faqid']) ? $_GET['faqid'] : -1;
185
186
        if (-1 == $faqid) {
187
            $totalcategories = $categoryHandler->getCategoriesCount(-1);
188
            if (0 == $totalcategories) {
189
                redirect_header('category.php?op=mod', 3, _AM_SF_NEED_CATEGORY_QUESTION);
190
            }
191
        }
192
193
        $adminObject = \Xmf\Module\Admin::getInstance();
194
        xoops_cp_header();
195
196
        $adminObject->displayNavigation(basename(__FILE__));
197
        require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
198
199
        editfaq(true, $faqid);
200
        break;
201
    case 'addfaq':
202
        if (!$xoopsUser) {
203
            if (1 == $helper->getConfig('anonpost')) {
204
                $uid = 0;
205
            } else {
206
                redirect_header('index.php', 3, _NOPERM);
207
            }
208
        } else {
209
            $uid = $xoopsUser->uid();
210
        }
211
212
        $faqid = \Xmf\Request::getInt('faqid', -1, 'POST');
213
214
        // Creating the FAQ
215
        if (-1 != $faqid) {
216
            $faqObj = new Smartfaq\Faq($faqid);
217
        } else {
218
            $faqObj = $faqHandler->create();
219
        }
220
221
        // Putting the values in the FAQ object
222
        $faqObj->setGroups_read(isset($_POST['groups']) ? $_POST['groups'] : []);
223
        $faqObj->setVar('categoryid', \Xmf\Request::getInt('categoryid', 0, 'POST'));
224
        $faqObj->setVar('question', $_POST['question']);
225
        $faqObj->setVar('status', \Xmf\Request::getInt('status', Constants::SF_STATUS_ASKED, 'POST'));
226
227
        $notifToDo = null;
228
229
        switch ($faqObj->status()) {
230
            case Constants::SF_STATUS_NOTSET:
231
                $redirect_msg = _AM_SF_QUESTIONCREATEDOK;
232
                // Setting the new status
233
                $status    = Constants::SF_STATUS_OPENED;
234
                $notifToDo = [Constants::SF_NOT_QUESTION_PUBLISHED];
235
                $faqObj->setVar('uid', $uid);
236
                break;
237
            case Constants::SF_STATUS_ASKED:
238
                $redirect_msg = _AM_SF_QUESTIONPUBLISHED;
239
                // Setting the new status
240
                $status    = Constants::SF_STATUS_OPENED;
241
                $notifToDo = [Constants::SF_NOT_QUESTION_PUBLISHED];
242
                break;
243
            case 'default':
244
            default:
245
                $redirect_msg = _AM_SF_QUESTIONMODIFIED;
246
                // Setting the new status
247
                $status = $faqObj->status();
248
                break;
249
        }
250
        $faqObj->setVar('status', $status);
251
252
        // Storing the FAQ
253
        if (!$faqObj->store()) {
254
            redirect_header('<script>javascript:history.go(-1)</script>', 3, _AM_SF_ERROR . Smartfaq\Utility::formatErrors($faqObj->getErrors()));
255
        }
256
257
        // Send notifications
258
        if (!empty($notifToDo)) {
259
            $faqObj->sendNotifications($notifToDo);
260
        }
261
262
        redirect_header('question.php', 2, $redirect_msg);
263
264
        break;
265
    case 'del':
266
        global $xoopsConfig, $xoopsDB;
267
268
        $module_id        = $xoopsModule->getVar('mid');
269
        $grouppermHandler = xoops_getHandler('groupperm');
270
271
        $faqid = \Xmf\Request::getInt('faqid', 0, 'POST');
272
        $faqid = \Xmf\Request::getInt('faqid', $faqid, 'GET');
273
274
        $faqObj = new Smartfaq\Faq($faqid);
275
276
        $confirm  = \Xmf\Request::getInt('confirm', 0, 'POST');
277
        $question = \Xmf\Request::getString('question', '', 'POST');
278
279
        if ($confirm) {
280
            if (!$faqHandler->delete($faqObj)) {
281
                redirect_header('question.php', 2, _AM_SF_FAQ_DELETE_ERROR);
282
            }
283
284
            redirect_header('question.php', 2, sprintf(_AM_SF_QUESTIONISDELETED, $faqObj->question()));
285
        } else {
286
            // no confirm: show deletion condition
287
            $faqid = \Xmf\Request::getInt('faqid', 0, 'GET');
288
            xoops_cp_header();
289
            xoops_confirm([
290
                              'op'      => 'del',
291
                              'faqid'   => $faqObj->faqid(),
292
                              'confirm' => 1,
293
                              'name'    => $faqObj->question(),
294
                          ], 'question.php', _AM_SF_DELETETHISQUESTION . " <br>'" . $faqObj->question() . "'. <br> <br>", _AM_SF_DELETE);
295
            xoops_cp_footer();
296
        }
297
298
        exit();
299
        break;
300
    case 'default':
301
    default:
302
        $adminObject = \Xmf\Module\Admin::getInstance();
303
        xoops_cp_header();
304
        $adminObject->displayNavigation(basename(__FILE__));
305
306
        require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
307
        require_once XOOPS_ROOT_PATH . '/class/pagenav.php';
308
309
        global $xoopsConfig, $xoopsDB, $xoopsModule;
310
311
        echo "<br>\n";
312
313
        Smartfaq\Utility::collapsableBar('toptable', 'toptableicon');
314
315
        echo "<img id='toptableicon' src=" . XOOPS_URL . '/modules/' . $xoopsModule->dirname() . "/assets/images/icon/close12.gif alt=''></a>&nbsp;" . _AM_SF_OPENED_TITLE . '</h3>';
316
        echo "<div id='toptable'>";
317
        echo '<span style="color: #567; margin: 3px 0 12px 0; font-size: small; display: block; ">' . _AM_SF_OPENED_DSC . '</span>';
318
319
        // Get the total number of published FAQs
320
        $totalfaqs = $faqHandler->getFaqsCount(-1, [Constants::SF_STATUS_OPENED]);
321
        // creating the FAQ objects that are published
322
        $faqsObj = $faqHandler->getFaqs($helper->getConfig('perpage'), $startfaq, Constants::SF_STATUS_OPENED);
323
        //        $totalFaqsOnPage = count($faqsObj);
324
        $allCats = $categoryHandler->getObjects(null, true);
325
        echo "<table width='100%' cellspacing=1 cellpadding=3 border=0 class = outer>";
326
        echo '<tr>';
327
        echo "<th width='40' class='bg3' align='center'><b>" . _AM_SF_ARTID . '</b></td>';
328
        echo "<th width='20%' class='bg3' align='left'><b>" . _AM_SF_ARTCOLNAME . '</b></td>';
329
        echo "<th class='bg3' align='left'><b>" . _AM_SF_QUESTION . '</b></td>';
330
331
        echo "<th width='90' class='bg3' align='center'><b>" . _AM_SF_ASKED . '</b></td>';
332
333
        echo "<th width='90' class='bg3' align='center'><b>" . _AM_SF_CREATED . '</b></td>';
334
        echo "<th width='60' class='bg3' align='center'><b>" . _AM_SF_ACTION . '</b></td>';
335
        echo '</tr>';
336
        //var_dump( $faqsObj);
337
        if ($totalfaqs > 0) {
338
            global $pathIcon16;
339
            foreach (array_keys($faqsObj) as $i) {
340
                $categoryObj = $allCats[$faqsObj[$i]->categoryid()];
341
342
                $modify = "<a href='question.php?op=mod&amp;faqid=" . $faqsObj[$i]->faqid() . "'><img src='" . $pathIcon16 . '/edit.png' . "' title='" . _AM_SF_EDITART . "' alt='" . _AM_SF_EDITART . "'></a>";
343
                $delete = "<a href='question.php?op=del&amp;faqid=" . $faqsObj[$i]->faqid() . "'><img src='" . $pathIcon16 . '/delete.png' . "' title='" . _AM_SF_DELETEART . "' alt='" . _AM_SF_DELETEART . "'></a>";
344
345
                $requester = Smartfaq\Utility::getLinkedUnameFromId($faqsObj[$i]->uid(), $smartModuleConfig['userealname']);
346
347
                echo '<tr>';
348
                echo "<td class='head' align='center'>" . $faqsObj[$i]->faqid() . '</td>';
349
                echo "<td class='even' align='left'>" . $categoryObj->name() . '</td>';
350
                echo "<td class='even' align='left'><a href='" . XOOPS_URL . '/modules/' . $xoopsModule->dirname() . '/answer.php?faqid=' . $faqsObj[$i]->faqid() . "'>" . $faqsObj[$i]->question(100) . '</a></td>';
351
352
                echo "<td class='even' align='center'>" . $requester . '</td>';
353
354
                echo "<td class='even' align='center'>" . $faqsObj[$i]->datesub('s') . '</td>';
355
                echo "<td class='even' align='center'> $modify $delete </td>";
356
                echo '</tr>';
357
            }
358
        } else {
359
            $faqid = -1;
360
            echo '<tr>';
361
            echo "<td class='head' align='center' colspan= '7'>" . _AM_SF_NOQUEUED . '</td>';
362
            echo '</tr>';
363
        }
364
        echo "</table>\n";
365
        echo "<br>\n";
366
367
        $pagenav = new \XoopsPageNav($totalfaqs, $helper->getConfig('perpage'), $startfaq, 'startfaq');
368
        echo '<div style="text-align:right;">' . $pagenav->renderNav() . '</div>';
369
        echo '</div>';
370
371
        $totalcategories = $categoryHandler->getCategoriesCount(-1);
372
        if ($totalcategories > 0) {
373
            editfaq();
374
        }
375
376
        break;
377
}
378
379
require_once __DIR__ . '/admin_footer.php';
380