Ejemplo n.º 1
0
 /**
  * gets data to the view and filters the ads
  * @param  Model_Category $category 
  * @param  Model_Location $location
  * @return array           
  */
 public function list_logic($category = NULL, $location = NULL)
 {
     //user recognition
     $user = Auth::instance()->get_user() == NULL ? NULL : Auth::instance()->get_user();
     $ads = new Model_Ad();
     //filter by category or location
     if ($category !== NULL) {
         $ads->where('id_category', 'in', $category->get_siblings_ids());
     }
     if ($location !== NULL) {
         $ads->where('id_location', 'in', $location->get_siblings_ids());
     }
     //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)'), '>', DB::expr('NOW()'));
     }
     $res_count = $ads->count_all();
     // check if there are some advet.-s
     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, 'location' => $location !== NULL ? $location->seoname : NULL));
         Breadcrumbs::add(Breadcrumb::factory()->set_title(__("Page ") . $pagination->current_page));
         //we sort all ads with few parameters
         $ads = $ads->order_by('published', 'desc')->limit($pagination->items_per_page)->offset($pagination->offset)->find_all();
     } else {
         // array of categories sorted for view
         return array('ads' => NULL, 'pagination' => NULL, 'user' => $user, 'category' => $category, 'location' => $location);
     }
     // array of categories sorted for view
     return array('ads' => $ads, 'pagination' => $pagination, 'user' => $user, 'category' => $category, 'location' => $location);
 }
Ejemplo n.º 2
0
 /**
  * deletes all the locations
  * @return void 
  */
 public function action_delete_all()
 {
     if (core::post('confirmation')) {
         //delete location icons
         $locations = new Model_Location();
         if ($id_location = intval(Core::post('id_location')) and $id_location > 0) {
             $selected_location = new Model_Location($id_location);
             $locations->where('id_location', 'in', $selected_location->get_siblings_ids())->where('id_location', '!=', $selected_location->id_location);
         } else {
             $locations->where('id_location', '!=', '1')->find_all();
         }
         $locations = $locations->find_all();
         foreach ($locations as $location) {
             $root = DOCROOT . 'images/locations/';
             //root folder
             if (is_dir($root)) {
                 @unlink($root . $location->seoname . '.png');
                 // delete icon from Amazon S3
                 if (core::config('image.aws_s3_active')) {
                     $s3->deleteObject(core::config('image.aws_s3_bucket'), 'images/locations/' . $location->seoname . '.png');
                 }
             }
         }
         $query_update = DB::update('ads');
         $query_delete = DB::delete('locations');
         if ($id_location = intval(Core::post('id_location')) and $id_location > 0) {
             $query_update->set(array('id_location' => $selected_location->id_location));
             $query_delete->where('id_location', 'in', $selected_location->get_siblings_ids())->where('id_location', '!=', $selected_location->id_location);
         } else {
             $query_update->set(array('id_location' => '1'));
             $query_delete->where('id_location', '!=', '1');
         }
         $query_update->execute();
         $query_delete->execute();
         Core::delete_cache();
         Alert::set(Alert::SUCCESS, __('All locations were deleted.'));
     } else {
         Alert::set(Alert::ERROR, __('You did not confirmed your delete action.'));
     }
     HTTP::redirect(Route::url('oc-panel', array('controller' => 'location', 'action' => 'index')));
 }
Ejemplo n.º 3
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);
     }
 }
Ejemplo n.º 4
0
 /**
  * gets data to the view and filters the ads
  * @param  Model_Category $category 
  * @param  Model_Location $location
  * @return array           
  */
 public function list_logic($category = NULL, $location = NULL)
 {
     //user recognition
     $user = Auth::instance()->get_user() == NULL ? NULL : Auth::instance()->get_user();
     $ads = new Model_Ad();
     //filter by category or location
     if ($category !== NULL) {
         $ads->where('id_category', 'in', $category->get_siblings_ids());
     }
     if ($location !== NULL) {
         $ads->where('id_location', 'in', $location->get_siblings_ids());
     }
     //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());
     }
     //if sort by distance
     if (core::request('sort') == 'distance' and Model_User::get_userlatlng()) {
         $ads->select(array(DB::expr('degrees(acos(sin(radians(' . $_COOKIE['mylat'] . ')) * sin(radians(`latitude`)) + cos(radians(' . $_COOKIE['mylat'] . ')) * cos(radians(`latitude`)) * cos(radians(abs(' . $_COOKIE['mylng'] . ' - `longitude`))))) * 69.172'), 'distance'))->where('latitude', 'IS NOT', NULL)->where('longitude', 'IS NOT', NULL);
     }
     // featured ads
     $featured = NULL;
     if (Theme::get('listing_slider') == 2) {
         $featured = clone $ads;
         $featured = $featured->where('featured', '>=', Date::unix2mysql())->order_by('featured', 'desc')->limit(Theme::get('num_home_latest_ads', 4))->find_all();
     } elseif (Theme::get('listing_slider') == 3) {
         $featured = clone $ads;
         $featured = $featured->where('featured', '>=', Date::unix2mysql())->order_by(DB::expr('RAND()'))->limit(Theme::get('num_home_latest_ads', 4))->find_all();
     }
     $res_count = clone $ads;
     $res_count = $res_count->count_all();
     // check if there are some advet.-s
     if ($res_count > 0) {
         // pagination module
         $pagination = Pagination::factory(array('view' => 'pagination', 'total_items' => $res_count, 'items_per_page' => core::config('advertisement.advertisements_per_page')))->route_params(array('controller' => $this->request->controller(), 'action' => $this->request->action(), 'category' => $category !== NULL ? $category->seoname : NULL, 'location' => $location !== NULL ? $location->seoname : NULL));
         Breadcrumbs::add(Breadcrumb::factory()->set_title(__("Page ") . $pagination->current_page));
         /**
          * order depending on the sort parameter
          */
         switch (core::request('sort', core::config('advertisement.sort_by'))) {
             //title z->a
             case 'title-asc':
                 $ads->order_by('title', 'asc')->order_by('published', 'desc');
                 break;
                 //title a->z
             //title a->z
             case 'title-desc':
                 $ads->order_by('title', 'desc')->order_by('published', 'desc');
                 break;
                 //cheaper first
             //cheaper first
             case 'price-asc':
                 $ads->order_by('price', 'asc')->order_by('published', 'desc');
                 break;
                 //expensive first
             //expensive first
             case 'price-desc':
                 $ads->order_by('price', 'desc')->order_by('published', 'desc');
                 break;
                 //featured
             //featured
             case 'featured':
                 $ads->order_by('featured', 'desc')->order_by('published', 'desc');
                 break;
                 //rating
             //rating
             case 'rating':
                 $ads->order_by('rate', 'desc')->order_by('published', 'desc');
                 break;
                 //distance
             //distance
             case 'distance':
                 if (Model_User::get_userlatlng()) {
                     $ads->order_by('distance', 'asc')->order_by('published', 'asc');
                 }
                 break;
                 //oldest first
             //oldest first
             case 'published-asc':
                 $ads->order_by('published', 'asc');
                 break;
                 //newest first
             //newest first
             case 'published-desc':
             default:
                 $ads->order_by('published', 'desc');
                 break;
         }
         //we sort all ads with few parameters
         $ads = $ads->limit($pagination->items_per_page)->offset($pagination->offset)->find_all();
     } else {
         // array of categories sorted for view
         return array('ads' => NULL, 'pagination' => NULL, 'user' => $user, 'category' => $category, 'location' => $location, 'featured' => NULL);
     }
     // array of categories sorted for view
     return array('ads' => $ads, 'pagination' => $pagination, 'user' => $user, 'category' => $category, 'location' => $location, 'featured' => $featured);
 }
Ejemplo n.º 5
0
 /**
  * counts how many ads have each category
  * @param  boolean $location_filter filters by location 
  * @param  Model_Location $location 
  * @return array
  */
 public static function get_category_count($location_filter = TRUE, $location = NULL)
 {
     //cache by location
     if ($location_filter === TRUE and $location and $location->loaded()) {
         $id_location = $location->id_location;
     } elseif ($location_filter === TRUE and Model_Location::current()->loaded()) {
         $id_location = Model_Location::current()->id_location;
     } else {
         $id_location = 'all';
     }
     //name used in the cache for storage
     $cache_name = 'get_category_count_' . $id_location;
     if (($cats_count = Core::cache($cache_name)) === NULL) {
         $expr_date = is_numeric(core::config('advertisement.expire_date')) ? core::config('advertisement.expire_date') : 0;
         $db_prefix = Database::instance('default')->table_prefix();
         //get the categories that have ads id_category->num ads
         $count_ads = DB::select('c.id_category', array(DB::expr('COUNT("a.id_ad")'), 'count'))->from(array('categories', 'c'))->join(array('ads', 'a'))->using('id_category')->where('a.id_category', '=', DB::expr($db_prefix . 'c.id_category'))->where(DB::expr('IF(' . $expr_date . ' <> 0, DATE_ADD( published, INTERVAL ' . $expr_date . ' DAY), DATE_ADD( NOW(), INTERVAL 1 DAY))'), '>', Date::unix2mysql())->where('a.status', '=', Model_Ad::STATUS_PUBLISHED);
         //filter the count by location
         if ($location_filter === TRUE and $location and $location->loaded()) {
             $count_ads = $count_ads->where('a.id_location', 'in', $location->get_siblings_ids());
         } elseif ($location_filter === TRUE and Model_Location::current()->loaded()) {
             $count_ads = $count_ads->where('a.id_location', 'in', Model_Location::current()->get_siblings_ids());
         }
         $count_ads = $count_ads->group_by('c.id_category')->order_by('c.order', 'asc')->cached()->execute();
         $count_ads = $count_ads->as_array('id_category');
         //getting the count of ads into the parents
         $parents_count = array();
         foreach ($count_ads as $count_ad) {
             $id_category = $count_ad['id_category'];
             $count = $count_ad['count'];
             //adding himself if doesnt exists
             if (!isset($parents_count[$id_category])) {
                 $parents_count[$id_category] = $count_ad;
                 $parents_count[$id_category]['has_siblings'] = FALSE;
             }
             $category = new Model_Category($id_category);
             //for each parent of this category add the count
             $parents_ids = $category->get_parents_ids();
             if (count($parents_ids) > 0) {
                 foreach ($parents_ids as $id) {
                     if (isset($parents_count[$id])) {
                         $parents_count[$id]['count'] += $count_ads[$category->id_category]['count'];
                     } else {
                         $parents_count[$id]['count'] = $count_ads[$category->id_category]['count'];
                     }
                     $parents_count[$id]['has_siblings'] = TRUE;
                 }
             }
         }
         //get all the categories with level 0 and 1
         $categories = new self();
         $categories = $categories->where('id_category', '!=', 1)->where('parent_deep', 'IN', array(0, 1))->order_by('order', 'asc')->cached()->find_all();
         //generating the array
         $cats_count = array();
         foreach ($categories as $category) {
             $has_siblings = isset($parents_count[$category->id_category]) ? $parents_count[$category->id_category]['has_siblings'] : FALSE;
             //they may not have counted the siblings since the count was 0 but he actually has siblings...
             if ($has_siblings === FALSE and $category->has_siblings()) {
                 $has_siblings = TRUE;
             }
             $cats_count[$category->id_category] = $category->as_array();
             $cats_count[$category->id_category] = array('id_category' => $category->id_category, 'seoname' => $category->seoname, 'name' => $category->name, 'id_category_parent' => $category->id_category_parent, 'parent_deep' => $category->parent_deep, 'order' => $category->order, 'price' => $category->price, 'has_siblings' => $has_siblings, 'count' => isset($parents_count[$category->id_category]) ? $parents_count[$category->id_category]['count'] : 0);
         }
         //cache the result is expensive!
         Core::cache($cache_name, $cats_count);
     }
     return $cats_count;
 }