A File Collection points to one or more PDF that can be attached to a therapy application
Inheritance: extends BaseActiveRecordVersioned
Beispiel #1
0
 public function actionDeleteFileCollections()
 {
     $result = 1;
     foreach ($_POST['file_collections'] as $file_collection_id) {
         try {
             if ($collection = OphCoTherapyapplication_FileCollection::model()->findByPk($file_collection_id)) {
                 foreach ($collection->file_assignments as $file_assignment) {
                     if (!$collection->removeFileById($file_assignment->file_id)) {
                         $result = 0;
                     }
                 }
                 if (!$collection->delete()) {
                     $result = 0;
                 }
             }
         } catch (Exception $e) {
             Yii::log("couldn't remove file collection {$file_collection_id}: " . $e->getMessage(), 'error');
             $result = 0;
         }
     }
     echo $result;
 }
}
echo $form->dropDownList($element, $side . '_start_period_id', CHtml::listData($start_periods, 'id', 'name'), $html_options, false, array('label' => 4, 'field' => 6));
?>
</div>

<div id="<?php 
echo get_class($element) . '_' . $side;
?>
_urgency_reason"
    <?php 
if (!$urgent) {
    echo 'style="display: none;"';
}
?>
>
    <?php 
echo $form->textArea($element, $side . '_urgency_reason', array(), false, array(), $layoutColumns);
?>
</div>

<?php 
$html_options = array('options' => array(), 'empty' => '- Please select -', 'div_id' => get_class($element) . '_' . $side . '_filecollections', 'div_class' => 'elementField', 'label' => 'File Attachments');
$collections = OphCoTherapyapplication_FileCollection::model()->activeOrPk($element->getFileCollectionValuesForSide($side))->findAll();
//TODO: have sorting with display_order when implemented
/*
$collections = OphCoTherapyapplication_FileCollection::::model()->findAll(array('order'=>'display_order asc'));
foreach ($collections as $collection) {
    $html_options['options'][(string) $collection->id] = array('data-order' => $collection->display_order);
}
*/
$form->multiSelectList($element, get_class($element) . '[' . $side . '_filecollections]', $side . '_filecollections', 'id', CHtml::listData($collections, 'id', 'name'), array(), $html_options, false, false, null, false, false, array('label' => 4, 'field' => 6));
 /**
  * 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";
 }
 public function actionDownloadFileCollection($id)
 {
     if ($collection = OphCoTherapyapplication_FileCollection::model()->findByPk((int) $id)) {
         $pf = $collection->getZipFile();
         if ($pf) {
             $this->redirect($pf->getDownloadURL());
         }
     }
     throw new CHttpException('400', 'File Collection does not exist');
 }