XCL Web Application Platform 2.5.0
The XoopsCube Legacy Project
Loading...
Searching...
No Matches
notification_functions.php
1<?php
13
14
15// RMV-NOTIFY
16
17// FIXME: Do some caching, so we don't retrieve the same category / event info repeated several times.
18
26function notificationEnabled($style, $module_id=null)
27{
28 if (isset($GLOBALS['xoopsModuleConfig']['notification_enabled'])) {
29 $status = $GLOBALS['xoopsModuleConfig']['notification_enabled'];
30 } else {
31 if (!isset($module_id)) {
32 return false;
33 }
34 $module_handler =& xoops_gethandler('module');
35 $module =& $module_handler->get($module_id);
36 if (!empty($module) && 1 == $module->getVar('hasnotification')) {
37 $config_handler =& xoops_gethandler('config');
38 $config = $config_handler->getConfigsByCat(0, $module_id);
39 $status = $config['notification_enabled'];
40 } else {
41 return false;
42 }
43 }
44 include_once XOOPS_ROOT_PATH . '/include/notification_constants.php';
45 if (('block' == $style) && (XOOPS_NOTIFICATION_ENABLEBLOCK == $status || XOOPS_NOTIFICATION_ENABLEBOTH == $status)) {
46 return true;
47 }
48 if (('inline' == $style) && (XOOPS_NOTIFICATION_ENABLEINLINE == $status || XOOPS_NOTIFICATION_ENABLEBOTH == $status)) {
49 return true;
50 }
51 // if ($status != XOOPS_NOTIFICATION_DISABLE) {
52 // return true;
53 // }
54 return false;
55}
56
66function &notificationCategoryInfo($category_name = null, $module_id = null)
67{
68 if (!isset($module_id)) {
69 global $xoopsModule;
70 $module_id = !empty($xoopsModule) ? $xoopsModule->getVar('mid') : 0;
71 $module =& $xoopsModule;
72 } else {
73 $module_handler =& xoops_gethandler('module');
74 $module =& $module_handler->get($module_id);
75 }
76
77 if (!is_object($module)) {
78 $ret = false;
79 return $ret;
80 }
81
82 $not_config =& $module->getInfo('notification');
83 if (null == $category_name) {
84 return $not_config['category'];
85 }
86 foreach ($not_config['category'] as $category) {
87 if ($category['name'] == $category_name) {
88 return $category;
89 }
90 }
91
92 $ret = false;
93 return $ret;
94}
95
107function &notificationCommentCategoryInfo($module_id=null)
108{
109 $all_categories =& notificationCategoryInfo('', $module_id);
110 if (empty($all_categories)) {
111 return false;
112 }
113 foreach ($all_categories as $category) {
114 $all_events =& notificationEvents($category['name'], false, $module_id);
115 if (empty($all_events)) {
116 continue;
117 }
118 foreach ($all_events as $event) {
119 if ('comment' == $event['name']) {
120 return $category;
121 }
122 }
123 }
124
125 $ret = false;
126 return $ret;
127}
128
129// TODO: some way to include or exclude admin-only events...
130
140function &notificationEvents($category_name, $enabled_only, $module_id=null)
141{
142 if (!isset($module_id)) {
143 global $xoopsModule;
144 $module_id = !empty($xoopsModule) ? $xoopsModule->getVar('mid') : 0;
145 $module =& $xoopsModule;
146 } else {
147 $module_handler =& xoops_gethandler('module');
148 $module =& $module_handler->get($module_id);
149 }
150
151 if (!is_object($module)) {
152 $ret = false;
153 return $ret;
154 }
155
156 $not_config =& $module->getInfo('notification');
157 $config_handler =& xoops_gethandler('config');
158 $mod_config = $config_handler->getConfigsByCat(0, $module_id);
159
160 $category =& notificationCategoryInfo($category_name, $module_id);
161
162 global $xoopsConfig;
163 $event_array = [];
164
165 $override_comment = false;
166 $override_commentsubmit = false;
167 $override_bookmark = false;
168
169 foreach ($not_config['event'] as $event) {
170 if ($event['category'] == $category_name) {
171 $event['mail_template_dir'] = XOOPS_ROOT_PATH . '/modules/' . $module->getVar('dirname') . '/language/' . $xoopsConfig['language'] . '/mail_template/';
172 if (!$enabled_only || notificationEventEnabled($category, $event, $module)) {
173 $event_array[] = $event;
174 }
175 if ('comment' == $event['name']) {
176 $override_comment = true;
177 }
178 if ('comment_submit' == $event['name']) {
179 $override_commentsubmit = true;
180 }
181 if ('bookmark' == $event['name']) {
182 $override_bookmark = true;
183 }
184 }
185 }
186
187
188 $root =& XCube_Root::getSingleton();
189 $root->mLanguageManager->loadPageTypeMessageCatalog('notification');
190
191 // Insert comment info if applicable
192
193 if ($module->getVar('hascomments')) {
194 $com_config = $module->getInfo('comments');
195 if (!empty($category['item_name']) && $category['item_name'] == $com_config['itemName']) {
196 $mail_template_dir = XOOPS_ROOT_PATH . '/language/' . $xoopsConfig['language'] . '/mail_template/';
197 include_once XOOPS_ROOT_PATH . '/include/comment_constants.php';
198 $config_handler =& xoops_gethandler('config');
199 $com_config = $config_handler->getConfigsByCat(0, $module_id);
200 if (!$enabled_only) {
201 $insert_comment = true;
202 $insert_submit = true;
203 } else {
204 $insert_comment = false;
205 $insert_submit = false;
206 switch ($com_config['com_rule']) {
207 case XOOPS_COMMENT_APPROVENONE:
208 // comments disabled, no comment events
209 break;
210 case XOOPS_COMMENT_APPROVEALL:
211 // all comments are automatically approved, no 'submit'
212 if (!$override_comment) {
213 $insert_comment = true;
214 }
215 break;
216 case XOOPS_COMMENT_APPROVEUSER:
217 case XOOPS_COMMENT_APPROVEADMIN:
218 // comments first submitted, require later approval
219 if (!$override_comment) {
220 $insert_comment = true;
221 }
222 if (!$override_commentsubmit) {
223 $insert_submit = true;
224 }
225 break;
226 }
227 }
228 if ($insert_comment) {
229 $event = ['name' =>'comment', 'category' => $category['name'], 'title' =>_NOT_COMMENT_NOTIFY, 'caption' =>_NOT_COMMENT_NOTIFYCAP, 'description' =>_NOT_COMMENT_NOTIFYDSC, 'mail_template_dir' =>$mail_template_dir, 'mail_template' =>'comment_notify', 'mail_subject' =>_NOT_COMMENT_NOTIFYSBJ];
230 if (!$enabled_only || notificationEventEnabled($category, $event, $module)) {
231 $event_array[] = $event;
232 }
233 }
234 if ($insert_submit) {
235 $event = ['name' =>'comment_submit', 'category' => $category['name'], 'title' =>_NOT_COMMENTSUBMIT_NOTIFY, 'caption' =>_NOT_COMMENTSUBMIT_NOTIFYCAP, 'description' =>_NOT_COMMENTSUBMIT_NOTIFYDSC, 'mail_template_dir' =>$mail_template_dir, 'mail_template' =>'commentsubmit_notify', 'mail_subject' =>_NOT_COMMENTSUBMIT_NOTIFYSBJ, 'admin_only' =>1];
236 if (!$enabled_only || notificationEventEnabled($category, $event, $module)) {
237 $event_array[] = $event;
238 }
239 }
240 }
241 }
242
243 // Insert bookmark info if appropriate
244
245 if (!empty($category['allow_bookmark'])) {
246 if (!$override_bookmark) {
247 $event = ['name' =>'bookmark', 'category' => $category['name'], 'title' =>_NOT_BOOKMARK_NOTIFY, 'caption' =>_NOT_BOOKMARK_NOTIFYCAP, 'description' =>_NOT_BOOKMARK_NOTIFYDSC];
248 if (!$enabled_only || notificationEventEnabled($category, $event, $module)) {
249 $event_array[] = $event;
250 }
251 }
252 }
253
254 return $event_array;
255}
256
269function notificationEventEnabled(&$category, &$event, &$module)
270{
271 $config_handler =& xoops_gethandler('config');
272 $mod_config = $config_handler->getConfigsByCat(0, $module->getVar('mid'));
273
274 $option_name = notificationGenerateConfig($category, $event, 'option_name');
275 if (is_array($mod_config['notification_events']) && in_array($option_name, $mod_config['notification_events'])) {
276 return true;
277 }
278 $notification_handler =& xoops_gethandler('notification');
279
280 return false;
281}
282
283
293function &notificationEventInfo($category_name, $event_name, $module_id=null)
294{
295 $all_events =& notificationEvents($category_name, false, $module_id);
296 if (is_array($all_events)) {
297 foreach ($all_events as $event) {
298 if ($event['name'] == $event_name) {
299 return $event;
300 }
301 }
302 }
303
304 $ret = false;
305 return $ret;
306}
307
308
316
317function &notificationSubscribableCategoryInfo($module_id=null)
318{
319 $all_categories =& notificationCategoryInfo('', $module_id);
320
321 // FIXME: better or more standardized way to do this?
322 $script_url = explode('/', xoops_getenv('PHP_SELF'));
323 $script_name = $script_url[count($script_url)-1];
324
325 $sub_categories = [];
326
327 foreach ($all_categories as $category) {
328 // Check the script name
329
330 $subscribe_from = $category['subscribe_from'];
331 if (!is_array($subscribe_from)) {
332 if ('*' == $subscribe_from) {
333 $subscribe_from = [$script_name];
334 // FIXME: this is just a hack: force a match
335 } else {
336 $subscribe_from = [$subscribe_from];
337 }
338 }
339 if (!in_array($script_name, $subscribe_from)) {
340 continue;
341 }
342
343 // If 'item_name' is missing, automatic match. Otherwise
344 // check if that argument exists...
345
346 if (empty($category['item_name'])) {
347 $category['item_name'] = '';
348 $category['item_id'] = 0;
349 $sub_categories[] = $category;
350 } else {
351 $item_name = $category['item_name'];
352 $id = ('' !== $item_name && isset($_GET[$item_name])) ? (int)$_GET[$item_name] : 0;
353 if ($id > 0) {
354 $category['item_id'] = $id;
355 $sub_categories[] = $category;
356 }
357 }
358 }
359 return $sub_categories;
360}
361
376function notificationGenerateConfig(&$category, &$event, $type)
377{
378 switch ($type) {
379 case 'option_value':
380 case 'name':
381 return 'notify:' . $category['name'] . '-' . $event['name'];
382 break;
383 case 'option_name':
384 return $category['name'] . '-' . $event['name'];
385 break;
386 default:
387 return false;
388 break;
389 }
390}