diff --git a/modules/cms/classes/MediaLibrary.php b/modules/cms/classes/MediaLibrary.php index a7f87acba..576bc5cee 100644 --- a/modules/cms/classes/MediaLibrary.php +++ b/modules/cms/classes/MediaLibrary.php @@ -467,8 +467,9 @@ class MediaLibrary * S3 doesn't allow getting the last modified timestamp for folders, * so this feature is disabled - folders timestamp is always NULL. */ - $lastModified = $itemType == MediaLibraryItem::TYPE_FILE ? - $this->getStorageDisk()->lastModified($path) : null; + $lastModified = $itemType == MediaLibraryItem::TYPE_FILE + ? $this->getStorageDisk()->lastModified($path) + : null; /* * The folder size (number of items) doesn't respect filters. That @@ -476,8 +477,9 @@ class MediaLibrary * zero items for a folder that contains files not visible with a * currently applied filter. -ab */ - $size = $itemType == MediaLibraryItem::TYPE_FILE ? - $this->getStorageDisk()->size($path) : $this->getFolderItemCount($path); + $size = $itemType == MediaLibraryItem::TYPE_FILE + ? $this->getStorageDisk()->size($path) + : $this->getFolderItemCount($path); $publicUrl = $this->storagePath.$relativePath; return new MediaLibraryItem($relativePath, $size, $lastModified, $itemType, $publicUrl); @@ -492,12 +494,14 @@ class MediaLibrary { $folderItems = array_merge( $this->getStorageDisk()->files($path), - $this->getStorageDisk()->directories($path)); + $this->getStorageDisk()->directories($path) + ); $size = 0; foreach ($folderItems as $folderItem) { - if ($this->isVisible($folderItem)) + if ($this->isVisible($folderItem)) { $size++; + } } return $size; @@ -543,14 +547,14 @@ class MediaLibrary usort($itemList, function($a, $b) use ($sortBy) { switch ($sortBy) { - case self::SORT_BY_TITLE : return strcasecmp($a->path, $b->path); - case self::SORT_BY_SIZE : + case self::SORT_BY_TITLE: return strcasecmp($a->path, $b->path); + case self::SORT_BY_SIZE: if ($a->size > $b->size) return -1; return $a->size < $b->size ? 1 : 0; break; - case self::SORT_BY_MODIFIED : + case self::SORT_BY_MODIFIED: if ($a->lastModified > $b->lastModified) return -1; @@ -593,7 +597,8 @@ class MediaLibrary return $this->storageDisk; return $this->storageDisk = Storage::disk( - Config::get('cms.storage.media.disk', 'local')); + Config::get('cms.storage.media.disk', 'local') + ); } /** diff --git a/modules/cms/classes/MediaLibraryItem.php b/modules/cms/classes/MediaLibraryItem.php index fccab364e..3289bca2b 100644 --- a/modules/cms/classes/MediaLibraryItem.php +++ b/modules/cms/classes/MediaLibraryItem.php @@ -1,7 +1,7 @@ getCropSessionDirPath($cropSessionKey); $fullSessionDirectoryPath = temp_path($sessionDirectoryPath); - if (!File::isDirectory($fullSessionDirectoryPath)) + if (!File::isDirectory($fullSessionDirectoryPath)) { throw new SystemException('The image editing session is not found.'); + } - // Find the image on the disk and resize it + /* + * Find the image on the disk and resize it + */ $imagePath = $fullSessionDirectoryPath.'/'.$fileName; - if (!File::isFile($imagePath)) + if (!File::isFile($imagePath)) { throw new SystemException('The image is not found on the disk.'); + } $extension = pathinfo($originalFileName, PATHINFO_EXTENSION); - $targetImageName = basename($originalFileName, '.'.$extension).'-'.$selectionData['x'] - .'-'.$selectionData['y'].'-'.$selectionData['w'].'-'.$selectionData['h'].'-'; + $targetImageName = basename($originalFileName, '.'.$extension).'-' + .$selectionData['x'].'-' + .$selectionData['y'].'-' + .$selectionData['w'].'-' + .$selectionData['h'].'-'; + $targetImageName .= time(); $targetImageName .= '.'.$extension; $targetTmpPath = $fullSessionDirectoryPath.'/'.$targetImageName; + /* + * Crop the image, otherwise copy original to target destination. + */ if ($selectionData['w'] == 0 || $selectionData['h'] == 0) { - // If cropping is not required, copy the oiginal image to the target destination. File::copy($imagePath, $targetTmpPath); } else { $resizer = Resizer::open($imagePath); - - $resizer->resample($selectionData['x'], $selectionData['y'], $selectionData['w'], $selectionData['h'], $selectionData['w'], $selectionData['h']); + $resizer->resample( + $selectionData['x'], + $selectionData['y'], + $selectionData['w'], + $selectionData['h'], + $selectionData['w'], + $selectionData['h'] + ); $resizer->save($targetTmpPath, 95); } - // Upload the cropped file to the Library - $targetPath = $path.'cropped-images/'.$targetImageName; + /* + * Upload the cropped file to the Library + */ + $targetFolder = $path.'cropped-images'; + $targetPath = $targetFolder.'/'.$targetImageName; $library = MediaLibrary::instance(); - $library->put($targetPath, file_get_contents($targetTmpPath)); - return $library->getPathUrl($targetPath); + + return [ + 'publicUrl' => $library->getPathUrl($targetPath), + 'documentType' => MediaLibraryItem::FILE_TYPE_IMAGE, + 'itemType' => MediaLibraryItem::TYPE_FILE, + 'path' => $targetPath, + 'title' => $targetImageName, + 'folder' => $targetFolder + ]; } } \ No newline at end of file diff --git a/modules/cms/widgets/mediamanager/assets/js/mediamanager-browser-min.js b/modules/cms/widgets/mediamanager/assets/js/mediamanager-browser-min.js index b560c485b..1384cdb90 100644 --- a/modules/cms/widgets/mediamanager/assets/js/mediamanager-browser-min.js +++ b/modules/cms/widgets/mediamanager/assets/js/mediamanager-browser-min.js @@ -321,8 +321,7 @@ if(selectedItems[0].getAttribute('data-document-type')!=='image'){alert(this.opt return} var path=selectedItems[0].getAttribute('data-path') new $.oc.mediaManager.imageCropPopup(path,{alias:this.options.alias,onDone:callback})} -MediaManager.prototype.onImageCropped=function(imageUrl){var item={documentType:'image',publicUrl:imageUrl} -this.$el.trigger('popupcommand',['insert-cropped',item])} +MediaManager.prototype.onImageCropped=function(result){this.$el.trigger('popupcommand',['insert-cropped',result])} MediaManager.prototype.clearSearchTrackInputTimer=function(){if(this.searchTrackInputTimer===null) return clearTimeout(this.searchTrackInputTimer) @@ -573,8 +572,7 @@ MediaManagerImageCropPopup.prototype.cropAndInsert=function(){var data={img:$(th $.oc.stripeLoadIndicator.show() this.$popupElement.find('form').request(this.options.alias+'::onCropImage',{data:data}).always(function(){$.oc.stripeLoadIndicator.hide()}).done(this.proxy(this.onImageCropped))} MediaManagerImageCropPopup.prototype.onImageCropped=function(response){this.hide() -if(this.options.onDone!==undefined) -this.options.onDone(response.result)} +if(this.options.onDone!==undefined){this.options.onDone(response)}} MediaManagerImageCropPopup.prototype.showResizePopup=function(){this.$popupElement.find('button[data-command=resize]').popup({content:this.$popupElement.find('[data-control="resize-template"]').html(),zIndex:1220})} MediaManagerImageCropPopup.prototype.onResizePopupShown=function(ev,button,popup){var $popup=$(popup),$widthControl=$popup.find('input[name=width]'),$heightControl=$popup.find('input[name=height]'),imageWidth=this.fixDimensionValue(this.$popupElement.find('input[data-control=dimension-width]').val()),imageHeight=this.fixDimensionValue(this.$popupElement.find('input[data-control=dimension-height]').val()) $widthControl.val(imageWidth) @@ -635,7 +633,8 @@ this.initRulers() this.initJCrop()} MediaManagerImageCropPopup.prototype.onSelectionModeChanged=function(){var mode=this.getSelectionMode(),$widthInput=this.getWidthInput(),$heightInput=this.getHeightInput() if(mode==='normal'){$widthInput.attr('disabled','disabled') -$heightInput.attr('disabled','disabled')}else{$widthInput.removeAttr('disabled') +$heightInput.attr('disabled','disabled')} +else{$widthInput.removeAttr('disabled') $heightInput.removeAttr('disabled') $widthInput.val(this.fixDimensionValue($widthInput.val())) $heightInput.val(this.fixDimensionValue($heightInput.val()))} diff --git a/modules/cms/widgets/mediamanager/assets/js/mediamanager.imagecroppopup.js b/modules/cms/widgets/mediamanager/assets/js/mediamanager.imagecroppopup.js index 5f54ecf12..8cb94e21e 100644 --- a/modules/cms/widgets/mediamanager/assets/js/mediamanager.imagecroppopup.js +++ b/modules/cms/widgets/mediamanager/assets/js/mediamanager.imagecroppopup.js @@ -236,20 +236,24 @@ } $.oc.stripeLoadIndicator.show() - this.$popupElement.find('form').request( - this.options.alias+'::onCropImage', { + + this.$popupElement + .find('form') + .request(this.options.alias+'::onCropImage', { data: data - } - ).always(function() { - $.oc.stripeLoadIndicator.hide() - }).done(this.proxy(this.onImageCropped)) + }) + .always(function() { + $.oc.stripeLoadIndicator.hide() + }) + .done(this.proxy(this.onImageCropped)) } MediaManagerImageCropPopup.prototype.onImageCropped = function(response) { this.hide() - if (this.options.onDone !== undefined) - this.options.onDone(response.result) + if (this.options.onDone !== undefined) { + this.options.onDone(response) + } } MediaManagerImageCropPopup.prototype.showResizePopup = function() { @@ -409,7 +413,8 @@ if (mode === 'normal') { $widthInput.attr('disabled', 'disabled') $heightInput.attr('disabled', 'disabled') - } else { + } + else { $widthInput.removeAttr('disabled') $heightInput.removeAttr('disabled') @@ -449,13 +454,13 @@ var command = $(ev.currentTarget).data('command') switch (command) { - case 'insert' : + case 'insert': this.cropAndInsert() break - case 'resize' : + case 'resize': this.showResizePopup() break - case 'undo-resizing' : + case 'undo-resizing': this.undoResizing() break } diff --git a/modules/cms/widgets/mediamanager/assets/js/mediamanager.js b/modules/cms/widgets/mediamanager/assets/js/mediamanager.js index 994a2a927..bb37dab30 100644 --- a/modules/cms/widgets/mediamanager/assets/js/mediamanager.js +++ b/modules/cms/widgets/mediamanager/assets/js/mediamanager.js @@ -810,13 +810,8 @@ }) } - MediaManager.prototype.onImageCropped = function(imageUrl) { - var item = { - documentType: 'image', - publicUrl: imageUrl - } - - this.$el.trigger('popupcommand', ['insert-cropped', item]) + MediaManager.prototype.onImageCropped = function(result) { + this.$el.trigger('popupcommand', ['insert-cropped', result]) } //