Passed
Push — develop ( d2cc07...247245 )
by Andrew
02:40
created

RichVariables::installEventListeners()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 16
nc 3
nop 0
dl 0
loc 26
rs 8.5806
c 0
b 0
f 0
1
<?php
2
/**
3
 * Rich Variables plugin for Craft CMS 3.x
4
 *
5
 * Allows you to easily use Craft Globals as variables in Rich Text fields
6
 *
7
 * @link      https://nystudio107.com
8
 * @copyright Copyright (c) 2017 nystudio107
9
 */
10
11
namespace nystudio107\richvariables;
12
13
use nystudio107\richvariables\models\Settings;
14
use nystudio107\richvariables\assetbundles\richvariables\RichVariablesAsset;
15
16
use Craft;
17
use craft\base\Plugin;
18
use craft\redactor\events\RegisterPluginPathsEvent;
0 ignored issues
show
Bug introduced by
The type craft\redactor\events\RegisterPluginPathsEvent was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
use craft\redactor\Field as RichText;
0 ignored issues
show
Bug introduced by
The type craft\redactor\Field was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
use craft\services\Plugins;
21
22
use yii\base\Event;
23
24
use Composer\Semver\Comparator;
25
use yii\base\Exception;
26
use yii\base\InvalidConfigException;
27
28
/**
29
 * Class RichVariables
30
 *
31
 * @author    nystudio107
32
 * @package   RichVariables
33
 * @since     1.0.0
34
 */
35
class RichVariables extends Plugin
36
{
37
    // Static Properties
38
    // =========================================================================
39
40
    /**
41
     * @var RichVariables
42
     */
43
    public static $plugin;
44
45
    // Public Methods
46
    // =========================================================================
47
48
    /**
49
     * @inheritdoc
50
     */
51
    public function init()
52
    {
53
        parent::init();
54
        self::$plugin = $this;
55
56
        // Only load for AdminCP, non-console requests
57
        $request = Craft::$app->getRequest();
58
        if ($request->getIsCpRequest()) {
59
            // Handler: Plugins::EVENT_AFTER_LOAD_PLUGINS
60
            Event::on(
61
                Plugins::class,
62
                Plugins::EVENT_AFTER_LOAD_PLUGINS,
63
                function () {
64
                    // Add in our event listeners that are needed for every request
65
                    $this->installEventListeners();
66
                }
67
            );
68
        }
69
        Craft::info(
70
            Craft::t(
71
                'rich-variables',
72
                '{name} plugin loaded',
73
                ['name' => $this->name]
74
            ),
75
            __METHOD__
76
        );
77
    }
78
79
    // Protected Methods
80
    // =========================================================================
81
82
    protected function installEventListeners()
83
    {
84
        // Make sure the Redactor plugin is installed
85
        $redactor = Craft::$app->getPlugins()->getPlugin('redactor');
86
        if ($redactor) {
87
            // Event handler: RichText::EVENT_REGISTER_PLUGIN_PATHS
88
            Event::on(
89
                RichText::class,
90
                RichText::EVENT_REGISTER_PLUGIN_PATHS,
91
                function (RegisterPluginPathsEvent $event) {
92
                    /** @var Plugin $redactor */
93
                    $redactor = Craft::$app->getPlugins()->getPlugin('redactor');
94
                    $versionDir = 'v1/';
95
                    if (Comparator::greaterThanOrEqualTo($redactor->version, '2.0.0')) {
96
                        $versionDir = 'v2/';
97
                    }
98
                    // Add the path to our Redactor plugin
99
                    $src = Craft::getAlias('@nystudio107/richvariables/redactor/plugins/'.$versionDir);
100
                    $event->paths[] = $src;
101
                }
102
            );
103
            // Register our asset bundle
104
            try {
105
                Craft::$app->getView()->registerAssetBundle(RichVariablesAsset::class);
106
            } catch (InvalidConfigException $e) {
107
                Craft::error($e->getMessage(), __METHOD__);
108
            }
109
        }
110
    }
111
112
    /**
113
     * @inheritdoc
114
     */
115
    protected function createSettingsModel()
116
    {
117
        return new Settings();
118
    }
119
120
    /**
121
     * @inheritdoc
122
     */
123
    protected function settingsHtml(): string
124
    {
125
        // Get all of the globals sets
126
        $globalsHandles = [];
127
        $allGlobalsSets = Craft::$app->getGlobals()->getAllSets();
128
        foreach ($allGlobalsSets as $globalsSet) {
129
            $globalsHandles[$globalsSet->handle] = $globalsSet->name;
130
        }
131
132
        // Render our settings template
133
        try {
134
            return Craft::$app->view->renderTemplate(
135
                'rich-variables/settings',
136
                [
137
                    'settings' => $this->getSettings(),
138
                    'globalsSets' => $globalsHandles,
139
                ]
140
            );
141
        } catch (\Twig_Error_Loader $e) {
142
            Craft::error($e->getMessage(), __METHOD__);
143
        } catch (Exception $e) {
144
            Craft::error($e->getMessage(), __METHOD__);
145
        }
146
147
        return '';
148
    }
149
}
150