PageRenderTime 50ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/wp/wp-content/plugins/events-manager/classes/em-locations.php

https://bitbucket.org/akeda/bmw-id-hris
PHP | 315 lines | 240 code | 19 blank | 56 comment | 78 complexity | 01a9ba195568452f65db0fb2b8fa15f3 MD5 | raw file
  1. <?php
  2. /**
  3. * Static class which will help bulk add/edit/retrieve/manipulate arrays of EM_Location objects.
  4. * Optimized for specifically retreiving locations (whether eventful or not). If you want event data AND location information for each event, use EM_Events
  5. *
  6. */
  7. class EM_Locations extends EM_Object {
  8. /**
  9. * Returns an array of EM_Location objects
  10. * @param boolean $eventful
  11. * @param boolean $return_objects
  12. * @return array
  13. */
  14. function get( $args = array(), $count=false ){
  15. global $wpdb;
  16. $events_table = EM_EVENTS_TABLE;
  17. $locations_table = EM_LOCATIONS_TABLE;
  18. $locations = array();
  19. //Quick version, we can accept an array of IDs, which is easy to retrieve
  20. if( self::array_is_numeric($args) ){ //Array of numbers, assume they are event IDs to retreive
  21. //We can just get all the events here and return them
  22. $sql = "SELECT * FROM $locations_table WHERE location_id=".implode(" OR location_id=", $args);
  23. $results = $wpdb->get_results($sql,ARRAY_A);
  24. $events = array();
  25. foreach($results as $result){
  26. $locations[$result['location_id']] = new EM_Location($result);
  27. }
  28. return apply_filters('em_locations_get', $locations, $args); //We return all the events matched as an EM_Event array.
  29. }elseif( is_numeric($args) ){
  30. //return an event in the usual array format
  31. return apply_filters('em_locations_get', array(new EM_Location($args)), $args);
  32. }elseif( is_array($args) && is_object(current($args)) && get_class((current($args))) == 'EM_Location' ){
  33. return apply_filters('em_locations_get', $args, $args);
  34. }
  35. //We assume it's either an empty array or array of search arguments to merge with defaults
  36. $args = self::get_default_search($args);
  37. $limit = ( $args['limit'] && is_numeric($args['limit'])) ? "LIMIT {$args['limit']}" : '';
  38. $offset = ( $limit != "" && is_numeric($args['offset']) ) ? "OFFSET {$args['offset']}" : '';
  39. //Get the default conditions
  40. $conditions = self::build_sql_conditions($args);
  41. //Put it all together
  42. $EM_Location = new EM_Location(0); //Empty class for strict message avoidance
  43. $fields = $locations_table .".". implode(", {$locations_table}.", array_keys($EM_Location->fields));
  44. $where = ( count($conditions) > 0 ) ? " WHERE " . implode ( " AND ", $conditions ):'';
  45. //Get ordering instructions
  46. $EM_Event = new EM_Event(); //blank event for below
  47. $accepted_fields = $EM_Location->get_fields(true);
  48. $accepted_fields = array_merge($EM_Event->get_fields(true),$accepted_fields);
  49. $orderby = self::build_sql_orderby($args, $accepted_fields, get_option('dbem_events_default_order'));
  50. //Now, build orderby sql
  51. $orderby_sql = ( count($orderby) > 0 ) ? 'ORDER BY '. implode(', ', $orderby) : '';
  52. $fields = ( $count ) ? $locations_table.'.location_id':$locations_table.'.post_id';
  53. if( EM_MS_GLOBAL ){
  54. $selectors = ( $count ) ? 'COUNT('.$locations_table.'.location_id)':$locations_table.'.post_id, '.$locations_table.'.blog_id';
  55. }else{
  56. $selectors = ( $count ) ? 'COUNT('.$locations_table.'.location_id)':$locations_table.'.post_id';
  57. }
  58. //Create the SQL statement and execute
  59. $sql = "
  60. SELECT $selectors FROM $locations_table
  61. LEFT JOIN $events_table ON {$locations_table}.location_id={$events_table}.location_id
  62. $where
  63. GROUP BY {$locations_table}.location_id
  64. $orderby_sql
  65. $limit $offset
  66. ";
  67. //If we're only counting results, return the number of results
  68. if( $count ){
  69. return apply_filters('em_locations_get_array', count($wpdb->get_col($sql)), $args);
  70. }
  71. $results = $wpdb->get_results($sql, ARRAY_A);
  72. //If we want results directly in an array, why not have a shortcut here?
  73. if( $args['array'] == true ){
  74. return apply_filters('em_locations_get_array', $results, $args);
  75. }
  76. if( EM_MS_GLOBAL ){
  77. foreach ( $results as $location ){
  78. if( empty($location['blog_id']) ) $location['blog_id'] = get_current_site()->blog_id;
  79. $locations[] = em_get_location($location['post_id'], $location['blog_id']);
  80. }
  81. }else{
  82. foreach ( $results as $location ){
  83. $locations[] = em_get_location($location['post_id'], 'post_id');
  84. }
  85. }
  86. return apply_filters('em_locations_get', $locations, $args);
  87. }
  88. function count( $args = array() ){
  89. return apply_filters('em_locations_count', self::get($args, true), $args);
  90. }
  91. /**
  92. * Output a set of matched of events
  93. * @param array $args
  94. * @return string
  95. */
  96. function output( $args ){
  97. global $EM_Location;
  98. $EM_Location_old = $EM_Location; //When looping, we can replace EM_Location global with the current event in the loop
  99. //Can be either an array for the get search or an array of EM_Location objects
  100. if( !empty($args['pagination']) && !array_key_exists('page',$args) && !empty($_REQUEST['pno']) && is_numeric($_REQUEST['pno']) ){
  101. $page = $args['page'] = $_REQUEST['pno'];
  102. }
  103. if( is_object(current($args)) && get_class((current($args))) == 'EM_Location' ){
  104. $func_args = func_get_args();
  105. $locations = $func_args[0];
  106. $args = (!empty($func_args[1])) ? $func_args[1] : array();
  107. $args = apply_filters('em_locations_output_args', self::get_default_search($args), $locations);
  108. $limit = ( !empty($args['limit']) && is_numeric($args['limit']) ) ? $args['limit']:false;
  109. $offset = ( !empty($args['offset']) && is_numeric($args['offset']) ) ? $args['offset']:0;
  110. $page = ( !empty($args['page']) && is_numeric($args['page']) ) ? $args['page']:1;
  111. $locations_count = count($locations);
  112. }else{
  113. $args = apply_filters('em_locations_output_args', self::get_default_search($args) );
  114. $limit = ( !empty($args['limit']) && is_numeric($args['limit']) ) ? $args['limit']:false;
  115. $offset = ( !empty($args['offset']) && is_numeric($args['offset']) ) ? $args['offset']:0;
  116. $page = ( !empty($args['page']) && is_numeric($args['page']) ) ? $args['page']:1;
  117. $args_count = $args;
  118. $args_count['limit'] = 0;
  119. $args_count['offset'] = 0;
  120. $args_count['page'] = 1;
  121. $locations_count = self::count($args_count);
  122. $locations = self::get( $args );
  123. }
  124. //What format shall we output this to, or use default
  125. $format = ( $args['format'] == '' ) ? get_option( 'dbem_location_list_item_format' ) : $args['format'] ;
  126. $output = "";
  127. $locations = apply_filters('em_locations_output_locations', $locations);
  128. if ( count($locations) > 0 ) {
  129. foreach ( $locations as $EM_Location ) {
  130. $output .= $EM_Location->output($format);
  131. }
  132. //Add headers and footers to output
  133. if( $format == get_option ( 'dbem_location_list_item_format' ) ){
  134. $single_event_format_header = get_option ( 'dbem_location_list_item_format_header' );
  135. $single_event_format_footer = get_option ( 'dbem_location_list_item_format_footer' );
  136. $output = $single_event_format_header . $output . $single_event_format_footer;
  137. }
  138. //Pagination (if needed/requested)
  139. if( !empty($args['pagination']) && !empty($limit) && $locations_count >= $limit ){
  140. $output .= self::get_pagination_links($args, $locations_count, 'search_locations', self::get_default_search());
  141. }
  142. } else {
  143. $output = get_option ( 'dbem_no_locations_message' );
  144. }
  145. //FIXME check if reference is ok when restoring object, due to changes in php5 v 4
  146. $EM_Location_old= $EM_Location;
  147. return apply_filters('em_locations_output', $output, $locations, $args);
  148. }
  149. function delete( $args = array() ){
  150. $locations = array();
  151. if( !is_object(current($args)) ){
  152. //we've been given an array or search arguments to find the relevant locations to delete
  153. $locations = self::get($args);
  154. }elseif( get_class(current($args)) == 'EM_Location' ){
  155. //we're deleting an array of locations
  156. $locations = $args;
  157. }
  158. $results = array();
  159. foreach ( $locations as $EM_Location ){
  160. $results[] = $EM_Location->delete();
  161. }
  162. return apply_filters('em_locations_delete', in_array(false, $results), $locations);
  163. }
  164. public static function get_post_search($args = array(), $filter = false, $request = array()){
  165. $return = parent::get_post_search($args, $filter, $request);
  166. //remove unwanted arguments or if not explicitly requested
  167. if( empty($_REQUEST['scope']) && empty($request['scope']) && !empty($return['scope']) ){
  168. unset($return['scope']);
  169. }
  170. return apply_filters('em_locations_get_post_search', $return);
  171. }
  172. /**
  173. * Builds an array of SQL query conditions based on regularly used arguments
  174. * @param array $args
  175. * @return array
  176. */
  177. function build_sql_conditions( $args = array(), $count=false ){
  178. self::$context = EM_POST_TYPE_LOCATION;
  179. global $wpdb;
  180. $events_table = EM_EVENTS_TABLE;
  181. $locations_table = EM_LOCATIONS_TABLE;
  182. $conditions = parent::build_sql_conditions($args);
  183. //search locations
  184. if( !empty($args['search']) ){
  185. $like_search = array($locations_table.'.post_content','location_name','location_address','location_town','location_postcode','location_state','location_region','location_country');
  186. $conditions['search'] = "(".implode(" LIKE '%{$args['search']}%' OR ", $like_search). " LIKE '%{$args['search']}%')";
  187. }
  188. //eventful locations
  189. if( true == $args['eventful'] ){
  190. $conditions['eventful'] = "{$events_table}.event_id IS NOT NULL AND event_status=1";
  191. }elseif( true == $args['eventless'] ){
  192. $conditions['eventless'] = "{$events_table}.event_id IS NULL";
  193. }
  194. //owner lookup
  195. if( !empty($args['owner']) && is_numeric($args['owner'])){
  196. $conditions['owner'] = "location_owner=".$args['owner'];
  197. }elseif( !empty($args['owner']) && $args['owner'] == 'me' && is_user_logged_in() ){
  198. $conditions['owner'] = 'location_owner='.get_current_user_id();
  199. }
  200. //blog id in events table
  201. if( EM_MS_GLOBAL && !empty($args['blog']) ){
  202. if( is_numeric($args['blog']) ){
  203. if( is_main_site($args['blog']) ){
  204. $conditions['blog'] = "(".$locations_table.".blog_id={$args['blog']} OR ".$locations_table.".blog_id IS NULL)";
  205. }else{
  206. $conditions['blog'] = "(".$locations_table.".blog_id={$args['blog']})";
  207. }
  208. }else{
  209. if( !is_array($args['blog']) && preg_match('/^([\-0-9],?)+$/', $args['blog']) ){
  210. $conditions['blog'] = "(".$locations_table.".blog_id IN ({$args['blog']}) )";
  211. }elseif( is_array($args['blog']) && self::array_is_numeric($args['blog']) ){
  212. $conditions['blog'] = "(".$locations_table.".blog_id IN (".implode(',',$args['blog']).") )";
  213. }
  214. }
  215. }
  216. //status
  217. $conditions['status'] = "(`location_status` >= 0)"; //pending and published if status is not explicitly defined (Default is 1)
  218. if( array_key_exists('status',$args) ){
  219. if( is_numeric($args['status']) ){
  220. $conditions['status'] = "(`location_status`={$args['status']} )"; //trash (-1), pending, (0) or published (1)
  221. }elseif( $args['status'] == 'pending' ){
  222. $conditions['status'] = "(`location_status`=0)"; //pending
  223. }elseif( $args['status'] == 'publish' ){
  224. $conditions['status'] = "(`location_status`=1)"; //published
  225. }elseif( $args['status'] === null || $args['status'] == 'draft' ){
  226. $conditions['status'] = "(`location_status` IS NULL )"; //show draft items
  227. }elseif( $args['status'] == 'trash' ){
  228. $conditions['status'] = "(`location_status` = -1 )"; //show trashed items
  229. }elseif( $args['status'] == 'all'){
  230. $conditions['status'] = "(`location_status` >= 0 OR `location_status` IS NULL)"; //search all statuses that aren't trashed
  231. }elseif( $args['status'] == 'everything'){
  232. unset($conditions['status']); //search all statuses
  233. }
  234. }
  235. //private locations
  236. if( empty($args['private']) ){
  237. $conditions['private'] = "(`location_private`=0)";
  238. }elseif( !empty($args['private_only']) ){
  239. $conditions['private_only'] = "(`location_private`=1)";
  240. }
  241. //post search
  242. if( !empty($args['post_id'])){
  243. if( is_array($args['post_id']) ){
  244. $conditions['post_id'] = "($locations_table.post_id IN (".implode(',',$args['post_id'])."))";
  245. }else{
  246. $conditions['post_id'] = "($locations_table.post_id={$args['post_id']})";
  247. }
  248. }
  249. return apply_filters('em_locations_build_sql_conditions', $conditions, $args);
  250. }
  251. /* Overrides EM_Object method to apply a filter to result
  252. * @see wp-content/plugins/events-manager/classes/EM_Object#build_sql_orderby()
  253. */
  254. function build_sql_orderby( $args, $accepted_fields, $default_order = 'ASC' ){
  255. self::$context = EM_POST_TYPE_LOCATION;
  256. return apply_filters( 'em_locations_build_sql_orderby', parent::build_sql_orderby($args, $accepted_fields, get_option('dbem_events_default_order')), $args, $accepted_fields, $default_order );
  257. }
  258. /*
  259. * Generate a search arguments array from defalut and user-defined.
  260. * @see wp-content/plugins/events-manager/classes/EM_Object::get_default_search()
  261. */
  262. function get_default_search($array = array()){
  263. self::$context = EM_POST_TYPE_LOCATION;
  264. $defaults = array(
  265. 'eventful' => false, //Locations that have an event (scope will also play a part here
  266. 'eventless' => false, //Locations WITHOUT events, eventful takes precedence
  267. 'orderby' => 'location_name',
  268. 'town' => false,
  269. 'state' => false,
  270. 'country' => false,
  271. 'region' => false,
  272. 'status' => 1, //approved locations only
  273. 'scope' => 'all', //we probably want to search all locations by default, not like events
  274. 'blog' => get_current_blog_id(),
  275. 'private' => current_user_can('read_private_locations'),
  276. 'private_only' => false,
  277. 'post_id' => false
  278. );
  279. if( EM_MS_GLOBAL ){
  280. if( get_site_option('dbem_ms_mainblog_locations') ){
  281. //when searching in MS Global mode with all locations being stored on the main blog, blog_id becomes redundant as locations are stored in one blog table set
  282. $array['blog'] = false;
  283. }elseif( (!is_admin() || defined('DOING_AJAX')) && empty($array['blog']) && is_main_site() && get_site_option('dbem_ms_global_locations') ){
  284. //if enabled, by default we display all blog locations on main site
  285. $array['blog'] = false;
  286. }
  287. }
  288. $array['eventful'] = ( !empty($array['eventful']) && $array['eventful'] == true );
  289. $array['eventless'] = ( !empty($array['eventless']) && $array['eventless'] == true );
  290. if( is_admin() ){
  291. $defaults['owner'] = !current_user_can('read_others_locations') ? get_current_user_id():false;
  292. }
  293. return apply_filters('em_locations_get_default_search', parent::get_default_search($defaults, $array), $array, $defaults);
  294. }
  295. }
  296. ?>