TagController.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. namespace app\modules\admin\controllers;
  3. use app\common\components\PinYin;
  4. use app\common\controllers\BController;
  5. use app\models\Tag;
  6. use Yii;
  7. class TagController extends BController
  8. {
  9. public $layout = 'main';
  10. public function actionList()
  11. {
  12. $query = Tag::find();
  13. if (Yii::$app->request->isAjax) {
  14. $data = [];
  15. $params = Yii::$app->request->get('Tag');
  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,'tag'=>$result->tag,'quote_num'=>$result->quote_num,'url'=>$result->url,'pinyin'=>$result->pinyin,'recommend'=>$result->recommend,'disabled'=>$result->disabled,'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'=>'tag','title'=>Tag::getAttributeName('tag'),'align'=>'center','class'=>'col-md-2'),
  40. array('field'=>'pinyin','title'=>Tag::getAttributeName('pinyin'),'align'=>'center','sortable'=>true,'formatter'=>'editFormatter','class'=>'col-md-1'),
  41. array('field'=>'quote_num','title'=>Tag::getAttributeName('quote_num'),'align'=>'center','sortable'=>true,'formatter'=>'editFormatter','class'=>'col-md-1'),
  42. array('field'=>'recommend','title'=>Tag::getAttributeName('recommend'),'align'=>'center','formatter'=>'switchFormatter','class'=>'col-md-2'),
  43. array('field'=>'disabled','title'=>Tag::getAttributeName('disabled'),'align'=>'center','formatter'=>'switchFormatter','class'=>'col-md-2'),
  44. array('field'=>'create_time','title'=>Tag::getAttributeName('create_time'),'align'=>'center','sortable'=>true,'class'=>'col-md-2'),
  45. array('field'=>'operate','title'=>Tag::getAttributeName('operate'),'align'=>'center','events'=>'window.operateEvents','formatter'=>'operateFormatter','class'=>'col-md-2'),
  46. );
  47. $this->tableConfig = array('table'=>Tag::shortTableName(),'url'=>$this->createRealUrl(['admin/tag/list']),'setFieldUrl'=>$this->createRealUrl(['admin/tag/setfield']),'idField'=>Tag::modelPrimaryKey(),'checkbox'=>1,'dropmenu'=>1,'pagination'=>true,'pagesize'=>20,'refresh'=>true);
  48. return $this->render('list',array('model'=>new Tag()));
  49. }
  50. //添加
  51. function actionAdd()
  52. {
  53. $model = new Tag();
  54. if(Yii::$app->request->isAjax&&$model->load(Yii::$app->request->post())){
  55. $exist = Tag::find()->where("tag='".$model->tag."'")->exists();
  56. if($exist)
  57. {
  58. $msgdata = ['error' => 1,'msg' => '已存在该标签,添加失败'];
  59. echo_json($msgdata);
  60. }
  61. $model->create_time = TIMESTAMP;
  62. if(!$model->validate())
  63. {
  64. $msgdata = ['error' => 1,'msg' => $model->returnFirstError()];
  65. }
  66. else
  67. {
  68. $tagList = explode("\r\n",$model->tag);
  69. if(count($tagList)>1)
  70. {
  71. foreach($tagList as $tag)
  72. {
  73. $exist = Tag::find()->where("tag='".$tag."'")->exists();
  74. if($exist)
  75. {
  76. continue;
  77. }
  78. $newModel = new Tag();
  79. $newModel->tag = $tag;
  80. $newModel->create_time = $model->create_time;
  81. $newModel->url = $model->url;
  82. $newModel->recommend = intval($model->recommend);
  83. $newModel->disabled = intval($model->disabled);
  84. $pinyin = new PinYin();
  85. $newModel->pinyin = $pinyin->encode($newModel->tag);
  86. $newModel->save();
  87. }
  88. $msgdata = ['error' => 0,'msg' => '操作成功'];
  89. }
  90. else
  91. {
  92. $pinyin = new PinYin();
  93. $model->pinyin = $pinyin->encode($model->tag);
  94. if($model->save())
  95. {
  96. $msgdata = ['error' => 0,'msg' => '操作成功'];
  97. }
  98. else
  99. {
  100. $msgdata = ['error' => 1,'msg' => '操作失败'];
  101. }
  102. }
  103. }
  104. echo_json($msgdata);
  105. }
  106. return $this->renderAjax('add',array('model'=>$model));
  107. }
  108. //批量删除
  109. public function actionMultidelete()
  110. {
  111. $ids = Yii::$app->request->get('ids');
  112. if(!empty($ids))
  113. {
  114. Tag::deleteAll("id in(".$ids.")");
  115. $msgdata = ['error' => 0,'msg' => '操作成功'];
  116. }
  117. else
  118. {
  119. $msgdata = ['error' => 1,'msg' => '请选择操作记录'];
  120. }
  121. echo_json($msgdata);
  122. }
  123. function actionEdit()
  124. {
  125. $id = $this->getKeyId();
  126. $model = Tag::findOne($id);
  127. check_record_exists($model);
  128. if(Yii::$app->request->isAjax&&$model->load(Yii::$app->request->post())){
  129. $post = Yii::$app->request->post();
  130. $model->recommend = $post['Tag']['recommend']?$post['Tag']['recommend']:0;
  131. $model->disabled = $post['Tag']['disabled']?$post['Tag']['disabled']:0;
  132. if(!$model->validate())
  133. {
  134. $msgdata = ['error' => 1,'msg' => $model->returnFirstError()];
  135. }
  136. else
  137. {
  138. if(empty($model->pinyin))
  139. {
  140. $pinyin = new PinYin();
  141. $model->pinyin = $pinyin->encode($model->tag);
  142. }
  143. if($model->save())
  144. {
  145. $msgdata = ['error' => 0,'msg' => '操作成功'];
  146. }
  147. else
  148. {
  149. $msgdata = ['error' => 1,'msg' => '操作失败'];
  150. }
  151. }
  152. echo_json($msgdata);
  153. }
  154. return $this->renderAjax('edit',array('model'=>$model));
  155. }
  156. //删除
  157. public function actionDel()
  158. {
  159. $id = $this->getKeyId();
  160. $model = Tag::findOne($id);
  161. check_record_exists($model);
  162. if($model->delete())
  163. {
  164. $msgdata = ['error' => 0,'msg' => '操作成功!'];
  165. }
  166. else
  167. {
  168. $msgdata = ['error' => 1,'msg' => '操作失败!'];
  169. }
  170. echo_json($msgdata);
  171. }
  172. }