<?php

class ProjectsController 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 Projects();
        $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 . "%' 
            OR aftp_addres 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['ftp_address'],  
			 $row['ftp_username'],  
			  $row['ftp_port'],        
            array(                      
                'id' => $row['id'],
                'code' => $row['project_name'],
                'status' => $row->isActive
            )
        );
        }

        $data['data'] = $rowData;

        echo json_encode($data);
    }

    public function actionCreate()
    {
        $model=new Projects();
        $id = (int) Yii::app()->request->getParam('id');
        if($id>0){
            $model=$model->findByPk($id);
            
		if (!empty($model->assigned_user_id)) {
            $model->assigned_user_id = explode(',', $model->assigned_user_id);
        } else {
            $model->assigned_user_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 Projects();
        if (isset($_POST['Projects']))
        {
        
            if ((int) @$_POST['Projects']['id'] > 0)
            { 
                $id= (int) @$_POST['Projects']['id'];
                $model=$model->findByPk($id);
                $audit_type = 'UPDATED';
            }else{
                $audit_type = 'CREATED';
            }
    
            $model->attributes=$_POST['Projects'];
			$model->github_repo=$_POST['Projects']['github_repo'];
			$model->assigned_user_id = !empty($_POST['Projects']['assigned_user_id']) && is_array($_POST['Projects']['assigned_user_id'])
    ? implode(',', $_POST['Projects']['assigned_user_id'])
    : null;

            if($model->validate())
               
            {    
                 $model->save();
                $jsonData['status'] = 'success';
                $jsonData['frm'] = $_POST['project-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 Projects();
        $model = $model->findByPk($id);
        $model->isActive = ($status == "Y" ) ? "N" : "Y";
        $model->save();
        echo json_encode(array("status" => "success"));
    }
 
}