<?php

class UserprojectsController extends Controller
{
	
	 public function accessRules() {

        return array(

                array('allow', // allow all users to perform 'index' and 'view' actions

                  'actions' => array('login', 'error', 'logout','newlogin'),

                  'users' => array('*'),

                  ),

                array('allow', // allow authenticated user to perform 'create' and 'update' actions

                  'actions' => array('Index','GetList','Create','Save','Disable'),

                  'expression' => '$user->isMember()',

                  ),

                array('deny', // deny all users

                  'users' => array('*'),

                  ),

                );

    }
    
	public function actionIndex()
	{
        
        $user_id   = (int) Yii::app()->user->id; 
        $modeluser = User::model()->findByPk($user_id);

        if ($modeluser->user_group_id != 1) { 
            $this->redirect(Yii::app()->baseUrl . '/index.php');
            Yii::app()->end();
        }
		$this->render('index');
	}

	public function actionGetList() {
        $model = new UserProjects();
        $draw = $_POST['draw'];
        $offset = $_POST['start'];
        $limit = $_POST['length'];

        $condition = '1="1"';
        
        if (isset($_POST['search']['value']) && $_POST['search']['value'] != "") {
            $searchString = $_POST['search']['value'];
            $condition .= " AND (id LIKE '%" . $searchString . "%' 
            OR project_name LIKE '%" . $searchString . "%')";
        }

        $count = $model->count(array('condition' => $condition));

        $model = $model->findAll(array(
            'condition' => $condition,
            'limit' => $limit,
            'offset' => $offset,
            'order' => 'project_name ASC'
        ));

        $recordsTotal = $count;
        $recordsFiltered = $count;

        $data['draw'] = $draw;
        $data['recordsTotal'] = $recordsTotal;
        $data['recordsFiltered'] = $recordsFiltered;

        $rowData = array();
        $m = $offset;

        foreach ($model as $row) {
            $rowData[] = array(
            ++$m,
            $row['project_name'], 
            $row['users']['first_name'].' '. $row['users']['last_name'],       
            array(                      
                'id' => $row['id'],
                'code' => $row['project_name'],
                'status' => $row->isActive
            )
        );
        }

        $data['data'] = $rowData;

        echo json_encode($data);
    }

    public function actionCreate()
    {
        $model=new UserProjects();
        $id = (int) Yii::app()->request->getParam('id');
        if($id>0){
            $model=$model->findByPk($id);
        }
     Yii::app()->clientScript->scriptMap = array('*jquery.js' => false, '*jquery.min.js' => false,'jquery.min.js' => false, '*jquery-ui.js' => false, '*jquery-ui.min.js' => false , 'jquery-2.1.4.min.js' => false);  
    $this->render('create', array('model' => $model,'from' =>$from), false, true);
    }

    public function actionSave()
    {
    $model = new UserProjects();
        if (isset($_POST['UserProjects']))
        {
        
            if ((int) @$_POST['UserProjects']['id'] > 0)
            { 
                $id= (int) @$_POST['UserProjects']['id'];
                $model=$model->findByPk($id);
                $audit_type = 'UPDATED';
            }else{
                $audit_type = 'CREATED';
            }
    
            $model->attributes=$_POST['UserProjects'];

			if (!empty($model->project_id)) {
        $project = Projects::model()->findByPk($model->project_id);
        $model->project_name = $project ? $project->project_name : null;
    }
			
            if($model->validate())
               
            {    
                 $model->save();
                $jsonData['status'] = 'success';
                $jsonData['frm'] = $_POST['userproject-form'];
                $jsonData['from'] = $_POST['from'];
                $jsonData['id'] = $model->id;
                echo CJSON::encode($jsonData);
            }
            else
            {
                $error = CActiveForm::validate($model, '', false);
                if ($error != '[]')
            echo $error;
            }
        }
    }
    public function actionDisable() {
        $id = Yii::app()->request->getparam('id');
        $status = Yii::app()->request->getparam('status');
        $model = new UserProjects();
        $model = $model->findByPk($id);
        $model->isActive = ($status == "Y" ) ? "N" : "Y";
        $model->save();
        echo json_encode(array("status" => "success"));
    }
}