Esempio n. 1
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;
 }
Esempio n. 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;
     }
     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;
 }