public function generateSessions($args = array())
 {
     $output = '';
     // Get sequences
     $today = date('Y-m-d');
     $initialEndDate = empty($args) ? strtotime('+13 months') : strtotime($args[0]);
     $sequences = OphTrOperationbooking_Operation_Sequence::model()->findAll('start_date <= :end_date AND (end_date IS NULL or end_date >= :today)', array(':end_date' => date('Y-m-d', $initialEndDate), ':today' => $today));
     foreach ($sequences as $sequence) {
         $criteria = new CDbCriteria();
         $criteria->addCondition('sequence_id = :sequence_id');
         $criteria->params[':sequence_id'] = $sequence->id;
         $criteria->order = 'date desc';
         $session = OphTrOperationbooking_Operation_Session::model()->find($criteria);
         // The date of the most recent session for this sequence plus one day, or the sequence start date if no sessions for this sequence yet
         $startDate = empty($session) ? strtotime($sequence->start_date) : strtotime($session->date) + 60 * 60 * 24;
         // Sessions should be generated up to the smaller of initialEndDate (+13 months or command line) and sequence end_date
         if ($sequence->end_date && strtotime($sequence->end_date) < $initialEndDate) {
             $endDate = strtotime($sequence->end_date);
         } else {
             $endDate = $initialEndDate;
         }
         $dateList = array();
         if ($sequence->interval_id == 1) {
             // NO REPEAT (single session)
             // If a session already exists for this one off there's no point creating another
             if (empty($session)) {
                 $dateList[] = $sequence->start_date;
             }
         } elseif ($sequence->interval_id == 6 && $sequence->week_selection) {
             // MONTHLY REPEAT (weeks x,y of month)
             $date = date('Y-m-d', $startDate);
             $time = $startDate;
             // Get the next occurrence of the sequence on/after the start date
             while (date('N', $time) != date('N', strtotime($sequence->start_date))) {
                 $date = date('Y-m-d', mktime(0, 0, 0, date('m', $time), date('d', $time) + 1, date('Y', $time)));
                 $time = strtotime($date);
             }
             $dateList = $sequence->getWeekOccurrences($sequence->weekday, $sequence->week_selection, $time, $endDate, $date, date('Y-m-d', $endDate));
         } else {
             // WEEKLY REPEAT (every x weeks)
             // There is a repeat interval, e.g. once every two weeks. In the instance of two weeks, the
             //	function below returns 60 * 60 * 24 * 14, i.e. two weeks
             $interval = $sequence->interval->getInteger($endDate);
             // The number of days in the interval - 14 in the case of two week interval
             $days = $interval / 24 / 60 / 60;
             // IF there's no session use the sequence start date. If there is use the most recent
             //	session date plus the interval (e.g. two weeks)
             if (empty($session)) {
                 $nextStartDate = $startDate;
             } else {
                 $nextStartDate = $startDate + $interval - 86400;
             }
             // Convert $nextStartDate (a timestamp of the seqence start date or the most recent session date plus the interval to a date.
             $date = date('Y-m-d', $nextStartDate);
             // The timestamp of the start date
             $time = $nextStartDate;
             // get the next occurrence of the sequence on/after the start date
             // Check to see if the day of the week for the time is the same day of the week as the sequence start date
             //	Process loop if it isn't
             while (date('N', $time) != date('N', strtotime($sequence->start_date))) {
                 // Set the date to $time + 1 day
                 $date = date('Y-m-d', mktime(0, 0, 0, date('m', $time), date('d', $time) + 1, date('Y', $time)));
                 // Set the time to the timstamp for the date + 1 day
                 $time = strtotime($date);
             }
             while ($time <= $endDate) {
                 $dateList[] = $date;
                 $date = date('Y-m-d', mktime(0, 0, 0, date('m', $time), date('d', $time) + $days, date('Y', $time)));
                 $time = strtotime($date);
             }
         }
         if (!empty($dateList)) {
             // Process dateList into sessions
             foreach ($dateList as $date) {
                 // TODO: Check for collisions, maybe in Session validation code
                 $new_session = new OphTrOperationbooking_Operation_Session();
                 foreach (array('start_time', 'end_time', 'consultant', 'anaesthetist', 'paediatric', 'general_anaesthetic', 'theatre_id', 'default_admission_time') as $attribute) {
                     $new_session->{$attribute} = $sequence->{$attribute};
                 }
                 $new_session->date = $date;
                 $new_session->sequence_id = $sequence->id;
                 $new_session->firm_id = $sequence->firm_id;
                 if (Yii::app()->params['sessions_unavailable_past_date'] && $date >= Yii::app()->params['sessions_unavailable_past_date']) {
                     $new_session->available = 0;
                 }
                 $new_session->save();
             }
             $output .= "Sequence ID {$sequence->id}: Created " . count($dateList) . " session(s).\n";
         }
     }
     if (!empty($args[1])) {
         return $output;
     }
 }
 public function actionAddSession()
 {
     $session = new OphTrOperationbooking_Operation_Session();
     $errors = array();
     if (!empty($_POST)) {
         $session->attributes = $_POST['OphTrOperationbooking_Operation_Session'];
         if (!$session->save()) {
             $errors = $session->getErrors();
         } else {
             if (empty($errors)) {
                 Audit::add('admin', 'create', $session->id, null, array('module' => 'OphTrOperationbooking', 'model' => 'OphTrOperationbooking_Operation_Session'));
                 $this->redirect(array('/OphTrOperationbooking/admin/viewSessions'));
             }
         }
     } elseif (isset($_GET['sequence_id'])) {
         $session->sequence_id = $_GET['sequence_id'];
     }
     $this->render('/admin/editsession', array('session' => $session, 'errors' => $errors));
 }