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.

213 lines
5.8 KiB

  1. <?php
  2. /**
  3. * @package Widgets
  4. *
  5. * @file Post.php
  6. * This file is part of Movim.
  7. *
  8. * @brief The Post visualisation widget
  9. *
  10. * @author Jaussoin Timothée <edhelas_at_movim_dot_com>
  11. *
  12. * @version 1.0
  13. * @date 1 december 2014
  14. *
  15. * Copyright (C)2014 MOVIM project
  16. *
  17. * See COPYING for licensing information.
  18. */
  19. use Moxl\Xec\Action\Pubsub\PostPublish;
  20. use Moxl\Xec\Action\Pubsub\TestPostPublish;
  21. use Moxl\Xec\Action\Microblog\CommentCreateNode;
  22. use \Michelf\Markdown;
  23. use Respect\Validation\Validator;
  24. class Publish extends WidgetBase
  25. {
  26. function load()
  27. {
  28. $this->addjs('publish.js');
  29. $this->registerEvent('pubsub_postpublish_handle', 'onPublish');
  30. $this->registerEvent('pubsub_testpostpublish_handle', 'onTestPublish');
  31. $this->registerEvent('pubsub_testpostpublish_error', 'onTestPublishError');
  32. }
  33. function onPublish($packet)
  34. {
  35. list($to, $node, $id) = array_values($packet->content);
  36. RPC::call('Publish.enableSend');
  37. // Only for the microblog for the moment
  38. if($node == 'urn:xmpp:microblog:0') {
  39. $cn = new CommentCreateNode;
  40. $cn->setTo($to)
  41. ->setParentId($id)
  42. ->request();
  43. }
  44. }
  45. function onTestPublish($packet)
  46. {
  47. list($server, $node) = array_values($packet->content);
  48. $this->ajaxCreate($server, $node);
  49. }
  50. function onTestPublishError($packet)
  51. {
  52. Notification::append(null, $this->__('publish.no_publication'));
  53. }
  54. function ajaxCreateBlog()
  55. {
  56. $this->ajaxCreate($this->user->getLogin(), 'urn:xmpp:microblog:0');
  57. }
  58. function ajaxCreate($server, $node)
  59. {
  60. if(!$this->validateServerNode($server, $node)) return;
  61. $view = $this->tpl();
  62. $view->assign('to', $server);
  63. $view->assign('node', $node);
  64. RPC::call('MovimTpl.fill', 'main section > div:nth-child(2)', $view->draw('_publish_create', true));
  65. $pd = new \Modl\ItemDAO;
  66. $item = $pd->getItem($server, $node);
  67. $view = $this->tpl();
  68. $view->assign('item', $item);
  69. $view->assign('server', $server);
  70. $view->assign('node', $node);
  71. Header::fill($view->draw('_publish_header', true));
  72. RPC::call('Publish.setEmbed');
  73. }
  74. function ajaxFormFilled($server, $node)
  75. {
  76. $view = $this->tpl();
  77. $view->assign('server', $server);
  78. $view->assign('node', $node);
  79. Dialog::fill($view->draw('_publish_back_confirm', true));
  80. }
  81. function ajaxPreview($form)
  82. {
  83. if($form->content->value != '') {
  84. $view = $this->tpl();
  85. $view->assign('content', Markdown::defaultTransform($form->content->value));
  86. Dialog::fill($view->draw('_publish_preview', true), true);
  87. } else {
  88. Notification::append(false, $this->__('publish.no_content_preview'));
  89. }
  90. }
  91. function ajaxHelp()
  92. {
  93. $view = $this->tpl();
  94. Dialog::fill($view->draw('_publish_help', true), true);
  95. }
  96. /*
  97. * Sic, doing this hack and wait to have a proper way to test it in the standard
  98. */
  99. function ajaxTestPublish($server, $node)
  100. {
  101. if(!$this->validateServerNode($server, $node)) return;
  102. $t = new TestPostPublish;
  103. $t->setTo($server)
  104. ->setNode($node)
  105. ->request();
  106. }
  107. function ajaxPublish($form)
  108. {
  109. RPC::call('Publish.disableSend');
  110. if($form->content->value != '') {
  111. $content = Markdown::defaultTransform($form->content->value);
  112. $p = new PostPublish;
  113. $p->setFrom($this->user->getLogin())
  114. ->setTo($form->to->value)
  115. ->setNode($form->node->value);
  116. //->setLocation($geo)
  117. //->enableComments()
  118. if($form->title->value != '') {
  119. $p->setTitle($form->title->value);
  120. }
  121. if($form->embed->value != '' && filter_var($form->embed->value, FILTER_VALIDATE_URL)) {
  122. $embed = Embed\Embed::create($form->embed->value);
  123. $content .= $this->prepareEmbed($embed);
  124. $p->setLink($form->embed->value);
  125. if($embed->type == 'photo') {
  126. $key = key($embed->images);
  127. $p->setImage($embed->images[0]['value'], $embed->title, $embed->images[0]['mime']);
  128. }
  129. }
  130. $p->setContentHtml(rawurldecode($content))
  131. ->request();
  132. } else {
  133. RPC::call('Publish.enableSend');
  134. Notification::append(false, $this->__('publish.no_content'));
  135. }
  136. }
  137. function ajaxEmbedTest($url)
  138. {
  139. if($url == '') {
  140. return;
  141. } elseif(!filter_var($url, FILTER_VALIDATE_URL)) {
  142. Notification::append(false, $this->__('publish.valid_url'));
  143. return;
  144. }
  145. $embed = Embed\Embed::create($url);
  146. $html = $this->prepareEmbed($embed);
  147. if($embed->type == 'photo') {
  148. RPC::call('movim_fill', 'gallery', $this->prepareGallery($embed));
  149. }
  150. RPC::call('movim_fill', 'preview', $html);
  151. }
  152. function prepareEmbed($embed)
  153. {
  154. $view = $this->tpl();
  155. $view->assign('embed', $embed);
  156. return $view->draw('_publish_embed', true);
  157. }
  158. function prepareGallery($embed)
  159. {
  160. $view = $this->tpl();
  161. $view->assign('embed', $embed);
  162. return $view->draw('_publish_gallery', true);
  163. }
  164. private function validateServerNode($server, $node)
  165. {
  166. $validate_server = Validator::string()->noWhitespace()->length(6, 40);
  167. $validate_node = Validator::string()->length(3, 100);
  168. if(!$validate_server->validate($server)
  169. || !$validate_node->validate($node)
  170. ) return false;
  171. else return true;
  172. }
  173. function display()
  174. {
  175. }
  176. }