You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

298 lines
8.7 KiB

  1. <?php
  2. /**
  3. * @package Widgets
  4. *
  5. * @file Notifs.php
  6. * This file is part of MOVIM.
  7. *
  8. * @brief The notification widget
  9. *
  10. * @author Timothée Jaussoin <edhelas@gmail.com>
  11. *
  12. * @version 1.0
  13. * @date 16 juin 2011
  14. *
  15. * Copyright (C)2010 MOVIM project
  16. *
  17. * See COPYING for licensing information.
  18. */
  19. class Notifs extends WidgetCommon
  20. {
  21. function load()
  22. {
  23. $this->addcss('notifs.css');
  24. $this->addjs('notifs.js');
  25. $this->registerEvent('notification', 'onNotification');
  26. $this->registerEvent('notificationdelete', 'onNotificationDelete');
  27. $this->registerEvent('notifications', 'displayNotifications');
  28. $this->registerEvent('nonotification', 'onNoNotification');
  29. }
  30. /*
  31. * Retrieve all the notifications
  32. */
  33. /*function ajaxGetNotifications() {
  34. $p = new moxl\NotificationGet();
  35. $p->setTo($this->user->getLogin())
  36. ->request();
  37. }*/
  38. /*
  39. * Delete a notification item
  40. */
  41. /*function ajaxDeleteNotification($id) {
  42. $d = new moxl\NotificationItemDelete();
  43. $d->setTo($this->user->getLogin())
  44. ->setId($id)
  45. ->request();
  46. }*/
  47. /*
  48. * Create the notification element from the inbox item
  49. * @param SimpleXMLElement $item
  50. */
  51. /*function onNotification($item) {
  52. $arr = explodeURI((string)$item->entry->link[0]->attributes()->href);
  53. $post = end(explode('/', $arr['node']));
  54. $notifs = Cache::c('activenotifs');
  55. $request = $this->genCallAjax(
  56. "ajaxGetComments",
  57. "'".$this->user->getLogin()."'",
  58. "'".$post."'");
  59. $delete = $this->genCallAjax(
  60. "ajaxDeleteNotification",
  61. "'".(string)$item->attributes()->id."'");
  62. $nhtml = '
  63. <li>
  64. <a href="'.Route::urlize('friend',$arr['path']).'"
  65. onclick="'.$request.'">
  66. <p>
  67. <span style="font-weight: bold;">'.
  68. (string)$item->entry->source->author->name.'
  69. </span>'.prepareDate(strtotime((string)$item->entry->published)).'<br />'.
  70. (string)$item->entry->content.'
  71. </p>
  72. </a>
  73. <a class="delete" href="#" onclick="'.$delete.'">'.t('Delete').'</a>
  74. <div class="clear"></div>
  75. </li>
  76. ';
  77. $notifs[(string)$item->attributes()->id] = $nhtml;
  78. Cache::c('activenotifs', $notifs);
  79. }*/
  80. /*
  81. * In notification deletion
  82. * @param string $id
  83. */
  84. /*function onNotificationDelete($id) {
  85. $notifs = Cache::c('activenotifs');
  86. unset($notifs[$id]);
  87. Cache::c('activenotifs', $notifs);
  88. RPC::call('movim_fill', 'notifs', $this->prepareNotifs());
  89. }*/
  90. /*
  91. * Display all the notifications to the browser
  92. */
  93. /*function displayNotifications() {
  94. RPC::call('movim_fill', 'notifs', $this->prepareNotifs());
  95. }*/
  96. /*
  97. * Display all the notifications to the browser if there is no new
  98. * notifications
  99. */
  100. /*function onNoNotification() {
  101. RPC::call('movim_fill', 'notifs', $this->prepareNotifs());
  102. }*/
  103. /*
  104. * Create the list of notifications
  105. * @return string
  106. */
  107. function prepareNotifs()
  108. {
  109. $notifsnum = 0;
  110. $html = '
  111. <div id="notifslist">
  112. <!--<a
  113. class="button icon color green refresh"
  114. style="margin: 0.5em;"
  115. onclick="'.$this->genCallAjax("ajaxGetNotifications").';
  116. this.innerHTML = \''.t('Updating').'\';
  117. this.className= \'button color orange icon loading\';
  118. this.onclick=null;">
  119. '.t('Refresh').'
  120. </a>-->
  121. <ul>';
  122. // XMPP notifications
  123. $notifs = Cache::c('activenotifs');
  124. if($notifs == false)
  125. $notifs = array();
  126. if(sizeof($notifs) != 0) {
  127. $notifsnum += sizeof($notifs);
  128. /*$html .= '
  129. <li class="title">'.
  130. t('Notifications').'
  131. <span class="num">'.sizeof($notifs).'</span>
  132. </li>';*/
  133. foreach($notifs as $n => $val) {
  134. if($val == 'sub')
  135. $html .= $this->prepareNotifInvitation($n);
  136. //else
  137. // $html .= $val;
  138. }
  139. }
  140. // Contact request pending
  141. /*$cd = new \modl\ContactDAO();
  142. $subscribes = $cd->getRosterSubscribe();
  143. if(sizeof($subscribes) != 0) {
  144. $notifsnum += sizeof($subscribes);
  145. $html .= '
  146. <li class="title">'.
  147. t('Contact request pending').'
  148. <span class="num">'.sizeof($subscribes).'</span>
  149. </li>';
  150. foreach($subscribes as $s) {
  151. $html .= '
  152. <li>
  153. <a href="'.Route::urlize('friend', $s->jid).'">
  154. <img class="avatar" src="'.$s->getPhoto('s').'" />
  155. '.
  156. $s->getTrueName().'
  157. </a>
  158. </li>';
  159. }
  160. }
  161. */
  162. $html .= '
  163. </ul>
  164. </div>';
  165. $notifsnew = '';
  166. if($notifsnum > 0)
  167. $notifsnew = 'class="red"';
  168. /*$html = '
  169. <div id="notifstab" onclick="showNotifsList();">
  170. <span '.$notifsnew.'>'.
  171. $notifsnum.'
  172. </span>
  173. </div>'.$html;
  174. */
  175. return $html;
  176. }
  177. function ajaxSubscribed($jid) {
  178. $p = new moxl\PresenceSubscribed();
  179. $p->setTo($jid)
  180. ->request();
  181. }
  182. function ajaxRefuse($jid) {
  183. $p = new moxl\PresenceUnsubscribed();
  184. $p->setTo($jid)
  185. ->request();
  186. $notifs = Cache::c('activenotifs');
  187. unset($notifs[$jid]);
  188. Cache::c('activenotifs', $notifs);
  189. RPC::call('movim_fill', 'notifs', $this->prepareNotifs());
  190. RPC::commit();
  191. }
  192. function ajaxAddRoster($jid) {
  193. $r = new moxl\RosterAddItem();
  194. $r->setTo($jid)
  195. ->setFrom($this->user->getLogin())
  196. ->request();
  197. }
  198. function ajaxSubscribe($jid) {
  199. $p = new moxl\PresenceSubscribe();
  200. $p->setTo($jid)
  201. ->request();
  202. $notifs = Cache::c('activenotifs');
  203. unset($notifs[$jid]);
  204. Cache::c('activenotifs', $notifs);
  205. RPC::call('movim_fill', 'notifs', $this->prepareNotifs());
  206. RPC::commit();
  207. }
  208. /*
  209. * Prepare a notification for incoming invitation
  210. * @return string
  211. */
  212. function prepareNotifInvitation($from) {
  213. $html .= '
  214. <li>
  215. <form id="acceptcontact">
  216. <p>'.$from.' '.t('wants to talk with you'). '</p>
  217. <a
  218. class="button color green icon add merged left "
  219. id="notifsvalidate"
  220. onclick="
  221. '.$this->genCallAjax("ajaxAddRoster", "'".$from."'").'
  222. setTimeout(function() {'.
  223. $this->genCallAjax("ajaxSubscribed", "'".$from."'").
  224. '}, 1000);
  225. setTimeout(function() {'.
  226. $this->genCallAjax("ajaxSubscribe", "'".$from."'").
  227. '}, 2000);
  228. ">'.
  229. t("Add").'
  230. </a><a
  231. class="button color red alone icon no merged right"
  232. onclick="'.$this->genCallAjax("ajaxRefuse", "'".$from."'").'">
  233. </a>
  234. </form>
  235. <div class="clear"></div>
  236. </li>';
  237. return $html;
  238. }
  239. function build()
  240. {
  241. ?>
  242. <div id="notifs">
  243. <?php echo $this->prepareNotifs(); ?>
  244. </div>
  245. <?php
  246. }
  247. }