LiveController.php 7.41 KB
<?php
namespace backend\controllers;

use Yii;
use app\models\Master;
use backend\components\Pagination;
use backend\widgets\Filter;

/**
 * Live controller
 */
class LiveController extends BaseController
{

    /**
     * 直播间
     * @return type
     */
    public function actionRoom()
    {
        
        $filter = Filter::self()->liveStatus()->keyword()->submit()->reset();
        if ($condition = $filter->get_where(true)){
            $model = \app\models\Room::findByCondition($condition);
        }else{
            $model = \app\models\Room::find();
        }
        //var_dump($condition);
        
        $count = clone $model;
        $pagination = new Pagination(['totalCount' =>$count->count()]);
        $list = $model->with('master')->offset($pagination->offset)->limit($pagination->limit)->orderBy(['create_time'=>SORT_DESC])->all();
        
        //var_dump($list);
        
        return $this->render('room',[
            'filter'=>$filter,
            'pagination'=>$pagination,
            'list'=>$list,
        ]);
    }
    
    /**
     * 主播
     * @return type
     */
    public function actionMaster()
    {
        $query = Master::find();
        $conditions = Yii::$app->request->get();

        if(array_filter($conditions)){
            $sql ="name like '%".$conditions['name']."%'";
        }else{
            $sql = "";
        }
        $pagination = new Pagination([
            'defaultPageSize' => 15,
            'totalCount' => $query->count(),
        ]);
        $masters = $query->where($sql)
            ->orderBy('create_time')
            ->offset($pagination->offset)
            ->limit($pagination->limit)
            ->all();
        return $this->render('master',[
            'list'=>$masters,
            'condition'=>$conditions
        ]);
    }
    
    /**
     * 视频
     * @return type
     */
    public function actionVideo()
    {
        return $this->render('video');
    }

    /**
     * 添加主播
     * @return type
     */
    public function actionAddmaster()
    {
        $model = new Master();

        if ($posts = Yii::$app->request->post()) {
            $model->name = $posts['name'];
            $model->meta = $posts['meta'];
            $model->pic = $posts['pic'];
            $model->create_time = $model->update_time = time();
            if($model->save()){
                Yii::$app->session->setFlash('success', '保存成功。');
            }else{
                Yii::$app->session->setFlash('error', '保存失败。');
            }
            return $this->redirect(['master']);
        } else {
            return $this->render('add_master',['action'=>'/live/addmaster']);
        }
    }

    public function actionEditmaster()
    {

        $id = Yii::$app->request->get('id',0);
        $master_id = Yii::$app->request->post('master_id',0);

        if($id || $master_id){
            if($posts = Yii::$app->request->post()){
                $model = Master::findOne($posts['master_id']);
                $model->name = $posts['name'];
                $model->meta = $posts['meta'];
                $model->pic = $posts['pic'];
                if($model->save()){
                    //return $this->redirect(['editmaster','id' => $model->master_id]);
                    Yii::$app->session->setFlash('success', '编辑成功。');
                }else{
                    Yii::$app->session->setFlash('error', '编辑失败。');
                }
                return $this->refresh();
            }else{
                $row = Master::find()->where(['master_id'=>$id])->one();
                return $this->render('add_master',[
                    'action'=>'/live/editmaster',
                    'opt'=>'update',
                    'row'=>$row
                ]);
            }
        }
    }

    public function actionDelmaster(){
        $id = Yii::$app->request->get('id',0);
        if($id){
            $model = Master::findOne($id);
            if($model->delete()){
                return $this->redirect(['master']);
            }else{
                Yii::$app->session->setFlash('error', '删除失败。');
            }
        }
    }
    /**
     * 创建直播间
     * @return type
     */
    public function actionRoomcreate()
    {
        $this->setParam('uploader', '');
        
        $model = new \app\models\Room;
        if (Yii::$app->getRequest()->isPost){
            $post = Yii::$app->getRequest()->post();
            $post['starting_time'] = !$post['starting_time'] ? : strtotime($post['starting_time']);
            $model->setAttributes($post);
            if ($model->save()){
                Yii::$app->session->setFlash('success', '创建成功。');
            }else{
                Yii::$app->session->setFlash('error', '创建失败。');
            }
            
            return $this->redirect('/live/room');
        }
        
        //所有主播
        $masters = \app\models\Master::getId2name();
        $masters = [''=>'0 无主播']+$list;
        
        return $this->render('roomedit',[
            'model'=> $model,
            'masters'=>$masters,
        ]);
    }
    
    /**
     * 编辑直播间
     * @return type
     */
    public function actionRoomedit()
    {
        if (!$id = Yii::$app->getRequest()->getQueryParam('id')){
            return $this->redirect($this->_refer);
        }
        $this->setParam('uploader', '');
        
        $model = new \app\models\Room;
        $model = $model->findOne($id) ? : $model;
        if (Yii::$app->getRequest()->isPost){
            $post = Yii::$app->getRequest()->post();
            $post['starting_time'] = $post['starting_time'] ? strtotime($post['starting_time']) : 0;
            $model->setAttributes($post);
            Yii::$app->session->setFlash('success', '编辑成功。');
            if ($model->save()){
                Yii::$app->session->setFlash('success', '编辑成功。');
            }else{
                Yii::$app->session->setFlash('error', '编辑失败。');
            }
            
            return $this->redirect('/live/room');
        }
        
        //所有主播
        $masters = \app\models\Master::getId2name();
        $masters = [''=>'0 无主播']+$list;
        
        return $this->render('roomedit',[
            'model'=> $model,
            'masters'=>$masters,
        ]);
    }
    
    /**
     * 直播间操作
     * @return type
     */
    public function actionRoomoperate()
    {
        if (!$id = Yii::$app->getRequest()->getQueryParam('id')){
            return $this->redirect($this->_refer);
        }
        if (!$type = Yii::$app->getRequest()->getQueryParam('type')){
            return $this->redirect($this->_refer);
        }
        
        $model = new \app\models\Room;
        if (!$model = $model->findOne($id)){
            Yii::$app->session->setFlash('error', '找不到该直播间');
            return $this->redirect($this->_refer);
        }
        
        switch ($type){
            case 'del'://删除直播间
                $r = $model->delete();
                break;
            case 'close'://关闭直播间
                $model->status = 0;
                $r = $model->update();
                break;
            default:
                break;
        }
        if (!empty($r)){
            Yii::$app->session->setFlash('success', '操作成功');
        }else{
            Yii::$app->session->setFlash('error', '操作失败');
        }
        
        return $this->redirect($this->_refer);
    }
}