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