XCL Web Application Platform 2.5.0
The XoopsCube Legacy Project
Loading...
Searching...
No Matches
CommentListAction.class.php
1<?php
11
12if (!defined('XOOPS_ROOT_PATH')) {
13 exit();
14}
15
16require_once XOOPS_MODULE_PATH . '/legacy/class/AbstractListAction.class.php';
17require_once XOOPS_MODULE_PATH . '/legacy/admin/forms/CommentFilterForm.class.php';
18require_once XOOPS_MODULE_PATH . '/legacy/admin/forms/CommentListForm.class.php';
19require_once XOOPS_MODULE_PATH . '/legacy/admin/actions/CommentEditAction.class.php';
20
22{
23 public $mCommentObjects = [];
24 public $mActionForm = null;
25 public $mpageArr = [5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 60, 70, 80, 90, 100, 0];
26
27 public function prepare(&$controller, &$xoopsUser)
28 {
29 $this->mActionForm =new Legacy_CommentListForm();
30 $this->mActionForm->prepare();
31 }
32
33 public function &_getHandler()
34 {
35 $handler =& xoops_getmodulehandler('comment');
36 return $handler;
37 }
38
39 public function &_getPageNavi()
40 {
41 $navi =new XCube_PageNavigator($this->_getBaseUrl(), XCUBE_PAGENAVI_START | XCUBE_PAGENAVI_PERPAGE);
42 if (isset($_REQUEST[$navi->mPrefix.'perpage']) && 0 == (int)$_REQUEST[$navi->mPrefix . 'perpage']) {
43 $navi->setPerpage(0);
44 }
45 return $navi;
46 }
47
48 public function &_getFilterForm()
49 {
50 $filter =new Legacy_CommentFilterForm($this->_getPageNavi(), $this->_getHandler());
51 return $filter;
52 }
53
54 public function _getBaseUrl()
55 {
56 return './index.php?action=CommentList';
57 }
58
59 public function executeViewIndex(&$controller, &$xoopsUser, &$render)
60 {
61 //
62 // Load the module and the comment user infomations.
63 //
64 foreach (array_keys($this->mObjects) as $key) {
65 $this->mObjects[$key]->loadModule();
66 $this->mObjects[$key]->loadUser();
67 $this->mObjects[$key]->loadStatus();
68 }
69
70 $moduleArr = [];
71 $handler =& xoops_getmodulehandler('comment');
72 $modIds = $handler->getModuleIds();
73
74 $moduleHandler =& xoops_gethandler('module');
75 foreach ($modIds as $mid) {
76 $module =& $moduleHandler->get($mid);
77 if (is_object($module)) {
78 $moduleArr[] =& $module;
79 }
80 unset($module);
81 }
82
83 $statusArr = [];
84 $statusHandler =& xoops_getmodulehandler('commentstatus');
85 $statusArr =& $statusHandler->getObjects();
86
87 $render->setTemplateName('comment_list.html');
88 $render->setAttribute('objects', $this->mObjects);
89 $render->setAttribute('pageNavi', $this->mFilter->mNavi);
90 $render->setAttribute('moduleArr', $moduleArr);
91 $render->setAttribute('statusArr', $statusArr);
92 $render->setAttribute('filterForm', $this->mFilter);
93 $render->setAttribute('pageArr', $this->mpageArr);
94 // Add the actionForm to the template
95 $render->setAttribute('actionForm', $this->mActionForm);
96
97 $comment_handler =& $this->_getHandler();
98 $comment_total = $comment_handler->getCount();
99 $pending_comment_total = $comment_handler->getCount(new Criteria('com_status', XOOPS_COMMENT_PENDING));
100 $active_comment_total = $comment_handler->getCount(new Criteria('com_status', XOOPS_COMMENT_ACTIVE));
101 $hidden_comment_total = $comment_handler->getCount(new Criteria('com_status', XOOPS_COMMENT_HIDDEN));
102 $render->setAttribute('CommentTotal', $comment_total);
103 $render->setAttribute('pendingCommentTotal', $pending_comment_total);
104 $render->setAttribute('activeCommentTotal', $active_comment_total);
105 $render->setAttribute('hiddenCommentTotal', $hidden_comment_total);
106 }
107
108 public function execute(&$controller, &$xoopsUser)
109 {
110 if (null != xoops_getrequest('_form_control_cancel')) {
111 return LEGACY_FRAME_VIEW_CANCEL;
112 }
113
114 $this->mActionForm->fetch();
115 $this->mActionForm->validate();
116
117 if ($this->mActionForm->hasError()) {
118 return $this->_processConfirm($controller, $xoopsUser);
119 } else {
120 return $this->_processSave($controller, $xoopsUser);
121 }
122 }
123
124 public function _processConfirm(&$controller, &$xoopsUser)
125 {
126 $statusArr = $this->mActionForm->get('status');
127 $commentHandler =& xoops_getmodulehandler('comment');
128 //
129 // Do mapping.
130 //
131 foreach (array_keys($statusArr) as $cid) {
132 $comment =& $commentHandler->get($cid);
133 if (is_object($comment)) {
134 $this->mCommentObjects[$cid] =& $comment;
135 }
136 unset($comment);
137 }
138
139 return LEGACY_FRAME_VIEW_INPUT;
140 }
141
142 public function _processSave(&$controller, &$xoopsUser)
143 {
144 $statusArr = $this->mActionForm->get('status');
145
146 $comment_handler = xoops_gethandler('comment');
147
148 foreach (array_keys($statusArr) as $cid) {
149 $comment =& $comment_handler->get($cid);
150 if (is_object($comment)) {
151 $olddata['com_status'] = $comment->get('com_status');
152 $newdata['com_status'] = $this->mActionForm->get('status', $cid);
153 if (count(array_diff_assoc($olddata, $newdata)) > 0) {
154 $comment->set('com_status', $this->mActionForm->get('status', $cid));
155 if (!$comment_handler->insert($comment)) {
156 return LEGACY_FRAME_VIEW_ERROR;
157 }
158
159 $add_userpost = false;
160 $call_approvefunc = false;
161 $call_updatefunc = false;
162 $notify_event = false;
163
164 if (!empty($newdata['com_status']) && XOOPS_COMMENT_PENDING != $newdata['com_status']) {
165 if (XOOPS_COMMENT_PENDING == $olddata['com_status']) {
166 $add_userpost = true;
167 if (XOOPS_COMMENT_ACTIVE == $newdata['com_status']) {
168 $call_updatefunc = true;
169 $call_approvefunc = true;
170 $notify_event = 'comment';
171 }
172 } elseif (XOOPS_COMMENT_HIDDEN == $olddata['com_status'] && XOOPS_COMMENT_ACTIVE == $newdata['com_status']) {
173 $call_updatefunc = true;
174 } elseif (XOOPS_COMMENT_ACTIVE == $olddata['com_status'] && XOOPS_COMMENT_HIDDEN == $newdata['com_status']) {
175 $call_updatefunc = true;
176 }
177 }
178
179 $comment_config = (new Legacy_CommentEditAction())->loadCallbackFile($comment);
180
181 if ($comment_config && false != $call_approvefunc) {
182 $function = $comment_config['callback']['approve'];
183 if (function_exists($function)) {
184 call_user_func($function, $comment);
185 }
186 }
187
188 if ($comment_config && false != $call_updatefunc) {
189 $function = $comment_config['callback']['update'];
190 if (function_exists($function)) {
191 $criteria = new CriteriaCompo(new Criteria('com_modid', $comment->getVar('com_modid')));
192 $criteria->add(new Criteria('com_itemid', $comment->getVar('com_itemid')));
193 $criteria->add(new Criteria('com_status', XOOPS_COMMENT_ACTIVE));
194 $comment_count = $comment_handler->getCount($criteria);
195 call_user_func_array($function, [$comment->getVar('com_itemid'), $comment_count, $comment->getVar('com_id')]);
196 }
197 }
198
199 $uid = $comment->getVar('com_uid');
200 if ($uid > 0 && false != $add_userpost) {
201 $member_handler =& xoops_gethandler('member');
202 $poster =& $member_handler->getUser($uid);
203 if (is_object($poster)) {
204 $member_handler->updateUserByField($poster, 'posts', $poster->getVar('posts') + 1);
205 }
206 }
207
208 //notification
209 // RMV-NOTIFY
210 // trigger notification event if necessary
211 if ($notify_event) {
212 $not_modid = $comment->getVar('com_modid');
213 include_once XOOPS_ROOT_PATH . '/include/notification_functions.php';
214 $not_catinfo =& notificationCommentCategoryInfo($not_modid);
215 $not_category = $not_catinfo['name'];
216 $not_itemid = $comment->getVar('com_itemid');
217 $not_event = $notify_event;
218 $comment_tags = [];
219 $module_handler =& xoops_gethandler('module');
220 $not_module =& $module_handler->get($not_modid);
221 $com_config =& $not_module->getInfo('comments');
222 $comment_url = $com_config['pageName'] . '?';
223 //Umm....not use com_exparams(--;;Fix Me!)
224 //$extra_params = $comment->getVar('com_exparams');
225 //$comment_url .= $extra_params;
226 $comment_url .= $com_config['itemName'];
227 $comment_tags['X_COMMENT_URL'] = XOOPS_URL . '/modules/' . $not_module->getVar('dirname') . '/' .$comment_url . '=' . $comment->getVar('com_itemid').'&amp;com_id='.$comment->getVar('com_id').'&amp;com_rootid='.$comment->getVar('com_rootid').'#comment'.$comment->getVar('com_id');
228 $notification_handler =& xoops_gethandler('notification');
229 $notification_handler->triggerEvent($not_category, $not_itemid, $not_event, $comment_tags, false, $not_modid);
230 }//notify if
231 }//count if
232 }//object if
233 }//foreach
234
235 foreach (array_keys($statusArr) as $cid) {
236 if (1 == $this->mActionForm->get('delete', $cid)) {
237 $comment =& $comment_handler->get($cid);
238 if (is_object($comment)) {
239 if (!$comment_handler->delete($comment)) {
240 return LEGACY_FRAME_VIEW_ERROR;
241 }
242
243 if (1 != $comment->get('com_status') && $comment->get('com_uid') > 0) {
244 $memberhandler =& xoops_gethandler('member');
245 $user =& $memberhandler->getUser($comment->get('com_uid'));
246 if (is_object($user)) {
247 $count = $user->get('posts');
248 if ($count > 0) {
249 $memberhandler->updateUserByField($user, 'posts', $count - 1);
250 }
251 }
252 }
253 // get all comments posted later within the same thread
254 $thread_comments =& $comment_handler->getThread($comment->getVar('com_rootid'), $cid);
255 include_once XOOPS_ROOT_PATH.'/class/tree.php';
256 $xot = new XoopsObjectTree($thread_comments, 'com_id', 'com_pid', 'com_rootid');
257 $child_comments =& $xot->getFirstChild($cid);
258 // now set new parent ID for direct child comments
259 $new_pid = $comment->getVar('com_pid');
260 $errs = [];
261 foreach (array_keys($child_comments) as $i) {
262 $child_comments[$i]->setVar('com_pid', $new_pid);
263 // if the deleted comment is a root comment, need to change root id to own id
264 if (false != $comment->isRoot()) {
265 $new_rootid = $child_comments[$i]->getVar('com_id');
266 $child_comments[$i]->setVar('com_rootid', $child_comments[$i]->getVar('com_id'));
267 if (!$comment_handler->insert($child_comments[$i])) {
268 $errs[] = 'Could not change comment parent ID from <b>'.$cid.'</b> to <b>'.$new_pid.'</b>. (ID: '.$new_rootid.')';
269 } else {
270 // need to change root id for all its child comments as well
271 $c_child_comments =& $xot->getAllChild($new_rootid);
272 $cc_count = is_countable($c_child_comments) ? count($c_child_comments) : 0;
273 foreach (array_keys($c_child_comments) as $j) {
274 $c_child_comments[$j]->setVar('com_rootid', $new_rootid);
275 if (!$comment_handler->insert($c_child_comments[$j])) {
276 $errs[] = 'Could not change comment root ID from <b>'.$cid.'</b> to <b>'.$new_rootid.'</b>.';
277 }
278 }
279 }
280 } else {
281 if (!$comment_handler->insert($child_comments[$i])) {
282 $errs[] = 'Could not change comment parent ID from <b>'.$cid.'</b> to <b>'.$new_pid.'</b>.';
283 }
284 }
285 }
286 if (count($errs) > 0) {
287 return LEGACY_FRAME_VIEW_ERROR;
288 }
289
290 //
291 // callback
292 //
293 $comment_config = (new Legacy_CommentEditAction())->loadCallbackFile($comment);
294
295 if ($comment_config) {
296 $function = $comment_config['callback']['update'];
297 if (function_exists($function)) {
298 $criteria = new CriteriaCompo(new Criteria('com_modid', $comment->getVar('com_modid')));
299 $criteria->add(new Criteria('com_itemid', $comment->getVar('com_itemid')));
300 $criteria->add(new Criteria('com_status', XOOPS_COMMENT_ACTIVE));
301 $comment_count = $comment_handler->getCount($criteria);
302 call_user_func_array($function, [$comment->getVar('com_itemid'), $comment_count, $comment->getVar('com_id')]);
303 }
304 }
305 }//if object
306 }//if
307 }//foreach
308 return LEGACY_FRAME_VIEW_SUCCESS;
309 }
310
317 public function executeViewInput(&$controller, &$xoopsUser, &$render)
318 {
319 foreach (array_keys($this->mCommentObjects) as $key) {
320 $this->mCommentObjects[$key]->loadModule();
321 $this->mCommentObjects[$key]->loadUser();
322 $this->mCommentObjects[$key]->loadStatus();
323 }
324
325 $render->setTemplateName('comment_list_confirm.html');
326 $render->setAttribute('commentObjects', $this->mCommentObjects);
327 $render->setAttribute('actionForm', $this->mActionForm);
328 //
329 // To support a template writer, this send the list of mid that
330 // actionForm kept.
331 //
332 $t_arr = $this->mActionForm->get('status');
333 $render->setAttribute('cids', array_keys($t_arr));
334 }
335
336 public function executeViewSuccess(&$controller, &$xoopsUser, &$renderer)
337 {
338 $controller->executeForward('./index.php?action=CommentList');
339 }
340
341 public function executeViewError(&$controller, &$xoopsUser, &$renderer)
342 {
343 $controller->executeRedirect('./index.php?action=CommentList', 1, _MD_LEGACY_ERROR_DBUPDATE_FAILED);
344 }
345
346 public function executeViewCancel(&$controller, &$xoopsUser, &$renderer)
347 {
348 $controller->executeForward('./index.php?action=CommentList');
349 }
350}
executeViewInput(&$controller, &$xoopsUser, &$render)