Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuell1 committed Jul 10, 2022
2 parents 4d1a85e + af1ae16 commit 5d5a1ae
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 64 deletions.
4 changes: 3 additions & 1 deletion Plugin.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php namespace Samuell\ContentEditor;
<?php

namespace Samuell\ContentEditor;

use System\Classes\PluginBase;

Expand Down
21 changes: 14 additions & 7 deletions components/ContentEditor.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
<?php namespace Samuell\ContentEditor\Components;
<?php

namespace Samuell\ContentEditor\Components;

use Cache;
use File;
use BackendAuth;
use Cms\Classes\Content;
use Cms\Classes\ComponentBase;
use RainLab\Translate\Classes\Translator;

use Samuell\ContentEditor\Models\Settings;

Expand Down Expand Up @@ -73,6 +76,9 @@ public function onRun()
$this->addCss('assets/contenteditor.css');
$this->addJs('assets/content-tools.min.js');
$this->addJs('assets/contenteditor.js');

// Add scripts only once
$this->renderPartial('@scripts.htm');
}
}

Expand Down Expand Up @@ -125,7 +131,7 @@ public function getFile()
{
if (Content::load($this->getTheme(), $this->file)) {
return $this->renderContent($this->file);
} else if (Content::load($this->getTheme(), $this->defaultFile)) { // if no locale file exists -> render the default, without language suffix
} elseif (Content::load($this->getTheme(), $this->defaultFile)) { // if no locale file exists -> render the default, without language suffix
return $this->renderContent($this->defaultFile);
}

Expand All @@ -144,17 +150,17 @@ public function setFile($file)

public function setTranslateFile($file)
{
$translate = \RainLab\Translate\Classes\Translator::instance();
$translate = Translator::instance();
$defaultLocale = $translate->getDefaultLocale();
$locale = $translate->getLocale();

// Compability with Rainlab.StaticPage
// StaticPages content does not append default locale to file.
if ($this->fileExists($file) && $locale === $defaultLocale) {
return $file;
return $file;
}

return substr_replace($file, '.'.$locale, strrpos($file, '.'), 0);
return substr_replace($file, '.' . $locale, strrpos($file, '.'), 0);
}

public function checkEditor()
Expand All @@ -163,12 +169,13 @@ public function checkEditor()
return $backendUser && $backendUser->hasAccess('samuell.contenteditor.editor');
}

public function fileExists($file) {
public function fileExists($file)
{
return File::exists((new Content)->getFilePath($file));
}

public function translateExists()
{
return class_exists('\RainLab\Translate\Classes\Translator');
return class_exists(Translator::class);
}
}
29 changes: 0 additions & 29 deletions components/contenteditor/default.htm
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,3 @@
{% if __SELF__.class %}class="{{ __SELF__.class }}"{% endif %}>
{{ __SELF__.content|raw }}
</{% if __SELF__.fixture %}{{ __SELF__.fixture }}{% else %}div{% endif %}>

{% if __SELF__.renderCount == 1 %}
{% put scripts %}
<script type="text/javascript">
/* CONTENT EDITOR SCRIPT START */
ContentTools.StylePalette.add([
{% for style in __SELF__.palettes %}
new ContentTools.Style('{{ style.name ? style.name : style.class }}', '{{ style.class }}', {{ style.allowed_tags|json_encode()|raw }}),
{% endfor %}
]);
editor.toolbox().tools([
[
{% for value in __SELF__.buttons %}
'{{ value }}',
{% endfor %}
],
[
'undo',
'redo',
'remove'
]
]);
/* CONTENT EDITOR SCRIPT END */
</script>
{% endput %}
{% put styles %}
<link rel="stylesheet" href="/contenteditor/styles">
{% endput %}
{% endif %}
1 change: 0 additions & 1 deletion components/contenteditor/render.htm
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

{% if __SELF__.fixture %}<{{ __SELF__.fixture }} {% if __SELF__.class %}class="{{ __SELF__.class }}"{% endif %}>{% endif %}
{{ content|raw }}
{% if __SELF__.fixture %}</{{ __SELF__.fixture }}>{% endif %}
Expand Down
26 changes: 26 additions & 0 deletions components/contenteditor/scripts.htm
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{% put scripts %}
<script type="text/javascript">
/* CONTENT EDITOR SCRIPT START */
ContentTools.StylePalette.add([
{% for style in __SELF__.palettes %}
new ContentTools.Style('{{ style.name ? style.name : style.class }}', '{{ style.class }}', {{ style.allowed_tags|json_encode()|raw }}),
{% endfor %}
]);
editor.toolbox().tools([
[
{% for value in __SELF__.buttons %}
'{{ value }}',
{% endfor %}
],
[
'undo',
'redo',
'remove'
]
]);
/* CONTENT EDITOR SCRIPT END */
</script>
{% endput %}
{% put styles %}
<link rel="stylesheet" href="/contenteditor/styles">
{% endput %}
4 changes: 3 additions & 1 deletion http/controllers/AdditionalStylesController.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php namespace Samuell\ContentEditor\Http\Controllers;
<?php

namespace Samuell\ContentEditor\Http\Controllers;

use Response;
use Illuminate\Routing\Controller;
Expand Down
37 changes: 20 additions & 17 deletions http/controllers/ImageController.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
<?php namespace Samuell\ContentEditor\Http\Controllers;
<?php

namespace Samuell\ContentEditor\Http\Controllers;

use File;
use Lang;
use Input;
use Response;
use Exception;
use SystemException;
use ApplicationException;
use Cms\Classes\MediaLibrary;
use Media\Classes\MediaLibrary;
use October\Rain\Resize\Resizer;
use Illuminate\Routing\Controller;
use October\Rain\Database\Attach\Resizer;
use Samuell\ContentEditor\Models\Settings;
use October\Rain\Filesystem\Definitions as FileDefinitions;

/**
* ImageController
*
*
* Handle content editor image upload
*/
class ImageController extends Controller
Expand All @@ -32,7 +36,7 @@ public function upload()
* Convert uppcare case file extensions to lower case
*/
$extension = strtolower($uploadedFile->getClientOriginalExtension());
$fileName = File::name($fileName).'.'.$extension;
$fileName = File::name($fileName) . '.' . $extension;

/*
* File name contains non-latin characters, attempt to slug the value
Expand All @@ -54,29 +58,28 @@ public function upload()
$path = MediaLibrary::validatePath($path);

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

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

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

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

}

public function save()
Expand All @@ -86,14 +89,14 @@ public function save()
$width = post('width');
$height = post('height');
$filePath = post('filePath');
$relativeFilePath = config('cms.storage.media.path').$filePath;
$relativeFilePath = config('cms.storage.media.path', config('system.storage.media.path')) . $filePath;

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');
$tempDirectory = temp_path() . '/contenteditor';
$tempFilePath = temp_path() . post('filePath');
File::makeDirectory($tempDirectory, 0777, true, true);

if (!File::put($tempFilePath, $file)) {
Expand Down
9 changes: 6 additions & 3 deletions models/Settings.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php namespace Samuell\ContentEditor\Models;
<?php

namespace Samuell\ContentEditor\Models;

use File;
use Lang;
use Model;
use Less_Parser;
use Cache;
Expand All @@ -19,7 +20,7 @@ class Settings extends Model
public function initSettingsData()
{
$this->additional_styles = File::get(plugins_path() . '/samuell/contenteditor/assets/additional-css.css');
$this->enabled_buttons = ["bold", "italic", "link", "align-left", "align-center", "align-right", "heading", "subheading", "subheading3", "subheading4", "subheading5", "paragraph", "unordered-list", "ordered-list", "table", "indent", "unindent", "line-break", "image", "video", "preformatted"];
$this->enabled_buttons = ['bold', 'italic', 'link', 'align-left', 'align-center', 'align-right', 'heading', 'subheading', 'subheading3', 'subheading4', 'subheading5', 'paragraph', 'unordered-list', 'ordered-list', 'table', 'indent', 'unindent', 'line-break', 'image', 'video', 'preformatted'];
}

// list of buttons
Expand Down Expand Up @@ -91,12 +92,14 @@ public static function renderCss()
if (Cache::has(self::CACHE_KEY)) {
return Cache::get(self::CACHE_KEY);
}

try {
$customCss = self::compileCss();
Cache::forever(self::CACHE_KEY, $customCss);
} catch (Exception $ex) {
$customCss = '/* ' . $ex->getMessage() . ' */';
}

return $customCss;
}

Expand Down
13 changes: 8 additions & 5 deletions routes.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
<?php

use Samuell\ContentEditor\Http\Middleware\EditorPermissionsMiddleware;
use Samuell\ContentEditor\Http\Controllers\ImageController;
use Samuell\ContentEditor\Http\Controllers\AdditionalStylesController;

Route::group(['prefix' => 'contenteditor'], function () {

Route::middleware(['web', EditorPermissionsMiddleware::class])->group(function () {
Route::post('image/upload', 'Samuell\ContentEditor\Http\Controllers\ImageController@upload');
Route::post('image/save', 'Samuell\ContentEditor\Http\Controllers\ImageController@save');
});
Route::middleware(['web', EditorPermissionsMiddleware::class])
->group(function () {
Route::post('image/upload', [ImageController::class, 'upload']);
Route::post('image/save', [ImageController::class, 'save']);
});

// Additional styles route
Route::get('styles', 'Samuell\ContentEditor\Http\Controllers\AdditionalStylesController@render');
Route::get('styles', [AdditionalStylesController::class, 'render']);
});
2 changes: 2 additions & 0 deletions updates/version.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,5 @@
1.3.3:
- Allow only image extensions for upload
- Fix italic tag name
1.3.4:
- Small fixes & code cleanup

0 comments on commit 5d5a1ae

Please sign in to comment.