Exemplo n.º 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();
             //any status but needs to see your ads ;)
             $ads->where('id_user', '=', $this->user->id_user);
             //by default sort by published date
             if (empty($this->_sort)) {
                 $this->_sort['published'] = 'desc';
             }
             //filter results by param, verify field exists and has a value and sort the results
             $ads->api_filter($this->_filter_params)->api_sort($this->_sort);
             //how many? used in header X-Total-Count
             $count = $ads->count_all();
             //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);
                 foreach ($a['customfields'] as $key => $values) {
                     $a['customfields'][$key]['value'] = $a[$key];
                 }
                 $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);
     }
 }
Exemplo n.º 2
0
 public function action_info()
 {
     //try to get the info from the cache
     $info = Core::cache('action_info', NULL);
     //not cached :(
     if ($info === NULL) {
         $ads = new Model_Ad();
         $total_ads = $ads->count_all();
         $last_ad = $ads->select('published')->order_by('published', 'desc')->limit(1)->find();
         $last_ad = $last_ad->published;
         $ads = new Model_Ad();
         $first_ad = $ads->select('published')->order_by('published', 'asc')->limit(1)->find();
         $first_ad = $first_ad->published;
         $views = new Model_Visit();
         $total_views = $views->count_all();
         $users = new Model_User();
         $total_users = $users->count_all();
         $info = array('site_url' => Core::config('general.base_url'), 'site_name' => Core::config('general.site_name'), 'site_description' => Core::config('general.site_description'), 'created' => $first_ad, 'updated' => $last_ad, 'email' => Core::config('email.notify_email'), 'version' => Core::VERSION, 'theme' => Core::config('appearance.theme'), 'theme_mobile' => Core::config('appearance.theme_mobile'), 'charset' => Kohana::$charset, 'timezone' => Core::config('i18n.timezone'), 'locale' => Core::config('i18n.locale'), 'currency' => '', 'ads' => $total_ads, 'views' => $total_views, 'users' => $total_users);
         Core::cache('action_info', $info);
     }
     $this->response->headers('Content-type', 'application/javascript');
     $this->response->body(json_encode($info));
 }
Exemplo 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);
     }
 }
Exemplo n.º 4
0
 public function action_profile()
 {
     Breadcrumbs::add(Breadcrumb::factory()->set_title(__('Home'))->set_url(Route::url('default')));
     Breadcrumbs::add(Breadcrumb::factory()->set_title(__('Users'))->set_url(Route::url('profiles')));
     Breadcrumbs::add(Breadcrumb::factory()->set_title(__('User Profile')));
     $seoname = $this->request->param('seoname', NULL);
     if ($seoname !== NULL) {
         $user = new Model_User();
         $user->where('seoname', '=', $seoname)->where('status', '=', Model_User::STATUS_ACTIVE)->limit(1)->cached()->find();
         if ($user->loaded()) {
             $this->template->title = __('User Profile') . ' - ' . $user->name;
             //$this->template->meta_description = $user->name;//@todo phpseo
             $this->template->bind('content', $content);
             $ads = new Model_Ad();
             $ads->where('id_user', '=', $user->id_user)->where('status', '=', Model_Ad::STATUS_PUBLISHED)->order_by('created', 'desc');
             // case when user dont have any ads
             if (($count_all = $ads->count_all()) == 0) {
                 $profile_ads = NULL;
                 $pagination = NULL;
             } else {
                 $pagination = Pagination::factory(array('view' => 'pagination', 'total_items' => $count_all, 'items_per_page' => core::config('advertisement.advertisements_per_page')));
                 $ads = $ads->limit($pagination->items_per_page)->offset($pagination->offset)->cached()->find_all();
             }
             $this->template->content = View::factory('pages/user/profile', array('user' => $user, 'profile_ads' => $ads, 'pagination' => $pagination));
         } else {
             //throw 404
             throw HTTP_Exception::factory(404, __('Page not found'));
         }
     } else {
         //throw 404
         throw HTTP_Exception::factory(404, __('Page not found'));
     }
 }
Exemplo n.º 5
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);
 }