CommentsController.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 actionFilter($id)
  61. {
  62. $model = $this->findModel($id);
  63. return $this->redirect(array('/manager/comments-filter/create', 'uid1'=>$model->ip_address, 'uid2'=>md5($model->user_agent), 'ntext'=>$model->fakename));
  64. }
  65. /**
  66. * Displays a single Comments model.
  67. * @param int $id
  68. * @return string
  69. * @throws NotFoundHttpException if the model cannot be found
  70. */
  71. public function actionView($id)
  72. {
  73. return $this->render('view', [
  74. 'model' => $this->findModel($id),
  75. ]);
  76. }
  77. /**
  78. * Creates a new Comments model.
  79. * If creation is successful, the browser will be redirected to the 'view' page.
  80. * @return string|\yii\web\Response
  81. */
  82. public function actionCreate()
  83. {
  84. $model = new Comments();
  85. if ($this->request->isPost) {
  86. if ($model->load($this->request->post()) && $model->save()) {
  87. return $this->redirect(['view', 'id' => $model->id]);
  88. }
  89. } else {
  90. $model->loadDefaultValues();
  91. }
  92. return $this->render('create', [
  93. 'model' => $model,
  94. ]);
  95. }
  96. /**
  97. * Updates an existing Comments model.
  98. * If update is successful, the browser will be redirected to the 'view' page.
  99. * @param int $id
  100. * @return string|\yii\web\Response
  101. * @throws NotFoundHttpException if the model cannot be found
  102. */
  103. public function actionUpdate($id)
  104. {
  105. $model = $this->findModel($id);
  106. if (isset($_POST['Comments'])) {
  107. $model->message = $_POST['Comments']['message'];
  108. $model->visible = $_POST['Comments']['visible'];
  109. if ($model->save()) {
  110. return $this->redirect(['view', 'id' => $model->id]);
  111. }
  112. }
  113. return $this->render('update', [
  114. 'model' => $model,
  115. ]);
  116. }
  117. /**
  118. * Deletes an existing Comments model.
  119. * If deletion is successful, the browser will be redirected to the 'index' page.
  120. * @param int $id
  121. * @return \yii\web\Response
  122. * @throws NotFoundHttpException if the model cannot be found
  123. */
  124. public function actionDelete($id)
  125. {
  126. $this->findModel($id)->delete();
  127. return $this->redirect(['index']);
  128. }
  129. /**
  130. * @param $id
  131. * @return string
  132. * @throws NotFoundHttpException
  133. */
  134. public function actionToggle($id,$value)
  135. {
  136. $model = $this->findModel($id);
  137. $model->updateAttributes(['visible'=>$value]);
  138. return $this->actionIndex();
  139. }
  140. public function actionAjaxtoggle()
  141. {
  142. $ret = ['stat'=>'err'];
  143. if( $this->request->isPost && $this->request->post('id') && $this->request->post('value') ){
  144. $id = $this->request->post('id');
  145. $value = $this->request->post('value');
  146. $model = $this->findModel($id);
  147. if( $model->updateAttributes(['visible'=>$value]) ){
  148. $ret = ['stat'=>'ok', 'id'=>$id, 'value'=>$value];
  149. }
  150. }
  151. return \yii\helpers\Json::encode($ret);;
  152. }
  153. /**
  154. * Finds the Comments model based on its primary key value.
  155. * If the model is not found, a 404 HTTP exception will be thrown.
  156. * @param int $id
  157. * @return Comments the loaded model
  158. * @throws NotFoundHttpException if the model cannot be found
  159. */
  160. protected function findModel($id)
  161. {
  162. if (($model = Comments::findOne(['id' => $id])) !== null) {
  163. return $model;
  164. }
  165. throw new NotFoundHttpException('The requested page does not exist.');
  166. }
  167. public function actionConf()
  168. {
  169. $model = $this->confModel();
  170. if ($this->request->isPost && $post = $this->request->post()){
  171. $post['Conf'] = $this->formNormalizer($post['Conf']);
  172. if( $model->load($post,'Conf') && $model->save()) {
  173. \Yii::$app->cache->delete(CommentsConf::$keysCache);
  174. return $this->redirect('index');
  175. }
  176. }
  177. return $this->render('conf', [
  178. 'model' => $model,
  179. ]);
  180. }
  181. public function formNormalizer($post)
  182. {
  183. $post['AllStop'] = $this->CheckYN(@$post['AllStop']);
  184. $post['reg'] = $this->CheckYN(@$post['reg']);
  185. $post['d0'] = $this->CheckYN(@$post['d0']);
  186. $post['d1'] = $this->CheckYN(@$post['d1']);
  187. $post['d2'] = $this->CheckYN(@$post['d2']);
  188. $post['d3'] = $this->CheckYN(@$post['d3']);
  189. $post['d4'] = $this->CheckYN(@$post['d4']);
  190. $post['d5'] = $this->CheckYN(@$post['d5']);
  191. $post['d6'] = $this->CheckYN(@$post['d6']);
  192. return $post;
  193. }
  194. public function CheckYN($attr)
  195. {
  196. return (isset($attr) && $attr == 'Y')?'Y':'N';
  197. }
  198. protected function confModel()
  199. {
  200. if (($model = CommentsConf::findOne(0)) !== null) {
  201. return $model;
  202. }
  203. throw new NotFoundHttpException('The requested page does not exist.');
  204. }
  205. }