public function actionCrop($dims = '1328x560,776x864')
 {
     $dimensions = explode(',', $dims);
     $xy = explode('x', $dimensions[0]);
     $wh = explode('x', $dimensions[1]);
     $src_x = $xy[0];
     $src_y = $xy[1];
     $dest_w = $wh[0];
     $dest_h = $wh[1];
     // find all cropped images in ophinvisualfields_field_measurement->cropped_image_id:
     $fields = OphInVisualfields_Field_Measurement::model()->findAll();
     foreach ($fields as $field) {
         $full = ProtectedFile::model()->findByPk($field->image_id);
         $cropped = ProtectedFile::model()->findByPk($field->cropped_image_id);
         // if the value isnt set, move on
         if (!$full || !$cropped) {
             continue;
         }
         // next step, take image_id and open image:
         if (file_exists($full->getPath())) {
             $src = imagecreatefromgif($full->getPath());
             $dest = imagecreatetruecolor($dest_w, $dest_h);
             imagecopy($dest, $src, 0, 0, $src_x, $src_y, $dest_w, $dest_h);
             imagegif($dest, $cropped->getPath());
             echo 'patient: ' . $field->getPatientMeasurement()->patient->hos_num . ', path: ' . $cropped->getPath() . PHP_EOL;
             // Reset sizes
             $full->size = filesize($full->getPath());
             $full->save();
             $cropped->size = filesize($cropped->getPath());
             $cropped->save();
         }
     }
 }
 private function updateMeasurementReference($measurement_id, $eye_id)
 {
     $existing = $this->dbConnection->createCommand()->select(array('fm.id fm_id', 'mr.id mr_id'))->from('ophinvisualfields_field_measurement fm')->join('patient_measurement pm', 'pm.id = fm.patient_measurement_id')->join('measurement_reference mr', 'mr.patient_measurement_id = pm.id and mr.event_id = :event_id')->join('event ev', 'ev.id = mr.event_id')->where('fm.eye_id = :eye_id and mr.event_id = :event_id', array(':eye_id' => $eye_id, ':event_id' => $this->event_id))->queryRow();
     if ($existing) {
         if ($existing['fm_id'] != $measurement_id) {
             MeasurementReference::model()->deleteByPk($existing['mr_id']);
         } else {
             // Nothing to do
             return;
         }
     }
     if ($measurement_id) {
         OphInVisualfields_Field_Measurement::model()->findByPk($measurement_id)->attach($this->event);
     }
 }
 public function actionImport($importDir, $archiveDir, $errorDir, $dupDir, $interval = 'PT45M', $pasImport = false)
 {
     $this->importDir = $this->checkSeparator($importDir);
     $this->archiveDir = $this->checkSeparator($archiveDir);
     $this->errorDir = $this->checkSeparator($errorDir);
     $this->dupDir = $this->checkSeparator($dupDir);
     $this->interval = $interval;
     $fhirMarshal = Yii::app()->fhirMarshal;
     $eventType = EventType::model()->find('class_name=:class_name', array(':class_name' => 'OphInVisualfields'));
     if (!$eventType) {
         echo 'Cannot find OphInVisualfields event type, cannot continue' . PHP_EOL;
         die;
     }
     echo 'Processing FMES files...' . PHP_EOL;
     $filenames = glob($this->importDir . '/*.fmes');
     echo count($filenames) . " files to process\n";
     foreach ($filenames as $file) {
         try {
             $basename = basename($file);
             echo $basename . PHP_EOL;
             // First check the file has not already been imported:
             $field = file_get_contents($file);
             $fieldObject = $fhirMarshal->parseXml($field);
             if ($protected_file = ProtectedFile::model()->find('name=:name', array(':name' => $fieldObject->file_reference))) {
                 echo '- ProtectedFile exists (' . $protected_file->id . ')' . PHP_EOL;
                 $this->move($this->dupDir, $file);
                 continue;
             }
             // Extract the patient number
             $matches = array();
             preg_match('/__OE_PATIENT_ID_([0-9]*)__/', $field, $matches);
             if (count($matches) < 2) {
                 echo '- Failed to extract patient ID' . PHP_EOL;
                 $this->move($this->errorDir, $file);
                 continue;
             }
             $match = str_pad($matches[1], 7, '0', STR_PAD_LEFT);
             // Fetch the patient
             if ($pasImport) {
                 $model = new Patient(null);
                 $model->hos_num = $match;
                 $results = $model->search()->getData();
                 $patient = reset($results);
             } else {
                 $patient = Patient::model()->find('hos_num=:hos_num', array(':hos_num' => $match));
             }
             if (!$patient) {
                 echo "- Failed to find patient ({$match})" . PHP_EOL;
                 $this->move($this->errorDir, $file);
                 continue;
             }
             $pid = $patient->id;
             $field = preg_replace('/__OE_PATIENT_ID_([0-9]*)__/', $pid, $field);
             // Convert to measurement
             $resource_type = 'MeasurementVisualFieldHumphrey';
             $service = Yii::app()->service->getService($resource_type);
             $fieldObject = $fhirMarshal->parseXml($field);
             $tx = Yii::app()->db->beginTransaction();
             $ref = $service->fhirCreate($fieldObject);
             $tx->commit();
             $refId = $ref->getId();
             $measurement = OphInVisualfields_Field_Measurement::model()->findByPk($refId);
             $study_datetime = $measurement->study_datetime;
             // Check for existing legacy events
             if (!($episode = Episode::model()->find('legacy = 1 AND patient_id = :patient_id', array(':patient_id' => $pid)))) {
                 echo '- No legacy episode found, creating...';
                 $episode = new Episode();
                 $episode->legacy = 1;
                 $episode->patient_id = $pid;
                 $episode->save();
                 echo 'done' . PHP_EOL;
                 // As there are no previous legacy events, we can create a new event
                 $this->newEvent($episode, $eventType, $measurement);
                 $this->move($this->archiveDir, $file);
             } else {
                 // There is a legacy episode, so there may be unmatched legacy field events
                 $criteria = new CdbCriteria();
                 $criteria->condition = 'event_type_id = :event_type_id and t.deleted = 0 and ep.deleted = 0 and ep.legacy = 1 and ep.patient_id = :patient_id';
                 $criteria->join = 'join episode ep on ep.id = t.episode_id';
                 $criteria->order = 't.event_date desc';
                 $criteria->params = array(':patient_id' => $pid, ':event_type_id' => $eventType->id);
                 if ($this->interval) {
                     // we're looking for all events that are bound to a legacy episode,
                     // for the given patient, looking for the last created test -
                     // this accounts for multiple tests per eye - the implication
                     // being that the newest test overrides the last test for the same eye
                     // (e.g. when a mistake is made and the test is re-ran):
                     // Base time on interval defined by user, a narrow time slot that the test falls within
                     $startCreatedTime = new DateTime($study_datetime);
                     $endCreatedTime = new DateTime($study_datetime);
                     $startCreatedTime->sub(new DateInterval($this->interval));
                     $endCreatedTime->add(new DateInterval($this->interval));
                     $criteria->condition .= ' AND t.event_date >= STR_TO_DATE("' . $startCreatedTime->format('Y-m-d H:i:s') . '", "%Y-%m-%d %H:%i:%s") AND t.event_date <= STR_TO_DATE("' . $endCreatedTime->format('Y-m-d H:i:s') . '", "%Y-%m-%d %H:%i:%s")';
                 }
                 // Of events, there can only be one or none:
                 // FIXME: This can return multiple events, so how do we choose?
                 $events = Event::model()->findAll($criteria);
                 if (count($events) == 1) {
                     echo '- Found existing event (' . $events[0]->id . ')' . PHP_EOL;
                     $element = Element_OphInVisualfields_Image::model()->find('event_id = :event_id', array(':event_id' => $events[0]->id));
                     $side = strtolower($measurement->eye->name);
                     if ($existing = $element->{"{$side}_field"}) {
                         if ($measurement->study_datetime > $existing->study_datetime) {
                             echo "Newer than existing measurement on {$side}, overwriting\n";
                             $element->{"{$side}_field_id"} = $measurement->id;
                             $unattached = $existing;
                         } else {
                             echo "Older than existing measurement on {$side}, ignoring\n";
                             $unattached = $measurement;
                         }
                         // Add dummy reference for the unattached measurement
                         $ref = new MeasurementReference();
                         $ref->patient_measurement_id = $unattached->getPatientMeasurement()->id;
                         $ref->save();
                     } else {
                         echo "No existing measurement on {$side}, adding\n";
                         $element->{"{$side}_field_id"} = $measurement->id;
                     }
                     $element->save();
                     $this->move($this->archiveDir, $file);
                 } elseif (count($events) > 1) {
                     echo '- Found more than one matching event, cannot attach' . PHP_EOL;
                     $this->move($this->errorDir, $file);
                 } else {
                     // No events in match window, so we create a new one
                     $this->newEvent($episode, $eventType, $measurement);
                     $this->move($this->archiveDir, $file);
                 }
             }
         } catch (Exception $ex) {
             echo $ex . PHP_EOL;
             if (@$tx && $tx->active) {
                 echo '- rolling back tx' . PHP_EOL;
                 $tx->rollback();
             }
             $this->move($this->errorDir, $file);
         }
     }
 }
<?php

/**
 * (C) OpenEyes Foundation, 2014
 * 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 <http://www.gnu.org/licenses/>.
 *
 * @link http://www.openeyes.org.uk
 *
 * @author OpenEyes <*****@*****.**>
 * @copyright Copyright (C) 2014, OpenEyes Foundation
 * @license http://www.gnu.org/licenses/gpl-3.0.html The GNU General Public License V3.0
 */
$fields = OphInVisualfields_Field_Measurement::model()->getUnattachedForPatient($this->patient, $side == 'left' ? Eye::LEFT : Eye::RIGHT, $this->event);
if (!$element->{"{$side}_field_id"} && $fields) {
    $element->{"{$side}_field_id"} = end($fields)->id;
}
$field_data = array();
foreach ($fields as $field) {
    $field_data[$field->id] = array('id' => $field->id, 'url' => Yii::app()->baseUrl . "/file/view/{$field->cropped_image_id}/400/img.gif", 'date' => date(Helper::NHS_DATE_FORMAT . ' H:i:s', strtotime($field->study_datetime)), 'strategy' => $field->strategy->name, 'pattern' => $field->pattern->name, 'image_id' => $field->image_id);
}
$current_field = $element->{"{$side}_field_id"} ? $field_data[$element->{"{$side}_field_id"}] : null;
Yii::app()->clientScript->registerScript("OphInVisualfields_available_fields_{$side}", "var OphInVisualfields_available_fields_{$side} = " . CJSON::encode($field_data), CClientScript::POS_END);
?>
<div class="element-eye <?php 
echo $side;
?>
-eye column">
	<?php 
 /**
  * @param type $patient_id
  *
  * @return type
  */
 private function getPatientFieldMeasurements($patient_id)
 {
     $patient = Patient::model()->find('hos_num=:hos_num', array(':hos_num' => $patient_id));
     if ($patient) {
         $criteria = new CDbCriteria();
         $criteria->join = 'join patient_measurement on patient_measurement.id=patient_measurement_id';
         $criteria->condition = 'patient_measurement.patient_id=' . $patient->id;
         return OphInVisualfields_Field_Measurement::model()->findAll($criteria);
     }
     return array();
 }