/** * 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)); } } }