<?php

function highlightcell($cellcolor,$xcrud){
$db12 = Xcrud_db::get_instance();
	    $query = "SELECT mon_shift,
						 mon_hrs,
		                 mon_color,
						 tues_shift,
					     tues_hrs,
		                 tues_color,
						 wed_shift,
						 wed_hrs,
						 wed_color,
						 thurs_shift,
						 thurs_hrs,
						 thurs_color,
						 fri_shift,
						 fri_hrs,
						 fri_color,
						 sat_shift,
						 sat_hrs,
						 sat_color,
						 sun_shift,
						 sun_hrs,
						 sun_color
					  
		          FROM rosteringtimesheet";
				  	 
				   
        
        $db12->query($query);
        $outl = $db12->result();
        foreach($out as $outputl){
            $mon_shift[] = $outputl['mon_shift'];
			$mon_hrs[] = $outputl['mon_hrs'];
			$mon_color[] = $outputl['mon_color'];
			
		
        } 

$xcrud->highlight('mon_shift','=',$mon_shift[],$mon_color[]);
$xcrud->highlight('mon_hrs','=',$mon_hrs[],$mon_color[]);


	
}

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

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

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


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

//tuesday

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

$postdata->set('tues_hrs', $tueshours);

$xcrud->highlight('tues_shift','=',$tues_shift,$tuescolor);
$xcrud->highlight('tues_hrs','=',$tueshours,$tuescolor);

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

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

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

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

$postdata->set('thurs_hrs', $thurshours);

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

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

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

$xcrud->highlight('fri_shift','=',$fri_shift,$fricolor);
$xcrud->highlight('fri_hrs','=',$frihours,$fricolor);

//sat


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

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

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

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

//sun

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

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

$xcrud->highlight('sun_shift','=',$sun_shift,$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';
}
