/** * abstraction to process FileCollection form. * * @param OphCoTherapyapplication_FileCollection $model * @param string $audit_type */ protected function processFileCollectionForm($model, $audit_type = 'create') { $model->attributes = $_POST['OphCoTherapyapplication_FileCollection']; // validate the model $model->validate(); $transaction = Yii::app()->getDb()->beginTransaction(); // slightly complex rollback process because of files being copied into the protected file store // we want to be able to roll this back as well as the db process. try { $pf_ids = $this->processFileCollectionFileUpload($model, $_FILES['OphCoTherapyapplication_FileCollection_files']); if (!count($model->getErrors())) { if ($model->save()) { // because this might be an update, we get the current files on the model so that we don't remove files // from it $curr_pf_ids = array(); foreach ($model->files as $file) { $curr_pf_ids[] = $file->id; } $model->updateFiles(array_merge($curr_pf_ids, $pf_ids)); Audit::add('admin', $audit_type, $model->id, null, array('module' => 'OphCoTherapyapplication', 'model' => 'OphCoTherapyapplication_FileCollection')); Yii::app()->user->setFlash('success', 'File Collection created'); $transaction->commit(); $this->redirect(array('viewfilecollections')); } } // clear out any protected files that might have been created. $criteria = new CDbCriteria(); $criteria->addInCondition('id', $pf_ids); foreach (ProtectedFile::model()->findAll($criteria) as $pf) { $pf->delete(); } // if we've got this far, something is amiss $transaction->rollback(); } catch (Exception $e) { Yii::log('OphCoTherapyapplication_FileCollection creation error: ' . $e->getMessage(), 'error'); Yii::app()->user->setFlash('error', 'An unexpected error has occurred'); // clear out any protected files that might have been created. $criteria = new CDbCriteria(); $criteria->addInCondition('id', $pf_ids); foreach (ProtectedFile::model()->findAll($criteria) as $pf) { $pf->delete(); } $transaction->rollback(); } }
/** * 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"; }