XOOPS 2.5.6  Final
 All Classes Namespaces Files Functions Variables Pages
notification_functions.php
Go to the documentation of this file.
1 <?php
20 defined('XOOPS_ROOT_PATH') or die('Restricted access');
21 
22 // RMV-NOTIFY
23 
24 // FIXME: Do some caching, so we don't retrieve the same category / event
25 // info many times.
26 
34 function notificationEnabled($style, $module_id = null)
35 {
36  if (isset($GLOBALS['xoopsModuleConfig']['notification_enabled'])) {
37  $status = $GLOBALS['xoopsModuleConfig']['notification_enabled'];
38  } else {
39  if (! isset($module_id)) {
40  return false;
41  }
42  $module_handler =& xoops_gethandler('module');
44  if (!empty($module) && $module->getVar('hasnotification') == 1) {
45  $config_handler =& xoops_gethandler('config');
46  $config = $config_handler->getConfigsByCat(0, $module_id);
47  $status = $config['notification_enabled'];
48  } else {
49  return false;
50  }
51  }
52  include_once $GLOBALS['xoops']->path('include/notification_constants.php');
53  if (($style == 'block') && ($status == XOOPS_NOTIFICATION_ENABLEBLOCK || $status == XOOPS_NOTIFICATION_ENABLEBOTH)) {
54  return true;
55  }
56  if (($style == 'inline') && ($status == XOOPS_NOTIFICATION_ENABLEINLINE || $status == XOOPS_NOTIFICATION_ENABLEBOTH)) {
57  return true;
58  }
59  // if ($status != XOOPS_NOTIFICATION_DISABLE) {
60  // return true;
61  // }
62  return false;
63 }
64 
74 function &notificationCategoryInfo($category_name = '', $module_id = null)
75 {
76  if (!isset($module_id)) {
77  global $xoopsModule;
78  $module_id = !empty($xoopsModule) ? $xoopsModule->getVar('mid') : 0;
80  } else {
81  $module_handler =& xoops_gethandler('module');
83  }
84  $not_config = & $module->getInfo('notification');
85  if (empty($category_name)) {
86  return $not_config['category'];
87  }
88  foreach ($not_config['category'] as $category) {
89  if ($category['name'] == $category_name) {
90  return $category;
91  }
92  }
93  $ret = false;
94  return $ret;
95 }
96 
111 {
112  $ret = false;
113  $all_categories =& notificationCategoryInfo('', $module_id);
114  if (empty($all_categories)) {
115  return $ret;
116  }
117  foreach ($all_categories as $category) {
118  $all_events =& notificationEvents($category['name'], false, $module_id);
119  if (empty($all_events)) {
120  continue;
121  }
122  foreach ($all_events as $event) {
123  if ($event['name'] == 'comment') {
124  return $category;
125  }
126  }
127  }
128  return $ret;
129 }
130 
131 // TODO: some way to include or exclude admin-only events...
132 
142 function &notificationEvents($category_name, $enabled_only, $module_id = null)
143 {
144  if (!isset($module_id)) {
145  global $xoopsModule;
146  $module_id = !empty($xoopsModule) ? $xoopsModule->getVar('mid') : 0;
148  } else {
149  $module_handler =& xoops_gethandler('module');
151  }
152  $not_config = $module->getInfo('notification');
153  $config_handler = & xoops_gethandler('config');
154  $mod_config = $config_handler->getConfigsByCat(0, $module_id);
155 
156  $category = notificationCategoryInfo($category_name, $module_id);
157 
158  global $xoopsConfig;
159  $event_array = array();
160 
161  $override_comment = false;
162  $override_commentsubmit = false;
163  $override_bookmark = false;
164 
165  foreach ($not_config['event'] as $event) {
166  if ($event['category'] == $category_name) {
167  if (!is_dir($dir = XOOPS_ROOT_PATH . '/modules/' . $module->getVar('dirname') . '/language/' . $xoopsConfig['language'] . '/mail_template/')) {
168  $dir = XOOPS_ROOT_PATH . '/modules/' . $module->getVar('dirname') . '/language/english/mail_template/';
169  }
170  $event['mail_template_dir'] = $dir;
171  if (!$enabled_only || notificationEventEnabled($category, $event, $module)) {
172  $event_array[] = $event;
173  }
174  if ($event['name'] == 'comment') {
175  $override_comment = true;
176  }
177  if ($event['name'] == 'comment_submit') {
178  $override_commentsubmit = true;
179  }
180  if ($event['name'] == 'bookmark') {
181  $override_bookmark = true;
182  }
183  }
184  }
185 
186  xoops_loadLanguage('notification');
187  // Insert comment info if applicable
188 
189  if ($module->getVar('hascomments')) {
190  $com_config = $module->getInfo('comments');
191  if (!empty($category['item_name']) && $category['item_name'] == $com_config['itemName']) {
192  if (!is_dir($dir = XOOPS_ROOT_PATH . '/language/' . $xoopsConfig['language'] . '/mail_template/')) {
193  $dir = XOOPS_ROOT_PATH . '/language/english/mail_template/';
194  }
195  $mail_template_dir = $dir;
196 
197  include_once $GLOBALS['xoops']->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']) {
208  // comments disabled, no comment events
209  break;
211  // all comments are automatically approved, no 'submit'
212  if (!$override_comment) {
213  $insert_comment = true;
214  }
215  break;
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 = array(
230  'name' => 'comment' ,
231  'category' => $category['name'] ,
232  'title' => _NOT_COMMENT_NOTIFY ,
233  'caption' => _NOT_COMMENT_NOTIFYCAP ,
234  'description' => _NOT_COMMENT_NOTIFYDSC ,
235  'mail_template_dir' => $mail_template_dir ,
236  'mail_template' => 'comment_notify' ,
237  'mail_subject' => _NOT_COMMENT_NOTIFYSBJ);
238  if (!$enabled_only || notificationEventEnabled($category, $event, $module)) {
239  $event_array[] = $event;
240  }
241  }
242  if ($insert_submit) {
243  $event = array(
244  'name' => 'comment_submit' ,
245  'category' => $category['name'] ,
246  'title' => _NOT_COMMENTSUBMIT_NOTIFY ,
247  'caption' => _NOT_COMMENTSUBMIT_NOTIFYCAP ,
248  'description' => _NOT_COMMENTSUBMIT_NOTIFYDSC ,
249  'mail_template_dir' => $mail_template_dir ,
250  'mail_template' => 'commentsubmit_notify' ,
251  'mail_subject' => _NOT_COMMENTSUBMIT_NOTIFYSBJ ,
252  'admin_only' => 1);
253  if (!$enabled_only || notificationEventEnabled($category, $event, $module)) {
254  $event_array[] = $event;
255  }
256  }
257 
258  }
259  }
260 
261  // Insert bookmark info if appropriate
262 
263 
264  if (!empty($category['allow_bookmark'])) {
265  if (!$override_bookmark) {
266  $event = array(
267  'name' => 'bookmark' ,
268  'category' => $category['name'] ,
269  'title' => _NOT_BOOKMARK_NOTIFY ,
270  'caption' => _NOT_BOOKMARK_NOTIFYCAP ,
271  'description' => _NOT_BOOKMARK_NOTIFYDSC);
272  if (!$enabled_only || notificationEventEnabled($category, $event, $module)) {
273  $event_array[] = $event;
274  }
275  }
276  }
277 
278  return $event_array;
279 
280 }
281 
294 function notificationEventEnabled(&$category, &$event, &$module)
295 {
296  $config_handler =& xoops_gethandler('config');
297  $mod_config = $config_handler->getConfigsByCat(0, $module->getVar('mid'));
298 
299  if (is_array($mod_config['notification_events']) && $mod_config['notification_events'] != array()) {
300  $option_name = notificationGenerateConfig($category, $event, 'option_name');
301  if (in_array($option_name, $mod_config['notification_events'])) {
302  return true;
303  }
304  $notification_handler = & xoops_gethandler('notification');
305  }
306  return false;
307 }
308 
318 function &notificationEventInfo($category_name, $event_name, $module_id = null)
319 {
320  $all_events = notificationEvents($category_name, false, $module_id);
321  foreach($all_events as $event) {
322  if ($event['name'] == $event_name) {
323  return $event;
324  }
325  }
326  $ret = false;
327  return $ret;
328 }
329 
339 {
340  $all_categories = notificationCategoryInfo('', $module_id);
341 
342  // FIXME: better or more standardized way to do this?
343  $script_url = explode('/', $_SERVER['PHP_SELF']);
344  $script_name = $script_url[count($script_url) - 1];
345 
346  $sub_categories = array();
347  foreach ($all_categories as $category) {
348  // Check the script name
349  $subscribe_from = $category['subscribe_from'];
350  if (!is_array($subscribe_from)) {
351  if ($subscribe_from == '*') {
352  $subscribe_from = array(
353  $script_name);
354  // FIXME: this is just a hack: force a match
355  } else {
356  $subscribe_from = array(
357  $subscribe_from);
358  }
359  }
360  if (!in_array($script_name, $subscribe_from)) {
361  continue;
362  }
363  // If 'item_name' is missing, automatic match. Otherwise
364  // check if that argument exists...
365  if (empty($category['item_name'])) {
366  $category['item_name'] = '';
367  $category['item_id'] = 0;
368  $sub_categories[] = $category;
369  } else {
370  $item_name = $category['item_name'];
371  $id = ($item_name != '' && isset($_GET[$item_name])) ? intval($_GET[$item_name]) : 0;
372  if ($id > 0) {
373  $category['item_id'] = $id;
374  $sub_categories[] = $category;
375  }
376  }
377  }
378  return $sub_categories;
379 
380 }
381 
394 function notificationGenerateConfig(&$category, &$event, $type)
395 {
396  switch ($type) {
397  case 'option_value':
398  case 'name':
399  return 'notify:' . $category['name'] . '-' . $event['name'];
400  break;
401  case 'option_name':
402  return $category['name'] . '-' . $event['name'];
403  break;
404  default:
405  return false;
406  break;
407  }
408 }
409 ?>