Exemple #1
0
 /**
 * Delete the asset file on disk
 * 
 * @param boolean $cascade If true records that depend on this record will also be deleted
 * @access private
 */
 function beforeDelete($cascade = true)
 {
     $this->read();
     if (!empty($this->data['Asset']) && is_array($this->data['Asset'])) {
         $assets = array();
         $entries = $this->find('all', array('conditions' => array('Asset.parent_id' => $this->data['Asset']['parent_id'], 'Asset.model' => $this->data['Asset']['model'], 'Asset.foreign_key' => $this->data['Asset']['foreign_key'], 'Asset.field' => $this->data['Asset']['field'])));
         if (!empty($entries) && is_array($entries)) {
             foreach ($entries as $entry) {
                 $assets[] = $entry['Asset']['filename'];
             }
         }
         $model = new Object();
         $model->id = $this->data['Asset']['foreign_key'];
         $model->name = $this->data['Asset']['model'];
         $response = assetDelete($model, $assets);
         if (is_array($response) && $response['status'] != true) {
             assetError($this, $response['message'], $response['type']);
             return false;
         }
     }
     return true;
 }
Exemple #2
0
 /**
 * Saves the original data entry, and clears out any data needing to be overwritten (files and records)
 * 
 * @param object $model Model using this behavior
 * @param string $name Name of the AssetBehvior child instance
 * @access private
 */
 function _saveOriginal(&$model, $name)
 {
     $setting = $this->settings[$model->name][$name];
     # create original data to save into the database
     $original = array('foreign_key' => $model->id, 'model' => $model->name, 'field' => $name, 'version' => 'original', 'filename' => null, 'mime' => assetMimeType($model->data[$name]['tmp_name']), 'size' => $model->data[$name]['size']);
     # include dimensions if a valid file-type (image, swf, etc)
     list($original['width'], $original['height']) = getimagesize($model->data[$name]['tmp_name']);
     # if this is not an update and files are set to overwrite
     if (!isset($setting['overwrite']) || $setting['overwrite'] == true) {
         # count the existing rows for renaming the file
         $existing = $model->{$name}->find('all', array('fields' => array('id', 'filename'), 'conditions' => array('model' => $model->name, 'foreign_key' => $model->id, 'field' => $name)));
         # loop through the existing files and remove each entry
         foreach (!empty($existing) && is_array($existing) ? $existing : array() as $remove) {
             assetDelete($model, array($remove[$name]['filename']));
         }
         # delete all existing records for this field
         $model->{$name}->deleteAll(array('model' => $model->name, 'foreign_key' => $model->id, 'field' => $name));
     }
     # set the filename field value
     $original['filename'] = assetFileName($model->data[$name]['name'], 'original', $name);
     # create new entry
     $model->{$name}->create();
     # save the original file data
     if (!$model->{$name}->create() || !$model->{$name}->save($original, false)) {
         $model->{$name}->delete($original['parent_id']);
         $this->_setError($model, 'Critical Error found while saving record in afterSave: ' . $name, 'error');
         return false;
     }
     # update the parent_id
     $model->{$name}->saveField('parent_id', $model->{$name}->id);
     # create the directory path for this asset
     if (!assetDirectoryCreate($model)) {
         $this->_setError($model, 'Storage directory was not created, file not properly uploaded. Please try again and/or contact an Administrator.');
         return false;
     }
     $this->directory = assetDirectoryPath($model);
     # move the original file
     if (!assetSaveFile($model, $model->data[$name]['tmp_name'], $original['filename'])) {
         $this->_setError($model, 'Critical Error found while saving file in afterSave: ' . $name, 'error');
         return false;
     }
     # save the original filename
     unset($model->data[$name]);
     $model->data[$name]['filename'] = $original['filename'];
     $model->data[$name]['type'] = $original['mime'];
     return true;
 }