CommentsController.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 (isset($_POST['Comments'])) {
  96. $model->message = $_POST['Comments']['message'];
  97. $model->visible = $_POST['Comments']['visible'];
  98. if ($model->save()) {
  99. return $this->redirect(['view', 'id' => $model->id]);
  100. }
  101. }
  102. return $this->render('update', [
  103. 'model' => $model,
  104. ]);
  105. }
  106. /**
  107. * Deletes an existing Comments model.
  108. * If deletion is successful, the browser will be redirected to the 'index' page.
  109. * @param int $id
  110. * @return \yii\web\Response
  111. * @throws NotFoundHttpException if the model cannot be found
  112. */
  113. public function actionDelete($id)
  114. {
  115. $this->findModel($id)->delete();
  116. return $this->redirect(['index']);
  117. }
  118. /**
  119. * @param $id
  120. * @return string
  121. * @throws NotFoundHttpException
  122. */
  123. public function actionToggle($id,$value)
  124. {
  125. $model = $this->findModel($id);
  126. $model->updateAttributes(['visible'=>$value]);
  127. return $this->actionIndex();
  128. }
  129. public function actionAjaxtoggle()
  130. {
  131. $ret = ['stat'=>'err'];
  132. if( $this->request->isPost && $this->request->post('id') && $this->request->post('value') ){
  133. $id = $this->request->post('id');
  134. $value = $this->request->post('value');
  135. $model = $this->findModel($id);
  136. if( $model->updateAttributes(['visible'=>$value]) ){
  137. $ret = ['stat'=>'ok', 'id'=>$id, 'value'=>$value];
  138. }
  139. }
  140. return \yii\helpers\Json::encode($ret);;
  141. }
  142. /**
  143. * Finds the Comments model based on its primary key value.
  144. * If the model is not found, a 404 HTTP exception will be thrown.
  145. * @param int $id
  146. * @return Comments the loaded model
  147. * @throws NotFoundHttpException if the model cannot be found
  148. */
  149. protected function findModel($id)
  150. {
  151. if (($model = Comments::findOne(['id' => $id])) !== null) {
  152. return $model;
  153. }
  154. throw new NotFoundHttpException('The requested page does not exist.');
  155. }
  156. public function actionConf()
  157. {
  158. $model = $this->confModel();
  159. if ($this->request->isPost && $post = $this->request->post()){
  160. $post['Conf'] = $this->formNormalizer($post['Conf']);
  161. if( $model->load($post,'Conf') && $model->save()) {
  162. \Yii::$app->cache->delete(CommentsConf::$keysCache);
  163. return $this->redirect('index');
  164. }
  165. }
  166. return $this->render('conf', [
  167. 'model' => $model,
  168. ]);
  169. }
  170. public function formNormalizer($post)
  171. {
  172. $post['AllStop'] = $this->CheckYN(@$post['AllStop']);
  173. $post['reg'] = $this->CheckYN(@$post['reg']);
  174. $post['d0'] = $this->CheckYN(@$post['d0']);
  175. $post['d1'] = $this->CheckYN(@$post['d1']);
  176. $post['d2'] = $this->CheckYN(@$post['d2']);
  177. $post['d3'] = $this->CheckYN(@$post['d3']);
  178. $post['d4'] = $this->CheckYN(@$post['d4']);
  179. $post['d5'] = $this->CheckYN(@$post['d5']);
  180. $post['d6'] = $this->CheckYN(@$post['d6']);
  181. return $post;
  182. }
  183. public function CheckYN($attr)
  184. {
  185. return (isset($attr) && $attr == 'Y')?'Y':'N';
  186. }
  187. protected function confModel()
  188. {
  189. if (($model = CommentsConf::findOne(0)) !== null) {
  190. return $model;
  191. }
  192. throw new NotFoundHttpException('The requested page does not exist.');
  193. }
  194. }