/** * 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() { $loc = new Model_Location(); // loaded category if (Controller::$location !== NULL) { if (Controller::$location->loaded()) { $location = Controller::$location->id_location; // id_location //list of children of current location // if list_loc dosent have siblings take brothers // $list_loc = $loc->where('id_location_parent', '=', $location)->order_by('order', 'asc')->cached()->find_all(); if (count($list_loc) == 0) { $list_loc = $loc->where('id_location_parent', '=', Controller::$location->id_location_parent)->order_by('order', 'asc')->cached()->find_all(); } //parent of current location $loc_parent_deep = $loc->where('id_location', '=', Controller::$location->id_location_parent)->limit(1)->find(); // array with name and seoname of a location and his parent. Is to build breadcrumb in widget $current_and_parent = array('name' => Controller::$location->name, 'id' => Controller::$location->id_location, 'seoname' => Controller::$location->seoname, 'parent_name' => $loc_parent_deep->name, 'id_parent' => $loc_parent_deep->id_location_parent, 'parent_seoname' => $loc_parent_deep->seoname); } } else { $list_loc = $loc->where('id_location_parent', '=', 1)->order_by('order', 'asc')->cached()->find_all(); $current_and_parent = NULL; } $this->locations = $this->locations; $this->loc_items = $list_loc; $this->loc_breadcrumb = $current_and_parent; $this->cat_seoname = NULL; if (Controller::$category !== NULL) { if (Controller::$category->loaded()) { $this->cat_seoname = Controller::$category->seoname; } } }
/** * returns the current category * @return Model_Category */ public static function current() { //we don't have so let's retrieve if (self::$_current === NULL) { self::$_current = new self(); if (Request::current()->param('category') != URL::title(__('all'))) { self::$_current = self::$_current->where('seoname', '=', Request::current()->param('category'))->limit(1)->cached()->find(); } } return self::$_current; }
/** * returns the current location * @return Model_Location */ public static function current() { //we don't have so let's retrieve if (self::$_current === NULL) { self::$_current = new self(); if (Model_Ad::current() != NULL and Model_Ad::current()->loaded() and Model_Ad::current()->location->loaded()) { self::$_current = Model_Ad::current()->location; } elseif (Request::current()->param('location') != NULL || Request::current()->param('location') != URL::title(__('all'))) { self::$_current = self::$_current->where('seoname', '=', Request::current()->param('location'))->limit(1)->cached()->find(); } } return self::$_current; }
/** * Initialize properties before running the controller methods (actions), * so they are available to our action. */ public function before($template = NULL) { parent::before(); Theme::checker(); $this->maintenance(); /** * selected category */ if ($this->request->param('category', NULL) != 'all') { $slug_cat = new Model_Category(); $seo_cat = $slug_cat->where('seoname', '=', $this->request->param('category'))->limit(1)->cached()->find(); if ($seo_cat->loaded()) { self::$category = $seo_cat; } } /** * selected location */ if ($this->request->param('location', NULL) != NULL || $this->request->param('location') != 'all') { $slug_loc = new Model_Location(); $seo_loc = $slug_loc->where('seoname', '=', $this->request->param('location'))->limit(1)->cached()->find(); if ($seo_loc->loaded()) { self::$location = $seo_loc; } } if ($this->auto_render === TRUE) { // Load the template if ($template !== NULL) { $this->template = $template; } $this->template = View::factory($this->template); // Initialize template values $this->template->title = core::config('general.site_name'); $this->template->meta_keywords = ''; $this->template->meta_description = ''; $this->template->meta_copywrite = 'Open Classifieds ' . Core::version; $this->template->content = ''; $this->template->styles = array(); $this->template->scripts = array(); //we can not cache this view since theres dynamic parts //$this->template->header = View::factory('header'); //setting inner views try to get from fragment // if (Auth::instance()->logged_in()) // $this->template->header = View::fragment('header_front_login','header'); // else $this->template->header = View::factory('header'); // $this->template->header = View::fragment('header_front','header'); //no fragment since CSRF gets cached :( $this->template->footer = View::fragment('footer_front', 'footer'); } }
/** * This function will upgrade DB that didn't existed in versions prior to 2.3.0 */ public function action_230() { //Cron update try { DB::query(Database::UPDATE, "UPDATE `" . self::$db_prefix . "crontab` SET period='00 3 * * *' WHERE callback='Sitemap::generate' LIMIT 1")->execute(); DB::query(Database::UPDATE, "UPDATE `" . self::$db_prefix . "crontab` SET period='00 5 * * *' WHERE callback='Core::delete_cache' LIMIT 1")->execute(); DB::query(Database::UPDATE, "UPDATE `" . self::$db_prefix . "crontab` SET period='00 4 1 * *' WHERE callback='Core::optimize_db' LIMIT 1")->execute(); DB::query(Database::UPDATE, "UPDATE `" . self::$db_prefix . "crontab` SET period='00 7 * * *' WHERE callback='Cron_Ad::unpaid' LIMIT 1")->execute(); DB::query(Database::UPDATE, "UPDATE `" . self::$db_prefix . "crontab` SET period='00 8 * * *' WHERE callback='Cron_Ad::expired_featured' LIMIT 1")->execute(); DB::query(Database::UPDATE, "UPDATE `" . self::$db_prefix . "crontab` SET period='00 9 * * *' WHERE callback='Cron_Ad::expired' LIMIT 1")->execute(); } catch (exception $e) { } //control login attempts try { DB::query(Database::UPDATE, "ALTER TABLE `" . self::$db_prefix . "users` ADD `last_failed` DATETIME NULL DEFAULT NULL ;")->execute(); } catch (exception $e) { } try { DB::query(Database::UPDATE, "ALTER TABLE `" . self::$db_prefix . "users` ADD `failed_attempts` int(10) unsigned DEFAULT 0")->execute(); } catch (exception $e) { } //categories/locations/users/ads has_image/last_modified try { DB::query(Database::UPDATE, "ALTER TABLE `" . self::$db_prefix . "categories` ADD `last_modified` DATETIME NULL DEFAULT NULL ;")->execute(); } catch (exception $e) { } try { DB::query(Database::UPDATE, "ALTER TABLE `" . self::$db_prefix . "categories` ADD `has_image` TINYINT( 1 ) NOT NULL DEFAULT '0' ;")->execute(); } catch (exception $e) { } try { DB::query(Database::UPDATE, "ALTER TABLE `" . self::$db_prefix . "locations` ADD `last_modified` DATETIME NULL DEFAULT NULL ;")->execute(); } catch (exception $e) { } try { DB::query(Database::UPDATE, "ALTER TABLE `" . self::$db_prefix . "locations` ADD `has_image` TINYINT( 1 ) NOT NULL DEFAULT '0' ;")->execute(); } catch (exception $e) { } try { DB::query(Database::UPDATE, "ALTER TABLE `" . self::$db_prefix . "users` ADD `has_image` TINYINT( 1 ) NOT NULL DEFAULT '0' ;")->execute(); } catch (exception $e) { } try { DB::query(Database::UPDATE, "ALTER TABLE `" . self::$db_prefix . "ads` ADD `last_modified` DATETIME NULL DEFAULT NULL ;")->execute(); } catch (exception $e) { } //new configs $configs = array(array('config_key' => 'aws_s3_active', 'group_name' => 'image', 'config_value' => 0), array('config_key' => 'aws_access_key', 'group_name' => 'image', 'config_value' => ''), array('config_key' => 'aws_secret_key', 'group_name' => 'image', 'config_value' => ''), array('config_key' => 'aws_s3_bucket', 'group_name' => 'image', 'config_value' => ''), array('config_key' => 'aws_s3_domain', 'group_name' => 'image', 'config_value' => 0), array('config_key' => 'disallow_nudes', 'group_name' => 'image', 'config_value' => 0), array('config_key' => 'html_head', 'group_name' => 'general', 'config_value' => ''), array('config_key' => 'html_footer', 'group_name' => 'general', 'config_value' => ''), array('config_key' => 'login_to_contact', 'group_name' => 'advertisement', 'config_value' => 0), array('config_key' => 'custom_css', 'group_name' => 'appearance', 'config_value' => 0), array('config_key' => 'custom_css_version', 'group_name' => 'appearance', 'config_value' => 0), array('config_key' => 'only_admin_post', 'group_name' => 'advertisement', 'config_value' => 0), array('config_key' => 'map_active', 'group_name' => 'appearance', 'config_value' => 1), array('config_key' => 'map_jscode', 'group_name' => 'appearance', 'config_value' => ''), array('config_key' => 'map_settings', 'group_name' => 'appearance', 'config_value' => ''), array('config_key' => 'recaptcha_active', 'group_name' => 'general', 'config_value' => ''), array('config_key' => 'recaptcha_secretkey', 'group_name' => 'general', 'config_value' => ''), array('config_key' => 'recaptcha_sitekey', 'group_name' => 'general', 'config_value' => '')); Model_Config::config_array($configs); //upgrade has_image field to use it as images count $ads = new Model_Ad(); $ads = $ads->where('has_images', '>', 0)->find_all(); if (count($ads)) { foreach ($ads as $ad) { $ad->has_images = 0; //begin with 0 images $route = $ad->image_path(); $folder = DOCROOT . $route; $image_keys = array(); if (is_dir($folder)) { //retrive ad pictures foreach (new DirectoryIterator($folder) as $file) { if (!$file->isDot()) { $key = explode('_', $file->getFilename()); $key = end($key); $key = explode('.', $key); $key = isset($key[0]) ? $key[0] : NULL; if (is_numeric($key)) { if (strpos($file->getFilename(), 'thumb_') === 0) { $image_keys[] = $key; } } } } //count images and reordering file names if (count($image_keys)) { asort($image_keys); foreach ($image_keys as $image_key) { $ad->has_images++; @rename($folder . $ad->seotitle . '_' . $image_key . '.jpg', $folder . $ad->seotitle . '_' . $ad->has_images . '.jpg'); @rename($folder . 'thumb_' . $ad->seotitle . '_' . $image_key . '.jpg', $folder . 'thumb_' . $ad->seotitle . '_' . $ad->has_images . '.jpg'); } } } //update has_images count try { $ad->save(); } catch (Exception $e) { throw HTTP_Exception::factory(500, $e->getMessage()); } } } //upgrade categories has_image $images_path = DOCROOT . 'images/categories'; if (is_dir($images_path)) { //retrive cat pictures foreach (new DirectoryIterator($images_path) as $file) { if ($file->isFile()) { $cat_name = str_replace('.png', '', $file->getFilename()); $cat = new Model_Category(); $cat->where('seoname', '=', $cat_name)->find(); if ($cat->loaded()) { $cat->has_image = 1; $cat->save(); } } } } //upgrade locations has_image $images_path = DOCROOT . 'images/locations'; if (is_dir($images_path)) { //retrive loc pictures foreach (new DirectoryIterator($images_path) as $file) { if ($file->isFile()) { $loc_name = str_replace('.png', '', $file->getFilename()); $loc = new Model_Location(); $loc->where('seoname', '=', $loc_name)->find(); if ($loc->loaded()) { $loc->has_image = 1; $loc->save(); } } } } //upgrade users has_image $images_path = DOCROOT . 'images/users'; if (is_dir($images_path)) { //retrive user pictures foreach (new DirectoryIterator($images_path) as $file) { if ($file->isFile() and is_numeric($id_user = str_replace('.png', '', $file->getFilename()))) { $user = new Model_User($id_user); if ($user->loaded()) { $user->has_image = 1; $user->save(); } } } } }
/** * deletes all the locations * @return void */ public function action_delete_all() { if (core::post('confirmation')) { //delete location icons $locations = new Model_Location(); if ($id_location = intval(Core::post('id_location')) and $id_location > 0) { $selected_location = new Model_Location($id_location); $locations->where('id_location', 'in', $selected_location->get_siblings_ids())->where('id_location', '!=', $selected_location->id_location); } else { $locations->where('id_location', '!=', '1')->find_all(); } $locations = $locations->find_all(); foreach ($locations as $location) { $root = DOCROOT . 'images/locations/'; //root folder if (is_dir($root)) { @unlink($root . $location->seoname . '.png'); // delete icon from Amazon S3 if (core::config('image.aws_s3_active')) { $s3->deleteObject(core::config('image.aws_s3_bucket'), 'images/locations/' . $location->seoname . '.png'); } } } $query_update = DB::update('ads'); $query_delete = DB::delete('locations'); if ($id_location = intval(Core::post('id_location')) and $id_location > 0) { $query_update->set(array('id_location' => $selected_location->id_location)); $query_delete->where('id_location', 'in', $selected_location->get_siblings_ids())->where('id_location', '!=', $selected_location->id_location); } else { $query_update->set(array('id_location' => '1')); $query_delete->where('id_location', '!=', '1'); } $query_update->execute(); $query_delete->execute(); Core::delete_cache(); Alert::set(Alert::SUCCESS, __('All locations were deleted.')); } else { Alert::set(Alert::ERROR, __('You did not confirmed your delete action.')); } HTTP::redirect(Route::url('oc-panel', array('controller' => 'location', 'action' => 'index'))); }
/** * * 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')); } } }
public function action_import_tool() { $this->template->title = __('Import tool for locations and categories'); Breadcrumbs::add(Breadcrumb::factory()->set_title($this->template->title)); //sending a CSV if ($_POST) { foreach ($_FILES as $file => $path) { $csv = $path["tmp_name"]; $csv_2[] = $file; if ($path['size'] > 1048576) { Alert::set(Alert::ERROR, __('1 MB file')); $this->redirect(Route::url('oc-panel', array('controller' => 'tools', 'action' => 'import_tool'))); } if ($file == 'csv_file_categories' and $csv != FALSE) { $expected_header = array('name', 'category_parent', 'price'); $cat_array = Core::csv_to_array($csv, $expected_header); if (count($cat_array) > 10000) { Alert::set(Alert::ERROR, __('limited to 10.000 at a time')); $this->redirect(Route::url('oc-panel', array('controller' => 'tools', 'action' => 'import_tool'))); } if ($cat_array === FALSE) { Alert::set(Alert::ERROR, __('Something went wrong, please check format of the file! Remove single quotes or strange characters, in case you have any.')); } else { foreach ($cat_array as $cat) { //category parent was sent? if ($cat[1]) { $category_parent = new Model_Category(); $category_parent->where('name', '=', $cat[1])->limit(1)->find(); if ($category_parent->loaded()) { $cat[1] = $category_parent->id_category; } else { $cat[1] = 1; } } else { $cat[1] = 1; } Model_Category::create_name($cat[0], 0, $cat[1], 0, $cat[2]); } Core::delete_cache(); Alert::set(Alert::SUCCESS, __('Categories successfully imported.')); } } elseif ($file == 'csv_file_locations' and $csv != FALSE) { $expected_header = array('name', 'location_parent', 'latitude', 'longitude'); $loc_array = Core::csv_to_array($csv, $expected_header); if (count($loc_array) > 10000) { Alert::set(Alert::ERROR, __('limited to 10.000 at a time')); $this->redirect(Route::url('oc-panel', array('controller' => 'tools', 'action' => 'import_tool'))); } if ($loc_array === FALSE) { Alert::set(Alert::ERROR, __('Something went wrong, please check format of the file! Remove single quotes or strange characters, in case you have any.')); } else { foreach ($loc_array as $loc) { //location parent was sent? if ($loc[1]) { $location_parent = new Model_Location(); $location_parent->where('name', '=', $loc[1])->limit(1)->find(); if ($location_parent->loaded()) { $loc[1] = $location_parent->id_location; } else { $loc[1] = 1; } } else { $loc[1] = 1; } Model_Location::create_name($loc[0], 0, $loc[1], 0, $loc[2], $loc[3]); } Core::delete_cache(); Alert::set(Alert::SUCCESS, __('Locations successfully imported.')); } } } } $this->template->content = View::factory('oc-panel/pages/tools/import_tool'); }
/** * * 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')); } } }