Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support CMS 5 #1

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 22 additions & 16 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,30 @@
"type": "silverstripe-vendormodule",
"keywords": ["silverstripe", "upload", "uploader", "files", "forms", "cms"],
"license": "BSD-3-Clause",
"authors": [{
"name": "Uncle Cheese",
"email": "[email protected]"
}],
"require": {
"silverstripe/framework": "4.*"
"authors": [
{
"name": "Uncle Cheese",
"email": "[email protected]"
}
],
"require":
{
"silverstripe/framework": "^4 || ^5"
},
"autoload": {
"psr-4": {
"UncleCheese\\DropZone\\": "code/"
}
},
"minimum-stability": "dev",
"prefer-stable": true,
"extra": {
"installer-name": "dropzone",
"expose": [
"javascript",
"css",
"images"
]
},
"autoload": {
"psr-4": {
"UncleCheese\\Dropzone\\": "src/"
}
}
}
"javascript",
"images",
"templates"
]
}
}
13 changes: 8 additions & 5 deletions javascript/dropzone.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,9 @@ require.register("dropzone/lib/dropzone.js", function (exports, module) {

/*
This is a list of all available events you can register on a dropzone object.

You can register an event handler like this:

dropzone.on("dragEnter", function() { });
*/

Expand Down Expand Up @@ -420,7 +420,7 @@ require.register("dropzone/lib/dropzone.js", function (exports, module) {
this.element.classList.add("dz-started");
}
if (this.previewsContainer) {

file.previewElement = Dropzone.createElement(this.options.previewTemplate.trim());
file.previewTemplate = file.previewElement;
this.previewsContainer.appendChild(file.previewElement);
Expand Down Expand Up @@ -586,6 +586,9 @@ require.register("dropzone/lib/dropzone.js", function (exports, module) {
this.element.dropzone = this;
elementOptions = (_ref = Dropzone.optionsForElement(this.element)) != null ? _ref : {};
this.options = extend({}, this.defaultOptions, elementOptions, options != null ? options : {});
if (this.options.maxFilesize === 0) {
this.options.maxFilesize = 256;
}
if (this.options.forceFallback || !Dropzone.isBrowserSupported()) {
return this.options.fallback.call(this);
}
Expand Down Expand Up @@ -1752,7 +1755,7 @@ require.register("dropzone/lib/dropzone.js", function (exports, module) {


/*

Bugfix for iOS 6 and 7
Source: http://stackoverflow.com/questions/11929099/html5-canvas-drawimage-ratio-bug-ios
based on the work of https://github.com/stomita/ios-imagefile-megapixel
Expand Down Expand Up @@ -1872,4 +1875,4 @@ if (typeof exports == "object") {
} else {
this["Dropzone"] = require("dropzone");
}
})()
})()
150 changes: 73 additions & 77 deletions src/DropzoneFile.php
Original file line number Diff line number Diff line change
@@ -1,92 +1,88 @@
<?php

namespace UncleCheese\Dropzone;
namespace UncleCheese\DropZone;

use SilverStripe\Assets\Folder;
use SilverStripe\Assets\Image;
use SilverStripe\Control\Director;
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Manifest\ModuleResourceLoader;
use SilverStripe\Assets\Folder;
use SilverStripe\ORM\DataExtension;

/**
* Adds helper methods to the core {@link File} object
*
* @package unclecheese/dropzone
* @package unclecheese/dropzone
* @author Uncle Cheese <[email protected]>
*/
class DropzoneFile extends DataExtension
{


/**
* Helper method for determining if this is an Image
*
* @return boolean
*/
public function IsImage()
{
return $this->owner instanceof Image;
}


/**
* Gets a thumbnail for this file given a size. If it's an Image,
* it will render the actual file. If not, it will provide an icon based
* on the extension.
*
* @param int $w The width of the image
* @param int $h The height of the image
* @return Image_Cached
*/
public function getPreviewThumbnail($w = null, $h = null)
{
if(!$w) { $w = $this->owner->config()->grid_thumbnail_width;
}
if(!$h) { $h = $this->owner->config()->grid_thumbnail_height;
}

if($this->IsImage() && Director::fileExists($this->owner->Filename)) {
return $this->owner->CroppedImage($w, $h);
}

$sizes = Config::inst()->forClass(FileAttachmentField::class)->icon_sizes;
sort($sizes);

foreach($sizes as $size) {
if($w <= $size) {
if($this->owner instanceof Folder) {
$file = $this->getFilenameForType('_folder', $size);
}
else {
$file = $this->getFilenameForType($this->owner->getExtension(), $size);
}
if(!file_exists(BASE_PATH.'/'.$file)) {
$file = $this->getFilenameForType('_blank', $size);
}

$image = Image::create();
$image->setFromLocalFile(Director::getAbsFile($file), basename($file));

return $image;
}
}
}


/**
* Gets a filename based on the extension and the size
*
* @param string $ext The extension of the file, e.g. "pdf"
* @param int $size The size of the image
* @return string
*/
protected function getFilenameForType($ext, $size)
{
return ModuleResourceLoader::singleton()->resolveURL(sprintf(
'unclecheese/dropzone:images/file-icons/%spx/%s.png',
$size,
strtolower($ext)
));
}
}


/**
* Helper method for determining if this is an Image
*
* @return boolean
*/
public function IsImage()
{
return $this->owner instanceof Image;
}


/**
* Gets a thumbnail for this file given a size. If it's an Image,
* it will render the actual file. If not, it will provide an icon based
* on the extension.
* @param int $w The width of the image
* @param int $h The height of the image
* @return Image_Cached
*/
public function getPreviewThumbnail($w = null, $h = null)
{
if (!$w) {
$w = $this->owner->config()->grid_thumbnail_width;
}
if (!$h) {
$h = $this->owner->config()->grid_thumbnail_height;
}

if ($this->IsImage() && Director::fileExists($this->owner->Filename)) {
return $this->owner->CroppedImage($w, $h);
}

$sizes = Config::inst()->forClass('FileAttachmentField')->icon_sizes;
sort($sizes);

foreach ($sizes as $size) {
if ($w <= $size) {
if ($this->owner instanceof Folder) {
$file = $this->getFilenameForType('_folder', $size);
} else {
$file = $this->getFilenameForType($this->owner->getExtension(), $size);
}
if (!file_exists(BASE_PATH . '/' . $file)) {
$file = $this->getFilenameForType('_blank', $size);
}

return new File(Director::makeRelative($file));
}
}
}

/**
* Gets a filename based on the extension and the size
*
* @param string $ext The extension of the file, e.g. "pdf"
* @param int $size The size of the image
* @return string
*/
protected function getFilenameForType($ext, $size)
{
return sprintf(
'%s/images/file-icons/%spx/%s.png',
basename(dirname(__FILE__)),
$size,
strtolower($ext)
);
}
}
Loading