Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuell1 committed Aug 11, 2019
2 parents a24fdd7 + 4e6e93e commit 5a8a2d9
Show file tree
Hide file tree
Showing 7 changed files with 269 additions and 133 deletions.
79 changes: 43 additions & 36 deletions Plugin.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
<?php namespace Samuell\ContentEditor;

use App;
use Event;
use Backend;
use System\Classes\PluginBase;

/**
Expand All @@ -11,39 +8,49 @@
class Plugin extends PluginBase
{

/**
* Returns information about this plugin.
*
* @return array
*/
public function pluginDetails()
{
return [
'name' => 'Content Editor',
'description' => 'Front-end content editor',
'author' => 'Samuell',
'icon' => 'icon-edit'
];
}
/**
* Returns information about this plugin.
*
* @return array
*/
public function pluginDetails()
{
return [
'name' => 'Content Editor',
'description' => 'Front-end content editor',
'author' => 'Samuell',
'icon' => 'icon-edit'
];
}

public function registerComponents()
{
return [
'Samuell\ContentEditor\Components\ContentEditor' => 'contenteditor',
];
}
public function registerComponents()
{
return [
'Samuell\ContentEditor\Components\ContentEditor' => 'contenteditor',
];
}

public function registerSettings()
{
return [
'settings' => [
'label' => 'Content Editor Settings',
'description' => 'Manage main editor settings.',
'icon' => 'icon-cog',
'class' => 'Samuell\ContentEditor\Models\Settings',
'order' => 500,
'permissions' => ['samuell.contenteditor.access_settings']
]
];
}
public function registerSettings()
{
return [
'settings' => [
'label' => 'Content Editor Settings',
'description' => 'Manage main editor settings.',
'icon' => 'icon-cog',
'class' => 'Samuell\ContentEditor\Models\Settings',
'order' => 500,
'permissions' => ['samuell.contenteditor.access_settings']
]
];
}

public function registerPermissions()
{
return [
'samuell.contenteditor.access_settings' => [
'tab' => 'Content Editor',
'label' => 'Access content editor settings'
],
];
}
}
86 changes: 73 additions & 13 deletions assets/contenteditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@
* Load ContentTools
*/
var editor = ContentTools.EditorApp.get();
editor.init('[data-editable], [data-fixture]', 'data-file');
editor.init(
'[data-editable], [data-fixture]',
'data-file',
function (domElement) {
return domElement.hasAttribute('data-fixture');
}
);

var siteUrl = document.location.origin; // get site url for requests

ContentTools.RESTRICTED_ATTRIBUTES['*'] = []; // allow style attribute on elements

/*
* Save event
*/
Expand Down Expand Up @@ -131,6 +139,49 @@ ContentTools.Tools.Subheading5 = (function(_super) {
function imageUploader(dialog) {
var image, xhr, xhrComplete, xhrProgress;


// Image rotate
// function rotateImage(direction) {
// var formData;

// xhrComplete = function (event) {
// var response;
// if (parseInt(event.target.readyState) !== 4) return;

// xhr = null;
// xhrComplete = null;
// dialog.busy(false);

// if (parseInt(event.target.status) === 200) {
// response = JSON.parse(event.target.responseText);
// if (response.errors) {
// for (var k in response.errors) console.log(response.errors[k]);
// new ContentTools.FlashUI('no');
// }
// else {
// image = {
// size: response.size,
// url: response.url + '?_ignore=' + Date.now()
// };
// dialog.populate(image.url, image.size);
// }
// } else {
// new ContentTools.FlashUI('no');
// }
// };

// dialog.busy(true);

// formData = new FormData();
// formData.append('url', image.url);
// formData.append('direction', direction);

// xhr = new XMLHttpRequest();
// xhr.addEventListener('readystatechange', xhrComplete);
// xhr.open('POST', siteUrl+'/contenteditor/image/rotate', true);
// xhr.send(formData);
// }

// Image upload cancel
dialog.addEventListener('imageuploader.cancelUpload', function () {
// Stop the upload
Expand All @@ -150,6 +201,14 @@ function imageUploader(dialog) {
image = null;
});

// dialog.addEventListener('imageuploader.rotateccw', function () {
// rotateImage('CCW');
// });

// dialog.addEventListener('imageuploader.rotatecw', function () {
// rotateImage('CW');
// });

// Image upload
dialog.addEventListener('imageuploader.fileready', function (ev) {

Expand Down Expand Up @@ -178,20 +237,20 @@ function imageUploader(dialog) {

// Handle the result of the upload
if (parseInt(ev.target.status) == 200) {
// Unpack the response (from JSON)
response = JSON.parse(ev.target.responseText);

// Store the image details
image = {
if (response.errors) {
for (var k in response.errors) console.log(response.errors[k]);
new ContentTools.FlashUI('no');
} else {
image = {
size: response.size,
alt: response.filename,
url: response.url
};

// Populate the dialog
dialog.populate(image.url, image.size);
url: response.url,
filePath: response.filePath
};
dialog.populate(image.url, image.size);
}
} else {
// The request failed, notify the user
new ContentTools.FlashUI('no');
}
}
Expand All @@ -208,7 +267,7 @@ function imageUploader(dialog) {
xhr = new XMLHttpRequest();
xhr.upload.addEventListener('progress', xhrProgress);
xhr.addEventListener('readystatechange', xhrComplete);
xhr.open('POST', siteUrl+'/samuell/contenteditor/image/upload', true);
xhr.open('POST', siteUrl+'/contenteditor/image/upload', true);
xhr.send(formData);
});

Expand Down Expand Up @@ -257,6 +316,7 @@ function imageUploader(dialog) {
// Build the form data to post to the server
formData = new FormData();
formData.append('url', image.url);
formData.append('filePath', image.filePath);
formData.append('width', image.size[0]);
formData.append('height', image.size[1]);
formData.append('alt', image.alt);
Expand All @@ -269,7 +329,7 @@ function imageUploader(dialog) {
// Make the request
xhr = new XMLHttpRequest();
xhr.addEventListener('readystatechange', xhrComplete);
xhr.open('POST', siteUrl+'/samuell/contenteditor/image/save', true);
xhr.open('POST', siteUrl+'/contenteditor/image/save', true);
xhr.send(formData);
});

Expand Down
125 changes: 125 additions & 0 deletions http/controllers/ImageController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php namespace Samuell\ContentEditor\Http\Controllers;

use ApplicationException;
use Exception;
use Response;
use File;
use Input;
use Illuminate\Routing\Controller;
use October\Rain\Database\Attach\Resizer;
use Cms\Classes\MediaLibrary;
use Cms\Helpers\File as FileHelper;
use Samuell\ContentEditor\Models\Settings;
use Samuell\ContentEditor\Http\Middleware\EditorPermissionsMiddleware;

class ImageController extends Controller
{
public function __construct()
{
$this->middleware('web');
$this->middleware(EditorPermissionsMiddleware::class);
}

public function upload()
{
try {
if (!Input::hasFile('image')) {
throw new ApplicationException('File missing from request');
}

$uploadedFile = Input::file('image');
$fileName = $uploadedFile->getClientOriginalName();

// Convert uppcare case file extensions to lower case
$extension = strtolower($uploadedFile->getClientOriginalExtension());
$fileName = File::name($fileName).'.'.$extension;

// File name contains non-latin characters, attempt to slug the value
if (!FileHelper::validateName($fileName)) {
$fileNameSlug = Str::slug(File::name($fileName), '-');
$fileName = $fileNameSlug.'.'.$extension;
}
if (!$uploadedFile->isValid()) {
throw new ApplicationException($uploadedFile->getErrorMessage());
}

$path = Settings::get('image_folder', 'contenteditor');
$path = MediaLibrary::validatePath($path);

$realPath = empty(trim($uploadedFile->getRealPath()))
? $uploadedFile->getPath() . DIRECTORY_SEPARATOR . $uploadedFile->getFileName()
: $uploadedFile->getRealPath();

MediaLibrary::instance()->put(
$path.'/'.$fileName,
File::get($realPath)
);

list($width, $height) = getimagesize($uploadedFile);

return Response::json([
'url' => MediaLibrary::instance()->getPathUrl($path.'/'.$fileName),
'filePath' => $path.'/'.$fileName,
'filename' => $fileName,
'size' => [
$width,
$height
]
]);
} catch (Exception $ex) {
throw new ApplicationException($ex);
}

}

public function save()
{
$url = post('url');
$crop = post('crop');
$width = post('width');
$height = post('height');

if ($crop && $crop != '0,0,1,1') {
$crop = explode(',', $crop);

$file = MediaLibrary::instance()->get(post('filePath'));
$tempDirectory = temp_path().'/contenteditor';
$tempFilePath = temp_path().post('filePath');
File::makeDirectory($tempDirectory, 0777, true, true);

if (!File::put($tempFilePath, $file)) {
throw new SystemException('Error saving remote file to a temporary location.');
}

$width = floor(post('width') * $crop[3] - post('width') * $crop[1]);
$height = floor(post('height') * $crop[2] - post('height') * $crop[0]);

Resizer::open($tempFilePath)
->crop(
floor(post('width') * $crop[1]),
floor(post('height') * $crop[0]),
$width,
$height
)
->save($tempFilePath, 100);

$pathParts = pathinfo(post('filePath'));
$newFilePath = $pathParts['dirname'] . '/' . $pathParts['filename'] . '-c.' . $pathParts['extension'];

MediaLibrary::instance()->put($newFilePath, file_get_contents($tempFilePath));

$url = MediaLibrary::instance()->getPathUrl($newFilePath);
}

return Response::json([
'url' => $url,
'width' => $width,
'crop' => post('crop'),
'alt' => post('alt'),
'size' => [
$width,
$height
]
]);
}
}
18 changes: 18 additions & 0 deletions http/middleware/EditorPermissionsMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php namespace Samuell\ContentEditor\Http\Middleware;

use Closure;
use Samuell\ContentEditor\Models\Settings;
use Backend\Facades\BackendAuth;

class EditorPermissionsMiddleware
{
public function handle($request, Closure $next)
{
$backendUser = BackendAuth::getUser();
if ($backendUser && $backendUser->hasAccess(Settings::get('permissions', 'cms.manage_content'))) {
return $next($request);
}

return abort(404);
}
}
5 changes: 2 additions & 3 deletions models/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Settings extends Model

public function initSettingsData()
{
$this->additional_styles = File::get(plugins_path().'/samuell/contenteditor/assets/additional-css.css');
$this->additional_styles = File::get(plugins_path() . '/samuell/contenteditor/assets/additional-css.css');
}

// list of buttons
Expand Down Expand Up @@ -92,8 +92,7 @@ public static function renderCss()
try {
$customCss = self::compileCss();
Cache::forever(self::CACHE_KEY, $customCss);
}
catch (Exception $ex) {
} catch (Exception $ex) {
$customCss = '/* ' . $ex->getMessage() . ' */';
}
return $customCss;
Expand Down
Loading

0 comments on commit 5a8a2d9

Please sign in to comment.