PageRenderTime 26ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/core/media/class.media.template.php

http://buddypress-media.googlecode.com/
PHP | 400 lines | 177 code | 102 blank | 121 comment | 15 complexity | 07f31c3f968cd2dd0ef3df4e5ebe56f3 MD5 | raw file
Possible License(s): AGPL-1.0, Apache-2.0, GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * BP-MEDIA MEDIA ITEM TEMPLATE CLASS
  4. * Operates main loop in templates when displaying media items
  5. *
  6. * @version 0.1.9
  7. * @since 0.1.9
  8. * @package BP-Media
  9. * @subpackage Media Item
  10. * @license GPL v2.0
  11. * @link http://code.google.com/p/buddypress-media/
  12. *
  13. * ========================================================================================================
  14. */
  15. class BPM_media_template extends BPM_db_walker {
  16. var $pag_links; // Pagination links for the current page in the template object
  17. // ============================================================================================================ //
  18. /**
  19. * Creates a template object filled with query result objects, based on user supplied parameters
  20. *
  21. * @version 0.1.9
  22. * @since 0.1.9
  23. *
  24. * @param array $args | @see BPM_db::runSelectQuery() for array structure
  25. * @param array $ctrl | @see BPM_db::runSelectQuery() for array structure
  26. * @return int | Exception on failure. Count of items returned by the query on success.
  27. */
  28. function __construct($args=null, $ctrl=null) {
  29. global $bpm;
  30. try {
  31. $x_items = $bpm->config->getKeyVal("media", "mediasList", "displayGridX");
  32. $y_items = $bpm->config->getKeyVal("media", "mediasList", "displayGridY");
  33. }
  34. catch (BPM_exception $child) {
  35. throw new BPM_exception( array(
  36. 'numeric'=>1,
  37. 'text'=>"Error reading from config class",
  38. 'file'=>__FILE__, 'line'=>__LINE__, 'method'=>__METHOD__,
  39. 'child'=>$child
  40. ));
  41. }
  42. $ctrl_default = array(
  43. 'page' => $defaults['page'] = isset( $_REQUEST['page'] ) ? intval( $_REQUEST['page'] ) : 1,
  44. "per_page"=>( $x_items * $y_items )
  45. );
  46. $ctrl = wp_parse_args($ctrl, $ctrl_default);
  47. $ctrl['format'] = "array_object"; // Force to be array_object to prevent accidentally
  48. // passing wrong results format
  49. // Load the class
  50. // =========================================================================
  51. // Get total number of matching items in db
  52. try {
  53. $this->total_item_count = (int) self::query($args, null, array("count"=>true, "format"=>"var") );
  54. // Calculate number of pages
  55. $this->total_pages = ceil( (int) $this->total_item_count / (int) $ctrl["per_page"] );
  56. // Get matching items in db, up to limit set by "per_page" arg in $ctrl array
  57. $db_result = self::query($args, $ctrl);
  58. }
  59. catch (BPM_exception $child) {
  60. throw new BPM_exception( array(
  61. 'numeric'=>2,
  62. 'text'=>"Error during query operation",
  63. 'data'=>array('args'=>$args, 'ctrl'=>$ctrl),
  64. 'file'=>__FILE__, 'line'=>__LINE__, 'method'=>__METHOD__,
  65. 'child'=>$child
  66. ));
  67. }
  68. // Load the database results into objects and add them to the items array
  69. if($db_result){
  70. foreach( $db_result as $row ){
  71. $result = new BPM_media();
  72. foreach( $row as $col => $val ){
  73. $result->{$col} = $val;
  74. }
  75. unset($col, $val);
  76. $this->items[] = $result;
  77. }
  78. unset($row);
  79. }
  80. $this->item_count = count($this->items);
  81. $this->in_the_loop = false;
  82. $this->current_item = 0;
  83. // Build the pagination links string
  84. // =========================================================================
  85. $this->pag_links = paginate_links( array(
  86. 'base' => add_query_arg( 'page', '%#%' ),
  87. 'format' => '',
  88. 'total' => $this->total_pages,
  89. 'current' => (int) $ctrl["page"],
  90. 'prev_text' => '&larr;',
  91. 'next_text' => '&rarr;',
  92. 'mid_size' => 1
  93. ));
  94. return $this->item_count;
  95. }
  96. /**
  97. * Returns an array of Media Item data structures based on user supplied parameters
  98. *
  99. * @version 0.1.9
  100. * @since 0.1.9
  101. *
  102. * @param int $max | The maximum number of results to return from the database
  103. * @param string $owner_type | The system role of the member that uploaded the media
  104. * @param int $page | Used to set current page when traversing a multi-page data set
  105. * @param int $per_page | Number of objects to return per page when transversing multi-page data set
  106. *
  107. * @param int $privacy | Sets the privacy level of the media item
  108. * @param bool $priv_override | Makes system ignore privacy level when returning results
  109. *
  110. * @param string $ordersort | Sets sort order of the returned data set
  111. * @param string $orderkey | Sets which parameter to order the returned results by
  112. *
  113. * @param int array $owner_include | Only show items owned by member ID provided. For multiple ID's, pass as an array.
  114. * @param int array $owner_exclude | Only show items NOT owned by member ID provided. For multiple ID's, pass as an array.
  115. *
  116. * @param int array $member_include | Only show items the member ID provided has been tagged in. For multiple ID's, pass as an array.
  117. * @param int array $member_exclude | Only show items the member ID provided has NOT been tagget in. For multiple ID's, pass as an array.
  118. *
  119. * @param int $time_start | Only show items created after this timestamp. gmdate() format.
  120. * @param int $time_end | Only show items created before this timestamp. gmdate() format.
  121. *
  122. * @param string array $tag_include | Only show items tagged with the keyword provided. For multiple keywords, pass as an array.
  123. * @param string array $tag_exclude | Only show items NOT tagged with the keyword provided. For multiple keywords, pass as an array.
  124. *
  125. * @return int/array | Exception on failure. Int on count. Array of media item objects on success.
  126. *
  127. *
  128. * In future releases, you'll be able to run geographic searches like:
  129. * "Find all the items tagged with [keyword] within [distance] of [specific place]"
  130. *
  131. * Bounding Box
  132. * ------------
  133. * Because finding all items within a *round* area is extremely CPU intensive, we use a bounding box
  134. * to set the area to search in. When you run a query like "Find all [single females] within [5km] of
  135. * [specific place] in [London]" you first set the bounding box to surround London, then run the radial
  136. * search inside it. We'll provide an example in a future release.
  137. *
  138. * @param float $geo_bound_lat_a | Bounding box upper left corner latitude.
  139. * @param float $geo_bound_lon_a | Bounding box upper left corner longitude.
  140. * @param float $geo_bound_lat_a | Bounding box lower right corner latitude.
  141. * @param float $geo_bound_lon_a | Bounding box lower right corner longitude.
  142. *
  143. * Datum
  144. * -----
  145. * This is the point you want to search outwards from. Obviously, it has to be inside the bounding
  146. * box or you'll get zero results.
  147. *
  148. * @param float $geo_datum_lat | Datum latitude.
  149. * @param float $geo_datum_lon | Datum longitude.
  150. *
  151. * Radius
  152. * ------
  153. * Radius from the datum, in kilometers, to search for items in.
  154. *
  155. * @param float $geo_radius | Datum latitude.
  156. *
  157. */
  158. public function query($args=null, $columns=null, $ctrl=null) {
  159. global $bpm;
  160. // Default Args
  161. // ==============================================
  162. $args_default = array();
  163. $args_default['owner_include'] = null;
  164. $args_default['owner_exclude'] = null; // used to remove OWN uploads when viewing latest ones on site
  165. $args_default['member_include'] = null;
  166. $args_default['member_exclude'] = null; // used to remove OWN tag when viewing latest ones on site
  167. $args_default['time_start'] = null;
  168. $args_default['time_end'] = null;
  169. $args_default['tag_include'] = null;
  170. $args_default['tag_exclude'] = null;
  171. $args_default['geo_bound_lat_a'] = null;
  172. $args_default['geo_bound_lon_a'] = null;
  173. $args_default['geo_bound_lat_b'] = null;
  174. $args_default['geo_bound_lon_b'] = null;
  175. $args_default['geo_datum_lat'] = null;
  176. $args_default['geo_datum_lon'] = null;
  177. $args_default['geo_radius'] = null;
  178. $args = wp_parse_args($args, $args_default);
  179. // Default Ctrl
  180. // ==============================================
  181. $ctrl_default = array();
  182. try {
  183. $x_items = $bpm->config->getKeyVal("media", "mediasList", "displayGridX");
  184. $y_items = $bpm->config->getKeyVal("media", "mediasList", "displayGridY");
  185. }
  186. catch (BPM_exception $child) {
  187. throw new BPM_exception( array(
  188. 'numeric'=>1,
  189. 'text'=>"Error reading from config class",
  190. 'file'=>__FILE__, 'line'=>__LINE__, 'method'=>__METHOD__,
  191. 'child'=>$child
  192. ));
  193. }
  194. $ctrl_default['page'] = 1;
  195. $ctrl_default['per_page'] = ($x_items * $y_items);
  196. $ctrl_default['format'] = "array_object";
  197. $ctrl = wp_parse_args($ctrl, $ctrl_default);
  198. // Primary class options
  199. // #######################################################################
  200. $query_args = array();
  201. // Owner id passed to the function (used for showing a single member's
  202. // latest uploads, or the member's friends uploads)
  203. // =======================================================================
  204. if($args['owner_include']){
  205. $query_args[] = array("col"=>"owner_id", "op"=>"=", "val"=>$args['owner_include']);
  206. }
  207. if($args['owner_exclude']){
  208. $query_args[] = array("col"=>"owner_id", "op"=>"!=", "val"=>$args['owner_exclude']);
  209. }
  210. // Start time / end time passed to function. Note isset() is used because
  211. // datetime zenith (01-01-1970) == '0'
  212. // =======================================================================
  213. if( isset($args['time_start']) ){
  214. $query_args[] = array("col"=>"date_created", "op"=>">", "val"=>gmdate( "Y-m-d H:i:s", $args['time_start'] ) );
  215. }
  216. if( isset($args['time_end']) ){
  217. $query_args[] = array("col"=>"date_created", "op"=>"<", "val"=>gmdate( "Y-m-d H:i:s", $args['time_end'] ) );
  218. }
  219. // Joined class options
  220. // #######################################################################
  221. $join = array();
  222. // Member tag passed to the function (used for showing uploads tagged with
  223. // a member, or the member's friends)
  224. // =======================================================================
  225. if( $args['member_include'] || $args['member_exclude']){
  226. $join["BPM_mTag"] = array(
  227. "on"=>array("pri"=>"id", "op"=>"=", "sec"=>"tagged_id"),
  228. "args"=>array(
  229. array("col"=>"status", "op"=>"=", "val"=>3) // Tag is approved
  230. )
  231. );
  232. // Include member tags
  233. if($args['member_include']){
  234. $join["BPM_mTag"]["args"][] = array("col"=>"tagged_id", "op"=>"=", "val"=>$args['member_include']);
  235. }
  236. // Exclude member tags
  237. if($args['member_exclude']){
  238. $join["BPM_mTag"]["args"][] = array("col"=>"tagged_id", "op"=>"!=", "val"=>$args['member_exclude']);
  239. }
  240. }
  241. // Keyword tag passed to the function (used for showing uploads tagged
  242. // with a member, or the member's friends)
  243. // =======================================================================
  244. if( $args['tag_include'] || $args['tag_exclude']){
  245. $join["BPM_keyword"] = array(
  246. "on"=>array("pri"=>"id", "op"=>"=", "sec"=>"media_id"),
  247. "args"=>array(
  248. array("col"=>"status", "op"=>"=", "val"=>2) // Tag is approved
  249. )
  250. );
  251. // Include keyword tags
  252. if($args['tag_include']){
  253. $join["BPM_keyword"]["args"][] = array("col"=>"keyword", "op"=>"=", "val"=>$args['tag_include']);
  254. }
  255. // Exclude keyword tags
  256. if($args['tag_exclude']){
  257. $join["BPM_keyword"]["args"][] = array("col"=>"keyword", "op"=>"!=", "val"=>$args['tag_exclude']);
  258. }
  259. }
  260. // Query
  261. // #######################################################################
  262. $db = new BPM_db();
  263. if($join){
  264. $primary = array(
  265. "class"=>BPM_media::_struct(),
  266. "args"=>$query_args
  267. );
  268. try {
  269. $result = $db->runSelectQueryJoin($primary, $join, $columns, $ctrl);
  270. }
  271. catch (BPM_exception $child) {
  272. throw new BPM_exception( array(
  273. 'numeric'=>2,
  274. 'text'=>"Error during query operation",
  275. 'data'=>array('primary'=>$primary, 'join'=>$join, 'columns'=>$columns, 'ctrl'=>$ctrl),
  276. 'file'=>__FILE__, 'line'=>__LINE__, 'method'=>__METHOD__,
  277. 'child'=>$child
  278. ));
  279. }
  280. }
  281. else {
  282. try {
  283. $result = $db->runSelectQuery( BPM_media::_struct(), $query_args, $columns, $ctrl);
  284. }
  285. catch (BPM_exception $child) {
  286. throw new BPM_exception( array(
  287. 'numeric'=>3,
  288. 'text'=>"Error during query operation",
  289. 'data'=>array('args'=>$query_args, 'columns'=>$columns, 'ctrl'=>$ctrl),
  290. 'file'=>__FILE__, 'line'=>__LINE__, 'method'=>__METHOD__,
  291. 'child'=>$child
  292. ));
  293. }
  294. }
  295. return $result;
  296. }
  297. } // End of class BPM_media_template
  298. ?>