FriendlinkController.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace app\modules\admin\controllers;
  3. use app\common\controllers\BController;
  4. use app\models\FriendLink;
  5. use app\modules\admin\models\Attachment;
  6. use Yii;
  7. class FriendlinkController extends BController
  8. {
  9. public $layout = 'main';
  10. public function actionList()
  11. {
  12. $query = FriendLink::find();
  13. if (Yii::$app->request->isAjax) {
  14. $data = [];
  15. $params = Yii::$app->request->get('FriendLink');
  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,'title'=>$result->title,'url'=>$result->url,'logo'=>$result->logo,'is_image'=>$result->is_image,'type'=>$result->type,'list_order'=>$result->list_order,'disabled'=>$result->disabled);
  33. }
  34. $result = ["total"=>$countQuery->count(),"totalNotFiltered"=>$countQuery->count(),"rows"=>$data];
  35. echo_json($result);
  36. }
  37. $this->tableTitle = array(
  38. array('field'=>'id','title'=>FriendLink::getAttributeName('id'),'align'=>'center','sortable'=>true,'class'=>'col-md-1'),
  39. array('field'=>'title','title'=>FriendLink::getAttributeName('title'),'align'=>'center','class'=>'col-md-2'),
  40. array('field'=>'url','title'=>FriendLink::getAttributeName('url'),'align'=>'center','sortable'=>true,'formatter'=>'editFormatter','class'=>'col-md-2'),
  41. array('field'=>'is_image','title'=>FriendLink::getAttributeName('is_image'),'align'=>'center','formatter'=>'switchFormatter','class'=>'col-md-1'),
  42. array('field'=>'list_order','title'=>FriendLink::getAttributeName('list_order'),'align'=>'center','sortable'=>true,'formatter'=>'editFormatter','class'=>'col-md-2'),
  43. array('field'=>'disabled','title'=>FriendLink::getAttributeName('disabled'),'align'=>'center','formatter'=>'switchFormatter','class'=>'col-md-2'),
  44. array('field'=>'operate','title'=>FriendLink::getAttributeName('operate'),'align'=>'center','events'=>'window.operateEvents','formatter'=>'operateFormatter','class'=>'col-md-2'),
  45. );
  46. $this->tableConfig = array('table'=>FriendLink::shortTableName(),'url'=>$this->createRealUrl(['admin/friendlink/list']),'setFieldUrl'=>$this->createRealUrl(['admin/friendlink/setfield']),'idField'=>FriendLink::modelPrimaryKey(),'checkbox'=>0,'dropmenu'=>1,'pagination'=>true,'pagesize'=>20,'refresh'=>true);
  47. return $this->render('list',array('model'=>new FriendLink()));
  48. }
  49. //添加
  50. function actionAdd()
  51. {
  52. $model = new FriendLink();
  53. if(Yii::$app->request->isAjax&&$model->load(Yii::$app->request->post())){
  54. if(!$model->validate())
  55. {
  56. $msgdata = ['error' => 1,'msg' => $model->returnFirstError()];
  57. }
  58. else
  59. {
  60. if($model->save())
  61. {
  62. Attachment::relateAttachmentByTable($model->logo,FriendLink::shortTableName(),'logo',$model->id);
  63. $msgdata = ['error' => 0,'msg' => '操作成功'];
  64. }
  65. else
  66. {
  67. $msgdata = ['error' => 1,'msg' => '操作失败'];
  68. }
  69. }
  70. echo_json($msgdata);
  71. }
  72. return $this->renderAjax('add',array('model'=>$model));
  73. }
  74. function actionEdit()
  75. {
  76. $id = $this->getKeyId();
  77. $model = FriendLink::findOne($id);
  78. check_record_exists($model);
  79. if(Yii::$app->request->isAjax&&$model->load(Yii::$app->request->post())){
  80. $post = Yii::$app->request->post();
  81. $model->is_image = $post['FriendLink']['is_image']?$post['FriendLink']['is_image']:0;
  82. $model->disabled = $post['FriendLink']['disabled']?$post['FriendLink']['disabled']:0;
  83. if(!$model->validate())
  84. {
  85. $msgdata = ['error' => 1,'msg' => $model->returnFirstError()];
  86. }
  87. else
  88. {
  89. if($model->save())
  90. {
  91. Attachment::relateAttachmentByTable($model->logo,FriendLink::shortTableName(),'logo',$model->id);
  92. $msgdata = ['error' => 0,'msg' => '操作成功'];
  93. }
  94. else
  95. {
  96. $msgdata = ['error' => 1,'msg' => '操作失败'];
  97. }
  98. }
  99. echo_json($msgdata);
  100. }
  101. return $this->renderAjax('add',array('model'=>$model));
  102. }
  103. //删除
  104. public function actionDel()
  105. {
  106. $id = $this->getKeyId();
  107. $model = FriendLink::findOne($id);
  108. check_record_exists($model);
  109. if($model->delete())
  110. {
  111. $msgdata = ['error' => 0,'msg' => '操作成功!'];
  112. }
  113. else
  114. {
  115. $msgdata = ['error' => 1,'msg' => '操作失败!'];
  116. }
  117. echo_json($msgdata);
  118. }
  119. }