Completed
Push — master ( 8e5652...4e30dc )
by Michael
02:28
created

request.php (7 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__ . '/header.php';
13
require_once XOOPS_ROOT_PATH . '/header.php';
14
15
global $xoopsUser, $xoopsConfig, $xoopsModuleConfig, $xoopsModule;
16
17
// Creating the category handler object
18
/** @var \XoopsModules\Smartfaq\CategoryHandler $categoryHandler */
19
$categoryHandler = \XoopsModules\Smartfaq\Helper::getInstance()->getHandler('Category');
20
21
// Creating the FAQ handler object
22
/** @var \XoopsModules\Smartfaq\FaqHandler $faqHandler */
23
$faqHandler = \XoopsModules\Smartfaq\Helper::getInstance()->getHandler('Faq');
24
25
// Get the total number of categories
26
$totalCategories = count($categoryHandler->getCategories());
27
28
if (0 == $totalCategories) {
29
    redirect_header('index.php', 1, _AM_SF_NOCOLEXISTS);
30
}
31
32
// Find if the user is admin of the module
33
$isAdmin = Smartfaq\Utility::userIsAdmin();
34
// If the user is not admin AND we don't allow user submission, exit
35 View Code Duplication
if (!($isAdmin
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...
36
      || (isset($xoopsModuleConfig['allowrequest'])
37
          && 1 == $xoopsModuleConfig['allowrequest']
38
          && (is_object($xoopsUser) || (isset($xoopsModuleConfig['anonpost']) && 1 == $xoopsModuleConfig['anonpost']))))) {
39
    redirect_header('index.php', 1, _NOPERM);
40
}
41
42
$op = '';
43
44
if (isset($_GET['op'])) {
45
    $op = $_GET['op'];
46
}
47
if (isset($_POST['op'])) {
48
    $op = $_POST['op'];
49
}
50
51
switch ($op) {
52
    case 'post':
53
54
        global $xoopsUser, $xoopsConfig, $xoopsModule, $xoopsModuleConfig, $xoopsDB;
55
56
        $newFaqObj = $faqHandler->create();
57
58 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...
59
            if (1 == $xoopsModuleConfig['anonpost']) {
60
                $uid = 0;
61
            } else {
62
                redirect_header('index.php', 3, _NOPERM);
63
            }
64
        } else {
65
            $uid = $xoopsUser->uid();
0 ignored issues
show
The method uid cannot be called on $xoopsUser (of type integer|double|string|array|boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
66
        }
67
68
        // Putting the values about the FAQ in the FAQ object
69
        $newFaqObj->setVar('categoryid', $_POST['categoryid']);
70
        $newFaqObj->setVar('uid', $uid);
71
        $newFaqObj->setVar('question', $_POST['question']);
72
        $notifypub = isset($_POST['notifypub']) ? $_POST['notifypub'] : 0;
73
        $newFaqObj->setVar('notifypub', $notifypub);
74
75
        // Setting the status of the FAQ
76
        if (1 == $xoopsModuleConfig['autoapprove_request']) {
77
            $newFaqObj->setVar('status', Constants::SF_STATUS_OPENED);
78
        } else {
79
            $newFaqObj->setVar('status', Constants::SF_STATUS_ASKED);
80
        }
81
82
        // Storing the FAQ object in the database
83
        if (!$newFaqObj->store()) {
84
            redirect_header('javascript:history.go(-1)', 3, _MD_SF_REQUEST_ERROR . Smartfaq\Utility::formatErrors($newFaqObj->getErrors()));
85
        }
86
87
        // Get the cateopry object related to that FAQ
88
        // If autoapprove_requested
89
        if (1 == $xoopsModuleConfig['autoapprove_request']) {
90
            // We do not not subscribe user to notification on publish since we publish it right away
91
92
            // Send notifications
93
            $newFaqObj->sendNotifications([Constants::SF_NOT_QUESTION_PUBLISHED]);
94
95
            $redirect_msg = _MD_SF_REQUEST_RECEIVED_AND_PUBLISHED;
96 View Code Duplication
        } else {
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...
97
            // Subscribe the user to On Published notification, if requested
98
            if (1 == $notifypub) {
99
                require_once XOOPS_ROOT_PATH . '/include/notification_constants.php';
100
                $notificationHandler = xoops_getHandler('notification');
101
                $notificationHandler->subscribe('question', $newFaqObj->faqid(), 'approved', XOOPS_NOTIFICATION_MODE_SENDONCETHENDELETE);
102
            }
103
            // Send notifications
104
            $newFaqObj->sendNotifications([Constants::SF_NOT_QUESTION_SUBMITTED]);
105
106
            $redirect_msg = _MD_SF_REQUEST_RECEIVED_NEED_APPROVAL;
107
        }
108
109
        //redirect_header("javascript:history.go(-2)", 3, $redirect_msg);
0 ignored issues
show
Unused Code Comprehensibility introduced by
73% 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...
110
        redirect_header('index.php', 2, $redirect_msg);
111
        break;
112
113
    case 'form':
114
    default:
115
116
        global $xoopsUser, $myts;
117
118
        $GLOBALS['xoopsOption']['template_main'] = 'smartfaq_submit.tpl';
119
        require_once XOOPS_ROOT_PATH . '/header.php';
120
        require_once __DIR__ . '/footer.php';
121
122
        $name = $xoopsUser ? ucwords($xoopsUser->getVar('uname')) : 'Anonymous';
0 ignored issues
show
The method getVar cannot be called on $xoopsUser (of type integer|double|string|array|boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
123
124
        $moduleName =& $myts->displayTarea($xoopsModule->getVar('name'));
125
        $xoopsTpl->assign('whereInSection', $moduleName);
126
        $xoopsTpl->assign('lang_submit', _MD_SF_REQUEST);
127
128
        $xoopsTpl->assign('lang_intro_title', _MD_SF_REQUEST);
129
        $xoopsTpl->assign('lang_intro_text', _MD_SF_GOODDAY . "<b>$name</b>, " . $myts->displayTarea($xoopsModuleConfig['requestintromsg']));
130
131
        require_once __DIR__ . '/include/request.inc.php';
132
133
        $name = $xoopsUser ? ucwords($xoopsUser->getVar('uname')) : 'Anonymous';
0 ignored issues
show
The method getVar cannot be called on $xoopsUser (of type integer|double|string|array|boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
134
135
        $sectionname = $myts->htmlSpecialChars($xoopsModule->getVar('name'));
136
137
        require_once __DIR__ . '/include/request.inc.php';
138
139
        require_once XOOPS_ROOT_PATH . '/footer.php';
140
141
        break;
142
}
143