/** * new order therefore new subscription created * @param Model_Order $order * @return void */ public static function new_order(Model_Order $order) { $plan = new Model_Plan($order->id_product); //disable all the previous membership DB::update('subscriptions')->set(array('status' => 0))->where('id_user', '=', $order->id_user)->execute(); //create a new subscription for this product $subscription = new Model_Subscription(); $subscription->id_order = $order->id_order; $subscription->id_user = $order->id_user; $subscription->id_plan = $plan->id_plan; $subscription->amount_ads = $plan->amount_ads; $subscription->amount_ads_left = $plan->amount_ads; $subscription->expire_date = Date::unix2mysql(strtotime('+' . $plan->days . ' days')); $subscription->status = 1; try { $subscription->save(); } catch (Exception $e) { throw HTTP_Exception::factory(500, $e->getMessage()); } }
/** User adds himself to a mailing list */ public static function subscribe() { if (!$_REQUEST['list']) { return RedView::set('error', "Choose a list."); } $list = R::load('list', $_REQUEST['list']); if (!$list->id) { return RedView::set('error', "List doesn't exist."); } $scrip = Model_Subscription::createBean($_REQUEST); $scrip->main = self::getUser(); R::associate($list, $scrip); $_SESSION['user'] = $scrip->main->export(); return RedView::set('message', 'Success!'); }
/** * confirm payment for order * * @param string $id_order [unique indentifier of order] * @param string $txn_id id of the transaction depending on provider */ public function confirm_payment($paymethod = 'paypal', $txn_id = NULL) { // update orders if ($this->loaded()) { $this->status = self::STATUS_PAID; $this->pay_date = Date::unix2mysql(); $this->paymethod = $paymethod; $this->txn_id = $txn_id; try { $this->save(); } catch (Exception $e) { throw HTTP_Exception::factory(500, $e->getMessage()); } //if saved delete coupon from session and -- number of coupons. Model_Coupon::sale($this->coupon); //for membership plans if ($this->id_product >= 100) { Model_Subscription::new_order($this); $replace_email = array('[AD.TITLE]' => $this->description, '[URL.AD]' => Route::url('pricing'), '[ORDER.ID]' => $this->id_order, '[PRODUCT.ID]' => $this->id_product, '[VAT.COUNTRY]' => (isset($this->VAT) and $this->VAT > 0) ? $this->VAT_country : '', '[VAT.NUMBER]' => (isset($this->VAT) and $this->VAT > 0) ? $this->VAT_number : '', '[VAT.PERCENTAGE]' => (isset($this->VAT) and $this->VAT > 0) ? $this->VAT : ''); } else { $ad = $this->ad; //depending on the product different actions switch ($this->id_product) { case Model_Order::PRODUCT_AD_SELL: $ad->sale($this); break; case Model_Order::PRODUCT_TO_TOP: $ad->to_top(); break; case Model_Order::PRODUCT_TO_FEATURED: $ad->to_feature($this->featured_days); Social::social_post_featured_ad($ad); break; case Model_Order::PRODUCT_CATEGORY: $ad->paid_category(); break; } $url_ad = Route::url('ad', array('category' => $ad->category->seoname, 'seotitle' => $ad->seotitle)); $replace_email = array('[AD.TITLE]' => $ad->title, '[URL.AD]' => $url_ad, '[ORDER.ID]' => $this->id_order, '[PRODUCT.ID]' => $this->id_product, '[VAT.COUNTRY]' => (isset($this->VAT) and $this->VAT > 0) ? $this->VAT_country : '', '[VAT.NUMBER]' => (isset($this->VAT) and $this->VAT > 0) ? $this->VAT_number : '', '[VAT.PERCENTAGE]' => (isset($this->VAT) and $this->VAT > 0) ? $this->VAT : ''); } //send email to site owner! new sale!! if (core::config('email.new_ad_notify') == TRUE) { Email::content(core::config('email.notify_email'), core::config('general.site_name'), core::config('email.notify_email'), core::config('general.site_name'), 'ads-sold', $replace_email); } } }
/** * 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) { if (Core::config('general.subscriptions') == TRUE and $advert->user->subscription()->loaded() and $advert->user->subscription()->amount_ads_left <= 0 and $advert->user->subscription()->amount_ads_left != -1) { Alert::set(Alert::WARNING, sprintf(__('You do not have more ads left to publish.'), $active_ad->email)); HTTP::redirect(Route::url('pricing')); } else { $advert->status = Model_Ad::STATUS_PUBLISHED; // status active $advert->published = Date::unix2mysql(); try { $advert->save(); Model_Subscription::new_ad($advert->user); 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 (in_array($moderation, [Model_Ad::PAYMENT_ON, Model_Ad::PAYMENT_MODERATION])) { // check category price $amount = $ad->category->price > 0 ? $ad->category->price : $ad->category->parent->price; //swapping moderation since theres no price :( if ($amount == 0) { if ($moderation == Model_Ad::PAYMENT_MODERATION) { $moderation = Model_Ad::MODERATION_ON; } else { $moderation = Model_Ad::POST_DIRECTLY; } } } //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_Subscription::new_ad($ad->user); 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); }
public function testsubscription_delete() { $data = array('id' => 1); $subscriptionService = $this->getMockBuilder('\\Box\\Mod\\Invoice\\ServiceSubscription')->getMock(); $subscriptionService->expects($this->atLeastOnce())->method('delete')->will($this->returnValue(true)); $validatorMock = $this->getMockBuilder('\\Box_Validate')->getMock(); $validatorMock->expects($this->atLeastOnce())->method('checkRequiredParamsForArray'); $dbMock = $this->getMockBuilder('\\Box_Database')->getMock(); $model = new \Model_Subscription(); $model->loadBean(new \RedBeanPHP\OODBBean()); $dbMock->expects($this->atLeastOnce())->method('getExistingModelById')->will($this->returnValue($model)); $di = new \Box_Di(); $di['validator'] = $validatorMock; $di['db'] = $dbMock; $di['mod_service'] = $di->protect(function () use($subscriptionService) { return $subscriptionService; }); $this->api->setDi($di); $result = $this->api->subscription_delete($data); $this->assertInternalType('bool', $result); $this->assertTrue($result); }
/** * get the current subscription of the user * @return Model_Subscription */ public function subscription() { $s = new Model_Subscription(); $s->where('id_user', '=', $this->id_user)->where('status', '=', 1)->order_by('created', 'desc')->find(); return $s; }
public function testunsubscribe() { $subscribtionModel = new \Model_Subscription(); $subscribtionModel->loadBean(new \RedBeanPHP\OODBBean()); $dbMock = $this->getMockBuilder('\\Box_Database')->getMock(); $dbMock->expects($this->atLeastOnce())->method('store'); $di = new \Box_Di(); $di['db'] = $dbMock; $this->service->setDi($di); $this->service->unsubscribe($subscribtionModel); }
/** * 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); } }