XOOPS  2.6.0
helper.php
Go to the documentation of this file.
1 <?php
2 /*
3  You may not change or alter any portion of this comment or credits
4  of supporting developers from this source code or any supporting source code
5  which is considered copyrighted (c) material of the original comment or credit authors.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10  */
11 
20 {
26  public function init()
27  {
28  $this->setDirname('notifications');
29  define('NOTIFICATIONS_MODE_SENDALWAYS', 0);
30  define('NOTIFICATIONS_MODE_SENDONCETHENDELETE', 1);
31  define('NOTIFICATIONS_MODE_SENDONCETHENWAIT', 2);
32  define('NOTIFICATIONS_MODE_WAITFORLOGIN', 3);
33 
34  define('NOTIFICATIONS_METHOD_DISABLE', 0);
35  define('NOTIFICATIONS_METHOD_PM', 1);
36  define('NOTIFICATIONS_METHOD_EMAIL', 2);
37 
38  define('NOTIFICATIONS_DISABLE', 0);
39  define('NOTIFICATIONS_ENABLEBLOCK', 1);
40  define('NOTIFICATIONS_ENABLEINLINE', 2);
41  define('NOTIFICATIONS_ENABLEBOTH', 3);
42  }
43 
47  public static function getInstance()
48  {
49  return parent::getInstance();
50  }
51 
55  public function getHandlerNotification()
56  {
57  return $this->getHandler('notification');
58  }
59 
66  public function enabled($style, $module_dirname = null)
67  {
69  if ($status = $xoops->getModuleConfig('notifications_enabled')) {
70  } else {
71  if (!isset($module_dirname)) {
72  return false;
73  }
74 
75  if (!$status = $xoops->getModuleConfig('notifications_enabled', $module_dirname)) {
76  return false;
77  }
78  }
79  if (($style == 'block') && ($status == NOTIFICATIONS_ENABLEBLOCK || $status == NOTIFICATIONS_ENABLEBOTH)) {
80  return true;
81  }
82  if (($style == 'inline') && ($status == NOTIFICATIONS_ENABLEINLINE || $status == NOTIFICATIONS_ENABLEBOTH)) {
83  return true;
84  }
85 
86  return false;
87  }
88 
96  public function getItem($category, $item_id, $dirname = null)
97  {
99  if (!isset($dirname)) {
100  $dirname = $xoops->module->getVar('dirname');
101  }
102 
103  /* @var $plugin NotificationsPluginInterface */
104  if ($plugin = \Xoops\Module\Plugin::getPlugin($dirname, 'notifications')) {
105  return $plugin->item($category, intval($item_id));
106  }
107  return false;
108  }
109 
118  public function getTags($category, $item_id, $event, $dirname = null)
119  {
121  if (!isset($dirname)) {
122  $dirname = $xoops->module->getVar('dirname');
123  }
124 
125  /* @var $plugin NotificationsPluginInterface */
126  if ($plugin = \Xoops\Module\Plugin::getPlugin($dirname, 'notifications')) {
127  return $plugin->tags($category, intval($item_id), $event, $dirname);
128  }
129  return array();
130  }
131 
142  public function getCategory($category_name = '', $dirname = null)
143  {
145  if (!isset($dirname)) {
146  $dirname = $xoops->module->getVar('dirname');
147  }
148 
149  /* @var $plugin NotificationsPluginInterface */
150  if ($plugin = \Xoops\Module\Plugin::getPlugin($dirname, 'notifications')) {
151  $categories = $plugin->categories();
152  if (empty($category_name)) {
153  return $categories;
154  }
155  foreach ($categories as $category) {
156  if ($category['name'] == $category_name) {
157  return $category;
158  }
159  }
160  }
161  return false;
162  }
163 
174  public function getCommentsCategory($dirname = null)
175  {
176  $ret = array();
177  $all_categories = $this->getCategory('', $dirname);
178  if (empty($all_categories)) {
179  return $ret;
180  }
181  foreach ($all_categories as $category) {
182  $all_events = $this->getEvents($category['name'], false, $dirname);
183  if (empty($all_events)) {
184  continue;
185  }
186  foreach ($all_events as $event) {
187  if ($event['name'] == 'comment') {
188  return $category;
189  }
190  }
191  }
192  return $ret;
193  }
194 
195  // TODO: some way to include or exclude admin-only events...
196 
207  public function getEvents($category_name, $enabled_only, $dirname = null)
208  {
211 
212  if (!isset($dirname)) {
213  $dirname = $xoops->isModule() ? $xoops->module->getVar('dirname') : '';
214  }
215  /* @var $plugin NotificationsPluginInterface */
216  if ($plugin = \Xoops\Module\Plugin::getPlugin($dirname, 'notifications')) {
217 
218  $events = $plugin->events();
219 
220  $category = $this->getCategory($category_name, $dirname);
221 
222  $event_array = array();
223 
224  $override_comment = false;
225  $override_commentsubmit = false;
226  $override_bookmark = false;
227 
228  foreach ($events as $event) {
229  if ($event['category'] == $category_name) {
230  if (!is_dir($dir = \XoopsBaseConfig::get('root-path') . '/modules/' . $dirname . '/locale/' . $xoops->getConfig('locale') . '/templates/')) {
231  $dir = \XoopsBaseConfig::get('root-path') . '/modules/' . $dirname . '/locale/en_US/templates/';
232  }
233  $event['mail_template_dir'] = $dir;
234  if (!$enabled_only || $this->eventEnabled($category, $event, $dirname)) {
235  $event_array[] = $event;
236  }
237  if ($event['name'] == 'comment') {
238  $override_comment = true;
239  }
240  if ($event['name'] == 'comment_submit') {
241  $override_commentsubmit = true;
242  }
243  if ($event['name'] == 'bookmark') {
244  $override_bookmark = true;
245  }
246  }
247  }
248 
249  $helper->loadLanguage('main');
250  // Insert comment info if applicable
251 
252  /* @var $commentsPlugin CommentsPluginInterface */
253  if ($xoops->isActiveModule('comments') && $commentsPlugin = \Xoops\Module\Plugin::getPlugin($dirname, 'comments')) {
254  //todo replace this
255  if (!empty($category['item_name']) && $category['item_name'] == $commentsPlugin->itemName()) {
256  if (!is_dir($dir = \XoopsBaseConfig::get('root-path') . '/locale/' . $xoops->getConfig('locale') . '/templates/')) {
257  $dir = \XoopsBaseConfig::get('root-path') . '/locale/en_US/templates/';
258  }
259  $mail_template_dir = $dir;
260 
261  $com_config = $xoops->getModuleConfigs($dirname);
262  if (!$enabled_only) {
263  $insert_comment = true;
264  $insert_submit = true;
265  } else {
266  $insert_comment = false;
267  $insert_submit = false;
268  switch ($com_config['com_rule']) {
269  case COMMENTS_APPROVENONE:
270  // comments disabled, no comment events
271  break;
272  case COMMENTS_APPROVEALL:
273  // all comments are automatically approved, no 'submit'
274  if (!$override_comment) {
275  $insert_comment = true;
276  }
277  break;
278  case COMMENTS_APPROVEUSER:
279  case COMMENTS_APPROVEADMIN:
280  // comments first submitted, require later approval
281  if (!$override_comment) {
282  $insert_comment = true;
283  }
284  if (!$override_commentsubmit) {
285  $insert_submit = true;
286  }
287  break;
288  }
289  }
290  if ($insert_comment) {
291  $event = array(
292  'name' => 'comment',
293  'category' => $category['name'],
296  'description' => _MD_NOTIFICATIONS_COMMENT_NOTIFYDSC,
297  'mail_template_dir' => $mail_template_dir,
298  'mail_template' => 'comment_notify',
299  'mail_subject' => _MD_NOTIFICATIONS_COMMENT_NOTIFYSBJ
300  );
301  if (!$enabled_only || $this->eventEnabled($category, $event, $dirname)) {
302  $event_array[] = $event;
303  }
304  }
305  if ($insert_submit) {
306  $event = array(
307  'name' => 'comment_submit',
308  'category' => $category['name'],
312  'mail_template_dir' => $mail_template_dir,
313  'mail_template' => 'commentsubmit_notify',
315  'admin_only' => 1
316  );
317  if (!$enabled_only || $this->eventEnabled($category, $event, $dirname)) {
318  $event_array[] = $event;
319  }
320  }
321  }
322  }
323 
324  // Insert bookmark info if appropriate
325 
326  if (!empty($category['allow_bookmark'])) {
327  if (!$override_bookmark) {
328  $event = array(
329  'name' => 'bookmark',
330  'category' => $category['name'],
333  'description' => _MD_NOTIFICATIONS_BOOKMARK_NOTIFYDSC
334  );
335  if (!$enabled_only || $this->eventEnabled($category, $event, $dirname)) {
336  $event_array[] = $event;
337  }
338  }
339  }
340 
341  return $event_array;
342  }
343  return array();
344  }
345 
359  public function eventEnabled(&$category, &$event, $dirname)
360  {
362 
363  $mod_config = $xoops->getModuleConfigs($dirname);
364 
365  if (is_array($mod_config['notification_events']) && $mod_config['notification_events'] != array()) {
366  $option_name = $this->generateConfig($category, $event, 'option_name');
367  if (in_array($option_name, $mod_config['notification_events'])) {
368  return true;
369  }
370  }
371  return false;
372  }
373 
384  public function getEvent($category_name, $event_name, $module_dirname = null)
385  {
386  $all_events = $this->getEvents($category_name, false, $module_dirname);
387  foreach ($all_events as $event) {
388  if ($event['name'] == $event_name) {
389  return $event;
390  }
391  }
392  return false;
393  }
394 
403  public function getSubscribableCategories($module_dirname = null)
404  {
405  $all_categories = $this->getCategory('', $module_dirname);
406 
407  // FIXME: better or more standardized way to do this?
408  $script_url = explode('/', $_SERVER['PHP_SELF']);
409  $script_name = $script_url[count($script_url) - 1];
410 
411  $sub_categories = array();
412  foreach ($all_categories as $category) {
413  // Check the script name
414  $subscribe_from = $category['subscribe_from'];
415  if (!is_array($subscribe_from)) {
416  if ($subscribe_from == '*') {
417  $subscribe_from = array(
418  $script_name
419  );
420  // FIXME: this is just a hack: force a match
421  } else {
422  $subscribe_from = array(
423  $subscribe_from
424  );
425  }
426  }
427  if (!in_array($script_name, $subscribe_from)) {
428  continue;
429  }
430  // If 'item_name' is missing, automatic match. Otherwise
431  // check if that argument exists...
432  if (empty($category['item_name'])) {
433  $category['item_name'] = '';
434  $category['item_id'] = 0;
435  $sub_categories[] = $category;
436  } else {
437  $item_name = $category['item_name'];
438  $id = ($item_name != '' && isset($_GET[$item_name])) ? intval($_GET[$item_name]) : 0;
439  if ($id > 0) {
440  $category['item_id'] = $id;
441  $sub_categories[] = $category;
442  }
443  }
444  }
445  return $sub_categories;
446  }
447 
461  public function generateConfig(&$category, &$event, $type)
462  {
463  switch ($type) {
464  case 'option_value':
465  case 'name':
466  return 'notify:' . $category['name'] . '-' . $event['name'];
467  break;
468  case 'option_name':
469  return $category['name'] . '-' . $event['name'];
470  break;
471  default:
472  return false;
473  break;
474  }
475  }
476 
481  {
483  $config_handler = $xoops->getHandlerConfig();
484  $configs = $this->getPluginableConfigs($module);
485 
486  $order = count($xoops->getModuleConfigs($module->getVar('dirname')));
487  foreach ($configs as $config) {
488  $confobj = $config_handler->createConfig();
489  $confobj->setVar('conf_modid', $module->getVar('mid'));
490  $confobj->setVar('conf_catid', 0);
491  $confobj->setVar('conf_name', $config['name']);
492  $confobj->setVar('conf_title', $config['title'], true);
493  $confobj->setVar('conf_desc', $config['description'], true);
494  $confobj->setVar('conf_formtype', $config['formtype']);
495  $confobj->setVar('conf_valuetype', $config['valuetype']);
496  $confobj->setConfValueForInput($config['default'], true);
497  $confobj->setVar('conf_order', $order);
498  if (isset($config['options']) && is_array($config['options'])) {
499  foreach ($config['options'] as $key => $value) {
500  $confop = $config_handler->createConfigOption();
501  $confop->setVar('confop_name', $key, true);
502  $confop->setVar('confop_value', $value, true);
503  $confobj->setConfOptions($confop);
504  unset($confop);
505  }
506  }
507  ++$order;
508  $config_handler->insertConfig($confobj);
509  }
510  }
511 
516  {
518  $this->getHandlerNotification()->unsubscribeByModule($module->getVar('mid'));
519 
520 
521  $configNames = array('notifications_enabled', 'notification_events');
522  $config_handler = $xoops->getHandlerConfig();
523 
524  //Delete all configs
525  $criteria = new CriteriaCompo();
526  $criteria->add(new Criteria('conf_modid', $module->getVar('mid')));
527  $criteria->add(new Criteria('conf_name', "('" . implode("','", $configNames) . "')", 'IN'));
528  $configs = $config_handler->getConfigs($criteria);
529  /* @var $config XoopsConfigItem */
530  foreach ($configs as $config) {
531  $config_handler->deleteConfig($config);
532  }
533  }
534 
541  {
542  $configs = array();
543  $options['_MD_NOTIFICATIONS_CONFIG_DISABLE'] = NOTIFICATIONS_DISABLE;
544  $options['_MD_NOTIFICATIONS_CONFIG_ENABLEBLOCK'] = NOTIFICATIONS_ENABLEBLOCK;
545  $options['_MD_NOTIFICATIONS_CONFIG_ENABLEINLINE'] = NOTIFICATIONS_ENABLEINLINE;
546  $options['_MD_NOTIFICATIONS_CONFIG_ENABLEBOTH'] = NOTIFICATIONS_ENABLEBOTH;
547 
548  $configs[] = array(
549  'name' => 'notifications_enabled',
550  'title' => '_MD_NOTIFICATIONS_CONFIG_ENABLE',
551  'description' => '_MD_NOTIFICATIONS_CONFIG_ENABLEDSC',
552  'formtype' => 'select',
553  'valuetype' => 'int',
554  'default' => NOTIFICATIONS_ENABLEBOTH,
555  'options' => $options
556  );
557  // Event-specific notification options
558  // FIXME: doesn't work when update module... can't read back the array of options properly... " changing to &quot;
559  $options = array();
560  $categories = $this->getCategory('', $module->getVar('dirname'));
561  foreach ($categories as $category) {
562  $events = $this->getEvents($category['name'], false, $module->getVar('dirname'));
563  foreach ($events as $event) {
564  if (!empty($event['invisible'])) {
565  continue;
566  }
567  $option_name = $category['title'] . ' : ' . $event['title'];
568  $option_value = $category['name'] . '-' . $event['name'];
569  $options[$option_name] = $option_value;
570  }
571  unset($events);
572  }
573  unset($categories);
574  $configs[] = array(
575  'name' => 'notification_events',
576  'title' => '_MD_NOTIFICATIONS_CONFIG_EVENTS',
577  'description' => '_MD_NOTIFICATIONS_CONFIG_EVENTSDSC',
578  'formtype' => 'select_multi',
579  'valuetype' => 'array',
580  'default' => array_values($options),
581  'options' => $options
582  );
583  return $configs;
584  }
585 }
getPluginableConfigs(XoopsModule $module)
Definition: helper.php:540
getHandlerNotification()
Definition: helper.php:55
static getInstance()
Definition: Xoops.php:160
static getInstance()
Definition: helper.php:47
$options['editor']
getEvent($category_name, $event_name, $module_dirname=null)
Definition: helper.php:384
const _MD_NOTIFICATIONS_COMMENTSUBMIT_NOTIFYDSC
Definition: main.php:66
$_SERVER['REQUEST_URI']
deleteModuleRelations(XoopsModule $module)
Definition: helper.php:515
getCategory($category_name= '', $dirname=null)
Definition: helper.php:142
const _MD_NOTIFICATIONS_COMMENT_NOTIFYDSC
Definition: main.php:62
const _MD_NOTIFICATIONS_COMMENTSUBMIT_NOTIFYSBJ
Definition: main.php:67
getSubscribableCategories($module_dirname=null)
Definition: helper.php:403
insertModuleRelations(XoopsModule $module)
Definition: helper.php:480
const _MD_NOTIFICATIONS_COMMENTSUBMIT_NOTIFY
Definition: main.php:64
getVar($key, $format= 's')
generateConfig(&$category, &$event, $type)
Definition: helper.php:461
const _MD_NOTIFICATIONS_COMMENT_NOTIFYCAP
Definition: main.php:61
const _MD_NOTIFICATIONS_BOOKMARK_NOTIFYCAP
Definition: main.php:71
$xoops
Definition: admin.php:25
$id
Definition: admin_menu.php:36
getEvents($category_name, $enabled_only, $dirname=null)
Definition: helper.php:207
getItem($category, $item_id, $dirname=null)
Definition: helper.php:96
eventEnabled(&$category, &$event, $dirname)
Definition: helper.php:359
$status
if($_SERVER['REQUEST_METHOD']== 'POST') $config_handler
static get($name)
$module
Definition: main.php:52
$configs
Definition: config.php:27
$type
Definition: misc.php:33
getTags($category, $item_id, $event, $dirname=null)
Definition: helper.php:118
$helper
return $config
$criteria
$dirname
Definition: backend.php:38
$dir
Definition: browse.php:56
getCommentsCategory($dirname=null)
Definition: helper.php:174
enabled($style, $module_dirname=null)
Definition: helper.php:66
const _MD_NOTIFICATIONS_COMMENT_NOTIFYSBJ
Definition: main.php:63
const _MD_NOTIFICATIONS_COMMENTSUBMIT_NOTIFYCAP
Definition: main.php:65
const _MD_NOTIFICATIONS_COMMENT_NOTIFY
Definition: main.php:60
const _MD_NOTIFICATIONS_BOOKMARK_NOTIFY
Definition: main.php:70
const _MD_NOTIFICATIONS_BOOKMARK_NOTIFYDSC
Definition: main.php:72