Welcome to mirror list, hosted at ThFree Co, Russian Federation.

templatemanager.php « lib - github.com/ONLYOFFICE/onlyoffice-nextcloud.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0dd1b3d2021fcfe3a61c76f11aca33a03f021681 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<?php
/**
 *
 * (c) Copyright Ascensio System SIA 2021
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

namespace OCA\Onlyoffice;


/**
 * Template manager
 *
 * @package OCA\Onlyoffice
 */
class TemplateManager {

    /**
     * Application name
     *
     * @var string
     */
    private static $appName = "onlyoffice";

    /**
     * Template folder name
     *
     * @var string
     */
    private static $templateFolderName = "template";

    /**
     * Get global template directory
     *
     * @return Folder
     */
    public static function GetGlobalTemplateDir() {
        $rootFolder = \OC::$server->getRootFolder();

        $appData = $rootFolder->get("appdata_" . \OC::$server->getConfig()->GetSystemValue("instanceid", null));

        $appDir = $appData->nodeExists(self::$appName) ? $appData->get(self::$appName) : $appData->newFolder(self::$appName);
        $templateDir = $appDir->nodeExists(self::$templateFolderName) ? $appDir->get(self::$templateFolderName) : $appDir->newFolder(self::$templateFolderName);

        return $templateDir;
    }

    /**
     * Get global templates
     *
     * @param string $type - template format type
     *
     * @return array
     */
    public static function GetGlobalTemplates($type = null) {
        $templates = [];
        $templateDir = self::GetGlobalTemplateDir();

        if (!empty($type)) {
            $mime = self::GetMimeTemplate($type);
            $templatesList = $templateDir->searchByMime($mime);

        } else {
            $templatesList = $templateDir->getDirectoryListing();
        }

        foreach ($templatesList as $templatesItem) {
            $template = [
                "id" => $templatesItem->getId(),
                "name" => $templatesItem->getName(),
                "type" => TemplateManager::GetTypeTemplate($templatesItem->getMimeType())
            ];
            array_push($templates, $template);
        }

        return $templates;
    }

    /**
     * Get template content
     *
     * @param string $templateId - identifier file template
     *
     * @return string
     */
    public static function GetTemplate($templateId) {
        $logger = \OC::$server->getLogger();

        $templateDir = self::GetGlobalTemplateDir();
        try {
            $templates = $templateDir->getById($templateId);
        } catch(\Exception $e) {
            $logger->logException($e, ["message" => "GetTemplate: $templateId", "app" => self::$appName]);
            return null;
        }

        if (empty($templates)) {
            $logger->info("Template not found: $templateId", ["app" => self::$appName]);
            return null;
        }

        $content = $templates[0]->getContent();

        return $content;
    }

    /**
     * Get type template from mimetype
     *
     * @param string $mime - mimetype
     *
     * @return string
     */
    public static function GetTypeTemplate($mime) {
        switch($mime) {
            case "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
                return "document";
            case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
                return "spreadsheet";
            case "application/vnd.openxmlformats-officedocument.presentationml.presentation":
                return "presentation";
        }

        return "";
    }

    /**
     * Get mimetype template from format type
     *
     * @param string $type - format type
     *
     * @return string
     */
    public static function GetMimeTemplate($type) {
        switch($type) {
            case "document":
                return "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
            case "spreadsheet":
                return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            case "presentation":
                return "application/vnd.openxmlformats-officedocument.presentationml.presentation";
        }

        return "";
    }

    /**
     * Check template type
     *
     * @param string $name - template name
     *
     * @return bool
     */
    public static function IsTemplateType($name) {
        $ext = strtolower(pathinfo($name, PATHINFO_EXTENSION));
        switch($ext) {
            case "docx":
            case "xlsx":
            case "pptx":
                return true;
        }

        return false;
    }

    /**
     * Get template
     *
     * @param string $name - file name
     *
     * @return string
     */
    public static function GetEmptyTemplate($name) {
        $ext = strtolower("." . pathinfo($name, PATHINFO_EXTENSION));

        $lang = \OC::$server->getL10NFactory("")->get("")->getLanguageCode();

        $templatePath = self::GetEmptyTemplatePath($lang, $ext);

        $template = file_get_contents($templatePath);
        return $template;
    }

    /**
     * Get template path
     *
     * @param string $lang - language
     * @param string $ext - file extension
     *
     * @return string
     */
    public static function GetEmptyTemplatePath($lang, $ext) {
        if (!array_key_exists($lang, self::$localPath)) {
            $lang = "en";
        }

        return dirname(__DIR__) . DIRECTORY_SEPARATOR . "assets" . DIRECTORY_SEPARATOR . self::$localPath[$lang] . DIRECTORY_SEPARATOR . "new" . $ext;
    }

    /**
     * Mapping local path to templates
     *
     * @var Array
     */
    private static $localPath = [
        "az" => "az-Latn-AZ",
        "bg" => "bg-BG",
        "cs" => "cs-CZ",
        "de" => "de-DE",
        "de_DE" => "de-DE",
        "el" => "el-GR",
        "en" => "en-US",
        "en_GB" => "en-GB",
        "es" => "es-ES",
        "fr" => "fr-FR",
        "it" => "it-IT",
        "ja" => "ja-JP",
        "ko" => "ko-KR",
        "lv" => "lv-LV",
        "nl" => "nl-NL",
        "pl" => "pl-PL",
        "pt_BR" => "pt-BR",
        "pt_PT" => "pt-PT",
        "ru" => "ru-RU",
        "sk" => "sk-SK",
        "sv" => "sv-SE",
        "uk" => "uk-UA",
        "vi" => "vi-VN",
        "zh_CN" => "zh-CN"
    ];
}