/** * creates an ad from a row of import * @param class adsimport $adi * @return boolean */ private function create_ad($adi) { //new advertisement $ad = new Model_Ad(); //create user? if ($adi->id_user == NULL or !is_numeric($adi->id_user)) { //create the user $user = Model_User::create_user($adi->user_email, $adi->user_name); //check if in the table other users with same email set the id_user, then gets faster ;) try { DB::update('adsimport')->set(array('id_user' => $user->id_user))->where('user_email', '=', $adi->user_email)->execute(); } catch (Exception $e) { } //set id user to the new ad $ad->id_user = $user->id_user; } else { $ad->id_user = $adi->id_user; } //create category? if ($adi->id_category == NULL or !is_numeric($adi->id_category)) { //create the category $cat = Model_Category::create_name($adi->category); //check if in the table other cats with same name set the id_category, then gets faster ;) try { DB::update('adsimport')->set(array('id_category' => $cat->id_category))->where('category', '=', $adi->category)->execute(); } catch (Exception $e) { } //set id user to the new ad $ad->id_category = $cat->id_category; } else { $ad->id_category = $adi->id_category; } //create location? if (isset($adi->location) and !empty($adi->location) and ($adi->id_location == NULL or !is_numeric($adi->id_location))) { //create the location $loc = Model_Location::create_name($adi->location); //check if in the table other cats with same name set the id_location, then gets faster ;) try { DB::update('adsimport')->set(array('id_location' => $loc->id_location))->where('location', '=', $adi->location)->execute(); } catch (Exception $e) { } //set id user to the new ad $ad->id_location = $loc->id_location; } elseif (is_numeric($adi->id_location)) { $ad->id_location = $adi->id_location; } $ad->title = $adi->title; $ad->seotitle = $ad->gen_seo_title($adi->title); $ad->description = Text::html2bb($adi->description); $ad->published = $adi->date; $ad->created = $adi->date; $ad->price = $adi->price; $ad->address = $adi->address; $ad->phone = $adi->phone; $ad->website = $adi->website; $ad->status = Model_Ad::STATUS_PUBLISHED; try { $ad->save(); } catch (Exception $e) { return FALSE; } //save images if (($has_images = $this->process_images($ad, $adi)) > 0) { $ad->has_images = $has_images; try { $ad->save(); } catch (Exception $e) { return FALSE; } } //mark it as done try { DB::update('adsimport')->set(array('processed' => 1))->where('id_import', '=', $adi->id_import)->execute(); return TRUE; } catch (Exception $e) { return FALSE; } }
/** * confirms the post of and advertisement * @return void */ public function action_confirm() { $advert = new Model_Ad($this->request->param('id')); if ($advert->loaded()) { if (Auth::instance()->get_user()->id_user !== $advert->id_user) { Alert::set(Alert::ALERT, __("This is not your advertisement.")); HTTP::redirect(Route::url('oc-panel', array('controller' => 'myads', 'action' => 'index'))); } if (core::config('general.moderation') == Model_Ad::EMAIL_CONFIRMATION) { $advert->status = Model_Ad::STATUS_PUBLISHED; // status active $advert->published = Date::unix2mysql(); try { $advert->save(); Model_Subscribe::notify($advert); Alert::set(Alert::INFO, __('Your advertisement is successfully activated! Thank you!')); } catch (Exception $e) { throw HTTP_Exception::factory(500, $e->getMessage()); } } elseif (core::config('general.moderation') == Model_Ad::EMAIL_MODERATION) { $advert->status = Model_Ad::STATUS_NOPUBLISHED; try { $advert->save(); Alert::set(Alert::INFO, __('Advertisement is received, but first administrator needs to validate. Thank you for being patient!')); } catch (Exception $e) { throw HTTP_Exception::factory(500, $e->getMessage()); } } $this->redirect(Route::url('ad', array('category' => $advert->category->seoname, 'seotitle' => $advert->seotitle))); } }
/** * creates a new ad * @param array $data * @param model_user $user * @return array */ public static function new_ad($data, $user) { $return_message = ''; $checkout_url = ''; //akismet spam filter if (isset($data['title']) and isset($data['description']) and core::akismet($data['title'], $user->email, $data['description']) == TRUE) { // is user marked as spammer? Make him one :) if (core::config('general.black_list')) { $user->user_spam(); } return array('error' => __('This post has been considered as spam! We are sorry but we can not publish this advertisement.'), 'error_type' => Alert::ALERT); } //akismet $ad = new Model_Ad(); $ad->id_user = $user->id_user; $ad->values($data); $ad->seotitle = $ad->gen_seo_title($ad->title); $ad->created = Date::unix2mysql(); try { $ad->save(); } catch (ORM_Validation_Exception $e) { return array('validation_errors' => $e->errors('ad')); } catch (Exception $e) { return array('error' => $e->getMessage(), 'error_type' => Alert::ALERT); } /////////// NOTIFICATION Emails,messages to user and Status of the ad // depending on user flow (moderation mode), change usecase $moderation = core::config('general.moderation'); //calculate how much he needs to pay in case we have payment on if ($moderation == Model_Ad::PAYMENT_ON or $moderation == Model_Ad::PAYMENT_MODERATION) { // check category price, if 0 check parent if ($ad->category->price == 0) { $cat_parent = new Model_Category($ad->category->id_category_parent); //category without price if ($cat_parent->price == 0) { //swapping moderation since theres no price :( if ($moderation == Model_Ad::PAYMENT_ON) { $moderation = Model_Ad::POST_DIRECTLY; } elseif ($moderation == Model_Ad::PAYMENT_MODERATION) { $moderation = Model_Ad::MODERATION_ON; } } else { $amount = $cat_parent->price; } } else { $amount = $ad->category->price; } } //where and what we say to the user depending ont he moderation switch ($moderation) { case Model_Ad::PAYMENT_ON: case Model_Ad::PAYMENT_MODERATION: $ad->status = Model_Ad::STATUS_NOPUBLISHED; $order = Model_Order::new_order($ad, $user, Model_Order::PRODUCT_CATEGORY, $amount, NULL, Model_Order::product_desc(Model_Order::PRODUCT_CATEGORY) . ' ' . $ad->category->name); // redirect to invoice $return_message = __('Please pay before we publish your advertisement.'); $checkout_url = Route::url('default', array('controller' => 'ad', 'action' => 'checkout', 'id' => $order->id_order)); break; case Model_Ad::EMAIL_MODERATION: case Model_Ad::EMAIL_CONFIRMATION: $ad->status = Model_Ad::STATUS_UNCONFIRMED; $url_ql = $user->ql('oc-panel', array('controller' => 'myads', 'action' => 'confirm', 'id' => $ad->id_ad)); $user->email('ads-confirm', array('[URL.QL]' => $url_ql, '[AD.NAME]' => $ad->title)); $return_message = __('Advertisement is posted but first you need to activate. Please check your email!'); break; case Model_Ad::MODERATION_ON: $ad->status = Model_Ad::STATUS_NOPUBLISHED; $url_ql = $user->ql('oc-panel', array('controller' => 'myads', 'action' => 'update', 'id' => $ad->id_ad)); $user->email('ads-notify', array('[URL.QL]' => $url_ql, '[AD.NAME]' => $ad->title)); // email to notify user of creating, but it is in moderation currently $return_message = __('Advertisement is received, but first administrator needs to validate. Thank you for being patient!'); break; case Model_Ad::POST_DIRECTLY: default: $ad->status = Model_Ad::STATUS_PUBLISHED; $ad->published = $ad->created; $url_cont = $user->ql('contact'); $url_ad = $user->ql('ad', array('category' => $ad->category->seoname, 'seotitle' => $ad->seotitle)); $user->email('ads-user-check', array('[URL.CONTACT]' => $url_cont, '[URL.AD]' => $url_ad, '[AD.NAME]' => $ad->title)); Model_Subscribe::notify($ad); $return_message = __('Advertisement is posted. Congratulations!'); break; } //save the last changes on status $ad->save(); //notify admins new ad $ad->notify_admins(); return array('message' => $return_message, 'checkout_url' => $checkout_url, 'ad' => $ad); }
/** * unfavorite an ad * @param integer $id_user user * @param integer $id_ad ad * @return boolean */ public static function unfavorite($id_user, $id_ad) { //try to find the fav $fav = new Model_Favorite(); $fav->where('id_user', '=', $id_user)->where('id_ad', '=', $id_ad)->find(); if ($fav->loaded()) { $fav->delete(); // update ad favorite counter $ad = new Model_Ad($id_ad); if ($ad->loaded()) { $ad->favorited--; try { $ad->save(); } catch (Exception $e) { return FALSE; } } return TRUE; } else { return FALSE; } }
/** * [action_to_featured] [pay to go in featured] * */ public function action_to_featured() { //check pay to featured top is enabled if (core::config('payment.to_featured') == FALSE) { throw HTTP_Exception::factory(404, __('Page not found')); } $id_product = Model_Order::PRODUCT_TO_FEATURED; //check ad exists $id_ad = $this->request->param('id'); //how many days if (!is_numeric($days = Core::request('featured_days'))) { $plans = Model_Order::get_featured_plans(); $days = array_keys($plans); $days = reset($days); } //get price for the days $amount = Model_Order::get_featured_price($days); $ad = new Model_Ad($id_ad); if ($ad->loaded()) { //case when payment is set to 0,gets featured for free... if ($amount <= 0) { $ad->featured = Date::unix2mysql(time() + $days * 24 * 60 * 60); try { $ad->save(); } catch (Exception $e) { throw HTTP_Exception::factory(500, $e->getMessage()); } $this->redirect(Route::url('list')); } $currency = core::config('payment.paypal_currency'); $order = Model_Order::new_order($ad, $ad->user, $id_product, $amount, $currency, NULL, $days); // redirect to payment $this->redirect(Route::url('default', array('controller' => 'ad', 'action' => 'checkout', 'id' => $order->id_order))); } else { throw HTTP_Exception::factory(404, __('Page not found')); } }
/** * Mark advertisement as active : STATUS = 1 */ public function action_activate() { $id = $this->request->param('id'); $param_current_url = Core::get('current_url'); $format_id = explode('_', $id); foreach ($format_id as $id) { if (isset($id) and $id !== '') { $active_ad = new Model_Ad($id); if ($active_ad->loaded()) { if ($active_ad->status != Model_Ad::STATUS_PUBLISHED) { $active_ad->published = Date::unix2mysql(); $active_ad->status = Model_Ad::STATUS_PUBLISHED; try { $active_ad->save(); Model_Subscribe::notify($active_ad); } catch (Exception $e) { throw HTTP_Exception::factory(500, $e->getMessage()); } } } } } $this->multiple_mails($format_id); // sending many mails at the same time @TODO EMAIl Alert::set(Alert::SUCCESS, __('Advertisement is active and published')); if ($param_current_url == Model_Ad::STATUS_NOPUBLISHED and in_array(core::config('general.moderation'), Model_Ad::$moderation_status)) { HTTP::redirect(Route::url('oc-panel', array('controller' => 'ad', 'action' => 'moderate'))); } elseif ($param_current_url == Model_Ad::STATUS_PUBLISHED) { HTTP::redirect(Route::url('oc-panel', array('controller' => 'ad', 'action' => 'index'))); } else { HTTP::redirect(Route::url('oc-panel', array('controller' => 'ad', 'action' => 'index')) . '?status=' . $param_current_url); } }
/** * cleans old pictures * @return [type] [description] */ public function action_cleanimages() { $count_deleted = 0; //loop for directory image $folder = DOCROOT . 'images'; //year foreach (new DirectoryIterator($folder) as $year) { if ($year->isDir() and !$year->isDot() and is_numeric($year->getFilename())) { //month foreach (new DirectoryIterator($year->getPathname()) as $month) { if ($month->isDir() and !$month->isDot() and is_numeric($month->getFilename())) { //day foreach (new DirectoryIterator($month->getPathname()) as $day) { if ($day->isDir() and !$day->isDot() and is_numeric($day->getFilename())) { //id_ad foreach (new DirectoryIterator($day->getPathname()) as $id_ad) { if ($id_ad->isDir() and !$id_ad->isDot() and is_numeric($id_ad->getFilename())) { $delete = TRUE; //if ad is available leave it, if not delete folder ID $ad = new Model_Ad($id_ad->getFilename()); if ($ad->loaded() and $ad->status == Model_Ad::STATUS_PUBLISHED) { $delete = FALSE; } //ok lets get rid of it! if ($delete === TRUE) { echo '<br>Deleting: ' . $id_ad->getFilename() . '---' . $id_ad->getPathname(); File::delete($id_ad->getPathname()); //if the ad was loaded means had a different status, put it like he doesnt have images. if ($ad->loaded()) { $ad->has_images = 0; $ad->save(); //$ad->delete();//optional } $count_deleted++; } } } } } } } } } echo '<br>deleted ' . $count_deleted; }
/** * Mark advertisement as active : STATUS = 1 */ public function action_activate() { // First generate QR! $id = $this->request->param('id'); $param_current_url = $this->request->param('current_url'); $format_id = explode('_', $id); foreach ($format_id as $id) { if (isset($id) and $id !== '') { $active_ad = new Model_Ad($id); if ($active_ad->loaded()) { if ($active_ad->status != 1) { $active_ad->published = Date::unix2mysql(time()); $active_ad->status = Model_Ad::STATUS_PUBLISHED; try { $active_ad->save(); //subscription is on $data = array('title' => $title = $active_ad->title, 'cat' => $cat = $active_ad->category, 'loc' => $loc = $active_ad->location); Model_Subscribe::find_subscribers($data, floatval(str_replace(',', '.', $active_ad->price)), $active_ad->seotitle, Auth::instance()->get_user()->email); // if subscription is on } catch (Exception $e) { throw new HTTP_Exception_500($e->getMessage()); } } else { Alert::set(Alert::ALERT, __("Warning, Advertisement is already marked as 'active'")); if ($param_current_url == Model_Ad::STATUS_NOPUBLISHED) { Request::current()->redirect(Route::url('oc-panel', array('controller' => 'ad', 'action' => 'moderate'))); } elseif ($param_current_url == Model_Ad::STATUS_PUBLISHED) { Request::current()->redirect(Route::url('oc-panel', array('controller' => 'ad', 'action' => 'index'))); } else { Request::current()->redirect(Route::url('oc-panel', array('controller' => 'ad', 'action' => 'index')) . '?define=' . $param_current_url); } } } else { //throw 404 throw new HTTP_Exception_404(); } } } $this->multiple_mails($format_id); // sending many mails at the same time @TODO EMAIl if (Core::config('sitemap.on_post') == TRUE) { Sitemap::generate(); } Alert::set(Alert::SUCCESS, __('Advertisement is active and published')); if ($param_current_url == Model_Ad::STATUS_NOPUBLISHED) { Request::current()->redirect(Route::url('oc-panel', array('controller' => 'ad', 'action' => 'moderate'))); } elseif ($param_current_url == Model_Ad::STATUS_PUBLISHED) { Request::current()->redirect(Route::url('oc-panel', array('controller' => 'ad', 'action' => 'index'))); } else { Request::current()->redirect(Route::url('oc-panel', array('controller' => 'ad', 'action' => 'index')) . '?define=' . $param_current_url); } }
/** * does the DB migration * @param pointer $db * @param string $pf db_prefix */ private function migrate($db, $pf) { set_time_limit(0); $db_config = core::config('database.default'); $prefix = $db_config['table_prefix']; //connect DB original/to where we migrate $dbo = Database::instance('default'); //oc_accounts --> oc_users $users_map = array(); $accounts = $db->query(Database::SELECT, 'SELECT * FROM `' . $pf . 'accounts`'); foreach ($accounts as $account) { $user = new Model_User(); $user->where('email', '=', $account['email'])->limit(1)->find(); if (!$user->loaded()) { $user->name = $account['name']; $user->email = $account['email']; $user->password = $account['password']; $user->created = $account['createdDate']; $user->last_modified = $account['lastModifiedDate']; $user->last_login = $account['lastSigninDate']; $user->status = $account['active']; $user->id_role = 1; $user->seoname = $user->gen_seo_title($user->name); $user->save(); } $users_map[$account['email']] = $user->id_user; } //categories --> categories $categories_map = array(0 => 1); $categories = $db->query(Database::SELECT, 'SELECT * FROM `' . $pf . 'categories` ORDER BY `idCategoryParent` ASC'); foreach ($categories as $category) { $cat = new Model_Category(); $cat->name = $category['name']; $cat->order = $category['order']; $cat->created = $category['created']; $cat->seoname = $category['friendlyName']; $cat->price = $category['price']; $cat->description = substr($category['description'], 0, 250); $cat->parent_deep = $category['idCategoryParent'] > 0 ? 1 : 0; //there's only 1 deep $cat->id_category_parent = isset($categories_map[$category['idCategoryParent']]) ? $categories_map[$category['idCategoryParent']] : 1; $cat->save(); //we save old_id stores the new ID, so later we know the category parent, and to changes the ADS category id $categories_map[$category['idCategory']] = $cat->id_category; } //locations --> locations $locations_map = array(0 => 1); $locations = $db->query(Database::SELECT, 'SELECT * FROM `' . $pf . 'locations` ORDER BY `idLocationParent` ASC'); foreach ($locations as $location) { $loc = new Model_Location(); $loc->name = $location['name']; $loc->seoname = $location['friendlyName']; $loc->parent_deep = $location['idLocationParent'] > 0 ? 1 : 0; //there's only 1 deep $loc->id_location_parent = isset($locations_map[$location['idLocationParent']]) ? $locations_map[$location['idLocationParent']] : 1; $loc->save(); //we save old_id stores the new ID, so later we know the location parent, and to changes the ADS location id $locations_map[$location['idLocation']] = $loc->id_location; } //posts --> ads $ads_map = array(); $ads = $db->query(Database::SELECT, 'SELECT * FROM `' . $pf . 'posts`'); foreach ($ads as $a) { if (Valid::email($a['email'])) { $ad = new Model_Ad(); $ad->id_ad = $a['idPost']; //so images still work $ad->id_user = isset($users_map[$a['email']]) ? $users_map[$a['email']] : Model_User::create_email($a['email'], $a['name']); $ad->id_category = isset($categories_map[$a['idCategory']]) ? $categories_map[$a['idCategory']] : 1; $ad->id_location = isset($locations_map[$a['idLocation']]) ? $locations_map[$a['idLocation']] : 1; $ad->title = $a['title']; $ad->seotitle = $ad->gen_seo_title($a['title']); $ad->description = !empty($a['description']) ? Text::html2bb($a['description']) : $a['title']; $ad->address = $a['place']; $ad->price = $a['price']; $ad->phone = $a['phone']; $ad->has_images = $a['hasImages']; $ad->ip_address = ip2long($a['ip']); $ad->created = $a['insertDate']; $ad->published = $ad->created; //Status migration...big mess! if ($a['isAvailable'] == 0 and $a['isConfirmed'] == 0) { $ad->status = Model_Ad::STATUS_NOPUBLISHED; } elseif ($a['isAvailable'] == 1 and $a['isConfirmed'] == 0) { $ad->status = Model_Ad::STATUS_NOPUBLISHED; } elseif ($a['isAvailable'] == 1 and $a['isConfirmed'] == 1) { $ad->status = Model_Ad::STATUS_PUBLISHED; } elseif ($a['isAvailable'] == 0 and $a['isConfirmed'] == 1) { $ad->status = Model_Ad::STATUS_UNAVAILABLE; } elseif ($a['isAvailable'] == 2) { $ad->status = Model_Ad::STATUS_SPAM; } else { $ad->status = Model_Ad::STATUS_UNAVAILABLE; } try { $ad->save(); } catch (ORM_Validation_Exception $e) { // d($e->errors('')); } $ads_map[$a['idPost']] = $ad->id_ad; } } //posthits --> visits, mass migration $insert = 'INSERT INTO `' . $prefix . 'visits` ( `id_ad`, `created`, `ip_address`) VALUES'; $step = 5000; $total = $db->query(Database::SELECT, 'SELECT count(*) cont FROM `' . $pf . 'postshits`')->as_array(); $total = $total[0]['cont']; for ($i = 0; $i < $total; $i += $step) { $hits = $db->query(Database::SELECT, 'SELECT * FROM `' . $pf . 'postshits` LIMIT ' . $i . ', ' . $step); $values = ''; foreach ($hits as $hit) { //build insert query $values .= '(' . $hit['idPost'] . ', \'' . $hit['hitTime'] . '\', \'' . ip2long($hit['ip']) . '\'),'; } $dbo->query(Database::INSERT, $insert . substr($values, 0, -1)); } //old way of migrating // $hits = $db->query(Database::SELECT, 'SELECT * FROM `'.$pf.'postshits` '); // foreach ($hits as $hit) // { // //build insert query // $visit = new Model_Visit(); // $visit->id_ad = (isset($ads_map[$hit['idPost']]))?$ads_map[$hit['idPost']]:NULL; // $visit->created = $hit['hitTime']; // $visit->ip_address = ip2long($hit['ip']); // $visit->save(); // } }
public function action_confirm_post() { $advert_id = $this->request->param('id'); $advert = new Model_Ad($advert_id); if ($advert->loaded()) { if (core::config('general.moderation') == Model_Ad::EMAIL_CONFIRMATION) { $advert->status = 1; // status active $advert->published = Date::unix2mysql(time()); try { $advert->save(); //subscription is on $data = array('title' => $title = $advert->title, 'cat' => $cat = $advert->category, 'loc' => $loc = $advert->location); Model_Subscribe::find_subscribers($data, floatval(str_replace(',', '.', $advert->price)), $advert->seotitle, Auth::instance()->get_user()->email); // if subscription is on Alert::set(Alert::INFO, __('Your advertisement is successfully activated! Thank you!')); $this->request->redirect(Route::url('ad', array('category' => $advert->id_category, 'seotitle' => $advert->seotitle))); } catch (Exception $e) { throw new HTTP_Exception_500($e->getMessage()); } } if (core::config('general.moderation') == Model_Ad::EMAIL_MODERATION) { $advert->status = 0; // status active try { $advert->save(); Alert::set(Alert::INFO, __('Advertisement is received, but first administrator needs to validate. Thank you for being patient!')); $this->request->redirect(Route::url('ad', array('category' => $advert->id_category, 'seotitle' => $advert->seotitle))); } catch (Exception $e) { throw new HTTP_Exception_500($e->getMessage()); } } } }
/** * 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)); }
/** * [save_new_ad Save new advertisement if validated, with a given parameters * * @param [array] $data [post values] * @param [int] $status [status of advert.] * @param [bool] $published [Confirms if advert is published. ref to model_ad] * @param [int] $moderation [moderation status/mode] * * @return [view] View dependant on usecase */ public function save_new_ad($data, $status, $published, $moderation) { $user = new Model_User(); $new_ad = new Model_Ad(); //$_POST is submitted for a new ad if ($this->request->post()) { if (captcha::check('publish_new')) { //FORM DATA $seotitle = $new_ad->gen_seo_title($data['title']); $new_ad->title = Model_Ad::banned_words($data['title']); $new_ad->id_location = $data['loc']; $new_ad->id_category = $data['cat']; $new_ad->description = Model_Ad::banned_words($data['description']); $new_ad->seotitle = $seotitle; $new_ad->status = $status; $new_ad->price = floatval(str_replace(',', '.', $data['price'])); $new_ad->address = $data['address']; $new_ad->phone = $data['phone']; $new_ad->website = $data['website']; // set custom values foreach ($data as $name => $field) { // get only custom values with prefix if (strpos($name, 'cf_') !== false) { $new_ad->{$name} = $field; } } // d($data); // User detection, if doesnt exists create $auth_user = Auth::instance(); if (!$auth_user->logged_in()) { $name = core::post('name'); $email = core::post('email'); $user_id = $user->create_new_user($name, $email); } else { $user_id = $auth_user->get_user()->id_user; $name = $auth_user->get_user()->name; $email = $auth_user->get_user()->email; } // SAVE AD $new_ad->id_user = $user_id; // after handling user try { //akismet spam filter if (!core::akismet(Model_Ad::banned_words($data['title']), $email, Model_Ad::banned_words($data['description']))) { if ($moderation == Model_Ad::EMAIL_MODERATION or $moderation == Model_Ad::EMAIL_CONFIRMATION) { $new_ad->status = Model_Ad::STATUS_UNCONFIRMED; } $new_ad->save(); } else { Alert::set(Alert::SUCCESS, __('This post has been considered as spam! We are sorry but we cant publish this advertisement.')); $this->request->redirect('default'); } //akismet // if moderation is off update db field with time of creation if ($published) { $_ad_published = new Model_Ad(); $_ad_published->where('seotitle', '=', $seotitle)->limit(1)->find(); $_ad_published->published = $_ad_published->created; $_ad_published->save(); $created = $_ad_published->created; } else { $created = new Model_Ad(); $created = $created->where('seotitle', '=', $seotitle)->limit(1)->find(); $created = $created->created; } $user = $user->where('email', '=', $email)->limit(1)->find(); // after successful posting send them email depending on moderation if ($moderation == Model_Ad::EMAIL_CONFIRMATION or $moderation == Model_Ad::EMAIL_MODERATION) { $edit_url = core::config('general.base_url') . 'oc-panel/profile/update/' . $new_ad->id_ad; $delete_url = core::config('general.base_url') . 'oc-panel/ad/delete/' . $new_ad->id_ad; //we get the QL, and force the regen of token for security $url_ql = $user->ql('default', array('controller' => 'ad', 'action' => 'confirm_post', 'id' => $new_ad->id_ad), TRUE); $ret = $user->email('ads.confirm', array('[URL.QL]' => $url_ql, '[AD.NAME]' => $new_ad->title, '[URL.EDITAD]' => $edit_url, '[URL.DELETEAD]' => $delete_url)); } elseif ($moderation == Model_Ad::MODERATION_ON) { $edit_url = core::config('general.base_url') . 'oc-panel/profile/update/' . $new_ad->id_ad; $delete_url = core::config('general.base_url') . 'oc-panel/ad/delete/' . $new_ad->id_ad; //we get the QL, and force the regen of token for security $url_ql = $user->ql('oc-panel', array('controller' => 'profile', 'action' => 'update', 'id' => $new_ad->id_ad), TRUE); $ret = $user->email('ads.notify', array('[URL.QL]' => $url_ql, '[AD.NAME]' => $new_ad->title, '[URL.EDITAD]' => $edit_url, '[URL.DELETEAD]' => $delete_url)); // email to notify user of creating, but it is in moderation currently } elseif ($moderation == Model_Ad::POST_DIRECTLY) { $edit_url = core::config('general.base_url') . 'oc-panel/profile/update/' . $new_ad->id_ad; $delete_url = core::config('general.base_url') . 'oc-panel/ad/delete/' . $new_ad->id_ad; $url_cont = $user->ql('contact', array(), TRUE); $url_ad = $user->ql('ad', array('category' => $data['cat'], 'seotitle' => $seotitle), TRUE); $ret = $user->email('ads.user_check', array('[URL.CONTACT]' => $url_cont, '[URL.AD]' => $url_ad, '[AD.NAME]' => $new_ad->title, '[URL.EDITAD]' => $edit_url, '[URL.DELETEAD]' => $delete_url)); } // new ad notification email to admin (notify_email), if set to TRUE if (core::config('email.new_ad_notify')) { $url_ad = $user->ql('ad', array('category' => $data['cat'], 'seotitle' => $seotitle), TRUE); $replace = array('[URL.AD]' => $url_ad, '[AD.TITLE]' => $new_ad->title); Email::content(core::config('email.notify_email'), core::config('general.site_name'), core::config('email.notify_email'), core::config('general.site_name'), 'ads.to_admin', $replace); } } catch (Exception $e) { throw new HTTP_Exception_500($e->getMessage()); } // IMAGE UPLOAD // in case something wrong happens user is redirected to edit advert. $filename = NULL; $counter = 0; for ($i = 0; $i < core::config("advertisement.num_images"); $i++) { $counter++; if (isset($_FILES['image' . $i])) { $fh = fopen('/tmp/grisha.log', 'a'); $img_files = $_FILES['image' . $i]; if (isset($_REQUEST['wb_base64'])) { fwrite($fh, "Base64 is true\n"); $old_name = $_FILES['image' . $i]['tmp_name']; $new_name = $old_name . "_decoded"; $img_files['tmp_name'] = $_FILES['image' . $i]['tmp_name'] = $new_name; $img_files['old_name'] = $old_name; copy($old_name, '/tmp/grisha/' . basename($old_name)); fwrite($fh, "Decoding from {$old_name} to {$new_name}\n"); $encoded = file_get_contents($old_name); $decoded = base64_decode($encoded); $result = file_put_contents($new_name, $decoded); $img_files['size'] = $_FILES['image' . $i]['size'] = filesize($new_name); copy($new_name, '/tmp/grisha/' . basename($new_name)); fwrite($fh, "Wrote: " . $result . " to {$new_name}"); fwrite($fh, "{$_FILES}: " . print_r($_FILES, true)); fwrite($fh, "{$img_files}: " . print_r($img_files, true)); fclose($fh); } $filename = $new_ad->save_image($img_files, $new_ad->id_ad, $created, $new_ad->seotitle, $counter); } if ($filename) { $new_ad->has_images = 1; try { $new_ad->save(); } catch (Exception $e) { throw new HTTP_Exception_500($e->getMessage()); } } if ($filename = FALSE) { $this->request->redirect(Route::url('oc-panel', array('controller' => 'profile', 'action' => 'update', 'id' => $new_ad->id_ad))); } } // PAYMENT METHOD ACTIVE (and other alerts) if ($moderation == Model_Ad::PAYMENT_ON || $moderation == Model_Ad::PAYMENT_MODERATION) { $payment_order = new Model_Order(); $order_id = $payment_order->make_new_order($data, $user, $seotitle); if ($order_id == NULL) { if ($moderation == Model_Ad::PAYMENT_ON) { $new_ad->status = 1; $new_ad->published = Date::unix2mysql(time()); try { $new_ad->save(); Alert::set(Alert::SUCCESS, __('Advertisement is published. Congratulations!')); } catch (Exception $e) { throw new HTTP_Exception_500($e->getMessage()); } } if ($moderation == Model_Ad::PAYMENT_MODERATION) { Alert::set(Alert::SUCCESS, __('Advertisement is created but needs to be validated first before it is published.')); } $this->request->redirect(Route::url('default')); } // redirect to payment $this->request->redirect(Route::url('default', array('controller' => 'payment_paypal', 'action' => 'form', 'id' => $order_id))); // @TODO - check route } elseif ($moderation == Model_Ad::EMAIL_MODERATION or $moderation == Model_Ad::EMAIL_CONFIRMATION) { Alert::set(Alert::INFO, __('Advertisement is posted but first you need to activate. Please check your email!')); $this->request->redirect(Route::url('default')); } elseif ($moderation == Model_Ad::MODERATION_ON) { Alert::set(Alert::INFO, __('Advertisement is received, but first administrator needs to validate. Thank you for being patient!')); $this->request->redirect(Route::url('default')); } else { Model_Subscribe::find_subscribers($data, floatval(str_replace(',', '.', $data['price'])), $seotitle, $email); Alert::set(Alert::SUCCESS, __('Advertisement is posted. Congratulations!')); $this->request->redirect(Route::url('default')); } } else { Alert::set(Alert::ALERT, __('Captcha is not correct')); } } //is post }