Completed
Push — master ( 7146f9...4447ca )
by Julito
09:33
created

CkEditor::createHtmlStyle()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 2
nop 0
dl 0
loc 14
rs 10
c 0
b 0
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
namespace Chamilo\CoreBundle\Component\Editor\CkEditor;
5
6
use Chamilo\CoreBundle\Component\Editor\Editor;
7
use Chamilo\CoreBundle\Component\Utils\ChamiloApi;
8
9
/**
10
 * Class CkEditor.
11
 *
12
 * @package Chamilo\CoreBundle\Component\Editor\CkEditor
13
 */
14
class CkEditor extends Editor
15
{
16
    /**
17
     * Return the HTML code required to run editor.
18
     *
19
     * @return string
20
     */
21
    public function createHtml()
22
    {
23
        $html = '<textarea id="'.$this->getName().'" name="'.$this->getName().'" class="ckeditor">
24
                 '.$this->value.'
25
                 </textarea>';
26
        $html .= $this->editorReplace();
27
28
        return $html;
29
    }
30
31
    /**
32
     * Return the HTML code required to run editor.
33
     *
34
     * @return string
35
     */
36
    public function createHtmlStyle()
37
    {
38
        $style = '';
39
        if (trim($this->value) === '<html><head><title></title></head><body></body></html>' || $this->value === '') {
40
            $style = api_get_bootstrap_and_font_awesome();
41
            $style .= api_get_css(ChamiloApi::getEditorDocStylePath());
42
        }
43
44
        $html = '<textarea id="'.$this->getName().'" name="'.$this->getName().'" class="ckeditor">
45
                 '.$style.htmlspecialchars($this->value, ENT_COMPAT).'
46
                 </textarea>';
47
        $html .= $this->editorReplace();
48
49
        return $html;
50
    }
51
52
    /**
53
     * @return string
54
     */
55
    public function editorReplace()
56
    {
57
        $toolbar = new Toolbar\Basic(
58
            $this->urlGenerator,
59
            $this->toolbarSet,
60
            $this->config,
61
            'CkEditor'
62
        );
63
64
        $toolbar->setLanguage($this->getLocale());
65
        $config = $toolbar->getConfig();
66
        $javascript = $this->toJavascript($config);
67
68
        $html = "<script>
69
           CKEDITOR.replace('".$this->getName()."',
70
               $javascript
71
           );
72
           </script>";
73
74
        return $html;
75
    }
76
77
    /**
78
     * Get the templates in JSON format.
79
     *
80
     * @return string
81
     */
82
    public function simpleFormatTemplates(): string
83
    {
84
        $templates = $this->getEmptyTemplate();
85
86
        if (api_is_allowed_to_edit(false, true)) {
87
            $platformTemplates = $this->getPlatformTemplates();
88
            $templates = array_merge($templates, $platformTemplates);
89
        }
90
91
        $personalTemplates = $this->getPersonalTemplates();
92
        $templates = array_merge($templates, $personalTemplates);
93
94
        return json_encode($templates);
95
    }
96
97
    /**
98
     * Get the empty template.
99
     *
100
     * @return array
101
     */
102
    private function getEmptyTemplate(): array
103
    {
104
        return [
105
            [
106
                'title' => get_lang('EmptyTemplate'),
107
                'description' => null,
108
                'image' => api_get_path(WEB_PUBLIC_PATH).'img/template_thumb/empty.gif',
109
                'html' => '
110
                <!DOCYTPE html>
111
                <html>
112
                    <head>
113
                        <meta charset="'.api_get_system_encoding().'" />
114
                    </head>
115
                    <body  dir="'.api_get_text_direction().'">
116
                        <p>
117
                            <br/>
118
                        </p>
119
                    </body>
120
                    </html>
121
                </html>
122
            ',
123
            ],
124
        ];
125
    }
126
127
    /**
128
     * Get the platform templates.
129
     *
130
     * @return array
131
     */
132
    private function getPlatformTemplates()
133
    {
134
        $entityManager = \Database::getManager();
135
        $systemTemplates = $entityManager->getRepository('ChamiloCoreBundle:SystemTemplate')->findAll();
136
        $cssTheme = api_get_path(WEB_CSS_PATH).'themes/'.api_get_visual_theme().'/';
137
        $search = ['{CSS_THEME}', '{IMG_DIR}', '{REL_PATH}', '{COURSE_DIR}', '{CSS}'];
138
        $replace = [
139
            $cssTheme,
140
            api_get_path(REL_PATH).'public/img/',
141
            api_get_path(REL_PATH),
142
            api_get_path(REL_PATH).'public/img/document/',
143
            '',
144
        ];
145
146
        $templateList = [];
147
148
        foreach ($systemTemplates as $template) {
149
            $image = $template->getImage();
150
            $image = !empty($image) ? $image : 'empty.gif';
151
            $image = api_get_path(WEB_PUBLIC_PATH).'img/template_thumb/'.$image;
152
            $templateContent = $template->getContent();
153
            $content = str_replace($search, $replace, $templateContent);
154
155
            $templateList[] = [
156
                'title' => get_lang($template->getTitle()),
157
                'description' => get_lang($template->getComment()),
158
                'image' => $image,
159
                'html' => $content,
160
            ];
161
        }
162
163
        return $templateList;
164
    }
165
166
    /**
167
     * @param int $userId
168
     *
169
     * @return array
170
     */
171
    private function getPersonalTemplates($userId = 0)
172
    {
173
        if (empty($userId)) {
174
            $userId = api_get_user_id();
175
        }
176
177
        $entityManager = \Database::getManager();
178
        $templatesRepo = $entityManager->getRepository('ChamiloCoreBundle:Templates');
179
        $user = api_get_user_entity($userId);
180
        $course = $entityManager->find('ChamiloCoreBundle:Course', api_get_course_int_id());
181
182
        if (!$user || !$course) {
0 ignored issues
show
introduced by
$user is of type Chamilo\UserBundle\Entity\User, thus it always evaluated to true.
Loading history...
183
            return [];
184
        }
185
186
        $courseTemplates = $templatesRepo->getCourseTemplates($course, $user);
187
        $templateList = [];
188
189
        foreach ($courseTemplates as $templateData) {
190
            $template = $templateData[0];
191
            $courseDirectory = $course->getDirectory();
192
193
            $templateItem = [];
194
            $templateItem['title'] = $template->getTitle();
195
            $templateItem['description'] = $template->getDescription();
196
            $templateItem['image'] = api_get_path(WEB_PUBLIC_PATH).'img/template_thumb/noimage.gif';
197
            $templateItem['html'] = file_get_contents(api_get_path(SYS_COURSE_PATH)
198
                .$courseDirectory.'/document'.$templateData['path']);
199
200
            $image = $template->getImage();
201
            if (!empty($image)) {
202
                $templateItem['image'] = api_get_path(WEB_COURSE_PATH)
203
                    .$courseDirectory.'/upload/template_thumbnails/'.$template->getImage();
204
            }
205
206
            $templateList[] = $templateItem;
207
        }
208
209
        return $templateList;
210
    }
211
}
212