Passed
Push — 2.6.0 ( ...f22176 )
by steve
18:56
created

Wysiwyg   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 12
eloc 41
dl 0
loc 112
rs 10
c 1
b 1
f 0
ccs 0
cts 66
cp 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getComponentDetails() 0 3 1
A getValueDisplay() 0 6 2
A registerScripts() 0 3 1
A getValue() 0 7 1
A getProperties() 0 6 1
A getFilterField() 0 3 1
A getConfigSets() 0 33 5
1
<?php
2
/**
3
 * @link http://www.newicon.net/neon
4
 * @copyright Copyright (c) 2016 Newicon Ltd
5
 * @license http://www.newicon.net/neon/license
6
 */
7
8
namespace neon\core\form\fields;
9
10
use neon\core\form\assets\CkeditorAsset;
11
use neon\core\helpers\Html;
12
use neon\core\helpers\Arr;
13
use neon\core\helpers\Str;
14
use yii\helpers\HtmlPurifier;
15
16
/**
17
 * Class Wysiwyg
18
 *
19
 * The wysiwyg components can be extended by setting
20
 * FILL IN SOMETHING ABOUT HOW TO EXTEND THE PLUGINS
21
 *
22
 * @package neon\core\form
23
 */
24
class Wysiwyg extends Textarea
25
{
26
	/**
27
	 * The DDS data type to store the value of the field
28
	 * @var string
29
	 */
30
	public $ddsDataType = 'textlong';
31
32
	public $type = 'text';
33
34
	/**
35
	 * If this is true users will have access to firefly's media browser
36
	 * - the media browser expects neon-administrator
37
	 * @var bool
38
	 */
39
	public $useMediaBrowser = false;
40
41
	/**
42
	 * @var string
43
	 */
44
	protected $allowableTags = "<p><a><em><strong><b><i><u><strike><cite><code><ul><ol><li><dl><dt><dd><img><table><tbody><tr><td><th><h1><h2><h3><h4><h5><h6><blockquote><hr><span><br><sup><sub><div><mark><pre>";
45
46
	/**
47
	 * @inheritdoc
48
	 */
49
	public function registerScripts($view)
50
	{
51
		CkeditorAsset::register($view);
52
	}
53
54
	public function getFilterField()
55
	{
56
		return ['class' => 'text'];
57
	}
58
59
	public function getProperties()
60
	{
61
		$properties = parent::getProperties();
62
		$properties[] = 'configSets';
63
		$properties[] = 'useMediaBrowser';
64
		return $properties;
65
	}
66
67
	public function getConfigSets()
68
	{
69
		$defaultConfigSets = [
70
			'simple' => [
71
				'toolbar' =>  [
72
					[
73
						'name' => 'paragraph',
74
						'items' => [ 'Bold', 'Italic', 'Underline', 'Link', 'Unlink', 'EmojiPanel', 'Source' ]
75
					]
76
				]
77
			],
78
			'default' => [
79
				'toolbar' =>  [
80
					[ 'name' => 'paragraph', 'items' => [ 'Format', 'Bold', 'Italic', 'Underline', '-', 'Superscript', 'Subscript', '-', 'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock','-', 'NumberedList', 'BulletedList', 'Outdent', 'Indent', 'Blockquote'] ],
81
					[ 'name' => 'links', 'items' => [ 'Link', 'Unlink', 'Anchor', '-', 'EmojiPanel' ] ],
82
					[ 'name' => 'insert', 'items' => [
83
						'Image', ($this->useMediaBrowser ? 'Fireflyimage' : 'SimpleImageUpload'), '-', 'Table', 'SpecialChar' ] ],
84
					[ 'name' => 'document', 'items' => ['Maximize' ] ],
85
					[ 'name' => 'source', 'items' => [ 'RemoveFormat', '-', 'Source' ] ]
86
				],
87
				'extraPlugins' => [($this->useMediaBrowser ? 'fireflyimage' : 'simpleImageUpload')],
88
				'extraAllowedContent'=>'img[srcset,sizes,loading]'
89
			]
90
		];
91
92
		$additionalConfigSets = Arr::get(neon('core')->classConfig, '\neon\core\form\fields\Wysiwyg.configSets', []);
0 ignored issues
show
Bug Best Practice introduced by
The property classConfig does not exist on neon\core\ApplicationWeb. Since you implemented __get, consider adding a @property annotation.
Loading history...
93
		$fullConfig = array_merge_recursive($defaultConfigSets, $additionalConfigSets);
94
		// convert the extraPlugins into a comma separated list as required by ckeditor
95
		foreach ($fullConfig as $set=>$config) {
96
			if (isset($config['extraPlugins']))
97
				$fullConfig[$set]['extraPlugins'] = implode(',', $config['extraPlugins']);
98
		}
99
		return json_encode($fullConfig);
100
	}
101
102
103
	/**
104
	 * @inheritdoc
105
	 */
106
	public function getValue()
107
	{
108
		return trim(HtmlPurifier::process($this->_value, function($config) {
109
			$def = $config->getHTMLDefinition(true);
110
			$def->addAttribute('img', 'srcset', 'Text');
111
			$def->addAttribute('img', 'sizes', 'Text');
112
			$def->addAttribute('img', 'loading', 'Text');
113
		}));
114
	}
115
116
	/**
117
	 * @inheritdoc
118
	 *
119
	 * Return the purified value but allow tags to be as normal
120
	 * otherwise will lose ability to edit wysiwyg fields properly
121
	 */
122
	public function getValueDisplay($context='')
123
	{
124
		if ($context==='grid') {
125
			return strip_tags(Str::truncate($this->getValue(), 200, '...', null, true));
126
		}
127
		return $this->getValue();
128
	}
129
130
	/**
131
	 * @return array
132
	 */
133
	public function getComponentDetails()
134
	{
135
		return ['group' => 'Rich Text',  'icon' => 'fa fa-font', 'order' => 15];
136
	}
137
}
138