/**
* main method to run the command for file collection creation.
*
* @TODO: look for a summary text file to include.
* @TODO: search for existing file collections and update instead of adding.
*
* @param array $args
*
* @return int|void
*/
public function run($args)
{
if (!count($args) == 1) {
$this->usageError('missing source path argument');
}
if (!is_readable($args[0])) {
$this->usageError('cannot read specified source path ' . $args[0]);
}
$base_path = $args[0];
// read directory structure into data
$file_list = $this->buildFileList($base_path, './');
$file_ext_regexp = implode('|', $this->file_extensions);
$sets = array();
// determine the file collections to be created
foreach ($file_list as $fname => $details) {
if (preg_match('/' . $file_ext_regexp . '$/', $fname)) {
$path = str_replace(DIRECTORY_SEPARATOR, ' - ', dirname($fname));
if (!@$sets[$path]) {
$summary_text = $this->summary_text_default;
$summary_filepath = $base_path . dirname($fname) . DIRECTORY_SEPARATOR . $this->summary_filename;
if ($this->summary_filename && file_exists($summary_filepath)) {
// read the summary text in from the file
$summary_text = file_get_contents($summary_filepath);
}
$sets[$path] = array('summary' => $summary_text, 'files' => array($details));
} else {
$sets[$path]['files'][] = $details;
}
}
}
$created = 0;
$modified = 0;
// iterate through and create the file collections.
foreach ($sets as $set_name => $set_details) {
$created_flag = false;
$transaction = Yii::app()->getDb()->beginTransaction();
$pf_list = array();
$pf_ids = array();
try {
foreach ($set_details['files'] as $details) {
$pf = ProtectedFile::createFromFile($details['source']);
if ($pf->save()) {
$pf_ids[] = $pf->id;
$pf_list[] = $pf;
} else {
foreach ($pf_list as $pf) {
$pf->delete();
}
break;
}
}
// update the existing file collection if there is one
$criteria = new CDbCriteria();
$criteria->addCondition('name = :nm');
$criteria->params = array(':nm' => $set_name);
if (!($fc = OphCoTherapyapplication_FileCollection::model()->find($criteria))) {
$fc = new OphCoTherapyapplication_FileCollection();
$fc->name = $set_name;
$created_flag = true;
}
$fc->summary = $set_details['summary'];
if (!$fc->validate()) {
echo "unexpected validation error with file collection\n";
var_dump($fc->getErrors());
$transaction->rollback();
} else {
if ($fc->save()) {
$fc->updateFiles($pf_ids);
Audit::add('admin', 'create', $fc->id, null, array('module' => 'OphCoTherapyapplication', 'model' => 'OphCoTherapyapplication_FileCollection'));
$transaction->commit();
$created_flag ? $created++ : $modified++;
} else {
foreach ($pf_list as $pf) {
$pf->delete();
}
$transaction->rollback();
}
}
} catch (Exception $e) {
echo $e->getMessage();
foreach ($pf_list as $pf) {
$pf->delete();
}
$transaction->rollback();
}
}
echo 'Processing complete, ' . $created . ' collections created, ' . $modified . " collections updated\n";
}