SearchwordsController.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace app\modules\admin\controllers;
  3. use app\common\controllers\BController;
  4. use app\models\ContentModel;
  5. use app\models\SearchRecord;
  6. use Yii;
  7. class SearchwordsController extends BController
  8. {
  9. public $layout = 'main';
  10. public function actionList()
  11. {
  12. $query = SearchRecord::find();
  13. if (Yii::$app->request->isAjax) {
  14. $data = [];
  15. $params = Yii::$app->request->get('SearchRecord',[]);
  16. $query = mergeParams($query,$params);
  17. $countQuery = clone $query;
  18. //分页
  19. if(isset($_GET['limit'])){ $query->limit(intval($_GET['limit']));}
  20. if(isset($_GET['offset'])){ $query->offset(intval($_GET['offset']));}
  21. //排序
  22. if(isset($_GET['sort'])&&isset($_GET['sortOrder']))
  23. {
  24. $resultList = $query->orderBy([$_GET['sort']=>($_GET['sortOrder']=='asc'?SORT_ASC:SORT_DESC)])->all();
  25. }
  26. else
  27. {
  28. $resultList = $query->orderBy(['id'=>SORT_ASC])->all();
  29. }
  30. foreach($resultList as $result)
  31. {
  32. $data[] = array('id'=>$result->id,'content_model_id'=>$result->content_model_id?ContentModel::modelOptions($result->content_model_id):'','keyword'=>$result->keyword,'result_num'=>$result->result_num,'times'=>$result->times,'recommend'=>$result->recommend,'create_time'=>get_date($result->create_time,'Y-m-d H:i'));
  33. }
  34. $result = ["total"=>$countQuery->count(),"totalNotFiltered"=>$countQuery->count(),"rows"=>$data];
  35. echo_json($result);
  36. }
  37. $this->tableTitle = array(
  38. array('field'=>'id','checkbox'=>false,'formatter'=>'checkboxFormatter','class'=>'col-md-1'),
  39. array('field'=>'content_model_id','title'=>SearchRecord::getAttributeName('content_model_id'),'align'=>'center','class'=>'col-md-2'),
  40. array('field'=>'keyword','title'=>SearchRecord::getAttributeName('keyword'),'align'=>'center','class'=>'col-md-2'),
  41. array('field'=>'result_num','title'=>SearchRecord::getAttributeName('result_num'),'align'=>'center','class'=>'col-md-1'),
  42. array('field'=>'times','title'=>SearchRecord::getAttributeName('times'),'align'=>'center','sortable'=>true,'formatter'=>'editFormatter','class'=>'col-md-1'),
  43. array('field'=>'recommend','title'=>SearchRecord::getAttributeName('recommend'),'align'=>'center','formatter'=>'switchFormatter','class'=>'col-md-1'),
  44. array('field'=>'create_time','title'=>SearchRecord::getAttributeName('create_time'),'align'=>'center','sortable'=>true,'class'=>'col-md-2'),
  45. array('field'=>'operate','title'=>SearchRecord::getAttributeName('operate'),'align'=>'center','events'=>'window.operateEvents','formatter'=>'operateFormatter','class'=>'col-md-2'),
  46. );
  47. $this->tableConfig = array('table'=>SearchRecord::shortTableName(),'url'=>$this->createRealUrl(['admin/searchwords/list']),'setFieldUrl'=>$this->createRealUrl(['admin/searchwords/setfield']),'idField'=>SearchRecord::modelPrimaryKey(),'checkbox'=>1,'dropmenu'=>1,'pagination'=>true,'pagesize'=>20,'refresh'=>true);
  48. return $this->render('list',array('model'=>new SearchRecord()));
  49. }
  50. //添加
  51. function actionAdd()
  52. {
  53. $model = new SearchRecord();
  54. if(Yii::$app->request->isAjax&&$model->load(Yii::$app->request->post())){
  55. $exist = SearchRecord::find()->where("keyword='".$model->keyword."' and content_model_id='".$model->content_model_id."'")->exists();
  56. if($exist)
  57. {
  58. $msgdata = ['error' => 1,'msg' => '已存在相同搜索词,添加失败'];
  59. echo_json($msgdata);
  60. }
  61. $model->create_time = TIMESTAMP;
  62. $model->content_model_id = intval($model->content_model_id);
  63. if(!$model->validate())
  64. {
  65. $msgdata = ['error' => 1,'msg' => $model->returnFirstError()];
  66. }
  67. else
  68. {
  69. if($model->save())
  70. {
  71. $msgdata = ['error' => 0,'msg' => '操作成功'];
  72. }
  73. else
  74. {
  75. $msgdata = ['error' => 1,'msg' => '操作失败'];
  76. }
  77. }
  78. echo_json($msgdata);
  79. }
  80. return $this->renderAjax('add',array('model'=>$model));
  81. }
  82. //批量删除
  83. public function actionMultidelete()
  84. {
  85. $ids = Yii::$app->request->get('ids');
  86. if(!empty($ids))
  87. {
  88. SearchRecord::deleteAll("id in(".$ids.")");
  89. $msgdata = ['error' => 0,'msg' => '操作成功'];
  90. }
  91. else
  92. {
  93. $msgdata = ['error' => 1,'msg' => '请选择操作记录'];
  94. }
  95. echo_json($msgdata);
  96. }
  97. //删除
  98. public function actionDel()
  99. {
  100. $id = $this->getKeyId();
  101. $model = SearchRecord::findOne($id);
  102. check_record_exists($model);
  103. if($model->delete())
  104. {
  105. $msgdata = ['error' => 0,'msg' => '操作成功!'];
  106. }
  107. else
  108. {
  109. $msgdata = ['error' => 1,'msg' => '操作失败!'];
  110. }
  111. echo_json($msgdata);
  112. }
  113. }