<?php


function highlightcell($xcrud,$rostering_timesheet){
	
	//monday cell

$db202 = Xcrud_db::get_instance();
	    $query = "SELECT 
		           shifts.shiftid,
				   shifts.hours,
				   shifts.color
				   			  
		          FROM internalrostering
				  INNER JOIN shifts
				  ON  internalrostering.mon_shift = shifts.shiftid";
				  	 	   
        
        $db202->query($query);
        $out202 = $db202->result();
        foreach($out202 as $output202){
            $mon_shift = $output202['shiftid'];
			$mon_hrs = $output202['hours'];
			$mon_color = $output202['color'];


$rostering_timesheet->highlight('mon_shift','=',$mon_shift,$mon_color);
$rostering_timesheet->highlight('mon_hrs','=',$mon_hrs,$mon_color);


		} 

 
//tuesday cell

$db203 = Xcrud_db::get_instance();
	    $query = "SELECT 
		           shifts.shiftid,
				   shifts.hours,
				   shifts.color
				   			  
		          FROM internalrostering
				  INNER JOIN shifts
				  ON  internalrostering.tues_shift = shifts.shiftid";
				  	 	   
        
        $db203->query($query);
        $out203 = $db203->result();
        foreach($out203 as $output203){
            $tues_shift = $output203['shiftid'];
			$tues_hrs = $output203['hours'];
			$tues_color = $output203['color'];


$rostering_timesheet->highlight('tues_shift','=',$tues_shift,$tues_color);
$rostering_timesheet->highlight('tues_hrs','=',$tues_hrs,$tues_color);


		} 

//wed cell

$db17 = Xcrud_db::get_instance();
	    $query = "SELECT 
		           shifts.shiftid,
				   shifts.hours,
				   shifts.color
				   			  
		          FROM internalrostering
				  INNER JOIN shifts
				  ON  internalrostering.wed_shift = shifts.shiftid";
				  	 	   
        
        $db17->query($query);
        $out10 = $db17->result();
        foreach($out10 as $output10){
            $wed_shift = $output10['shiftid'];
			$wed_hrs = $output10['hours'];
			$wed_color = $output10['color'];


$rostering_timesheet->highlight('wed_shift','=',$wed_shift,$wed_color);
$rostering_timesheet->highlight('wed_hrs','=',$wed_hrs,$wed_color);


		} 

//thurs cell

$db18 = Xcrud_db::get_instance();
	    $query = "SELECT 
		           shifts.shiftid,
				   shifts.hours,
				   shifts.color
				   			  
		          FROM internalrostering
				  INNER JOIN shifts
				  ON  internalrostering.thurs_shift = shifts.shiftid";
				  	 	   
        
        $db18->query($query);
        $out11 = $db18->result();
        foreach($out11 as $output11){
            $thurs_shift = $output11['shiftid'];
			$thurs_hrs = $output11['hours'];
			$thurs_color = $output11['color'];


$rostering_timesheet->highlight('thurs_shift','=',$thurs_shift,$thurs_color);
$rostering_timesheet->highlight('thurs_hrs','=',$thurs_hrs,$thurs_color);


		} 


//friday cell

$db19 = Xcrud_db::get_instance();
	    $query = "SELECT 
		           shifts.shiftid,
				   shifts.hours,
				   shifts.color
				   			  
		          FROM internalrostering
				  INNER JOIN shifts
				  ON  internalrostering.fri_shift = shifts.shiftid";
				  	 	   
        
        $db19->query($query);
        $out12 = $db19->result();
        foreach($out12 as $output12){
            $fri_shift = $output12['shiftid'];
			$fri_hrs = $output12['hours'];
			$fri_color = $output12['color'];


$rostering_timesheet->highlight('fri_shift','=',$fri_shift,$fri_color);
$rostering_timesheet->highlight('fri_hrs','=',$fri_hrs,$fri_color);


		} 



//Sat cell

$db20 = Xcrud_db::get_instance();
	    $query = "SELECT 
		           shifts.shiftid,
				   shifts.hours,
				   shifts.color
				   			  
		          FROM internalrostering
				  INNER JOIN shifts
				  ON  internalrostering.sat_shift = shifts.shiftid";
				  	 	   
        
        $db20->query($query);
        $out13 = $db20->result();
        foreach($out13 as $output13){
            $sat_shift = $output13['shiftid'];
			$sat_hrs = $output13['hours'];
			$sat_color = $output13['color'];


$rostering_timesheet->highlight('sat_shift','=',$sat_shift,$sat_color);
$rostering_timesheet->highlight('sat_hrs','=',$sat_hrs,$sat_color);


		} 

//Sun cell

$db21 = Xcrud_db::get_instance();
	    $query = "SELECT 
		           shifts.shiftid,
				   shifts.hours,
				   shifts.color
				   			  
		          FROM internalrostering
				  INNER JOIN shifts
				  ON  internalrostering.sun_shift = shifts.shiftid";
				  	 	   
        
        $db21->query($query);
        $out14 = $db21->result();
        foreach($out14 as $output14){
            $sun_shift = $output14['shiftid'];
			$sun_hrs = $output14['hours'];
			$sun_color = $output14['color'];


$rostering_timesheet->highlight('sun_shift','=',$sun_shift,$sun_color);
$rostering_timesheet->highlight('sun_hrs','=',$sun_hrs,$sun_color);


		} 

}



function calculate_totalhoursperweek($postdata,$rostering_timesheet,$xcrud){
	
	
	
//mon
 	
$m_shift = $postdata->get('mon_shift');
if (!isset($m_shift))
    {
         $m_shift = 'None';
		
    }
   
$db1 = Xcrud_db::get_instance();
	    $query = "SELECT shiftid,
		                 hours,
		                 color
					  
		          FROM shifts
				  				  
				   WHERE shiftid = '" . $m_shift . "'";
        
        $db1->query($query);
        $out = $db1->result();
        foreach($out as $output){
			$mondayshift=$output['shiftid'];
            $monhours = $output['hours'];
			$moncolor = $output['color'];
        } 

$postdata->set('mon_hrs', $monhours);

//$xcrud->pass_var('mon_color', $moncolor);


//$rostering_timesheet->highlight('mon_shift','=',$mondayshift,$moncolor);
//$rostering_timesheet->highlight('mon_hrs','=',$monhours,$moncolor);

//tuesday

$t_shift = $postdata->get('tues_shift');
if (!isset($t_shift))
    {
         $t_shift = 'None';
		
    }
   
$db2 = Xcrud_db::get_instance();
	    $query = "SELECT shiftid,
		                  hours,
		              color
					  
		          FROM shifts
				  				  
				   WHERE shiftid = '" . $t_shift . "'";
        
        $db2->query($query);
        $out = $db2->result();
        foreach($out as $output){
			$tuesdayshift=$output['shiftid'];
            $tueshours = $output['hours'];
			$tuescolor = $output['color'];
        } 

$postdata->set('tues_hrs', $tueshours);
//$xcrud->pass_var('tues_color', $tuescolor);
//$xcrud->highlight('tues_shift','=',$tuesdayshift,$tuescolor);
//$xcrud->highlight('tues_hrs','=',$tueshours ,$tuescolor);

//wednesday
$w_shift = $postdata->get('wed_shift');
if (!isset($w_shift))
    {
         $w_shift = 'None';
		
    }
   
$db3 = Xcrud_db::get_instance();
	    $query = "SELECT shiftid,
		               hours,
		              color
					  
		          FROM shifts
				  				  
				   WHERE shiftid = '" . $w_shift . "'";
        
        $db3->query($query);
        $out = $db3->result();
        foreach($out as $output){
			$wednesdayshift=$output['shiftid'];
            $wedhours = $output['hours'];
			$wedcolor = $output['color'];
        } 

$postdata->set('wed_hrs', $wedhours);

//$xcrud->pass_var('wed_color', $wedcolor);

//$xcrud->highlight('wed_shift','=',$wednesdayshift,$wedcolor);
//$xcrud->highlight('wed_hrs','=',$wedhours ,$wedcolor);


//thurs
$th_shift = $postdata->get('thurs_shift');
   if (!isset($th_shift))
    {
         $th_shift = 'None';
		
    }
$db4 = Xcrud_db::get_instance();
	    $query = "SELECT shiftid,
		                 hours,
		              color
					  
		          FROM shifts
				  				  
				   WHERE shiftid = '" . $th_shift . "'";
        
        $db4->query($query);
        $out = $db4->result();
        foreach($out as $output){
			$thursdayshift=$output['shiftid'];
            $thurshours = $output['hours'];
			$thurscolor = $output['color'];
        } 

$postdata->set('thurs_hrs', $thurshours);
//$xcrud->pass_var('thurs_color', $thurscolor);

//$xcrud->highlight('thurs_shift','=',$thursdayshift,$thurscolor);
//$xcrud->highlight('thurs_hrs','=',$thurshours ,$thurscolor);


//fri
$frid_shift = $postdata->get('fri_shift');
   if (!isset($frid_shift))
    {
         $frid_shift = 'None';
		
    }
$db5 = Xcrud_db::get_instance();
	    $query = "SELECT shiftid,
		               hours,
		              color
					  
		          FROM shifts
				  				  
				   WHERE shiftid = '" . $frid_shift . "'";
        
        $db5->query($query);
        $out = $db5->result();
        foreach($out as $output){
			$fridayshift=$output['shiftid'];
            $frihours = $output['hours'];
			$fricolor = $output['color'];
        } 

$postdata->set('fri_hrs', $frihours);

//$xcrud->pass_var('fri_color', $fricolor);
//$xcrud->highlight('fri_shift','=',$fridayshift,$fricolor);
//$xcrud->highlight('fri_hrs','=',$frihours ,$fricolor);



//sat


$saturday_shift = $postdata->get('sat_shift');

   if (!isset($saturday_shift))
    {
         $saturday_shift = 'None';
		
    }
$db6 = Xcrud_db::get_instance();
	    $query = "SELECT shiftid,
		               hours,
		              color
					  
		          FROM shifts
				  				  
				   WHERE shiftid = '" . $saturday_shift . "'";
        
        $db6->query($query);
        $out = $db6->result();
        foreach($out as $output){
			$saturdayshift=$output['shiftid'];
            $sathours = $output['hours'];
			$satcolor = $output['color'];
        } 

$postdata->set('sat_hrs', $sathours);

//$xcrud->pass_var('sat_color', $satcolor);


//$xcrud->highlight('sat_shift','=',$saturdayshift,$satcolor);
//$xcrud->highlight('sat_hrs','=',$sathours ,$satcolor);



//sun

$s_shift = $postdata->get('s_shift');
  if (!isset($s_shift))
    {
         $s_shift = 'None';
		
    }
$db7 = Xcrud_db::get_instance();
	    $query = "SELECT shiftid,
		              hours,
		              color
					  
		          FROM shifts
				  				  
				   WHERE shiftid = '" . $s_shift . "'";
        
        $db7->query($query);
        $out = $db7->result();
        foreach($out as $output){
			$sundayshift=$output['shiftid'];
            $sunhours = $output['hours'];
			$suncolor = $output['color'];
        } 

$postdata->set('sun_hrs', $sunhours);

//$xcrud->pass_var('sun_color', $suncolor);
//$xcrud->highlight('sun_shift','=',$sundayshift,$suncolor);
//$xcrud->highlight('sun_hrs','=',$sunhours ,$suncolor);



//insert color infor silently into rosteringtimesheet table




	
//$mon = $postdata->get('mon_hrs');
//$tues = $postdata->get('tues_hrs');
//$wed = $postdata->get('wed_hrs');
//$thurs = $postdata->get('thurs_hrs');
//$fri = $postdata->get('fri_hrs');
//$sat = $postdata->get('sat_hrs');
//$sun = $postdata->get('sun_hrs');

$totalhours = $monhours+$tueshours+$wedhours+$thurshours+$frihours+$sathours+$sunhours;

 $postdata->set('total_hrs', $totalhours);
 
 $employeeid = $postdata->get('employeeid');
 
        $db = Xcrud_db::get_instance();
	    $query = "SELECT 
		              max_hours_to_work
					  
		          FROM employeemaster
				  				  
				   WHERE employeeid = ".$employeeid;
        
        $db->query($query);
        $outm = $db->result();
        foreach($outm as $outputm){
            $maxhours = $outputm['max_hours_to_work'];
			
        } 
		
		if ($totalhours > $maxhours)
    {
        $xcrud->set_exception('total_hrs','Hours Exceeded!','error'); 
		
		
		
}
 	 
 
 
	 

 
}
function calculate_totalhoursperweekg($value, $field, $priimary_key, $list, $xcrud)
{
   return '<div>' 
        
        . '<input type="text" name="'.$xcrud->fieldname_encode($fieldname).'" value="'.$value.'" class="xcrud-input" />'
        
        . '</div>';
}

//display hours automatically
function autocalculatehours($postdata, $primary,$xcrud){
	
	
$mondayshift = $postdata->get('mon_shift');
 
        $db = Xcrud_db::get_instance();
	    $query = "SELECT 
		              
		
		           hours FROM shifts WHERE shiftname = ".$mondayshift;
        
        $db->query($query);
        $outm = $db->result();
        foreach($outm as $outputm){
            $resulthours[] = $outputm['hours'];
        } 
		$postdata->set('mon_hrs',$resulthours[]);
		
}


//display colors for cell for the shift automatically


function after($postdata,$xcrud) {
    
  $monshift= $postdata->get('mon_shift');
   	if ($monshift = ('9am-12pm'))
    {
         $postdata->set('mon_hrs', 3.0);
		
    }
 

		
	 
}

function validate_maxhours($postdata, $primary, $xcrud){
    
	
$totalhrs = $postdata->get('total_hrs');

 $employeeid = $postdata->get('employeeid');
 
        $db = Xcrud_db::get_instance();
	    $query = "SELECT 
		              max_hours_to_work
					  
		          FROM employeemaster
				  				  
				   WHERE employeeid = ".$employeeid;
        
        $db->query($query);
        $outm = $db->result();
        foreach($outm as $outputm){
            $maxhours[] = $outputm['max_hours_to_work'];
        } 
		
		if ($totalhrs > $maxhours)
    {
         $xcrud->set_exception($totalhrs, 'Employee max hours reached. Contact Manager', 'error');
		 $xcrud->highlight('total_hrs', '>', $maxhours, 'red');
    }
 	 

 
}



function publish_action($xcrud)
{
    if ($xcrud->get('primary'))
    {
        $db = Xcrud_db::get_instance();
        $query = 'UPDATE base_fields SET `bool` = b\'1\' WHERE id = ' . (int)$xcrud->get('primary');
        $db->query($query);
    }
}
function unpublish_action($xcrud)
{
    if ($xcrud->get('primary'))
    {
        $db = Xcrud_db::get_instance();
        $query = 'UPDATE base_fields SET `bool` = b\'0\' WHERE id = ' . (int)$xcrud->get('primary');
        $db->query($query);
    }
}

function exception_example($postdata, $primary, $xcrud)
{
    // get random field from $postdata
    $postdata_prepared = array_keys($postdata->to_array());
    shuffle($postdata_prepared);
    $random_field = array_shift($postdata_prepared);
    // set error message
    $xcrud->set_exception($random_field, 'This is a test error', 'error');
}

function test_column_callback($value, $fieldname, $primary, $row, $xcrud)
{
    return $value . ' - nice!';
}

function after_upload_example($field, $file_name, $file_path, $params, $xcrud)
{
    $ext = trim(strtolower(strrchr($file_name, '.')), '.');
    if ($ext != 'pdf' && $field == 'uploads.simple_upload')
    {
        unlink($file_path);
        $xcrud->set_exception('simple_upload', 'This is not PDF', 'error');
    }
}

function movetop($xcrud)
{
    if ($xcrud->get('primary') !== false)
    {
        $primary = (int)$xcrud->get('primary');
        $db = Xcrud_db::get_instance();
        $query = 'SELECT `officeCode` FROM `offices` ORDER BY `ordering`,`officeCode`';
        $db->query($query);
        $result = $db->result();
        $count = count($result);

        $sort = array();
        foreach ($result as $key => $item)
        {
            if ($item['officeCode'] == $primary && $key != 0)
            {
                array_splice($result, $key - 1, 0, array($item));
                unset($result[$key + 1]);
                break;
            }
        }

        foreach ($result as $key => $item)
        {
            $query = 'UPDATE `offices` SET `ordering` = ' . $key . ' WHERE officeCode = ' . $item['officeCode'];
            $db->query($query);
        }
    }
}
function movebottom($xcrud)
{
    if ($xcrud->get('primary') !== false)
    {
        $primary = (int)$xcrud->get('primary');
        $db = Xcrud_db::get_instance();
        $query = 'SELECT `officeCode` FROM `offices` ORDER BY `ordering`,`officeCode`';
        $db->query($query);
        $result = $db->result();
        $count = count($result);

        $sort = array();
        foreach ($result as $key => $item)
        {
            if ($item['officeCode'] == $primary && $key != $count - 1)
            {
                unset($result[$key]);
                array_splice($result, $key + 1, 0, array($item));
                break;
            }
        }

        foreach ($result as $key => $item)
        {
            $query = 'UPDATE `offices` SET `ordering` = ' . $key . ' WHERE officeCode = ' . $item['officeCode'];
            $db->query($query);
        }
    }
}

function show_description($value, $fieldname, $primary_key, $row, $xcrud)
{
    $result = '';
    if ($value == '1')
    {
        $result = '<i class="fa fa-check" />' . 'OK';
    }
    elseif ($value == '2')
    {
        $result = '<i class="fa fa-circle-o" />' . 'Pending';
    }
    return $result;
}

function custom_field($value, $fieldname, $primary_key, $row, $xcrud)
{
	$mondayshift = $postdata->get('mon_shift');
 
        $db = Xcrud_db::get_instance();
	    $query = "SELECT 
		              
		
		           hours FROM shifts WHERE shiftname = ".$mondayshift;
        
        $db->query($query);
        $outm = $db->result();
        foreach($outm as $outputm){
            $resulthours[] = $outputm['hours'];
        } 
	
    return '<input type="text" readonly class="xcrud-input" name="' . $xcrud->fieldname_encode($resulthours) . '" value="' . $value .
        '" />';
}
function unset_val($postdata)
{
    $postdata->del('Paid');
}

function format_phone($new_phone)
{
    $new_phone = preg_replace("/[^0-9]/", "", $new_phone);

    if (strlen($new_phone) == 7)
        return preg_replace("/([0-9]{3})([0-9]{4})/", "$1-$2", $new_phone);
    elseif (strlen($new_phone) == 10)
        return preg_replace("/([0-9]{3})([0-9]{3})([0-9]{4})/", "($1) $2-$3", $new_phone);
    else
        return $new_phone;
}

function before_list_example($list, $xcrud)
{
    var_dump($list);
}

function after_update_test($pd, $pm, $xc)
{
    $xc->search = 0;
}

function after_upload_test($field, &$filename, $file_path, $upload_config, $this)
{
    $filename = 'bla-bla-bla';
}
