123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- <?php
- namespace app\modules\admin\controllers;
- use app\common\controllers\BController;
- use app\models\FriendLink;
- use app\modules\admin\models\Attachment;
- use Yii;
- class FriendlinkController extends BController
- {
- public $layout = 'main';
- public function actionList()
- {
- $query = FriendLink::find();
- if (Yii::$app->request->isAjax) {
- $data = [];
- $params = Yii::$app->request->get('FriendLink');
- $query = mergeParams($query,$params);
- $countQuery = clone $query;
- //分页
- if(isset($_GET['limit'])){ $query->limit(intval($_GET['limit']));}
- if(isset($_GET['offset'])){ $query->offset(intval($_GET['offset']));}
- //排序
- if(isset($_GET['sort'])&&isset($_GET['sortOrder']))
- {
- $resultList = $query->orderBy([$_GET['sort']=>($_GET['sortOrder']=='asc'?SORT_ASC:SORT_DESC)])->all();
- }
- else
- {
- $resultList = $query->orderBy(['id'=>SORT_ASC])->all();
- }
- foreach($resultList as $result)
- {
- $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);
- }
- $result = ["total"=>$countQuery->count(),"totalNotFiltered"=>$countQuery->count(),"rows"=>$data];
- echo_json($result);
- }
- $this->tableTitle = array(
- array('field'=>'id','title'=>FriendLink::getAttributeName('id'),'align'=>'center','sortable'=>true,'class'=>'col-md-1'),
- array('field'=>'title','title'=>FriendLink::getAttributeName('title'),'align'=>'center','class'=>'col-md-2'),
- array('field'=>'url','title'=>FriendLink::getAttributeName('url'),'align'=>'center','sortable'=>true,'formatter'=>'editFormatter','class'=>'col-md-2'),
- array('field'=>'is_image','title'=>FriendLink::getAttributeName('is_image'),'align'=>'center','formatter'=>'switchFormatter','class'=>'col-md-1'),
- array('field'=>'list_order','title'=>FriendLink::getAttributeName('list_order'),'align'=>'center','sortable'=>true,'formatter'=>'editFormatter','class'=>'col-md-2'),
- array('field'=>'disabled','title'=>FriendLink::getAttributeName('disabled'),'align'=>'center','formatter'=>'switchFormatter','class'=>'col-md-2'),
- array('field'=>'operate','title'=>FriendLink::getAttributeName('operate'),'align'=>'center','events'=>'window.operateEvents','formatter'=>'operateFormatter','class'=>'col-md-2'),
- );
- $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);
- return $this->render('list',array('model'=>new FriendLink()));
- }
- //添加
- function actionAdd()
- {
- $model = new FriendLink();
- if(Yii::$app->request->isAjax&&$model->load(Yii::$app->request->post())){
- if(!$model->validate())
- {
- $msgdata = ['error' => 1,'msg' => $model->returnFirstError()];
- }
- else
- {
- if($model->save())
- {
- Attachment::relateAttachmentByTable($model->logo,FriendLink::shortTableName(),'logo',$model->id);
- $msgdata = ['error' => 0,'msg' => '操作成功'];
- }
- else
- {
- $msgdata = ['error' => 1,'msg' => '操作失败'];
- }
- }
- echo_json($msgdata);
- }
- return $this->renderAjax('add',array('model'=>$model));
- }
- function actionEdit()
- {
- $id = $this->getKeyId();
- $model = FriendLink::findOne($id);
- check_record_exists($model);
- if(Yii::$app->request->isAjax&&$model->load(Yii::$app->request->post())){
- $post = Yii::$app->request->post();
- $model->is_image = $post['FriendLink']['is_image']?$post['FriendLink']['is_image']:0;
- $model->disabled = $post['FriendLink']['disabled']?$post['FriendLink']['disabled']:0;
- if(!$model->validate())
- {
- $msgdata = ['error' => 1,'msg' => $model->returnFirstError()];
- }
- else
- {
- if($model->save())
- {
- Attachment::relateAttachmentByTable($model->logo,FriendLink::shortTableName(),'logo',$model->id);
- $msgdata = ['error' => 0,'msg' => '操作成功'];
- }
- else
- {
- $msgdata = ['error' => 1,'msg' => '操作失败'];
- }
- }
- echo_json($msgdata);
- }
- return $this->renderAjax('add',array('model'=>$model));
- }
- //删除
- public function actionDel()
- {
- $id = $this->getKeyId();
- $model = FriendLink::findOne($id);
- check_record_exists($model);
- if($model->delete())
- {
- $msgdata = ['error' => 0,'msg' => '操作成功!'];
- }
- else
- {
- $msgdata = ['error' => 1,'msg' => '操作失败!'];
- }
- echo_json($msgdata);
- }
- }
|