/** * Удаление бонуса у юзера * * @param int $user_id * @param int $bonus_id * * @throws Exception */ public function actionDelBonus($user_id, $bonus_id) { $model = UserBonuses::model()->find('id = :id AND user_id = :user_id', array(':id' => $bonus_id, ':user_id' => $user_id)); if (!$model) { throw new Exception(404, Yii::t('backend', 'Бонус не найден')); } $model->delete(); $this->ajax['status'] = TRUE; $this->ajax['msg'] = Yii::t('backend', 'Бонус <b>:name</b> удален', array(':name' => e($model->bonusInfo->title))); echo json_encode($this->ajax); }
public function actionActivation($bonus_id) { if (request()->isPostRequest) { $char_id = (int) request()->getPost('char_id'); $criteria = new CDbCriteria(); $criteria->addCondition('user_id = :user_id AND t.id = :id'); $criteria->params = array(':user_id' => user()->getId(), ':id' => $bonus_id); $criteria->with = array('bonusInfo' => array('scopes' => array('opened'), 'with' => array('items' => array('scopes' => array('opened'), 'with' => 'itemInfo')))); $bonus = UserBonuses::model()->find($criteria); if ($bonus === NULL) { user()->setFlash(FlashConst::MESSAGE_ERROR, Yii::t('main', 'Бонус не найден.')); } elseif ($bonus->status == 1) { user()->setFlash(FlashConst::MESSAGE_ERROR, Yii::t('main', 'Бонус уже активирован (дата активации: :date_activation).', array(':date_activation' => $bonus->updated_at))); } else { try { $l2 = l2('gs', user()->gs_id)->connect(); $charIdfieldName = $l2->getField('characters.char_id'); $character = $l2->getDb()->createCommand("SELECT COUNT(0) FROM {{characters}} WHERE " . $charIdfieldName . " = :char_id")->bindParam(':char_id', $char_id, PDO::PARAM_INT)->queryScalar(); if (!$character) { user()->setFlash(FlashConst::MESSAGE_ERROR, Yii::t('main', 'Персонаж на сервере не найден.')); } else { // Предметы для записи в БД $items = array(); foreach ($bonus->bonusInfo->items as $item) { $items[] = array('owner_id' => $char_id, 'item_id' => $item->item_id, 'count' => $item->count, 'enchant' => $item->enchant); } $res = $l2->multiInsertItem($items); if ($res > 0) { $bonus->status = 1; $bonus->save(FALSE); // Логирую действие юзера if (app()->params['user_actions_log']) { $log = new UserActionsLog(); $log->user_id = user()->getId(); $log->action_id = UserActionsLog::ACTION_ACTIVATED_BONUS; $log->params = json_encode($items); $log->save(FALSE); } user()->setFlash(FlashConst::MESSAGE_SUCCESS, Yii::t('main', 'Бонус активирован.')); } else { user()->setFlash(FlashConst::MESSAGE_ERROR, Yii::t('main', 'Не удалось активировать Ваш бонус.')); } } } catch (Exception $e) { user()->setFlash(FlashConst::MESSAGE_ERROR, Yii::t('main', 'Произошла ошибка! Попробуйте повторить позже.')); Yii::log($e->getMessage(), CLogger::LEVEL_ERROR, __METHOD__ . ' ' . __LINE__); } } } else { user()->setFlash(FlashConst::MESSAGE_ERROR, Yii::t('main', 'Ошибка! Переданы не все параметры.')); } $this->redirectBack(); }