Skip to content

Commit

Permalink
1.9.3: Support of latest October CMS V2, and V3. Small refactor of ex…
Browse files Browse the repository at this point in the history
…tender interface.
  • Loading branch information
FlusherDock1 committed May 8, 2022
1 parent a47791d commit 4085755
Show file tree
Hide file tree
Showing 7 changed files with 212 additions and 193 deletions.
57 changes: 57 additions & 0 deletions classes/event/AbstractFormExtender.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php namespace ReaZzon\Editor\Classes\Event;

use Backend\Widgets\Form;
use October\Rain\Events\Dispatcher;
use System\Classes\PluginManager;

abstract class AbstractFormExtender
{
protected $controllerClass;
protected $modelClass;

protected $fieldType;
protected $fieldWidgetPath;

public function subscribe(Dispatcher $event)
{
$this->controllerClass = $this->getControllerClass();
$this->modelClass = $this->getModelClass();

$this->fieldType = 'editorjs';
$this->fieldWidgetPath = 'ReaZzon\Editor\FormWidgets\EditorJS';

if (PluginManager::instance()->hasPlugin('RainLab.Translate')
&& !PluginManager::instance()->isDisabled('RainLab.Translate')) {
$this->fieldType = 'mleditorjs';
$this->fieldWidgetPath = 'ReaZzon\Editor\FormWidgets\MLEditorJS';
}

if ($this->isEnabled()) {
$event->listen('backend.form.extendFields', function (Form $widget) {

if (!$widget->getController() instanceof $this->controllerClass) {
return;
}

if (!$widget->model instanceof $this->modelClass) {
return;
}

$this->replaceField($widget);

});

$this->extendModel();
}
}

abstract protected function replaceField(Form $widget);

abstract protected function extendModel();

abstract protected function getControllerClass();

abstract protected function getModelClass();

abstract protected function isEnabled();
}
79 changes: 34 additions & 45 deletions classes/event/ExtendIndicatorNews.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace ReaZzon\Editor\Classes\Event;

use Backend\Widgets\Form;
use System\Classes\PluginManager;
use ReaZzon\Editor\Models\Settings;

Expand All @@ -8,57 +9,45 @@
* @package ReaZzon\Editor\Classes\Event
* @author Nick Khaetsky, [email protected]
*/
class ExtendIndicatorNews
class ExtendIndicatorNews extends AbstractFormExtender
{
/**
* Add listeners
* @param \Illuminate\Events\Dispatcher $event
*/
public function subscribe($event)
protected function replaceField(Form $widget)
{
if (Settings::get('integration_indikator_news', false) &&
PluginManager::instance()->hasPlugin('Indikator.News')) {

$event->listen('backend.form.extendFields', function ($widget) {

// Only for Indikator.News Posts controller
if (!$widget->getController() instanceof \Indikator\News\Controllers\Posts) {
return;
}

// Only for Indikator.News Post model
if (!$widget->model instanceof \Indikator\News\Models\Posts) {
return;
}

$fieldType = 'editorjs';
$fieldWidgetPath = 'ReaZzon\Editor\FormWidgets\EditorJS';
if ($field = $widget->getField('content')) {
$field->displayAs($this->fieldWidgetPath);
$field->stretch = true;
}
}

if (PluginManager::instance()->hasPlugin('RainLab.Translate')
&& !PluginManager::instance()->isDisabled('RainLab.Translate')) {
$fieldType = 'mleditorjs';
$fieldWidgetPath = 'ReaZzon\Editor\FormWidgets\MLEditorJS';
}
protected function extendModel()
{
\Indikator\News\Models\Posts::extend(function ($model) {
/** @var \October\Rain\Database\Model $model */
$model->implement[] = 'ReaZzon.Editor.Behaviors.ConvertToHtml';

// Finding content field and changing it's type regardless whatever it already is.
foreach ($widget->getFields() as $field) {
if ($field->fieldName === 'content') {
$field->config['type'] = $fieldType;
$field->config['widget'] = $fieldWidgetPath;
$field->config['stretch'] = true;
}
}
$model->addDynamicMethod('getContentRenderAttribute', function () use ($model) {
return $model->convertJsonToHtml($model->getAttribute('content'));
});
});
}

// Replacing original content_render attribute.
\Indikator\News\Models\Posts::extend(function ($model) {
/** @var \October\Rain\Database\Model $model */
$model->implement[] = 'ReaZzon.Editor.Behaviors.ConvertToHtml';
protected function getControllerClass()
{
return \Indikator\News\Controllers\Posts::class;
}

$model->addDynamicMethod('getContentRenderAttribute', function () use ($model) {
return $model->convertJsonToHtml($model->getAttribute('content'));
});
});
protected function getModelClass()
{
return \Indikator\News\Models\Posts::class;
}

protected function isEnabled()
{
if (Settings::get('integration_indikator_news', false) &&
PluginManager::instance()->hasPlugin('Indikator.News')) {
return true;
}

return false;
}
}
}
79 changes: 34 additions & 45 deletions classes/event/ExtendLovataGoodNews.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace ReaZzon\Editor\Classes\Event;

use Backend\Widgets\Form;
use System\Classes\PluginManager;
use ReaZzon\Editor\Models\Settings;

Expand All @@ -8,57 +9,45 @@
* @package ReaZzon\Editor\Classes\Event
* @author Nick Khaetsky, [email protected]
*/
class ExtendLovataGoodNews
class ExtendLovataGoodNews extends AbstractFormExtender
{
/**
* Add listeners
* @param \Illuminate\Events\Dispatcher $event
*/
public function subscribe($event)
protected function replaceField(Form $widget)
{
if (Settings::get('integration_good_news', false) &&
PluginManager::instance()->hasPlugin('Lovata.GoodNews')) {

$event->listen('backend.form.extendFields', function ($widget) {

// Only for Lovata.GoodNews Articles controller
if (!$widget->getController() instanceof \Lovata\GoodNews\Controllers\Articles) {
return;
}

// Only for Lovata.GoodNews Article model
if (!$widget->model instanceof \Lovata\GoodNews\Models\Article) {
return;
}

$fieldType = 'editorjs';
$fieldWidgetPath = 'ReaZzon\Editor\FormWidgets\EditorJS';
if ($field = $widget->getField('content')) {
$field->displayAs($this->fieldWidgetPath);
$field->stretch = true;
}
}

if (PluginManager::instance()->hasPlugin('RainLab.Translate')
&& !PluginManager::instance()->isDisabled('RainLab.Translate')) {
$fieldType = 'mleditorjs';
$fieldWidgetPath = 'ReaZzon\Editor\FormWidgets\MLEditorJS';
}
protected function extendModel()
{
\Lovata\GoodNews\Classes\Item\ArticleItem::extend(function ($elementItem) {
/** @var \October\Rain\Database\Model $elementItem */
$elementItem->implement[] = 'ReaZzon.Editor.Behaviors.ConvertToHtml';

// Finding content field and changing it's type regardless whatever it already is.
foreach ($widget->getFields() as $field) {
if ($field->fieldName === 'content') {
$field->config['type'] = $fieldType;
$field->config['widget'] = $fieldWidgetPath;
$field->config['stretch'] = true;
}
}
$elementItem->addDynamicMethod('getContentAttribute', function () use ($elementItem) {
return $elementItem->convertJsonToHtml($elementItem->getAttribute('content'));
});
});
}

// Replacing original content attribute.
\Lovata\GoodNews\Classes\Item\ArticleItem::extend(function ($elementItem) {
/** @var \October\Rain\Database\Model $elementItem */
$elementItem->implement[] = 'ReaZzon.Editor.Behaviors.ConvertToHtml';
protected function getControllerClass()
{
return \Lovata\GoodNews\Controllers\Articles::class;
}

$elementItem->addDynamicMethod('getContentAttribute', function () use ($elementItem) {
return $elementItem->convertJsonToHtml($elementItem->getAttribute('content'));
});
});
protected function getModelClass()
{
return \Lovata\GoodNews\Models\Article::class;
}

protected function isEnabled()
{
if (Settings::get('integration_good_news', false) &&
PluginManager::instance()->hasPlugin('Lovata.GoodNews')) {
return true;
}

return false;
}
}
}
87 changes: 38 additions & 49 deletions classes/event/ExtendRainLabBlog.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace ReaZzon\Editor\Classes\Event;

use Backend\Widgets\Form;
use System\Classes\PluginManager;
use ReaZzon\Editor\Models\Settings;

Expand All @@ -8,65 +9,53 @@
* @package ReaZzon\Editor\Classes\Event
* @author Nick Khaetsky, [email protected]
*/
class ExtendRainLabBlog
class ExtendRainLabBlog extends AbstractFormExtender
{
/**
* Add listeners
* @param \Illuminate\Events\Dispatcher $event
*/
public function subscribe($event)
protected function replaceField(Form $widget)
{
if (Settings::get('integration_blog', false) &&
PluginManager::instance()->hasPlugin('RainLab.Blog')) {

$event->listen('backend.form.extendFields', function ($widget) {

// Only for RainLab.Blog Posts controller
if (!$widget->getController() instanceof \RainLab\Blog\Controllers\Posts) {
return;
}

// Only for RainLab.Blog Post model
if (!$widget->model instanceof \RainLab\Blog\Models\Post) {
return;
}
if ($field = $widget->getField('content')) {
$field->displayAs($this->fieldWidgetPath);
$field->stretch = true;
}
}

$fieldType = 'editorjs';
$fieldWidgetPath = 'ReaZzon\Editor\FormWidgets\EditorJS';
protected function extendModel()
{
// Replacing original content_html attribute.
$this->modelClass::extend(function ($model) {
$model->implement[] = 'ReaZzon.Editor.Behaviors.ConvertToHtml';

if (PluginManager::instance()->hasPlugin('RainLab.Translate')
&& !PluginManager::instance()->isDisabled('RainLab.Translate')) {
$fieldType = 'mleditorjs';
$fieldWidgetPath = 'ReaZzon\Editor\FormWidgets\MLEditorJS';
$model->bindEvent('model.getAttribute', function ($attribute, $value) use ($model) {
if ($attribute == 'content_html') {
return $model->convertJsonToHtml($model->getAttribute('content'));
}
});

// Finding content field and changing its type regardless whatever it already is.
foreach ($widget->getFields() as $field) {
if ($field->fieldName === 'content') {
$field->config['type'] = $fieldType;
$field->config['widget'] = $fieldWidgetPath;
$field->config['stretch'] = true;
}
}
$model->bindEvent('model.translate.resolveComputedFields', function ($locale) use ($model) {
return [
'content_html' => $model->convertJsonToHtml($model->asExtension('TranslatableModel')->getAttributeTranslated('content', $locale))
];
});
});
}

// Replacing original content_html attribute.
\RainLab\Blog\Models\Post::extend(function ($model) {
/** @var \October\Rain\Database\Model $model */
$model->implement[] = 'ReaZzon.Editor.Behaviors.ConvertToHtml';
protected function getControllerClass()
{
return \RainLab\Blog\Controllers\Posts::class;
}

$model->bindEvent('model.getAttribute', function ($attribute, $value) use ($model) {
if ($attribute == 'content_html') {
return $model->convertJsonToHtml($model->getAttribute('content'));
}
});
protected function getModelClass()
{
return \RainLab\Blog\Models\Post::class;
}

$model->bindEvent('model.translate.resolveComputedFields', function ($locale) use ($model) {
return [
'content_html' => $model->convertJsonToHtml($model->asExtension('TranslatableModel')->getAttributeTranslated('content', $locale))
];
});
});
protected function isEnabled()
{
if (Settings::get('integration_blog', false) &&
PluginManager::instance()->hasPlugin('RainLab.Blog')) {
return true;
}

return false;
}
}
Loading

0 comments on commit 4085755

Please sign in to comment.