Skip to content

Commit 4085755

Browse files
committed
1.9.3: Support of latest October CMS V2, and V3. Small refactor of extender interface.
1 parent a47791d commit 4085755

7 files changed

Lines changed: 212 additions & 193 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php namespace ReaZzon\Editor\Classes\Event;
2+
3+
use Backend\Widgets\Form;
4+
use October\Rain\Events\Dispatcher;
5+
use System\Classes\PluginManager;
6+
7+
abstract class AbstractFormExtender
8+
{
9+
protected $controllerClass;
10+
protected $modelClass;
11+
12+
protected $fieldType;
13+
protected $fieldWidgetPath;
14+
15+
public function subscribe(Dispatcher $event)
16+
{
17+
$this->controllerClass = $this->getControllerClass();
18+
$this->modelClass = $this->getModelClass();
19+
20+
$this->fieldType = 'editorjs';
21+
$this->fieldWidgetPath = 'ReaZzon\Editor\FormWidgets\EditorJS';
22+
23+
if (PluginManager::instance()->hasPlugin('RainLab.Translate')
24+
&& !PluginManager::instance()->isDisabled('RainLab.Translate')) {
25+
$this->fieldType = 'mleditorjs';
26+
$this->fieldWidgetPath = 'ReaZzon\Editor\FormWidgets\MLEditorJS';
27+
}
28+
29+
if ($this->isEnabled()) {
30+
$event->listen('backend.form.extendFields', function (Form $widget) {
31+
32+
if (!$widget->getController() instanceof $this->controllerClass) {
33+
return;
34+
}
35+
36+
if (!$widget->model instanceof $this->modelClass) {
37+
return;
38+
}
39+
40+
$this->replaceField($widget);
41+
42+
});
43+
44+
$this->extendModel();
45+
}
46+
}
47+
48+
abstract protected function replaceField(Form $widget);
49+
50+
abstract protected function extendModel();
51+
52+
abstract protected function getControllerClass();
53+
54+
abstract protected function getModelClass();
55+
56+
abstract protected function isEnabled();
57+
}
Lines changed: 34 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php namespace ReaZzon\Editor\Classes\Event;
22

3+
use Backend\Widgets\Form;
34
use System\Classes\PluginManager;
45
use ReaZzon\Editor\Models\Settings;
56

@@ -8,57 +9,45 @@
89
* @package ReaZzon\Editor\Classes\Event
910
* @author Nick Khaetsky, nick@reazzon.ru
1011
*/
11-
class ExtendIndicatorNews
12+
class ExtendIndicatorNews extends AbstractFormExtender
1213
{
13-
/**
14-
* Add listeners
15-
* @param \Illuminate\Events\Dispatcher $event
16-
*/
17-
public function subscribe($event)
14+
protected function replaceField(Form $widget)
1815
{
19-
if (Settings::get('integration_indikator_news', false) &&
20-
PluginManager::instance()->hasPlugin('Indikator.News')) {
21-
22-
$event->listen('backend.form.extendFields', function ($widget) {
23-
24-
// Only for Indikator.News Posts controller
25-
if (!$widget->getController() instanceof \Indikator\News\Controllers\Posts) {
26-
return;
27-
}
28-
29-
// Only for Indikator.News Post model
30-
if (!$widget->model instanceof \Indikator\News\Models\Posts) {
31-
return;
32-
}
33-
34-
$fieldType = 'editorjs';
35-
$fieldWidgetPath = 'ReaZzon\Editor\FormWidgets\EditorJS';
16+
if ($field = $widget->getField('content')) {
17+
$field->displayAs($this->fieldWidgetPath);
18+
$field->stretch = true;
19+
}
20+
}
3621

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

43-
// Finding content field and changing it's type regardless whatever it already is.
44-
foreach ($widget->getFields() as $field) {
45-
if ($field->fieldName === 'content') {
46-
$field->config['type'] = $fieldType;
47-
$field->config['widget'] = $fieldWidgetPath;
48-
$field->config['stretch'] = true;
49-
}
50-
}
28+
$model->addDynamicMethod('getContentRenderAttribute', function () use ($model) {
29+
return $model->convertJsonToHtml($model->getAttribute('content'));
5130
});
31+
});
32+
}
5233

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

58-
$model->addDynamicMethod('getContentRenderAttribute', function () use ($model) {
59-
return $model->convertJsonToHtml($model->getAttribute('content'));
60-
});
61-
});
39+
protected function getModelClass()
40+
{
41+
return \Indikator\News\Models\Posts::class;
42+
}
43+
44+
protected function isEnabled()
45+
{
46+
if (Settings::get('integration_indikator_news', false) &&
47+
PluginManager::instance()->hasPlugin('Indikator.News')) {
48+
return true;
6249
}
50+
51+
return false;
6352
}
64-
}
53+
}
Lines changed: 34 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php namespace ReaZzon\Editor\Classes\Event;
22

3+
use Backend\Widgets\Form;
34
use System\Classes\PluginManager;
45
use ReaZzon\Editor\Models\Settings;
56

@@ -8,57 +9,45 @@
89
* @package ReaZzon\Editor\Classes\Event
910
* @author Nick Khaetsky, nick@reazzon.ru
1011
*/
11-
class ExtendLovataGoodNews
12+
class ExtendLovataGoodNews extends AbstractFormExtender
1213
{
13-
/**
14-
* Add listeners
15-
* @param \Illuminate\Events\Dispatcher $event
16-
*/
17-
public function subscribe($event)
14+
protected function replaceField(Form $widget)
1815
{
19-
if (Settings::get('integration_good_news', false) &&
20-
PluginManager::instance()->hasPlugin('Lovata.GoodNews')) {
21-
22-
$event->listen('backend.form.extendFields', function ($widget) {
23-
24-
// Only for Lovata.GoodNews Articles controller
25-
if (!$widget->getController() instanceof \Lovata\GoodNews\Controllers\Articles) {
26-
return;
27-
}
28-
29-
// Only for Lovata.GoodNews Article model
30-
if (!$widget->model instanceof \Lovata\GoodNews\Models\Article) {
31-
return;
32-
}
33-
34-
$fieldType = 'editorjs';
35-
$fieldWidgetPath = 'ReaZzon\Editor\FormWidgets\EditorJS';
16+
if ($field = $widget->getField('content')) {
17+
$field->displayAs($this->fieldWidgetPath);
18+
$field->stretch = true;
19+
}
20+
}
3621

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

43-
// Finding content field and changing it's type regardless whatever it already is.
44-
foreach ($widget->getFields() as $field) {
45-
if ($field->fieldName === 'content') {
46-
$field->config['type'] = $fieldType;
47-
$field->config['widget'] = $fieldWidgetPath;
48-
$field->config['stretch'] = true;
49-
}
50-
}
28+
$elementItem->addDynamicMethod('getContentAttribute', function () use ($elementItem) {
29+
return $elementItem->convertJsonToHtml($elementItem->getAttribute('content'));
5130
});
31+
});
32+
}
5233

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

58-
$elementItem->addDynamicMethod('getContentAttribute', function () use ($elementItem) {
59-
return $elementItem->convertJsonToHtml($elementItem->getAttribute('content'));
60-
});
61-
});
39+
protected function getModelClass()
40+
{
41+
return \Lovata\GoodNews\Models\Article::class;
42+
}
43+
44+
protected function isEnabled()
45+
{
46+
if (Settings::get('integration_good_news', false) &&
47+
PluginManager::instance()->hasPlugin('Lovata.GoodNews')) {
48+
return true;
6249
}
50+
51+
return false;
6352
}
64-
}
53+
}
Lines changed: 38 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php namespace ReaZzon\Editor\Classes\Event;
22

3+
use Backend\Widgets\Form;
34
use System\Classes\PluginManager;
45
use ReaZzon\Editor\Models\Settings;
56

@@ -8,65 +9,53 @@
89
* @package ReaZzon\Editor\Classes\Event
910
* @author Nick Khaetsky, nick@reazzon.ru
1011
*/
11-
class ExtendRainLabBlog
12+
class ExtendRainLabBlog extends AbstractFormExtender
1213
{
13-
/**
14-
* Add listeners
15-
* @param \Illuminate\Events\Dispatcher $event
16-
*/
17-
public function subscribe($event)
14+
protected function replaceField(Form $widget)
1815
{
19-
if (Settings::get('integration_blog', false) &&
20-
PluginManager::instance()->hasPlugin('RainLab.Blog')) {
21-
22-
$event->listen('backend.form.extendFields', function ($widget) {
23-
24-
// Only for RainLab.Blog Posts controller
25-
if (!$widget->getController() instanceof \RainLab\Blog\Controllers\Posts) {
26-
return;
27-
}
28-
29-
// Only for RainLab.Blog Post model
30-
if (!$widget->model instanceof \RainLab\Blog\Models\Post) {
31-
return;
32-
}
16+
if ($field = $widget->getField('content')) {
17+
$field->displayAs($this->fieldWidgetPath);
18+
$field->stretch = true;
19+
}
20+
}
3321

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

37-
if (PluginManager::instance()->hasPlugin('RainLab.Translate')
38-
&& !PluginManager::instance()->isDisabled('RainLab.Translate')) {
39-
$fieldType = 'mleditorjs';
40-
$fieldWidgetPath = 'ReaZzon\Editor\FormWidgets\MLEditorJS';
28+
$model->bindEvent('model.getAttribute', function ($attribute, $value) use ($model) {
29+
if ($attribute == 'content_html') {
30+
return $model->convertJsonToHtml($model->getAttribute('content'));
4131
}
32+
});
4233

43-
// Finding content field and changing its type regardless whatever it already is.
44-
foreach ($widget->getFields() as $field) {
45-
if ($field->fieldName === 'content') {
46-
$field->config['type'] = $fieldType;
47-
$field->config['widget'] = $fieldWidgetPath;
48-
$field->config['stretch'] = true;
49-
}
50-
}
34+
$model->bindEvent('model.translate.resolveComputedFields', function ($locale) use ($model) {
35+
return [
36+
'content_html' => $model->convertJsonToHtml($model->asExtension('TranslatableModel')->getAttributeTranslated('content', $locale))
37+
];
5138
});
39+
});
40+
}
5241

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

58-
$model->bindEvent('model.getAttribute', function ($attribute, $value) use ($model) {
59-
if ($attribute == 'content_html') {
60-
return $model->convertJsonToHtml($model->getAttribute('content'));
61-
}
62-
});
47+
protected function getModelClass()
48+
{
49+
return \RainLab\Blog\Models\Post::class;
50+
}
6351

64-
$model->bindEvent('model.translate.resolveComputedFields', function ($locale) use ($model) {
65-
return [
66-
'content_html' => $model->convertJsonToHtml($model->asExtension('TranslatableModel')->getAttributeTranslated('content', $locale))
67-
];
68-
});
69-
});
52+
protected function isEnabled()
53+
{
54+
if (Settings::get('integration_blog', false) &&
55+
PluginManager::instance()->hasPlugin('RainLab.Blog')) {
56+
return true;
7057
}
58+
59+
return false;
7160
}
7261
}

0 commit comments

Comments
 (0)