Completed
Push — master ( 454ebd...de22fe )
by Michael
03:58
created

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