Passed
Push — master ( 2744eb...81ba93 )
by Michael
02:46
created

admin/question.php (5 issues)

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
/** @var Smartfaq\Helper $helper */
12
$helper = Smartfaq\Helper::getInstance();
13
14
require_once __DIR__ . '/admin_header.php';
15
16
global $xoopsUser;
17
18
    // Creating the faq handler object
19
/** @var Smartfaq\FaqHandler $faqHandler */
20
$faqHandler = Smartfaq\Helper::getInstance()->getHandler('Faq');
21
22
// Creating the category handler object
23
/** @var Smartfaq\CategoryHandler $categoryHandler */
24
$categoryHandler = Smartfaq\Helper::getInstance()->getHandler('Category');
25
26
$op = '';
27
if (isset($_GET['op'])) {
28
    $op = $_GET['op'];
29
}
30
if (isset($_POST['op'])) {
31
    $op = $_POST['op'];
32
}
33
34
// Where shall we start?
35
$startfaq = \Xmf\Request::getInt('startfaq', 0, 'GET');
0 ignored issues
show
The type Xmf\Request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
36
37
/**
38
 * @param bool $showmenu
39
 * @param int  $faqid
40
 */
41
function editfaq($showmenu = false, $faqid = -1)
42
{
43
    global $faqHandler, $categoryHandler, $xoopsUser, $xoopsConfig, $xoopsDB, $modify,  $xoopsModule, $XOOPS_URL, $myts;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
44
    /** @var Smartfaq\Helper $helper */
45
    $helper = Smartfaq\Helper::getInstance();
46
47
    require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
48
    // If there is a parameter, and the id exists, retrieve data: we're editing a faq
49
    if (-1 != $faqid) {
50
        // Creating the FAQ object
51
        $faqObj = new Smartfaq\Faq($faqid);
52
53
        if ($faqObj->notLoaded()) {
54
            redirect_header('faq.php', 1, _AM_SF_NOARTTOEDIT);
55
        }
56
        switch ($faqObj->status()) {
57
58
            case Constants::SF_STATUS_ASKED:
59
                $breadcrumb_action    = _AM_SF_APPROVING;
60
                $collapsableBar_title = _AM_SF_QUESTION_APPROVING;
61
                $collapsableBar_info  = _AM_SF_QUESTION_APPROVING_INFO;
62
                $button_caption       = _AM_SF_QUEUE;
63
                break;
64
65
            case 'default':
66
            default:
67
                $breadcrumb_action    = _AM_SF_EDITING;
68
                $collapsableBar_title = _AM_SF_EDITQUES;
69
                $collapsableBar_info  = _AM_SF_EDITING_INFO;
70
                $button_caption       = _AM_SF_MODIFY;
71
                break;
72
        }
73
74
        // Creating the category of this FAQ
75
        $categoryObj = $categoryHandler->get($faqObj->categoryid());
76
77
        echo "<br>\n";
78
        Smartfaq\Utility::collapsableBar('bottomtable', 'bottomtableicon');
79
        echo "<img id='bottomtableicon' src=" . XOOPS_URL . '/modules/' . $xoopsModule->dirname() . "/assets/images/icon/close12.gif alt=''></a>&nbsp;" . $collapsableBar_title . '</h3>';
80
        echo "<div id='bottomtable'>";
81
        echo '<span style="color: #567; margin: 3px 0 12px 0; font-size: small; display: block; ">' . $collapsableBar_info . '</span>';
82
    } else {
83
        // there's no parameter, so we're adding a faq
84
        $faqObj = $faqHandler->create();
85
        $faqObj->setVar('uid', $xoopsUser->getVar('uid'));
86
        $categoryObj = $categoryHandler->create();
87
88
        $breadcrumb_action = _AM_SF_CREATINGNEW;
89
        $button_caption    = _AM_SF_CREATE;
90
91
        Smartfaq\Utility::collapsableBar('bottomtable', 'bottomtableicon');
92
        echo "<img id='bottomtableicon' src=" . XOOPS_URL . '/modules/' . $xoopsModule->dirname() . "/assets/images/icon/close12.gif alt=''></a>&nbsp;" . _AM_SF_CREATEQUESTION . '</h3>';
93
        echo "<div id='bottomtable'>";
94
    }
95
    $sform = new \XoopsThemeForm(_AM_SF_OPEN_QUESTION, 'op', xoops_getenv('PHP_SELF'), 'post', true);
96
    $sform->setExtra('enctype="multipart/form-data"');
97
98
    // faq requester
99
    $sform->addElement(new \XoopsFormLabel(_AM_SF_REQUESTED_BY, Smartfaq\Utility::getLinkedUnameFromId($faqObj->uid(), $helper->getConfig('userealname'))));
100
101
    // CATEGORY
102
    /*
103
    * Get information for pulldown menu using XoopsTree.
104
    * First var is the database table
105
    * Second var is the unique field ID for the categories
106
    * Last one is not set as we do not have sub menus in Smartfaq
107
    */
108
109
    $mytree = new Smartfaq\Tree($xoopsDB->prefix('smartfaq_categories'), 'categoryid', 'parentid');
110
    ob_start();
111
    $mytree->makeMySelBox('name', 'weight', $categoryObj->categoryid());
112
    $sform->addElement(new \XoopsFormLabel(_AM_SF_CATEGORY_QUESTION, ob_get_contents()));
113
    ob_end_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
    $button_tray = new \XoopsFormElementTray('', '');
133
    $hidden      = new \XoopsFormHidden('op', 'addfaq');
134
    $button_tray->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
146
        // there's no faqid? Then it's a new faq
147
        // $button_tray -> addElement( new \XoopsFormButton( '', 'mod', _AM_SF_CREATE, 'submit' ) );
148
        $butt_create = new \XoopsFormButton('', '', _AM_SF_CREATE, 'submit');
149
        $butt_create->setExtra('onclick="this.form.elements.op.value=\'addfaq\'"');
150
        $button_tray->addElement($butt_create);
151
152
        $butt_clear = new \XoopsFormButton('', '', _AM_SF_CLEAR, 'reset');
153
        $button_tray->addElement($butt_clear);
154
155
        $butt_cancel = new \XoopsFormButton('', '', _AM_SF_CANCEL, 'button');
156
        $butt_cancel->setExtra('onclick="history.go(-1)"');
157
        $button_tray->addElement($butt_cancel);
158
    } else {
159
        // else, we're editing an existing faq
160
        // $button_tray -> addElement( new \XoopsFormButton( '', 'mod', _AM_SF_MODIFY, 'submit' ) );
161
        $butt_create = new \XoopsFormButton('', '', $button_caption, 'submit');
162
        $butt_create->setExtra('onclick="this.form.elements.op.value=\'addfaq\'"');
163
        $button_tray->addElement($butt_create);
164
165
        $butt_edit = new \XoopsFormButton('', '', _AM_SF_OPEN_QUESTION_EDIT, 'button');
166
        $butt_edit->setExtra("onclick=\"location='faq.php?op=mod&amp;faqid=" . $faqid . "'\"");
167
        $button_tray->addElement($butt_edit);
168
169
        $butt_cancel = new \XoopsFormButton('', '', _AM_SF_CANCEL, 'button');
170
        $butt_cancel->setExtra('onclick="history.go(-1)"');
171
        $button_tray->addElement($butt_cancel);
172
    }
173
174
    $sform->addElement($button_tray);
175
    $sform->display();
176
    echo '</div>';
177
    unset($hidden);
178
}
179
180
/* -- Available operations -- */
181
switch ($op) {
182
    case 'mod':
183
184
        global $xoopsConfig, $xoopsDB, $xoopsModule, $modify, $myts;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
185
        $faqid = isset($_GET['faqid']) ? $_GET['faqid'] : -1;
186
187
        if (-1 == $faqid) {
188
            $totalcategories = $categoryHandler->getCategoriesCount(-1);
189
            if (0 == $totalcategories) {
190
                redirect_header('category.php?op=mod', 3, _AM_SF_NEED_CATEGORY_QUESTION);
191
            }
192
        }
193
194
        $adminObject = \Xmf\Module\Admin::getInstance();
195
        xoops_cp_header();
196
197
        $adminObject->displayNavigation(basename(__FILE__));
198
        require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
199
200
        editfaq(true, $faqid);
201
        break;
202
203
    case 'addfaq':
204
        if (!$xoopsUser) {
205
            if (1 == $helper->getConfig('anonpost')) {
206
                $uid = 0;
207
            } else {
208
                redirect_header('index.php', 3, _NOPERM);
209
            }
210
        } else {
211
            $uid = $xoopsUser->uid();
212
        }
213
214
        $faqid = \Xmf\Request::getInt('faqid', -1, 'POST');
215
216
        // Creating the FAQ
217
        if (-1 != $faqid) {
218
            $faqObj = new Smartfaq\Faq($faqid);
219
        } else {
220
            $faqObj = $faqHandler->create();
221
        }
222
223
        // Putting the values in the FAQ object
224
        $faqObj->setGroups_read(isset($_POST['groups']) ? $_POST['groups'] : []);
225
        $faqObj->setVar('categoryid', isset($_POST['categoryid']) ? (int)$_POST['categoryid'] : 0);
226
        $faqObj->setVar('question', $_POST['question']);
227
        $faqObj->setVar('status', isset($_POST['status']) ? (int)$_POST['status'] : Constants::SF_STATUS_ASKED);
228
229
        $notifToDo = null;
230
231
        switch ($faqObj->status()) {
232
233
            case Constants::SF_STATUS_NOTSET:
234
                $redirect_msg = _AM_SF_QUESTIONCREATEDOK;
235
                // Setting the new status
236
                $status    = Constants::SF_STATUS_OPENED;
237
                $notifToDo = [Constants::SF_NOT_QUESTION_PUBLISHED];
238
                $faqObj->setVar('uid', $uid);
239
                break;
240
241
            case Constants::SF_STATUS_ASKED:
242
                $redirect_msg = _AM_SF_QUESTIONPUBLISHED;
243
                // Setting the new status
244
                $status    = Constants::SF_STATUS_OPENED;
245
                $notifToDo = [Constants::SF_NOT_QUESTION_PUBLISHED];
246
                break;
247
248
            case 'default':
249
            default:
250
                $redirect_msg = _AM_SF_QUESTIONMODIFIED;
251
                // Setting the new status
252
                $status = $faqObj->status();
253
                break;
254
        }
255
        $faqObj->setVar('status', $status);
256
257
        // Storing the FAQ
258
        if (!$faqObj->store()) {
259
            redirect_header('javascript:history.go(-1)', 3, _AM_SF_ERROR . Smartfaq\Utility::formatErrors($faqObj->getErrors()));
260
        }
261
262
        // Send notifications
263
        if (!empty($notifToDo)) {
264
            $faqObj->sendNotifications($notifToDo);
265
        }
266
267
        redirect_header('question.php', 2, $redirect_msg);
268
269
        break;
270
271
    case 'del':
272
        global  $xoopsConfig, $xoopsDB;
273
274
        $module_id    = $xoopsModule->getVar('mid');
275
        $gpermHandler = xoops_getHandler('groupperm');
276
277
        $faqid = \Xmf\Request::getInt('faqid', 0, 'POST');
278
        $faqid = \Xmf\Request::getInt('faqid', $faqid, 'GET');
279
280
        $faqObj = new Smartfaq\Faq($faqid);
281
282
        $confirm  = \Xmf\Request::getInt('confirm', 0, POST);
0 ignored issues
show
The constant POST was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
283
        $question = \Xmf\Request::getString('question', '', 'POST');
284
285
        if ($confirm) {
286
            if (!$faqHandler->delete($faqObj)) {
287
                redirect_header('question.php', 2, _AM_SF_FAQ_DELETE_ERROR);
288
            }
289
290
            redirect_header('question.php', 2, sprintf(_AM_SF_QUESTIONISDELETED, $faqObj->question()));
291
        } else {
292
            // no confirm: show deletion condition
293
            $faqid = \Xmf\Request::getInt('faqid', 0, 'GET');
294
            xoops_cp_header();
295
            xoops_confirm([
296
                              'op'      => 'del',
297
                              'faqid'   => $faqObj->faqid(),
298
                              'confirm' => 1,
299
                              'name'    => $faqObj->question()
300
                          ], 'question.php', _AM_SF_DELETETHISQUESTION . " <br>'" . $faqObj->question() . "'. <br> <br>", _AM_SF_DELETE);
301
            xoops_cp_footer();
302
        }
303
304
        exit();
305
        break;
306
307
    case 'default':
308
    default:
309
        $adminObject = \Xmf\Module\Admin::getInstance();
310
        xoops_cp_header();
311
        $adminObject->displayNavigation(basename(__FILE__));
312
313
        require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
314
        require_once XOOPS_ROOT_PATH . '/class/pagenav.php';
315
316
        global  $xoopsConfig, $xoopsDB,  $xoopsModule, $smartModuleConfig;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
317
318
        echo "<br>\n";
319
320
        Smartfaq\Utility::collapsableBar('toptable', 'toptableicon');
321
322
        echo "<img id='toptableicon' src=" . XOOPS_URL . '/modules/' . $xoopsModule->dirname() . "/assets/images/icon/close12.gif alt=''></a>&nbsp;" . _AM_SF_OPENED_TITLE . '</h3>';
323
        echo "<div id='toptable'>";
324
        echo '<span style="color: #567; margin: 3px 0 12px 0; font-size: small; display: block; ">' . _AM_SF_OPENED_DSC . '</span>';
325
326
        // Get the total number of published FAQs
327
        $totalfaqs = $faqHandler->getFaqsCount(-1, [Constants::SF_STATUS_OPENED]);
328
        // creating the FAQ objects that are published
329
        $faqsObj         = $faqHandler->getFaqs($helper->getConfig('perpage'), $startfaq, Constants::SF_STATUS_OPENED);
330
//        $totalFaqsOnPage = count($faqsObj);
331
        $allCats         = $categoryHandler->getObjects(null, true);
332
        echo "<table width='100%' cellspacing=1 cellpadding=3 border=0 class = outer>";
333
        echo '<tr>';
334
        echo "<th width='40' class='bg3' align='center'><b>" . _AM_SF_ARTID . '</b></td>';
335
        echo "<th width='20%' class='bg3' align='left'><b>" . _AM_SF_ARTCOLNAME . '</b></td>';
336
        echo "<th class='bg3' align='left'><b>" . _AM_SF_QUESTION . '</b></td>';
337
338
        echo "<th width='90' class='bg3' align='center'><b>" . _AM_SF_ASKED . '</b></td>';
339
340
        echo "<th width='90' class='bg3' align='center'><b>" . _AM_SF_CREATED . '</b></td>';
341
        echo "<th width='60' class='bg3' align='center'><b>" . _AM_SF_ACTION . '</b></td>';
342
        echo '</tr>';
343
        //var_dump( $faqsObj);
344
        if ($totalfaqs > 0) {
345
            global $pathIcon16;
346
            foreach (array_keys($faqsObj) as $i) {
347
                $categoryObj = $allCats[$faqsObj[$i]->categoryid()];
348
349
                $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>";
350
                $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>";
351
352
                $requester = Smartfaq\Utility::getLinkedUnameFromId($faqsObj[$i]->uid(), $smartModuleConfig['userealname']);
353
354
                echo '<tr>';
355
                echo "<td class='head' align='center'>" . $faqsObj[$i]->faqid() . '</td>';
356
                echo "<td class='even' align='left'>" . $categoryObj->name() . '</td>';
357
                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>';
358
359
                echo "<td class='even' align='center'>" . $requester . '</td>';
360
361
                echo "<td class='even' align='center'>" . $faqsObj[$i]->datesub('s') . '</td>';
362
                echo "<td class='even' align='center'> $modify $delete </td>";
363
                echo '</tr>';
364
            }
365
        } else {
366
            $faqid = -1;
367
            echo '<tr>';
368
            echo "<td class='head' align='center' colspan= '7'>" . _AM_SF_NOQUEUED . '</td>';
369
            echo '</tr>';
370
        }
371
        echo "</table>\n";
372
        echo "<br>\n";
373
374
        $pagenav = new \XoopsPageNav($totalfaqs, $helper->getConfig('perpage'), $startfaq, 'startfaq');
375
        echo '<div style="text-align:right;">' . $pagenav->renderNav() . '</div>';
376
        echo '</div>';
377
378
        $totalcategories = $categoryHandler->getCategoriesCount(-1);
379
        if ($totalcategories > 0) {
380
            editfaq();
381
        }
382
383
        break;
384
}
385
386
require_once __DIR__ . '/admin_footer.php';
387