Exemplo n.º 1
0
Arquivo: api.php Projeto: anqh/anqh
 /**
  * Action: search
  */
 public function action_search()
 {
     $this->data['events'] = array();
     $term = trim(Arr::get($_REQUEST, 'q', ''));
     $search = explode(':', Arr::get($_REQUEST, 'search', 'name'));
     $limit = (int) Arr::get($_REQUEST, 'limit', 25);
     $order = Arr::get($_REQUEST, 'order', 'name.asc');
     $field = explode(':', Arr::get($_REQUEST, 'field', 'id:name:city'));
     $filter = Arr::get($_REQUEST, 'filter');
     // Term must be at least 3 characters
     if (strlen($term) >= 3) {
         // 500 events max
         $limit = max(min($limit, 500), 1);
         // Get order
         $orders = $this->_prepare_order($order, self::$_orderable);
         $orders = empty($orders) ? array('name' => 'asc') : $orders;
         // Get fields
         $fields = empty($field) || ($field[0] = 'all') ? self::$_fields : array_intersect($field, self::$_fields);
         $fields = empty($fields) ? array('id', 'name') : $fields;
         // Get search
         $searches = empty($search) ? self::$_searchable : array_intersect($search, self::$_searchable);
         $searches = empty($searches) ? array('name') : $searches;
         // Validate filter
         $filter = !empty($filter) && ($filter == 'upcoming' || $filter == 'past' || strpos($filter, 'date:') !== false) ? $filter : null;
         // Build query
         $event = new Model_Event();
         $events = DB::select_array($event->fields());
         foreach ($orders as $column => $direction) {
             $events->order_by($column, $direction);
         }
         if ($filter == 'upcoming') {
             // Upcoming events
             $events->where('stamp_begin', '>=', time());
         } else {
             if ($filter == 'past') {
                 // Past events
                 $events->where('stamp_begin', '<', time());
             } else {
                 $filter = explode(':', $filter);
                 if (count($filter) == 2 && $filter[0] == 'date') {
                     // Search only between dates
                     list($from, $to) = explode('-', $filter[1]);
                     if ((int) $from && (int) $to) {
                         $events->where('stamp_begin', 'BETWEEN', array($from, $to));
                     } else {
                         if ((int) $from) {
                             $events->where('stamp_begin', '>=', $from);
                         } else {
                             if ((int) $to) {
                                 $events->and_where('stamp_begin', '<=', $to);
                             }
                         }
                     }
                 }
             }
         }
         $events->where_open();
         foreach ($searches as $search) {
             if ($search == 'venue' || $search == 'city') {
                 $search .= '_name';
             }
             $events->or_where($search, 'ILIKE', '%' . $term . '%');
         }
         $events->where_close();
         // Build data
         foreach ($event->load($events, $limit) as $event) {
             $this->data['events'][] = $this->_prepare_event($event, $fields);
         }
     }
 }