<?php
require_once (PATH_CONTROLLERS . "Common.php") ;
require_once (PATH_MODELS . "activity/ProjectClass.php") ;
require_once (PATH_MODELS . "activity/CustomerClass.php") ;

class Project extends ControllerBase {
	private $type = "" ;
	function __construct() {
		$this->db = $_SESSION[SE_DB] ;
	}
	function __destruct() {
		unset($this->db) ;
	}
	public function processRequest($params) {
		$this->type = REQ_VIEW ;
		
		try {
			$this->db->open() ;
			if (isset($params) && count($params) > 0) {
				if (isset($params['type']))
					$this->type = $params['type'] ;
			}
			switch ($this->type) {
				case REQ_ADD:
					$this->addRecord($params) ;
					break ;
				case REQ_UPDATE:
					$this->updateRecord($params) ;
					break ;
				case REQ_DELETE:
					$this->deleteRecord($params) ;
					break ;
				case REQ_GET:
					$this->getRecord($params) ;
					break ;
				case REQ_LIST:
					$this->getList($params) ;
					break ;
				case REQ_REPORT:
					$this->getReport($params) ;
					break ;
				case REQ_VIEW:
					$this->getView() ;
					break ;
				default:
					$this->sendJsonResponse(Status::Error,"invalid request.","",$this->type) ;
					break ;
			}
			$this->db->close() ;
			return true ;
		} catch (Exception $e) {
			$this->db->close() ;
			die ($e->getMessage()) ;
		}
	}
	private function addRecord($params) {
		$cls 		= new ProjectClass($this->db) ;
		$datas 		= array() ;
		$orgid 		= $_SESSION[SE_ORGID] ;
		$modifyby 	= $_SESSION[SE_USERID] ;
		$modifydate = date_create('now')->format('Y-m-d H:i:s') ;
		$ws 		= $_SESSION[SE_REMOTE_IP];
		
		$datas[] = $this->db->fieldValue(ProjectTable::C_TITLE,$this->getParam($params,'title',"")) ;
		$datas[] = $this->db->fieldValue(ProjectTable::C_DESC,$this->getParam($params,'desc',"")) ;
		$datas[] = $this->db->fieldValue(ProjectTable::C_CODE,$this->getParam($params,'code',"")) ;
		$datas[] = $this->db->fieldValue(ProjectTable::C_CUSTOMER_ID,$this->getParam($params,'customer',"")) ;
		$datas[] = $this->db->fieldValue(ProjectTable::C_ORG_ID,$orgid) ;
		$datas[] = $this->db->fieldValue(ProjectTable::C_WS_ID,$ws) ;
		$datas[] = $this->db->fieldValue(ProjectTable::C_MODIFY_BY,$modifyby) ;
		$datas[] = $this->db->fieldValue(ProjectTable::C_CREATE_BY,$modifyby) ;
		$datas[] = $this->db->fieldValue(ProjectTable::C_MODIFY_DATE,$modifydate) ;
		$datas[] = $this->db->fieldValue(ProjectTable::C_CREATE_DATE,$modifydate) ;
		
		try {
			$id = $cls->addRecord($datas) ;
			if ($id > 0) {
				$this->sendJsonResponse(Status::Ok,"Project successfully added to the system.",$id,$this->type);
			} else {
				$this->sendJsonResponse(Status::Error,"Sorry, there is a error in adding new Project to the system.",$id, $this->type) ;
			}
		} catch (Exception $e) {
			Log::write('[Project]' . $e->getMessage());
			$this->sendJsonResponse(Status::Error,"Sorry, there is a error in database operation.","",$this->type) ;
		}
		unset($cls) ;
	}
	private function updateRecord($params) {
		if (isset($params['id'])) {
			$id = $params['id'] ;
			$cls = new ProjectClass($this->db) ;
			try {
				$datas = array() ;
                
                $modifyby 	= $_SESSION[SE_USERID] ;
                $modifydate = date_create('now')->format('Y-m-d H:i:s') ;
                $ws = $_SESSION[SE_REMOTE_IP] ;

				$datas[] = $this->db->fieldValue(ProjectTable::C_TITLE,$this->getParam($params,'title',"")) ;
				$datas[] = $this->db->fieldValue(ProjectTable::C_DESC,$this->getParam($params,'desc',"")) ;
				$datas[] = $this->db->fieldValue(ProjectTable::C_CODE,$this->getParam($params,'code',"")) ;
				$datas[] = $this->db->fieldValue(ProjectTable::C_CUSTOMER_ID,$this->getParam($params,'customer',"")) ;
                $datas[] = $this->db->fieldValue(ProjectTable::C_WS_ID,$ws) ;
                $datas[] = $this->db->fieldValue(ProjectTable::C_MODIFY_BY,$modifyby) ;
                $datas[] = $this->db->fieldValue(ProjectTable::C_MODIFY_DATE,$modifydate) ;
                
				$cls->updateRecord($id,$datas) ;
				$this->sendJsonResponse(Status::Ok,"Project detail successfully updated to the system.",$id,$this->type) ;
			} catch (Exception $e) {
				Log::write('[Project]' . $e->getMessage());
				$this->sendJsonResponse(Status::Error,"Sorry, there is a error in updating Project detail to the system.","",$this->type) ;
			}
			unset($cls) ;
		}else {
			$this->sendJsonResponse(Status::Error,"You must supply the Claim Project id you wish to update. Please try again.","",$this->type);
		}
	}
	private function deleteRecord($params) {
		if (isset($params['id'])) {
			$id = $params['id'] ;
			$cls = new ProjectClass($this->db) ;
			$isUsed = $cls->isUsed($id);
                
           if($isUsed !== ""){
                    $this->sendJsonResponse(Status::Error,"This Project is being used in " . $isUsed,"",$this->type) ;
            } else {
				try {
					$cls->deleteRecord($id) ; 
					$this->sendJsonResponse(Status::Ok,"Project successfully deleted from the system.","",$this->type);
				} catch (Exception $e) {
					Log::write('[Project]' . $e->getMessage());
					$this->sendJsonResponse(Status::Error,"Sorry, there is a problem in deleting Project record from the system.","",$this->type) ;
				}
			}
			unset($cls) ;
		} else {
			$this->sendJsonResponse(Status::Error,"You must supply the Project id you wish to delete. Please try again.","",$this->type);
		}
	}
	private function getList($datas=null) {
		$cls = new ProjectClass($this->db) ;
		$clsCust = new CustomerClass($this->db) ;
		$filter = $this->db->fieldParam(ProjectTable::C_ORG_ID) ;
		$params = array() ;
		$params[] = $this->db->valueParam(ProjectTable::C_ORG_ID,$_SESSION[SE_ORGID]) ;
		$rows = $cls->getTable($filter,ProjectTable::C_DESC,$params) ;
		$list = "" ;
		foreach ($rows as $row) {
			$customer = ($row[ProjectTable::C_CUSTOMER_ID]==0)?'-':$clsCust->getRecord($row[ProjectTable::C_CUSTOMER_ID])[CustomerTable::C_NAME];
			$id = $row[ProjectTable::C_ID] ;
			$list .= "<tr>" ;
			$list .= "<td>" . $id . "</td>" ;
			$list .= "<td>" . $row[ProjectTable::C_CODE] . "</td>" ;
			$list .= "<td>" . $row[ProjectTable::C_TITLE] . "</td>" ;
			$list .= "<td>" . $row[ProjectTable::C_DESC] . "</td>" ;
			$list .= "<td>" . $customer . "</td>" ;
			$list .= "<td style='text-align:center'><a href='javascript:' onclick='editProject(" . $id . ",this)'><img src='image/edit_16.png' title='Edit'></img></a></td>" ;
			$list .= "<td style='text-align:center'><a href='javascript:' onclick='deleteProject(" . $id . ",this)'><img src='image/delete_16.png' title='Delete'></img></a></td>" ;
			$list .= "</tr>" ;
		}
		
		unset($rows) ;
		unset($cls) ;
		return $list ;
	}
	private function getRecord($params=null) {
		if (isset($params['id'])) {
			$id = $params['id'] ;
			$cls = new ProjectClass($this->db) ;
			$row = $cls->getRecord($id) ;
			if (is_null($row)) {
				$this->sendJsonResponse(Status::Error,"Invalid Project id. Please try again.",$id,$this->type);
			} else {
				$datas = array() ;
				$datas['id'] = $id ;
				$datas['title'] = $row[ProjectTable::C_TITLE];
				$datas['desc'] = $row[ProjectTable::C_DESC];
				$datas['code'] = $row[ProjectTable::C_CODE];
				$datas['customer'] = $row[ProjectTable::C_CUSTOMER_ID];
				$this->sendJsonResponse(Status::Ok,"",$datas,$this->type) ;
			}
			unset($cls) ;
		} else {
			$this->sendJsonResponse(Status::Error,"Missing Project id. Please try again.","",$this->type);
		}
	}
	private function getView() {
        Log::write("Project " . $this->getWebPageURL());
		ob_start() ;
		include (PATH_VIEWS . "activity/ProjectView.php") ;
		echo Util::minifyHtml(ob_get_clean()) ;
	}
	private function getReport($params=null) {
		require_once(PATH_LIB . 'ListPdf.php');
	
		$cls = new ProjectClass($this->db) ;
		$filter = $this->db->fieldParam(ProjectTable::C_ORG_ID) ;
		$params = array() ;
		$params[] = $this->db->valueParam(ProjectTable::C_ORG_ID,$_SESSION[SE_ORGID]) ;
		$rows = $cls->getTable($filter,ProjectTable::C_ID,$params) ;
	
		$i = 'items';
		$nr = 'newrow';
		$datas = array() ;
		foreach ($rows as $row) {
			$items = array() ;
			$items[$i][] = $this->createPdfItem($row[ProjectTable::C_ID],30) ;
			$items[$i][] = $this->createPdfItem($row[ProjectTable::C_CODE],120) ;
			$items[$i][] = $this->createPdfItem($row[ProjectTable::C_TITLE],200) ;
			$items[$i][] = $this->createPdfItem($row[ProjectTable::C_DESC],200) ;
			$items[$i][] = $this->createPdfItem($row[ProjectTable::C_CUSTOMER_ID],200) ;
			$items[$nr] = "1";
			$datas[] = $items ;
		}
		$cols = array() ;
		$cols[] = $this->createPdfItem("ID",30,0,"C","B");
		$cols[] = $this->createPdfItem("Code",120,0,"C","B") ;
		$cols[] = $this->createPdfItem("Title",200,0,"C","B") ;
		$cols[] = $this->createPdfItem("Description",200,0,"C","B") ;
		$cols[] = $this->createPdfItem("Customer",200,0,"C","B") ;
		$pdf = new ListPdf('P');
		$pdf->setCompanyName($_SESSION[SE_ORGNAME]) ;
		$pdf->setReportTitle("Project Listing") ;
		$pdf->setColumnsHeader($cols) ;
		$pdf->render($datas) ;
		$pdf->Output('project.pdf', 'I');
		unset($rows) ;
		unset($cls) ;
		unset($datas) ;
		unset($params) ;
		unset($items) ;
		unset($cols) ;
	}
	private function getCustomer() {
		$vls = $this->getValueList(CustomerTable::C_TABLE, CustomerTable::C_ID, CustomerTable::C_NAME,array('code'=>'0','desc'=>'--- Select a Customer ---')) ;
		return Util::createOptionValue($vls) ;
	}
}
?>