CommentsController.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. namespace manager\controllers;
  3. use manager\models\Comments;
  4. use app\models\CommentsConf;
  5. use yii\data\ActiveDataProvider;
  6. use yii\web\Controller;
  7. use yii\web\NotFoundHttpException;
  8. use yii\filters\VerbFilter;
  9. /**
  10. * CommentsController implements the CRUD actions for Comments model.
  11. */
  12. class CommentsController extends BaseController
  13. {
  14. /**
  15. * @inheritDoc
  16. */
  17. public function behaviors()
  18. {
  19. return array_merge(
  20. parent::behaviors(),
  21. [
  22. 'verbs' => [
  23. 'class' => VerbFilter::className(),
  24. 'actions' => [
  25. 'delete' => ['POST'],
  26. ],
  27. ],
  28. ]
  29. );
  30. }
  31. /**
  32. * Lists all Comments models.
  33. *
  34. * @return string
  35. */
  36. public function actionIndex()
  37. {
  38. $dataProvider = new ActiveDataProvider([
  39. 'query' => Comments::find(),
  40. 'pagination' => [
  41. 'pageSize' => 50
  42. ],
  43. 'sort' => [
  44. 'defaultOrder' => [
  45. 'visible' => SORT_DESC,
  46. 'created_at' => SORT_DESC,
  47. ]
  48. ],
  49. ]);
  50. return $this->render('index', [
  51. 'dataProvider' => $dataProvider,
  52. ]);
  53. }
  54. /**
  55. * Displays a single Comments model.
  56. * @param int $id
  57. * @return string
  58. * @throws NotFoundHttpException if the model cannot be found
  59. */
  60. public function actionView($id)
  61. {
  62. return $this->render('view', [
  63. 'model' => $this->findModel($id),
  64. ]);
  65. }
  66. /**
  67. * Creates a new Comments model.
  68. * If creation is successful, the browser will be redirected to the 'view' page.
  69. * @return string|\yii\web\Response
  70. */
  71. public function actionCreate()
  72. {
  73. $model = new Comments();
  74. if ($this->request->isPost) {
  75. if ($model->load($this->request->post()) && $model->save()) {
  76. return $this->redirect(['view', 'id' => $model->id]);
  77. }
  78. } else {
  79. $model->loadDefaultValues();
  80. }
  81. return $this->render('create', [
  82. 'model' => $model,
  83. ]);
  84. }
  85. /**
  86. * Updates an existing Comments model.
  87. * If update is successful, the browser will be redirected to the 'view' page.
  88. * @param int $id
  89. * @return string|\yii\web\Response
  90. * @throws NotFoundHttpException if the model cannot be found
  91. */
  92. public function actionUpdate($id)
  93. {
  94. $model = $this->findModel($id);
  95. if ($this->request->isPost && $model->load($this->request->post()) && $model->save()) {
  96. return $this->redirect(['view', 'id' => $model->id]);
  97. }
  98. return $this->render('update', [
  99. 'model' => $model,
  100. ]);
  101. }
  102. /**
  103. * Deletes an existing Comments model.
  104. * If deletion is successful, the browser will be redirected to the 'index' page.
  105. * @param int $id
  106. * @return \yii\web\Response
  107. * @throws NotFoundHttpException if the model cannot be found
  108. */
  109. public function actionDelete($id)
  110. {
  111. $this->findModel($id)->delete();
  112. return $this->redirect(['index']);
  113. }
  114. /**
  115. * @param $id
  116. * @return string
  117. * @throws NotFoundHttpException
  118. */
  119. public function actionToggle($id,$value)
  120. {
  121. $model = $this->findModel($id);
  122. $model->updateAttributes(['visible'=>$value]);
  123. return $this->actionIndex();
  124. }
  125. public function actionAjaxtoggle()
  126. {
  127. $ret = ['stat'=>'err'];
  128. if( $this->request->isPost && $this->request->post('id') && $this->request->post('value') ){
  129. $id = $this->request->post('id');
  130. $value = $this->request->post('value');
  131. $model = $this->findModel($id);
  132. if( $model->updateAttributes(['visible'=>$value]) ){
  133. $ret = ['stat'=>'ok', 'id'=>$id, 'value'=>$value];
  134. }
  135. }
  136. return \yii\helpers\Json::encode($ret);;
  137. }
  138. /**
  139. * Finds the Comments model based on its primary key value.
  140. * If the model is not found, a 404 HTTP exception will be thrown.
  141. * @param int $id
  142. * @return Comments the loaded model
  143. * @throws NotFoundHttpException if the model cannot be found
  144. */
  145. protected function findModel($id)
  146. {
  147. if (($model = Comments::findOne(['id' => $id])) !== null) {
  148. return $model;
  149. }
  150. throw new NotFoundHttpException('The requested page does not exist.');
  151. }
  152. public function actionConf()
  153. {
  154. $model = $this->confModel();
  155. if ($this->request->isPost && $post = $this->request->post()){
  156. $post['Conf'] = $this->formNormalizer($post['Conf']);
  157. if( $model->load($post,'Conf') && $model->save()) {
  158. \Yii::$app->cache->delete(CommentsConf::$keysCache);
  159. return $this->redirect('index');
  160. }
  161. }
  162. return $this->render('conf', [
  163. 'model' => $model,
  164. ]);
  165. }
  166. public function formNormalizer($post)
  167. {
  168. $post['AllStop'] = $this->CheckYN(@$post['AllStop']);
  169. $post['reg'] = $this->CheckYN(@$post['reg']);
  170. $post['d0'] = $this->CheckYN(@$post['d0']);
  171. $post['d1'] = $this->CheckYN(@$post['d1']);
  172. $post['d2'] = $this->CheckYN(@$post['d2']);
  173. $post['d3'] = $this->CheckYN(@$post['d3']);
  174. $post['d4'] = $this->CheckYN(@$post['d4']);
  175. $post['d5'] = $this->CheckYN(@$post['d5']);
  176. $post['d6'] = $this->CheckYN(@$post['d6']);
  177. return $post;
  178. }
  179. public function CheckYN($attr)
  180. {
  181. return (isset($attr) && $attr == 'Y')?'Y':'N';
  182. }
  183. protected function confModel()
  184. {
  185. if (($model = CommentsConf::findOne(0)) !== null) {
  186. return $model;
  187. }
  188. throw new NotFoundHttpException('The requested page does not exist.');
  189. }
  190. }