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

answer.php (1 issue)

1
<?php
2
3
/**
4
 * Module: SmartFAQ
5
 * Author: The SmartFactory <www.smartfactory.ca>
6
 * Licence: GNU
7
 */
8
9
use Xmf\Request;
10
use XoopsModules\Smartfaq;
11
use XoopsModules\Smartfaq\Constants;
12
13
require_once __DIR__ . '/header.php';
14
15
global $xoopsUser, $xoopsConfig, $xoopsModuleConfig, $xoopsModule;
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...
16
17
// If user is anonymous and we don't allow anonymous posting, exit; else, get the uid
18
if (!$xoopsUser && (1 != $xoopsModuleConfig['anonpost'])) {
19
    redirect_header('index.php', 3, _NOPERM);
20
}
21
22
$op = 'form';
23
24
// Getting the operation we are doing
25
if (isset($_GET['op'])) {
26
    $op = $_GET['op'];
27
}
28
if (isset($_POST['op'])) {
29
    $op = $_POST['op'];
30
}
31
32
// Getting the faqid
33
$faqid = Request::getInt('faqid', 0, 'GET');
34
$faqid = Request::getInt('faqid', $faqid, 'POST');
35
36
// If no FAQ is selected, exit
37
if (0 == $faqid) {
38
    redirect_header('javascript:history.go(-1)', 1, _MD_SF_NOFAQSELECTED);
39
}
40
41
// Creating the FAQ handler object
42
/** @var \XoopsModules\Smartfaq\FaqHandler $faqHandler */
43
$faqHandler = \XoopsModules\Smartfaq\Helper::getInstance()->getHandler('Faq');
44
45
// Creating the answer handler object
46
/** @var \XoopsModules\Smartfaq\AnswerHandler $answerHandler */
47
$answerHandler = \XoopsModules\Smartfaq\Helper::getInstance()->getHandler('Answer');
48
49
switch ($op) {
50
    // The answer is posted
51
    case 'post':
52
53
        global $faqObj, $xoopsUser, $xoopsConfig, $xoopsModule, $xoopsModuleConfig, $xoopsDB;
54
55
        // If user is anonymous and we don't allow anonymous posting, exit; else, get the uid
56
        if (!$xoopsUser) {
57
            if (1 == $xoopsModuleConfig['anonpost']) {
58
                $uid = 0;
59
            } else {
60
                redirect_header('index.php', 3, _NOPERM);
61
            }
62
        } else {
63
            $uid = $xoopsUser->uid();
64
        }
65
66
        // Creating the FAQ object for the selected FAQ
67
        $faqObj = new Smartfaq\Faq($faqid);
68
69
        // If the selected FAQ was not found, exit
70
        if ($faqObj->notLoaded()) {
71
            redirect_header('javascript:history.go(-2)', 1, _MD_SF_NOFAQSELECTED);
72
        }
73
74
        // Get the category object related to that FAQ
75
        $categoryObj = $faqObj->category();
76
77
        // Create the answer object
78
        $newAnswerObj = $answerHandler->create();
79
80
        // Putting the values in the answer object
81
        $newAnswerObj->setVar('faqid', $faqObj->faqid());
82
        $newAnswerObj->setVar('answer', $_POST['answer']);
83
        $newAnswerObj->setVar('uid', $uid);
84
85
        // Depending of the status of the FAQ, some values need to be set
86
        $original_status = $faqObj->status();
87
        switch ($original_status) {
88
            // This is an Open Question
89
            case Constants::SF_STATUS_OPENED:
90
                if (1 == $xoopsModuleConfig['autoapprove_answer']) {
91
                    // We automatically approve submitted answer for Open Question, so the question become a Submitted Q&A
92
                    if (1 == $xoopsModuleConfig['autoapprove_submitted_faq']) {
93
                        // We automatically approve Submitted Q&A
94
                        $redirect_msg = _MD_SF_QNA_RECEIVED_AND_PUBLISHED;
95
                        $faqObj->setVar('status', Constants::SF_STATUS_PUBLISHED);
96
                        $newAnswerObj->setVar('status', Constants::SF_AN_STATUS_APPROVED);
97
                        $notifCase = 1;
98
                    } else {
99
                        // Submitted Q&A need approbation
100
                        $redirect_msg = _MD_SF_QNA_RECEIVED_NEED_APPROVAL;
101
                        $faqObj->setVar('status', Constants::SF_STATUS_SUBMITTED);
102
                        $newAnswerObj->setVar('status', Constants::SF_AN_STATUS_PROPOSED);
103
                        $notifCase = 2;
104
                    }
105
                } else {
106
                    // Submitted answer need approbation
107
                    $redirect_msg = _MD_SF_OPEN_ANSWER_NEED_APPROBATION;
108
                    $faqObj->setVar('status', Constants::SF_STATUS_ANSWERED);
109
                    $newAnswerObj->setVar('status', Constants::SF_AN_STATUS_PROPOSED);
110
111
                    $notifCase = 3;
112
                }
113
                break;
114
115
            // This is a published FAQ for which a user submitted a new answer
116
            case Constants::SF_STATUS_PUBLISHED:
117
            case Constants::SF_STATUS_NEW_ANSWER:
118
                if (1 == $xoopsModuleConfig['autoapprove_answer_new']) {
119
                    // We automatically approve new submitted answer for already published FAQ
120
                    $redirect_msg = '4';
121
                    $faqObj->setVar('status', Constants::SF_STATUS_SUBMITTED);
122
                    $newAnswerObj->setVar('status', Constants::SF_AN_STATUS_APPROVED);
123
                    $notifCase = 4;
124
                } else {
125
                    // New submitted answer need approbation
126
                    $redirect_msg = _MD_SF_FAQ_NEW_ANSWER_NEED_APPROBATION;
127
                    $faqObj->setVar('status', Constants::SF_STATUS_NEW_ANSWER);
128
                    $newAnswerObj->setVar('status', Constants::SF_AN_STATUS_PROPOSED);
129
                    $notifCase = 5;
130
                }
131
                break;
132
        }
133
134
        // Storing the FAQ object in the database
135
        if (!$faqObj->store()) {
136
            redirect_header('javascript:history.go(-1)', 3, _MD_SF_SUBMIT_ERROR . Smartfaq\Utility::formatErrors($faqObj->getErrors()));
137
        }
138
139
        // Storing the answer object in the database
140
        if (!$newAnswerObj->store()) {
141
            redirect_header('javascript:history.go(-1)', 3, _MD_SF_SUBMIT_ERROR . Smartfaq\Utility::formatErrors($newAnswerObj->getErrors()));
142
        }
143
144
        /** @var \XoopsNotificationHandler $notificationHandler */
145
        $notificationHandler = xoops_getHandler('notification');
146
        switch ($notifCase) {
147
            case 1:
148
                // Question submitted, auto-approved; became Q&A, auto-approved
149
                // We do not not subscribe user to notification on publish since we publish it right away
150
151
                // Send notifications
152
                $faqObj->sendNotifications([Constants::SF_NOT_FAQ_PUBLISHED]);
153
                break;
154
155
            case 2:
156
                // Answer for an open question submitted, auto-approved; became Q&A, need approbation
157
                if (isset($_POST['notifypub']) && 1 == $_POST['notifypub']) {
158
                    require_once XOOPS_ROOT_PATH . '/include/notification_constants.php';
159
                    $notificationHandler->subscribe('faq', $faqObj->faqid(), 'approved', XOOPS_NOTIFICATION_MODE_SENDONCETHENDELETE);
160
                }
161
                // Send notifications
162
                $faqObj->sendNotifications([Constants::SF_NOT_FAQ_SUBMITTED]);
163
                break;
164
165
            case 3:
166
                // Answer submitted, needs approbation
167
                if (isset($_POST['notifypub']) && 1 == $_POST['notifypub']) {
168
                    require_once XOOPS_ROOT_PATH . '/include/notification_constants.php';
169
                    $notificationHandler->subscribe('question', $newAnswerObj->answerid(), 'approved', XOOPS_NOTIFICATION_MODE_SENDONCETHENDELETE);
170
                }
171
                // Send notifications
172
                $faqObj->sendNotifications([Constants::SF_NOT_QUESTION_SUBMITTED]);
173
                break;
174
            case 4:
175
                // New answer submitted for a published Q&A, auto-approved
176
                // TODO...
177
                break;
178
179
            case 5:
180
                // New answer submitted for a published Q&A, need approbation
181
                // Send notifications
182
                if (isset($_POST['notifypub']) && 1 == $_POST['notifypub']) {
183
                    require_once XOOPS_ROOT_PATH . '/include/notification_constants.php';
184
                    $notificationHandler->subscribe('faq', $newAnswerObj->answerid(), 'answer_approved', XOOPS_NOTIFICATION_MODE_SENDONCETHENDELETE);
185
                }
186
187
                $faqObj->sendNotifications([Constants::SF_NOT_NEW_ANSWER_PROPOSED]);
188
                break;
189
        }
190
191
        redirect_header('index.php', 3, $redirect_msg);
192
        break;
193
194
    case 'form':
195
    default:
196
197
        global $xoopsUser, $xoopsModule, $_SERVER;
198
199
        // Creating the FAQ object for the selected FAQ
200
        $faqObj = new Smartfaq\Faq($faqid);
201
202
        // If the selected FAQ was not found, exit
203
        if ($faqObj->notLoaded()) {
204
            redirect_header('javascript:history.go(-1)', 1, _MD_SF_NOFAQSELECTED);
205
        }
206
207
        // Creating the category object that holds the selected FAQ
208
        $categoryObj = $faqObj->category();
209
210
        // Creating the answer object
211
        $answerObj = $faqObj->answer();
212
213
        // Check user permissions to access that category of the selected FAQ
214
        if (Smartfaq\Utility::faqAccessGranted($faqObj) < 0) {
215
            redirect_header('javascript:history.go(-1)', 1, _NOPERM);
216
        }
217
218
        $GLOBALS['xoopsOption']['template_main'] = 'smartfaq_submit.tpl';
219
        require_once XOOPS_ROOT_PATH . '/header.php';
220
        require_once __DIR__ . '/footer.php';
221
222
        $name = $xoopsUser ? ucwords($xoopsUser->getVar('uname')) : 'Anonymous';
223
224
        $moduleName =& $myts->displayTarea($xoopsModule->getVar('name'));
225
        $xoopsTpl->assign('whereInSection', $moduleName);
226
        $xoopsTpl->assign('lang_submit', _MD_SF_SUBMITANSWER);
227
228
        $xoopsTpl->assign('lang_intro_title', sprintf(_MD_SF_SUBMITANSWERTO, ucwords($xoopsModule->name())));
229
        $xoopsTpl->assign('lang_intro_text', _MD_SF_GOODDAY . "<b>$name</b>, " . _MD_SF_SUBMITANSWER_INTRO);
230
231
        require_once __DIR__ . '/include/answer.inc.php';
232
233
        require_once XOOPS_ROOT_PATH . '/footer.php';
234
        break;
235
}
236