<?php
/**
 * Created by PhpStorm.
 * User: Sam
 * Date: 2017/1/4
 * Time: 下午9:12
 */

namespace backend\controllers;


<<<<<<< .mine
use app\models\SmsAdmin;
use Yii;
use app\models\Teacher;
use app\models\VideoMaking;
use app\models\Project;
use app\models\ProjectSearch;
use yii\data\SqlDataProvider;
||||||| .r93
=======
use app\models\Lecture;
use app\models\SmsAdmin;
use Yii;
use yii\data\SqlDataProvider;
>>>>>>> .r101
use yii\web\Controller;
<<<<<<< .mine
use yii\grid\GridView;
||||||| .r93
=======
use yii\grid\GridView;
use yii\data\ActiveDataProvider;
>>>>>>> .r101

class LecturereleaseController extends Controller
{
    public function beforeAction($action)
    {
        if (empty(Yii::$app->user->identity)) {
            return $this->redirect(['/default/login']);
        }
        return parent::beforeAction($action); // TODO: Change the autogenerated stub
    }

    public function init()
    {
        parent::init();
<<<<<<< .mine
//        var_dump(Yii::$app->user->identity);exit;
//        if (empty(Yii::$app->user->identity)){
//            return $this->redirect(['/default/login']);
//        }
//        $pro_info = new Project();
//        $this->pro_projectname = $pro_info->getProjectName();
//        $this->pro_school = $pro_info->getSchoolName();
//        $this->pro_teacher = $pro_info->getTeacherName();
//        $this->pro_over = $pro_info->getOverList();

        $course_info = new VideoMaking();
        $pro_info = new Project();
        $person = new SmsAdmin();
        $teacher = new Teacher();
        $this->pro_projectname = $pro_info->getProjectName();
        $this->pro_school = $pro_info->getSchoolName();
        $this->course_list = $course_info->getCourseList();
        $this->teacher_list = $teacher->getTeacherList();
        $this->person_list = $person->getPersonList();
||||||| .r93
//        var_dump(Yii::$app->user->identity);exit;
//        if (empty(Yii::$app->user->identity)){
//            return $this->redirect(['/default/login']);
//        }
//        $pro_info = new Project();
//        $this->pro_projectname = $pro_info->getProjectName();
//        $this->pro_school = $pro_info->getSchoolName();
//        $this->pro_teacher = $pro_info->getTeacherName();
//        $this->pro_over = $pro_info->getOverList();

        $search_info = new VideoShoot();
        $course_info = new VideoMaking();
        $pro_info = new Project();
        $person = new SmsAdmin();
        $teacher = new Teacher();
        $this->pro_projectname = $pro_info->getProjectName();
        $this->pro_school = $pro_info->getSchoolName();
        $this->course_list = $course_info->getCourseList();
        $this->teacher_list = $teacher->getTeacherList();
        $this->person_list = $person->getPersonList();
=======
>>>>>>> .r101
    }


    public function actionIndex()
    {
        $this->layout = 'main';
        // $searchModel = new ProjectSearch();
        $query = Yii::$app->request->queryParams;
        $sql_parms = '';
        $lecture = new Lecture();
        if (!empty($query['Lecture'])) {
            $query_parms = array_filter($query['Lecture']);
            $sql_parms = 'where true';
        }

        if (isset($query_parms['title'])) {
            $sql_parms .= " and id = '" . $query_parms['title'] . "'";
        }

        $sql = "select id,title,content,speaker,datetime,address from lecture " . $sql_parms . " order by id desc";

        $command = Yii::$app->db->createCommand('SELECT COUNT(*) FROM lecture ' . $sql_parms);
        $count = $command->queryScalar();

        $dataProvider = new SqlDataProvider([
            'sql' => $sql,
            'totalCount' => $count,
            'pagination' => [
                'pageSize' => 15,
            ],
            'sort' => [
                'attributes' => [
                    'id',
                ],
            ],
        ]);

        GridView::widget([
            'dataProvider' => $dataProvider,
        ]);

        return $this->render('index', [
            'searchModel' => $lecture,
            'dataProvider' => $dataProvider,
        ]);
    }

    public function actionCreate()
    {
        $this->layout = 'main';
        $model = new Lecture();

        if (!empty(Yii::$app->request->post())) {
            $params = Yii::$app->request->post();
//            var_dump($params['Lecture']['title']);exit;
            $model->setAttributes([
                'title ' => $params['Lecture']['content'],
                'content' => $params['Lecture']['title'],
                'speaker' => $params['Lecture']['speaker'],
                'datetime' => intval(strtotime($params['Lecture']['datetime'])),
                'address' => $params['Lecture']['address'],
            ]);
        }

        if (!empty(Yii::$app->request->post()) && $model->save()) {
            Yii::$app->cache->delete('index');
            return $this->redirect(['index']);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    public function actionEdit()
    {
        $id = Yii::$app->request->get('id');
        $model = Lecture::findOne($id);
        if(empty($model['datetime'])) { $model['datetime'] = null; }
        $model['datetime'] = date('Y-m-d', $model['datetime']);

        if (!empty(Yii::$app->request->post())) {
            $params = Yii::$app->request->post();
            $model->setAttributes([
                'title ' => $params['Lecture']['title'],
                'content' => $params['Lecture']['content'],
                'speaker' => $params['Lecture']['speaker'],
                'datetime' => intval(strtotime($params['Lecture']['datetime'])),
                'address' => $params['Lecture']['address'],
            ]);
        }

        if (!empty(Yii::$app->request->post()) && $model->save()) {
            Yii::$app->cache->delete('index');
            return $this->redirect(['index']);
        } else {
            return $this->render('edit', [
                'model' => $model,
            ]);
        }
    }

    public function actionDelete()
    {
        $query = Yii::$app->request->queryParams;
        $id = $query['id'];
        $data = Lecture::findOne($id);
        $data->delete();

        Yii::$app->cache->delete('index');
        return $this->redirect(['index']);
    }

}






