172 public $mTrigger =
null;
177 public $mTriggerPreAction =
null;
181 parent::__construct($db);
184 $this->mTrigger->register(
'XoopsNotificationHandler.Trigger');
187 $this->mTriggerPreAction->register(
'XoopsNotificationHandler.TriggerPreAction');
194 protected function _escapeValue($value, $type=XOBJ_DTYPE_STRING)
197 case XOBJ_DTYPE_BOOL:
198 $value = $value ?
'1' :
'0';
201 $value = (int)$value;
203 case XOBJ_DTYPE_FLOAT:
204 $value = (float)$value;
206 case XOBJ_DTYPE_STRING:
207 case XOBJ_DTYPE_TEXT:
209 $value = $this->db->quoteString($value);
210 $value = substr($value, 1, strlen($value)-2);
226 $notification->setNew();
228 return $notification;
239 public function &
get($id)
244 $sql =
'SELECT * FROM '.$this->db->prefix(
'xoopsnotifications').
' WHERE not_id='.$id;
245 if ($result = $this->db->query($sql)) {
246 $numrows = $this->db->getRowsNum($result);
249 $notification->assignVars($this->db->fetchArray($result));
250 $ret =& $notification;
268 $not_category =
null;
273 if (
'xoopsnotification' != strtolower(get_class($notification))) {
276 if (!$notification->isDirty()) {
279 if (!$notification->cleanVars()) {
282 foreach ($notification->cleanVars as $k => $v) {
285 if ($notification->isNew()) {
286 $not_id = $this->db->genId(
'xoopsnotifications_not_id_seq');
287 $sql = sprintf(
'INSERT INTO %s (not_id, not_modid, not_itemid, not_category, not_uid, not_event, not_mode) VALUES (%u, %u, %u, %s, %u, %s, %u)', $this->db->prefix(
'xoopsnotifications'), $not_id, $not_modid, $not_itemid, $this->db->quoteString($not_category), $not_uid, $this->db->quoteString($not_event), $not_mode);
289 $sql = sprintf(
'UPDATE %s SET not_modid = %u, not_itemid = %u, not_category = %s, not_uid = %u, not_event = %s, not_mode = %u WHERE not_id = %u', $this->db->prefix(
'xoopsnotifications'), $not_modid, $not_itemid, $this->db->quoteString($not_category), $not_uid, $this->db->quoteString($not_event), $not_mode, $not_id);
291 if (!$result = $this->db->query($sql)) {
294 if (empty($not_id)) {
295 $not_id = $this->db->getInsertId();
297 $notification->assignVar(
'not_id', $not_id);
308 public function delete(&$notification)
310 if (
'xoopsnotification' != strtolower(get_class($notification))) {
313 $sql = sprintf(
'DELETE FROM %s WHERE not_id = %u', $this->db->prefix(
'xoopsnotifications'), $notification->getVar(
'not_id'));
314 if (!$result = $this->db->query($sql)) {
328 public function &
getObjects($criteria =
null, $id_as_key =
false)
332 $sql =
'SELECT * FROM '.$this->db->prefix(
'xoopsnotifications');
333 if (isset($criteria) && $criteria instanceof \criteriaelement) {
334 $sql .=
' '.$criteria->renderWhere();
335 $sort = (
'' != $criteria->getSort()) ? $criteria->getSort() :
'not_id';
336 $sql .=
' ORDER BY '.$sort.
' '.$criteria->getOrder();
337 $limit = $criteria->getLimit();
338 $start = $criteria->getStart();
340 $result = $this->db->query($sql, $limit, $start);
344 while ($myrow = $this->db->fetchArray($result)) {
346 $notification->assignVars($myrow);
348 $ret[] =& $notification;
350 $ret[$myrow[
'not_id']] =& $notification;
352 unset($notification);
367 $sql =
'SELECT COUNT(*) FROM '.$this->db->prefix(
'xoopsnotifications');
368 if (isset($criteria) && $criteria instanceof \criteriaelement) {
369 $sql .=
' '.$criteria->renderWhere();
371 if (!$result =& $this->db->query($sql)) {
374 [$count] = $this->db->fetchRow($result);
387 $sql =
'DELETE FROM '.$this->db->prefix(
'xoopsnotifications');
388 if (isset($criteria) && $criteria instanceof \criteriaelement) {
389 $sql .=
' '.$criteria->renderWhere();
391 if (!$result = $this->db->query($sql)) {
430 $criteria->add(
new Criteria(
'not_modid', (
int)$module_id));
431 $criteria->add(
new Criteria(
'not_category', $this->_escapeValue($category)));
432 $criteria->add(
new Criteria(
'not_itemid', (
int)$item_id));
433 $criteria->add(
new Criteria(
'not_event', $this->_escapeValue($event)));
434 $criteria->add(
new Criteria(
'not_uid', (
int)$user_id));
436 if (1 == count($objects)) {
455 public function isSubscribed($category, $item_id, $event, $module_id, $user_id)
458 $criteria->add(
new Criteria(
'not_modid', (
int)$module_id));
459 $criteria->add(
new Criteria(
'not_category', $this->_escapeValue($category)));
460 $criteria->add(
new Criteria(
'not_itemid', (
int)$item_id));
461 $criteria->add(
new Criteria(
'not_event', $this->_escapeValue($event)));
462 $criteria->add(
new Criteria(
'not_uid', (
int)$user_id));
484 public function subscribe($category, $item_id, $events, $mode=
null, $module_id=
null, $user_id=
null)
486 if (!isset($user_id)) {
488 if (empty($xoopsUser)) {
491 $user_id = $xoopsUser->getVar(
'uid');
495 if (!isset($module_id)) {
497 $module_id = $xoopsModule->getVar(
'mid');
502 $mode = $user->getVar(
'notify_mode');
505 if (!is_array($events)) {
508 foreach ($events as $event) {
509 if ($notification =& $this->
getNotification($module_id, $category, $item_id, $event, $user_id)) {
510 if ($notification->getVar(
'not_mode') != $mode) {
514 $notification =& $this->
create();
515 $notification->setVar(
'not_modid', $module_id);
516 $notification->setVar(
'not_category', $this->_escapeValue($category));
517 $notification->setVar(
'not_itemid', $item_id);
518 $notification->setVar(
'not_uid', $user_id);
519 $notification->setVar(
'not_event', $this->_escapeValue($event));
520 $notification->setVar(
'not_mode', $mode);
521 $this->
insert($notification);
540 $criteria =
new Criteria(
'not_uid', $user_id);
558 $criteria->add(
new Criteria(
'not_modid', $module_id));
559 $criteria->add(
new Criteria(
'not_category', $this->_escapeValue($category)));
561 $criteria->add(
new Criteria(
'not_itemid', $item_id));
563 $criteria->add(
new Criteria(
'not_uid', $user_id));
564 $results = $this->
getObjects($criteria,
true);
566 foreach (array_keys($results) as $i) {
567 $ret[] = $results[$i]->getVar(
'not_event');
584 public function &
getByItemId($module_id, $item_id, $order =
null, $status =
null)
587 $criteria->add(
new Criteria(
'com_itemid', (
int)$item_id));
588 if (isset($status)) {
589 $criteria->add(
new Criteria(
'com_status', (
int)$status));
592 $criteria->setOrder($order);
618 public function triggerEvents($category, $item_id, $events, $extra_tags= [], $user_list= [], $module_id=
null, $omit_user_id=
null)
620 if (!is_array($events)) {
623 foreach ($events as $event) {
624 $this->triggerEvent($category, $item_id, $event, $extra_tags, $user_list, $module_id, $omit_user_id);
628 public function triggerEvent($category, $item_id, $event, $extra_tags= [], $user_list= [], $module_id=
null, $omit_user_id=
null)
631 if (!isset($module_id)) {
633 $module =& $xoopsModule;
634 $module_id = !empty($xoopsModule) ? $xoopsModule->getVar(
'mid') : 0;
636 $module_handler = xoops_gethandler(
'module');
637 $module =& $module_handler->get($module_id);
640 $not_config = $module->getInfo(
'notification');
642 $event_correct =
false;
643 foreach ($not_config[
'event'] as $event_config) {
644 if (($event_config[
'name'] == $event)&&($event_config[
'category'] == $category)) {
645 $event_correct =
true;
662 if ($event_correct) {
675 $force_return =
false;
677 $this->mTriggerPreAction->call(
new XCube_Ref($category),
new XCube_Ref($event),
new XCube_Ref($item_id),
678 new XCube_Ref($extra_tags),
new XCube_Ref($module),
new XCube_Ref($user_list),
679 new XCube_Ref($omit_user_id),
new XCube_Ref($not_config),
680 new XCube_Ref($force_return));
682 $this->mTrigger->call($category, $event, $item_id, $extra_tags,
new XCube_Ref($module), $user_list, $omit_user_id, $not_config,
new XCube_Ref($force_return));
689 $config_handler = xoops_gethandler(
'config');
690 $mod_config =& $config_handler->getConfigsByCat(0, $module->getVar(
'mid'));
691 if (empty($mod_config[
'notification_enabled'])) {
694 $category_info =& notificationCategoryInfo($category, $module_id);
695 $event_info =& notificationEventInfo($category, $event, $module_id);
696 if (!in_array(notificationGenerateConfig($category_info, $event_info,
'option_name'), $mod_config[
'notification_events']) && empty($event_info[
'invisible'])) {
700 if (!isset($omit_user_id)) {
702 if (!empty($xoopsUser)) {
703 $omit_user_id = $xoopsUser->getVar(
'uid');
708 $criteria =
new CriteriaCompo();
709 $criteria->add(
new Criteria(
'not_modid', (
int)$module_id));
710 $criteria->add(
new Criteria(
'not_category', $this->_escapeValue($category)));
711 $criteria->add(
new Criteria(
'not_itemid', (
int)$item_id));
712 $criteria->add(
new Criteria(
'not_event', $this->_escapeValue($event)));
713 $mode_criteria =
new CriteriaCompo();
714 $mode_criteria->add(
new Criteria(
'not_mode', XOOPS_NOTIFICATION_MODE_SENDALWAYS),
'OR');
715 $mode_criteria->add(
new Criteria(
'not_mode', XOOPS_NOTIFICATION_MODE_SENDONCETHENDELETE),
'OR');
716 $mode_criteria->add(
new Criteria(
'not_mode', XOOPS_NOTIFICATION_MODE_SENDONCETHENWAIT),
'OR');
717 $criteria->add($mode_criteria);
718 if (!empty($user_list)) {
719 $user_criteria =
new CriteriaCompo();
720 foreach ($user_list as $user) {
721 $user_criteria->add(
new Criteria(
'not_uid', $user),
'OR');
723 $criteria->add($user_criteria);
725 $notifications =& $this->
getObjects($criteria);
726 if (empty($notifications)) {
733 if (!empty($not_config)) {
734 if (!empty($not_config[
'tags_file'])) {
735 $tags_file = XOOPS_ROOT_PATH .
'/modules/' . $module->getVar(
'dirname') .
'/' . $not_config[
'tags_file'];
736 if (file_exists($tags_file)) {
737 include_once $tags_file;
738 if (!empty($not_config[
'tags_func'])) {
739 $tags_func = $not_config[
'tags_func'];
740 if (function_exists($tags_func)) {
741 $tags = $tags_func($category, (
int)$item_id, $event);
747 if (!empty($not_config[
'lookup_file'])) {
748 $lookup_file = XOOPS_ROOT_PATH .
'/modules/' . $module->getVar(
'dirname') .
'/' . $not_config[
'lookup_file'];
749 if (file_exists($lookup_file)) {
750 include_once $lookup_file;
751 if (!empty($not_config[
'lookup_func'])) {
752 $lookup_func = $not_config[
'lookup_func'];
753 if (function_exists($lookup_func)) {
754 $item_info = $lookup_func($category, (
int)$item_id);
760 $tags[
'X_ITEM_NAME'] = !empty($item_info[
'name']) ? $item_info[
'name'] :
'[' . _NOT_ITEMNAMENOTAVAILABLE .
']';
761 $tags[
'X_ITEM_URL'] = !empty($item_info[
'url']) ? $item_info[
'url'] :
'[' . _NOT_ITEMURLNOTAVAILABLE .
']';
762 $tags[
'X_ITEM_TYPE'] = !empty($category_info[
'item_name']) ? $category_info[
'title'] :
'[' . _NOT_ITEMTYPENOTAVAILABLE .
']';
763 $tags[
'X_MODULE'] = $module->getVar(
'name',
'n');
764 $tags[
'X_MODULE_URL'] = XOOPS_URL .
'/modules/' . $module->getVar(
'dirname') .
'/';
765 $tags[
'X_NOTIFY_CATEGORY'] = $category;
766 $tags[
'X_NOTIFY_EVENT'] = $event;
768 $template_dir = $event_info[
'mail_template_dir'];
769 $template = $event_info[
'mail_template'] .
'.tpl';
770 $subject = $event_info[
'mail_subject'];
772 foreach ($notifications as $notification) {
773 if (empty($omit_user_id) || $notification->getVar(
'not_uid') != $omit_user_id) {
777 $tags[
'X_UNSUBSCRIBE_URL'] = XOOPS_URL .
'/notifications.php';
778 $tags = array_merge($tags, $extra_tags);
780 $notification->notifyUser($template_dir, $template, $subject, $tags);
794 $criteria =
new Criteria(
'not_uid', (
int)$user_id);
813 public function unsubscribe($category, $item_id, $events, $module_id=
null, $user_id=
null)
815 if (!isset($user_id)) {
817 if (empty($xoopsUser)) {
820 $user_id = $xoopsUser->getVar(
'uid');
824 if (!isset($module_id)) {
826 $module_id = $xoopsModule->getVar(
'mid');
830 $criteria->add(
new Criteria(
'not_modid', (
int)$module_id));
831 $criteria->add(
new Criteria(
'not_category', $this->_escapeValue($category)));
832 $criteria->add(
new Criteria(
'not_itemid', (
int)$item_id));
833 $criteria->add(
new Criteria(
'not_uid', (
int)$user_id));
834 if (!is_array($events)) {
838 foreach ($events as $event) {
839 $event_criteria->add(
new Criteria(
'not_event', $this->_escapeValue($event)),
'OR');
841 $criteria->add($event_criteria);
858 $criteria =
new Criteria(
'not_modid', (
int)$module_id);
875 $criteria->add(
new Criteria(
'not_modid', (
int)$module_id));
876 $criteria->add(
new Criteria(
'not_category', $this->_escapeValue($category)));
877 $criteria->add(
new Criteria(
'not_itemid', (
int)$item_id));
893 $criteria->add(
new Criteria(
'not_uid', (
int)$user_id));
894 $criteria->add(
new Criteria(
'not_mode', XOOPS_NOTIFICATION_MODE_WAITFORLOGIN));
896 $notifications = $this->
getObjects($criteria,
true);
897 foreach ($notifications as $n) {
898 $n->setVar(
'not_mode', XOOPS_NOTIFICATION_MODE_SENDONCETHENWAIT);
915 $notification->unsetNew();
916 $notification->setVar($field_name, $field_value);
917 return $this->
insert($notification);