<?php

class LocationsController extends Controller 
{
    public function accessRules() {

        return array(

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

                array('allow',
                  'actions' => array('Index','GetList','Create','SaveLocations','Disable'),
                  'expression' => '$user->isMember()',
                  ),

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

                );

    }
    
	public function actionIndex()
	{  
		$this->render('index');
	}

	public function actionGetList() {
        $model = new Locations();
        $draw = $_POST['draw'];
        $offset = (int) $_POST['start'];
        $limit  = (int) $_POST['length'];
        $condition = '1=1';
        
        if (isset($_POST['search']['value']) && $_POST['search']['value'] != "") {
            $searchString = $_POST['search']['value'];
            $condition .= " AND (id LIKE '%" . $searchString . "%' 
            OR location_name LIKE '%" . $searchString . "%')";
        }

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

        $model = $model->findAll(array(
            'condition' => $condition,
            'limit' => $limit,
            'offset' => $offset,
            'order' => 'location_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['location_name'],          
            array(                      
                'id' => $row['id'],
                'code' => $row['location_name'],
                'status' => $row->isActive
            )
        );
        }

        $data['data'] = $rowData;

        echo json_encode($data);
    }

    public function actionCreate()
    {
        $model = new Locations();
        $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);  
        $from = Yii::app()->request->getParam('from', '');
        $this->render('create', array('model' => $model, 'from' => $from), false, true);
    }

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

        $model->attributes = $_POST['Locations'];

        if ($audit_type === 'UPDATED') { 
            $model->updated_by = Yii::app()->user->id;
            $model->updated_datetime = date('Y-m-d H:i:s');
        } else {
            $model->created_by = Yii::app()->user->id;
            $model->created_datetime = date('Y-m-d H:i:s');
        }
            
        if ($model->validate()) {
            $model->save();
            $jsonData['status'] = 'success';
            $jsonData['frm'] = $_POST['Locations-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 Locations();
        $model = $model->findByPk($id);
        
        if ($model === null) {
            echo json_encode(array('status' => 'fail'));
            return;
        }
        
        $model->isActive = ($status == 'Y') ? 'N' : 'Y';
        $model->save();

        echo json_encode(array('status' => 'success'));
    }
 
}