/**
  * Stores a file.
  *
  * @param AssetFileModel $file
  * @return bool
  */
 public function storeFile(AssetFileModel $file)
 {
     if ($file->id) {
         $fileRecord = AssetFileRecord::model()->findById($file->id);
         if (!$fileRecord) {
             throw new Exception(Craft::t("No asset exists with the ID “{id}”", array('id' => $file->id)));
         }
     } else {
         $elementRecord = new ElementRecord();
         $elementRecord->type = ElementType::Asset;
         $elementRecord->enabled = 1;
         $elementRecord->save();
         $fileRecord = new AssetFileRecord();
         $fileRecord->id = $elementRecord->id;
     }
     $fileRecord->sourceId = $file->sourceId;
     $fileRecord->folderId = $file->folderId;
     $fileRecord->filename = $file->filename;
     $fileRecord->kind = $file->kind;
     $fileRecord->size = $file->size;
     $fileRecord->width = $file->width;
     $fileRecord->height = $file->height;
     $fileRecord->dateModified = $file->dateModified;
     if ($fileRecord->save()) {
         if (!$file->id) {
             // Save the ID on the model now that we have it
             $file->id = $fileRecord->id;
             // Give it a default title based on the file name
             $file->getContent()->title = str_replace('_', ' ', IOHelper::getFileName($file->filename, false));
             $this->saveFileContent($file, false);
         }
         // Update the search index
         craft()->search->indexElementAttributes($file);
         return true;
     } else {
         $file->addErrors($fileRecord->getErrors());
         return false;
     }
 }
Beispiel #2
0
 /**
  * Saves the record for an asset.
  *
  * @param AssetFileModel $file
  *
  * @throws \Exception
  * @return bool
  */
 public function storeFile(AssetFileModel $file)
 {
     $isNewFile = !$file->id;
     if (!$isNewFile) {
         $fileRecord = AssetFileRecord::model()->findById($file->id);
         if (!$fileRecord) {
             throw new Exception(Craft::t("No asset exists with the ID “{id}”", array('id' => $file->id)));
         }
     } else {
         $fileRecord = new AssetFileRecord();
     }
     $fileRecord->sourceId = $file->sourceId;
     $fileRecord->folderId = $file->folderId;
     $fileRecord->filename = $file->filename;
     $fileRecord->kind = $file->kind;
     $fileRecord->size = $file->size;
     $fileRecord->width = $file->width;
     $fileRecord->height = $file->height;
     $fileRecord->dateModified = $file->dateModified;
     $fileRecord->validate();
     $file->addErrors($fileRecord->getErrors());
     if ($file->hasErrors()) {
         return false;
     }
     $transaction = craft()->db->getCurrentTransaction() === null ? craft()->db->beginTransaction() : null;
     try {
         if ($isNewFile && !$file->getContent()->title) {
             // Give it a default title based on the file name
             $file->getContent()->title = str_replace('_', ' ', IOHelper::getFileName($file->filename, false));
         }
         // Fire an 'onBeforeSaveAsset' event
         $this->onBeforeSaveAsset(new Event($this, array('asset' => $file, 'isNewAsset' => $isNewFile)));
         // Save the element
         if (craft()->elements->saveElement($file, false)) {
             // Now that we have an element ID, save it on the other stuff
             if ($isNewFile) {
                 $fileRecord->id = $file->id;
             }
             // Save the file row
             $fileRecord->save(false);
             if ($transaction !== null) {
                 $transaction->commit();
             }
         } else {
             return false;
         }
     } catch (\Exception $e) {
         if ($transaction !== null) {
             $transaction->rollback();
         }
         throw $e;
     }
     // If we've made it here, everything has been successful so far.
     // Fire an 'onSaveAsset' event
     $this->onSaveAsset(new Event($this, array('asset' => $file)));
     if ($this->hasEventHandler('onSaveFileContent')) {
         // Fire an 'onSaveFileContent' event (deprecated)
         $this->onSaveFileContent(new Event($this, array('file' => $file)));
     }
     return true;
 }
 /**
  * Saves the record for an asset.
  *
  * @param AssetFileModel $file
  *
  * @throws \Exception
  * @return bool
  */
 public function storeFile(AssetFileModel $file)
 {
     $isNewFile = !$file->id;
     if (!$isNewFile) {
         $fileRecord = AssetFileRecord::model()->findById($file->id);
         if (!$fileRecord) {
             throw new Exception(Craft::t("No asset exists with the ID “{id}”.", array('id' => $file->id)));
         }
     } else {
         $fileRecord = new AssetFileRecord();
     }
     $fileRecord->sourceId = $file->sourceId;
     $fileRecord->folderId = $file->folderId;
     $fileRecord->filename = $file->filename;
     $fileRecord->kind = $file->kind;
     $fileRecord->size = $file->size;
     $fileRecord->width = $file->width;
     $fileRecord->height = $file->height;
     $fileRecord->dateModified = $file->dateModified;
     $fileRecord->validate();
     $file->addErrors($fileRecord->getErrors());
     if ($file->hasErrors()) {
         return false;
     }
     if ($isNewFile && !$file->getContent()->title) {
         // Give it a default title based on the file name
         $file->getContent()->title = str_replace('_', ' ', IOHelper::getFileName($file->filename, false));
     }
     $transaction = craft()->db->getCurrentTransaction() === null ? craft()->db->beginTransaction() : null;
     try {
         // Fire an 'onBeforeSaveAsset' event
         $event = new Event($this, array('asset' => $file, 'isNewAsset' => $isNewFile));
         $this->onBeforeSaveAsset($event);
         // Is the event giving us the go-ahead?
         if ($event->performAction) {
             // Save the element
             $success = craft()->elements->saveElement($file, false);
             // If it didn't work, rollback the transaction in case something changed in onBeforeSaveAsset
             if (!$success) {
                 if ($transaction !== null) {
                     $transaction->rollback();
                 }
                 return false;
             }
             // Now that we have an element ID, save it on the other stuff
             if ($isNewFile) {
                 $fileRecord->id = $file->id;
             }
             // Save the file row
             $fileRecord->save(false);
         } else {
             $success = false;
         }
         // Commit the transaction regardless of whether we saved the asset, in case something changed
         // in onBeforeSaveAsset
         if ($transaction !== null) {
             $transaction->commit();
         }
     } catch (\Exception $e) {
         if ($transaction !== null) {
             $transaction->rollback();
         }
         throw $e;
     }
     if ($success) {
         // Fire an 'onSaveAsset' event
         $this->onSaveAsset(new Event($this, array('asset' => $file, 'isNewAsset' => $isNewFile)));
         if ($this->hasEventHandler('onSaveFileContent')) {
             // Fire an 'onSaveFileContent' event (deprecated)
             $this->onSaveFileContent(new Event($this, array('file' => $file)));
         }
     }
     return $success;
 }
 private function _updateAsset($asset, $image, $path)
 {
     // Update our model
     $asset->size = IOHelper::getFileSize($path);
     $asset->width = $image->getWidth();
     $asset->height = $image->getHeight();
     // Then, make sure we update the asset info as stored in the database
     $fileRecord = AssetFileRecord::model()->findById($asset->id);
     $fileRecord->size = $asset->size;
     $fileRecord->width = $asset->width;
     $fileRecord->height = $asset->height;
     $fileRecord->dateModified = IOHelper::getLastTimeModified($path);
     $fileRecord->save(false);
 }