public function action_index() { $this->auto_render = FALSE; $info = array('title' => 'RSS ' . htmlspecialchars(Core::config('general.site_name')), 'pubDate' => date("r"), 'description' => __('Latest published'), 'generator' => 'Open Classifieds'); $items = array(); //last ads, you can modify this value at: advertisement.feed_elements $ads = new Model_Ad(); $ads->where('status', '=', Model_Ad::STATUS_PUBLISHED)->order_by('published', 'desc')->limit(Core::config('advertisement.feed_elements')); //filter by category aor location if (Model_Category::current()->loaded()) { $ads->where('id_category', '=', Model_Category::current()->id_category); } if (Model_Location::current()->loaded()) { $ads->where('id_location', '=', Model_Location::current()->id_location); } $ads = $ads->cached()->find_all(); foreach ($ads as $a) { $url = Route::url('ad', array('category' => $a->category->seoname, 'seotitle' => $a->seotitle)); $item = array('title' => htmlspecialchars($a->title, ENT_QUOTES), 'link' => $url, 'pubDate' => Date::mysql2unix($a->published), 'description' => htmlspecialchars(Text::removebbcode($a->description), ENT_QUOTES), 'guid' => $url); if ($a->get_first_image() !== NULL) { $item['description'] = '<img src="' . $a->get_first_image() . '" />' . $item['description']; } $items[] = $item; } $xml = Feed::create($info, $items); $this->response->headers('Content-type', 'text/xml'); $this->response->body($xml); }
/** * Automatically executed before the widget action. Can be used to set * class properties, do authorization checks, and execute other custom code. * * @return void */ public function before() { $ads = new Model_Ad(); $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()); } switch ($this->ads_type) { case 'popular': $id_ads = array_keys(Model_Visit::popular_ads()); if (count($id_ads) > 0) { $ads->where('id_ad', 'IN', $id_ads); } break; case 'featured': $ads->where('featured', 'IS NOT', NULL)->where('featured', '>', Date::unix2mysql())->order_by('featured', 'desc'); break; case 'latest': default: $ads->order_by('published', 'desc'); break; } $ads = $ads->limit($this->ads_limit)->cached()->find_all(); //die(print_r($ads)); $this->ads = $ads; }
public function action_index() { //template header $this->template->title = ''; // $this->template->meta_keywords = 'keywords'; $this->template->meta_description = Core::config('general.site_description'); //setting main view/template and render pages // swith to decide on ads_in_home $ads = new Model_Ad(); $ads->where('status', '=', Model_Ad::STATUS_PUBLISHED); switch (core::config('advertisement.ads_in_home')) { case 2: $id_ads = array_keys(Model_Visit::popular_ads()); if (count($id_ads) > 0) { $ads->where('id_ad', 'IN', $id_ads); } break; case 1: $ads->where('featured', 'IS NOT', NULL)->where('featured', 'BETWEEN', array(DB::expr('NOW()'), Date::unix2mysql(time() + core::config('payment.featured_days') * 24 * 60 * 60)))->order_by('featured', 'desc'); break; case 0: default: $ads->order_by('published', 'desc'); break; } //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()')); } $ads = $ads->limit(Theme::get('num_home_latest_ads', 4))->cached()->find_all(); $this->ads = $ads; $categs = Model_Category::get_category_count(); $this->template->bind('content', $content); $this->template->content = View::factory('pages/home', array('ads' => $ads, 'categs' => $categs)); }
/** * Automatically executed before the widget action. Can be used to set * class properties, do authorization checks, and execute other custom code. * * @return void */ public function before() { $ads = new Model_Ad(); $ads->where('status', '=', Model_Ad::STATUS_PUBLISHED); $ads->where('featured', 'IS NOT', NULL)->where('featured', 'BETWEEN', array(DB::expr('NOW()'), Date::unix2mysql(time() + core::config('payment.featured_days') * 24 * 60 * 60)))->order_by('featured', 'desc'); $ads = $ads->limit($this->ads_limit)->cached()->find_all(); $this->ads = $ads; }
/** * Automatically executed before the widget action. Can be used to set * class properties, do authorization checks, and execute other custom code. * * @return void */ public function before() { $ads = new Model_Ad(); $ads->where('status', '=', Model_Ad::STATUS_PUBLISHED); $ads->where('featured', 'IS NOT', NULL)->where('featured', '>', Date::unix2mysql())->order_by('featured', 'desc'); $ads = $ads->limit($this->ads_limit)->find_all(); $this->ads = $ads; }
public function action_index() { if (core::config('general.auto_locate')) { Theme::$scripts['footer'][] = '//maps.google.com/maps/api/js?sensor=false&libraries=geometry&v=3.7'; Theme::$scripts['footer'][] = '//cdn.jsdelivr.net/gmaps/0.4.15/gmaps.min.js'; } //template header $this->template->title = ''; // $this->template->meta_keywords = 'keywords'; if (core::config('general.site_description') != '') { $this->template->meta_description = core::config('general.site_description'); } else { $this->template->meta_description = core::config('general.site_name') . ' ' . __('official homepage, get your post listed now.'); } //setting main view/template and render pages // swith to decide on ads_in_home $ads = new Model_Ad(); $ads->where('status', '=', Model_Ad::STATUS_PUBLISHED); $ads_in_home = core::config('advertisement.ads_in_home'); //in case we do not count visits we cant show popular if (core::config('advertisement.count_visits') == 0 and $ads_in_home == 2) { $ads_in_home = 0; } switch ($ads_in_home) { case 2: $id_ads = array_keys(Model_Visit::popular_ads()); if (count($id_ads) > 0) { $ads->where('id_ad', 'IN', $id_ads); } break; case 1: $ads->where('featured', 'IS NOT', NULL)->where('featured', '>=', Date::unix2mysql())->order_by('featured', 'desc'); break; case 4: $ads->where('featured', 'IS NOT', NULL)->where('featured', '>=', Date::unix2mysql())->order_by(DB::expr('RAND()')); break; case 0: default: $ads->order_by('published', 'desc'); break; } //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()); } $ads = $ads->limit(Theme::get('num_home_latest_ads', 4))->cached()->find_all(); $categs = Model_Category::get_category_count(); $locats = Model_Location::get_location_count(); $auto_locats = NULL; if (core::config('general.auto_locate') and Model_User::get_userlatlng()) { $auto_locats = new Model_Location(); $auto_locats = $auto_locats->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`))))) * 111.321'), 'distance'))->where('latitude', 'IS NOT', NULL)->where('longitude', 'IS NOT', NULL)->having('distance', '<=', '100')->order_by('distance', 'desc')->find_all()->as_array(); } $this->template->bind('content', $content); $this->template->content = View::factory('pages/home', array('ads' => $ads, 'categs' => $categs, 'locats' => $locats, 'auto_locats' => $auto_locats)); }
/** * returns the current ad * @return Model_Ad */ public static function current() { //we don't have so let's retrieve if (self::$_current === NULL) { self::$_current = new self(); if (strtolower(Request::current()->controller() == 'Ad') and strtolower(Request::current()->action()) == 'view' and Request::current()->param('seotitle') !== NULL) { self::$_current = self::$_current->where('seotitle', '=', Request::current()->param('seotitle'))->limit(1)->cached()->find(); } } return self::$_current; }
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; } }
/** * Action MODERATION */ public function action_moderate() { //template header $this->template->title = __('Moderation'); $this->template->meta_description = __('Moderation'); $this->template->scripts['footer'][] = '/js/oc-panel/moderation.js'; //find all tables $ads = new Model_Ad(); $res_count = $ads->where('status', '=', Model_Ad::STATUS_NOPUBLISHED)->count_all(); if ($res_count > 0) { $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())); $ads = $ads->where('ad.status', '=', Model_Ad::STATUS_NOPUBLISHED)->order_by('created', 'desc')->limit($pagination->items_per_page)->offset($pagination->offset)->find_all(); //find all tables $hits = new Model_Visit(); $hits->find_all(); $list_cat = Model_Category::get_all(); $list_loc = Model_Location::get_all(); $arr_hits = array(); // array of hit integers // fill array with hit integers foreach ($ads as $key_ads) { // match hits with ad $h = $hits->where('id_ad', '=', $key_ads->id_ad); $count = count($h->find_all()); // count individual hits array_push($arr_hits, $count); } $this->template->content = View::factory('oc-panel/pages/moderate', array('ads' => $ads, 'pagination' => $pagination, 'category' => $list_cat, 'location' => $list_loc, 'hits' => $arr_hits)); // create view, and insert list with data } else { Alert::set(Alert::INFO, __('You do not have any advertisements waiting to be published')); $this->template->content = View::factory('oc-panel/pages/moderate', array('ads' => NULL)); } }
public function action_profile() { $this->auto_render = FALSE; $xml = 'FALSE'; $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()) { $info = array('title' => 'RSS ' . $user->name, 'pubDate' => date("r"), 'description' => $user->name . ' - ' . $user->description, 'generator' => 'Open Classifieds'); $items = array(); //last ads, you can modify this value at: advertisement.feed_elements $ads = new Model_Ad(); $ads->where('status', '=', Model_Ad::STATUS_PUBLISHED)->where('id_user', '=', $user->id_user)->order_by('published', 'desc')->limit(Core::config('advertisement.feed_elements')); $ads = $ads->cached()->find_all(); foreach ($ads as $a) { $url = Route::url('ad', array('category' => $a->category->seoname, 'seotitle' => $a->seotitle)); $item = array('title' => htmlspecialchars($a->title, ENT_QUOTES), 'link' => $url, 'pubDate' => Date::mysql2unix($a->published), 'description' => htmlspecialchars(Text::removebbcode($a->description), ENT_QUOTES), 'guid' => $url); if ($a->get_first_image() !== NULL) { $item['description'] = '<img src="' . $a->get_first_image() . '" />' . $item['description']; } $items[] = $item; } $xml = Feed::create($info, $items); } } $this->response->headers('Content-type', 'text/xml'); $this->response->body($xml); }
/** * Automatically executed before the widget action. Can be used to set * class properties, do authorization checks, and execute other custom code. * * @return void */ public function before() { $ad = new Model_Ad(); $ad->where('seotitle', '=', Request::current()->param('seotitle'))->limit(1)->find(); if ($ad->loaded()) { $this->id_ad = $ad->id_ad; } }
public function action_index() { $this->before('/pages/maps'); $this->template->title = __('Map'); $this->template->height = Core::get('height', '100%'); $this->template->width = Core::get('width', '100%'); $this->template->zoom = Core::get('zoom', core::config('advertisement.map_zoom')); $this->template->height_thumb = Core::config('image.height_thumb') / 4; $this->template->width_thumb = Core::config('image.width_thumb') / 4; if (Model_User::get_userlatlng()) { $this->template->center_lon = $_COOKIE['mylng']; $this->template->center_lat = $_COOKIE['mylat']; } else { $this->template->center_lon = Core::get('lon', core::config('advertisement.center_lon')); $this->template->center_lat = Core::get('lat', core::config('advertisement.center_lat')); } $ads = new Model_Ad(); $ads->where('status', '=', Model_Ad::STATUS_PUBLISHED)->where('address', 'IS NOT', NULL)->where('latitude', 'IS NOT', NULL)->where('longitude', 'IS NOT', NULL); //filter by category if (core::get('category') !== NULL) { $category = new Model_Category(); $category->where('seoname', '=', core::get('category'))->cached()->limit(1)->find(); if ($category->loaded()) { $ads->where('id_category', 'IN', $category->get_siblings_ids()); } } //filter by location if (core::get('location') !== NULL) { $location = new Model_location(); $location->where('seoname', '=', core::get('location'))->cached()->limit(1)->find(); if ($location->loaded()) { $ads->where('id_location', 'IN', $location->get_siblings_ids()); } } //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 only 1 ad if (is_numeric(core::get('id_ad'))) { $ads = $ads->where('id_ad', '=', core::get('id_ad')); } $ads = $ads->order_by('published', 'desc')->limit(Core::config('advertisement.map_elements'))->find_all(); $this->template->ads = $ads; }
/** * Automatically executed before the widget action. Can be used to set * class properties, do authorization checks, and execute other custom code. * * @return void */ public function before() { $ads = new Model_Ad(); $ads->where('status', '=', Model_Ad::STATUS_PUBLISHED); switch ($this->ads_type) { case 'popular': $id_ads = array_keys(Model_Visit::popular_ads()); if (count($id_ads) > 0) { $ads->where('id_ad', 'IN', $id_ads); } break; case 'featured': $ads->where('featured', 'IS NOT', NULL)->where('featured', 'BETWEEN', array(DB::expr('NOW()'), Date::unix2mysql(time() + core::config('payment.featured_days') * 24 * 60 * 60)))->order_by('featured', 'desc'); break; case 'latest': default: $ads->order_by('published', 'desc'); break; } $ads = $ads->limit($this->ads_limit)->cached()->find_all(); //die(print_r($ads)); $this->ads = $ads; }
/** * Automatically executed before the widget action. Can be used to set * class properties, do authorization checks, and execute other custom code. * * @return void */ public function before() { $ad = new Model_Ad(); $user_ads = clone $ad; //get current ad do not filter by user since admin also can see $ad->where('seotitle', '=', Request::current()->param('seotitle'))->limit(1)->find(); if ($ad->loaded() and Auth::instance()->logged_in()) { $user = Auth::instance()->get_user(); if ($user->id_role == Model_Role::ROLE_ADMIN or $user->id_user == $ad->id_user) { $this->ad = $ad; $this->user_ads = $user_ads->where('id_user', '=', $ad->id_user)->find_all(); } } }
/** * Action MODERATION */ public function action_moderate() { //template header $this->template->title = __('Moderation'); $this->template->meta_description = __('Moderation'); $this->template->scripts['footer'][] = 'js/jquery.toolbar.js'; $this->template->scripts['footer'][] = '/js/oc-panel/moderation.js'; //find all tables $ads = new Model_Ad(); $res_count = $ads->where('status', '=', Model_Ad::STATUS_NOPUBLISHED)->count_all(); if ($res_count > 0) { $pagination = Pagination::factory(array('view' => 'oc-panel/crud/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())); $ads = $ads->where('status', '=', Model_Ad::STATUS_NOPUBLISHED)->order_by('created', 'desc')->limit($pagination->items_per_page)->offset($pagination->offset)->find_all(); $this->template->content = View::factory('oc-panel/pages/moderate', array('ads' => $ads, 'pagination' => $pagination)); } else { Alert::set(Alert::INFO, __('You do not have any advertisements waiting to be published')); $this->template->content = View::factory('oc-panel/pages/moderate', array('ads' => NULL)); } }
public function action_export() { $csv_header = array('user_name', 'user_email', 'title', 'description', 'date', 'category', 'location', 'price', 'address', 'phone', 'website', 'image_1', 'image_2', 'image_3', 'image_4'); //the name of the file that user will download $file_name = 'export.csv'; //name of the TMP file $output_file = tempnam('/tmp', $file_name); //writting $output = fopen($output_file, 'w'); //header of the CSV fputcsv($output, $csv_header); //model ad $ads = new Model_Ad(); $ads->where('status', '=', Model_Ad::STATUS_PUBLISHED); $ads = $ads->find_all(); //each element foreach ($ads as $ad) { $pic1 = NULL; $pic2 = NULL; $pic3 = NULL; $pic4 = NULL; $images = $ad->get_images(); if (count($images) > 0) { if (isset($images[1])) { $pic1 = $images[1]['image']; } if (isset($images[2])) { $pic2 = $images[2]['image']; } if (isset($images[3])) { $pic3 = $images[3]['image']; } if (isset($images[4])) { $pic4 = $images[4]['image']; } } $a = array($ad->user->name, $ad->user->email, $ad->title, $ad->description, $ad->published, $ad->category->name, $ad->location->name, $ad->price, $ad->address, $ad->phone, $ad->website, $pic1, $pic2, $pic3, $pic4); fputcsv($output, $a); } fclose($output); //returns the file to the browser as attachement and deletes the TMP file Response::factory()->send_file($output_file, $file_name, array('delete' => TRUE)); }
public function action_stats() { Breadcrumbs::add(Breadcrumb::factory()->set_title(__('My ads'))->set_url(Route::url('oc-panel', array('controller' => 'myads', 'action' => 'index')))); Breadcrumbs::add(Breadcrumb::factory()->set_title(__('Stats'))); Controller::$full_width = TRUE; $this->template->scripts['footer'] = array('js/chart.min.js', 'js/chart.js-php.js', 'js/oc-panel/stats/dashboard.js'); $this->template->title = __('Stats'); $this->template->bind('content', $content); $content = View::factory('oc-panel/profile/stats'); $list_ad = array(); $advert = new Model_Ad(); //single stats for 1 ad if (is_numeric($id_ad = $this->request->param('id'))) { $advert = new Model_Ad($id_ad); if ($advert->loaded()) { //if admin or moderator user is the advert user ;) hack!! if ($this->user->id_role == Model_Role::ROLE_ADMIN or $this->user->id_role == Model_Role::ROLE_MODERATOR) { $user = $advert->user; } else { $user = $this->user; } if ($user->id_user !== $advert->id_user) { Alert::set(Alert::ALERT, __("This is not your advertisement.")); HTTP::redirect(Route::url('oc-panel', array('controller' => 'myads', 'action' => 'index'))); } Breadcrumbs::add(Breadcrumb::factory()->set_title($advert->title)); // make a list of 1 ad (array), and than pass this array to query (IN).. To get correct visits $list_ad[] = $id_ad; } } //we didnt filter by ad, so lets get them all! if (empty($list_ad)) { $ads = new Model_Ad(); $collection_of_user_ads = $ads->where('id_user', '=', $this->user->id_user)->find_all(); $list_ad = array(); foreach ($collection_of_user_ads as $key) { // make a list of ads (array), and than pass this array to query (IN).. To get correct visits $list_ad[] = $key->id_ad; } } // if user doesn't have any ads if (empty($list_ad)) { $list_ad = array(NULL); } $content->advert = $advert; //Getting the dates and range $from_date = Core::post('from_date', strtotime('-1 month')); $to_date = Core::post('to_date', time()); //we assure is a proper time stamp if not we transform it if (is_string($from_date) === TRUE) { $from_date = strtotime($from_date); } if (is_string($to_date) === TRUE) { $to_date = strtotime($to_date); } //mysql formated dates $my_from_date = Date::unix2mysql($from_date); $my_to_date = Date::unix2mysql($to_date); //dates range we are filtering $dates = Date::range($from_date, $to_date, '+1 day', 'Y-m-d', array('date' => 0, 'count' => 0), 'date'); //dates displayed in the form $content->from_date = date('Y-m-d', $from_date); $content->to_date = date('Y-m-d', $to_date); /////////////////////CONTACT STATS//////////////////////////////// //visits created last XX days $query = DB::select(DB::expr('DATE(created) date'))->select(DB::expr('COUNT(contacted) count'))->from('visits')->where('contacted', '=', 1)->where('id_ad', 'in', $list_ad)->where('created', 'between', array($my_from_date, $my_to_date))->group_by(DB::expr('DATE( created )'))->order_by('date', 'asc')->execute(); $contacts_dates = $query->as_array('date'); //Today $query = DB::select(DB::expr('COUNT(contacted) count'))->from('visits')->where('contacted', '=', 1)->where('id_ad', 'in', $list_ad)->where(DB::expr('DATE( created )'), '=', DB::expr('CURDATE()'))->group_by(DB::expr('DATE( created )'))->order_by('created', 'asc')->execute(); $contacts = $query->as_array(); $content->contacts_today = isset($contacts[0]['count']) ? $contacts[0]['count'] : 0; //Yesterday $query = DB::select(DB::expr('COUNT(contacted) count'))->from('visits')->where('contacted', '=', 1)->where('id_ad', 'in', $list_ad)->where(DB::expr('DATE( created )'), '=', date('Y-m-d', strtotime('-1 day')))->group_by(DB::expr('DATE( created )'))->order_by('created', 'asc')->execute(); $contacts = $query->as_array(); $content->contacts_yesterday = isset($contacts[0]['count']) ? $contacts[0]['count'] : 0; // //Last 30 days contacts $query = DB::select(DB::expr('COUNT(contacted) count'))->from('visits')->where('contacted', '=', 1)->where('id_ad', 'in', $list_ad)->where('created', 'between', array(date('Y-m-d', strtotime('-30 day')), date::unix2mysql()))->execute(); $contacts = $query->as_array(); $content->contacts_month = isset($contacts[0]['count']) ? $contacts[0]['count'] : 0; //total contacts $query = DB::select(DB::expr('COUNT(contacted) count'))->where('contacted', '=', 1)->where('id_ad', 'in', $list_ad)->from('visits')->execute(); $contacts = $query->as_array(); $content->contacts_total = isset($contacts[0]['count']) ? $contacts[0]['count'] : 0; /////////////////////VISITS STATS//////////////////////////////// //visits created last XX days $query = DB::select(DB::expr('DATE(created) date'))->select(DB::expr('COUNT(id_visit) count'))->from('visits')->where('id_ad', 'in', $list_ad)->where('created', 'between', array($my_from_date, $my_to_date))->group_by(DB::expr('DATE( created )'))->order_by('date', 'asc')->execute(); $visits = $query->as_array('date'); $stats_daily = array(); foreach ($dates as $date) { $count_contants = isset($contacts_dates[$date['date']]['count']) ? $contacts_dates[$date['date']]['count'] : 0; $count_visits = isset($visits[$date['date']]['count']) ? $visits[$date['date']]['count'] : 0; $stats_daily[] = array('date' => $date['date'], 'views' => $count_visits, 'contacts' => $count_contants); } $content->stats_daily = $stats_daily; //Today $query = DB::select(DB::expr('COUNT(id_visit) count'))->from('visits')->where('id_ad', 'in', $list_ad)->where(DB::expr('DATE( created )'), '=', DB::expr('CURDATE()'))->group_by(DB::expr('DATE( created )'))->order_by('created', 'asc')->execute(); $visits = $query->as_array(); $content->visits_today = isset($visits[0]['count']) ? $visits[0]['count'] : 0; //Yesterday $query = DB::select(DB::expr('COUNT(id_visit) count'))->from('visits')->where('id_ad', 'in', $list_ad)->where(DB::expr('DATE( created )'), '=', date('Y-m-d', strtotime('-1 day')))->group_by(DB::expr('DATE( created )'))->order_by('created', 'asc')->execute(); $visits = $query->as_array(); $content->visits_yesterday = isset($visits[0]['count']) ? $visits[0]['count'] : 0; //Last 30 days visits $query = DB::select(DB::expr('COUNT(id_visit) count'))->from('visits')->where('id_ad', 'in', $list_ad)->where('created', 'between', array(date('Y-m-d', strtotime('-30 day')), date::unix2mysql()))->execute(); $visits = $query->as_array(); $content->visits_month = isset($visits[0]['count']) ? $visits[0]['count'] : 0; //total visits $query = DB::select(DB::expr('COUNT(id_visit) count'))->where('id_ad', 'in', $list_ad)->from('visits')->execute(); $visits = $query->as_array(); $content->visits_total = isset($visits[0]['count']) ? $visits[0]['count'] : 0; }
public function action_stats() { Breadcrumbs::add(Breadcrumb::factory()->set_title(__('Stats'))); //local files if (Theme::get('cdn_files') == FALSE) { $this->template->styles = array('css/datepicker.css' => 'screen'); $this->template->scripts['footer'] = array('js/bootstrap-datepicker.js', 'js/oc-panel/stats/dashboard.js'); } else { $this->template->styles = array('http://cdn.jsdelivr.net/bootstrap.datepicker/0.1/css/datepicker.css' => 'screen'); $this->template->scripts['footer'] = array('http://cdn.jsdelivr.net/bootstrap.datepicker/0.1/js/bootstrap-datepicker.js', 'js/oc-panel/stats/dashboard.js'); } $this->template->title = __('Stats'); $this->template->bind('content', $content); $content = View::factory('oc-panel/profile/stats'); //Getting the dates and range $from_date = Core::post('from_date', strtotime('-1 month')); $to_date = Core::post('to_date', time()); //we assure is a proper time stamp if not we transform it if (is_string($from_date) === TRUE) { $from_date = strtotime($from_date); } if (is_string($to_date) === TRUE) { $to_date = strtotime($to_date); } //mysql formated dates $my_from_date = Date::unix2mysql($from_date); $my_to_date = Date::unix2mysql($to_date); //dates range we are filtering $dates = Date::range($from_date, $to_date, '+1 day', 'Y-m-d', array('date' => 0, 'count' => 0), 'date'); //dates displayed in the form $content->from_date = date('Y-m-d', $from_date); $content->to_date = date('Y-m-d', $to_date); // user and his ads $user = Auth::instance()->get_user(); $ads = new Model_Ad(); $collection_of_user_ads = $ads->where('id_user', '=', $user->id_user)->find_all(); $list_ad = array(); foreach ($collection_of_user_ads as $key) { // make a list of ads (array), and than pass this array to query (IN).. To get correct visits $list_ad[] = $key->id_ad; } // if user doesn't have any ads if (empty($list_ad)) { $list_ad = array(NULL); } /////////////////////CONTACT STATS//////////////////////////////// //visits created last XX days $query = DB::select(DB::expr('DATE(created) date'))->select(DB::expr('COUNT(contacted) count'))->from('visits')->where('contacted', '=', 1)->where('id_ad', 'in', $list_ad)->where('created', 'between', array($my_from_date, $my_to_date))->group_by(DB::expr('DATE( created )'))->order_by('date', 'asc')->execute(); $contacts_dates = $query->as_array('date'); //Today $query = DB::select(DB::expr('COUNT(contacted) count'))->from('visits')->where('contacted', '=', 1)->where('id_ad', 'in', $list_ad)->where(DB::expr('DATE( created )'), '=', DB::expr('CURDATE()'))->group_by(DB::expr('DATE( created )'))->order_by('created', 'asc')->execute(); $contacts = $query->as_array(); $content->contacts_today = isset($contacts[0]['count']) ? $contacts[0]['count'] : 0; //Yesterday $query = DB::select(DB::expr('COUNT(contacted) count'))->from('visits')->where('contacted', '=', 1)->where('id_ad', 'in', $list_ad)->where(DB::expr('DATE( created )'), '=', date('Y-m-d', strtotime('-1 day')))->group_by(DB::expr('DATE( created )'))->order_by('created', 'asc')->execute(); $contacts = $query->as_array(); $content->contacts_yesterday = isset($contacts[0]['count']) ? $contacts[0]['count'] : 0; // //Last 30 days contacts $query = DB::select(DB::expr('COUNT(contacted) count'))->from('visits')->where('contacted', '=', 1)->where('id_ad', 'in', $list_ad)->where('created', 'between', array(date('Y-m-d', strtotime('-30 day')), date::unix2mysql()))->execute(); $contacts = $query->as_array(); $content->contacts_month = isset($contacts[0]['count']) ? $contacts[0]['count'] : 0; //total contacts $query = DB::select(DB::expr('COUNT(contacted) count'))->where('contacted', '=', 1)->where('id_ad', 'in', $list_ad)->from('visits')->execute(); $contacts = $query->as_array(); $content->contacts_total = isset($contacts[0]['count']) ? $contacts[0]['count'] : 0; /////////////////////VISITS STATS//////////////////////////////// //visits created last XX days $query = DB::select(DB::expr('DATE(created) date'))->select(DB::expr('COUNT(id_visit) count'))->from('visits')->where('id_ad', 'in', $list_ad)->where('created', 'between', array($my_from_date, $my_to_date))->group_by(DB::expr('DATE( created )'))->order_by('date', 'asc')->execute(); $visits = $query->as_array('date'); $stats_daily = array(); foreach ($dates as $date) { $count_contants = isset($contacts_dates[$date['date']]['count']) ? $contacts_dates[$date['date']]['count'] : 0; $count_visits = isset($visits[$date['date']]['count']) ? $visits[$date['date']]['count'] : 0; $stats_daily[] = array('date' => $date['date'], 'views' => $count_visits, 'contacts' => $count_contants); } $content->stats_daily = $stats_daily; //Today $query = DB::select(DB::expr('COUNT(id_visit) count'))->from('visits')->where('id_ad', 'in', $list_ad)->where(DB::expr('DATE( created )'), '=', DB::expr('CURDATE()'))->group_by(DB::expr('DATE( created )'))->order_by('created', 'asc')->execute(); $visits = $query->as_array(); $content->visits_today = isset($visits[0]['count']) ? $visits[0]['count'] : 0; //Yesterday $query = DB::select(DB::expr('COUNT(id_visit) count'))->from('visits')->where('id_ad', 'in', $list_ad)->where(DB::expr('DATE( created )'), '=', date('Y-m-d', strtotime('-1 day')))->group_by(DB::expr('DATE( created )'))->order_by('created', 'asc')->execute(); $visits = $query->as_array(); $content->visits_yesterday = isset($visits[0]['count']) ? $visits[0]['count'] : 0; //Last 30 days visits $query = DB::select(DB::expr('COUNT(id_visit) count'))->from('visits')->where('id_ad', 'in', $list_ad)->where('created', 'between', array(date('Y-m-d', strtotime('-30 day')), date::unix2mysql()))->execute(); $visits = $query->as_array(); $content->visits_month = isset($visits[0]['count']) ? $visits[0]['count'] : 0; //total visits $query = DB::select(DB::expr('COUNT(id_visit) count'))->where('id_ad', 'in', $list_ad)->from('visits')->execute(); $visits = $query->as_array(); $content->visits_total = isset($visits[0]['count']) ? $visits[0]['count'] : 0; }
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())); }
/** * * Display reviews advert. * @throws HTTP_Exception_404 * */ public function action_reviews() { $seotitle = $this->request->param('seotitle', NULL); if ($seotitle !== NULL and Core::config('advertisement.reviews') == 1) { $ad = new Model_Ad(); $ad->where('seotitle', '=', $seotitle)->where('status', '!=', Model_Ad::STATUS_SPAM)->limit(1)->cached()->find(); if ($ad->loaded()) { $errors = NULL; //adding a new review if ($this->request->post() and Auth::instance()->logged_in()) { $user = Auth::instance()->get_user(); //only able to review if bought the product if (Core::config('advertisement.reviews_paid') == 1) { $order = new Model_Order(); $order->where('id_ad', '=', $ad->id_ad)->where('id_user', '=', $user->id_user)->where('id_product', '=', Model_Order::PRODUCT_AD_SELL)->where('status', '=', Model_Order::STATUS_PAID)->find(); if (!$order->loaded()) { Alert::set(Alert::ERROR, __('You can only add a review if you bought this product')); $this->redirect(Route::url('ad-review', array('seotitle' => $ad->seotitle))); } } //not allowing to review to yourself if ($user->id_user == $ad->id_user) { Alert::set(Alert::ERROR, __('You can not review yourself.')); $this->redirect(Route::url('ad-review', array('seotitle' => $ad->seotitle))); } $review = new Model_Review(); $review->where('id_ad', '=', $ad->id_ad)->where_open()->or_where('id_user', '=', $user->id_user)->or_where('ip_address', '=', ip2long(Request::$client_ip))->where_close()->find(); //d($review); if (!$review->loaded()) { if (captcha::check('review')) { $validation = Validation::factory($this->request->post())->rule('rate', 'numeric')->rule('description', 'not_empty')->rule('description', 'min_length', array(':value', 5))->rule('description', 'max_length', array(':value', 1000)); if ($validation->check()) { $rate = core::post('rate'); if ($rate > Model_Review::RATE_MAX) { $rate = Model_Review::RATE_MAX; } elseif ($rate < 0) { $rate = 0; } $review = new Model_Review(); $review->id_user = $user->id_user; $review->id_ad = $ad->id_ad; $review->description = core::post('description'); $review->status = Model_Review::STATUS_ACTIVE; $review->ip_address = ip2long(Request::$client_ip); $review->rate = $rate; $review->save(); //email product owner?? notify him of new review $ad->user->email('ad-review', array('[AD.TITLE]' => $ad->title, '[RATE]' => $review->rate, '[DESCRIPTION]' => $review->description, '[URL.QL]' => $ad->user->ql('ad-review', array('seotitle' => $ad->seotitle)))); $ad->recalculate_rate(); $ad->user->recalculate_rate(); Alert::set(Alert::SUCCESS, __('Thanks for your review!')); } else { $errors = $validation->errors('ad'); foreach ($errors as $f => $err) { Alert::set(Alert::ALERT, $err); } } } else { Alert::set(Alert::ERROR, __('Wrong Captcha')); } } else { Alert::set(Alert::ERROR, __('You already added a review')); } } $this->template->scripts['footer'][] = 'js/jquery.raty.min.js'; $this->template->scripts['footer'][] = 'js/review.js'; Breadcrumbs::add(Breadcrumb::factory()->set_title(__('Home'))->set_url(Route::url('default'))); Breadcrumbs::add(Breadcrumb::factory()->set_title($ad->title)->set_url(Route::url('ad', array('seotitle' => $ad->seotitle, 'category' => $ad->category->seoname)))); $this->template->title = $ad->title . ' - ' . __('Reviews'); Breadcrumbs::add(Breadcrumb::factory()->set_title(__('Reviews'))); $this->template->meta_description = text::removebbcode($ad->description); $permission = TRUE; //permission to add hit to advert and give access rights. $auth_user = Auth::instance(); if (!$auth_user->logged_in() or $auth_user->get_user()->id_user != $ad->id_user and ($auth_user->get_user()->id_role != Model_Role::ROLE_ADMIN and $auth_user->get_user()->id_role != Model_Role::ROLE_MODERATOR) or $auth_user->get_user()->id_role != Model_Role::ROLE_ADMIN and $auth_user->get_user()->id_role != Model_Role::ROLE_MODERATOR) { $permission = FALSE; $user = NULL; } else { $user = $auth_user->get_user()->id_user; } $captcha_show = core::config('advertisement.captcha'); if ($ad->get_first_image() !== NULL) { Controller::$image = $ad->get_first_image(); } $reviews = new Model_Review(); $reviews = $reviews->where('id_ad', '=', $ad->id_ad)->where('status', '=', Model_Review::STATUS_ACTIVE)->find_all(); $this->template->bind('content', $content); $this->template->content = View::factory('pages/ad/reviews', array('ad' => $ad, 'permission' => $permission, 'captcha_show' => $captcha_show, 'user' => $user, 'reviews' => $reviews, 'errors' => $errors)); } else { //throw 404 throw HTTP_Exception::factory(404, __('Page not found')); } } else { //throw 404 throw HTTP_Exception::factory(404, __('Page not found')); } }
public function action_index() { if (core::config('general.auto_locate')) { if ($user_location = Core::post('user_location')) { Cookie::set('user_location', $user_location); $this->auto_render = FALSE; $this->template = View::factory('js'); $this->template->content = TRUE; return; } elseif (Core::get('user_location') == '0') { Cookie::delete('user_location'); } Theme::$scripts['async_defer'][] = '//maps.google.com/maps/api/js?libraries=geometry&v=3&key=' . core::config("advertisement.gm_api_key") . '&callback=initAutoLocate'; } //template header $this->template->title = ''; // $this->template->meta_keywords = 'keywords'; if (core::config('general.site_description') != '') { $this->template->meta_description = core::config('general.site_description'); } else { $this->template->meta_description = core::config('general.site_name') . ' ' . __('official homepage, get your post listed now.'); } //setting main view/template and render pages // get user location if any $user_location = NULL; if (is_numeric($user_id_location = Cookie::get('user_location'))) { $user_location = new Model_Location($user_id_location); if (!$user_location->loaded()) { $user_location = NULL; } } // swith to decide on ads_in_home $ads = new Model_Ad(); $ads->where('status', '=', Model_Ad::STATUS_PUBLISHED); if ($user_location) { $ads->where('id_location', 'in', $user_location->get_siblings_ids()); } $ads_in_home = core::config('advertisement.ads_in_home'); //in case we do not count visits we cant show popular if (core::config('advertisement.count_visits') == 0 and $ads_in_home == 2) { $ads_in_home = 0; } switch ($ads_in_home) { case 2: $id_ads = array_keys(Model_Visit::popular_ads()); if (count($id_ads) > 0) { $ads->where('id_ad', 'IN', $id_ads); } break; case 1: $ads->where('featured', 'IS NOT', NULL)->where('featured', '>=', Date::unix2mysql())->order_by('featured', 'desc'); break; case 4: $ads->where('featured', 'IS NOT', NULL)->where('featured', '>=', Date::unix2mysql())->order_by(DB::expr('RAND()')); break; case 0: default: $ads->order_by('published', 'desc'); break; } //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()); } $ads = $ads->limit(Theme::get('num_home_latest_ads', 4))->cached()->find_all(); $categs = Model_Category::get_category_count(TRUE, $user_location); $hide_categories = json_decode(Core::config('general.hide_homepage_categories'), TRUE); $auto_locats = NULL; $auto_location_distance = Core::config('general.measurement') == 'imperial' ? Num::round(Core::config('advertisement.auto_locate_distance') * 1.60934) : Core::config('advertisement.auto_locate_distance'); if (core::config('general.auto_locate') and !isset($_COOKIE['cancel_auto_locate']) and Model_User::get_userlatlng()) { $auto_locats = new Model_Location(); $auto_locats = $auto_locats->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`))))) * 111.321'), 'distance'))->where('latitude', 'IS NOT', NULL)->where('longitude', 'IS NOT', NULL)->having('distance', '<=', $auto_location_distance)->order_by('distance', 'asc')->find_all()->as_array(); } $this->template->bind('content', $content); $this->template->content = View::factory('pages/home', compact('ads', 'categs', 'auto_locats', 'user_location', 'hide_categories')); }
public function action_delete_image() { try { if (is_numeric($id_ad = $this->request->param('id')) and is_numeric($num_image = $this->_post_params['num_image'])) { $ad = new Model_Ad(); $ad->where('id_ad', '=', $id_ad)->where('id_user', '=', $this->user->id_user)->find(); if ($ad->loaded()) { if ($ret = $ad->delete_image($num_image)) { $this->rest_output($ret); } else { $this->_error($ret); } } 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_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')); } }
/** * send message to an advertisement * @param string $message * @param Model_User $user_from * @param integer $id_ad * @param integer $price negotiate price optionsl * @return bool / model_message */ public static function send_ad($message, $user_from, $id_ad, $price = NULL) { //get the ad if its available, and user to who we need to contact $ad = new Model_Ad(); $ad->where('id_ad', '=', $id_ad)->where('status', '=', Model_Ad::STATUS_PUBLISHED)->find(); //ad loaded and is not your ad.... if ($ad->loaded() == TRUE and $user_from->id_user != $ad->id_user) { //check if we already have a thread for that ad and user...then its a reply not a new message. $msg_thread = new Model_Message(); $msg_thread->where('id_message', '=', DB::expr('id_message_parent'))->where('id_ad', '=', $id_ad)->where('id_user_from', '=', $user_from->id_user)->limit(1)->find(); //actually reply not new thread.... if ($msg_thread->loaded()) { return self::reply($message, $user_from, $msg_thread->id_message, $price); } else { $ret = self::send($message, $user_from, $ad, $id_ad, NULL, $price); //send email only if no device ID since he got the push notification already if ($ret !== FALSE and !isset($ad->user->device_id)) { $ad->user->email('messaging-ad-contact', array('[AD.NAME]' => $ad->title, '[FROM.NAME]' => $user_from->name, '[TO.NAME]' => $ad->user->name, '[DESCRIPTION]' => $message, '[URL.QL]' => $ad->user->ql('oc-panel', array('controller' => 'messages', 'action' => 'message', 'id' => $ret->id_message)))); } return $ret; } } return FALSE; }
/** * send message to an advertisement * @param string $message * @param integer $id_user_from * @param integer $id_ad * @param integer $price negotiate price optionsl * @return bool / model_message */ public static function send_ad($message, $id_user_from, $id_ad, $price = NULL) { //get the ad if its available, and user to who we need to contact $ad = new Model_Ad(); $ad->where('id_ad', '=', $id_ad)->where('status', '=', Model_Ad::STATUS_PUBLISHED)->find(); //ad loaded and is not your ad.... if ($ad->loaded() == TRUE and $id_user_from != $ad->id_user) { return self::send($message, $id_user_from, $ad->id_user, $id_ad, NULL, $price); } return FALSE; }
/** * [save_new_ad Save new advertisement if validated, with a given parameters * * @param [array] $data [post values] * @param [int] $status [status of advert.] * @param [bool] $published [Confirms if advert is published. ref to model_ad] * @param [int] $moderation [moderation status/mode] * * @return [view] View dependant on usecase */ public function save_new_ad($data, $status, $published, $moderation) { $user = new Model_User(); $new_ad = new Model_Ad(); //$_POST is submitted for a new ad if ($this->request->post()) { if (captcha::check('publish_new')) { //FORM DATA $seotitle = $new_ad->gen_seo_title($data['title']); $new_ad->title = Model_Ad::banned_words($data['title']); $new_ad->id_location = $data['loc']; $new_ad->id_category = $data['cat']; $new_ad->description = Model_Ad::banned_words($data['description']); $new_ad->seotitle = $seotitle; $new_ad->status = $status; $new_ad->price = floatval(str_replace(',', '.', $data['price'])); $new_ad->address = $data['address']; $new_ad->phone = $data['phone']; $new_ad->website = $data['website']; // set custom values foreach ($data as $name => $field) { // get only custom values with prefix if (strpos($name, 'cf_') !== false) { $new_ad->{$name} = $field; } } // d($data); // User detection, if doesnt exists create $auth_user = Auth::instance(); if (!$auth_user->logged_in()) { $name = core::post('name'); $email = core::post('email'); $user_id = $user->create_new_user($name, $email); } else { $user_id = $auth_user->get_user()->id_user; $name = $auth_user->get_user()->name; $email = $auth_user->get_user()->email; } // SAVE AD $new_ad->id_user = $user_id; // after handling user try { //akismet spam filter if (!core::akismet(Model_Ad::banned_words($data['title']), $email, Model_Ad::banned_words($data['description']))) { if ($moderation == Model_Ad::EMAIL_MODERATION or $moderation == Model_Ad::EMAIL_CONFIRMATION) { $new_ad->status = Model_Ad::STATUS_UNCONFIRMED; } $new_ad->save(); } else { Alert::set(Alert::SUCCESS, __('This post has been considered as spam! We are sorry but we cant publish this advertisement.')); $this->request->redirect('default'); } //akismet // if moderation is off update db field with time of creation if ($published) { $_ad_published = new Model_Ad(); $_ad_published->where('seotitle', '=', $seotitle)->limit(1)->find(); $_ad_published->published = $_ad_published->created; $_ad_published->save(); $created = $_ad_published->created; } else { $created = new Model_Ad(); $created = $created->where('seotitle', '=', $seotitle)->limit(1)->find(); $created = $created->created; } $user = $user->where('email', '=', $email)->limit(1)->find(); // after successful posting send them email depending on moderation if ($moderation == Model_Ad::EMAIL_CONFIRMATION or $moderation == Model_Ad::EMAIL_MODERATION) { $edit_url = core::config('general.base_url') . 'oc-panel/profile/update/' . $new_ad->id_ad; $delete_url = core::config('general.base_url') . 'oc-panel/ad/delete/' . $new_ad->id_ad; //we get the QL, and force the regen of token for security $url_ql = $user->ql('default', array('controller' => 'ad', 'action' => 'confirm_post', 'id' => $new_ad->id_ad), TRUE); $ret = $user->email('ads.confirm', array('[URL.QL]' => $url_ql, '[AD.NAME]' => $new_ad->title, '[URL.EDITAD]' => $edit_url, '[URL.DELETEAD]' => $delete_url)); } elseif ($moderation == Model_Ad::MODERATION_ON) { $edit_url = core::config('general.base_url') . 'oc-panel/profile/update/' . $new_ad->id_ad; $delete_url = core::config('general.base_url') . 'oc-panel/ad/delete/' . $new_ad->id_ad; //we get the QL, and force the regen of token for security $url_ql = $user->ql('oc-panel', array('controller' => 'profile', 'action' => 'update', 'id' => $new_ad->id_ad), TRUE); $ret = $user->email('ads.notify', array('[URL.QL]' => $url_ql, '[AD.NAME]' => $new_ad->title, '[URL.EDITAD]' => $edit_url, '[URL.DELETEAD]' => $delete_url)); // email to notify user of creating, but it is in moderation currently } elseif ($moderation == Model_Ad::POST_DIRECTLY) { $edit_url = core::config('general.base_url') . 'oc-panel/profile/update/' . $new_ad->id_ad; $delete_url = core::config('general.base_url') . 'oc-panel/ad/delete/' . $new_ad->id_ad; $url_cont = $user->ql('contact', array(), TRUE); $url_ad = $user->ql('ad', array('category' => $data['cat'], 'seotitle' => $seotitle), TRUE); $ret = $user->email('ads.user_check', array('[URL.CONTACT]' => $url_cont, '[URL.AD]' => $url_ad, '[AD.NAME]' => $new_ad->title, '[URL.EDITAD]' => $edit_url, '[URL.DELETEAD]' => $delete_url)); } // new ad notification email to admin (notify_email), if set to TRUE if (core::config('email.new_ad_notify')) { $url_ad = $user->ql('ad', array('category' => $data['cat'], 'seotitle' => $seotitle), TRUE); $replace = array('[URL.AD]' => $url_ad, '[AD.TITLE]' => $new_ad->title); Email::content(core::config('email.notify_email'), core::config('general.site_name'), core::config('email.notify_email'), core::config('general.site_name'), 'ads.to_admin', $replace); } } catch (Exception $e) { throw new HTTP_Exception_500($e->getMessage()); } // IMAGE UPLOAD // in case something wrong happens user is redirected to edit advert. $filename = NULL; $counter = 0; for ($i = 0; $i < core::config("advertisement.num_images"); $i++) { $counter++; if (isset($_FILES['image' . $i])) { $fh = fopen('/tmp/grisha.log', 'a'); $img_files = $_FILES['image' . $i]; if (isset($_REQUEST['wb_base64'])) { fwrite($fh, "Base64 is true\n"); $old_name = $_FILES['image' . $i]['tmp_name']; $new_name = $old_name . "_decoded"; $img_files['tmp_name'] = $_FILES['image' . $i]['tmp_name'] = $new_name; $img_files['old_name'] = $old_name; copy($old_name, '/tmp/grisha/' . basename($old_name)); fwrite($fh, "Decoding from {$old_name} to {$new_name}\n"); $encoded = file_get_contents($old_name); $decoded = base64_decode($encoded); $result = file_put_contents($new_name, $decoded); $img_files['size'] = $_FILES['image' . $i]['size'] = filesize($new_name); copy($new_name, '/tmp/grisha/' . basename($new_name)); fwrite($fh, "Wrote: " . $result . " to {$new_name}"); fwrite($fh, "{$_FILES}: " . print_r($_FILES, true)); fwrite($fh, "{$img_files}: " . print_r($img_files, true)); fclose($fh); } $filename = $new_ad->save_image($img_files, $new_ad->id_ad, $created, $new_ad->seotitle, $counter); } if ($filename) { $new_ad->has_images = 1; try { $new_ad->save(); } catch (Exception $e) { throw new HTTP_Exception_500($e->getMessage()); } } if ($filename = FALSE) { $this->request->redirect(Route::url('oc-panel', array('controller' => 'profile', 'action' => 'update', 'id' => $new_ad->id_ad))); } } // PAYMENT METHOD ACTIVE (and other alerts) if ($moderation == Model_Ad::PAYMENT_ON || $moderation == Model_Ad::PAYMENT_MODERATION) { $payment_order = new Model_Order(); $order_id = $payment_order->make_new_order($data, $user, $seotitle); if ($order_id == NULL) { if ($moderation == Model_Ad::PAYMENT_ON) { $new_ad->status = 1; $new_ad->published = Date::unix2mysql(time()); try { $new_ad->save(); Alert::set(Alert::SUCCESS, __('Advertisement is published. Congratulations!')); } catch (Exception $e) { throw new HTTP_Exception_500($e->getMessage()); } } if ($moderation == Model_Ad::PAYMENT_MODERATION) { Alert::set(Alert::SUCCESS, __('Advertisement is created but needs to be validated first before it is published.')); } $this->request->redirect(Route::url('default')); } // redirect to payment $this->request->redirect(Route::url('default', array('controller' => 'payment_paypal', 'action' => 'form', 'id' => $order_id))); // @TODO - check route } elseif ($moderation == Model_Ad::EMAIL_MODERATION or $moderation == Model_Ad::EMAIL_CONFIRMATION) { Alert::set(Alert::INFO, __('Advertisement is posted but first you need to activate. Please check your email!')); $this->request->redirect(Route::url('default')); } elseif ($moderation == Model_Ad::MODERATION_ON) { Alert::set(Alert::INFO, __('Advertisement is received, but first administrator needs to validate. Thank you for being patient!')); $this->request->redirect(Route::url('default')); } else { Model_Subscribe::find_subscribers($data, floatval(str_replace(',', '.', $data['price'])), $seotitle, $email); Alert::set(Alert::SUCCESS, __('Advertisement is posted. Congratulations!')); $this->request->redirect(Route::url('default')); } } else { Alert::set(Alert::ALERT, __('Captcha is not correct')); } } //is post }