CommentController.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace backend\controllers;
  3. use backend\models\AuthArea;
  4. use common\models\Comment;
  5. use yii;
  6. use yii\filters\AccessControl;
  7. /**
  8. * 评论管理
  9. * @package backend\controllers
  10. */
  11. class CommentController extends BaseController
  12. {
  13. public $layout = 'iframe';
  14. const PAGESIZE = 20;//分页数
  15. public function behaviors()
  16. {
  17. return [
  18. 'access' => [
  19. 'class' => AccessControl::className(),
  20. 'rules' => [
  21. [
  22. 'actions' => [],
  23. 'allow' => true,
  24. 'roles' => ['@'],
  25. ],
  26. ],
  27. ],
  28. ];
  29. }
  30. /**
  31. * 列表
  32. * @return string
  33. */
  34. public function actionIndex()
  35. {
  36. $search =['start'=>'','end'=>'','type_id'=>'','name'=>''];
  37. $search['start'] = Yii::$app->request->get('start');
  38. $search['end'] = Yii::$app->request->get('end');
  39. $search['name'] = Yii::$app->request->get('name');
  40. //$models = UserMater::find()->where(['u.role'=>User::USER_ROLE_MATER])->from("{{%user_mater}} um")->leftJoin("{{%user}} u","um.uid = u.id");
  41. $models = Comment::find()->from("{{%comment}} c")->leftJoin("{{%building}} b","c.pid = b.id");
  42. if(!empty($search['start']))
  43. $models->andWhere("c.created_at > :start",[':start'=>strtotime($search['start'])]);
  44. if(!empty($search['end']))
  45. $models->andWhere("c.update_at < :end",[':end'=>strtotime($search['end'])+(3600*24-1)]);
  46. if(!empty($search['name']))
  47. $models->andWhere("b.name like :name ",[':name'=>"%".$search['name']."%"]);
  48. $citys = AuthArea::queryAuth();
  49. if($citys=='all')
  50. $models;
  51. elseif (!empty($citys))
  52. $models->andWhere("b.city in ({$citys})");
  53. else
  54. $models->andWhere("b.city = -1");
  55. $pages = new yii\data\Pagination(["totalCount" => $models->count(), "pageSize" => self::PAGESIZE]);
  56. $models = $models->offset($pages->offset)->limit($pages->limit)->orderBy("c.id DESC")->all();
  57. return $this->render('index', ['models' => $models, 'pages' => $pages,'name'=>'评论管理','search'=>$search]);
  58. }
  59. /**
  60. * 更新状态
  61. * @return string
  62. */
  63. public function actionUpdateStatus()
  64. {
  65. $id = Yii::$app->request->post('id');
  66. $model = Comment::find()->where(['id'=>$id])->one();
  67. if($model->status==Comment::STATUS_ACTIVE)
  68. {
  69. $title = "关闭评论";
  70. $model->status = Comment::STATUS_DELETED;
  71. }
  72. else{
  73. $title = "开启评论";
  74. $model->status = Comment::STATUS_ACTIVE;
  75. }
  76. if($model->save())
  77. $result = ['sign'=>1,'msg'=>$title.'成功'];
  78. else
  79. $result = ['sign'=>0,'msg'=>$title.'失败'];
  80. return json_encode($result);
  81. }
  82. }