/**
  * 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->categories != FALSE) {
         $this->cat_items = Model_Category::get_as_array();
         $this->cat_order_items = Model_Category::get_multidimensional();
     }
     // get all locations
     if ($this->locations != FALSE) {
         $this->loc_items = Model_Location::get_as_array();
         $this->loc_order_items = Model_Location::get_multidimensional();
     }
     if ($this->price != FALSE) {
         $this->price = TRUE;
     }
     // user
     if (Auth::instance()->logged_in()) {
         //subscriber
         // check if user is already subscribed
         $user_id = Auth::instance()->get_user()->id_user;
         $obj_subscriber = new Model_Subscribe();
         $subscriber = $obj_subscriber->where('id_user', '=', $user_id)->limit(1)->find();
         if ($subscriber->loaded()) {
             $this->subscriber = TRUE;
         }
         //if user logged in pass email and id
         $this->user_email = Auth::instance()->get_user()->email;
         $this->user_id = $user_id;
     } else {
         $this->user_id = 0;
     }
     //min - max price selected
     $this->min_price = $this->min_price;
     $this->max_price = $this->max_price;
 }
 /**
  * Function to notify subscribers
  */
 public static function notify(Model_Ad $ad)
 {
     $subscribers = new Model_Subscribe();
     if ($ad->price > 0) {
         $subscribers->where_open()->where(DB::EXPR((int) $ad->price), 'BETWEEN', array('min_price', 'max_price'))->or_where('max_price', '=', 0)->where_close();
     }
     //location is set
     if (is_numeric($ad->id_location)) {
         $subscribers->where('id_location', 'in', array($ad->id_location, 0));
     }
     //filter by category, 0 means all the cats, in case was not set
     $subscribers->where('id_category', 'in', array($ad->id_category, 0));
     $subscribers = $subscribers->find_all();
     $subscribers_id = array();
     // array to be filled with user emails
     foreach ($subscribers as $subs) {
         // do not repeat same users.
         if (!in_array($subs->id_user, $subscribers_id)) {
             $subscribers_id[] = $subs->id_user;
         }
     }
     // query for getting users, transform it to array and pass to email function
     if (count($subscribers_id) > 0) {
         $query = DB::select('email')->select('name')->from('users')->where('id_user', 'IN', $subscribers_id)->where('status', '=', Model_User::STATUS_ACTIVE)->execute();
         $users = $query->as_array();
         // Send mails like in newsletter, to multiple users simultaneously
         if (count($users) > 0) {
             $url_ad = Route::url('ad', array('category' => $ad->category->seoname, 'seotitle' => $ad->seotitle));
             $replace = array('[URL.AD]' => $url_ad, '[AD.TITLE]' => $ad->title);
             Email::content($users, '', core::config('email.notify_email'), core::config('general.site_name'), 'ads-subscribers', $replace);
         }
     }
 }
 public function action_index()
 {
     $email = Core::post('email_subscribe');
     if (Valid::email($email, TRUE)) {
         /* find user and compare emails */
         $obj_user = new Model_User();
         $user = $obj_user->where('email', '=', $email)->limit(1)->find();
         // case when user is not logged in.
         // We create new user if he doesn't exists in DB
         // and send him mail for ad created + new profile created
         if (!$user->loaded()) {
             $user = Model_User::create_email($email);
         }
         /* save this user to data base as subscriber */
         $arr_cat = Core::post('category_subscribe');
         // string in this case is returned as "int,int" so we need to format min/max price
         $price = Core::post('price_subscribe');
         if ($price = Core::post('price_subscribe')) {
             $min_price = substr($price, '0', stripos($price, ','));
             $max_price = substr($price, strrpos($price, ',') + 1);
         } else {
             //in case of mobile version
             // jquery mobile have different slider, so we need to get data differently
             $min_price = Core::post('price_subscribe-1');
             $max_price = Core::post('price_subscribe-2');
         }
         //if categry is not selected, subscribe them for al, set category to 0 thats all...
         if ($arr_cat === NULL) {
             $arr_cat[] = 0;
         }
         // create entry table subscriber for each category selected
         foreach ($arr_cat as $c => $id_value) {
             $obj_subscribe = new Model_Subscribe();
             $obj_subscribe->id_user = $user->id_user;
             $obj_subscribe->id_category = $id_value;
             $obj_subscribe->id_location = Core::post('location_subscribe');
             $obj_subscribe->min_price = $min_price;
             $obj_subscribe->max_price = $max_price;
             try {
                 $obj_subscribe->save();
             } catch (Exception $e) {
                 throw HTTP_Exception::factory(500, $e->getMessage());
             }
         }
         Alert::set(Alert::SUCCESS, __('Thank you for subscribing'));
         $this->redirect(Route::url('default'));
     } else {
         Alert::set(Alert::ALERT, __('Invalid Email'));
         $this->redirect(Route::url('default'));
     }
 }
Exemple #4
0
 /**
  * Function for saving emails to subscribers
  */
 public static function find_subscribers($data, $price, $seotitle, $email)
 {
     // locations are optional , get wiget settings for locations and categories
     $jsonObj = json_decode(core::config('widget.Widget_Subscribers_1373877069'), true);
     $subscribers = new Model_Subscribe();
     $category = new Model_Category($data['cat']);
     if ($category->loaded()) {
         if ($category->id_category_parent !== 1) {
             $cat_parent = $category->id_category_parent;
         }
     }
     //only min/max price is required in widget settings
     if ($price !== '0') {
         $subscribers->where('min_price', '<=', $price)->where('max_price', '>=', $price);
     } else {
         $subscribers->where('min_price', '<=', 0)->where('max_price', '>=', 0);
     }
     //location is set
     if ($data['loc'] != NULL and $jsonObj['data']['locations'] !== '0') {
         $subscribers = $subscribers->where('id_location', '=', $data['loc']);
     }
     //category is set
     if ($jsonObj['data']['categories'] !== '0') {
         $subscribers = $subscribers->where('id_category', 'IN', array($data['cat'], $cat_parent));
     }
     $subscribers = $subscribers->find_all();
     $subscribers_id = array();
     // array to be filled with user emails
     foreach ($subscribers as $subs) {
         // do not repeat same users.
         if (!in_array($subs->id_user, $subscribers_id)) {
             $subscribers_id[] = $subs->id_user;
         }
     }
     // query for getting users, transform it to array and pass to email function
     if (count($subscribers_id) > 0) {
         $query = DB::select('email')->select('name')->from('users')->where('id_user', 'IN', $subscribers_id)->where('status', '=', Model_User::STATUS_ACTIVE)->execute();
         $users = $query->as_array();
         $user = new Model_User();
         $user = $user->where('email', '=', $email)->where('status', '=', Model_User::STATUS_ACTIVE)->limit(1)->find();
         // Send mails like in newsletter, to multiple users simultaneously @TODO NOT YET READY
         if (count($users) > 0) {
             $url_ad = $user->ql('ad', array('category' => $data['cat'], 'seotitle' => $seotitle), TRUE);
             if (!Email::send($users, '', "Advertisement is created on " . core::config('general.site_name') . "!", "Hello, You may be interested in this one: \n\n " . $data['title'] . "! \n\n\n                                        You can visit this link to see advertisement " . $url_ad, "no-reply " . core::config('general.site_name'), core::config('email.notify_email'))) {
                 Alert::set(Alert::ERROR, __('Error on mail delivery, not sent'));
             }
         }
     }
 }
Exemple #5
0
 public function action_unsubscribe()
 {
     // unsubscribe user
     $obj_subscribe = new Model_Subscribe();
     $un_subscribe = $obj_subscribe->where('id_user', '=', $this->request->param('id'))->find_all();
     // foreach entry in table where user id, delete it
     foreach ($un_subscribe as $s) {
         try {
             $s->delete();
         } catch (Exception $e) {
             throw new HTTP_Exception_500($e->getMessage());
         }
     }
     Alert::set(Alert::SUCCESS, __('You are unsubscribed'));
     $this->request->redirect(Route::url('default'));
 }
Exemple #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->categories != 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
     if ($this->locations != FALSE) {
         list($locations, $order_locations) = Model_Location::get_all();
         $this->loc_items = $locations;
         $this->loc_order_items = $order_locations;
     }
     if ($this->price != FALSE) {
         $this->price = TRUE;
     }
     // user
     if (Auth::instance()->logged_in()) {
         //subscriber
         // check if user is already subscribed
         $user_id = Auth::instance()->get_user()->id_user;
         $obj_subscriber = new Model_Subscribe();
         $subscriber = $obj_subscriber->where('id_user', '=', $user_id)->limit(1)->find();
         if ($subscriber->loaded()) {
             $this->subscriber = TRUE;
         }
         //if user logged in pass email and id
         $this->user_email = Auth::instance()->get_user()->email;
         $this->user_id = $user_id;
     } else {
         $this->user_id = 0;
     }
     //min - max price selected
     $this->min_price = $this->min_price;
     $this->max_price = $this->max_price;
 }
Exemple #7
0
 /**
  * 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);
 }
 /**
  * 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)));
     }
 }
 public function action_unsubscribe()
 {
     $id_subscribe = $this->request->param('id');
     $subscription = new Model_Subscribe($id_subscribe);
     if ($subscription->loaded() and $subscription->id_user == Auth::instance()->get_user()->id_user) {
         try {
             $subscription->delete();
             Alert::set(Alert::SUCCESS, __('You are unsubscribed'));
         } catch (Exception $e) {
             throw HTTP_Exception::factory(500, $e->getMessage());
         }
         //unsusbcribe from elasticemail
         if (Core::config('email.elastic_listname') != '') {
             ElasticEmail::subscribe(Core::config('email.elastic_listname'), Auth::instance()->get_user()->email);
         }
         $this->redirect(Route::url('oc-panel', array('controller' => 'profile', 'action' => 'subscriptions')));
     }
 }
Exemple #10
0
 /**
  * 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);
     }
 }
Exemple #11
0
 /**
  * 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);
     }
 }
Exemple #12
0
 /**
  * Mark advertisement as active : STATUS = 1
  */
 public function action_activate()
 {
     $id = $this->request->param('id');
     $id_ads = (isset($id) and is_numeric($id)) ? array($id) : Core::get('id_ads');
     $param_current_url = Core::get('current_url');
     if (is_array($id_ads)) {
         $ads = new Model_Ad();
         $ads = $ads->where('id_ad', 'in', $id_ads)->find_all();
         foreach ($ads as $ad) {
             //if theres subscription we need to check
             if (Core::config('general.subscriptions') == TRUE and $ad->user->subscription()->loaded() and $ad->user->subscription()->amount_ads_left <= 0 and $ad->user->subscription()->amount_ads_left != -1) {
                 Alert::set(Alert::WARNING, sprintf(__('The customer %s does not have more ads left to publish.'), $ad->user->email));
             } elseif ($ad->status != Model_Ad::STATUS_PUBLISHED) {
                 $ad->published = Date::unix2mysql();
                 $ad->status = Model_Ad::STATUS_PUBLISHED;
                 try {
                     $ad->save();
                     Model_Subscription::new_ad($ad->user);
                     Model_Subscribe::notify($ad);
                     // Post on social media
                     Social::post_ad($ad);
                 } catch (Exception $e) {
                     throw HTTP_Exception::factory(500, $e->getMessage());
                 }
             }
         }
         $this->multiple_mails($id_ads);
         // 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);
     }
 }
Exemple #13
0
 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());
             }
         }
     }
 }
Exemple #14
0
 public function action_unsubscribe()
 {
     $id_subscribe = $this->request->param('id');
     $subscription = new Model_Subscribe($id_subscribe);
     if ($subscription->loaded()) {
         try {
             $subscription->delete();
             Alert::set(Alert::SUCCESS, __('You are unsubscribed'));
             $this->request->redirect(Route::url('oc-panel', array('controller' => 'profile', 'action' => 'subscriptions')));
         } catch (Exception $e) {
             throw new HTTP_Exception_500($e->getMessage());
         }
     }
 }
Exemple #15
0
 /**
  * [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
 }