Passed
Push — master ( 262b3f...86674d )
by Yannick
09:44
created

validateSettingsForm()   A

Complexity

Conditions 6
Paths 10

Size

Total Lines 26
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 26
rs 9.0111
cc 6
nc 10
nop 1
1
<?php
2
/**
3
 * (c) Copyright Ascensio System SIA 2025.
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
require_once __DIR__.'/../../../main/inc/global.inc.php';
18
19
class OnlyofficeSettingsFormBuilder
20
{
21
    /**
22
     * Directory with layouts.
23
     */
24
    private const ONLYOFFICE_LAYOUT_DIR = '/onlyoffice/layout/';
25
26
    /**
27
     * Build OnlyofficePlugin settings form.
28
     *
29
     * @return FormValidator
30
     */
31
    public static function buildSettingsForm(OnlyofficeAppsettings $settingsManager)
32
    {
33
        $plugin = $settingsManager->plugin;
34
        $demoData = $settingsManager->getDemoData();
35
        $plugin_info = $plugin->get_info();
36
        $message = '';
37
        $connectDemoCheckbox = $plugin_info['settings_form']->createElement(
38
            'checkbox',
39
            'connect_demo',
40
            '',
41
            $plugin->get_lang('connect_demo')
42
        );
43
        if (true === !$demoData['available']) {
44
            $message = $plugin->get_lang('demoPeriodIsOver');
45
            $connectDemoCheckbox->setAttribute('disabled');
46
        } else {
47
            if ($settingsManager->useDemo()) {
48
                $message = $plugin->get_lang('demoUsingMessage');
49
                $connectDemoCheckbox->setChecked(true);
50
            } else {
51
                $message = $plugin->get_lang('demoPrevMessage');
52
            }
53
        }
54
        $demoServerMessageHtml = Display::return_message(
55
            $message,
56
            'info'
57
        );
58
        $bannerTemplate = self::buildTemplate('get_docs_cloud_banner', [
59
            'docs_cloud_link' => $settingsManager->getLinkToDocs(),
60
            'banner_title' => $plugin->get_lang('DocsCloudBannerTitle'),
61
            'banner_main_text' => $plugin->get_lang('DocsCloudBannerMain'),
62
            'banner_button_text' => $plugin->get_lang('DocsCloudBannerButton'),
63
        ]);
64
        $plugin_info['settings_form']->insertElementBefore($connectDemoCheckbox, 'submit_button');
65
        $demoServerMessage = $plugin_info['settings_form']->createElement('html', $demoServerMessageHtml);
66
        $plugin_info['settings_form']->insertElementBefore($demoServerMessage, 'submit_button');
67
        $banner = $plugin_info['settings_form']->createElement('html', $bannerTemplate);
68
        $plugin_info['settings_form']->insertElementBefore($banner, 'submit_button');
69
70
        return $plugin_info['settings_form'];
71
    }
72
73
    /**
74
     * Validate OnlyofficePlugin settings form.
75
     *
76
     * @param OnlyofficeAppsettings $settingsManager - Onlyoffice SettingsManager
77
     *
78
     * @return OnlyofficePlugin
79
     */
80
    public static function validateSettingsForm(OnlyofficeAppsettings $settingsManager)
81
    {
82
        $plugin = $settingsManager->plugin;
83
        $errorMsg = null;
84
        $plugin_info = $plugin->get_info();
85
        $result = $plugin_info['settings_form']->getSubmitValues();
86
        unset($result['submit_button']);
87
        $settingsManager->newSettings = $result;
88
        if (!$settingsManager->selectDemo(true === (bool) $result['connect_demo'])) {
89
            $errorMsg = $plugin->get_lang('demoPeriodIsOver');
90
            self::displayError($errorMsg, $plugin->getConfigLink());
91
        }
92
        if (!empty($settingsManager->getDocumentServerUrl())) {
93
            if (false === (bool) $result['connect_demo']) {
94
                $httpClient = new OnlyofficeHttpClient();
95
                $jwtManager = new OnlyofficeJwtManager($settingsManager);
96
                $requestService = new OnlyofficeAppRequests($settingsManager, $httpClient, $jwtManager);
97
                list($error, $version) = $requestService->checkDocServiceUrl();
98
                if (!empty($error)) {
99
                    $errorMsg = $plugin->get_lang('connectionError').'('.$error.')'.(!empty($version) ? '(Version '.$version.')' : '');
100
                    self::displayError($errorMsg);
101
                }
102
            }
103
        }
104
105
        return $plugin;
106
    }
107
108
    /**
109
     * Build HTML-template.
110
     *
111
     * @param string $templateName - template name (*.tpl)
112
     * @param array  $params       - parameters to assign
113
     *
114
     * @return string
115
     */
116
    private static function buildTemplate($templateName, $params = [])
117
    {
118
        $tpl = new Template('', false, false, false, false, false, false);
119
        if (!empty($params)) {
120
            foreach ($params as $key => $param) {
121
                $tpl->assign($key, $param);
122
            }
123
        }
124
        $parsedTemplate = $tpl->fetch(self::ONLYOFFICE_LAYOUT_DIR.$templateName.'.tpl');
125
126
        return $parsedTemplate;
127
    }
128
129
    /**
130
     * Display error messahe.
131
     *
132
     * @param string $errorMessage - error message
133
     * @param string $location     - header location
134
     *
135
     * @return void
136
     */
137
    private static function displayError($errorMessage, $location = null)
138
    {
139
        Display::addFlash(
140
            Display::return_message(
141
                $errorMessage,
142
                'error'
143
            )
144
        );
145
        if (null !== $location) {
146
            header('Location: '.$location);
147
            exit;
148
        }
149
    }
150
}
151