<?php
/**
 * Manually Developed CI Controller
 *
 *
 * @package			AIR\controllers\accesslog_controller
 * @version			1.4
 * @copyright		2015, BizyCorp Internal Systems Development
 * @license			private, All rights reserved
 * @author			nuwan.wickramarathne
 *
 */
	
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Manually Developed CI Controller
 *
 *
 * Usage :- Give how to use this script
 *
 * 		1) function index
 * 		2) function view
 * 		3) function get_xml
 * 		4) function accesslogreport_details
 * 		5) function accessReport
 *
 * @package			AIR\controllers\accesslog_controller
 * @version			1.4
 * @uses
 * @see
 * @copyright		2015, BizyCorp Internal Systems Development
 * @license			private, All rights reserved
 * @author			nuwan.wickramarathne
 * @created			Mar 28, 2014 by nuwan.wickramarathne
 * @modified		Nov 12, 2015 by nuwan.wickramarathne, Nov 21, 2015 by nuwan.wickramarathne
 * @modification	BugID - 21750 - access log added to controller index and wb function, Doc block updated
*/
class Accesslog_controller extends CI_Controller{
	
	/**
	 * Function  index ()
	 *
	 * @param string $file
	 * @param string $wrapped_data
	 * @access public 
	 * @since 
	 * @modified           Jun 17, 2016 by Roshan Ruzaik <mrmroshan@yahoo.com>
	 * @modification    Fixed bug id 20482 ,21508 ,21387  
	 * @return void
	 */
	function index($file='access_wb',$wrapped_data=''){
		$this->load->view($file,$wrapped_data);
	}
	
	/**
	 * view function to view access log data
	 * @param string $page
	 * @param string $data
	 */
	public function view($page = 'access_log', $data=false ){
		
		if ($data) $wrapper['data'] = $data;
  			$wrapper['title'] = ucfirst($page); // Capitalize the first letter
		
		$this->load->view($page, $wrapper);
	}
	
	/**
	 * get_xml_report function
	 * view access log report
	 * 
	 * @param string $search
	 * @param number $limit
	 * @param number $offset
	 * @param string $file
	 * @param string $format
	 * @param string $startDate
	 * @param string $endDate
	 * @return number
	 */
	public function get_xml_report($search=FALSE,$limit=0,$offset=0,$file='access_log_report_gridfeed',$format='HTML', $startDate='', $endDate=''){
		
    	$startDate 	= $startDate!=''?$startDate:date("Y-m-d",strtotime(date("Y-m-d")."-6 days"));
    	$endDate 	= $endDate!=''?$endDate:date("Y-m-d");
		    	
    	$this->load->helper('file');
  		$logFiles = get_filenames('./application/logs/access/');
  		sort($logFiles);
  		$recordUserData='';
  		
  		$get_fiel=false;
  		if($startDate!=''){
  			for($x=0; $x<count($logFiles); $x++){
  				$startDu = date_diff(date_create($startDate),date_create(substr($logFiles[$x],3,10)));
  				$endDu = date_diff(date_create($endDate),date_create(substr($logFiles[$x],3,10)));
  				
  				if($startDu->format("%R%a days")>=0 && $endDu->format("%R%a days")<=0 ){
  					$recordUserData .= file_get_contents("./application/logs/access/".$logFiles[$x]);
  				}
  			}
  		}
    	$rw = simplexml_load_string('<rows>'.$recordUserData.'</rows>');
    	
		$data_set;
		$x=1;
		foreach ($rw->row as $childs){
			
			if(strchr($childs->cell[10],"/work_sheet/save/") && $childs->cell[14] && $childs->cell[14]!=' Mins'){
				$data_set[$x]['serial_no'] = 0;
				$data_set[$x]['createDate'] = $childs->cell[0].'';
				$data_set[$x]['staff_name'] = $childs->cell[1].'';
				
				$data_set[$x]['mode'] 		= $childs->cell[12].'';
				$data_set[$x]['duration'] 	= $childs->cell[14]?$childs->cell[14]:'0.00';
				$data_set[$x]['duration_hr']= '0.00';
				$data_set[$x]['total_count']= 0;
				$data_set[$x]['average'] 	= '0.00';
				$x++;
			}
		}
		
		/**
		 * staff_sorting ($a, $b)
		 * 
		 * This is a sortin function
		 *  
		 * @param array $a
		 * @param array $b
		 */
  		function staff_sorting ($a, $b) { return (strcmp ($a['staff_name'],$b['staff_name'])); }
		if($data_set)
  			usort($data_set, 'staff_sorting');
		
		$xx;
		for($x=1; $x<=count($data_set); $x++){ $xx[$x] = $data_set[$x-1]; }
		$data_set = $xx;
		
		$data_tem;
		$staff_name='';
		$duration="0.00";
		$y=1;
		
		
		for($x=1; $x<=count($data_set); $x++){
			if($staff_name==''){
				$staff_name = $data_set[$x]['staff_name'];
				$data_tem[$y] = $data_set[$x];
				if($data_set[$x]['mode']=='add' || $data_set[$x]['mode']=='Authorized')
					$data_tem[$y]['total_count']++;
				
				if($data_tem[$y]['duration'])
					$data_tem[$y]['duration'] = number_format(floatval($data_tem[$y]['duration']),2,".",'');
				else $data_tem[$y]['duration'] = '0.00';
				
				$data_set[$x]=$x;
			}
			else if($staff_name==$data_set[$x]['staff_name']){
				if($data_set[$x]['mode']=='add' || $data_set[$x]['mode']=='Authorized')
					$data_tem[$y]['total_count']++;
				$data_tem[$y]['duration'] = floatval($data_tem[$y]['duration'])+floatval($data_set[$x]['duration']);
				
				$data_set[$x]=$x;
			}
			else if($staff_name!=$data_set[$x]['staff_name']){
				$totleSeconds = round($data_tem[$y]['duration']*60);
				$data_tem[$y]['duration'] = date('i:s',$totleSeconds);  //number_format($data_tem[$y]['duration'],2,":",'');
				
				$data_tem[$y]['duration_hr'] = date('H:i', mktime(0,$data_tem[$y]['duration'])); 
				$totleSeconds = explode(":", $data_tem[$y]['duration']); //if you know its safe
				$totleSeconds = (($totleSeconds[0]*60) + $totleSeconds[1]);
				if($data_tem[$y]['total_count']!='0.00')
					$data_tem[$y]['average'] = date('i:s', (round($totleSeconds/$data_tem[$y]['total_count'])));
				else
					$data_tem[$y]['average'] = date('i:s', (round($totleSeconds)));
					
				$data_tem[$y]['serial_no'] = str_pad($y, 3, '0', STR_PAD_LEFT);
				
				$y++;
				$staff_name = $data_set[$x]['staff_name'];
				$data_tem[$y] = $data_set[$x];
				if($data_set[$x]['mode']=='add' || $data_set[$x]['mode']=='Authorized')
					$data_tem[$y]['total_count']++;
				if($data_tem[$y]['duration'])
					$data_tem[$y]['duration'] = number_format(floatval($data_tem[$y]['duration']),2,".",'');
				else $data_tem[$y]['duration'] = '0.00';
				
				$data_set[$x]=$x;
			}
		}
		if($data_tem[$y]['total_count']!=0){
			
			$totleSeconds = round($data_tem[$y]['duration']*60);
			$data_tem[$y]['duration'] = date('i:s',$totleSeconds);
			
			$data_tem[$y]['duration_hr'] = date('H:i', mktime(0,$data_tem[$y]['duration'])); 
			$totleSeconds = explode(":", $data_tem[$y]['duration']);
			$totleSeconds = (($totleSeconds[0]*60) + $totleSeconds[1]);
			$data_tem[$y]['average'] = date('i:s', (round($totleSeconds/$data_tem[$y]['total_count'])));
			
			$data_tem[$y]['serial_no'] = str_pad($y, 3, '0', STR_PAD_LEFT);
		}
		
		$data_set = $data_tem;
		
		$metaData['PK'] = 'staff_name';
		$metaData['createDate']['header'] 	= 'Create Id';
		$metaData['createDate']['type']		= 'varchar';
		$metaData['createDate']['width']	= '225';
		
		$metaData['serial_no']['header'] 	= 'No';
		$metaData['serial_no']['type']		= 'int';
		$metaData['serial_no']['width']		= '11';
		
		$metaData['staff_name']['header'] 	= 'Staff Name';
		$metaData['staff_name']['type']		= 'varchar';
		$metaData['staff_name']['width']	= '225';
		
		$metaData['duration']['header'] 	= 'Total Time spent on <br/>filling TS (min:sec)';
		$metaData['duration']['type']		= 'time';
		$metaData['duration']['width']		= '';
		
		$metaData['duration_hr']['header'] 	= 'Total Time spent on <br/>filling TS (hr:min)';
		$metaData['duration_hr']['type']	= 'time';
		$metaData['duration_hr']['width']	= '';
		
		$metaData['total_count']['header'] 	= 'Total TS added';
		$metaData['total_count']['type']	= 'int';
		$metaData['total_count']['width']	= '11';
		
		$metaData['average']['header'] 		= 'Average time spent <br/>for a TS (min:sec)';
		$metaData['average']['type']		= 'time';
		$metaData['average']['width']		= '';
		
		$total_rows = count($data_set);
		
		$dataArray['dataset']['metadata'] = $metaData;
		$dataArray['dataset']['total_rows'] = $total_rows;
		$dataArray['dataset']['result_set'] = $data_set;
			
		$wrapped_data['data'] = $dataArray;
	    $wrapped_data['format'] = $format;
	    //$wrapped_data['searchOn'] = $search;
	        
  		$this->load->view($file, $wrapped_data);
	}
	
	
	/**
	 * accesslogreport_details function
	 * access log detail view
	 * 
	 * @param string $search
	 * @param number $limit
	 * @param number $offset
	 * @param string $file
	 * @param string $format
	 * @param string $startDate
	 * @param string $endDate
	 * @param string $staffName
	 */
	public function accesslogreport_details($search=FALSE,$limit=0,$offset=0,$file='accesslogdetail_gridfeed',$format='HTML', $startDate='', $endDate='', $staffName=''){
		
		$staffName = str_ireplace("%20"," ",$staffName);
    	$startDate 	= $startDate!=''?$startDate:date("Y-m-d",strtotime(date("Y-m-d")."-6 days"));
    	$endDate 	= $endDate!=''?$endDate:date("Y-m-d");
		
    	$this->load->helper('file');
  		$logFiles = get_filenames('./application/logs/access/');
  		sort($logFiles);
  		$recordUserData='';
  		
  		$get_fiel=false;
  		if($startDate!=''){
  			for($x=0; $x<count($logFiles); $x++){
  				$startDu = date_diff(date_create($startDate),date_create(substr($logFiles[$x],3,10)));
  				$endDu = date_diff(date_create($endDate),date_create(substr($logFiles[$x],3,10)));
  				
  				if($startDu->format("%R%a days")>=0 && $endDu->format("%R%a days")<=0 ){
  					$recordUserData .= file_get_contents("./application/logs/access/".$logFiles[$x]);
  				}
  			}
  		}
    	$rw = simplexml_load_string('<rows>'.$recordUserData.'</rows>');
    	
		$data_set;
		$x=1;
		foreach ($rw->row as $childs){
			$staff_name	= $childs->cell[1].'';
			
			if(strchr($childs->cell[10],"/work_sheet/save/") && $childs->cell[14] && $childs->cell[14]!=' Mins' && $staff_name==$staffName){
				$data_set[$x]['date_created'] 	= $childs->cell[0].'';
  				$data_set[$x]['user_id'] 		= $childs->cell[1].'';
  				$data_set[$x]['member_type'] 	= $childs->cell[2].'';
 	 			$data_set[$x]['ip_address'] 	= $childs->cell[3].'';
 	 			$data_set[$x]['referer'] 		= $childs->cell[4].'';
 	 			$data_set[$x]['gateway'] 		= $childs->cell[5].'';
 	 			$data_set[$x]['server_port'] 	= $childs->cell[6].'';
 	 			$data_set[$x]['remote_port'] 	= $childs->cell[7].'';
 	 			$data_set[$x]['server_name'] 	= $childs->cell[8].'';
 	 			$data_set[$x]['request_method'] = $childs->cell[9].'';
 	 			$data_set[$x]['request_uri']	= $childs->cell[10].'';
 	 			$data_set[$x]['user_agent'] 	= $childs->cell[11].'';
 	 			$data_set[$x]['mode'] 			= $childs->cell[12].'';
 	 			$data_set[$x]['data'] 			= $childs->cell[13].'';
				$data_set[$x]['duration'] 		= $childs->cell[14]?$childs->cell[14].'':'0.00';
				$x++;
			}
		}
		//var_dump($data_set);
		
		$metaData['PK'] = 'date_created';
		$metaData['date_created']['header'] 	= 'Create Date';
		$metaData['date_created']['type']		= 'varchar';
		$metaData['date_created']['width']		= '225';
		
		$metaData['user_id']['header'] 			= 'Staff Name';
		$metaData['user_id']['type']			= 'varchar';
		$metaData['user_id']['width']			= '225';
				
  		$metaData['member_type']['header'] 		= 'Member type';
		$metaData['member_type']['type']		= 'varchar';
		$metaData['member_type']['width']		= '225';
 	 	
  		$metaData['ip_address']['header'] 		= 'IP address';
		$metaData['ip_address']['type']			= 'varchar';
		$metaData['ip_address']['width']		= '225';
 	 	
  		$metaData['referer']['header'] 			= 'Referer';
		$metaData['referer']['type']			= 'varchar';
		$metaData['referer']['width']			= '225';
 	 	
  		$metaData['gateway']['header'] 			= 'Gateway';
		$metaData['gateway']['type']			= 'varchar';
		$metaData['gateway']['width']			= '225';
 	 	
  		$metaData['server_port']['header'] 		= 'Server port';
		$metaData['server_port']['type']		= 'varchar';
		$metaData['server_port']['width']		= '225';
 	 	
  		$metaData['remote_port']['header']		= 'Remote port';
		$metaData['remote_port']['type']		= 'varchar';
		$metaData['remote_port']['width']		= '225';
 	 	
  		$metaData['server_name']['header'] 		= 'Server name';
		$metaData['server_name']['type']		= 'varchar';
		$metaData['server_name']['width']		= '225';
 	 	
  		$metaData['request_method']['header']	= 'Request method';
		$metaData['request_method']['type']		= 'varchar';
		$metaData['request_method']['width']	= '225';
 	 	
  		$metaData['request_uri']['header'] 		= 'Request URL';
		$metaData['request_uri']['type']		= 'varchar';
		$metaData['request_uri']['width']		= '225';
 	 	
  		$metaData['user_agent']['header'] 		= 'User agent';
		$metaData['user_agent']['type']			= 'varchar';
		$metaData['user_agent']['width']		= '225';
 	 	
  		$metaData['mode']['header'] 			= 'Mode';
		$metaData['mode']['type']				= 'varchar';
		$metaData['mode']['width']				= '225';
 	 	
  		$metaData['data']['header'] 			= 'Data';
		$metaData['data']['type']				= 'varchar';
		$metaData['data']['width']				= '225';
		
  		$metaData['duration']['header'] 		= 'Duration';
		$metaData['duration']['type']			= 'int';
		$metaData['duration']['width']			= '';
		
		$total_rows = count($data_set);
		
		$dataArray['dataset']['metadata'] = $metaData;
		$dataArray['dataset']['total_rows'] = $total_rows;
		$dataArray['dataset']['result_set'] = $data_set;
		
		$wrapped_data['data'] = $dataArray;
	    $wrapped_data['format'] = $format;
	    
  		$this->load->view($file, $wrapped_data);
	}

	/**
	 * accessReport functions
	 * load access log wb
	 * @param string $pathnotice
	 */	
	 function accessReport($pathnotice='accesslogreport_wb'){
	 	
  		$data['staffName'] 	= str_ireplace(" ","%20",$this->input->get('staffName'));
  		$data['startDate'] 	= $this->input->get('startDate');
  		$data['endDate'] 	= $this->input->get('endDate');
  		
  		$this->load->view($pathnotice,$data);
  	}
}