PageRenderTime 54ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/components/com_easyblog/helpers/event.php

https://bitbucket.org/pastor399/newcastleunifc
PHP | 418 lines | 291 code | 85 blank | 42 comment | 28 complexity | 32ae749384ed0ac9635eac6fa3ddf658 MD5 | raw file
  1. <?php
  2. /**
  3. * @package EasyBlog
  4. * @copyright Copyright (C) 2010 Stack Ideas Private Limited. All rights reserved.
  5. * @license GNU/GPL, see LICENSE.php
  6. *
  7. * EasyBlog is free software. This version may have been modified pursuant
  8. * to the GNU General Public License, and as distributed it includes or
  9. * is derivative of works licensed under the GNU General Public License or
  10. * other free or open source software licenses.
  11. * See COPYRIGHT.php for copyright notices and details.
  12. */
  13. defined('_JEXEC') or die('Restricted access');
  14. require_once( EBLOG_HELPERS . DIRECTORY_SEPARATOR .'helper.php' );
  15. jimport( 'joomla.filesystem.file' );
  16. class EasyBlogEventHelper
  17. {
  18. public function isEnabled()
  19. {
  20. // TODO: Some checking required here
  21. // Since we only support jomsocial now, load up their form
  22. if( JPluginHelper::isEnabled( 'system' , 'eventeasyblog' ) && $this->testExists( 'jomsocial' ) )
  23. {
  24. return true;
  25. }
  26. return false;
  27. }
  28. /**
  29. * Returns the group form html
  30. */
  31. public function getFormHTML( $uid = '0' , $blogSource = '')
  32. {
  33. $contents = '';
  34. // TODO: Check whether to load groupjive,jomsocial or any other group collaboration tools here.
  35. // Since we only support jomsocial now, load up their form
  36. if( JPluginHelper::isEnabled( 'system' , 'eventeasyblog' ) && $this->testExists( 'jomsocial' ) )
  37. {
  38. $contents = $this->jomsocialForm( $uid , $blogSource );
  39. }
  40. return $contents;
  41. }
  42. public function testExists( $source )
  43. {
  44. switch( $source )
  45. {
  46. case 'jomsocial':
  47. return JFile::exists( JPATH_ROOT . DIRECTORY_SEPARATOR . 'components' . DIRECTORY_SEPARATOR . 'com_community' . DIRECTORY_SEPARATOR . 'libraries' . DIRECTORY_SEPARATOR . 'core.php' );
  48. break;
  49. }
  50. }
  51. public function getSourceType()
  52. {
  53. $source = 'jomsocial';
  54. return $source;
  55. }
  56. /**
  57. * This is triggered when a blog post is removed from the site.
  58. *
  59. * @param int $postId
  60. */
  61. public function deleteContribution( $postId )
  62. {
  63. $db = EasyBlogHelper::db();
  64. $query = 'DELETE FROM ' . $db->nameQuote( '#__easyblog_external' ) . ' '
  65. . 'WHERE ' . $db->nameQuote( 'post_id' ) . ' = ' . $db->Quote( $postId );
  66. $db->setQuery( $query );
  67. $db->Query();
  68. return true;
  69. }
  70. public function updateContribution( $postId , $ids , $source )
  71. {
  72. // Maybe post was being updated and the user changed the group contributions.
  73. // Delete any existing post relations.
  74. $this->deleteContribution( $postId );
  75. $table = EasyBlogHelper::getTable( 'External' );
  76. if( !is_array( $ids ) )
  77. {
  78. $ids = array( $ids );
  79. }
  80. foreach( $ids as $id )
  81. {
  82. $table->set( 'post_id' , $postId );
  83. $table->set( 'source' , $source );
  84. $table->set( 'uid' , $id );
  85. }
  86. return $table->store();
  87. }
  88. private function jomsocialForm( $uid = '0' , $blogSource )
  89. {
  90. $my = JFactory::getUser();
  91. $file = JPATH_ROOT . DIRECTORY_SEPARATOR . 'components' . DIRECTORY_SEPARATOR . 'com_community' . DIRECTORY_SEPARATOR . 'libraries' . DIRECTORY_SEPARATOR . 'core.php';
  92. if( !JFile::exists( $file ) )
  93. {
  94. return false;
  95. }
  96. require_once( $file );
  97. $model = CFactory::getModel( 'Events' );
  98. $rows = $model->getEvents( null , $my->id , null , null , false , false , null , null , CEventHelper::ALL_TYPES , 0 , 999999 );
  99. $events = array();
  100. JTable::addIncludePath( JPATH_ROOT . DIRECTORY_SEPARATOR . 'components' . DIRECTORY_SEPARATOR . 'com_community' . DIRECTORY_SEPARATOR . 'tables' );
  101. foreach( $rows as $row )
  102. {
  103. $event = JTable::getInstance( 'Event' , 'CTable' );
  104. $event->load( $row->id );
  105. $data = new stdClass();
  106. $data->id = $event->id;
  107. $data->title = $event->title;
  108. $data->avatar = $event->getAvatar();
  109. $events[] = $data;
  110. }
  111. $theme = new CodeThemes( 'dashboard' );
  112. $theme->set( 'blogSource' , $blogSource );
  113. $theme->set( 'external' , $uid );
  114. $theme->set( 'selectedEvent', $uid );
  115. $theme->set( 'events' , $events );
  116. return $theme->fetch( 'dashboard.write.events.jomsocial.php' );
  117. }
  118. public function addCommentStream( $blog , $comment , $external )
  119. {
  120. return $this->addCommentStreamJomsocial( $blog , $comment , $external );
  121. }
  122. /**
  123. * Creates a stream item for the respective 3rd party plugin
  124. *
  125. * @param TableBlog $blog
  126. */
  127. public function addStream( $blog , $isNew , $key , $source )
  128. {
  129. // Since we only support jomsocial now, load up their form
  130. return $this->addStreamJomsocial( $blog , $isNew , $key , $source );
  131. }
  132. /**
  133. * Sends a notification item for the respective 3rd party plugins
  134. */
  135. public function sendNotifications( $blog , $isNew , $key , $source , $author )
  136. {
  137. return $this->sendNotificationsJomsocial( $blog , $isNew , $key , $source , $author );
  138. }
  139. private function sendNotificationsJomsocial( $blog , $isNew , $uid , $source , $author )
  140. {
  141. JFactory::getLanguage()->load( 'com_easyblog' , JPATH_ROOT );
  142. $config = EasyBlogHelper::getConfig();
  143. // @rule: Send email notifications out to subscribers.
  144. $author = EasyBlogHelper::getTable( 'Profile' );
  145. $author->load( $blog->created_by );
  146. $data[ 'blogTitle'] = $blog->title;
  147. $data[ 'blogAuthor'] = $author->getName();
  148. $data[ 'blogAuthorAvatar' ] = $author->getAvatar();
  149. $data[ 'blogAuthorLink' ] = EasyBlogRouter::getRoutedURL( 'index.php?option=com_easyblog&view=blogger&layout=listings&id=' . $author->id , false , true );
  150. $data[ 'blogAuthorEmail' ] = $author->user->email;
  151. $data[ 'blogIntro' ] = $blog->intro;
  152. $data[ 'blogContent' ] = $blog->content;
  153. $data[ 'blogLink' ] = EasyBlogRouter::getRoutedURL( 'index.php?option=com_easyblog&view=entry&id='. $blog->id, false, true);
  154. $date = EasyBlogDateHelper::dateWithOffSet( $blog->created );
  155. $data[ 'blogDate' ] = EasyBlogDateHelper::toFormat( $date , '%A, %B %e, %Y' );
  156. // If blog post is being posted from the back end and SH404 is installed, we should just use the raw urls.
  157. $sh404exists = EasyBlogRouter::isSh404Enabled();
  158. if( JFactory::getApplication()->isAdmin() && $sh404exists )
  159. {
  160. $data[ 'blogLink' ] = JURI::root() . 'index.php?option=com_easyblog&view=entry&id=' . $blog->id;
  161. $data[ 'blogAuthorLink' ] = JURI::root() . 'index.php?option=com_easyblog&view=blogger&layout=listings&id=' . $author->id;
  162. }
  163. if( is_array($uid) )
  164. {
  165. $uid = $uid[0];
  166. }
  167. JTable::addIncludePath( JPATH_ROOT . DIRECTORY_SEPARATOR . 'components' . DIRECTORY_SEPARATOR . 'com_community' . DIRECTORY_SEPARATOR . 'tables' );
  168. $event = JTable::getInstance( 'Event' , 'CTable' );
  169. $event->load( $uid );
  170. $members = $event->getMembers( 1 , 99999 );
  171. $emails = array();
  172. $jsCoreFile = JPATH_ROOT . DIRECTORY_SEPARATOR . 'components' . DIRECTORY_SEPARATOR . 'com_community' . DIRECTORY_SEPARATOR . 'libraries' . DIRECTORY_SEPARATOR . 'core.php';
  173. foreach( $members as $member )
  174. {
  175. $user = CFactory::getUser( $member->id );
  176. // Do not send email to the author.
  177. if( $author->user->email != $user->email )
  178. {
  179. $obj = new stdClass();
  180. $obj->email = $user->email;
  181. $emails[] = $obj;
  182. }
  183. }
  184. $notification = EasyBlogHelper::getHelper( 'Notification' );
  185. $emailBlogTitle = JString::substr( $blog->title , 0 , $config->get( 'main_mailtitle_length' ) );
  186. $emailTitle = JText::sprintf( 'COM_EASYBLOG_EMAIL_TITLE_NEW_BLOG_ADDED_WITH_TITLE' , $emailBlogTitle ) . ' ...';
  187. $notification->send( $emails , $emailTitle , 'email.blog.new' , $data );
  188. }
  189. private function addCommentStreamJomsocial( $blog , $comment , $external )
  190. {
  191. $jsCoreFile = JPATH_ROOT . DIRECTORY_SEPARATOR . 'components' . DIRECTORY_SEPARATOR . 'com_community' . DIRECTORY_SEPARATOR . 'libraries' . DIRECTORY_SEPARATOR . 'core.php';
  192. $config = EasyBlogHelper::getConfig();
  193. JFactory::getLanguage()->load( 'com_easyblog' , JPATH_ROOT );
  194. // We do not want to add activities if new blog activity is disabled.
  195. if( !$config->get( 'integrations_jomsocial_comment_new_activity' ) )
  196. {
  197. return false;
  198. }
  199. if( !JFile::exists( $jsCoreFile ) )
  200. {
  201. return false;
  202. }
  203. require_once( $jsCoreFile );
  204. JTable::addIncludePath( JPATH_ROOT . DIRECTORY_SEPARATOR . 'components' . DIRECTORY_SEPARATOR . 'com_community' . DIRECTORY_SEPARATOR . 'tables' );
  205. $event = JTable::getInstance( 'Group' , 'CTable' );
  206. $event->load( $external->uid );
  207. $config = EasyBlogHelper::getConfig();
  208. $command = 'easyblog.comment.add';
  209. $blogTitle = JString::substr( $blog->title , 0 , 30 ) . '...';
  210. $blogLink = EasyBlogRouter::getRoutedURL('index.php?option=com_easyblog&view=entry&id='. $comment->post_id, false, true);
  211. $content = '';
  212. if($config->get('integrations_jomsocial_submit_content'))
  213. {
  214. $content = $comment->comment;
  215. $content = EasyBlogCommentHelper::parseBBCode( $content );
  216. $content = nl2br( $content );
  217. $content = strip_tags( $content );
  218. $content = JString::substr( $content, 0 , $config->get( 'integrations_jomsocial_comments_length' ) );
  219. }
  220. $obj = new stdClass();
  221. $obj->title = JText::sprintf('COM_EASYBLOG_JS_ACTIVITY_COMMENT_ADDED' , $blogLink , $blogTitle );
  222. $obj->content = ($config->get('integrations_jomsocial_submit_content')) ? $content : '';
  223. $obj->cmd = $command;
  224. $obj->actor = $comment->created_by;
  225. $obj->target = 0;
  226. $obj->app = 'easyblog';
  227. $obj->cid = $comment->id;
  228. $obj->eventid = $event->id;
  229. if( $config->get( 'integrations_jomsocial_activity_likes' ) )
  230. {
  231. $obj->like_id = $comment->id;
  232. $obj->like_type = 'com_easyblog.comments';
  233. }
  234. if( $config->get( 'integrations_jomsocial_activity_comments' ) )
  235. {
  236. $obj->comment_id = $comment->id;
  237. $obj->comment_type = 'com_easyblog.comments';
  238. }
  239. // add JomSocial activities
  240. CFactory::load ( 'libraries', 'activities' );
  241. CActivityStream::add($obj);
  242. }
  243. private function addStreamJomsocial( $blog , $isNew , $uid , $source )
  244. {
  245. $jsCoreFile = JPATH_ROOT . DIRECTORY_SEPARATOR . 'components' . DIRECTORY_SEPARATOR . 'com_community' . DIRECTORY_SEPARATOR . 'libraries' . DIRECTORY_SEPARATOR . 'core.php';
  246. $config = EasyBlogHelper::getConfig();
  247. // Somehow the blog contribution is in an array.
  248. $uid = $uid[0];
  249. JFactory::getLanguage()->load( 'com_easyblog' , JPATH_ROOT );
  250. if( !JFile::exists( $jsCoreFile ) )
  251. {
  252. return false;
  253. }
  254. require_once( $jsCoreFile );
  255. $app = JFactory::getApplication();
  256. $title = JString::substr( $blog->title , 0 , 30 ) . '...';
  257. $easyBlogItemid = '';
  258. if( $app->isAdmin() )
  259. {
  260. $easyBlogItemid = EasyBlogRouter::getItemId('latest');
  261. $easyBlogItemid = '&Itemid=' . $easyBlogItemid;
  262. }
  263. $blogLink = EasyBlogRouter::getRoutedURL( 'index.php?option=com_easyblog&view=entry&id='. $blog->id . $easyBlogItemid, false, true);
  264. $my = JFactory::getUser();
  265. $blogContent = $blog->intro . $blog->content;
  266. $blogContent = EasyBlogHelper::getHelper( 'Videos' )->strip( $blogContent );
  267. $pattern = '#<img[^>]*>#i';
  268. preg_match( $pattern , $blogContent , $matches );
  269. // Remove all html tags from the content as we want to chop it down.
  270. $blogContent = strip_tags( $blogContent );
  271. if( JString::strlen( $blogContent ) > $config->get( 'integrations_jomsocial_blogs_length', 250 ) )
  272. {
  273. $blogContent = JString::substr($blogContent, 0, $config->get( 'integrations_jomsocial_blogs_length', 250 ) ) . ' ...';
  274. }
  275. if( $matches )
  276. {
  277. $matches[0] = JString::str_ireplace( 'img ' , 'img style="margin: 0 5px 5px 0;float: left; height: auto; width: 120px !important;"' , $matches[0 ] );
  278. $blogContent = $matches[0] . $blogContent . '<div style="clear: both;"></div>';
  279. }
  280. $blogContent .= '<div style="text-align: right;"><a href="' . $blogLink . '">' . JText::_( 'COM_EASYBLOG_CONTINUE_READING' ) . '</a></div>';
  281. $eventLink = CRoute::_( 'index.php?option=com_community&view=events&task=viewevent&eventid=' . $uid );
  282. JTable::addIncludePath( JPATH_ROOT . DIRECTORY_SEPARATOR . 'components' . DIRECTORY_SEPARATOR . 'com_community' . DIRECTORY_SEPARATOR . 'tables' );
  283. $event = JTable::getInstance( 'Event' , 'CTable' );
  284. $event->load( $uid );
  285. $title = JText::sprintf( 'COM_EASYBLOG_JS_ACTIVITY_EVENT_BLOG_ADDED' , $blogLink , $title , $eventLink , $event->title );
  286. $obj = new stdClass();
  287. $obj->title = $title;
  288. $obj->content = $blogContent;
  289. $obj->cmd = 'event.blog.added';
  290. if( $config->get( 'integrations_jomsocial_activity_likes' ) )
  291. {
  292. $obj->like_id = $blog->id;
  293. $obj->like_type = 'com_easyblog';
  294. }
  295. if( $config->get( 'integrations_jomsocial_activity_comments' ) )
  296. {
  297. $obj->comment_id = $blog->id;
  298. $obj->comment_type = 'com_easyblog';
  299. }
  300. $obj->actor = $my->id;
  301. $obj->target = $uid;
  302. $obj->app = 'easyblog';
  303. $obj->cid = $uid;
  304. $obj->eventid = $uid;
  305. // add JomSocial activities
  306. CFactory::load ( 'libraries', 'activities' );
  307. CActivityStream::add($obj);
  308. }
  309. public function getContribution( $postId, $sourcetype = 'jomsocial', $type = 'id')
  310. {
  311. $db = EasyBlogHelper::db();
  312. $externalTblName = '';
  313. if( $sourcetype == 'jomsocial' )
  314. {
  315. $externalTblName = '#__community_events';
  316. }
  317. $query = '';
  318. if( $type == 'name' || $type == 'title' )
  319. {
  320. $query = 'SELECT b.`title` FROM `#__easyblog_external` as a ';
  321. $query .= ' INNER JOIN ' . $db->NameQuote( $externalTblName ) . ' as b ON a.`uid` = b.`id`';
  322. }
  323. else
  324. {
  325. $query = 'SELECT `uid` FROM `#__easyblog_external` as a';
  326. }
  327. $query .= ' WHERE ' . $db->nameQuote( 'source' ) . ' = ' . $db->Quote( $sourcetype . '.event' );
  328. $query .= ' AND ' . $db->nameQuote( 'post_id' ) . ' = ' . $db->Quote( $postId );
  329. $db->setQuery( $query );
  330. $result = $db->loadResult();
  331. return $result;
  332. }
  333. }