/src/controllers/ForumThreadController.php

https://github.com/bizley/yii2-podium · PHP · 237 lines · 185 code · 14 blank · 38 comment · 27 complexity · efadba82f72fe45d26e0adb829896dc3 MD5 · raw file

  1. <?php
  2. namespace bizley\podium\controllers;
  3. use bizley\podium\filters\AccessControl;
  4. use bizley\podium\models\Category;
  5. use bizley\podium\models\Forum;
  6. use bizley\podium\models\Thread;
  7. use bizley\podium\models\User;
  8. use bizley\podium\rbac\Rbac;
  9. use bizley\podium\services\ThreadVerifier;
  10. use Yii;
  11. use yii\helpers\Html;
  12. use yii\web\Response;
  13. /**
  14. * Podium Forum controller
  15. * All actions concerning threads.
  16. * Not accessible directly.
  17. *
  18. * @author Paweł Bizley Brzozowski <pawel@positive.codes>
  19. * @since 0.5
  20. */
  21. class ForumThreadController extends BaseController
  22. {
  23. /**
  24. * @inheritdoc
  25. */
  26. public function behaviors()
  27. {
  28. return [
  29. 'access' => [
  30. 'class' => AccessControl::className(),
  31. 'rules' => [['allow' => false]],
  32. ],
  33. ];
  34. }
  35. /**
  36. * Returns separated thread actions.
  37. * @return array
  38. * @since 0.6
  39. */
  40. public function actions()
  41. {
  42. return [
  43. 'lock' => [
  44. 'class' => 'bizley\podium\actions\ThreadAction',
  45. 'permission' => Rbac::PERM_LOCK_THREAD,
  46. 'boolAttribute' => 'locked',
  47. 'switcher' => 'podiumLock',
  48. 'onMessage' => Yii::t('podium/flash', 'Thread has been locked.'),
  49. 'offMessage' => Yii::t('podium/flash', 'Thread has been unlocked.')
  50. ],
  51. 'pin' => [
  52. 'class' => 'bizley\podium\actions\ThreadAction',
  53. 'permission' => Rbac::PERM_PIN_THREAD,
  54. 'boolAttribute' => 'pinned',
  55. 'switcher' => 'podiumPin',
  56. 'onMessage' => Yii::t('podium/flash', 'Thread has been pinned.'),
  57. 'offMessage' => Yii::t('podium/flash', 'Thread has been unpinned.')
  58. ],
  59. ];
  60. }
  61. /**
  62. * Deleting the thread of given category ID, forum ID, own ID and slug.
  63. * @param int $cid category ID
  64. * @param int $fid forum ID
  65. * @param int $id thread ID
  66. * @param string $slug thread slug
  67. * @return string|Response
  68. */
  69. public function actionDelete($cid = null, $fid = null, $id = null, $slug = null)
  70. {
  71. $thread = (new ThreadVerifier([
  72. 'categoryId' => $cid,
  73. 'forumId' => $fid,
  74. 'threadId' => $id,
  75. 'threadSlug' => $slug
  76. ]))->verify();
  77. if (empty($thread)) {
  78. $this->error(Yii::t('podium/flash', 'Sorry! We can not find the thread you are looking for.'));
  79. return $this->redirect(['forum/index']);
  80. }
  81. if (!User::can(Rbac::PERM_DELETE_THREAD, ['item' => $thread])) {
  82. $this->error(Yii::t('podium/flash', 'Sorry! You do not have the required permission to perform this action.'));
  83. return $this->redirect(['forum/index']);
  84. }
  85. $postData = Yii::$app->request->post('thread');
  86. if ($postData) {
  87. if ($postData != $thread->id) {
  88. $this->error(Yii::t('podium/flash', 'Sorry! There was an error while deleting the thread.'));
  89. } else {
  90. if ($thread->podiumDelete()) {
  91. $this->success(Yii::t('podium/flash', 'Thread has been deleted.'));
  92. return $this->redirect([
  93. 'forum/forum',
  94. 'cid' => $thread->forum->category_id,
  95. 'id' => $thread->forum->id,
  96. 'slug' => $thread->forum->slug
  97. ]);
  98. }
  99. $this->error(Yii::t('podium/flash', 'Sorry! There was an error while deleting the thread.'));
  100. }
  101. }
  102. return $this->render('delete', ['model' => $thread]);
  103. }
  104. /**
  105. * Moving the thread of given category ID, forum ID, own ID and slug.
  106. * @param int $cid category ID
  107. * @param int $fid forum ID
  108. * @param int $id thread ID
  109. * @param string $slug thread slug
  110. * @return string|Response
  111. */
  112. public function actionMove($cid = null, $fid = null, $id = null, $slug = null)
  113. {
  114. $thread = (new ThreadVerifier([
  115. 'categoryId' => $cid,
  116. 'forumId' => $fid,
  117. 'threadId' => $id,
  118. 'threadSlug' => $slug
  119. ]))->verify();
  120. if (empty($thread)) {
  121. $this->error(Yii::t('podium/flash', 'Sorry! We can not find the thread you are looking for.'));
  122. return $this->redirect(['forum/index']);
  123. }
  124. if (!User::can(Rbac::PERM_MOVE_THREAD, ['item' => $thread])) {
  125. $this->error(Yii::t('podium/flash', 'Sorry! You do not have the required permission to perform this action.'));
  126. return $this->redirect(['forum/index']);
  127. }
  128. $forum = Yii::$app->request->post('forum');
  129. if ($forum) {
  130. if (!is_numeric($forum) || $forum < 1 || $forum == $thread->forum->id) {
  131. $this->error(Yii::t('podium/flash', 'You have to select the new forum.'));
  132. } else {
  133. if ($thread->podiumMoveTo($forum)) {
  134. $this->success(Yii::t('podium/flash', 'Thread has been moved.'));
  135. return $this->redirect([
  136. 'forum/thread',
  137. 'cid' => $thread->forum->category->id,
  138. 'fid' => $thread->forum->id,
  139. 'id' => $thread->id,
  140. 'slug' => $thread->slug
  141. ]);
  142. }
  143. $this->error(Yii::t('podium/flash', 'Sorry! There was an error while moving the thread.'));
  144. }
  145. }
  146. $categories = Category::find()->orderBy(['name' => SORT_ASC]);
  147. $forums = Forum::find()->orderBy(['name' => SORT_ASC]);
  148. $list = [];
  149. $options = [];
  150. foreach ($categories->each() as $cat) {
  151. $catlist = [];
  152. foreach ($forums->each() as $for) {
  153. if ($for->category_id == $cat->id) {
  154. $catlist[$for->id] = (User::can(Rbac::PERM_UPDATE_THREAD, ['item' => $for]) ? '* ' : '')
  155. . Html::encode($cat->name)
  156. . ' &raquo; '
  157. . Html::encode($for->name);
  158. if ($for->id == $thread->forum->id) {
  159. $options[$for->id] = ['disabled' => true];
  160. }
  161. }
  162. }
  163. $list[Html::encode($cat->name)] = $catlist;
  164. }
  165. return $this->render('move', [
  166. 'model' => $thread,
  167. 'list' => $list,
  168. 'options' => $options
  169. ]);
  170. }
  171. /**
  172. * Creating the thread of given category ID and forum ID.
  173. * @param int $cid category ID
  174. * @param int $fid forum ID
  175. * @return string|Response
  176. */
  177. public function actionNewThread($cid = null, $fid = null)
  178. {
  179. if (!User::can(Rbac::PERM_CREATE_THREAD)) {
  180. $this->error(Yii::t('podium/flash', 'Sorry! You do not have the required permission to perform this action.'));
  181. return $this->redirect(['forum/index']);
  182. }
  183. $forum = Forum::find()->where(['id' => $fid, 'category_id' => $cid])->limit(1)->one();
  184. if (empty($forum)) {
  185. $this->error(Yii::t('podium/flash', 'Sorry! We can not find the forum you are looking for.'));
  186. return $this->redirect(['forum/index']);
  187. }
  188. $model = new Thread();
  189. $model->scenario = 'new';
  190. $model->subscribe = 1;
  191. $preview = false;
  192. $postData = Yii::$app->request->post();
  193. if ($model->load($postData)) {
  194. $model->posts = 0;
  195. $model->views = 0;
  196. $model->category_id = $forum->category->id;
  197. $model->forum_id = $forum->id;
  198. $model->author_id = User::loggedId();
  199. if ($model->validate()) {
  200. if (isset($postData['preview-button'])) {
  201. $preview = true;
  202. } else {
  203. if ($model->podiumNew()) {
  204. $this->success(Yii::t('podium/flash', 'New thread has been created.'));
  205. return $this->redirect([
  206. 'forum/thread',
  207. 'cid' => $forum->category->id,
  208. 'fid' => $forum->id,
  209. 'id' => $model->id,
  210. 'slug' => $model->slug
  211. ]);
  212. }
  213. $this->error(Yii::t('podium/flash', 'Sorry! There was an error while creating the thread. Contact administrator about this problem.'));
  214. }
  215. }
  216. }
  217. return $this->render('new-thread', [
  218. 'preview' => $preview,
  219. 'model' => $model,
  220. 'forum' => $forum,
  221. ]);
  222. }
  223. }