public function import($filename, $reset_parent) { $this->reset_parent = $reset_parent; if (!$filename) { $this->usageError('Import filename required'); } if (!file_exists($filename)) { $this->usageError("Cannot find import file " . $filename); } if (!$this->reset_parent) { $this->required_cols[] = 'disorder_id'; } $connection = Yii::app()->db; $transaction = $connection->beginTransaction(); try { SecondaryToCommonOphthalmicDisorder::model()->deleteAll(); $file = file($filename); $columns = array(); $count = 0; $warnings = array(); foreach ($file as $index => $line) { if (!$index) { $columns = str_getcsv($line, ',', '"'); foreach ($this->required_cols as $req) { if (!in_array($req, $columns)) { throw new Exception("Missing required column '{$req}' from file {$filename}"); } } } else { if (!strlen(trim($line))) { // skip empty line continue; } $record = str_getcsv($line, ',', '"'); $data = array(); foreach ($columns as $i => $col) { $data[$col] = @$record[$i]; } if (!($subspecialty = $this->getSubspecialty($data['subspecialty_code']))) { $warnings[] = "no subspecialty found for {$data['subspecialty_code']}"; } else { if ($this->reset_parent) { $this->resetSubspecialty($subspecialty); } if ($cod = $this->getCOD($data['parent_disorder_id'], $subspecialty)) { if ($data['disorder_id'] && ($st_disorder = $this->getDisorder($data['disorder_id']))) { $st = new SecondaryToCommonOphthalmicDisorder(); $st->parent_id = $cod->id; $st->disorder_id = $st_disorder->id; $st->display_order = $this->getNextDisplayOrderForParent($cod); $st->save(); $count++; } else { if ($data['disorder_id'] && !$this->reset_parent) { $warnings[] = "Cannot find disorder with id {$data['disorder_id']}"; } } } else { $warnings[] = "{$data['parent_disorder_id']} not a common disorder for {$subspecialty->name}"; } } } } echo "Committing changes ...\n"; $transaction->commit(); echo "{$count} records created\n"; if ($warnings) { echo "There were " . count($warnings) . " warnings:\n"; foreach ($warnings as $warn) { echo $warn . "\n"; } } } catch (Exception $e) { $transaction->rollback(); $this->usageError($e->getMessage()); } }