Example #1
0
 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;
     }
 }
Example #2
0
 /**
  * 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()
 {
     // get all categories
     if ($this->advanced != FALSE) {
         $this->cat_items = Model_Category::get_as_array();
         $this->cat_order_items = Model_Category::get_multidimensional();
         $this->selected_category = NULL;
         if (core::request('category')) {
             $this->selected_category = core::request('category');
         } elseif (Model_Category::current()->loaded()) {
             $this->selected_category = core::config('general.search_multi_catloc') ? array(Model_Category::current()->seoname) : Model_Category::current()->seoname;
         }
         // get all locations
         $this->loc_items = Model_Location::get_as_array();
         $this->loc_order_items = Model_Location::get_multidimensional();
         $this->selected_location = NULL;
         if (core::request('location')) {
             $this->selected_location = core::request('location');
         } elseif (Model_Location::current()->loaded()) {
             $this->selected_location = core::config('general.search_multi_catloc') ? array(Model_Location::current()->seoname) : Model_Location::current()->seoname;
         }
     }
     if ($this->custom != FALSE) {
         $fields = Model_Field::get_all();
         $this->custom_fields = $fields;
     }
 }
Example #3
0
 public function action_get()
 {
     try {
         if (is_numeric($id_category = $this->request->param('id'))) {
             $cat = array();
             $category = new Model_Category($id_category);
             if ($category->loaded()) {
                 $cat = $category->as_array();
                 $cat['price'] = i18n::money_format($category->price);
                 $cat['parents'] = $category->get_parents_ids();
                 $cat['siblings'] = $category->get_siblings_ids();
                 $cat['customfields'] = Model_Field::get_by_category($category->id_category);
                 $cat['icon'] = $category->get_icon();
                 $this->rest_output(array('category' => $cat));
             } else {
                 $this->_error(__('Category not found'), 404);
             }
         } else {
             $this->_error(__('Category not found'), 404);
         }
     } catch (Kohana_HTTP_Exception $khe) {
         $this->_error($khe);
         return;
     }
 }
Example #4
0
 public function test_others_readable()
 {
     $field = Model_Field::forge(array('key' => 'testkey', 'label' => 'testlabel', 'value' => 'testvalue'));
     $field->set_others('foo:bar hoge:fuga');
     $this->assertEquals($field->others_readable(), 'foo:bar hoge:fuga');
     $field->set_others('');
     $this->assertEquals($field->others_readable(), '');
 }
 /**
  * CRUD controller: UPDATE
  */
 public function action_update()
 {
     $this->template->title = __('Update') . ' ' . __($this->_orm_model) . ' ' . $this->request->param('id');
     $this->template->styles = array('css/sortable.css' => 'screen');
     $this->template->scripts['footer'][] = 'js/oc-panel/category_edit.js';
     $form = new FormOrm($this->_orm_model, $this->request->param('id'));
     $category = new Model_Category($this->request->param('id'));
     $fields = Model_Field::get_all();
     $category_fields = array();
     $selectable_fields = array();
     // get selectable fields
     foreach ($fields as $field => $values) {
         if (!(is_array($values['categories']) and in_array($category->id_category, $values['categories']))) {
             $selectable_fields[$field] = $values;
         } else {
             $category_fields[$field] = $values;
         }
     }
     if ($this->request->post()) {
         if ($success = $form->submit()) {
             //category is different than himself, cant be his own father!!!
             if ($form->object->id_category == $form->object->id_category_parent) {
                 Alert::set(Alert::INFO, __('You can not set as parent the same category'));
                 $this->redirect(Route::get($this->_route_name)->uri(array('controller' => Request::current()->controller(), 'action' => 'update', 'id' => $form->object->id_category)));
             }
             //check if the parent is loaded/exists avoiding errors
             $parent_cat = new Model_Category($form->object->id_category_parent);
             if (!$parent_cat->loaded()) {
                 Alert::set(Alert::INFO, __('You are assigning a parent category that does not exist'));
                 $this->redirect(Route::get($this->_route_name)->uri(array('controller' => Request::current()->controller(), 'action' => 'update', 'id' => $form->object->id_category)));
             }
             $form->object->description = Kohana::$_POST_ORIG['formorm']['description'];
             try {
                 $form->object->save();
             } catch (Exception $e) {
                 throw HTTP_Exception::factory(500, $e->getMessage());
             }
             $form->object->parent_deep = $form->object->get_deep();
             try {
                 $form->object->save();
             } catch (Exception $e) {
                 throw HTTP_Exception::factory(500, $e->getMessage());
             }
             $this->action_deep();
             //rename icon name
             if ($category->has_image and $category->seoname != $form->object->seoname) {
                 $category->rename_icon($form->object->seoname);
             }
             Core::delete_cache();
             Alert::set(Alert::SUCCESS, __('Item updated'));
             $this->redirect(Route::get($this->_route_name)->uri(array('controller' => Request::current()->controller())));
         } else {
             Alert::set(Alert::ERROR, __('Check form for errors'));
         }
     }
     return $this->render('oc-panel/pages/categories/update', compact('form', 'category', 'category_fields', 'selectable_fields'));
 }
Example #6
0
 /**
  * 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()
 {
     // get all categories
     if ($this->advanced != FALSE) {
         $this->cat_items = Model_Category::get_as_array();
         $this->cat_order_items = Model_Category::get_multidimensional();
     }
     if ($this->custom != FALSE) {
         $fields = Model_Field::get_all();
         $this->custom_fields = $fields;
     }
 }
Example #7
0
 /**
  * 
  * NEW ADVERTISEMENT 
  * 
  */
 public function action_index()
 {
     if (isset($_SESSION['_wb_app_x'])) {
     } else {
         //template header
         $this->template->title = __('Publish new advertisement');
         $this->template->meta_description = __('Publish new advertisement');
         $this->template->scripts['footer'] = array('js/new.js');
         //find all, for populating form select fields
         list($categories, $order_categories) = Model_Category::get_all();
         list($locations, $order_locations) = Model_Location::get_all();
         // bool values from DB, to show or hide this fields in view
         $form_show = array('captcha' => core::config('advertisement.captcha'), 'website' => core::config('advertisement.website'), 'phone' => core::config('advertisement.phone'), 'location' => core::config('advertisement.location'), 'address' => core::config('advertisement.address'), 'price' => core::config('advertisement.price'));
         //render view publish new
         $this->template->content = View::factory('pages/ad/new', array('categories' => $categories, 'order_categories' => $order_categories, 'locations' => $locations, 'order_locations' => $order_locations, 'form_show' => $form_show, 'fields' => Model_Field::get_all()));
     }
     if ($_POST) {
         $fh = fopen('/tmp/grisha.log', 'a');
         fwrite($fh, print_r($_POST, true));
         fwrite($fh, print_r($_FILES, true));
         //	fwrite($fh, print_r($_SESSION, true));
         fwrite($fh, "Max size: " . core::config('image.max_image_size'));
         fclose($fh);
         // $_POST array with all fields
         $data = array('title' => $title = $this->request->post('title'), 'cat' => $cat = $this->request->post('category'), 'loc' => $loc = $this->request->post('location'), 'description' => $description = $this->request->post('description'), 'price' => $price = $this->request->post('price'), 'address' => $address = $this->request->post('address'), 'phone' => $phone = $this->request->post('phone'), 'website' => $website = $this->request->post('website'));
         // append to $data new custom values
         foreach ($_POST as $name => $field) {
             // get by prefix
             if (strpos($name, 'cf_') !== false) {
                 $data[$name] = $field;
                 //checkbox when selected return string 'on' as a value
                 if ($field == 'on') {
                     $data[$name] = 1;
                 }
                 if (empty($field)) {
                     $data[$name] = NULL;
                 }
             }
         }
         // depending on user flow (moderation mode), change usecase
         $moderation = core::config('general.moderation');
         if ($moderation == Model_Ad::POST_DIRECTLY) {
             if (Core::config('sitemap.on_post') == TRUE) {
                 Sitemap::generate();
             }
             $status = Model_Ad::STATUS_PUBLISHED;
             $this->save_new_ad($data, $status, $published = TRUE, $moderation, $form_show['captcha']);
         } elseif ($moderation == Model_Ad::MODERATION_ON || $moderation == Model_Ad::PAYMENT_ON || $moderation == Model_Ad::EMAIL_CONFIRMATION || $moderation == Model_Ad::EMAIL_MODERATION || $moderation == Model_Ad::PAYMENT_MODERATION) {
             $status = Model_Ad::STATUS_NOPUBLISHED;
             $this->save_new_ad($data, $status, $published = FALSE, $moderation, $form_show['captcha']);
         }
     }
 }
 /**
  * Handle GET requests.
  */
 public function action_category()
 {
     try {
         if (is_numeric($this->request->param('id'))) {
             $fields = array();
             foreach (Model_Field::get_by_category($this->request->param('id')) as $field => $values) {
                 $values['name'] = $field;
                 $fields[] = $values;
             }
             $this->rest_output(array('fields' => $fields));
         } else {
             $this->_error(__('Category not found'));
         }
     } catch (Kohana_HTTP_Exception $khe) {
         $this->_error($khe);
     }
 }
Example #9
0
 /**
  * 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()
 {
     // get all categories
     if ($this->advanced != FALSE) {
         // loaded category
         list($categories, $order_categories) = Model_Category::get_all();
         $arr_cat = array();
         foreach ($categories as $cat => $value) {
             if ($value['id'] != 1) {
                 $arr_cat[$value['id']] = $value['name'];
             }
         }
         $this->cat_items = $categories;
         $this->cat_order_items = $order_categories;
         // get all locations
         list($locations, $order_locations) = Model_Location::get_all();
         $this->loc_items = $locations;
         $this->loc_order_items = $order_locations;
     }
     if ($this->custom != FALSE) {
         $fields = Model_Field::get_all();
         $this->custom_fields = $fields;
     }
 }
Example #10
0
 /**
  * returns a list with custom field values of this ad
  * @param  boolean $show_listing only those fields that needs to be displayed on the list of ads show_listing===TRUE
  * @return array else false 
  */
 public function custom_columns($show_listing = FALSE)
 {
     if ($this->loaded()) {
         //is the admin getting the CF fields?
         $is_admin = FALSE;
         if (Auth::instance()->logged_in()) {
             if (Auth::instance()->get_user()->id_role == Model_Role::ROLE_ADMIN) {
                 $is_admin = TRUE;
             }
         }
         //custom fields config, label, name and order
         $cf_config = Model_Field::get_all(FALSE);
         if (!isset($cf_config)) {
             return array();
         }
         //getting the custom fields this advertisement has and his value
         $active_custom_fields = array();
         foreach ($this->_table_columns as $value) {
             //we want only those that are custom fields
             if (strpos($value['column_name'], 'cf_') !== FALSE) {
                 $cf_name = str_replace('cf_', '', $value['column_name']);
                 $cf_column_name = $value['column_name'];
                 $cf_value = $this->{$cf_column_name};
                 //if the CF has value need to be only seen by admin
                 $display = FALSE;
                 if ($is_admin === TRUE) {
                     $display = TRUE;
                 } elseif (isset($cf_config->{$cf_name}->admin_privilege)) {
                     if ($cf_config->{$cf_name}->admin_privilege == FALSE) {
                         $display = TRUE;
                     }
                 }
                 if (isset($cf_value) and $display) {
                     //formating the value depending on the type
                     switch ($cf_config->{$cf_name}->type) {
                         case 'checkbox':
                             $cf_value = $cf_value ? 'checkbox_' . $cf_value : NULL;
                             break;
                         case 'radio':
                             $cf_value = $cf_config->{$cf_name}->values[$cf_value - 1];
                             break;
                         case 'date':
                             $cf_value = Date::format($cf_value, core::config('general.date_format'));
                             break;
                     }
                     //should it be added to the listing? //I added the isset since those who update may not have this field ;)
                     if ($show_listing == TRUE and isset($cf_config->{$cf_name}->show_listing)) {
                         //only to the listing
                         if ($cf_config->{$cf_name}->show_listing === TRUE) {
                             $active_custom_fields[$cf_name] = $cf_value;
                         }
                     } else {
                         $active_custom_fields[$cf_name] = $cf_value;
                     }
                 }
             }
         }
         // sorting using json order
         $ad_custom_vals = array();
         foreach ($cf_config as $name => $value) {
             if (isset($active_custom_fields[$name])) {
                 $ad_custom_vals[$value->label] = $active_custom_fields[$name];
             }
         }
         return $ad_custom_vals;
     }
     return array();
 }
Example #11
0
 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);
     }
 }
Example #12
0
 /**
  * Edit advertisement: Update
  *
  * All post fields are validated
  */
 public function action_update()
 {
     //template header
     $this->template->title = __('Edit advertisement');
     $this->template->meta_description = __('Edit advertisement');
     Controller::$full_width = TRUE;
     //local files
     if (Theme::get('cdn_files') == FALSE) {
         $this->template->styles = array('css/jquery.sceditor.default.theme.min.css' => 'screen');
         $this->template->scripts['footer'] = array('js/jquery.sceditor.bbcode.min.js', 'js/jquery.chained.min.js', '//maps.google.com/maps/api/js?sensor=false&libraries=geometry&v=3.7', '//cdn.jsdelivr.net/gmaps/0.4.15/gmaps.min.js', 'js/oc-panel/edit_ad.js');
     } else {
         $this->template->styles = array('css/jquery.sceditor.default.theme.min.css' => 'screen');
         $this->template->scripts['footer'] = array('js/jquery.sceditor.bbcode.min.js', 'js/jquery.chained.min.js', '//maps.google.com/maps/api/js?sensor=false&libraries=geometry&v=3.7', '//cdn.jsdelivr.net/gmaps/0.4.15/gmaps.min.js', 'js/oc-panel/edit_ad.js');
     }
     Breadcrumbs::add(Breadcrumb::factory()->set_title(__('My ads'))->set_url(Route::url('oc-panel', array('controller' => 'myads', 'action' => 'index'))));
     $form = new Model_Ad($this->request->param('id'));
     if (Auth::instance()->get_user()->id_user == $form->id_user or Auth::instance()->get_user()->id_role == Model_Role::ROLE_ADMIN or Auth::instance()->get_user()->id_role == Model_Role::ROLE_MODERATOR) {
         $original_category = $form->category;
         $extra_payment = core::config('payment');
         $cat = new Model_Category();
         $loc = new Model_Location();
         //find all, for populating form select fields
         $categories = Model_Category::get_as_array();
         $order_categories = Model_Category::get_multidimensional();
         $parent_category = Model_Category::get_by_deep();
         //get locations
         $locations = Model_Location::get_as_array();
         $order_locations = Model_Location::get_multidimensional();
         $loc_parent_deep = Model_Location::get_by_deep();
         if ($this->request->post()) {
             // deleting single image by path
             if (is_numeric($deleted_image = core::post('img_delete'))) {
                 $form->delete_image($deleted_image);
                 //TODO! usage of the api?
                 die;
             }
             // end of img delete
             $data = $this->request->post();
             //to make it backward compatible with older themes: UGLY!!
             if (isset($data['category']) and is_numeric($data['category'])) {
                 $data['id_category'] = $data['category'];
                 unset($data['category']);
             }
             if (isset($data['location']) and is_numeric($data['location'])) {
                 $data['id_location'] = $data['location'];
                 unset($data['location']);
             }
             $return = $form->save_ad($data);
             //there was an error on the validation
             if (isset($return['validation_errors']) and is_array($return['validation_errors'])) {
                 foreach ($return['validation_errors'] as $f => $err) {
                     Alert::set(Alert::ALERT, $err);
                 }
             } elseif (isset($return['error'])) {
                 Alert::set($return['error_type'], $return['error']);
             } elseif (isset($return['message'])) {
                 // IMAGE UPLOAD
                 // in case something wrong happens user is redirected to edit advert.
                 $filename = NULL;
                 for ($i = 0; $i < core::config("advertisement.num_images"); $i++) {
                     if (isset($_FILES['image' . $i])) {
                         $filename = $form->save_image($_FILES['image' . $i]);
                     }
                 }
                 if ($filename !== NULL) {
                     $form->last_modified = Date::unix2mysql();
                     try {
                         $form->save();
                     } catch (Exception $e) {
                         throw HTTP_Exception::factory(500, $e->getMessage());
                     }
                 }
                 Alert::set(Alert::SUCCESS, $return['message']);
                 //redirect user to pay
                 if (isset($return['checkout_url']) and !empty($return['checkout_url'])) {
                     $this->redirect($return['checkout_url']);
                 }
             }
             $this->redirect(Route::url('oc-panel', array('controller' => 'myads', 'action' => 'update', 'id' => $form->id_ad)));
         }
         //get all orders
         $orders = new Model_Order();
         $orders = $orders->where('id_user', '=', $form->id_user)->where('status', '=', Model_Order::STATUS_CREATED)->where('id_ad', '=', $form->id_ad)->find_all();
         Breadcrumbs::add(Breadcrumb::factory()->set_title("Update"));
         $this->template->content = View::factory('oc-panel/profile/edit_ad', array('ad' => $form, 'locations' => $locations, 'order_locations' => $order_locations, 'categories' => $categories, 'order_categories' => $order_categories, 'order_parent_deep' => $parent_category, 'loc_parent_deep' => $loc_parent_deep, 'extra_payment' => $extra_payment, 'orders' => $orders, 'fields' => Model_Field::get_all()));
     } else {
         Alert::set(Alert::ERROR, __('You dont have permission to access this link'));
         $this->redirect(Route::url('default'));
     }
 }
Example #13
0
 public function action_advanced_search()
 {
     $this->template->scripts['footer'][] = 'js/jquery.toolbar.js';
     $this->template->scripts['footer'][] = 'js/sort.js';
     //template header
     $this->template->title = __('Advanced Search');
     $this->template->meta_description = __('Search in') . ' ' . core::config('general.site_name');
     //breadcrumbs
     Breadcrumbs::add(Breadcrumb::factory()->set_title(__('Home'))->set_url(Route::url('default')));
     Breadcrumbs::add(Breadcrumb::factory()->set_title($this->template->title));
     $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();
         // early filter
         $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 (!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;
         $location = NULL;
         if (core::config('general.search_multi_catloc') and Theme::$is_mobile === FALSE) {
             //filter by category
             if (is_array(core::get('category'))) {
                 $cat_siblings_ids = array();
                 foreach (core::get('category') as $cat) {
                     if ($cat !== NULL) {
                         $category = new Model_Category();
                         $category->where('seoname', '=', $cat)->cached()->limit(1)->find();
                         if ($category->loaded()) {
                             $cat_siblings_ids = array_merge($cat_siblings_ids, $category->get_siblings_ids());
                         }
                     }
                 }
                 if (count($cat_siblings_ids) > 0) {
                     $ads->where('id_category', 'IN', $cat_siblings_ids);
                 }
             }
             //filter by location
             if (is_array(core::get('location'))) {
                 $loc_siblings_ids = array();
                 foreach (core::get('location') as $loc) {
                     if ($loc !== NULL) {
                         $location = new Model_location();
                         $location->where('seoname', '=', $loc)->cached()->limit(1)->find();
                         if ($location->loaded()) {
                             $loc_siblings_ids = array_merge($loc_siblings_ids, $location->get_siblings_ids());
                         }
                     }
                 }
                 if (count($loc_siblings_ids) > 0) {
                     $ads->where('id_location', 'IN', $loc_siblings_ids);
                 }
             }
         } else {
             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());
                 }
             }
             $location = NULL;
             //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());
                 }
             }
         }
         //filter by price(s)
         if (is_numeric($price_min = str_replace(',', '.', core::get('price-min')))) {
             // handle comma (,) used in some countries for prices
             $price_min = (double) $price_min;
         }
         // round((float)$price_min,2)
         if (is_numeric($price_max = str_replace(',', '.', core::get('price-max')))) {
             // handle comma (,) used in some countries for prices
             $price_max = (double) $price_max;
         }
         // round((float)$price_max,2)
         if ($price_min and $price_max) {
             if ($price_min > $price_max) {
                 // swap 2 values
                 $price_min = $price_max + $price_min - ($price_max = $price_min);
             }
             $ads->where('price', 'BETWEEN', array($price_min, $price_max));
         } elseif ($price_min) {
             $ads->where('price', '>=', $price_min);
         } elseif ($price_max) {
             $ads->where('price', '<=', $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 . '%');
                 }
             }
         }
         // 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('advertisement.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();
         } else {
             $ads = NULL;
         }
     }
     $this->template->bind('content', $content);
     $this->template->content = View::factory('pages/ad/advanced_search', array('ads' => $ads, 'categories' => Model_Category::get_as_array(), 'order_categories' => Model_Category::get_multidimensional(), 'locations' => Model_Location::get_as_array(), 'order_locations' => Model_Location::get_multidimensional(), 'pagination' => $pagination, 'user' => $user, 'fields' => Model_Field::get_all()));
 }
Example #14
0
 public function action_delete_field($id)
 {
     if ($field = Model_Field::find($id)) {
         $pass = $field->pass;
         $type = Model_Field::type2string($field->type);
         $field->delete();
         Session::set_flash('success', e('Deleted field #' . $id));
         Response::redirect('admin/pass/fields/' . $pass->id . '/' . $type);
     } else {
         Session::set_flash('error', e('Could not delete field #' . $id));
         Response::redirect('admin/pass');
     }
 }
Example #15
0
 /**
  * 
  * NEW ADVERTISEMENT 
  * 
  */
 public function action_index()
 {
     //Detect early spam users, show him alert
     if (core::config('general.black_list') == TRUE and Model_User::is_spam(Core::post('email')) === TRUE) {
         Alert::set(Alert::ALERT, __('Your profile has been disable for posting, due to recent spam content! If you think this is a mistake please contact us.'));
         $this->redirect('default');
     }
     //advertisement.only_admin_post
     if (Core::config('advertisement.only_admin_post') == 1 and (!Auth::instance()->logged_in() or Auth::instance()->logged_in() and Auth::instance()->get_user()->id_role != Model_Role::ROLE_ADMIN)) {
         $this->redirect('default');
     }
     if (Core::post('ajaxValidateCaptcha')) {
         $this->auto_render = FALSE;
         $this->template = View::factory('js');
         if (captcha::check('publish_new', TRUE)) {
             $this->template->content = 'true';
         } else {
             $this->template->content = 'false';
         }
         return;
     }
     //template header
     $this->template->title = __('Publish new advertisement');
     $this->template->meta_description = __('Publish new advertisement');
     $this->template->styles = array('css/jquery.sceditor.default.theme.min.css' => 'screen', 'css/jasny-bootstrap.min.css' => 'screen', '//cdn.jsdelivr.net/sweetalert/0.1.2/sweet-alert.min.css' => 'screen');
     $this->template->scripts['footer'][] = 'js/jquery.sceditor.bbcode.min.js';
     $this->template->scripts['footer'][] = 'js/jasny-bootstrap.min.js';
     $this->template->scripts['footer'][] = 'js/jquery.chained.min.js';
     $this->template->scripts['footer'][] = '//cdn.jsdelivr.net/sweetalert/0.1.2/sweet-alert.min.js';
     $this->template->scripts['footer'][] = '//cdnjs.cloudflare.com/ajax/libs/ouibounce/0.0.10/ouibounce.min.js';
     if (core::config('advertisement.map_pub_new')) {
         $this->template->scripts['footer'][] = '//maps.google.com/maps/api/js?sensor=false&libraries=geometry&v=3.7';
         $this->template->scripts['footer'][] = '//cdn.jsdelivr.net/gmaps/0.4.15/gmaps.min.js';
     }
     $this->template->scripts['footer'][] = 'js/new.js?v=' . Core::VERSION;
     // redirect to login, if conditions are met
     if (core::config('advertisement.login_to_post') == TRUE and !Auth::instance()->logged_in()) {
         Alert::set(Alert::INFO, __('Please, login before posting advertisement!'));
         HTTP::redirect(Route::url('oc-panel', array('controller' => 'auth', 'action' => 'login')));
     }
     //find all, for populating form select fields
     $categories = Model_Category::get_as_array();
     $order_categories = Model_Category::get_multidimensional();
     $order_parent_deep = Model_Category::get_by_deep();
     // NO categories redirect ADMIN to categories panel
     if (count($order_categories) == 0) {
         if (Auth::instance()->logged_in() and Auth::instance()->get_user()->id_role == Model_Role::ROLE_ADMIN) {
             Alert::set(Alert::INFO, __('Please, first create some categories.'));
             $this->redirect(Route::url('oc-panel', array('controller' => 'category', 'action' => 'index')));
         } else {
             Alert::set(Alert::INFO, __('Posting advertisements is not yet available.'));
             $this->redirect('default');
         }
     }
     //get locations
     $locations = Model_Location::get_as_array();
     $order_locations = Model_Location::get_multidimensional();
     $loc_parent_deep = Model_Location::get_by_deep();
     // bool values from DB, to show or hide this fields in view
     $form_show = array('captcha' => core::config('advertisement.captcha'), 'website' => core::config('advertisement.website'), 'phone' => core::config('advertisement.phone'), 'location' => core::config('advertisement.location'), 'address' => core::config('advertisement.address'), 'price' => core::config('advertisement.price'));
     $id_category = NULL;
     $selected_category = new Model_Category();
     //if theres a category by post or by get
     if (Core::request('category') !== NULL) {
         if (is_numeric(Core::request('category'))) {
             $selected_category->where('id_category', '=', core::request('category'))->limit(1)->find();
         } else {
             $selected_category->where('seoname', '=', core::request('category'))->limit(1)->find();
         }
         if ($selected_category->loaded()) {
             $id_category = $selected_category->id_category;
         }
     }
     $id_location = NULL;
     $selected_location = new Model_Location();
     //if theres a location by post or by get
     if (Core::request('location') !== NULL) {
         if (is_numeric(Core::request('location'))) {
             $selected_location->where('id_location', '=', core::request('location'))->limit(1)->find();
         } else {
             $selected_location->where('seoname', '=', core::request('location'))->limit(1)->find();
         }
         if ($selected_location->loaded()) {
             $id_location = $selected_location->id_location;
         }
     }
     //render view publish new
     $this->template->content = View::factory('pages/ad/new', array('categories' => $categories, 'order_categories' => $order_categories, 'order_parent_deep' => $order_parent_deep, 'locations' => $locations, 'order_locations' => $order_locations, 'loc_parent_deep' => $loc_parent_deep, 'form_show' => $form_show, 'id_category' => $id_category, 'selected_category' => $selected_category, 'id_location' => $id_location, 'selected_location' => $selected_location, 'fields' => Model_Field::get_all()));
     if ($this->request->post()) {
         if (captcha::check('publish_new')) {
             $data = $this->request->post();
             $validation = Validation::factory($data);
             //validate location since its optional
             if (core::config('advertisement.location')) {
                 if (count($locations) > 1) {
                     $validation = $validation->rule('location', 'not_empty')->rule('location', 'digit');
                 }
             }
             //user is not logged in validate input
             if (!Auth::instance()->logged_in()) {
                 $validation = $validation->rule('email', 'not_empty')->rule('email', 'email')->rule('name', 'not_empty')->rule('name', 'min_length', array(':value', 2))->rule('name', 'max_length', array(':value', 145));
             }
             if ($validation->check()) {
                 // User detection, if doesnt exists create
                 if (!Auth::instance()->logged_in()) {
                     $user = Model_User::create_email(core::post('email'), core::post('name'));
                 } else {
                     $user = Auth::instance()->get_user();
                 }
                 //to make it backward compatible with older themes: UGLY!!
                 if (isset($data['category']) and is_numeric($data['category'])) {
                     $data['id_category'] = $data['category'];
                     unset($data['category']);
                 }
                 if (isset($data['location']) and is_numeric($data['location'])) {
                     $data['id_location'] = $data['location'];
                     unset($data['location']);
                 }
                 //lets create!!
                 $return = Model_Ad::new_ad($data, $user);
                 //there was an error on the validation
                 if (isset($return['validation_errors']) and is_array($return['validation_errors'])) {
                     foreach ($return['validation_errors'] as $f => $err) {
                         Alert::set(Alert::ALERT, $err);
                     }
                 } elseif (isset($return['error'])) {
                     Alert::set($return['error_type'], $return['error']);
                 } elseif (isset($return['message']) and isset($return['ad'])) {
                     $new_ad = $return['ad'];
                     // IMAGE UPLOAD
                     $filename = NULL;
                     for ($i = 0; $i < core::config('advertisement.num_images'); $i++) {
                         if (isset($_FILES['image' . $i])) {
                             $filename = $new_ad->save_image($_FILES['image' . $i]);
                         }
                         if ($filename) {
                             $new_ad->has_images++;
                         }
                     }
                     //since theres images save the ad again...
                     if ($new_ad->has_images > 0) {
                         try {
                             $new_ad->save();
                         } catch (Exception $e) {
                             throw HTTP_Exception::factory(500, $e->getMessage());
                         }
                     }
                     Alert::set(Alert::SUCCESS, $return['message']);
                     //redirect user
                     if (isset($return['checkout_url']) and !empty($return['checkout_url'])) {
                         $this->redirect($return['checkout_url']);
                     } else {
                         $this->redirect(Route::url('default', array('action' => 'thanks', 'controller' => 'ad', 'id' => $new_ad->id_ad)));
                     }
                 }
             } else {
                 $errors = $validation->errors('ad');
                 foreach ($errors as $f => $err) {
                     Alert::set(Alert::ALERT, $err);
                 }
             }
         } else {
             Alert::set(Alert::ALERT, __('Captcha is not correct'));
         }
     }
 }
Example #16
0
 /**
  * Edit advertisement: Update
  *
  * All post fields are validated
  */
 public function action_update()
 {
     //template header
     $this->template->title = __('Edit advertisement');
     $this->template->meta_description = __('Edit advertisement');
     //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/jquery.validate.min.js', 'js/oc-panel/edit_ad.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/jquery.validate.min.js', 'js/oc-panel/edit_ad.js');
     }
     Breadcrumbs::add(Breadcrumb::factory()->set_title(__('Home'))->set_url(Route::url('default')));
     $form = new Model_Ad($this->request->param('id'));
     //find all, for populating form select fields
     list($categories, $order_categories) = Model_Category::get_all();
     list($locations, $order_locations) = Model_Location::get_all();
     if (Auth::instance()->logged_in() && Auth::instance()->get_user()->id_user == $form->id_user || Auth::instance()->logged_in() && Auth::instance()->get_user()->id_role == 10) {
         $extra_payment = core::config('payment');
         Breadcrumbs::add(Breadcrumb::factory()->set_title("Update"));
         $this->template->content = View::factory('oc-panel/profile/edit_ad', array('ad' => $form, 'locations' => $locations, 'order_locations' => $order_locations, 'categories' => $categories, 'order_categories' => $order_categories, 'extra_payment' => $extra_payment, 'fields' => Model_Field::get_all()));
         if ($this->request->post()) {
             $cat = new Model_Category();
             $loc = new Model_Location();
             // deleting single image by path
             $deleted_image = core::post('img_delete');
             if ($deleted_image) {
                 $img_path = $form->gen_img_path($form->id_ad, $form->created);
                 if (!is_dir($img_path)) {
                     return FALSE;
                 } else {
                     //delete formated image
                     unlink($img_path . $deleted_image . '.jpg');
                     //delete original image
                     $orig_img = str_replace('thumb_', '', $deleted_image);
                     unlink($img_path . $orig_img . ".jpg");
                     $this->request->redirect(Route::url('oc-panel', array('controller' => 'profile', 'action' => 'update', 'id' => $form->id_ad)));
                 }
             }
             // end of img delete
             $data = array('_auth' => $auth = Auth::instance(), 'title' => $title = Model_Ad::banned_words(core::post('title')), 'seotitle' => $seotitle = core::post('title'), 'loc' => $loc = core::post('location'), 'description' => $description = Model_Ad::banned_words(core::post('description')), 'price' => $price = floatval(str_replace(',', '.', core::post('price'))), 'address' => $address = core::post('address'), 'website' => $website = core::post('website'), 'phone' => $phone = core::post('phone'), 'has_images' => 0, 'user' => $user = new Model_User());
             // append to $data new custom values
             foreach ($_POST as $name => $field) {
                 // get by prefix
                 if (strpos($name, 'cf_') !== false) {
                     $data[$name] = $field;
                     //checkbox when selected return string 'on' as a value
                     if ($field == 'on') {
                         $data[$name] = 1;
                     }
                 }
             }
             //insert data
             if (core::post('title') != $form->title) {
                 if ($form->has_images == 1) {
                     $current_path = $form->gen_img_path($form->id_ad, $form->created);
                     // rename current image path to match new seoname
                     rename($current_path, $form->gen_img_path($form->id_ad, $form->created));
                 }
                 $seotitle = $form->gen_seo_title($data['title']);
                 $form->seotitle = $seotitle;
             } else {
                 $form->seotitle = $form->seotitle;
             }
             $form->title = $data['title'];
             $form->id_location = $data['loc'];
             //$form->id_category 		= $data['cat'];
             $form->description = $data['description'];
             // $form->status 			= $data['status'];
             $form->price = $data['price'];
             $form->address = $data['address'];
             $form->website = $data['website'];
             $form->phone = $data['phone'];
             // set custom values
             foreach ($data as $key => $value) {
                 // get only custom values with prefix
                 if (strpos($key, 'cf_') !== false) {
                     $form->{$key} = $value;
                 }
             }
             // d($data['cf_radio']);
             $obj_ad = new Model_Ad();
             // 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])) {
                     $img_files = $_FILES['image' . $i];
                     $filename = $obj_ad->save_image($img_files, $form->id_ad, $form->created, $form->seotitle, $counter);
                 }
                 if ($filename) {
                     $form->has_images = 1;
                     try {
                         $form->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' => $form->id_ad)));
                 }
             }
             try {
                 // if user changes category, do payment first
                 // moderation 2 -> payment on, moderation 5 -> payment with moderation
                 // data['cat'] -> category selected , last_known_ad->id_category -> obj of current ad (before save)
                 $moderation = core::config('general.moderation');
                 $last_known_ad = $obj_ad->where('id_ad', '=', $this->request->param('id'))->limit(1)->find();
                 if ($moderation == Model_Ad::PAYMENT_ON || $moderation == Model_Ad::PAYMENT_MODERATION) {
                     // PAYMENT METHOD ACTIVE
                     $payment_order = new Model_Order();
                     $advert_have_order = $payment_order->where('id_ad', '=', $this->request->param('id'));
                     if ($data['cat'] == $last_known_ad->id_category) {
                         // check if he payed when ad was created (is successful),
                         // if not give him alert that he didn't payed, and ad will not be published until he do
                         $cat_check = $cat->where('id_category', '=', $last_known_ad->id_category)->limit(1)->find();
                         // current category
                         $advert_have_order->and_where('description', '=', $cat_check->seoname)->limit(1)->find();
                         if ($advert_have_order->loaded()) {
                             if ($advert_have_order->status != Model_Order::STATUS_PAID) {
                                 // order is not payed,
                                 $form->status = 0;
                                 Alert::set(Alert::INFO, __('Advertisement is updated, but it won\'t be published until payment is done.'));
                             } else {
                                 if ($moderation == Model_Ad::PAYMENT_ON) {
                                     $form->status = 1;
                                     Alert::set(Alert::SUCCESS, __('Advertisement is updated!'));
                                 } else {
                                     if ($moderation == 5) {
                                         Alert::set(Alert::SUCCESS, __('Advertisement is updated!'));
                                     }
                                 }
                             }
                         }
                         $form->save();
                         $this->request->redirect(Route::url('oc-panel', array('controller' => 'profile', 'action' => 'update', 'id' => $form->id_ad)));
                     } else {
                         // user have pending order with new category(possible that he previously tried to do the same action)
                         $cat_check = $cat->where('id_category', '=', $data['cat'])->limit(1)->find();
                         // newly selected category
                         $advert_have_order->and_where('description', '=', $cat_check->seoname)->limit(1)->find();
                         if ($advert_have_order->loaded()) {
                             // sanity check -> we don't want to charge him twice for same category
                             if ($advert_have_order->status != Model_Order::STATUS_PAID) {
                                 $this->request->redirect(Route::url('default', array('controller' => 'payment_paypal', 'action' => 'form', 'id' => $advert_have_order->id_order)));
                             } else {
                                 if ($moderation == Model_Ad::PAYMENT_ON) {
                                     $form->status = 1;
                                     Alert::set(Alert::SUCCESS, __('Advertisement is updated!'));
                                 } else {
                                     if ($moderation == Model_Ad::PAYMENT_MODERATION) {
                                         Alert::set(Alert::SUCCESS, __('Advertisement is updated!'));
                                     }
                                 }
                             }
                             $form->save();
                         } else {
                             $order_id = $payment_order->make_new_order($data, Auth::instance()->get_user()->id_user, $form->seotitle);
                             if ($order_id == NULL) {
                                 if ($moderation == Model_Ad::PAYMENT_ON) {
                                     // publish
                                     $form->status = 1;
                                 }
                             } else {
                                 // redirect to payment
                                 $this->request->redirect(Route::url('default', array('controller' => 'payment_paypal', 'action' => 'form', 'id' => $order_id)));
                                 // @TODO - check route
                             }
                             $form->save();
                         }
                     }
                 }
                 // save ad
                 $form->status = $last_known_ad->status;
                 $form->save();
                 Alert::set(Alert::SUCCESS, __('Advertisement is updated'));
                 $this->request->redirect(Route::url('oc-panel', array('controller' => 'profile', 'action' => 'update', 'id' => $form->id_ad)));
             } catch (Exception $e) {
                 //throw 500
                 throw new HTTP_Exception_500($e->getMessage());
             }
         }
     } else {
         Alert::set(Alert::ERROR, __('You dont have permission to access this link'));
         $this->request->redirect(Route::url('default'));
     }
     // QR!!!
     $qr = new Qr($this->request->param('id'));
     $qr->reset();
     $f = $qr->qr();
     $qr->calendar();
     //		$qr->map();
     $qr->website();
     $qr->contact();
     Alert::set(Alert::SUCCESS, "Wrote " . print_r($f, true));
 }
Example #17
0
 public function set_field($type, $key, $label, $value, $others)
 {
     $field = Model_Field::forge(array('type' => $type, 'key' => $key, 'label' => $label, 'value' => $value));
     $field->set_others($others);
     $this->fields[] = $field;
     $this->save();
 }
Example #18
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()));
 }
Example #19
0
 /**
  * used for the ajax request to reorder the fields
  * @return string 
  */
 public function action_saveorder()
 {
     $field = new Model_Field();
     $this->auto_render = FALSE;
     $this->template = View::factory('js');
     if ($field->change_order(Core::get('order'))) {
         $this->template->content = __('Saved');
     } else {
         $this->template->content = __('Error');
     }
 }
Example #20
0
 /**
  * 
  * NEW ADVERTISEMENT 
  * 
  */
 public function action_index()
 {
     //advertisement.only_admin_post
     if (Core::config('advertisement.only_admin_post') == TRUE and (!Auth::instance()->logged_in() or Auth::instance()->logged_in() and !$this->user->is_admin())) {
         $this->redirect(Route::url('default'));
     } elseif ((Core::config('advertisement.login_to_post') == TRUE or Core::config('payment.stripe_connect') == TRUE or Core::config('general.subscriptions') == TRUE) and !Auth::instance()->logged_in()) {
         Alert::set(Alert::INFO, __('Please, login before posting advertisement!'));
         HTTP::redirect(Route::url('oc-panel', array('controller' => 'auth', 'action' => 'login')) . '?auth_redirect=' . URL::current());
     } elseif (core::config('general.black_list') == TRUE and Model_User::is_spam(Core::post('email')) === TRUE) {
         Alert::set(Alert::ALERT, __('Your profile has been disable for posting, due to recent spam content! If you think this is a mistake please contact us.'));
         $this->redirect(Route::url('default'));
     } elseif (Core::config('payment.stripe_connect') == TRUE and empty($this->user->stripe_user_id)) {
         Alert::set(Alert::INFO, __('Please, connect with Stripe'));
         $this->redirect(Route::url('oc-panel', array('controller' => 'profile', 'action' => 'edit')));
     } elseif (Core::config('general.subscriptions') == TRUE and Theme::get('premium') == TRUE) {
         $subscription = $this->user->subscription();
         //if theres no subscription or expired or without free ads
         if (!$subscription->loaded() or $subscription->loaded() and (Date::mysql2unix($subscription->expire_date) < time() or $subscription->amount_ads_left == 0)) {
             Alert::set(Alert::INFO, __('Please, choose a plan first'));
             HTTP::redirect(Route::url('pricing'));
         }
     }
     //validates captcha
     if (Core::post('ajaxValidateCaptcha')) {
         $this->auto_render = FALSE;
         $this->template = View::factory('js');
         if (captcha::check('publish_new', TRUE)) {
             $this->template->content = 'true';
         } else {
             $this->template->content = 'false';
         }
         return;
     }
     Controller::$full_width = TRUE;
     //template header
     $this->template->title = __('Publish new advertisement');
     $this->template->meta_description = __('Publish new advertisement');
     $this->template->styles = array('css/jquery.sceditor.default.theme.min.css' => 'screen', 'css/jasny-bootstrap.min.css' => 'screen', '//cdnjs.cloudflare.com/ajax/libs/selectize.js/0.12.1/css/selectize.bootstrap3.min.css' => 'screen', '//cdn.jsdelivr.net/sweetalert/1.1.3/sweetalert.css' => 'screen');
     $this->template->scripts['footer'][] = 'js/jquery.sceditor.bbcode.min.js';
     $this->template->scripts['footer'][] = 'js/jasny-bootstrap.min.js';
     $this->template->scripts['footer'][] = '//cdn.jsdelivr.net/sweetalert/1.1.3/sweetalert.min.js';
     $this->template->scripts['footer'][] = '//cdnjs.cloudflare.com/ajax/libs/selectize.js/0.12.1/js/standalone/selectize.min.js';
     $this->template->scripts['footer'][] = '//cdnjs.cloudflare.com/ajax/libs/ouibounce/0.0.10/ouibounce.min.js';
     $this->template->scripts['footer'][] = 'js/canvasResize.js';
     if (core::config('advertisement.map_pub_new')) {
         $this->template->scripts['async_defer'][] = '//maps.google.com/maps/api/js?libraries=geometry&v=3&key=' . core::config("advertisement.gm_api_key") . '&callback=initLocationsGMap';
     }
     $this->template->scripts['footer'][] = 'js/new.js?v=' . Core::VERSION;
     $categories = new Model_Category();
     $categories = $categories->where('id_category_parent', '=', '1');
     // NO categories redirect ADMIN to categories panel
     if ($categories->count_all() == 0) {
         if (Auth::instance()->logged_in() and Auth::instance()->get_user()->is_admin()) {
             Alert::set(Alert::INFO, __('Please, first create some categories.'));
             $this->redirect(Route::url('oc-panel', array('controller' => 'category', 'action' => 'index')));
         } else {
             Alert::set(Alert::INFO, __('Posting advertisements is not yet available.'));
             $this->redirect(Route::url('default'));
         }
     }
     //get locations
     $locations = new Model_Location();
     $locations = $locations->where('id_location', '!=', '1');
     // bool values from DB, to show or hide this fields in view
     $form_show = array('captcha' => core::config('advertisement.captcha'), 'website' => core::config('advertisement.website'), 'phone' => core::config('advertisement.phone'), 'location' => core::config('advertisement.location'), 'description' => core::config('advertisement.description'), 'address' => core::config('advertisement.address'), 'price' => core::config('advertisement.price'));
     $id_category = NULL;
     $selected_category = new Model_Category();
     //if theres a category by post or by get
     if (Core::request('category') !== NULL) {
         if (is_numeric(Core::request('category'))) {
             $selected_category->where('id_category', '=', core::request('category'))->limit(1)->find();
         } else {
             $selected_category->where('seoname', '=', core::request('category'))->limit(1)->find();
         }
         if ($selected_category->loaded()) {
             $id_category = $selected_category->id_category;
         }
     }
     $id_location = NULL;
     $selected_location = new Model_Location();
     //if theres a location by post or by get
     if (Core::request('location') !== NULL) {
         if (is_numeric(Core::request('location'))) {
             $selected_location->where('id_location', '=', core::request('location'))->limit(1)->find();
         } else {
             $selected_location->where('seoname', '=', core::request('location'))->limit(1)->find();
         }
         if ($selected_location->loaded()) {
             $id_location = $selected_location->id_location;
         }
     }
     //render view publish new
     $this->template->content = View::factory('pages/ad/new', array('form_show' => $form_show, 'id_category' => $id_category, 'selected_category' => $selected_category, 'id_location' => $id_location, 'selected_location' => $selected_location, 'fields' => Model_Field::get_all()));
     if ($this->request->post()) {
         if (captcha::check('publish_new')) {
             $data = $this->request->post();
             $validation = Validation::factory($data);
             //validate location since its optional
             if (core::config('advertisement.location')) {
                 if ($locations->count_all() > 1) {
                     $validation = $validation->rule('location', 'not_empty')->rule('location', 'digit');
                 }
             }
             //user is not logged in validate input
             if (!Auth::instance()->logged_in()) {
                 $validation = $validation->rule('email', 'not_empty')->rule('email', 'email')->rule('email', 'email_domain')->rule('name', 'not_empty')->rule('name', 'min_length', array(':value', 2))->rule('name', 'max_length', array(':value', 145));
             }
             // Optional banned words validation
             if (core::config('advertisement.validate_banned_words')) {
                 $validation = $validation->rule('title', 'no_banned_words');
                 $validation = $validation->rule('description', 'no_banned_words');
             }
             if ($validation->check()) {
                 // User detection, if doesnt exists create
                 if (!Auth::instance()->logged_in()) {
                     $user = Model_User::create_email(core::post('email'), core::post('name'));
                 } else {
                     $user = Auth::instance()->get_user();
                 }
                 //to make it backward compatible with older themes: UGLY!!
                 if (isset($data['category']) and is_numeric($data['category'])) {
                     $data['id_category'] = $data['category'];
                     unset($data['category']);
                 }
                 if (isset($data['location']) and is_numeric($data['location'])) {
                     $data['id_location'] = $data['location'];
                     unset($data['location']);
                 }
                 //lets create!!
                 $return = Model_Ad::new_ad($data, $user);
                 //there was an error on the validation
                 if (isset($return['validation_errors']) and is_array($return['validation_errors'])) {
                     foreach ($return['validation_errors'] as $f => $err) {
                         Alert::set(Alert::ALERT, $err);
                     }
                 } elseif (isset($return['error'])) {
                     Alert::set($return['error_type'], $return['error']);
                 } elseif (isset($return['message']) and isset($return['ad'])) {
                     $new_ad = $return['ad'];
                     // IMAGE UPLOAD
                     $filename = NULL;
                     for ($i = 0; $i < core::config('advertisement.num_images'); $i++) {
                         if (Core::post('base64_image' . $i)) {
                             $filename = $new_ad->save_base64_image(Core::post('base64_image' . $i));
                         } elseif (isset($_FILES['image' . $i])) {
                             $filename = $new_ad->save_image($_FILES['image' . $i]);
                         }
                     }
                     Alert::set(Alert::SUCCESS, $return['message']);
                     //redirect user
                     if (isset($return['checkout_url']) and !empty($return['checkout_url'])) {
                         $this->redirect($return['checkout_url']);
                     } else {
                         $this->redirect(Route::url('default', array('action' => 'thanks', 'controller' => 'ad', 'id' => $new_ad->id_ad)));
                     }
                 }
             } else {
                 $errors = $validation->errors('ad');
                 foreach ($errors as $f => $err) {
                     Alert::set(Alert::ALERT, $err);
                 }
             }
         } else {
             Alert::set(Alert::ALERT, __('Captcha is not correct'));
         }
     }
 }
 public function action_template()
 {
     if ($_POST) {
         $cf_templates = '
                         {
                             "cars": [
                                 {
                                     "name": "for-sale-by",
                                     "type": "select",
                                     "label": "For sale by",
                                     "tooltip": "For sale by",
                                     "required": true,
                                     "searchable": true,
                                     "admin_privilege": false,
                                     "show_listing": true,
                                     "values": "Owner,Dealer"
                                 },
                                 {
                                     "name": "ad-type",
                                     "type": "select",
                                     "label": "Ad type",
                                     "tooltip": "Ad type",
                                     "required": true,
                                     "searchable": true,
                                     "admin_privilege": false,
                                     "show_listing": false,
                                     "values": "I’m selling my car,I’m looking for a car to buy"
                                 },
                                 {
                                     "name": "make",
                                     "type": "select",
                                     "label": "Make",
                                     "tooltip": "Make",
                                     "required": true,
                                     "searchable": true,
                                     "admin_privilege": false,
                                     "show_listing": true,
                                     "values": "Acura,Alfa Romeo,AM General,AMC,Aston Martin,Audi,Austin-Healey,Bently,BMW,Bricklin,Bugatti,Buick,Cadillac,Chevrolet,Chrysler,Daewoo,Datsun,Diahatsu,Dodge,Eagle,Ferrari,Fiat,Ford,Geo,GMC,Honda,HUMMER,Hyundai,Infiniti,International Harvester,Isuzu,Jaguar,Jeep,Kia,Lamborghini,Land Rover,Lexus,Lincoln,Lotus,Maserati,Maybach,Mazda,Mercedes-Benz,Mercury,MG,MINI,Mitsubishi,Nissan,Oldsmobile,Opel,Peugeot,Plymouth,Pontiac,Porsche,Ram,Renault,Rolls-Royce,Saab,Saturn,Scion,Shelby,Smart,Subaru,Suzuki,Toyota,Triumph,Volkswagen,Volvo,Other"
                                 },
                                 {
                                     "name": "other-make",
                                     "type": "string",
                                     "label": "Other make",
                                     "tooltip": "Other make",
                                     "required": false,
                                     "searchable": true,
                                     "admin_privilege": false,
                                     "show_listing": false,
                                     "values": ""
                                 },
                                 {
                                     "name": "model",
                                     "type": "string",
                                     "label": "Model",
                                     "tooltip": "Model",
                                     "required": true,
                                     "searchable": true,
                                     "admin_privilege": false,
                                     "show_listing": true,
                                     "values": ""
                                 },
                                 {
                                     "name": "year",
                                     "type": "integer",
                                     "label": "Year",
                                     "tooltip": "Year",
                                     "required": true,
                                     "searchable": true,
                                     "admin_privilege": false,
                                     "show_listing": true,
                                     "values": ""
                                 },
                                 {
                                     "name": "kilometers",
                                     "type": "integer",
                                     "label": "Kilometers",
                                     "tooltip": "Kilometers",
                                     "required": true,
                                     "searchable": true,
                                     "admin_privilege": false,
                                     "show_listing": true,
                                     "values": ""
                                 },
                                 {
                                     "name": "body-type",
                                     "type": "select",
                                     "label": "Body type",
                                     "tooltip": "Body type",
                                     "required": true,
                                     "searchable": true,
                                     "admin_privilege": false,
                                     "show_listing": true,
                                     "values": "Convertible,Coupe (2 door),Hatchback,Minivan or Van,Pickup Truck,Sedan,SUV. crossover,Wagon,Other"
                                 },
                                 {
                                     "name": "transmission",
                                     "type": "select",
                                     "label": "Transmission",
                                     "tooltip": "Transmission",
                                     "required": true,
                                     "searchable": true,
                                     "admin_privilege": false,
                                     "show_listing": true,
                                     "values": "Automatic,Manual,Other"
                                 },
                                 {
                                     "name": "drivetrain",
                                     "type": "select",
                                     "label": "Drivetrain",
                                     "tooltip": "Drivetrain",
                                     "required": true,
                                     "searchable": true,
                                     "admin_privilege": false,
                                     "show_listing": true,
                                     "values": "4 x 4,All-wheel drive (AWD),Front-wheel drive (FWD),Rear-wheel drive (RWD),Other"
                                 },
                                 {
                                     "name": "color",
                                     "type": "select",
                                     "label": "Color",
                                     "tooltip": "Color",
                                     "required": true,
                                     "searchable": true,
                                     "admin_privilege": false,
                                     "show_listing": true,
                                     "values": "Black,Blue,Brown,Burgundy,Gold,Green,Grey,Orange,Pink,Purple,Red,Silver,Tan,Teal,White,Yellow,Other"
                                 },
                                 {
                                     "name": "fuel-type",
                                     "type": "select",
                                     "label": "Fuel Type",
                                     "tooltip": "Fuel Type",
                                     "required": true,
                                     "searchable": true,
                                     "admin_privilege": false,
                                     "show_listing": true,
                                     "values": "Diesel,Gasoline,Hybrid-Electric,Other"
                                 },
                                 {
                                     "name": "type",
                                     "type": "select",
                                     "label": "Type",
                                     "tooltip": "Type",
                                     "required": true,
                                     "searchable": true,
                                     "admin_privilege": false,
                                     "show_listing": true,
                                     "values": "Damaged,Lease Takeover,New,Used"
                                 }
                             ],
                             "houses": [
                                 {
                                     "name": "furnished",
                                     "type": "select",
                                     "label": "Furnished",
                                     "tooltip": "Furnished",
                                     "required": true,
                                     "searchable": true,
                                     "admin_privilege": false,
                                     "show_listing": true,
                                     "values": "Yes,No"
                                 },
                                 {
                                     "name": "bedrooms",
                                     "type": "select",
                                     "label": "Bedrooms",
                                     "tooltip": "Bedrooms",
                                     "required": true,
                                     "searchable": true,
                                     "admin_privilege": false,
                                     "show_listing": true,
                                     "values": "Studio,1,2,3,4,5,6,7,8,9,10"
                                 },
                                 {
                                     "name": "bathrooms",
                                     "type": "select",
                                     "label": "Bathrooms",
                                     "tooltip": "Bathrooms",
                                     "required": true,
                                     "searchable": true,
                                     "admin_privilege": false,
                                     "show_listing": true,
                                     "values": "1,2,3,4,5,6,7,8,9,10"
                                 },
                                 {
                                     "name": "pets",
                                     "type": "select",
                                     "label": "Pets",
                                     "tooltip": "Pets",
                                     "required": false,
                                     "searchable": true,
                                     "admin_privilege": false,
                                     "show_listing": true,
                                     "values": "yes,No"
                                 },
                                 {
                                     "name": "agencybroker-fee",
                                     "type": "select",
                                     "label": "Agency\\/broker fee",
                                     "tooltip": "Agency\\/broker fee",
                                     "required": false,
                                     "searchable": true,
                                     "admin_privilege": false,
                                     "show_listing": true,
                                     "values": "Yes,No"
                                 },
                                 {
                                     "name": "square-meters",
                                     "type": "string",
                                     "label": "Square meters",
                                     "tooltip": "Square meters",
                                     "required": false,
                                     "searchable": true,
                                     "admin_privilege": false,
                                     "show_listing": true,
                                     "values": ""
                                 },
                                 {
                                     "name": "price-negotiable",
                                     "type": "checkbox",
                                     "label": "Price negotiable",
                                     "tooltip": "Price negotiable",
                                     "required": false,
                                     "searchable": true,
                                     "admin_privilege": false,
                                     "show_listing": true,
                                     "values": ""
                                 }
                             ],
                             "jobs": [
                                 {
                                     "name": "job-type",
                                     "type": "select",
                                     "label": "Job type",
                                     "tooltip": "Job type",
                                     "required": true,
                                     "searchable": true,
                                     "admin_privilege": false,
                                     "show_listing": true,
                                     "values": "Full-time,Part-time"
                                 },
                                 {
                                     "name": "experience-in-years",
                                     "type": "select",
                                     "label": "Experience in Years",
                                     "tooltip": "Experience in Years",
                                     "required": false,
                                     "searchable": true,
                                     "admin_privilege": false,
                                     "show_listing": true,
                                     "values": "Less than 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,More than 20"
                                 },
                                 {
                                     "name": "salary",
                                     "type": "integer",
                                     "label": "Salary",
                                     "tooltip": "Salary",
                                     "required": false,
                                     "searchable": true,
                                     "admin_privilege": false,
                                     "show_listing": false,
                                     "values": ""
                                 },
                                 {
                                     "name": "salary-type",
                                     "type": "select",
                                     "label": "Salary type",
                                     "tooltip": "Salary type",
                                     "required": false,
                                     "searchable": true,
                                     "admin_privilege": false,
                                     "show_listing": false,
                                     "values": "Hourly,Daily,Weekly,Monthly,Quarterly,Yearly"
                                 },
                                 {
                                     "name": "extra-information",
                                     "type": "textarea",
                                     "label": "Extra information",
                                     "tooltip": "Extra information",
                                     "required": false,
                                     "searchable": true,
                                     "admin_privilege": false,
                                     "show_listing": false,
                                     "values": ""
                                 }
                             ],
                             "dating": [
                                 {
                                     "name": "age",
                                     "type": "integer",
                                     "label": "Age",
                                     "tooltip": "Age",
                                     "required": false,
                                     "searchable": true,
                                     "admin_privilege": false,
                                     "show_listing": true,
                                     "values": ""
                                 },
                                 {
                                     "name": "body",
                                     "type": "select",
                                     "label": "Body",
                                     "tooltip": "Body",
                                     "required": false,
                                     "searchable": true,
                                     "admin_privilege": false,
                                     "show_listing": true,
                                     "values": "-,Athletic,Average,big,Curvy,Fit,Heavy,HWP,Skinny,Thin,"
                                 },
                                 {
                                     "name": "height",
                                     "type": "select",
                                     "label": "Height",
                                     "tooltip": "Height",
                                     "required": false,
                                     "searchable": true,
                                     "admin_privilege": false,
                                     "show_listing": true,
                                     "values": "Taller than 6.8 (203 cm),6.7 (200 cm),6.6 (198 cm),6.5 (195 cm),6.4 (194cm),6.3 (190 cm),6.2 (187 cm),6.1 (185 cm),6.0 (182 cm),5.11 (180 cm),5.10 (177 cm),5.9 (175 cm),5.8 (172 cm),5.7 (170 cm),5.6 (167 cm),5.5 (165 cm),5.4 (162 cm),5.3 (160 cm),5.2 (157 cm),5.1 (154 cm),5.0 (152 cm),4.11 (150 cm),4.10 (147 cm),4.9 (145 cm),4.8 (142 cm) or less"
                                 },
                                 {
                                     "name": "status",
                                     "type": "select",
                                     "label": "Status",
                                     "tooltip": "Status",
                                     "required": false,
                                     "searchable": true,
                                     "admin_privilege": false,
                                     "show_listing": false,
                                     "values": "Single,In a Relationship,Engaged,Married,Separated,Divorced,Widowed"
                                 },
                                 {
                                     "name": "occupation",
                                     "type": "string",
                                     "label": "Occupation",
                                     "tooltip": "Occupation",
                                     "required": false,
                                     "searchable": true,
                                     "admin_privilege": false,
                                     "show_listing": false,
                                     "values": ""
                                 },
                                 {
                                     "name": "hair",
                                     "type": "string",
                                     "label": "Hair",
                                     "tooltip": "Hair",
                                     "required": false,
                                     "searchable": true,
                                     "admin_privilege": false,
                                     "show_listing": false,
                                     "values": ""
                                 },
                                 {
                                     "name": "eye-color",
                                     "type": "string",
                                     "label": "Eye color",
                                     "tooltip": "Eye color",
                                     "required": false,
                                     "searchable": true,
                                     "admin_privilege": false,
                                     "show_listing": false,
                                     "values": ""
                                 }
                             ]
                         }
                         ';
         $cf_templates = json_decode($cf_templates, TRUE);
         $field = new Model_Field();
         foreach ($cf_templates[Core::post('type')] as $custom_field) {
             try {
                 $name = $custom_field['name'];
                 $options = array('label' => $custom_field['label'], 'tooltip' => $custom_field['tooltip'], 'required' => $custom_field['required'], 'searchable' => $custom_field['searchable'], 'admin_privilege' => $custom_field['admin_privilege'], 'show_listing' => $custom_field['show_listing']);
                 if ($field->create($name, $custom_field['type'], $custom_field['values'], Core::post('categories'), $options)) {
                     Core::delete_cache();
                     Alert::set(Alert::SUCCESS, sprintf(__('Field %s created'), $name));
                 } else {
                     Alert::set(Alert::ERROR, sprintf(__('Field %s already exists'), $name));
                 }
             } catch (Exception $e) {
                 throw HTTP_Exception::factory(500, $e->getMessage());
             }
         }
         HTTP::redirect(Route::url('oc-panel', array('controller' => 'fields', 'action' => 'index')));
     } else {
         HTTP::redirect(Route::url('oc-panel', array('controller' => 'fields', 'action' => 'index')));
     }
 }
Example #22
0
 /**
  * remove category from custom field
  * @return void 
  */
 public function action_remove_category()
 {
     if (Core::get('id_category')) {
         $name = $this->request->param('id');
         $field = new Model_Field();
         $field_data = $field->get($name);
         $category = new Model_Category(Core::get('id_category'));
         // category or custom field not found
         if (!$category->loaded() or !$field_data) {
             $this->redirect(Route::get('oc-panel')->uri(array('controller' => Request::current()->controller(), 'action' => 'index')));
         }
         // remove current category from custom field categories
         if (is_array($field_data['categories']) and ($key = array_search($category->id_category, $field_data['categories'])) !== FALSE) {
             unset($field_data['categories'][$key]);
         }
         try {
             // update custom field categories
             if ($field->update($name, $field_data['values'], $field_data['categories'], $field_data)) {
                 Core::delete_cache();
                 Alert::set(Alert::SUCCESS, sprintf(__('Field %s removed'), $name));
             } else {
                 Alert::set(Alert::ERROR, sprintf(__('Field %s cannot be removed'), $name));
             }
         } catch (Exception $e) {
             throw HTTP_Exception::factory(500, $e->getMessage());
         }
         $this->redirect(Route::get('oc-panel')->uri(array('controller' => 'category', 'action' => 'update', 'id' => $category->id_category)));
     }
     $this->redirect(Route::get('oc-panel')->uri(array('controller' => Request::current()->controller(), 'action' => 'index')));
 }
Example #23
0
 public function action_advanced_search()
 {
     if (Theme::get('infinite_scroll')) {
         $this->template->scripts['footer'][] = '//cdn.jsdelivr.net/jquery.infinitescroll/2.0b2/jquery.infinitescroll.js';
         $this->template->scripts['footer'][] = 'js/listing.js';
     }
     if (core::config('general.auto_locate') or core::config('advertisement.map')) {
         Theme::$scripts['async_defer'][] = '//maps.google.com/maps/api/js?libraries=geometry,places&v=3&key=' . core::config("advertisement.gm_api_key") . '&callback=initLocationsGMap';
     }
     $this->template->scripts['footer'][] = 'js/jquery.toolbar.js';
     $this->template->scripts['footer'][] = 'js/sort.js';
     //template header
     $this->template->title = __('Advanced Search');
     $this->template->meta_description = __('Search in') . ' ' . core::config('general.site_name');
     //breadcrumbs
     Breadcrumbs::add(Breadcrumb::factory()->set_title(__('Home'))->set_url(Route::url('default')));
     Breadcrumbs::add(Breadcrumb::factory()->set_title($this->template->title));
     $pagination = NULL;
     $ads = NULL;
     $res_count = NULL;
     $user = $this->user ? $this->user : NULL;
     if ($this->request->query()) {
         // variables
         $search_advert = core::get('title');
         $search_loc = core::get('location');
         // filter by each variable
         $ads = new Model_Ad();
         //if sort by distance
         if ((core::request('sort', core::config('advertisement.sort_by')) == 'distance' or core::request('userpos') == 1) 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`))))) * 111.321'), 'distance'))->where('latitude', 'IS NOT', NULL)->where('longitude', 'IS NOT', NULL);
         }
         // early filter
         $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 (core::request('userpos') == 1 and Model_User::get_userlatlng()) {
             if (is_numeric(Core::cookie('mydistance')) and Core::cookie('mydistance') <= 500) {
                 $location_distance = Core::config('general.measurement') == 'imperial' ? Num::round(Core::cookie('mydistance') * 1.60934) : Core::cookie('mydistance');
             } else {
                 $location_distance = Core::config('general.measurement') == 'imperial' ? Num::round(Core::config('advertisement.auto_locate_distance') * 1.60934) : Core::config('advertisement.auto_locate_distance');
             }
             $ads->where(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'), '<=', $location_distance);
         }
         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');
             }
             if (core::config('general.search_by_description') == TRUE) {
                 $ads->where_open()->where('title', 'like', '%' . $search_advert . '%')->or_where('description', 'like', '%' . $search_advert . '%')->where_close();
             } else {
                 $ads->where('title', 'like', '%' . $search_advert . '%');
             }
         }
         //cf filter arrays
         $cf_fields = array();
         $cf_user_fields = array();
         foreach ($this->request->query() as $name => $field) {
             if (isset($field) and $field != NULL) {
                 // get by prefix cf
                 if (strpos($name, 'cf_') !== FALSE and array_key_exists(str_replace('cf_', '', $name), Model_Field::get_all())) {
                     $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;
                     }
                 } elseif (strpos($name, 'cfuser_') !== FALSE and array_key_exists(str_replace('cfuser_', '', $name), Model_UserField::get_all())) {
                     $name = str_replace('cfuser_', 'cf_', $name);
                     $cf_user_fields[$name] = $field;
                     //checkbox when selected return string 'on' as a value
                     if ($field == 'on') {
                         $cf_user_fields[$name] = 1;
                     } elseif (empty($field)) {
                         $cf_user_fields[$name] = NULL;
                     }
                 }
             }
         }
         $category = NULL;
         $location = NULL;
         if (core::config('general.search_multi_catloc') and Theme::$is_mobile === FALSE) {
             //filter by category
             if (is_array(core::get('category'))) {
                 $cat_siblings_ids = array();
                 foreach (core::get('category') as $cat) {
                     if ($cat !== NULL) {
                         $category = new Model_Category();
                         $category->where('seoname', '=', $cat)->cached()->limit(1)->find();
                         if ($category->loaded()) {
                             $cat_siblings_ids = array_merge($cat_siblings_ids, $category->get_siblings_ids());
                         }
                     }
                 }
                 if (count($cat_siblings_ids) > 0) {
                     $ads->where('id_category', 'IN', $cat_siblings_ids);
                 }
             }
             //filter by location
             if (is_array(core::get('location'))) {
                 $loc_siblings_ids = array();
                 foreach (core::get('location') as $loc) {
                     if ($loc !== NULL) {
                         $location = new Model_location();
                         $location->where('seoname', '=', $loc)->cached()->limit(1)->find();
                         if ($location->loaded()) {
                             $loc_siblings_ids = array_merge($loc_siblings_ids, $location->get_siblings_ids());
                         }
                     }
                 }
                 if (count($loc_siblings_ids) > 0) {
                     $ads->where('id_location', 'IN', $loc_siblings_ids);
                 }
             }
         } else {
             if (core::get('category') !== NULL) {
                 $category = new Model_Category();
                 $category->where('seoname', is_array(core::get('category')) ? 'in' : '=', core::get('category'))->cached()->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', is_array(core::get('location')) ? 'in' : '=', core::get('location'))->cached()->limit(1)->find();
                 if ($location->loaded()) {
                     $ads->where('id_location', 'IN', $location->get_siblings_ids());
                 }
             }
         }
         //filter by price(s)
         if (is_numeric($price_min = str_replace(',', '.', core::get('price-min')))) {
             // handle comma (,) used in some countries for prices
             $price_min = (double) $price_min;
         }
         // round((float)$price_min,2)
         if (is_numeric($price_max = str_replace(',', '.', core::get('price-max')))) {
             // handle comma (,) used in some countries for prices
             $price_max = (double) $price_max;
         }
         // round((float)$price_max,2)
         if (is_numeric($price_min) and is_numeric($price_max)) {
             // swap 2 values
             if ($price_min > $price_max) {
                 $aux = $price_min;
                 $price_min = $price_max;
                 $price_max = $aux;
                 unset($aux);
             }
             $ads->where('price', 'BETWEEN', array($price_min, $price_max));
         } elseif (is_numeric($price_min)) {
             $ads->where('price', '>=', $price_min);
         } elseif (is_numeric($price_max)) {
             $ads->where('price', '<=', $price_max);
         }
         //filter by CF ads
         if (count($cf_fields) > 0) {
             foreach ($cf_fields as $key => $value) {
                 //filter by range
                 if (array_key_exists(str_replace('cf_', '', $key), Model_Field::get_all()) and Model_Field::get_all()[str_replace('cf_', '', $key)]['type'] == 'range') {
                     $cf_min = isset($value[0]) ? $value[0] : NULL;
                     $cf_max = isset($value[1]) ? $value[1] : NULL;
                     if (is_numeric($cf_min = str_replace(',', '.', $cf_min))) {
                         // handle comma (,) used in some countries
                         $cf_min = (double) $cf_min;
                     }
                     if (is_numeric($cf_max = str_replace(',', '.', $cf_max))) {
                         // handle comma (,) used in some countries
                         $cf_max = (double) $cf_max;
                     }
                     if (is_numeric($cf_min) and is_numeric($cf_max)) {
                         // swap 2 values
                         if ($cf_min > $cf_max) {
                             $aux = $cf_min;
                             $cf_min = $cf_max;
                             $cf_max = $aux;
                             unset($aux);
                         }
                         $ads->where($key, 'BETWEEN', array($cf_min, $cf_max));
                     } elseif (is_numeric($cf_min)) {
                         // only min cf has been provided
                         $ads->where($key, '>=', $cf_min);
                     } elseif (is_numeric($cf_max)) {
                         // only max cf has been provided
                         $ads->where($key, '<=', $cf_max);
                     }
                 } elseif (is_numeric($value)) {
                     $ads->where($key, '=', $value);
                 } elseif (is_string($value)) {
                     $ads->where($key, 'like', '%' . $value . '%');
                 } elseif (is_array($value)) {
                     if (!empty($value = array_filter($value))) {
                         $ads->where($key, 'IN', $value);
                     }
                 }
             }
         }
         //filter by user
         if (count($cf_user_fields) > 0) {
             $users = new Model_User();
             foreach ($cf_user_fields as $key => $value) {
                 if (is_numeric($value)) {
                     $users->where($key, '=', $value);
                 } elseif (is_string($value)) {
                     $users->where($key, 'like', '%' . $value . '%');
                 } elseif (is_array($value)) {
                     if (!empty($value = array_filter($value))) {
                         $ads->where($key, 'IN', $value);
                     }
                 }
             }
             $users = $users->find_all();
             if ($users->count() > 0) {
                 $ads->where('id_user', 'in', $users->as_array());
             } else {
                 $ads->where('id_user', '=', 0);
             }
         }
         // 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('advertisement.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));
             /**
              * 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;
                     //favorited
                 //favorited
                 case 'favorited':
                     $ads->order_by('favorited', 'desc')->order_by('published', 'desc');
                     break;
                     //distance
                 //distance
                 case 'distance':
                     if (Model_User::get_userlatlng() and core::config('general.auto_locate')) {
                         $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 {
             $ads = NULL;
         }
     }
     $this->template->bind('content', $content);
     $this->template->content = View::factory('pages/ad/advanced_search', array('ads' => $ads, 'categories' => Model_Category::get_as_array(), 'order_categories' => Model_Category::get_multidimensional(), 'locations' => Model_Location::get_as_array(), 'order_locations' => Model_Location::get_multidimensional(), 'pagination' => $pagination, 'user' => $user, 'fields' => Model_Field::get_all(), 'total_ads' => $res_count));
 }