Ejemplo n.º 1
0
 function get($args = array())
 {
     global $wpdb;
     $categories_table = $wpdb->prefix . EM_CATEGORIES_TABLE;
     $events_table = $wpdb->prefix . EM_EVENTS_TABLE;
     //Quick version, we can accept an array of IDs, which is easy to retrieve
     if (self::array_is_numeric($args)) {
         //Array of numbers, assume they are event IDs to retreive
         //We can just get all the events here and return them
         $sql = "SELECT * FROM {$categories_table} WHERE category_id=" . implode(" OR category_id=", $args);
         $results = $wpdb->get_results(apply_filters('em_categories_get_sql', $sql), ARRAY_A);
         $categories = array();
         foreach ($results as $result) {
             $categories[$result['category_id']] = new EM_Category($result);
         }
         return $categories;
         //We return all the categories matched as an EM_Event array.
     }
     //We assume it's either an empty array or array of search arguments to merge with defaults
     $args = self::get_default_search($args);
     $limit = $args['limit'] && is_numeric($args['limit']) ? "LIMIT {$args['limit']}" : '';
     $offset = $limit != "" && is_numeric($args['offset']) ? "OFFSET {$args['offset']}" : '';
     //Get the default conditions
     $conditions = self::build_sql_conditions($args);
     //Put it all together
     $where = count($conditions) > 0 ? " WHERE " . implode(" AND ", $conditions) : '';
     //Get ordering instructions
     $EM_Category = new EM_Category();
     $accepted_fields = $EM_Category->get_fields(true);
     $orderby = self::build_sql_orderby($args, $accepted_fields, get_option('dbem_categories_default_order'));
     //Now, build orderby sql
     $orderby_sql = count($orderby) > 0 ? 'ORDER BY ' . implode(', ', $orderby) : '';
     //Create the SQL statement and execute
     $sql = "\n\t\t\tSELECT * FROM {$categories_table}\n\t\t\tLEFT JOIN {$events_table} ON {$events_table}.event_category_id={$categories_table}.category_id\n\t\t\t{$where}\n\t\t\tGROUP BY category_id\n\t\t\t{$orderby_sql}\n\t\t\t{$limit} {$offset}\n\t\t";
     $results = $wpdb->get_results(apply_filters('em_categories_get_sql', $sql, $args), ARRAY_A);
     //If we want results directly in an array, why not have a shortcut here?
     if ($args['array'] == true) {
         return $results;
     }
     //Make returned results EM_Event objects
     $results = is_array($results) ? $results : array();
     $categories = array();
     foreach ($results as $category_array) {
         $categories[$category_array['category_id']] = new EM_Category($category_array);
     }
     return apply_filters('em_categories_get', $categories);
 }