public function action_get() { try { if (is_numeric($id_ad = $this->request->param('id'))) { $ad = new Model_Ad($id_ad); if ($ad->loaded()) { if ($ad->id_user == $this->user->id_user) { $a = $ad->as_array(); $a['price'] = i18n::money_format($ad->price); $a['images'] = array_values($ad->get_images()); $a['category'] = $ad->category->as_array(); $a['location'] = $ad->location->as_array(); $a['customfields'] = Model_Field::get_by_category($ad->id_category); $this->rest_output(array('ad' => $a)); } else { $this->_error(__('Not your advertisement'), 401); } } else { $this->_error(__('Advertisement not found'), 404); } } else { $this->_error(__('Advertisement not found'), 404); } } catch (Kohana_HTTP_Exception $khe) { $this->_error($khe); return; } }
public function action_get() { try { if (is_numeric($id_ad = $this->request->param('id'))) { $ad = new Model_Ad(); //get distance to the ad if (isset($this->_params['latitude']) and isset($this->_params['longitude'])) { $ad->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')); } $ad->where('id_ad', '=', $id_ad)->where('status', '=', Model_Ad::STATUS_PUBLISHED)->cached()->find(); if ($ad->loaded()) { $a = $ad->as_array(); $a['price'] = i18n::money_format($ad->price); $a['images'] = array_values($ad->get_images()); $a['category'] = $ad->category->as_array(); $a['location'] = $ad->location->as_array(); $a['user'] = Controller_Api_Users::get_user_array($ad->user); $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)); $this->rest_output(array('ad' => $a)); } else { $this->_error(__('Advertisement not found'), 404); } } else { $this->_error(__('Advertisement not found'), 404); } } catch (Kohana_HTTP_Exception $khe) { $this->_error($khe); return; } }