|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* YAWIK |
|
4
|
|
|
* |
|
5
|
|
|
* @filesource |
|
6
|
|
|
* @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de) |
|
7
|
|
|
* @license MIT |
|
8
|
|
|
* @author [email protected] |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Core\Form\View\Helper; |
|
12
|
|
|
|
|
13
|
|
|
use Zend\Form\ElementInterface; |
|
14
|
|
|
use Zend\Form\View\Helper\FormTextarea; |
|
15
|
|
|
|
|
16
|
|
|
class FormEditor extends FormTextarea |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $theme = 'modern'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Default configuration of the form editor |
|
25
|
|
|
* |
|
26
|
|
|
* @see https://www.tinymce.com/docs/configure/integration-and-setup/ |
|
27
|
|
|
* @var array |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $options = [ |
|
30
|
|
|
'selector' => 'div.tinymce_modern', |
|
31
|
|
|
'inline' => true, |
|
32
|
|
|
'theme' => 'modern', |
|
33
|
|
|
'toolbar' => 'undo redo | formatselect | bold italic | alignleft aligncenter alignright alignjustify | link | bullist | removeformat', |
|
34
|
|
|
'menubar' => false, |
|
35
|
|
|
'advlist_bullet_styles' => 'square disc', |
|
36
|
|
|
'block_formats' => 'Headings=h4;', |
|
37
|
|
|
'removed_menuitems' => 'newdocument', |
|
38
|
|
|
'plugings' => [ |
|
39
|
|
|
'autolink lists advlist', |
|
40
|
|
|
'visualblocks code fullscreen', |
|
41
|
|
|
'contextmenu paste link', |
|
42
|
|
|
], |
|
43
|
|
|
]; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Language of tinyMCE |
|
47
|
|
|
* |
|
48
|
|
|
* @var string |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $language="de"; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $translator; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param ElementInterface $element |
|
59
|
|
|
* |
|
60
|
|
|
* @return string |
|
61
|
|
|
*/ |
|
62
|
|
|
public function render(ElementInterface $element) |
|
63
|
|
|
{ |
|
64
|
|
|
$name = $element->getName(); |
|
65
|
|
|
if (empty($name) && $name !== 0) { |
|
|
|
|
|
|
66
|
|
|
throw new \DomainException( |
|
67
|
|
|
sprintf( |
|
68
|
|
|
'%s requires that the element has an assigned name; none discovered', |
|
69
|
|
|
__METHOD__ |
|
70
|
|
|
) |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$renderer = $this->getView(); |
|
75
|
|
|
|
|
76
|
|
|
/* @var \Zend\View\Helper\HeadScript $headscript */ |
|
77
|
|
|
$headscript = $renderer->plugin('headscript'); |
|
78
|
|
|
$basepath = $renderer->plugin('basepath'); |
|
79
|
|
|
|
|
80
|
|
|
$headscript->appendFile($basepath('js/tinymce/tinymce.jquery.min.js')); |
|
81
|
|
|
$headscript->prependFile($basepath('js/jquery.min.js')); |
|
82
|
|
|
|
|
83
|
|
|
$headscript->offsetSetScript( |
|
84
|
|
|
'1000_tinymce_' . $this->getTheme(), |
|
85
|
|
|
' |
|
86
|
|
|
$(document).ready(function() { |
|
87
|
|
|
tinyMCE.init({' . $this->additionalOptions() . ', |
|
88
|
|
|
setup: function(editor) { |
|
89
|
|
|
setPlaceHolder = function(editor, show) { |
|
90
|
|
|
placeHolder = $("#placeholder-" + editor.id); |
|
91
|
|
|
if (placeHolder.length == 1) { |
|
92
|
|
|
if (show && editor.getContent() == "") { |
|
93
|
|
|
placeHolder.show(); |
|
94
|
|
|
} |
|
95
|
|
|
else { |
|
96
|
|
|
placeHolder.hide(); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
}, |
|
100
|
|
|
editor.on("focus", function(e) { |
|
101
|
|
|
setPlaceHolder(editor, false); |
|
102
|
|
|
}); |
|
103
|
|
|
editor.on("blur", function(e) { |
|
104
|
|
|
setPlaceHolder(editor, true); |
|
105
|
|
|
if (editor.isDirty()) { |
|
106
|
|
|
//console.log("blur event", e); |
|
107
|
|
|
editor.save(); |
|
108
|
|
|
var container = e.target.bodyElement; |
|
109
|
|
|
$(container).parents("html").addClass("yk-changed"); |
|
110
|
|
|
var form = $(container).parents("form"); |
|
111
|
|
|
//console.log("form", form, container); |
|
112
|
|
|
form.submit(); |
|
113
|
|
|
$(form).on("yk.forms.done", function(){ |
|
114
|
|
|
console.log("done"); |
|
115
|
|
|
//$(container).parents("html").removeClass("yk-changed"); |
|
116
|
|
|
}); |
|
117
|
|
|
} |
|
118
|
|
|
}); |
|
119
|
|
|
} |
|
120
|
|
|
}); |
|
121
|
|
|
});' |
|
122
|
|
|
); |
|
123
|
|
|
|
|
124
|
|
|
$attributes = $element->getAttributes(); |
|
125
|
|
|
$attributes['name'] = $name; |
|
126
|
|
|
$attributes['id'] = $name; |
|
127
|
|
|
$content = $element->getValue(); |
|
128
|
|
|
if (!isset($content)) { |
|
129
|
|
|
$content = ''; |
|
130
|
|
|
} |
|
131
|
|
|
if (is_string($content)) { |
|
132
|
|
|
|
|
133
|
|
|
$class = array_key_exists('class', $attributes)?$attributes['class']:''; |
|
134
|
|
|
$class .= (empty($class)?:' ') . ' tinymce_' . $this->getTheme(); |
|
135
|
|
|
$attributes['class'] = $class; |
|
136
|
|
|
$placeHolder = ''; |
|
137
|
|
|
$elementOptions = $element->getOptions(); |
|
138
|
|
|
if (array_key_exists('placeholder', $elementOptions) && !empty($elementOptions['placeholder'])) { |
|
139
|
|
|
$placeHolder = '<div id="placeholder-' . $name . '" style="border: 0 none; position: relative; top: 0ex; left: 10px; color: #aaa; height: 0px; overflow: visible;' . |
|
140
|
|
|
(empty($content)?'':'display:none;') . |
|
141
|
|
|
'">' . $this->translator->translate($elementOptions['placeholder']) . '</div>'; |
|
142
|
|
|
} |
|
143
|
|
|
return |
|
144
|
|
|
$placeHolder |
|
145
|
|
|
. sprintf( |
|
146
|
|
|
'<div %s >%s</div>', |
|
147
|
|
|
$this->createAttributesString($attributes), |
|
|
|
|
|
|
148
|
|
|
$content |
|
149
|
|
|
); |
|
150
|
|
|
|
|
151
|
|
|
} else { |
|
152
|
|
|
return (string) $content; |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Gets the name of the theme |
|
158
|
|
|
* |
|
159
|
|
|
* @return string |
|
160
|
|
|
*/ |
|
161
|
|
|
protected function getTheme() |
|
162
|
|
|
{ |
|
163
|
|
|
return $this->theme; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
protected function additionalOptions() |
|
167
|
|
|
{ |
|
168
|
|
|
$str = json_encode($this->options, ~JSON_HEX_QUOT & ~JSON_FORCE_OBJECT ); |
|
169
|
|
|
$str = preg_replace('/"([a-zA-Z_]+[a-zA-Z0-9_]*)":/','$1:',$str); |
|
170
|
|
|
return trim($str,'{}'); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Translations of "Job title" and "Subtitle" are directly made in the tinymce language files |
|
175
|
|
|
* |
|
176
|
|
|
* @param $language |
|
177
|
|
|
*/ |
|
178
|
|
|
public function setLanguage($language) { |
|
179
|
|
|
$this->language=$language; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Sets the language path for tinyMCE language files |
|
184
|
|
|
* |
|
185
|
|
|
* @param $languagePath |
|
186
|
|
|
*/ |
|
187
|
|
|
public function setLanguagePath($languagePath) { |
|
188
|
|
|
$this->languagePath=$languagePath; |
|
|
|
|
|
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Set a formular editor option |
|
193
|
|
|
* |
|
194
|
|
|
* @param $name |
|
195
|
|
|
* @param $value |
|
196
|
|
|
*/ |
|
197
|
|
|
public function setOption($name,$value){ |
|
198
|
|
|
if (array_key_exists($name, $this->options)) { |
|
199
|
|
|
$this->options[$name] = $value; |
|
200
|
|
|
}elseif ('language' == $name or 'language_url' == $name ) { |
|
|
|
|
|
|
201
|
|
|
$this->options[$name] = $value; |
|
202
|
|
|
|
|
203
|
|
|
}else{ |
|
204
|
|
|
throw new \InvalidArgumentException('Unknown Option ' . $name . ' in ' . __FILE__ . ' Line ' . __LINE__ ); |
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Set formular editor options |
|
210
|
|
|
* |
|
211
|
|
|
* @param $options |
|
212
|
|
|
*/ |
|
213
|
|
|
public function setOptions($options) |
|
214
|
|
|
{ |
|
215
|
|
|
foreach($options as $key => $val) { |
|
216
|
|
|
$this->setOption($key, $val); |
|
217
|
|
|
} |
|
218
|
|
|
} |
|
219
|
|
|
} |
|
220
|
|
|
|