/frontend/controllers/ProjectController.php

https://gitlab.com/jafari/resana · PHP · 216 lines · 147 code · 18 blank · 51 comment · 17 complexity · 9dbf42351a14593647de8e2247a0bfd7 MD5 · raw file

  1. <?php
  2. namespace frontend\controllers;
  3. use Yii;
  4. use common\models\Project;
  5. use yii\filters\AccessControl;
  6. use yii\web\Controller;
  7. use yii\web\NotFoundHttpException;
  8. use yii\filters\VerbFilter;
  9. use yii\web\Response;
  10. /**
  11. * ProjectController implements the CRUD actions for Project model.
  12. */
  13. class ProjectController extends Controller
  14. {
  15. /**
  16. * @inheritdoc
  17. */
  18. public function behaviors()
  19. {
  20. return [
  21. 'access' => [
  22. 'class' => AccessControl::className(),
  23. 'rules' => [
  24. [
  25. 'allow' => true,
  26. 'roles' => ['member'],
  27. 'actions' => ['index', 'create']
  28. ],
  29. [
  30. 'allow' => true,
  31. 'roles' => ['readProject'],
  32. 'actions' => ['view'],
  33. ],
  34. [
  35. 'allow' => true,
  36. 'roles' => ['updateProject'],
  37. 'actions' => ['delete', 'update','add-auth-key', 'delete-auth-key', 'auth-key'],
  38. ],
  39. ],
  40. ],
  41. 'verbs' => [
  42. 'class' => VerbFilter::className(),
  43. 'actions' => [
  44. 'delete' => ['post'],
  45. ],
  46. ],
  47. ];
  48. }
  49. /**
  50. * Lists all Project models.
  51. * @return mixed
  52. */
  53. public function actionIndex()
  54. {
  55. $myProjects = Project::find()->active()->my()->all();
  56. $contributeProjects = Project::find()->active()->contribute()->all();
  57. return $this->render('index', compact('myProjects', 'contributeProjects'));
  58. }
  59. /**
  60. * Index of AuthKey.
  61. * @return mixed
  62. */
  63. public function actionAuthKey($id)
  64. {
  65. $model = $this->findModel($id);
  66. $model->scenario = Project::SCENARIO_GENERATE_AUTH_KEY;
  67. if (Yii::$app->request->isAjax) {
  68. return $this->renderAjax('auth-key', [
  69. 'model' => $model,
  70. ]);
  71. }
  72. return $this->render('auth-key', [
  73. 'model' => $model,
  74. ]);
  75. }
  76. /**
  77. * Add auth_key for project.
  78. * @return mixed
  79. */
  80. public function actionAddAuthKey($id)
  81. {
  82. $model = $this->findModel($id);
  83. $model->scenario = Project::SCENARIO_GENERATE_AUTH_KEY;
  84. if (Yii::$app->request->isAjax) {
  85. Yii::$app->response->format = Response::FORMAT_JSON;
  86. if ($model->setAuthKey()) {
  87. return ['success' => true];
  88. }
  89. return ['success' => false, 'errors' => $model->errors];
  90. }
  91. return $this->redirect('auth-key');
  92. }
  93. /**
  94. * Delete auth_key field for capability of assign new token to user.
  95. * @return mixed
  96. */
  97. public function actionDeleteAuthKey($id)
  98. {
  99. $model = $this->findModel($id);
  100. $model->scenario = Project::SCENARIO_GENERATE_AUTH_KEY;
  101. if (Yii::$app->request->isAjax) {
  102. Yii::$app->response->format = Response::FORMAT_JSON;
  103. if ($model->deleteAuthKey()) {
  104. return ['success' => true];
  105. }
  106. return ['success' => false, 'errors' => $model->errors];
  107. }
  108. return $this->redirect('auth-key');
  109. }
  110. /**
  111. * Displays a single Project model.
  112. * @param integer $id
  113. * @return mixed
  114. */
  115. public function actionView($id)
  116. {
  117. return $this->render('view', [
  118. 'model' => $this->findModel($id),
  119. ]);
  120. }
  121. /**
  122. * Creates a new Project model.
  123. * If creation is successful, the browser will be redirected to the 'view' page.
  124. * @return mixed
  125. */
  126. public function actionCreate()
  127. {
  128. $model = new Project();
  129. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  130. if (Yii::$app->request->isAjax) {
  131. Yii::$app->response->format = Response::FORMAT_JSON;
  132. return ['success' => true];
  133. }
  134. return $this->redirect(['view', 'id' => $model->id]);
  135. }
  136. if (Yii::$app->request->isAjax) {
  137. return $this->renderAjax('_form', [
  138. 'model' => $model,
  139. ]);
  140. } else {
  141. return $this->render('create', [
  142. 'model' => $model,
  143. ]);
  144. }
  145. }
  146. /**
  147. * Updates an existing Project model.
  148. * If update is successful, the browser will be redirected to the 'view' page.
  149. * @param integer $id
  150. * @return mixed
  151. */
  152. public function actionUpdate($id)
  153. {
  154. $model = $this->findModel($id);
  155. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  156. if (Yii::$app->request->isAjax) {
  157. Yii::$app->response->format = Response::FORMAT_JSON;
  158. return ['success' => true];
  159. }
  160. return $this->redirect(['view', 'id' => $model->id]);
  161. }
  162. if (Yii::$app->request->isAjax) {
  163. return $this->renderAjax('_form', [
  164. 'model' => $model,
  165. ]);
  166. } else {
  167. return $this->render('update', [
  168. 'model' => $model,
  169. ]);
  170. }
  171. }
  172. /**
  173. * Deletes an existing Project model.
  174. * If deletion is successful, the browser will be redirected to the 'index' page.
  175. * @param integer $id
  176. * @return mixed
  177. */
  178. public function actionDelete($id)
  179. {
  180. $this->findModel($id)->disable();
  181. return $this->redirect(['index']);
  182. }
  183. /**
  184. * Finds the Project model based on its primary key value.
  185. * If the model is not found, a 404 HTTP exception will be thrown.
  186. * @param integer $id
  187. * @return Project the loaded model
  188. * @throws NotFoundHttpException if the model cannot be found
  189. */
  190. protected function findModel($id)
  191. {
  192. if (($model = Project::findOne($id)) !== null) {
  193. return $model;
  194. } else {
  195. throw new NotFoundHttpException('The requested page does not exist.');
  196. }
  197. }
  198. }