(C) Moorfields Eye Hospital NHS Foundation Trust, 2008-2011 (C) OpenEyes Foundation, 2011-2013 This file is part of OpenEyes. OpenEyes is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. OpenEyes 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 OpenEyes in a file titled COPYING. If not, see .
Author: OpenEyes (info@openeyes.org.uk)
Inheritance: extends BaseActiveRecordVersioned
 public function testReasonInactiveForRecordUpdate()
 {
     $test = new OphTrOperationbooking_ScheduleOperation_PatientUnavailable();
     $test->start_date = '2014-04-03';
     $test->end_date = '2014-04-03';
     $test->reason_id = $this->reasons('inactive_reason')->id;
     // force the scenario as means we don't have to actually save anything in the db for this test
     $test->scenario = 'update';
     $this->assertTrue($test->validate());
 }
 /**
  * Handle the patient unavailables.
  *
  * @see BaseEventTypeController::setElementComplexAttributesFromData($element, $data, $index)
  */
 protected function setComplexAttributes_Element_OphTrOperationbooking_ScheduleOperation($element, $data, $index)
 {
     if (isset($data['Element_OphTrOperationbooking_ScheduleOperation']['patient_unavailables'])) {
         $puns = array();
         foreach ($data['Element_OphTrOperationbooking_ScheduleOperation']['patient_unavailables'] as $i => $attributes) {
             if ($id = @$attributes['id']) {
                 $pun = OphTrOperationbooking_ScheduleOperation_PatientUnavailable::model()->findByPk($id);
             } else {
                 $pun = new OphTrOperationbooking_ScheduleOperation_PatientUnavailable();
             }
             $pun->attributes = Helper::convertNHS2MySQL($attributes);
             $puns[] = $pun;
         }
         $element->patient_unavailables = $puns;
     }
 }
 /**
  * Set the patient unavailable objects for this element.
  *
  * @param $unavailables
  *
  * @throws Exception
  */
 public function updatePatientUnavailables($unavailables)
 {
     $curr_by_id = array();
     $save = array();
     $criteria = new CDbCriteria();
     $criteria->addCondition('element_id = :eid');
     $criteria->params[':eid'] = $this->id;
     foreach (OphTrOperationbooking_ScheduleOperation_PatientUnavailable::model()->findAll($criteria) as $c_pun) {
         $curr_by_id[$c_pun->id] = $c_pun;
     }
     foreach ($unavailables as $unavailable) {
         if (@$unavailable['id']) {
             // it's an existing one
             $obj = $curr_by_id[$unavailable['id']];
             unset($curr_by_id[$unavailable['id']]);
         } else {
             $obj = new OphTrOperationbooking_ScheduleOperation_PatientUnavailable();
             $obj->element_id = $this->id;
         }
         $dosave = false;
         foreach (array('start_date', 'end_date', 'reason_id') as $attr) {
             if ($obj->{$attr} != $unavailable[$attr]) {
                 $dosave = true;
                 $obj->{$attr} = $unavailable[$attr];
             }
         }
         if ($dosave) {
             $save[] = $obj;
         }
     }
     foreach ($save as $s) {
         if (!$s->save()) {
             throw new Exception('Unable to save Patient Unavailable ' . print_r($s->getErrors(), true));
         }
     }
     foreach ($curr_by_id as $id => $d) {
         if (!$d->delete()) {
             throw new Exception('Unable to delete Patient Unavailable ' . print_r($d->getErrors(), true));
         }
     }
 }
					</thead>
					<tbody class="unavailables">
					<?php 
if ($element->patient_unavailables) {
    foreach ($element->patient_unavailables as $key => $unavailable) {
        $this->renderPartial('form_OphTrOperationbooking_ScheduleOperation_PatientUnavailable', array('key' => $key, 'unavailable' => $unavailable, 'form' => $form, 'element_name' => get_class($element)));
        ++$key;
    }
}
?>
					</tbody>
					<tfoot>
					<tr>
						<td colspan="4"><button class="secondary small addUnavailable">Add</button></td>
					</tr>
					</tfoot>
				</table>
			</div>
		</div>
	</fieldset>
<?php 
$template_unavailable = new OphTrOperationbooking_ScheduleOperation_PatientUnavailable();
$template_unavailable->setDefaultOptions();
?>
<script id="intraocularpressure_reading_template" type="text/html">
	<?php 
$this->renderPartial('form_OphTrOperationbooking_ScheduleOperation_PatientUnavailable', array('key' => '{{key}}', 'unavailable' => $template_unavailable, 'form' => $form, 'element_name' => get_class($element), 'dateFieldWidget' => 'TextField'));
?>
</script>