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.

416 lines
11 KiB

  1. <?php
  2. use Moxl\Xec\Action\Pubsub\GetItemsId;
  3. use Moxl\Xec\Action\Pubsub\GetMetadata;
  4. use Moxl\Xec\Action\Pubsub\GetAffiliations;
  5. use Moxl\Xec\Action\Pubsub\GetSubscriptions;
  6. use Moxl\Xec\Action\Pubsub\Subscribe;
  7. use Moxl\Xec\Action\Pubsub\Unsubscribe;
  8. use Moxl\Xec\Action\Pubsub\GetConfig;
  9. use Moxl\Xec\Action\Pubsub\SetConfig;
  10. use Moxl\Xec\Action\Pubsub\Delete;
  11. use Respect\Validation\Validator;
  12. class Group extends WidgetBase
  13. {
  14. private $_paging = 15;
  15. private $_role = null;
  16. function load()
  17. {
  18. $this->registerEvent('pubsub_getitem_handle', 'onItems', 'groups');
  19. $this->registerEvent('pubsub_getitems_handle', 'onItems', 'groups');
  20. $this->registerEvent('pubsub_getitemsid_handle', 'onItems', 'groups');
  21. $this->registerEvent('pubsub_getitems_error', 'onItemsError', 'groups');
  22. $this->registerEvent('pubsub_getitemsid_error', 'onItemsError', 'groups');
  23. $this->registerEvent('pubsub_subscribe_handle', 'onSubscribed');
  24. $this->registerEvent('pubsub_unsubscribe_handle', 'onUnsubscribed');
  25. $this->registerEvent('pubsub_getaffiliations_handle', 'onAffiliations');
  26. $this->registerEvent('pubsub_getsubscriptions_handle', 'onSubscriptions');
  27. $this->registerEvent('pubsub_delete_handle', 'onDelete');
  28. $this->registerEvent('pubsub_getconfig_handle', 'onConfig');
  29. $this->registerEvent('pubsub_setconfig_handle', 'onConfigSaved');
  30. $this->registerEvent('bookmark_set_handle', 'onBookmark');
  31. $this->addjs('group.js');
  32. }
  33. function onItems($packet)
  34. {
  35. list($server, $node) = array_values($packet->content);
  36. $this->displayItems($server, $node);
  37. RPC::call('Group.clearLoad');
  38. RPC::call('MovimTpl.showPanel');
  39. }
  40. function onDelete($packet)
  41. {
  42. $this->ajaxClear();
  43. }
  44. function onBookmark()
  45. {
  46. $this->ajaxClear();
  47. $g = new Groups;
  48. $g->ajaxHeader();
  49. $g->ajaxSubscriptions();
  50. }
  51. function onItemsError($packet)
  52. {
  53. list($server, $node) = array_values($packet->content);
  54. Notification::append(false, $this->__('group.empty'));
  55. $this->ajaxDelete($server, $node, true);
  56. $this->ajaxGetAffiliations($server, $node);
  57. // Display an error message
  58. RPC::call('Group.clearLoad');
  59. }
  60. function onAffiliations($packet)
  61. {
  62. list($affiliations, $server, $node) = array_values($packet->content);
  63. foreach($affiliations as $r) {
  64. if($r[0] == $this->user->getLogin())
  65. $this->_role = (string)$r[1];
  66. }
  67. Header::fill($this->prepareHeader($server, $node));
  68. //if(isset($this->_role)
  69. //&& ($this->_role == 'owner' || $this->_role == 'publisher')) {
  70. // $view = $this->tpl();
  71. // $view->assign('server', $server);
  72. // $view->assign('node', $node);
  73. // RPC::call('movim_append', 'group_widget', $view->draw('_group_publish', true));
  74. //}
  75. }
  76. function onSubscriptions($packet)
  77. {
  78. list($subscriptions, $server, $node) = array_values($packet->content);
  79. $view = $this->tpl();
  80. $view->assign('subscriptions', $subscriptions);
  81. $view->assign('server', $server);
  82. $view->assign('node', $node);
  83. Dialog::fill($view->draw('_group_subscriptions', true), true);
  84. }
  85. function onConfig($packet)
  86. {
  87. list($config, $server, $node) = array_values($packet->content);
  88. $view = $this->tpl();
  89. $xml = new \XMPPtoForm();
  90. $form = $xml->getHTML($config->x->asXML());
  91. $view->assign('form', $form);
  92. $view->assign('server', $server);
  93. $view->assign('node', $node);
  94. $view->assign('attributes', $config->attributes());
  95. Dialog::fill($view->draw('_group_config', true), true);
  96. }
  97. function onConfigSaved()
  98. {
  99. Notification::append(false, $this->__('group.config_saved'));
  100. }
  101. function onSubscribed($packet)
  102. {
  103. $arr = $packet->content;
  104. // Set the bookmark
  105. $r = new Rooms;
  106. $r->setBookmark();
  107. Notification::append(null, $this->__('group.subscribed'));
  108. // Set the public list
  109. /*
  110. //add the group to the public list (if checked)
  111. if($this->_data['listgroup'] == true){
  112. $add = new ListAdd();
  113. $add->setTo($this->_to)
  114. ->setNode($this->_node)
  115. ->setFrom($this->_from)
  116. ->setData($this->_data)
  117. ->request();
  118. }
  119. }*/
  120. }
  121. function onUnsubscribed($packet)
  122. {
  123. $arr = $packet->content;
  124. // Set the bookmark
  125. $r = new Rooms;
  126. $r->setBookmark();
  127. Notification::append(null, $this->__('group.unsubscribed'));
  128. }
  129. private function displayItems($server, $node)
  130. {
  131. if(!$this->validateServerNode($server, $node)) return;
  132. $html = $this->prepareGroup($server, $node);
  133. $view = $this->tpl();
  134. $view->assign('server', $server);
  135. $view->assign('node', $node);
  136. $html .= $view->draw('_group_publish', true);
  137. $header = $this->prepareHeader($server, $node);
  138. Header::fill($header);
  139. RPC::call('MovimTpl.fill', '#group_widget.'.stringToUri($server.'_'.$node), $html);
  140. RPC::call('Group.enableVideos');
  141. }
  142. function ajaxDelete($server, $node, $clean = false)
  143. {
  144. if(!$this->validateServerNode($server, $node)) return;
  145. $view = $this->tpl();
  146. $view->assign('server', $server);
  147. $view->assign('node', $node);
  148. $view->assign('clean', $clean);
  149. Dialog::fill($view->draw('_group_delete', true));
  150. }
  151. function ajaxDeleteConfirm($server, $node)
  152. {
  153. if(!$this->validateServerNode($server, $node)) return;
  154. $d = new Delete;
  155. $d->setTo($server)->setNode($node)
  156. ->request();
  157. }
  158. function ajaxGetMetadata($server, $node)
  159. {
  160. if(!$this->validateServerNode($server, $node)) return;
  161. $r = new GetMetadata;
  162. $r->setTo($server)->setNode($node)
  163. ->request();
  164. }
  165. function ajaxGetConfig($server, $node){
  166. if(!$this->validateServerNode($server, $node)) return;
  167. $r = new GetConfig;
  168. $r->setTo($server)
  169. ->setNode($node)
  170. ->request();
  171. }
  172. function ajaxSetConfig($data, $server, $node){
  173. if(!$this->validateServerNode($server, $node)) return;
  174. $r = new SetConfig;
  175. $r->setTo($server)
  176. ->setNode($node)
  177. ->setData($data)
  178. ->request();
  179. }
  180. function ajaxGetItems($server, $node)
  181. {
  182. if(!$this->validateServerNode($server, $node)) return;
  183. RPC::call('Group.addLoad', stringToUri($server.'_'.$node));
  184. $r = new GetItemsId;
  185. $r->setTo($server)
  186. ->setNode($node);
  187. $r->request();
  188. }
  189. function ajaxGetHistory($server, $node, $page)
  190. {
  191. $html = $this->prepareGroup($server, $node, $page);
  192. RPC::call('movim_append', 'group_widget', $html);
  193. RPC::call('Group.enableVideos');
  194. }
  195. function ajaxGetAffiliations($server, $node){
  196. if(!$this->validateServerNode($server, $node)) return;
  197. $r = new GetAffiliations;
  198. $r->setTo($server)->setNode($node)
  199. ->request();
  200. }
  201. function ajaxGetSubscriptions($server, $node)
  202. {
  203. if(!$this->validateServerNode($server, $node)) return;
  204. $r = new GetSubscriptions;
  205. $r->setTo($server)
  206. ->setNode($node)
  207. ->setSync()
  208. ->request();
  209. }
  210. function ajaxAskSubscribe($server, $node)
  211. {
  212. if(!$this->validateServerNode($server, $node)) return;
  213. $view = $this->tpl();
  214. $view->assign('server', $server);
  215. $view->assign('node', $node);
  216. $pd = new \Modl\ItemDAO;
  217. $item = $pd->getItem($server, $node);
  218. if(isset($item)) {
  219. $view->assign('item', $item);
  220. } else {
  221. $view->assign('item', null);
  222. }
  223. Dialog::fill($view->draw('_group_subscribe', true));
  224. }
  225. function ajaxSubscribe($form, $server, $node)
  226. {
  227. if(!$this->validateServerNode($server, $node)) return;
  228. $g = new Subscribe;
  229. $g->setTo($server)
  230. ->setNode($node)
  231. ->setFrom($this->user->getLogin())
  232. ->setData($form)
  233. ->request();
  234. }
  235. function ajaxAskUnsubscribe($server, $node)
  236. {
  237. if(!$this->validateServerNode($server, $node)) return;
  238. $view = $this->tpl();
  239. $view->assign('server', $server);
  240. $view->assign('node', $node);
  241. $pd = new \Modl\ItemDAO;
  242. $item = $pd->getItem($server, $node);
  243. if(isset($item)) {
  244. $view->assign('item', $item);
  245. } else {
  246. $view->assign('item', null);
  247. }
  248. Dialog::fill($view->draw('_group_unsubscribe', true));
  249. }
  250. function ajaxUnsubscribe($server, $node)
  251. {
  252. if(!$this->validateServerNode($server, $node)) return;
  253. $sd = new \Modl\SubscriptionDAO();
  254. foreach($sd->get($server, $node) as $s) {
  255. $g = new Unsubscribe;
  256. $g->setTo($server)
  257. ->setNode($node)
  258. ->setSubid($s->subid)
  259. ->setFrom($this->user->getLogin())
  260. ->request();
  261. }
  262. }
  263. function ajaxClear()
  264. {
  265. $html = $this->prepareEmpty();
  266. RPC::call('movim_fill', 'group_widget', $html);
  267. }
  268. function prepareEmpty()
  269. {
  270. $id = new \modl\ItemDAO();
  271. $view = $this->tpl();
  272. $view->assign('servers', $id->getGroupServers());
  273. $html = $view->draw('_group_empty', true);
  274. return $html;
  275. }
  276. private function prepareHeader($server, $node)
  277. {
  278. $pd = new \Modl\ItemDAO;
  279. $item = $pd->getItem($server, $node);
  280. $pd = new \Modl\SubscriptionDAO;
  281. $subscription = $pd->get($server, $node);
  282. $view = $this->tpl();
  283. $view->assign('item', $item);
  284. $view->assign('subscription', $subscription);
  285. $view->assign('role', $this->_role);
  286. return $view->draw('_group_header', true);
  287. }
  288. private function prepareGroup($server, $node, $page = 0)
  289. {
  290. $pd = new \Modl\PostnDAO();
  291. $posts = $pd->getNodeUnfiltered($server, $node, $page*$this->_paging, $this->_paging);
  292. $view = $this->tpl();
  293. $view->assign('server', $server);
  294. $view->assign('node', $node);
  295. $view->assign('page', $page);
  296. $view->assign('posts', $posts);
  297. $html = $view->draw('_group_posts', true);
  298. return $html;
  299. }
  300. private function validateServerNode($server, $node)
  301. {
  302. $validate_server = Validator::string()->noWhitespace()->length(6, 40);
  303. $validate_node = Validator::string()->length(3, 100);
  304. if(!$validate_server->validate($server)
  305. || !$validate_node->validate($node)
  306. ) return false;
  307. else return true;
  308. }
  309. function display()
  310. {
  311. $this->view->assign('server', false);
  312. $this->view->assign('node', false);
  313. if($this->validateServerNode($this->get('s'), $this->get('n'))) {
  314. $this->view->assign('server', $this->get('s'));
  315. $this->view->assign('node', $this->get('n'));
  316. }
  317. }
  318. }