<?php

class UserportalController extends Controller
{
	public function accessRules() {
		return array(
			array('allow', // allow all users to perform 'index' and 'view' actions
			  'actions' => array('login', 'error', 'logout'),
			  'users' => array('*'),
			  ),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
			  'actions' => array('Index','Create','GetList','Save','Delete', 'UnBlockUserPortal','BlockUserPortal'),
			  'expression' => '$user->isMember()',
			  ),
			array('deny', // deny all users
			  'users' => array('*'),
			  ),
			);
	}
public function actionIndex()
{
	$this->render('index');
}



public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}
	public function actionGetList()
	{
			$model=new UserPortal();
			$draw    = $_POST['draw'];
			$offset = $_POST['start'];
			$limit  = $_POST['length'];
			$columns = array(
			   0 =>'t.id',
			   1 =>'user_details.first_name',
			   2 =>'portal_details.portal_name',
			   3 =>'t.id'
			   );
			$filterColumn = $columns[$_POST['order'][0]['column']];
			$filterOrder  = $_POST['order'][0]['dir'];
			$orderBy   = $filterColumn." ".$filterOrder;
			$condition = '1=1';
			if(isset($_POST['search']['value']) && $_POST['search']['value']!="")
			{
			  $searchString = $_POST['search']['value'];
			 $condition ="  (portal_details.portal_name LIKE '%".$searchString."%' OR user_details.first_name  LIKE '%".$searchString."%'
			 					 OR user_details.last_name  LIKE '%".$searchString."%')";
			}
			if(isset($_POST['user_id']) && $_POST['user_id']!="")
			{
				$user_id  = (int)$_POST['user_id'];
				$condition .= ' AND user_id='.$user_id;
			}
			if(isset($_POST['portal_id']) && $_POST['portal_id']!="")
			{
				$portal_id  = (int)$_POST['portal_id'];
				$condition .= ' AND portal_id='.$portal_id;
			}
			$count= $model->with('portal_details','user_details')->count(array('condition'=>$condition));
			$model= $model->with('portal_details','user_details')->findAll(array('condition'=>$condition,'limit'=>$limit,'offset'=>$offset,'order'=>$orderBy));
			$recordsTotal = $count;
			$recordsFiltered = $count;
			$data['draw'] = $draw;
			$data['recordsTotal'] = $recordsTotal;
			$data['recordsFiltered'] = $recordsFiltered;
			$rowData=array();
			$m = 1;
			
			foreach ($model as $row)
			{
				$div = '';
				//if(Yii::app()->user->authorised_user == 1 || ((int) Yii::app()->user->havePermissionOn["mas_inv_rack_edit"])){
					$div .= '<button class="btn btn-primary btn-xs" onclick="loadPopupModal(\'Userportal/create&id='.$row['id'].'\',\'Edit User Portal\',\'lg\')"><i class="fa fa-pencil"></i></button>';
			  // }
			   // if(Yii::app()->user->authorised_user == 1 || ((int) Yii::app()->user->havePermissionOn["mas_inv_rack_delete"])){
			   //     $div .=  '<a href ="#" class="btn btn-danger btn-xs" onclick="deletePortal('.$row['id'].')"><i class="fa fa-trash-o "></i></a>';
			 //   }

			 //if(isset(Yii::app()->user->havePermissionOn['mas_Portal_disable']) ||  Yii::app()->user->authorised_user == 1)
			//{

			  if ($row->isActive == "Y") 
			  {
				$div .= ' <a href ="javascript:void(0);" class="btn btn-success btn-xs" onclick="disableUserportal(' . $row['id'] . ')"><i class="fa fa-toggle-on"></i></a>';
			  }
			  if ($row->isActive == 'N') 
			  {
					$div .= ' <a href ="javascript:void(0);" class="btn btn-danger btn-xs" onclick="enableUserportal(' . $row['id'] . ')"><i class="fa fa-ban"></i></a>';
			  }            
			
			//}
				   $rowData[]=array($m,
				   $row->user_details->Fullname,
					   $row->portal_details->portal_name,
					   '<div class="btn-wrap">'.$div.'</div>',
					   $row['isActive'],
					  );
			
				$m++;
				
			}

			$data['data'] = $rowData;
			echo json_encode($data);
	}


	public function actionCreate()
	{
		$model=new UserPortal();
		$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);
		$this->render('create', array('model' => $model), false, false);
	}


public function actionSave()
{
	
		$model = new UserPortal();
		if (isset($_POST['UserPortal']))
		{
		//$model->user_id = Yii::app()->user->id;
		$model->created_by = Yii::app()->user->id;
		$model->created_datetime =date('Y-m-d h:i:s');
		if ((int) @$_POST['UserPortal']['id'] > 0)
			{   $model->updated_by = Yii::app()->user->id;
				$model->updated_datetime =date('Y-m-d h:i:s');
				$id= (int) @$_POST['UserPortal']['id'];
				$model=$model->findByPk($id); 
				$audit_type = 'UPDATED'; 
				$edit_flag=1;
			}else{
			  $audit_type = 'CREATED';
			}
			$model->attributes=$_POST['UserPortal'];
			if ($model->save())
			{
				
				// 	// inserting to audit log
				// 	AuditLog::saveAuditLogs(
				// 	  'Portal_MASTER',
				// 	  Yii::app()->user->id,
				// 	  $audit_type,
				// 	  " with code: - {$model->portal_id}  "
				//   );
				  // end audit log
					$jsonData['status'] = 'success';
					$jsonData['id'] = $model->id;
				echo CJSON::encode($jsonData);
			}
			else
			{
					$error = CActiveForm::validate($model, '', false);
					if ($error != '[]')
				   echo $error;
			}
	  
		}
}


public function actionDelete()
{
  $id = $_POST['id'];
	$model=new UserPortal();
	$del = $model->findByPk($id); // audit log changes
	$model->deleteByPk($id);
	// inserting to audit log
// 	AuditLog::saveAuditLogs(
// 	  'Portal_MASTER',
// 	  Yii::app()->user->id,
// 	  'DELETED',
// 	  " with code: - {$del->portal_id}  "
//   );
	echo "success";
}



public function actionBlockUserPortal() 
{
  $id = $_POST['id'];
  if ($id > 0) {
	UserPortal::model()->updateByPK($id, array('isActive' => "N"));
	 // inserting to audit log
	 $model=new UserPortal();
	 $upt = $model->findByPk($id); // audit log changes
	//  AuditLog::saveAuditLogs(
	// 	 'Portal_MASTER',
	// 	 Yii::app()->user->id,
	// 	 'BLOCKED',
	// 	 " with name: - {$upt->portal_id}  "
	//  );
	// end audit log
	  $status['status'] = 'success';
  } else {
	  $status['status'] = 'error';
  }
  echo CJSON::encode($status);
}

public function actionUnBlockUserPortal() {
  $id = $_POST['id'];
  if ($id > 0) {
	UserPortal::model()->updateByPK($id, array('isActive' => "Y"));
	 // inserting to audit log
	 $model=new UserPortal();
	 $upt = $model->findByPk($id); // audit log changes
	//  AuditLog::saveAuditLogs(
	// 	 'Portal_MASTER',
	// 	 Yii::app()->user->id,
	// 	 'UNBLOCKED',
	// 	 " with name: - {$upt->portal_id}  "
	//  );
	// end audit log
	  $status['status'] = 'success';
  } else {
	  $status['status'] = 'error';
  }
  echo CJSON::encode($status);
}

  
}