PageRenderTime 93ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/core/activity/sub-activity-class-singlepost.php

http://buddypress-media.googlecode.com/
PHP | 380 lines | 175 code | 116 blank | 89 comment | 22 complexity | 185128220de604c993ede12fffe207ca MD5 | raw file
Possible License(s): AGPL-1.0, Apache-2.0, GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * BP-MEDIA ACTIVITY STREAM SINGLEPOST CLASS
  4. * Handles single-item posts in the activity stream
  5. *
  6. * @version 0.1.9
  7. * @since 0.1.9
  8. * @package BP-Media
  9. * @subpackage Activity Stream
  10. * @license GPL v2.0
  11. * @link http://code.google.com/p/buddypress-media/
  12. *
  13. * ========================================================================================================
  14. */
  15. class BPM_Activity_SinglePost {
  16. /**
  17. * Renders a single-item activity stream post
  18. *
  19. * @version 0.1.9
  20. * @since 0.1.9
  21. * @param int $activity_post_id | ID of the activity stream post
  22. * @param int $album_id | ID of the album that the media item belongs to
  23. * @param int $media_id | ID of media item
  24. * @return string | Text string of post contents
  25. */
  26. public static function renderPost($activity_post_id, $album_id, $media_id ){
  27. global $bp, $wpdb;
  28. if( empty($activity_post_id) || empty($album_id) || empty($media_id) ){
  29. return false;
  30. }
  31. // Retrieve the media item to add to the activity stream post
  32. /////////////////////////////////////////////////////////////////////////////////////////
  33. $args = array();
  34. $args['id'] = $media_id;
  35. $media_items = BPM_Media::query_media($args);
  36. unset($args);
  37. $media_item = $media_items[0];
  38. if($debug){
  39. echo "Fetch Full Media Items \n";
  40. var_dump($media_items);
  41. }
  42. // Generate the html block containing the linked media item thumbnail
  43. /////////////////////////////////////////////////////////////////////////////////////////
  44. $args = BPM_Cache::fill_resize_args(array( 'size'=>$bp->bpa->options['media_cache_images_activitySingleDisplaySize'] ));
  45. $cache_image = bp_media_get_preview_image_data( $args, $media_item );
  46. unset($args);
  47. $item_href = bp_core_get_user_domain($media_item->owner_id) . $bp->bpa->options['core_slug_base'] .'/'. $bp->bpa->options['albums_slug_home'] .'/'.
  48. $album_id .'/'. $bp->bpa->options['media_slug_single'] .'/'. $media_item->id;
  49. $content = '<div class="bpa-activity-wrap" id="default">';
  50. $content .= '<div class="bpa-activity-singleimage">';
  51. $content .= '<a class="bpa-activity-link" ';
  52. $content .= 'title="' . $media_item->title . '" ';
  53. $content .= 'href="' . $item_href . '">' . '<img class="bpa-activity-thumb" ';
  54. $content .= 'height="' . $cache_image['file_h'] . '" ';
  55. $content .= 'width="' . $cache_image['file_w'] . '" ';
  56. $content .= 'alt="' . $media_item->description . '" ';
  57. $content .= 'src="' . $cache_image['url'] . '"></a>';
  58. $content .= '<div class="bpa-activity-imagecaption">';
  59. $content .= $media_item->description;
  60. $content .= '</div>';
  61. $content .= '</div>';
  62. if($bp->bpa->options['core_favorites_activityShow'] == true) {
  63. $content .= BPM_Activity_Favorite::renderTags($activity_post_id);
  64. }
  65. $content .= '</div>';
  66. return $content;
  67. }
  68. /**
  69. * Adds a single media item post to a user's activity stream.
  70. *
  71. * BP-Media doesn't post single media items to the activity stream until somebody comments on the
  72. * item. Otherwise, the activity stream would be flooded with hundreds of posts every time members
  73. * did a batch upload.
  74. *
  75. * Because of the way the BP activity stream works, members can't comment on something unless it's
  76. * already posted in the activity stream. To get around this limitation, when somebody comments on
  77. * an item for the first time, this function posts just that one item to the activity stream, so we
  78. * can attach a comment to it.
  79. *
  80. * @version 0.1.9
  81. * @since 0.1.9
  82. * @param int $owner_id | ID of the album's owner
  83. * @param int $album_id | ID of the album that the media item belongs to
  84. * @param int $media_id | ID of media item
  85. * @return bool | True on success
  86. */
  87. public static function createPost($album_id, $media_id ){
  88. global $bp, $wpdb;
  89. // Should we push user activity stream posts to global activity stream
  90. $post_global = $bp->bpa->options['core_comments_globalStreamEnable'];
  91. // Retrieve the album that the activity stream post is based on
  92. /////////////////////////////////////////////////////////////////////////////////////////
  93. $args = array();
  94. $args['album_id'] = $album_id;
  95. $args['owner_id'] = false;
  96. $album = BPM_Album::query_albums($args);
  97. $album = $album[0];
  98. unset($args);
  99. if($debug){
  100. echo "Fetch Album \n";
  101. var_dump($album);
  102. }
  103. // Retrieve the media item to add to the activity stream post
  104. /////////////////////////////////////////////////////////////////////////////////////////
  105. $args = array();
  106. $args['id'] = $media_id;
  107. $args['owner_id'] = false;
  108. $media_items = BPM_Media::query_media($args);
  109. unset($args);
  110. $media_item = $media_items[0];
  111. if($debug){
  112. echo "Fetch Full Media Items \n";
  113. var_dump($media_items);
  114. }
  115. $item_href = bp_core_get_user_domain($media_item->owner_id) . $bp->bpa->options['core_slug_base'] .'/'. $bp->bpa->options['albums_slug_home'] .'/'.
  116. $album_id .'/'. $bp->bpa->options['media_slug_single'] .'/'. $media_item->id;
  117. $album_href = bp_core_get_user_domain($album->owner_id) . $bp->bpa->options['core_slug_base'] .'/'. $bp->bpa->options['albums_slug_home'] .'/'.
  118. $album_id .'/';
  119. $action = sprintf( __( '%s - %s from the album %s', "bp-media" ), bp_core_get_userlink($media_item->owner_id), '<a href="'. $item_href .'">'.$media_item->title.'</a>',
  120. '<a href="'. $album_href .'">'.$album->title.'</a>' );
  121. // If the admin has disabled posts to the global activity stream and the user has set the album's privacy level
  122. // to public (priv level = 0), increase the privacy level to 1 so it is hidden in the global activity stream
  123. if(($post_global == false) && ($album->privacy < 2)){
  124. $post_privacy = 1;
  125. } else {
  126. $post_privacy = $album->privacy;
  127. }
  128. // Generate the activity stream post array
  129. /////////////////////////////////////////////////////////////////////////////////////////
  130. $args = array();
  131. $args['id'] = null;
  132. $args['action'] = $action;
  133. $args['component'] = 'bp_album';
  134. $args['type'] = 'single_item';
  135. $args['primary_link'] = null;
  136. $args['user_id'] = $media_item->owner_id;
  137. $args['item_id'] = $album_id;
  138. $args['secondary_item_id'] = $media_item->id;
  139. $args['hide_sitewide'] = $post_privacy;
  140. if($debug){
  141. echo "Full Post Content \n";
  142. var_dump($args);
  143. }
  144. $activity_id = bp_activity_add($args);
  145. return $activity_id;
  146. }
  147. /**
  148. * Rebuilds the contents of an existing single-item activity stream post.
  149. *
  150. * @version 0.1.9
  151. * @since 0.1.9
  152. * @param int $owner_id | ID of the album's owner
  153. * @param int $album_id | ID of the album the media item belongs to
  154. * @param int $activity_post_id | ID of the activity stream post
  155. * @param bool $reset_timestamp | If set to true, the timestamp for the post will be reset to the current time
  156. * @return bool | True on success
  157. */
  158. static function updatePost($owner_id, $media_id, $album_id, $activity_post_id, $reset_timestamp = false){
  159. global $bp, $wpdb;
  160. // Should we push user activity stream posts to global activity stream
  161. $post_global = $bp->bpa->options['core_comments_globalStreamEnable'];
  162. // Retrieve the album that the activity stream post is based on
  163. /////////////////////////////////////////////////////////////////////////////////////////
  164. $args = array();
  165. $args['album_id'] = $album_id;
  166. $args['owner_id'] = $owner_id;
  167. $album = BPM_Album::query_albums($args);
  168. $album = $album[0];
  169. unset($args);
  170. if($debug){
  171. echo "updateSinglePost() Fetch Album \n";
  172. var_dump($album);
  173. }
  174. // Retrieve the media item that the activity stream post is based on
  175. /////////////////////////////////////////////////////////////////////////////////////////
  176. $args = array();
  177. $args['id'] = $media_id;
  178. $args['owner_id'] = $owner_id;
  179. $media_items = BPM_Media::query_media($args);
  180. unset($args);
  181. $media_item = $media_items[0];
  182. if($debug){
  183. echo "updateSinglePost() Fetch Full Media Items \n";
  184. var_dump($media_items);
  185. }
  186. $item_href = bp_core_get_user_domain($owner_id) . $bp->bpa->options['core_slug_base'] .'/'. $bp->bpa->options['albums_slug_home'] .'/'.
  187. $album_id .'/'. $bp->bpa->options['media_slug_single'] .'/'. $media_item->id;
  188. $album_href = bp_core_get_user_domain($owner_id) . $bp->bpa->options['core_slug_base'] .'/'. $bp->bpa->options['albums_slug_home'] .'/'.
  189. $album_id .'/';
  190. $action = sprintf( __( '%s - %s from the album %s', "bp-media" ), bp_core_get_userlink($owner_id), '<a href="'. $item_href .'">'.$media_item->title.'</a>',
  191. '<a href="'. $album_href .'">'.$album->title.'</a>' );
  192. // Update the activity stream post
  193. // /////////////////////////////////////////////////////////////////////////////////////////////////////////
  194. // If the admin has disabled posts to the global activity stream and the user has set the album's privacy level
  195. // to public (priv level = 0), increase the privacy level to 1 so it is hidden in the global activity stream
  196. if(($post_global == false) && ($album->privacy < 2)){
  197. $post_privacy = 1;
  198. } else {
  199. $post_privacy = $album->privacy;
  200. }
  201. if(!$reset_timestamp){
  202. // Since bp_activity_add() resets the timestamp on posts, we cannot use this function for updating activity
  203. // stream items. Otherwise, it would bring old posts back to the top of the activity stream when we delete
  204. // media items from them. We could get around it by querying the old post and retrieving its timestamp, but
  205. // that would require an extra query. Its better to do an update query and use the SQL server properly
  206. $sql = $wpdb->prepare("
  207. UPDATE {$bp->activity->table_name}
  208. SET action = %s,
  209. item_id = %d,
  210. hide_sitewide = %d
  211. WHERE id = %d",
  212. $action, $album_id, $post_privacy,
  213. $activity_post_id
  214. );
  215. // clear cache
  216. wp_cache_delete( 'bp_activity_sitewide_front', 'bp' );
  217. }
  218. else {
  219. $date_recorded = gmdate( "Y-m-d H:i:s" );
  220. $sql = $wpdb->prepare("
  221. UPDATE {$bp->activity->table_name}
  222. SET action = %s,
  223. item_id = %d,
  224. date_recorded = %s,
  225. hide_sitewide = %d
  226. WHERE id = %d",
  227. $action, $album_id, $date_recorded, $post_privacy,
  228. $activity_post_id
  229. );
  230. // clear cache
  231. wp_cache_delete( 'bp_activity_sitewide_front', 'bp' );
  232. }
  233. $result = $wpdb->query($sql);
  234. if($debug){
  235. echo "updateSinglePost()SQL:\n";
  236. var_dump($sql);
  237. echo "updateSinglePost() Result:\n";
  238. var_dump($result);
  239. }
  240. return $result;
  241. }
  242. /**
  243. * Checks if an activity post is a BP-Media single-item post
  244. *
  245. * @version 0.1.9
  246. * @since 0.1.9
  247. * @param int $activity_id | ID of the activity item to test
  248. * @return bool | True on success, false on failure
  249. */
  250. public static function isSingleItem($activity_post_id){
  251. global $bp, $wpdb;
  252. $sql = $wpdb->prepare("
  253. SELECT COUNT(id)
  254. FROM {$bp->activity->table_name}
  255. WHERE id = %d
  256. AND component = 'bp_album'
  257. AND type = 'single_item'",
  258. $activity_post_id
  259. );
  260. $item_count = (int)$wpdb->get_var($sql);
  261. return $item_count;
  262. }
  263. }
  264. ?>