1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* For licensing terms, see /license.txt */ |
6
|
|
|
|
7
|
|
|
namespace Chamilo\CoreBundle\Component\Editor\CkEditor; |
8
|
|
|
|
9
|
|
|
use Chamilo\CoreBundle\Component\Editor\Editor; |
10
|
|
|
use Chamilo\CoreBundle\Component\Utils\ChamiloApi; |
11
|
|
|
use Chamilo\CoreBundle\Entity\Course; |
12
|
|
|
use Chamilo\CoreBundle\Entity\SystemTemplate; |
13
|
|
|
use Chamilo\CoreBundle\Entity\Templates; |
14
|
|
|
use Database; |
15
|
|
|
|
16
|
|
|
class CkEditor extends Editor |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Return the HTML code required to run editor. |
20
|
|
|
* |
21
|
|
|
* @param string $value |
22
|
|
|
*/ |
23
|
|
|
public function createHtml($value): string |
24
|
|
|
{ |
25
|
|
|
$html = '<textarea id="'.$this->getTextareaId().'" name="'.$this->getName().'" > |
26
|
|
|
'.$value.' |
27
|
|
|
</textarea>'; |
28
|
|
|
$html .= $this->editorReplace(); |
29
|
|
|
|
30
|
|
|
return $html; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Return the HTML code required to run editor. |
35
|
|
|
* |
36
|
|
|
* @param string $value |
37
|
|
|
*/ |
38
|
|
|
public function createHtmlStyle($value): string |
39
|
|
|
{ |
40
|
|
|
$style = ''; |
41
|
|
|
$value = trim($value); |
42
|
|
|
|
43
|
|
|
if ('' === $value || '<html><head><title></title></head><body></body></html>' === $value) { |
44
|
|
|
$style = api_get_bootstrap_and_font_awesome(); |
45
|
|
|
$style .= api_get_css(ChamiloApi::getEditorDocStylePath()); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$html = '<textarea id="'.$this->getTextareaId().'" name="'.$this->getName().'" > |
49
|
|
|
'.$style.$value.' |
50
|
|
|
</textarea>'; |
51
|
|
|
$html .= $this->editorReplace(); |
52
|
|
|
|
53
|
|
|
return $html; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function editorReplace(): string |
57
|
|
|
{ |
58
|
|
|
$toolbar = new Toolbar\Basic( |
59
|
|
|
$this->urlGenerator, |
60
|
|
|
$this->toolbarSet, |
61
|
|
|
$this->config, |
62
|
|
|
'CkEditor' |
63
|
|
|
); |
64
|
|
|
|
65
|
|
|
$config = $toolbar->getConfig(); |
66
|
|
|
$config['selector'] = '#'.$this->getTextareaId(); |
67
|
|
|
$javascript = $this->toJavascript($config); |
68
|
|
|
|
69
|
|
|
// it replaces [browser] by image picker callback |
70
|
|
|
$javascript = str_replace('"[browser]"', $this->getImagePicker(), $javascript); |
71
|
|
|
|
72
|
|
|
return "<script> |
73
|
|
|
document.addEventListener('DOMContentLoaded', function() { |
74
|
|
|
tinymce.init( |
75
|
|
|
$javascript |
76
|
|
|
); |
77
|
|
|
}); |
78
|
|
|
</script>"; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param array $templates |
83
|
|
|
*/ |
84
|
|
|
public function formatTemplates($templates): string |
85
|
|
|
{ |
86
|
|
|
if (empty($templates)) { |
87
|
|
|
return ''; |
88
|
|
|
} |
89
|
|
|
$templateList = []; |
90
|
|
|
$cssTheme = api_get_path(WEB_CSS_PATH).'themes/'.api_get_visual_theme().'/'; |
91
|
|
|
$search = ['{CSS_THEME}', '{IMG_DIR}', '{REL_PATH}', '{COURSE_DIR}', '{CSS}']; |
92
|
|
|
$replace = [ |
93
|
|
|
$cssTheme, |
94
|
|
|
api_get_path(REL_CODE_PATH).'img/', |
95
|
|
|
api_get_path(REL_PATH), |
96
|
|
|
//api_get_path(REL_DEFAULT_COURSE_DOCUMENT_PATH), |
97
|
|
|
'', |
98
|
|
|
]; |
99
|
|
|
|
100
|
|
|
/** @var SystemTemplate $template */ |
101
|
|
|
foreach ($templates as $template) { |
102
|
|
|
$image = $template->getImage(); |
103
|
|
|
$image = !empty($image) ? $image : 'empty.gif'; |
104
|
|
|
|
105
|
|
|
/*$image = $this->urlGenerator->generate( |
106
|
|
|
'get_document_template_action', |
107
|
|
|
array('file' => $image), |
108
|
|
|
UrlGenerator::ABSOLUTE_URL |
109
|
|
|
);*/ |
110
|
|
|
|
111
|
|
|
$content = str_replace($search, $replace, $template->getContent()); |
112
|
|
|
|
113
|
|
|
$templateList[] = [ |
114
|
|
|
'title' => $this->translator->trans($template->getTitle()), |
115
|
|
|
'description' => $this->translator->trans($template->getComment()), |
116
|
|
|
'image' => $image, |
117
|
|
|
'html' => $content, |
118
|
|
|
]; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return json_encode($templateList); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get the templates in JSON format. |
126
|
|
|
* |
127
|
|
|
* @return false|string |
128
|
|
|
*/ |
129
|
|
|
public function simpleFormatTemplates() |
130
|
|
|
{ |
131
|
|
|
$templates = $this->getEmptyTemplate(); |
132
|
|
|
|
133
|
|
|
if (api_is_allowed_to_edit(false, true)) { |
134
|
|
|
$platformTemplates = $this->getPlatformTemplates(); |
135
|
|
|
$templates = array_merge($templates, $platformTemplates); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$personalTemplates = $this->getPersonalTemplates(); |
139
|
|
|
$templates = array_merge($templates, $personalTemplates); |
140
|
|
|
|
141
|
|
|
return json_encode($templates); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Get a custom image picker. |
146
|
|
|
* |
147
|
|
|
* @return string |
148
|
|
|
*/ |
149
|
|
|
private function getImagePicker() |
150
|
|
|
{ |
151
|
|
|
$callback = 'function (cb, value, meta) { |
152
|
|
|
var input = document.createElement("input"); |
153
|
|
|
input.setAttribute("type", "file"); |
154
|
|
|
input.setAttribute("accept", "image/*"); |
155
|
|
|
input.onchange = function () { |
156
|
|
|
var file = this.files[0]; |
157
|
|
|
var reader = new FileReader(); |
158
|
|
|
reader.onload = function () { |
159
|
|
|
var id = "blobid" + (new Date()).getTime(); |
160
|
|
|
var blobCache = tinymce.activeEditor.editorUpload.blobCache; |
161
|
|
|
var base64 = reader.result.split(",")[1]; |
162
|
|
|
var blobInfo = blobCache.create(id, file, base64); |
163
|
|
|
blobCache.add(blobInfo); |
164
|
|
|
cb(blobInfo.blobUri(), { title: file.name }); |
165
|
|
|
}; |
166
|
|
|
reader.readAsDataURL(file); |
167
|
|
|
}; |
168
|
|
|
input.click(); |
169
|
|
|
}'; |
170
|
|
|
|
171
|
|
|
return $callback; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Get the empty template. |
176
|
|
|
*/ |
177
|
|
|
private function getEmptyTemplate(): array |
178
|
|
|
{ |
179
|
|
|
return [ |
180
|
|
|
[ |
181
|
|
|
'title' => get_lang('Blank template'), |
182
|
|
|
'description' => null, |
183
|
|
|
'image' => api_get_path(WEB_PUBLIC_PATH).'img/template_thumb/empty.gif', |
184
|
|
|
'html' => ' |
185
|
|
|
<!DOCYTPE html> |
186
|
|
|
<html> |
187
|
|
|
<head> |
188
|
|
|
<meta charset="'.api_get_system_encoding().'" /> |
189
|
|
|
</head> |
190
|
|
|
<body dir="'.api_get_text_direction().'"> |
191
|
|
|
<p> |
192
|
|
|
<br/> |
193
|
|
|
</p> |
194
|
|
|
</body> |
195
|
|
|
</html> |
196
|
|
|
</html> |
197
|
|
|
', |
198
|
|
|
], |
199
|
|
|
]; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Get the platform templates. |
204
|
|
|
*/ |
205
|
|
|
private function getPlatformTemplates(): array |
206
|
|
|
{ |
207
|
|
|
$entityManager = Database::getManager(); |
208
|
|
|
$systemTemplates = $entityManager->getRepository(SystemTemplate::class)->findAll(); |
209
|
|
|
$cssTheme = api_get_path(WEB_CSS_PATH).'themes/'.api_get_visual_theme().'/'; |
210
|
|
|
$search = ['{CSS_THEME}', '{IMG_DIR}', '{REL_PATH}', '{COURSE_DIR}', '{CSS}']; |
211
|
|
|
$replace = [ |
212
|
|
|
$cssTheme, |
213
|
|
|
api_get_path(REL_PATH).'public/img/', |
214
|
|
|
api_get_path(REL_PATH), |
215
|
|
|
api_get_path(REL_PATH).'public/img/document/', |
216
|
|
|
'', |
217
|
|
|
]; |
218
|
|
|
|
219
|
|
|
$templateList = []; |
220
|
|
|
|
221
|
|
|
foreach ($systemTemplates as $template) { |
222
|
|
|
$image = $template->getImage(); |
223
|
|
|
$image = !empty($image) ? $image : 'empty.gif'; |
224
|
|
|
$image = api_get_path(WEB_PUBLIC_PATH).'img/template_thumb/'.$image; |
225
|
|
|
$templateContent = $template->getContent(); |
226
|
|
|
$content = str_replace($search, $replace, $templateContent); |
227
|
|
|
|
228
|
|
|
$templateList[] = [ |
229
|
|
|
'title' => get_lang($template->getTitle()), |
230
|
|
|
'description' => get_lang($template->getComment()), |
231
|
|
|
'image' => $image, |
232
|
|
|
'html' => $content, |
233
|
|
|
]; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
return $templateList; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* @param int $userId |
241
|
|
|
* |
242
|
|
|
* @return array |
243
|
|
|
*/ |
244
|
|
|
private function getPersonalTemplates($userId = 0) |
245
|
|
|
{ |
246
|
|
|
if (empty($userId)) { |
247
|
|
|
$userId = api_get_user_id(); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
$entityManager = Database::getManager(); |
251
|
|
|
$templatesRepo = $entityManager->getRepository(Templates::class); |
252
|
|
|
$user = api_get_user_entity($userId); |
253
|
|
|
$course = $entityManager->find(Course::class, api_get_course_int_id()); |
254
|
|
|
|
255
|
|
|
if (!$user || !$course) { |
256
|
|
|
return []; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
$courseTemplates = $templatesRepo->getCourseTemplates($course, $user); |
260
|
|
|
$templateList = []; |
261
|
|
|
|
262
|
|
|
foreach ($courseTemplates as $templateData) { |
263
|
|
|
$template = $templateData[0]; |
264
|
|
|
|
265
|
|
|
$templateItem = []; |
266
|
|
|
$templateItem['title'] = $template->getTitle(); |
267
|
|
|
$templateItem['description'] = $template->getDescription(); |
268
|
|
|
$templateItem['image'] = api_get_path(WEB_PUBLIC_PATH).'img/template_thumb/noimage.gif'; |
269
|
|
|
/*$templateItem['html'] = file_get_contents(api_get_path(SYS_COURSE_PATH) |
270
|
|
|
.$courseDirectory.'/document'.$templateData['path']);*/ |
271
|
|
|
|
272
|
|
|
$image = $template->getImage(); |
273
|
|
|
if (!empty($image)) { |
274
|
|
|
$templateItem['image'] = api_get_path(WEB_COURSE_PATH) |
275
|
|
|
.$courseDirectory.'/upload/template_thumbnails/'.$template->getImage(); |
|
|
|
|
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
$templateList[] = $templateItem; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
return $templateList; |
282
|
|
|
} |
283
|
|
|
} |
284
|
|
|
|