/** * 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); }
/** * 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); } }
/** * 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))); } }
/** * 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); } }