public function actionBuyTariffPlan() { $this->layout = '//layouts/usercpanel'; $user = HUser::getModel(); $tariffId = Yii::app()->request->getParam('tariffid'); if (!$user || !$tariffId) { throw404(); } $currentTariffModel = TariffPlans::model()->findByPk($tariffId); if (!$currentTariffModel || $currentTariffModel->active != TariffPlans::STATUS_ACTIVE) { throw404(); } // check current user tariff plan $currentTariffPlanInfo = TariffPlans::getTariffInfoByUserId($user->id); if ($currentTariffPlanInfo['issetTariff'] && $currentTariffPlanInfo['tariffDuration']) { if (!$currentTariffPlanInfo['activeTariff']) { Yii::app()->user->setFlash('error', Yii::t("module_tariffPlans", "You can only extend the tariff plan {name}", array("{name}" => $currentTariffPlanInfo['tariffName']))); $this->redirect(array('choosetariffplans')); Yii::app()->end(); } } // check balance if ($currentTariffModel->price) { # платный тариф if ($currentTariffModel->price > $user->balance) { Yii::app()->user->setFlash('error', tt('On your balance is not enough money to buy the chosen tariff plan', 'tariffPlans')); $this->redirect(array('choosetariffplans')); Yii::app()->end(); } } // check object count if ($currentTariffModel->limit_objects) { $usersObjects = TariffPlans::getCountUserObjects($user->id); if ($usersObjects > $currentTariffModel->limit_objects) { Yii::app()->user->setFlash('error', tt('The number of added ads exceeds the limit of the tariff. Remove its not relevant your ads and try again.', 'tariffPlans')); $this->redirect(array('choosetariffplans')); Yii::app()->end(); } } // apply action $interval = 'INTERVAL ' . $currentTariffModel->duration . ' DAY'; $dateEnd = new CDbExpression('NOW() + ' . $interval); if (TariffPlans::applyToUser($user->id, $tariffId, $dateEnd, $interval)) { if ($currentTariffModel->price) { # платный тариф $user->deductBalance($currentTariffModel->price); } Yii::app()->user->setFlash('success', tt('Tariff plan has been successfully applied', 'tariffPlans')); $this->redirect(array('tariffhistory')); } else { Yii::app()->user->setFlash('error', tc('Error. Repeat attempt later')); $this->redirect(array('choosetariffplans')); Yii::app()->end(); } }
private static function setCache() { $tariffs = TariffPlans::model()->cache(param('cachingTime', 1209600), self::getDependency())->findAll(); if ($tariffs) { foreach ($tariffs as $tariff) { self::$_cache[$tariff->id]['id'] = $tariff->id; self::$_cache[$tariff->id]['active'] = $tariff->active; self::$_cache[$tariff->id]['name'] = $tariff->name; self::$_cache[$tariff->id]['description'] = $tariff->description; self::$_cache[$tariff->id]['descriptionForBuy'] = $tariff->getDescriptionForBuy(); self::$_cache[$tariff->id]['limitObjects'] = $tariff->limit_objects; self::$_cache[$tariff->id]['limitPhotos'] = $tariff->limit_photos; self::$_cache[$tariff->id]['price'] = $tariff->price; self::$_cache[$tariff->id]['duration'] = $tariff->duration; self::$_cache[$tariff->id]['showAddress'] = $tariff->show_address; self::$_cache[$tariff->id]['showPhones'] = $tariff->show_phones; } } }