function getScheduleArray($filter_data)
 {
     global $current_user, $current_user_prefs;
     //Get all schedule data by general filter criteria.
     Debug::Arr($filter_data, 'Filter Data: ', __FILE__, __LINE__, __METHOD__, 10);
     if (!isset($filter_data['start_date']) or $filter_data['start_date'] == '') {
         return FALSE;
     }
     if (!isset($filter_data['end_date']) or $filter_data['end_date'] == '') {
         return FALSE;
     }
     $filter_data['start_date'] = TTDate::getBeginDayEpoch($filter_data['start_date']);
     $filter_data['end_date'] = TTDate::getEndDayEpoch($filter_data['end_date']);
     $blf = new BranchListFactory();
     $branch_options = $blf->getByCompanyIdArray($current_user->getCompany(), FALSE);
     $dlf = new DepartmentListFactory();
     $department_options = $dlf->getByCompanyIdArray($current_user->getCompany(), FALSE);
     $slf = new ScheduleListFactory();
     $slf->getSearchByCompanyIdAndArrayCriteria($current_user->getCompany(), $filter_data);
     Debug::text('Found Scheduled Rows: ' . $slf->getRecordCount(), __FILE__, __LINE__, __METHOD__, 10);
     if ($slf->getRecordCount() > 0) {
         foreach ($slf as $s_obj) {
             Debug::text('Schedule ID: ' . $s_obj->getId() . ' User ID: ' . $s_obj->getColumn('user_id') . ' Start Time: ' . $s_obj->getStartTime(), __FILE__, __LINE__, __METHOD__, 10);
             if (is_object($s_obj->getAbsencePolicyObject())) {
                 $absence_policy_name = (string) $s_obj->getAbsencePolicyObject()->getName();
             } else {
                 $absence_policy_name = 'N/A';
             }
             $iso_date_stamp = TTDate::getISODateStamp($s_obj->getStartTime());
             $schedule_shifts[$iso_date_stamp][$s_obj->getColumn('user_id') . $s_obj->getStartTime()] = array('id' => (int) $s_obj->getID(), 'user_id' => (int) $s_obj->getColumn('user_id'), 'user_created_by' => (int) $s_obj->getColumn('user_created_by'), 'user_full_name' => Misc::getFullName($s_obj->getColumn('first_name'), NULL, $s_obj->getColumn('last_name'), FALSE, FALSE), 'first_name' => $s_obj->getColumn('first_name'), 'last_name' => $s_obj->getColumn('last_name'), 'status_id' => (int) $s_obj->getStatus(), 'date_stamp' => TTDate::getAPIDate('DATE', TTDate::parseDateTime($s_obj->getColumn('date_stamp'))), 'start_date' => defined('TIMETREX_API') ? TTDate::getAPIDate('DATE+TIME', $s_obj->getStartTime()) : $s_obj->getStartTime(), 'end_date' => defined('TIMETREX_API') ? TTDate::getAPIDate('DATE+TIME', $s_obj->getEndTime()) : $s_obj->getEndTime(), 'start_time' => defined('TIMETREX_API') ? TTDate::getAPIDate('TIME', $s_obj->getStartTime()) : $s_obj->getStartTime(), 'end_time' => defined('TIMETREX_API') ? TTDate::getAPIDate('TIME', $s_obj->getEndTime()) : $s_obj->getEndTime(), 'total_time' => $s_obj->getTotalTime(), 'schedule_policy_id' => (int) $s_obj->getSchedulePolicyID(), 'absence_policy_id' => (int) $s_obj->getAbsencePolicyID(), 'absence_policy' => $absence_policy_name, 'branch_id' => (int) $s_obj->getBranch(), 'branch' => Option::getByKey($s_obj->getBranch(), $branch_options, NULL), 'department_id' => (int) $s_obj->getDepartment(), 'department' => Option::getByKey($s_obj->getDepartment(), $department_options, NULL));
             $schedule_shifts_index[$iso_date_stamp][$s_obj->getColumn('user_id')][] = $s_obj->getColumn('user_id') . $s_obj->getStartTime();
             unset($absence_policy_name);
         }
         //Debug::Arr($schedule_shifts, 'Committed Schedule Shifts: ', __FILE__, __LINE__, __METHOD__, 10);
         //Debug::Arr($schedule_shifts_index, 'Committed Schedule Shifts Index: ', __FILE__, __LINE__, __METHOD__, 10);
     } else {
         $schedule_shifts = array();
     }
     unset($slf);
     //Get holidays
     //FIXME: What if there are two holiday policies, one that defaults to working, and another that defaults to not working, and they are assigned
     //to two different groups of employees? For that matter what if the holiday policy isn't assigned to a specific user at all.
     $holiday_data = array();
     $hlf = new HolidayListFactory();
     $hlf->getByCompanyIdAndStartDateAndEndDate($current_user->getCompany(), $filter_data['start_date'], $filter_data['end_date']);
     Debug::text('Found Holiday Rows: ' . $hlf->getRecordCount(), __FILE__, __LINE__, __METHOD__, 10);
     foreach ($hlf as $h_obj) {
         if (is_object($h_obj->getHolidayPolicyObject()) and is_object($h_obj->getHolidayPolicyObject()->getAbsencePolicyObject())) {
             $holiday_data[TTDate::getISODateStamp($h_obj->getDateStamp())] = array('status_id' => (int) $h_obj->getHolidayPolicyObject()->getDefaultScheduleStatus(), 'absence_policy_id' => $h_obj->getHolidayPolicyObject()->getAbsencePolicyID(), 'absence_policy' => $h_obj->getHolidayPolicyObject()->getAbsencePolicyObject()->getName());
         } else {
             $holiday_data[TTDate::getISODateStamp($h_obj->getDateStamp())] = array('status_id' => 10);
             //Working
         }
     }
     unset($hlf);
     $recurring_schedule_shifts = array();
     $recurring_schedule_shifts_index = array();
     $rstlf = new RecurringScheduleTemplateListFactory();
     $rstlf->getSearchByCompanyIdAndArrayCriteria($current_user->getCompany(), $filter_data);
     Debug::text('Found Recurring Schedule Template Rows: ' . $rstlf->getRecordCount(), __FILE__, __LINE__, __METHOD__, 10);
     if ($rstlf->getRecordCount() > 0) {
         foreach ($rstlf as $rst_obj) {
             //Debug::text('Recurring Schedule Template ID: '. $rst_obj->getID() , __FILE__, __LINE__, __METHOD__, 10);
             $rst_obj->getShifts($filter_data['start_date'], $filter_data['end_date'], $holiday_data, $branch_options, $department_options, &$schedule_shifts, &$schedule_shifts_index);
         }
     } else {
         Debug::text('DID NOT find Recurring Schedule for this time period: ', __FILE__, __LINE__, __METHOD__, 10);
     }
     //Debug::Arr($schedule_shifts, 'Schedule Shifts: ', __FILE__, __LINE__, __METHOD__, 10);
     unset($schedule_shifts_index, $recurring_schedule_shifts_index);
     if (isset($schedule_shifts)) {
         return $schedule_shifts;
     }
     return FALSE;
 }
 function getShiftsByStartDateAndEndDate($start_date, $end_date)
 {
     //Make sure timezone isn't in the time format. Because recurring schedules
     //are timezone agnostic. 7:00AM in PST is also 7:00AM in EST.
     //This causes an issue where the previous users timezone carries over to the next
     //users timezone, causing errors.
     //TTDate::setTimeFormat('g:i A');
     if ($start_date == '') {
         return FALSE;
     }
     if ($end_date == '') {
         return FALSE;
     }
     if ($start_date < $this->getStartDate()) {
         $start_date = $this->getStartDate();
     }
     if ($this->getEndDate(TRUE) != NULL and $end_date > $this->getEndDate()) {
         $end_date = $this->getEndDate();
     }
     Debug::text('Start Date: ' . TTDate::getDate('DATE+TIME', $start_date) . ' End Date: ' . TTDate::getDate('DATE+TIME', $end_date), __FILE__, __LINE__, __METHOD__, 10);
     //Get week data
     $rstlf = new RecurringScheduleTemplateListFactory();
     $rstlf->getByRecurringScheduleTemplateControlId($this->getRecurringScheduleTemplateControl())->getCurrent();
     $max_week = 1;
     $weeks = array();
     if ($rstlf->getRecordCount() > 0) {
         foreach ($rstlf as $rst_obj) {
             Debug::text('Week: ' . $rst_obj->getWeek(), __FILE__, __LINE__, __METHOD__, 10);
             $template_week_rows[$rst_obj->getWeek()][] = $rst_obj->getObjectAsArray();
             $weeks[$rst_obj->getWeek()] = $rst_obj->getWeek();
             if ($rst_obj->getWeek() > $max_week) {
                 $max_week = $rst_obj->getWeek();
             }
         }
     }
     $weeks = $this->ReMapWeeks($weeks);
     //Get week of start_date
     $start_date_week = TTDate::getWeek($this->getStartDate(), 0);
     //Start week on Sunday to match Recurring Schedule.
     Debug::text('Week of Start Date: ' . $start_date_week, __FILE__, __LINE__, __METHOD__, 10);
     for ($i = $start_date; $i <= $end_date; $i += 86400) {
         $current_week = TTDate::getWeek($i, 0);
         //Start week on Sunday to match Recurring Schedule.
         //Debug::text('Current Date: '. TTDate::getDate('DATE+TIME', $i) .' Current Week: '. $current_week,__FILE__, __LINE__, __METHOD__, 10);
         $template_week = abs($current_week - $start_date_week) % $max_week + 1;
         //Debug::text('Template Week: '. $template_week .' Max Week: '. $max_week,__FILE__, __LINE__, __METHOD__, 10);
         $day_of_week = strtolower(date('D', $i));
         //Debug::text('Day Of Week: '. $day_of_week,__FILE__, __LINE__, __METHOD__, 10);
         if (isset($weeks[$template_week])) {
             $mapped_template_week = $weeks[$template_week];
             //Debug::text('&nbsp;&nbsp;Mapped Template Week: '. $mapped_template_week,__FILE__, __LINE__, __METHOD__, 10);
             if (isset($template_week_rows[$mapped_template_week])) {
                 //Debug::text('&nbsp;&nbsp;&nbsp;&nbsp;Starting Looping...!',__FILE__, __LINE__, __METHOD__, 10);
                 foreach ($template_week_rows[$mapped_template_week] as $template_week_arr) {
                     if ($template_week_arr['days'][$day_of_week] == TRUE) {
                         //Debug::text('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Found Scheduled Time: Start Time: '. TTDate::getDate('DATE+TIME', TTDate::getTimeLockedDate( $template_week_arr['start_time'], $i ) ),__FILE__, __LINE__, __METHOD__, 10);
                         $start_time = TTDate::getTimeLockedDate($template_week_arr['start_time'], $i);
                         $end_time = TTDate::getTimeLockedDate($template_week_arr['end_time'], $i);
                         if ($end_time < $start_time) {
                             //Spans the day boundary, add 86400 to end_time
                             $end_time = $end_time + 86400;
                             //Debug::text('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Schedule spans day boundary, bumping endtime to next day: ',__FILE__, __LINE__, __METHOD__, 10);
                         }
                         //Debug::text('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Start Date: '. TTDate::getDate('DATE+TIME', $start_time) .' End Date: '. TTDate::getDate('DATE+TIME', $end_time),__FILE__, __LINE__, __METHOD__, 10);
                         //$shifts[TTDate::getBeginDayEpoch($i)][] = array(
                         $shifts[TTDate::getISODateStamp($i)][] = array('status_id' => $template_week_arr['status_id'], 'start_time' => $start_time, 'raw_start_time' => TTDate::getDate('DATE+TIME', $start_time), 'end_time' => $end_time, 'raw_end_time' => TTDate::getDate('DATE+TIME', $end_time), 'total_time' => $template_week_arr['total_time'], 'schedule_policy_id' => $template_week_arr['schedule_policy_id'], 'branch_id' => $template_week_arr['branch_id'], 'department_id' => $template_week_arr['department_id'], 'job_id' => $template_week_arr['job_id'], 'job_item_id' => $template_week_arr['job_item_id']);
                         unset($start_time, $end_time);
                     } else {
                         //Debug::text('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;aSkipping!',__FILE__, __LINE__, __METHOD__, 10);
                     }
                 }
             } else {
                 //Debug::text('&nbsp;&nbsp;&nbsp;&nbsp;bSkipping!',__FILE__, __LINE__, __METHOD__, 10);
             }
         } else {
             //Debug::text('&nbsp;&nbsp;cSkipping!',__FILE__, __LINE__, __METHOD__, 10);
         }
     }
     //var_dump($shifts);
     if (isset($shifts)) {
         return $shifts;
     }
     return FALSE;
 }
 */
extract(FormVariables::GetVariables(array('action', 'page', 'sort_column', 'sort_order', 'ids')));
URLBuilder::setURL($_SERVER['SCRIPT_NAME'], array('sort_column' => $sort_column, 'sort_order' => $sort_order, 'page' => $page));
$sort_array = NULL;
if ($sort_column != '') {
    $sort_array = array($sort_column => $sort_order);
}
Debug::Arr($ids, 'Selected Objects', __FILE__, __LINE__, __METHOD__, 10);
$action = Misc::findSubmitButton();
switch ($action) {
    case 'add':
        Redirect::Page(URLBuilder::getURL(NULL, 'EditRecurringScheduleTemplate.php', FALSE));
        break;
    case 'copy':
        $rstf = new RecurringScheduleTemplateFactory();
        $rstlf = new RecurringScheduleTemplateListFactory();
        $rstclf = new RecurringScheduleTemplateControlListFactory();
        foreach ($ids as $id) {
            $rstclf->getByIdAndCompanyId($id, $current_company->getId());
            foreach ($rstclf as $rstc_obj) {
                $rstc_obj->StartTransaction();
                //Get week data
                $rstlf->getByRecurringScheduleTemplateControlId($rstc_obj->getId());
                if ($rstlf->getRecordCount() > 0) {
                    foreach ($rstlf as $rst_obj) {
                        $week_rows[$rst_obj->getId()] = array('id' => $rst_obj->getId(), 'week' => $rst_obj->getWeek(), 'sun' => $rst_obj->getSun(), 'mon' => $rst_obj->getMon(), 'tue' => $rst_obj->getTue(), 'wed' => $rst_obj->getWed(), 'thu' => $rst_obj->getThu(), 'fri' => $rst_obj->getFri(), 'sat' => $rst_obj->getSat(), 'start_time' => $rst_obj->getStartTime(), 'end_time' => $rst_obj->getEndTime(), 'total_time' => $rst_obj->getTotalTime(), 'schedule_policy_id' => $rst_obj->getSchedulePolicyID(), 'branch_id' => $rst_obj->getBranch(), 'department_id' => $rst_obj->getDepartment(), 'job_id' => $rst_obj->getJob(), 'job_item_id' => $rst_obj->getJobItem());
                    }
                }
                $rstc_obj->setId(FALSE);
                $rstc_obj->setName(Misc::generateCopyName($rstc_obj->getName()));
                if ($rstc_obj->isValid()) {