コード例 #1
0
 /**
  * Listing and updating moderation list for the forum of given ID.
  * @param integer $id forum ID
  * @return string|\yii\web\Response
  */
 public function actionMods($id = null)
 {
     $mod = null;
     $moderators = User::find()->where(['role' => User::ROLE_MODERATOR])->indexBy('id')->all();
     if (is_numeric($id) && $id > 0) {
         if (isset($moderators[$id])) {
             $mod = $moderators[$id];
         }
     } else {
         reset($moderators);
         $mod = current($moderators);
     }
     $searchModel = new ForumSearch();
     $dataProvider = $searchModel->searchForMods(Yii::$app->request->get());
     $postData = Yii::$app->request->post();
     if ($postData) {
         if (User::can(Rbac::PERM_PROMOTE_USER)) {
             $mod_id = !empty($postData['mod_id']) && is_numeric($postData['mod_id']) && $postData['mod_id'] > 0 ? $postData['mod_id'] : 0;
             $selection = !empty($postData['selection']) ? $postData['selection'] : [];
             $pre = !empty($postData['pre']) ? $postData['pre'] : [];
             if ($mod_id != $mod->id) {
                 $this->error(Yii::t('podium/flash', 'Sorry! There was an error while selecting the moderator ID.'));
             } else {
                 try {
                     $add = [];
                     foreach ($selection as $select) {
                         if (!in_array($select, $pre)) {
                             if ((new Query())->from(Forum::tableName())->where(['id' => $select])->exists() && (new Query())->from(Mod::tableName())->where(['forum_id' => $select, 'user_id' => $mod->id])->exists() === false) {
                                 $add[] = [$select, $mod->id];
                             }
                         }
                     }
                     $remove = [];
                     foreach ($pre as $p) {
                         if (!in_array($p, $selection)) {
                             if ((new Query())->from(Mod::tableName())->where(['forum_id' => $p, 'user_id' => $mod->id])->exists()) {
                                 $remove[] = $p;
                             }
                         }
                     }
                     if (!empty($add)) {
                         Yii::$app->db->createCommand()->batchInsert(Mod::tableName(), ['forum_id', 'user_id'], $add)->execute();
                     }
                     if (!empty($remove)) {
                         Yii::$app->db->createCommand()->delete(Mod::tableName(), ['forum_id' => $remove, 'user_id' => $mod->id])->execute();
                     }
                     Cache::getInstance()->delete('forum.moderators');
                     Log::info('Moderators updated', null, __METHOD__);
                     $this->success(Yii::t('podium/flash', 'Moderation list has been saved.'));
                 } catch (Exception $e) {
                     Log::error($e->getMessage(), null, __METHOD__);
                     $this->error(Yii::t('podium/flash', 'Sorry! There was an error while saving the moderatoration list.'));
                 }
                 return $this->refresh();
             }
         } else {
             $this->error(Yii::t('podium/flash', 'You are not allowed to perform this action.'));
         }
     }
     return $this->render('mods', ['moderators' => $moderators, 'mod' => $mod, 'searchModel' => $searchModel, 'dataProvider' => $dataProvider]);
 }
コード例 #2
0
 /**
  * Listing and updating moderation list for the forum of given ID.
  * @param integer $id forum ID
  * @return string|\yii\web\Response
  */
 public function actionMods($id = null)
 {
     $mod = null;
     $moderators = User::find()->where(['role' => User::ROLE_MODERATOR])->indexBy('id')->all();
     if (is_numeric($id) && $id > 0) {
         if (isset($moderators[$id])) {
             $mod = $moderators[$id];
         }
     } else {
         reset($moderators);
         $mod = current($moderators);
     }
     $searchModel = new ForumSearch();
     $dataProvider = $searchModel->searchForMods(Yii::$app->request->get());
     $postData = Yii::$app->request->post();
     if ($postData) {
         if (!User::can(Rbac::PERM_PROMOTE_USER)) {
             $this->error(Yii::t('podium/flash', 'You are not allowed to perform this action.'));
         } else {
             $mod_id = !empty($postData['mod_id']) && is_numeric($postData['mod_id']) && $postData['mod_id'] > 0 ? $postData['mod_id'] : 0;
             $selection = !empty($postData['selection']) ? $postData['selection'] : [];
             $pre = !empty($postData['pre']) ? $postData['pre'] : [];
             if ($mod_id != $mod->id) {
                 $this->error(Yii::t('podium/flash', 'Sorry! There was an error while selecting the moderator ID.'));
             } else {
                 if ($mod->updateModeratorForMany($selection, $pre)) {
                     $this->success(Yii::t('podium/flash', 'Moderation list has been saved.'));
                 } else {
                     $this->error(Yii::t('podium/flash', 'Sorry! There was an error while saving the moderatoration list.'));
                 }
                 return $this->refresh();
             }
         }
     }
     return $this->render('mods', ['moderators' => $moderators, 'mod' => $mod, 'searchModel' => $searchModel, 'dataProvider' => $dataProvider]);
 }