PageRenderTime 36ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/src/plugins/plg_kunena_community/activity.php

https://github.com/xillibit/Kunena-forum
PHP | 304 lines | 182 code | 39 blank | 83 comment | 31 complexity | 07e8dd6b37cda1ca04a70a478169487d MD5 | raw file
  1. <?php
  2. /**
  3. * Kunena Plugin
  4. *
  5. * @package Kunena.Plugins
  6. * @subpackage Community
  7. *
  8. * @copyright (C) 2008 - 2020 Kunena Team. All rights reserved.
  9. * @copyright (C) 2013 - 2014 iJoomla, Inc. All rights reserved.
  10. * @license https://www.gnu.org/copyleft/gpl.html GNU/GPL
  11. * @link https://www.kunena.org
  12. **/
  13. defined('_JEXEC') or die();
  14. use Joomla\CMS\Language\Text;
  15. use Joomla\CMS\Uri\Uri;
  16. /**
  17. * Class KunenaActivityCommunity
  18. *
  19. * @since Kunena
  20. */
  21. class KunenaActivityCommunity extends KunenaActivity
  22. {
  23. /**
  24. * @var null
  25. * @since Kunena
  26. */
  27. protected $params = null;
  28. /**
  29. * KunenaActivityCommunity constructor.
  30. *
  31. * @param $params
  32. *
  33. * @since Kunena
  34. */
  35. public function __construct($params)
  36. {
  37. $this->params = $params;
  38. }
  39. /**
  40. * @param $message
  41. *
  42. * @since Kunena
  43. * @throws Exception
  44. */
  45. public function onAfterPost($message)
  46. {
  47. if (Joomla\String\StringHelper::strlen($message->message) > $this->params->get('activity_points_limit', 0))
  48. {
  49. CFactory::load('libraries', 'userpoints');
  50. CUserPoints::assignPoint('com_kunena.thread.new');
  51. }
  52. $act = new stdClass;
  53. $act->cmd = 'wall.write';
  54. $act->actor = $message->userid;
  55. $act->target = 0;
  56. $act->title = Text::_(
  57. '{actor} ' . Text::sprintf(
  58. 'PLG_KUNENA_COMMUNITY_ACTIVITY_POST_TITLE',
  59. ' <a href="' . $message->getTopic()->getUrl() . '">' . $message->displayField('subject') . '</a>'
  60. )
  61. );
  62. $act->content = $this->buildContent($message);
  63. $act->app = 'kunena.thread.post';
  64. $act->cid = $message->thread;
  65. $act->access = $this->getAccess($message->getCategory());
  66. // Comments and like support
  67. $act->comment_id = $message->thread;
  68. $act->comment_type = 'kunena.thread.post';
  69. $act->like_id = $message->thread;
  70. $act->like_type = 'kunena.thread.post';
  71. // Do not add private activities
  72. if ($act->access > 20)
  73. {
  74. return;
  75. }
  76. CFactory::load('libraries', 'activities');
  77. $table = CActivityStream::add($act);
  78. if (is_object($table))
  79. {
  80. $table->like_id = $table->id;
  81. $table->store();
  82. }
  83. }
  84. /**
  85. * @param $message
  86. *
  87. * @return mixed|string|void
  88. * @since Kunena
  89. * @throws Exception
  90. */
  91. private function buildContent($message)
  92. {
  93. $parent = new stdClass;
  94. $parent->forceSecure = true;
  95. $parent->forceMinimal = true;
  96. $content = KunenaHtmlParser::parseBBCode($message->message, $parent, $this->params->get('activity_stream_limit', 0));
  97. // Add readmore permalink
  98. $content .= '<br/><br /><a rel="nofollow" href="' . $message->getPermaUrl() . '" class="small profile-newsfeed-item-action">' . Text::_('COM_KUNENA_READMORE') . '</a>';
  99. return $content;
  100. }
  101. /**
  102. * @param $category
  103. *
  104. * @return integer
  105. * @since Kunena
  106. */
  107. protected function getAccess($category)
  108. {
  109. // Activity access level: 0 = public, 20 = registered, 30 = friend, 40 = private
  110. $accesstype = $category->accesstype;
  111. if ($accesstype != 'joomla.group' && $accesstype != 'joomla.level')
  112. {
  113. // Private
  114. return 40;
  115. }
  116. // FIXME: Joomla 2.5 can mix up groups and access levels
  117. if (($accesstype == 'joomla.level' && $category->access == 1)
  118. || ($accesstype == 'joomla.group' && ($category->pub_access == 1 || $category->admin_access == 1))
  119. )
  120. {
  121. // Public
  122. $access = 0;
  123. }
  124. elseif (($accesstype == 'joomla.level' && $category->access == 2)
  125. || ($accesstype == 'joomla.group' && ($category->pub_access == 2 || $category->admin_access == 2))
  126. )
  127. {
  128. // Registered
  129. $access = 20;
  130. }
  131. else
  132. {
  133. // Other groups (=private)
  134. $access = 40;
  135. }
  136. return $access;
  137. }
  138. /**
  139. * @param $message
  140. *
  141. * @since Kunena
  142. * @throws Exception
  143. */
  144. public function onAfterReply($message)
  145. {
  146. if (Joomla\String\StringHelper::strlen($message->message) > $this->params->get('activity_points_limit', 0))
  147. {
  148. CFactory::load('libraries', 'userpoints');
  149. CUserPoints::assignPoint('com_kunena.thread.reply');
  150. }
  151. // Get users who have subscribed to the topic, excluding current user.
  152. $acl = KunenaAccess::getInstance();
  153. $subscribers = $acl->getSubscribers(
  154. $message->catid, $message->thread, KunenaAccess::TOPIC_SUBSCRIPTION, false, false, array($message->userid)
  155. );
  156. foreach ($subscribers as $userid)
  157. {
  158. $actor = CFactory::getUser($message->userid);
  159. $target = CFactory::getUser($userid);
  160. $params = new CParameter('');
  161. $params->set('actorName', $actor->getDisplayName());
  162. $params->set('recipientName', $target->getDisplayName());
  163. $params->set('url', Uri::getInstance()->toString(array('scheme', 'host', 'port')) . $message->getPermaUrl(null)); // {url} tag for activity. Used when hovering over avatar in notification window, as well as in email notification
  164. $params->set('title', $message->displayField('subject')); // (title) tag in language file
  165. $params->set('title_url', $message->getPermaUrl()); // Make the title in notification - linkable
  166. $params->set('message', $message->displayField('message')); // (message) tag in language file
  167. $params->set('actor', $actor->getDisplayName()); // Actor in the stream
  168. $params->set('actor_url', 'index.php?option=com_community&view=profile&userid=' . $actor->id); // Actor Link
  169. // Finally, send notifications
  170. CNotificationLibrary::add('kunena_reply', $actor->id, $target->id, Text::sprintf('PLG_KUNENA_COMMUNITY_ACTIVITY_REPLY_TITLE_ACT'), Text::sprintf('PLG_KUNENA_COMMUNITY_ACTIVITY_REPLY_TEXT'), '', $params);
  171. }
  172. // Activity stream
  173. $act = new stdClass;
  174. $act->cmd = 'wall.write';
  175. $act->actor = $message->userid;
  176. $act->target = 0; // No target
  177. $act->title = Text::_('{single}{actor}{/single}{multiple}{actors}{/multiple} ' . Text::sprintf('PLG_KUNENA_COMMUNITY_ACTIVITY_REPLY_TITLE', '<a href="' . $message->getTopic()->getUrl() . '">' . $message->subject . '</a>'));
  178. $act->content = $this->buildContent($message);
  179. $act->app = 'kunena.thread.reply';
  180. $act->cid = $message->thread;
  181. $act->access = $this->getAccess($message->getCategory());
  182. // Comments and like support
  183. $act->comment_id = $message->thread;
  184. $act->comment_type = 'kunena.thread.reply';
  185. $act->like_id = $message->thread;
  186. $act->like_type = 'kunena.thread.reply';
  187. // Do not add private activities
  188. if ($act->access > 20)
  189. {
  190. return;
  191. }
  192. CFactory::load('libraries', 'activities');
  193. $table = CActivityStream::add($act);
  194. if (is_object($table))
  195. {
  196. $table->like_id = $table->id;
  197. $table->store();
  198. }
  199. }
  200. /**
  201. * @param int $actor actor
  202. * @param int $target target
  203. * @param int $message message
  204. *
  205. * @since Kunena
  206. */
  207. public function onAfterThankyou($actor, $target, $message)
  208. {
  209. CFactory::load('libraries', 'userpoints');
  210. CUserPoints::assignPoint('com_kunena.thread.thankyou', $target);
  211. $actor = CFactory::getUser($actor);
  212. $target = CFactory::getUser($target);
  213. // Create CParameter use for params
  214. $params = new CParameter('');
  215. $params->set('actorName', $actor->getDisplayName());
  216. $params->set('recipientName', $target->getDisplayName());
  217. $params->set('recipientUrl', 'index.php?option=com_community&view=profile&userid=' . $target->id); // Actor Link
  218. $params->set('url', Uri::getInstance()->toString(array('scheme', 'host', 'port')) . $message->getPermaUrl(null)); // {url} tag for activity. Used when hovering over avatar in notification window, as well as in email notification
  219. $params->set('title', $message->displayField('subject')); // (title) tag in language file
  220. $params->set('title_url', $message->getPermaUrl()); // Make the title in notification - linkable
  221. $params->set('message', $message->message); // (message) tag in language file
  222. $params->set('actor', $actor->getDisplayName()); // Actor in the stream
  223. $params->set('actor_url', 'index.php?option=com_community&view=profile&userid=' . $actor->id); // Actor Link
  224. // Finally, send notifications
  225. CNotificationLibrary::add('kunena_thankyou', $actor->id, $target->id, Text::sprintf('PLG_KUNENA_COMMUNITY_ACTIVITY_THANKYOU_TITLE_ACT'), Text::sprintf('PLG_KUNENA_COMMUNITY_ACTIVITY_THANKYOU_TEXT'), '', $params);
  226. $act = new stdClass;
  227. $act->cmd = 'wall.write';
  228. $act->actor = $actor->id;
  229. $act->target = $target->id;
  230. $act->title = Text::sprintf('PLG_KUNENA_COMMUNITY_ACTIVITY_THANKYOU_WALL', $params->get('actor_url'), $params->get('actor'), $params->get('recipientUrl'), $params->get('recipientName'), $params->get('url'), $params->get('title'));
  231. $act->content = null;
  232. $act->app = 'kunena.message.thankyou';
  233. $act->cid = $target->id;
  234. $act->access = $this->getAccess($message->getCategory());
  235. // Comments and like supports
  236. $act->comment_id = $target->id;
  237. $act->comment_type = 'kunena.message.thankyou';
  238. $act->like_id = $target->id;
  239. $act->like_type = 'kunena.message.thankyou';
  240. // Do not add private activities
  241. if ($act->access > 20)
  242. {
  243. return;
  244. }
  245. CFactory::load('libraries', 'activities');
  246. $table = CActivityStream::add($act);
  247. if (is_object($table))
  248. {
  249. $table->like_id = $table->id;
  250. $table->store();
  251. }
  252. }
  253. /**
  254. * @param $target
  255. *
  256. * @since Kunena
  257. */
  258. public function onAfterDeleteTopic($target)
  259. {
  260. CFactory::load('libraries', 'activities');
  261. CActivityStream::remove('kunena.thread.post', $target->id);
  262. // TODO: Need get replied id
  263. CActivityStream::remove('kunena.thread.replied', $target->id);
  264. }
  265. }