Completed
Push — master ( 60cec0...287fc9 )
by Antonio
15s
created

CKEditorTrait::registerKCFinder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 19
ccs 0
cts 14
cp 0
rs 9.4285
cc 1
eloc 11
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * @copyright Copyright (c) 2013-2016 2amigOS! Consulting Group LLC
4
 * @link http://2amigos.us
5
 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
6
 */
7
namespace dosamigos\ckeditor;
8
9
use Yii;
10
use yii\helpers\ArrayHelper;
11
12
/**
13
 * CKEditorTrait has common methods for both CKEditor and CKEditorInline widgets.
14
 *
15
 * @author Antonio Ramirez <[email protected]>
16
 * @link http://www.ramirezcobos.com/
17
 * @link http://www.2amigos.us/
18
 * @package dosamigos\ckeditor
19
 */
20
trait CKEditorTrait
21
{
22
    /**
23
     * @var string the toolbar preset. It can be any of the following:
24
     *
25
     * - basic: will load the configuration on presets/basic.php
26
     * - full: will load the configuration on presets/full.php
27
     * - standard: will load the configuration on presets/standard.php
28
     * - custom: configuration will be based on [[clientOptions]].
29
     *
30
     * Defaults to 'standard'. It is important to note that any configuration item of the loaded presets can be
31
     * overrided by [[clientOptions]]
32
     */
33
    public $preset = 'standard';
34
35
    /**
36
     * Enable or disable kcfinder
37
     * @link https://kcfinder.sunhater.com
38
     * @var boolean
39
     */
40
    public $kcfinder = false;
41
42
    /**
43
     * KCFinder dynamic settings (using session)
44
     * @link http://kcfinder.sunhater.com/install#dynamic
45
     * @var array
46
     */
47
    public $kcfOptions = [];
48
49
    /**
50
     * KCFinder default dynamic settings
51
     * @link http://kcfinder.sunhater.com/install#dynamic
52
     * @var array
53
     */
54
    public static $kcfDefaultOptions = [
55
        'disabled' => false,
56
        'uploadURL' => '@web/upload',
57
        'uploadDir' => '@webroot/upload',
58
        'denyZipDownload' => true,
59
        'denyUpdateCheck' => true,
60
        'denyExtensionRename' => true,
61
        'theme' => 'default',
62
        'access' => [ // @link http://kcfinder.sunhater.com/install#_access
63
            'files' => [
64
                'upload' => true,
65
                'delete' => true,
66
                'copy' => true,
67
                'move' => true,
68
                'rename' => true,
69
            ],
70
            'dirs' => [
71
                'create' => true,
72
                'delete' => true,
73
                'rename' => true,
74
            ],
75
        ],
76
        'types' => [  // @link http://kcfinder.sunhater.com/install#_types
77
            'files' => [
78
                'type' => '',
79
            ],
80
        ],
81
        'thumbsDir' => '.thumbs',
82
        'thumbWidth' => 100,
83
        'thumbHeight' => 100,
84
    ];
85
86
    /**
87
     * @var array the options for the CKEditor 4 JS plugin.
88
     * Please refer to the CKEditor 4 plugin Web page for possible options.
89
     * @see http://docs.ckeditor.com/#!/guide/dev_installation
90
     */
91
    public $clientOptions = [];
92
93
    /**
94
     * Initializes the widget options.
95
     * This method sets the default values for various options.
96
     */
97 9
    protected function initOptions()
98
    {
99 9
        if ($this->kcfinder) {
100
            $this->registerKCFinder();
101
        }
102
103 9
        $options = [];
104 9
        switch ($this->preset) {
105 9
            case 'custom':
106 1
                $preset = null;
107 1
                break;
108 9
            case 'basic':
109 9
            case 'full':
110 9
            case 'standard':
111 8
                $preset = 'presets/' . $this->preset . '.php';
112 8
                break;
113 1
            default:
114 1
                $preset = 'presets/standard.php';
115 9
        }
116 9
        if ($preset !== null) {
117 9
            $options = require($preset);
118 9
        }
119 9
        $this->clientOptions = ArrayHelper::merge($options, $this->clientOptions);
120 9
    }
121
122
    /**
123
     * Registers KCFinder (@link https://kcfinder.sunhater.com)
124
     * @author Nabi KaramAliZadeh <[email protected]
125
     * @link http://www.nabi.ir
126
     */
127
    protected function registerKCFinder()
128
    {
129
        $this->kcfOptions = array_merge(self::$kcfDefaultOptions, $this->kcfOptions);
130
131
        $this->kcfOptions['uploadURL'] = Yii::getAlias($this->kcfOptions['uploadURL']);
132
        $this->kcfOptions['uploadDir'] = Yii::getAlias($this->kcfOptions['uploadDir']);
133
134
        Yii::$app->session['KCFINDER'] = $this->kcfOptions;
135
136
        $register = KCFinderAsset::register($this->view);
0 ignored issues
show
Bug introduced by
The property view does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
137
        $kcfinderUrl = $register->baseUrl;
138
139
        $browseOptions = [
140
            'filebrowserBrowseUrl' => $kcfinderUrl . '/browse.php?opener=ckeditor&type=files',
141
            'filebrowserUploadUrl' => $kcfinderUrl . '/upload.php?opener=ckeditor&type=files',
142
        ];
143
144
        $this->clientOptions = ArrayHelper::merge($browseOptions, $this->clientOptions);
145
    }
146
}
147