Completed
Branch master (1b2f30)
by Michael
06:29 queued 03:22
created

admin/question.php (10 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
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 32 and the first side effect is on line 9.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
/**
4
 * Module: SmartFAQ
5
 * Author: The SmartFactory <www.smartfactory.ca>
6
 * Licence: GNU
7
 */
8
9
include_once __DIR__ . '/admin_header.php';
10
11
// Creating the faq handler object
12
$faqHandler = sf_gethandler('faq');
13
14
// Creating the category handler object
15
$categoryHandler = sf_gethandler('category');
16
17
$op = '';
18
if (isset($_GET['op'])) {
19
    $op = $_GET['op'];
20
}
21
if (isset($_POST['op'])) {
22
    $op = $_POST['op'];
23
}
24
25
// Where shall we start?
26
$startfaq = isset($_GET['startfaq']) ? (int)$_GET['startfaq'] : 0;
27
28
/**
29
 * @param bool $showmenu
30
 * @param int  $faqid
0 ignored issues
show
Should the type for parameter $faqid not be string?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
31
 */
32
function editfaq($showmenu = false, $faqid = -1)
0 ignored issues
show
The function editfaq() has been defined more than once; this definition is ignored, only the first definition in admin/answer.php (L27-130) is considered.

This check looks for functions that have already been defined in other files.

Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the @ignore annotation.

/**
 * @ignore
 */
function getUser() {

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
33
{
34
    global $faqHandler, $categoryHandler, $xoopsUser, $xoopsUser, $xoopsConfig, $xoopsDB, $modify, $xoopsModuleConfig, $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...
35
36
    include_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
37
    // If there is a parameter, and the id exists, retrieve data: we're editing a faq
38
    if ($faqid != -1) {
39
        // Creating the FAQ object
40
        $faqObj = new sfFaq($faqid);
41
42
        if ($faqObj->notLoaded()) {
43
            redirect_header('faq.php', 1, _AM_SF_NOARTTOEDIT);
44
        }
45
        switch ($faqObj->status()) {
46
47
            case _SF_STATUS_ASKED:
48
                $breadcrumb_action    = _AM_SF_APPROVING;
49
                $collapsableBar_title = _AM_SF_QUESTION_APPROVING;
50
                $collapsableBar_info  = _AM_SF_QUESTION_APPROVING_INFO;
51
                $button_caption       = _AM_SF_QUEUE;
52
                break;
53
54
            case 'default':
55
            default:
56
                $breadcrumb_action    = _AM_SF_EDITING;
57
                $collapsableBar_title = _AM_SF_EDITQUES;
58
                $collapsableBar_info  = _AM_SF_EDITING_INFO;
59
                $button_caption       = _AM_SF_MODIFY;
60
                break;
61
        }
62
63
        // Creating the category of this FAQ
64
        $categoryObj = $categoryHandler->get($faqObj->categoryid());
65
66
        echo "<br />\n";
67
        sf_collapsableBar('bottomtable', 'bottomtableicon');
68
        echo "<img id='bottomtableicon' src=" . XOOPS_URL . '/modules/' . $xoopsModule->dirname() . "/assets/images/icon/close12.gif alt='' /></a>&nbsp;" . $collapsableBar_title . '</h3>';
69
        echo "<div id='bottomtable'>";
70
        echo "<span style=\"color: #567; margin: 3px 0 12px 0; font-size: small; display: block; \">" . $collapsableBar_info . '</span>';
71
    } else {
72
        // there's no parameter, so we're adding a faq
73
        $faqObj =& $faqHandler->create();
74
        $faqObj->setVar('uid', $xoopsUser->getVar('uid'));
75
        $categoryObj = $categoryHandler->create();
76
77
        $breadcrumb_action = _AM_SF_CREATINGNEW;
78
        $button_caption    = _AM_SF_CREATE;
79
80
        sf_collapsableBar('bottomtable', 'bottomtableicon');
81
        echo "<img id='bottomtableicon' src=" . XOOPS_URL . '/modules/' . $xoopsModule->dirname() . "/assets/images/icon/close12.gif alt='' /></a>&nbsp;" . _AM_SF_CREATEQUESTION . '</h3>';
82
        echo "<div id='bottomtable'>";
83
    }
84
    $sform = new XoopsThemeForm(_AM_SF_OPEN_QUESTION, 'op', xoops_getenv('PHP_SELF'));
85
    $sform->setExtra('enctype="multipart/form-data"');
86
87
    // faq requester
88
    $sform->addElement(new XoopsFormLabel(_AM_SF_REQUESTED_BY, sf_getLinkedUnameFromId($faqObj->uid(), $xoopsModuleConfig['userealname'])));
89
90
    // CATEGORY
91
    /*
92
    * Get information for pulldown menu using XoopsTree.
93
    * First var is the database table
94
    * Second var is the unique field ID for the categories
95
    * Last one is not set as we do not have sub menus in Smartfaq
96
    */
97
98
    $mytree = new XoopsTree($xoopsDB->prefix('smartfaq_categories'), 'categoryid', 'parentid');
99
    ob_start();
100
    $mytree->makeMySelBox('name', 'weight', $categoryObj->categoryid());
101
    $sform->addElement(new XoopsFormLabel(_AM_SF_CATEGORY_QUESTION, ob_get_contents()));
102
    ob_end_clean();
103
104
    // faq QUESTION
105
    $sform->addElement(new XoopsFormTextArea(_AM_SF_QUESTION, 'question', $faqObj->question(), 7, 60));
106
107
    // PER ITEM PERMISSIONS
108
    $memberHandler   = xoops_getHandler('member');
109
    $group_list      = $memberHandler->getGroupList();
110
    $groups_checkbox = new XoopsFormCheckBox(_AM_SF_PERMISSIONS_QUESTION, 'groups[]', $faqObj->getGroups_read());
111
    foreach ($group_list as $group_id => $group_name) {
112
        if ($group_id != XOOPS_GROUP_ADMIN) {
113
            $groups_checkbox->addOption($group_id, $group_name);
114
        }
115
    }
116
    $sform->addElement($groups_checkbox);
117
118
    // faq ID
119
    $sform->addElement(new XoopsFormHidden('faqid', $faqObj->faqid()));
120
121
    $button_tray = new XoopsFormElementTray('', '');
122
    $hidden      = new XoopsFormHidden('op', 'addfaq');
123
    $button_tray->addElement($hidden);
124
125
    $sform->addElement(new XoopsFormHidden('status', $faqObj->status()));
126
    // Setting the FAQ Status
127
    /*  $status_select = new XoopsFormSelect('', 'status', $status);
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% 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...
128
    $status_select->addOptionArray(sf_getStatusArray());
129
    $status_tray = new XoopsFormElementTray(_AM_SF_STATUS_EXP , '&nbsp;');
130
    $status_tray->addElement($status_select);
131
    $sform->addElement($status_tray);
132
    */
133
    if ($faqid == -1) {
134
135
        // there's no faqid? Then it's a new faq
136
        // $button_tray -> addElement( new XoopsFormButton( '', 'mod', _AM_SF_CREATE, 'submit' ) );
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...
137
        $butt_create = new XoopsFormButton('', '', _AM_SF_CREATE, 'submit');
138
        $butt_create->setExtra('onclick="this.form.elements.op.value=\'addfaq\'"');
139
        $button_tray->addElement($butt_create);
140
141
        $butt_clear = new XoopsFormButton('', '', _AM_SF_CLEAR, 'reset');
142
        $button_tray->addElement($butt_clear);
143
144
        $butt_cancel = new XoopsFormButton('', '', _AM_SF_CANCEL, 'button');
145
        $butt_cancel->setExtra('onclick="history.go(-1)"');
146
        $button_tray->addElement($butt_cancel);
147
    } else {
148
        // else, we're editing an existing faq
149
        // $button_tray -> addElement( new XoopsFormButton( '', 'mod', _AM_SF_MODIFY, 'submit' ) );
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...
150
        $butt_create = new XoopsFormButton('', '', $button_caption, 'submit');
151
        $butt_create->setExtra('onclick="this.form.elements.op.value=\'addfaq\'"');
152
        $button_tray->addElement($butt_create);
153
154
        $butt_edit = new XoopsFormButton('', '', _AM_SF_OPEN_QUESTION_EDIT, 'button');
155
        $butt_edit->setExtra("onclick=\"location='faq.php?op=mod&amp;faqid=" . $faqid . "'\"");
156
        $button_tray->addElement($butt_edit);
157
158
        $butt_cancel = new XoopsFormButton('', '', _AM_SF_CANCEL, 'button');
159
        $butt_cancel->setExtra('onclick="history.go(-1)"');
160
        $button_tray->addElement($butt_cancel);
161
    }
162
163
    $sform->addElement($button_tray);
164
    $sform->display();
165
    echo '</div>';
166
    unset($hidden);
167
}
168
169
/* -- Available operations -- */
170
switch ($op) {
171
    case 'mod':
0 ignored issues
show
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
172
173
        global $xoopsUser, $xoopsUser, $xoopsConfig, $xoopsDB, $xoopsModuleConfig, $xoopsModule, $modify, $myts;
174
        $faqid = isset($_GET['faqid']) ? $_GET['faqid'] : -1;
175
176 View Code Duplication
        if ($faqid == -1) {
177
            $totalcategories = $categoryHandler->getCategoriesCount(-1);
178
            if ($totalcategories == 0) {
179
                redirect_header('category.php?op=mod', 3, _AM_SF_NEED_CATEGORY_QUESTION);
180
            }
181
        }
182
183
        $indexAdmin = new ModuleAdmin();
184
        xoops_cp_header();
185
186
        echo $indexAdmin->addNavigation(basename(__FILE__));
187
        include_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
188
189
        editfaq(true, $faqid);
190
        break;
191
192
    case 'addfaq':
193
        global $xoopsUser;
194
195 View Code Duplication
        if (!$xoopsUser) {
196
            if ($xoopsModuleConfig['anonpost'] == 1) {
197
                $uid = 0;
198
            } else {
199
                redirect_header('index.php', 3, _NOPERM);
200
            }
201
        } else {
202
            $uid = $xoopsUser->uid();
203
        }
204
205
        $faqid = isset($_POST['faqid']) ? (int)$_POST['faqid'] : -1;
206
207
        // Creating the FAQ
208
        if ($faqid != -1) {
209
            $faqObj = new sfFaq($faqid);
210
        } else {
211
            $faqObj = $faqHandler->create();
212
        }
213
214
        // Putting the values in the FAQ object
215
        $faqObj->setGroups_read(isset($_POST['groups']) ? $_POST['groups'] : array());
216
        $faqObj->setVar('categoryid', isset($_POST['categoryid']) ? (int)$_POST['categoryid'] : 0);
217
        $faqObj->setVar('question', $_POST['question']);
218
        $faqObj->setVar('status', isset($_POST['status']) ? (int)$_POST['status'] : _SF_STATUS_ASKED);
219
220
        $notifToDo = null;
221
222
        switch ($faqObj->status()) {
223
224
            case _SF_STATUS_NOTSET:
225
                $redirect_msg = _AM_SF_QUESTIONCREATEDOK;
226
                // Setting the new status
227
                $status    = _SF_STATUS_OPENED;
228
                $notifToDo = array(_SF_NOT_QUESTION_PUBLISHED);
229
                $faqObj->setVar('uid', $uid);
230
                break;
231
232
            case _SF_STATUS_ASKED:
233
                $redirect_msg = _AM_SF_QUESTIONPUBLISHED;
234
                // Setting the new status
235
                $status    = _SF_STATUS_OPENED;
236
                $notifToDo = array(_SF_NOT_QUESTION_PUBLISHED);
237
                break;
238
239
            case 'default':
240
            default:
241
                $redirect_msg = _AM_SF_QUESTIONMODIFIED;
242
                // Setting the new status
243
                $status = $faqObj->status();
244
                break;
245
        }
246
        $faqObj->setVar('status', $status);
247
248
        // Storing the FAQ
249
        if (!$faqObj->store()) {
250
            redirect_header('javascript:history.go(-1)', 3, _AM_SF_ERROR . sf_formatErrors($faqObj->getErrors()));
251
        }
252
253
        // Send notifications
254
        if (!empty($notifToDo)) {
255
            $faqObj->sendNotifications($notifToDo);
256
        }
257
258
        redirect_header('question.php', 2, $redirect_msg);
259
260
        break;
261
262 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...
263
        global $xoopsUser, $xoopsUser, $xoopsConfig, $xoopsDB, $_GET;
264
265
        $module_id    = $xoopsModule->getVar('mid');
266
        $gpermHandler = xoops_getHandler('groupperm');
267
268
        $faqid = isset($_POST['faqid']) ? (int)$_POST['faqid'] : 0;
269
        $faqid = isset($_GET['faqid']) ? (int)$_GET['faqid'] : $faqid;
270
271
        $faqObj = new sfFaq($faqid);
272
273
        $confirm  = isset($_POST['confirm']) ? $_POST['confirm'] : 0;
274
        $question = isset($_POST['question']) ? $_POST['question'] : '';
275
276
        if ($confirm) {
277
            if (!$faqHandler->delete($faqObj)) {
278
                redirect_header('question.php', 2, _AM_SF_FAQ_DELETE_ERROR);
279
            }
280
281
            redirect_header('question.php', 2, sprintf(_AM_SF_QUESTIONISDELETED, $faqObj->question()));
282
        } else {
283
            // no confirm: show deletion condition
284
            $faqid = isset($_GET['faqid']) ? (int)$_GET['faqid'] : 0;
285
            xoops_cp_header();
286
            xoops_confirm(array('op' => 'del', 'faqid' => $faqObj->faqid(), 'confirm' => 1, 'name' => $faqObj->question()), 'question.php', _AM_SF_DELETETHISQUESTION . " <br />'" . $faqObj->question() . "'. <br /> <br />", _AM_SF_DELETE);
287
            xoops_cp_footer();
288
        }
289
290
        exit();
291
        break;
292
293
    case 'default':
294
    default:
295
        $indexAdmin = new ModuleAdmin();
296
        xoops_cp_header();
297
        echo $indexAdmin->addNavigation(basename(__FILE__));
298
299
        include_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
300
        include_once XOOPS_ROOT_PATH . '/class/pagenav.php';
301
302
        global $xoopsUser, $xoopsUser, $xoopsConfig, $xoopsDB, $xoopsModuleConfig, $xoopsModule, $smartModuleConfig;
303
304
        echo "<br />\n";
305
306
        sf_collapsableBar('toptable', 'toptableicon');
307
308
        echo "<img id='toptableicon' src=" . XOOPS_URL . '/modules/' . $xoopsModule->dirname() . "/assets/images/icon/close12.gif alt='' /></a>&nbsp;" . _AM_SF_OPENED_TITLE . '</h3>';
309
        echo "<div id='toptable'>";
310
        echo "<span style=\"color: #567; margin: 3px 0 12px 0; font-size: small; display: block; \">" . _AM_SF_OPENED_DSC . '</span>';
311
312
        // Get the total number of published FAQs
313
        $totalfaqs = $faqHandler->getFaqsCount(-1, array(_SF_STATUS_OPENED));
314
        // creating the FAQ objects that are published
315
        $faqsObj         = $faqHandler->getFaqs($xoopsModuleConfig['perpage'], $startfaq, _SF_STATUS_OPENED);
316
        $totalFaqsOnPage = count($faqsObj);
317
        $allcats         = $categoryHandler->getObjects(null, true);
318
        echo "<table width='100%' cellspacing=1 cellpadding=3 border=0 class = outer>";
319
        echo '<tr>';
320
        echo "<th width='40' class='bg3' align='center'><b>" . _AM_SF_ARTID . '</b></td>';
321
        echo "<th width='20%' class='bg3' align='left'><b>" . _AM_SF_ARTCOLNAME . '</b></td>';
322
        echo "<th class='bg3' align='left'><b>" . _AM_SF_QUESTION . '</b></td>';
323
324
        echo "<th width='90' class='bg3' align='center'><b>" . _AM_SF_ASKED . '</b></td>';
325
326
        echo "<th width='90' class='bg3' align='center'><b>" . _AM_SF_CREATED . '</b></td>';
327
        echo "<th width='60' class='bg3' align='center'><b>" . _AM_SF_ACTION . '</b></td>';
328
        echo '</tr>';
329
        //var_dump( $faqsObj);
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
330
        if ($totalfaqs > 0) {
331
            global $pathIcon16;
332
            foreach (array_keys($faqsObj) as $i) {
333
                $categoryObj =& $allcats[$faqsObj[$i]->categoryid()];
334
335
                $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>";
336
                $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>";
337
338
                $requester = sf_getLinkedUnameFromId($faqsObj[$i]->uid(), $smartModuleConfig['userealname']);
339
340
                echo '<tr>';
341
                echo "<td class='head' align='center'>" . $faqsObj[$i]->faqid() . '</td>';
342
                echo "<td class='even' align='left'>" . $categoryObj->name() . '</td>';
343
                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>';
344
345
                echo "<td class='even' align='center'>" . $requester . '</td>';
346
347
                echo "<td class='even' align='center'>" . $faqsObj[$i]->datesub('s') . '</td>';
348
                echo "<td class='even' align='center'> $modify $delete </td>";
349
                echo '</tr>';
350
            }
351
        } else {
352
            $faqid = -1;
353
            echo '<tr>';
354
            echo "<td class='head' align='center' colspan= '7'>" . _AM_SF_NOQUEUED . '</td>';
355
            echo '</tr>';
356
        }
357
        echo "</table>\n";
358
        echo "<br />\n";
359
360
        $pagenav = new XoopsPageNav($totalfaqs, $xoopsModuleConfig['perpage'], $startfaq, 'startfaq');
361
        echo '<div style="text-align:right;">' . $pagenav->renderNav() . '</div>';
362
        echo '</div>';
363
364
        $totalcategories = $categoryHandler->getCategoriesCount(-1);
365
        if ($totalcategories > 0) {
366
            editfaq();
367
        }
368
369
        break;
370
}
371
372
include_once __DIR__ . '/admin_footer.php';
373