Location: PHPKode > projects > ConPortal > conportal/redirects/create_shifts.php
<?php
/*
 *  ConPortal - Pomona College ITS scheduling appplication
 *  Copyright (C) 2005-2006  Pomona College
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of version 2 of the GNU General Public License
 *  as published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
require_once('../standard.php');

if (isset($_POST['shift_group']) && isset($_POST['dayList']) &&
    isset($_POST['position']) && isset($_POST['start_hour']) &&
    isset($_POST['start_min']) && isset($_POST['end_hour']) &&
    isset($_POST['end_min']) && isset($_POST['startdate_custom']) &&
	isset($_POST['start_AMPM']) && isset($_POST['end_AMPM']) &&
	isset($_POST['enddate_custom'])) {

    /*
     * Cast to string because otherwise invalid values such as 'dd' would
     * turn into a 0 here and then later, when doing the checks, it would
     * cast the string value to an integer (of 0) and thus, even if the input
     * is invalid, it would pass the check. If we cast to string here, then
     * the comparison is done as strings.
     */

	//adjust for AM/PM
	if($_POST['start_AMPM'] == "PM" && ($_POST['start_hour']!=12))
		$_POST['start_hour'] = $_POST['start_hour'] + 12;
	if($_POST['end_AMPM'] == "PM" && ($_POST['end_hour']!=12))
		$_POST['end_hour'] = $_POST['end_hour'] + 12;

    $start_hour = (string)abs(intval($_POST['start_hour']));
    $start_min  = (string)abs(intval($_POST['start_min']));
    $end_hour   = (string)abs(intval($_POST['end_hour']));
    $end_min    = (string)abs(intval($_POST['end_min']));
	
	$startDate = NULL;
	$endDate = NULL;

    if ($start_hour > 23 || $_POST['start_hour'] != $start_hour)
        print "Invalid starting hour '".$_POST['start_hour']."'\n";
    elseif ($start_min > 59 || $_POST['start_min'] != $start_min)
        print "Invalid starting minute '".$_POST['starting_min']."'\n";
    elseif ($end_hour > 23 || $_POST['end_hour'] != $end_hour)
        print "Invalid ending hour '".$_POST['end_hour']."'\n";
    elseif ($end_min > 59 || $_POST['end_min'] != $end_min)
        print "Invalid ending minute '".$_POST['end_min']."'\n";
    else {
        $start_ts = mktime($start_hour, $start_min, 00);
        $end_ts   = mktime($end_hour, $end_min, 00);
	//build other values for multi_shift
	$blackout_start_ts = mktime(BLACKOUT_START_HOURS, BLACKOUT_START_MINUTES, 00);
	$blackout_end_ts = mktime(BLACKOUT_END_HOURS, BLACKOUT_END_MINUTES, 00);
	//silly footwork to generate the value to add to timestamps
	$interval_ts = mktime(($start_hour + EZ_INTERVAL_HOURS),($start_min + EZ_INTERVAL_MINUTES), 00);
	$interval_ts = $interval_ts - $start_ts;

        if ($end_ts <= $start_ts)
            print "Start time must be before end time\n";
        else {
		if (intval($_POST['startdate_custom']) != 0 && isset($_POST['start_month']) &&
			isset($_POST['start_day']) && isset($_POST['start_year']))
		{
			$startDate = mktime(0, 0, 0, $_POST['start_month'],
			$_POST['start_day'], $_POST['start_year']);
		}
		if (intval($_POST['enddate_custom']) != 0 && isset($_POST['end_month']) &&
			isset($_POST['end_day']) && isset($_POST['end_year']))
		{
			$endDate = mktime(0, 0, 0, $_POST['end_month'],
			$_POST['end_day'], $_POST['end_year']);
		}
			
		//start dayList loop here; probably inefficient to check multi_shift each time so maybe someday FIXME
		foreach($_POST['dayList'] as $someDay)
		{
		//changed references from $_POST['day'] to someDay
	
			if(!$_POST['multi_shift']) //if unchecked, old behavior
			{
				$ret = createShift($_POST['shift_group'], $someDay,
                           	$_POST['position'], $start_ts, $end_ts, $startDate, $endDate);
				if (!$ret) {
					echo "<b>There was an error during the shift-creation ".
						"process: <br />";
					display_errors();
					echo "</b><br />\n";
					}
				}		
			else  //multi_shift is checked
			{
				//initially dump the start times
				$curr_start_ts = $start_ts;
				$curr_end_ts = $curr_start_ts + $interval_ts;
				//Note that this will NOT execute in the pathological case
				//where start + interval pushes us past the end timestamp in
				//one iteration.
				//I suppose the case could be made that one should use the given
				//start time and just end it at end_ts, but if that's the
				//case why the heck is the user using multi_shift?!?  
				while($curr_end_ts < $end_ts)
				{
					/*
					//if our current end is right at the 					
					//border just skip ahead
					//update - I think this code doesn't work!
					if(($curr_end_ts == $blackout_start_time) || 
						($curr_end_ts == $blackout_start_time))
					{
						$curr_start_time = $blackout_end_time;
						$curr_end_ts = $curr_start_ts + $interval_ts;
					}
					//$_POST['shift_group']
					//are we in the blackout zone?
					if(($blackout_start_ts < $curr_end_ts) && 
						($curr_end_ts < $blackout_end_ts))
					{
						//var_dump(date(DATE_RFC822,$curr_start_ts));
						//var_dump(date(DATE_RFC822,$blackout_start_ts));
						//var_dump(date(DATE_RFC822,$curr_end_ts));
						//var_dump(date(DATE_RFC822,$blackout_end_ts));
						//create end at start of blackout, assuming start isn't
						//already there; if it is, don't create anything.
						if(!($curr_start_ts == $blackout_start_ts))
						{
							//if the start is somehow in the middle of the blackout...
							if(($blackout_start_ts < $curr_start_ts) &&
								($curr_start_ts < $blackout_end_ts))
							{
								//set the start to the end of the blackout, then...	
								$curr_start_ts = $blackout_end_ts;
								$curr_end_ts = $curr_start_ts + $interval_ts;
								EZCreateShift($someDay, $curr_start_ts, $curr_end_ts, 
									$startDate, $endDate);
							}
							else
							{
								EZCreateShift($someDay ,$curr_start_ts, 									$blackout_start_ts, $startDate, $endDate);
							}
						}
						//since we've hit the blackout, advance curr_end_ts to the
						//end of the blackout period, so the update after the 
						//if/elseif/else statement does the right thing
						$curr_end_ts = $blackout_end_ts;
					}
					//Only time I can think of that this would happen would be if the
					//admin tried to have the multi_shifts start in the middle of 
					//the blackout, but far enough in so that start + interval
					//cleared the end of the blackout.  If they do, start the shifts
					//at the end of the blackout
					else if(($blackout_start_ts < $curr_start_ts) &&
						($curr_start_ts < $blackout_end_ts))
					{
						//set the start to the end of the blackout, then...	
						$curr_start_ts = $blackout_end_ts;
						$curr_end_ts = $curr_start_ts + $interval_ts;
						EZCreateShift($someDay, $curr_start_ts, $curr_end_ts, 
							$startDate, $endDate);
					}
					else //no blackout worries here!
					{
						EZCreateShift($someDay, $curr_start_ts, $curr_end_ts, 
							$startDate, $endDate);
					} */

					//refactored
					//if the start is in the blackout, or at the start of the blackout
					//move start to end of blackout, update end, and create
					if( ($curr_start_ts == $blackout_start_ts) ||
					( ($curr_start_ts > $blackout_start_ts) && 
					($curr_start_ts < $blackout_end_ts) ) )
					{
						$curr_start_ts = $blackout_end_ts;
						$curr_end_ts = $curr_start_ts + $interval_ts;
						//in the case where creation ends halfway through the blackout
						if($curr_start_ts > $end_ts)
						{
							break;
						}
						EZCreateShift($someDay, $curr_start_ts, $curr_end_ts, 
							$startDate, $endDate);
					}		
					else if( ($blackout_start_ts < $curr_start_ts) &&
						 ($curr_start_ts < $blackout_end_ts) )	
					{
						$curr_end_ts = $blackout_start_ts;
						EZCreateShift($someDay, $curr_start_ts, $curr_end_ts, 
							$startDate, $endDate);
						//set end so start is set to it at end of conditional block
						$curr_end_ts = $blackout_end_ts;
					}		
					else //no blackout worries here!
					{
						EZCreateShift($someDay, $curr_start_ts, $curr_end_ts, 
							$startDate, $endDate);
					} 

					//update values, making sure we don't hit the end
					$curr_start_ts = $curr_end_ts;
					$curr_end_ts = $curr_start_ts + $interval_ts;
					//if start+interval pushes us to or over the edge, just 
					//set to the edge and create that final shift 
					//(since the loop won't execute again!)
					if(($curr_end_ts > $end_ts) || ($curr_end_ts == $end_ts))
					{
						$curr_end_ts = $end_ts;
						EZCreateShift($someDay, $curr_start_ts, $curr_end_ts, 
							$startDate, $endDate);
					}
				}//end while
			}//end else
		}//end foreach	
	}//end else
}//end else

}//end if

		
header("Location: ".$_SERVER['HTTP_REFERER']);

//convenience function
function EZCreateShift($someDay, $start_ts, $end_ts, $startDate, $endDate)
{
	$ret = createShift($_POST['shift_group'], $someDay, $_POST['position'], $start_ts, 
		$end_ts, $startDate, $endDate);
	if (!$ret) {
		echo "<b>There was an error during the shift-creation process: <br />";
		display_errors();
		echo "</b><br />\n";
	}
}

?>
Return current item: ConPortal