helper_notification.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. /**
  3. * [Discuz!] (C)2001-2099 Comsenz Inc.
  4. * This is NOT a freeware, use is subject to license terms
  5. *
  6. * $Id: helper_notification.php 34003 2013-09-18 04:31:14Z nemohou $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. class helper_notification {
  12. public static function notification_add($touid, $type, $note, $notevars = array(), $system = 0, $category = -1) {
  13. global $_G;
  14. if(!($tospace = getuserbyuid($touid))) {
  15. return false;
  16. }
  17. space_merge($tospace, 'field_home');
  18. $filter = empty($tospace['privacy']['filter_note'])?array():array_keys($tospace['privacy']['filter_note']);
  19. if($filter && (in_array($type.'|0', $filter) || in_array($type.'|'.$_G['uid'], $filter))) {
  20. return false;
  21. }
  22. if($category == -1) {
  23. $category = 0;
  24. $categoryname = '';
  25. if($type == 'follow' || $type == 'follower') {
  26. switch ($type) {
  27. case 'follow' : $category = 5; break;
  28. case 'follower' : $category = 6; break;
  29. }
  30. $categoryname = $type;
  31. } else {
  32. foreach($_G['notice_structure'] as $key => $val) {
  33. if(in_array($type, $val)) {
  34. switch ($key) {
  35. case 'mypost' : $category = 1; break;
  36. case 'interactive' : $category = 2; break;
  37. case 'system' : $category = 3; break;
  38. case 'manage' : $category = 4; break;
  39. default : $category = 0;
  40. }
  41. $categoryname = $key;
  42. break;
  43. }
  44. }
  45. }
  46. } else {
  47. switch ($category) {
  48. case 1 : $categoryname = 'mypost'; break;
  49. case 2 : $categoryname = 'interactive'; break;
  50. case 3 : $categoryname = 'system'; break;
  51. case 4 : $categoryname = 'manage'; break;
  52. case 5 : $categoryname = 'follow'; break;
  53. case 6 : $categoryname = 'follower'; break;
  54. default : $categoryname = 'app';
  55. }
  56. }
  57. if($category == 0) {
  58. $categoryname = 'app';
  59. } elseif($category == 1 || $category == 2) {
  60. $categoryname = $type;
  61. }
  62. $notevars['actor'] = "<a href=\"home.php?mod=space&uid=$_G[uid]\">".$_G['member']['username']."</a>";
  63. if(!is_numeric($type)) {
  64. $vars = explode(':', $note);
  65. if(count($vars) == 2) {
  66. $notestring = lang('plugin/'.$vars[0], $vars[1], $notevars);
  67. } else {
  68. $notestring = lang('notification', $note, $notevars);
  69. }
  70. $frommyapp = false;
  71. } else {
  72. $frommyapp = true;
  73. $notestring = $note;
  74. }
  75. $oldnote = array();
  76. if($notevars['from_id'] && $notevars['from_idtype']) {
  77. $oldnote = C::t('home_notification')->fetch_by_fromid_uid($notevars['from_id'], $notevars['from_idtype'], $touid);
  78. }
  79. if(empty($oldnote['from_num'])) $oldnote['from_num'] = 0;
  80. $notevars['from_num'] = $notevars['from_num'] ? $notevars['from_num'] : 1;
  81. $setarr = array(
  82. 'uid' => $touid,
  83. 'type' => $type,
  84. 'new' => 1,
  85. 'authorid' => $_G['uid'],
  86. 'author' => $_G['username'],
  87. 'note' => $notestring,
  88. 'dateline' => $_G['timestamp'],
  89. 'from_id' => $notevars['from_id'],
  90. 'from_idtype' => $notevars['from_idtype'],
  91. 'from_num' => ($oldnote['from_num']+$notevars['from_num']),
  92. 'category' => $category
  93. );
  94. if($system) {
  95. $setarr['authorid'] = 0;
  96. $setarr['author'] = '';
  97. }
  98. $pkId = 0;
  99. if($oldnote['id']) {
  100. C::t('home_notification')->update($oldnote['id'], $setarr);
  101. $pkId = $oldnote['id'];
  102. } else {
  103. $oldnote['new'] = 0;
  104. $pkId = C::t('home_notification')->insert($setarr, true);
  105. }
  106. $banType = array('task');
  107. if($_G['setting']['cloud_status'] && !in_array($type, $banType)) {
  108. $noticeService = Cloud::loadClass('Service_Client_Notification');
  109. if($oldnote['id']) {
  110. $noticeService->update($touid, $pkId, $setarr['from_num'], $setarr['dateline'], $note);
  111. } else {
  112. $extra = $type == 'post' ? array('pId' => $notevars['pid']) : array();
  113. $extra['notekey'] = $note;
  114. $noticeService->add($touid, $pkId, $type, $setarr['authorid'], $setarr['author'], $setarr['from_id'], $setarr['from_idtype'], $setarr['note'], $setarr['from_num'], $setarr['dateline'], $extra);
  115. }
  116. }
  117. if(empty($oldnote['new'])) {
  118. C::t('common_member')->increase($touid, array('newprompt' => 1));
  119. $newprompt = C::t('common_member_newprompt')->fetch($touid);
  120. if($newprompt) {
  121. $newprompt['data'] = unserialize($newprompt['data']);
  122. if(!empty($newprompt['data'][$categoryname])) {
  123. $newprompt['data'][$categoryname] = intval($newprompt['data'][$categoryname]) + 1;
  124. } else {
  125. $newprompt['data'][$categoryname] = 1;
  126. }
  127. C::t('common_member_newprompt')->update($touid, array('data' => serialize($newprompt['data'])));
  128. } else {
  129. C::t('common_member_newprompt')->insert($touid, array($categoryname => 1));
  130. }
  131. require_once libfile('function/mail');
  132. $mail_subject = lang('notification', 'mail_to_user');
  133. sendmail_touser($touid, $mail_subject, $notestring, $frommyapp ? 'myapp' : $type);
  134. }
  135. if(!$system && $_G['uid'] && $touid != $_G['uid']) {
  136. C::t('home_friend')->update_num_by_uid_fuid(1, $_G['uid'], $touid);
  137. }
  138. }
  139. public static function manage_addnotify($type, $from_num = 0, $langvar = array()) {
  140. global $_G;
  141. $notifyusers = dunserialize($_G['setting']['notifyusers']);
  142. $notifytypes = explode(',', $_G['setting']['adminnotifytypes']);
  143. $notifytypes = array_flip($notifytypes);
  144. $notearr = array('from_id' => 1,'from_idtype' => $type, 'from_num' => $from_num);
  145. if($langvar) {
  146. $langkey = $langvar['langkey'];
  147. $notearr = array_merge($notearr, $langvar);
  148. } else {
  149. $langkey = 'manage_'.$type;
  150. }
  151. foreach($notifyusers as $uid => $user) {
  152. if($user['types'][$notifytypes[$type]]) {
  153. helper_notification::notification_add($uid, $type, $langkey, $notearr, 1, 4);
  154. }
  155. }
  156. }
  157. public function get_categorynum($newprompt_data) {
  158. global $_G;
  159. $categorynum = array();
  160. if(empty($newprompt_data) || !is_array($newprompt_data)) {
  161. return array();
  162. }
  163. foreach($newprompt_data as $key => $val) {
  164. if(in_array($key, array('follow', 'follower'))) {
  165. continue;
  166. }
  167. if(in_array($key, $_G['notice_structure']['mypost'])) {
  168. $categorynum['mypost'] += $val;
  169. } elseif(in_array($key, $_G['notice_structure']['interactive'])) {
  170. $categorynum['interactive'] += $val;
  171. }else{
  172. $categorynum[$key] = $val;
  173. }
  174. }
  175. return $categorynum;
  176. }
  177. public function update_newprompt($uid, $type) {
  178. global $_G;
  179. if($_G['member']['newprompt_num']) {
  180. $tmpprompt = $_G['member']['newprompt_num'];
  181. $num = 0;
  182. $updateprompt = 0;
  183. if(!empty($tmpprompt[$type])) {
  184. unset($tmpprompt[$type]);
  185. $updateprompt = true;
  186. }
  187. foreach($tmpprompt as $key => $val) {
  188. $num += $val;
  189. }
  190. if($num) {
  191. if($updateprompt) {
  192. C::t('common_member_newprompt')->update($uid, array('data' => serialize($tmpprompt)));
  193. C::t('common_member')->update($uid, array('newprompt'=>$num));
  194. }
  195. } else {
  196. C::t('common_member_newprompt')->delete($_G['uid']);
  197. C::t('common_member')->update($_G['uid'], array('newprompt'=>0));
  198. }
  199. }
  200. }
  201. }
  202. ?>