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.

198 lines
5.7 KiB

  1. <?php
  2. use Moxl\Xec\Action\Pubsub\GetItems;
  3. class Menu extends \Movim\Widget\Base
  4. {
  5. private $_paging = 15;
  6. function load()
  7. {
  8. $this->registerEvent('post', 'onPost');
  9. $this->registerEvent('post_retract', 'onRetract');
  10. $this->registerEvent('pubsub_postdelete', 'onRetract');
  11. $this->registerEvent('pubsub_getitem_handle', 'onHandle');
  12. $this->addjs('menu.js');
  13. $this->addcss('menu.css');
  14. }
  15. function onHandle($packet)
  16. {
  17. if(is_array($packet->content)
  18. && isset($packet->content['nodeid'])) {
  19. $this->onRetract($packet);
  20. } else {
  21. $this->onPost($packet);
  22. }
  23. }
  24. function onRetract($packet)
  25. {
  26. $this->ajaxGetAll();
  27. }
  28. function onStream($count)
  29. {
  30. $view = $this->tpl();
  31. $view->assign('count', $count);
  32. $view->assign('refresh', $this->call('ajaxGetAll'));
  33. RPC::call('movim_posts_unread', $count);
  34. RPC::call('MovimTpl.fill', '#menu_refresh', $view->draw('_menu_refresh', true));
  35. }
  36. function onPost($packet)
  37. {
  38. $pd = new \Modl\PostnDAO;
  39. $since = \Movim\Cache::c('since');
  40. $count = $pd->getCountSince($since);
  41. $post = $packet->content;
  42. if($count > 0
  43. && (strtotime($post->published) > strtotime($since))) {
  44. if($post->isMicroblog()) {
  45. $cd = new \Modl\ContactDAO;
  46. $contact = $cd->get($post->origin);
  47. if($post->title == null) {
  48. $title = __('post.default_title');
  49. } else {
  50. $title = $post->title;
  51. }
  52. if(!$post->isMine())
  53. Notification::append(
  54. 'news',
  55. $contact->getTrueName(),
  56. $title,
  57. $contact->getPhoto('s'),
  58. 2,
  59. $this->route('news', $post->nodeid)
  60. );
  61. } else {
  62. $logo = ($post->logo) ? $post->getLogo() : null;
  63. Notification::append(
  64. 'news',
  65. $post->title,
  66. $post->node,
  67. $logo,
  68. 2,
  69. $this->route('news', $post->nodeid)
  70. );
  71. }
  72. $this->onStream($count);
  73. }
  74. }
  75. function ajaxGetAll($page = 0)
  76. {
  77. $this->ajaxGet('all', null, null, $page);
  78. }
  79. function ajaxGetNews($page = 0)
  80. {
  81. $this->ajaxGet('news', null, null, $page);
  82. }
  83. function ajaxGetFeed($page = 0)
  84. {
  85. $this->ajaxGet('feed', null, null, $page);
  86. }
  87. function ajaxGetNode($server = null, $node = null, $page = 0)
  88. {
  89. $this->ajaxGet('node', $server, $node, $page);
  90. }
  91. function ajaxGetMe($page = 0)
  92. {
  93. $this->ajaxGet('me', null, null, $page);
  94. }
  95. function ajaxGet($type = 'all', $server = null, $node = null, $page = 0)
  96. {
  97. $html = $this->prepareList($type, $server, $node, $page);
  98. if($page > 0) {
  99. RPC::call('MovimTpl.append', '#menu_wrapper', $html);
  100. } else {
  101. RPC::call('MovimTpl.fill', '#menu_widget', $html);
  102. RPC::call('movim_posts_unread', 0);
  103. }
  104. RPC::call('Menu.refresh');
  105. //RPC::call('MovimTpl.scrollTop');
  106. }
  107. function ajaxRefresh()
  108. {
  109. Notification::append(null, $this->__('menu.refresh'));
  110. $sd = new \modl\SubscriptionDAO();
  111. $subscriptions = $sd->getSubscribed();
  112. foreach($subscriptions as $s) {
  113. $r = new GetItems;
  114. $r->setTo($s->server)
  115. ->setNode($s->node)
  116. ->request();
  117. }
  118. }
  119. function prepareList($type = 'all', $server = null, $node = null, $page = 0) {
  120. $view = $this->tpl();
  121. $pd = new \Modl\PostnDAO;
  122. $count = $pd->getCountSince(\Movim\Cache::c('since'));
  123. // getting newer, not older
  124. if($page == 0 || $page == ""){
  125. $count = 0;
  126. \Movim\Cache::c('since', date(DATE_ISO8601, strtotime($pd->getLastDate())));
  127. }
  128. $next = $page + 1;
  129. switch($type) {
  130. case 'all' :
  131. $view->assign('history', $this->call('ajaxGetAll', $next));
  132. $items = $pd->getAllPosts(false, $page * $this->_paging + $count, $this->_paging);
  133. break;
  134. case 'news' :
  135. $view->assign('history', $this->call('ajaxGetNews', $next));
  136. $items = $pd->getNews($page * $this->_paging + $count, $this->_paging);
  137. break;
  138. case 'feed' :
  139. $view->assign('history', $this->call('ajaxGetFeed', $next));
  140. $items = $pd->getFeed($page * $this->_paging + $count, $this->_paging);
  141. break;
  142. case 'me' :
  143. $view->assign('history', $this->call('ajaxGetMe', $next));
  144. $items = $pd->getMe($page * $this->_paging + $count, $this->_paging);
  145. break;
  146. case 'node' :
  147. $view->assign('history', $this->call('ajaxGetNode', '"'.$server.'"', '"'.$node.'"', '"'.$next.'"'));
  148. $items = $pd->getNode($server, $node, $page * $this->_paging + $count, $this->_paging);
  149. break;
  150. }
  151. $view->assign('items', $items);
  152. $view->assign('type', $type);
  153. $view->assign('page', $page);
  154. $view->assign('paging', $this->_paging);
  155. $html = $view->draw('_menu_list', true);
  156. if($page == 0 || $page == ""){
  157. $view = $this->tpl();
  158. $view->assign('to', $this->user->getLogin());
  159. $html .= $view->draw('_menu_add', true);
  160. }
  161. return $html;
  162. }
  163. function display()
  164. {
  165. }
  166. }