コード例 #1
0
 /**
  * Handle GET requests.
  */
 public function action_index()
 {
     try {
         if (is_numeric($this->request->param('id'))) {
             $this->action_get();
         } else {
             $output = array();
             $ads = new Model_Ad();
             $ads->where('status', '=', Model_Ad::STATUS_PUBLISHED);
             //search with lat and long!! nice!
             if (isset($this->_params['latitude']) and isset($this->_params['longitude'])) {
                 $ads->select(array(DB::expr('degrees(acos(sin(radians(' . $this->_params['latitude'] . ')) * sin(radians(`latitude`)) + cos(radians(' . $this->_params['latitude'] . ')) * cos(radians(`latitude`)) * cos(radians(abs(' . $this->_params['longitude'] . ' - `longitude`))))) * 69.172'), 'distance'))->where('latitude', 'IS NOT', NULL)->where('longitude', 'IS NOT', NULL);
                 //we unset the search by lat and long if not will be duplicated
                 unset($this->_filter_params['latitude']);
                 unset($this->_filter_params['longitude']);
             }
             //only published ads
             $ads->where('status', '=', Model_Ad::STATUS_PUBLISHED);
             //if ad have passed expiration time dont show
             if (core::config('advertisement.expire_date') > 0) {
                 $ads->where(DB::expr('DATE_ADD( published, INTERVAL ' . core::config('advertisement.expire_date') . ' DAY)'), '>', Date::unix2mysql());
             }
             //make a search with q? param
             if (isset($this->_params['q']) and strlen($this->_params['q'])) {
                 if (core::config('general.search_by_description') == TRUE) {
                     $ads->where_open()->where('title', 'like', '%' . $this->_params['q'] . '%')->or_where('description', 'like', '%' . $this->_params['q'] . '%')->where_close();
                 } else {
                     $ads->where('title', 'like', '%' . $this->_params['q'] . '%');
                 }
             }
             //getting all the ads of a category.
             if (isset($this->_filter_params['id_category']) and is_numeric($this->_filter_params['id_category']['value'])) {
                 $category = new Model_Category($this->_filter_params['id_category']['value']);
                 if ($category->loaded()) {
                     $ads->where('id_category', 'in', $category->get_siblings_ids());
                     unset($this->_filter_params['id_category']);
                 }
             }
             //getting all the ads of a location.
             if (isset($this->_filter_params['id_location']) and is_numeric($this->_filter_params['id_location']['value'])) {
                 $location = new Model_Location($this->_filter_params['id_location']['value']);
                 if ($location->loaded()) {
                     $ads->where('id_location', 'in', $location->get_siblings_ids());
                     unset($this->_filter_params['id_location']);
                 }
             }
             //filter results by param, verify field exists and has a value
             $ads->api_filter($this->_filter_params);
             //how many? used in header X-Total-Count
             $count = $ads->count_all();
             //by default sort by published date
             if (empty($this->_sort)) {
                 $this->_sort['published'] = 'desc';
             }
             //after counting sort values
             $ads->api_sort($this->_sort);
             //we add the order by in case was specified, this is not a column so we need to do it manually
             if (isset($this->_sort['distance']) and isset($this->_params['latitude']) and isset($this->_params['longitude'])) {
                 $ads->order_by('distance', $this->_sort['distance']);
             }
             //pagination with headers
             $pagination = $ads->api_pagination($count, $this->_params['items_per_page']);
             $ads = $ads->cached()->find_all();
             //as array
             foreach ($ads as $ad) {
                 $a = $ad->as_array();
                 $a['price'] = i18n::money_format($ad->price);
                 $a['thumb'] = $ad->get_first_image();
                 $a['customfields'] = Model_Field::get_by_category($ad->id_category);
                 //sorting by distance, lets add it!
                 if (isset($ad->distance)) {
                     $a['distance'] = i18n::format_measurement($ad->distance);
                 }
                 $a['url'] = Route::url('ad', array('category' => $ad->category->seoname, 'seotitle' => $ad->seotitle));
                 $output[] = $a;
             }
             $this->rest_output(array('ads' => $output), 200, $count, $pagination !== FALSE ? $pagination : NULL);
         }
     } catch (Kohana_HTTP_Exception $khe) {
         $this->_error($khe);
     }
 }
コード例 #2
0
ファイル: ad.php プロジェクト: Wildboard/WbWebApp
 public function action_advanced_search()
 {
     //template header
     $this->template->title = __('Advanced Search');
     $this->template->meta_description = __('Advanced Search');
     //breadcrumbs
     Breadcrumbs::add(Breadcrumb::factory()->set_title(__('Home'))->set_url(Route::url('default')));
     Breadcrumbs::add(Breadcrumb::factory()->set_title($this->template->title));
     $this->template->scripts['footer'] = array('js/search.js');
     // $cat_obj = new Model_Category();
     // $loc_obj = new Model_Location();
     list($cat_obj, $order_categories) = Model_Category::get_all();
     list($loc_obj, $order_locations) = Model_Location::get_all();
     $pagination = NULL;
     $ads = NULL;
     $user = Auth::instance()->get_user() == NULL ? NULL : Auth::instance()->get_user();
     if ($this->request->query()) {
         // variables
         $search_advert = core::get('title');
         $search_loc = core::get('location');
         // filter by each variable
         $ads = new Model_Ad();
         //if ad have passed expiration time dont show
         if (core::config('advertisement.expire_date') > 0) {
             $ads->where(DB::expr('DATE_ADD( published, INTERVAL ' . core::config('advertisement.expire_date') . ' DAY)'), '>', DB::expr('NOW()'));
         }
         if (!empty($search_advert) or core::get('search') !== NULL and strlen(core::get('search')) >= 3) {
             // if user is using search from header
             if (core::get('search')) {
                 $search_advert = core::get('search');
             }
             $ads->where_open()->where('title', 'like', '%' . $search_advert . '%')->or_where('description', 'like', '%' . $search_advert . '%')->where_close();
         }
         $cf_fields = array();
         foreach ($this->request->query() as $name => $field) {
             // get by prefix
             if (strpos($name, 'cf_') !== false) {
                 $cf_fields[$name] = $field;
                 //checkbox when selected return string 'on' as a value
                 if ($field == 'on') {
                     $cf_fields[$name] = 1;
                 } elseif (empty($field)) {
                     $cf_fields[$name] = NULL;
                 }
             }
         }
         $category = NULL;
         //filter by category
         if (core::get('category') !== NULL) {
             $category = new Model_Category();
             $category->where('seoname', '=', core::get('category'))->limit(1)->find();
             if ($category->loaded()) {
                 $ads->where('id_category', 'IN', $category->get_siblings_ids());
             }
         }
         $location = NULL;
         //filter by location
         if (core::get('location') !== NULL) {
             $location = new Model_location();
             $location->where('seoname', '=', core::get('location'))->limit(1)->find();
             if ($location->loaded()) {
                 $ads->where('id_location', 'IN', $location->get_siblings_ids());
             }
         }
         //filter by price
         if (is_numeric(core::get('price-min')) and is_numeric(core::get('price-max'))) {
             $ads->where('price', 'BETWEEN', array(core::get('price-min'), core::get('price-max')));
         }
         foreach ($cf_fields as $key => $value) {
             if (isset($value) and $value != NULL) {
                 if (is_numeric($value)) {
                     $ads->where($key, '=', $value);
                 } elseif (is_string($value)) {
                     $ads->where($key, 'like', '%' . $value . '%');
                 }
             }
         }
         $ads = $ads->where('status', '=', Model_Ad::STATUS_PUBLISHED);
         // count them for pagination
         $res_count = $ads->count_all();
         if ($res_count > 0) {
             // pagination module
             $pagination = Pagination::factory(array('view' => 'pagination', 'total_items' => $res_count, 'items_per_page' => core::config('general.advertisements_per_page')))->route_params(array('controller' => $this->request->controller(), 'action' => $this->request->action(), 'category' => $category !== NULL ? $category->seoname : NULL));
             Breadcrumbs::add(Breadcrumb::factory()->set_title(__("Page ") . $pagination->offset));
             $ads = $ads->order_by('published', 'desc')->limit($pagination->items_per_page)->offset($pagination->offset)->find_all();
         }
     }
     $this->template->bind('content', $content);
     $this->template->content = View::factory('pages/ad/advanced_search', array('ads' => $ads, 'categories' => $cat_obj, 'order_categories' => $order_categories, 'locations' => $loc_obj, 'order_locations' => $order_locations, 'pagination' => $pagination, 'user' => $user, 'fields' => Model_Field::get_all()));
 }