/** * Returns list of moderators for this forum. * @return integer[] */ public function getMods() { $mods = Cache::getInstance()->getElement('forum.moderators', $this->id); if ($mods === false) { $mods = []; $modteam = User::find()->select(['id', 'role'])->where(['status' => User::STATUS_ACTIVE, 'role' => [User::ROLE_ADMIN, User::ROLE_MODERATOR]])->asArray()->all(); foreach ($modteam as $user) { if ($user['role'] == User::ROLE_ADMIN) { $mods[] = $user['id']; } else { if ((new Query())->from(Mod::tableName())->where(['forum_id' => $this->id, 'user_id' => $user->id])->exists()) { $mods[] = $user['id']; } } } Cache::getInstance()->setElement('forum.moderators', $this->id, $mods); } return $mods; }
/** * Updates moderator assignment for given forums. * @param array $newForums new assigned forums' IDs * @param array $oldForums old assigned forums' IDs * @return boolean * @since 0.2 */ public function updateModeratorForMany($newForums = [], $oldForums = []) { try { $add = []; foreach ($newForums as $forum) { if (!in_array($forum, $oldForums)) { if ((new Query())->from(Forum::tableName())->where(['id' => $forum])->exists() && (new Query())->from(Mod::tableName())->where(['forum_id' => $forum, 'user_id' => $this->id])->exists() === false) { $add[] = [$forum, $this->id]; } } } $remove = []; foreach ($oldForums as $forum) { if (!in_array($forum, $newForums)) { if ((new Query())->from(Mod::tableName())->where(['forum_id' => $forum, 'user_id' => $this->id])->exists()) { $remove[] = $forum; } } } 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' => $this->id])->execute(); } Cache::getInstance()->delete('forum.moderators'); Log::info('Moderators updated', null, __METHOD__); return true; } catch (Exception $e) { Log::error($e->getMessage(), null, __METHOD__); } return false; }
/** * 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]); }
/** * Checks if User of given ID is moderator of this forum. * @param integer $user_id * @return boolean */ public function isMod($user_id = null) { return (new Query())->from(Mod::tableName())->where(['forum_id' => $this->id, 'user_id' => $user_id])->exists(); }
/** * Sets relation with Mod. * @return \yii\db\ActiveQuery */ public function getMods() { return $this->user->hasMany(Mod::className(), ['user_id' => $this->getId()]); }