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

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

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

    /**
     * 直播间
     * @return type
     */
    public function actionRoom()
    {
        $model = \app\models\Room::find();
        
        if ($filter = Yii::$app->getRequest()->getQueryParam('filter')){
            foreach ($filter as $field=>$value){
                if ($field == 'keyword'){
                    if (!empty($value)){
                        $model->andWhere(['like','title',"%{$value}%"])
                        ->orWhere(['room_id'=>$value])
                        ->orWhere(['master_id'=>$value]);
                    }
                }else{
                    $model->andWhere([$field=>$value]);
                }
            }
        }
        
        $count = clone $model;
        $pagination = new Pagination(['totalCount' =>$count->count()]);
        
        $list = \app\models\Room::find()->asArray()->offset($pagination->offset)->limit($pagination->limit)->orderBy(['create_time'=>SORT_DESC])->all();
        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');
        }
        
        return $this->render('roomedit',[
            'model'=> $model
        ]);
    }
    
    /**
     * 编辑直播间
     * @return type
     */
    public function actionRoomedit()
    {
        if (!$id = Yii::$app->getRequest()->getQueryParam('id')){
            $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']);
            $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');
        }
        
        return $this->render('roomedit',[
            'model'=> $model
        ]);
    }
    
    /**
     * 直播间操作
     * @return type
     */
    public function actionRoomoperate()
    {
        ;
    }
}