OphCoTherapyapplication_FileCollection::updateFiles PHP Method

updateFiles() public method

update the files for this collection.
public updateFiles ( int[] $file_ids )
$file_ids int[] - array of ProtectedFile ids to assign to the collection
    public function updateFiles($file_ids)
    {
        $current_files = array();
        $save_files = array();
        $current_files = $this->file_assignments;
        // go through each update file id, if it isn't assigned for this element,
        // create assignment and store for saving
        // if there is, remove from the current files array
        // anything left in current files at the end is ripe for deleting
        foreach ($file_ids as $file_id) {
            if (!array_key_exists($file_id, $current_files)) {
                $fa = new OphCoTherapyapplication_FileCollectionAssignment();
                $fa->attributes = array('collection_id' => $this->id, 'file_id' => $file_id);
                $save_files[] = $fa;
            } else {
                // don't want to delete later
                unset($current_files[$file_id]);
            }
        }
        // save what needs saving
        foreach ($save_files as $save) {
            $save->save();
        }
        // delete the rest
        foreach ($current_files as $curr) {
            $curr->delete();
        }
        $this->cleanCompressedFile();
    }

Usage Example

 /**
  * 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";
 }