Passed
Push — 1.11.x ( 03e196...058e18 )
by Yannick
09:50 queued 25s
created

OnlyofficeSettingsFormBuilder   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
eloc 61
c 1
b 0
f 0
dl 0
loc 121
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A buildSettingsForm() 0 37 3
A displayError() 0 10 2
A buildTemplate() 0 9 3
A validateSettingsForm() 0 21 6
1
<?php
2
/**
3
 *
4
 * (c) Copyright Ascensio System SIA 2023
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 *
18
 */
19
20
require_once __DIR__ . "/../../../main/inc/global.inc.php";
21
require_once __DIR__ . "/documentService.php";
22
23
class OnlyofficeSettingsFormBuilder {
24
25
    /**
26
     * Directory with layouts
27
     */
28
    private const ONLYOFFICE_LAYOUT_DIR = '/onlyoffice/layout/';
29
30
    /**
31
     * Build HTML-template
32
     *
33
     * @param string $templateName - template name (*.tpl)
34
     * @param array $params - parameters to assign
35
     *
36
     * @return string
37
     */
38
    private function buildTemplate($templateName, $params = []) {
39
        $tpl = new Template('', false, false, false, false, false, false);
40
        if (!empty($params)) {
41
            foreach ($params as $key => $param) {
42
                $tpl->assign($key, $param); 
43
            }
44
        }
45
        $parsedTemplate = $tpl->fetch(self::ONLYOFFICE_LAYOUT_DIR.$templateName.'.tpl');
46
        return $parsedTemplate;
47
    }
48
49
    /**
50
     * Display error messahe
51
     *
52
     * @param string $errorMessage - error message
53
     * @param string $location - header location
54
     *
55
     * @return void
56
     */
57
    private function displayError($errorMessage, $location = null) {
58
        Display::addFlash(
59
            Display::return_message(
60
                $errorMessage,
61
                'error'
62
            )
63
        );
64
        if ($location !== null) {
65
            header('Location: '.$location);
66
            exit;
67
        }
68
    }
69
70
    /**
71
     * Build OnlyofficePlugin settings form
72
     *
73
     * @param OnlyofficePlugin $plugin - OnlyofficePlugin
74
     *
75
     * @return FormValidator
76
     */
77
    public function buildSettingsForm($plugin) {
78
        $demoData = $plugin->getDemoData();
79
        $plugin_info = $plugin->get_info();
80
        $message = '';
81
        $connectDemoCheckbox = $plugin_info['settings_form']->createElement(
82
            'checkbox',
83
            'connect_demo', 
84
            '',
85
            $plugin->get_lang('connect_demo')
86
        );
87
        if (!$demoData['available'] === true) {
88
            $message = $plugin->get_lang('demoPeriodIsOver');
89
            $connectDemoCheckbox->setAttribute('disabled');
90
        } else {
91
            if ($plugin->useDemo()) {
92
                $message = $plugin->get_lang('demoUsingMessage');
93
                $connectDemoCheckbox->setChecked(true);
94
            } else {
95
                $message = $plugin->get_lang('demoPrevMessage');
96
            }
97
        }
98
        $demoServerMessageHtml = Display::return_message(
99
            $message,
100
            'info'
101
        );
102
        $bannerTemplate = self::buildTemplate('get_docs_cloud_banner', [
0 ignored issues
show
Bug Best Practice introduced by
The method OnlyofficeSettingsFormBuilder::buildTemplate() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

102
        /** @scrutinizer ignore-call */ 
103
        $bannerTemplate = self::buildTemplate('get_docs_cloud_banner', [
Loading history...
103
            'docs_cloud_link' => AppConfig::GetLinkToDocs(),
0 ignored issues
show
Bug Best Practice introduced by
The method AppConfig::GetLinkToDocs() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

103
            'docs_cloud_link' => AppConfig::/** @scrutinizer ignore-call */ GetLinkToDocs(),
Loading history...
104
            'banner_title' => $plugin->get_lang('DocsCloudBannerTitle'),
105
            'banner_main_text' => $plugin->get_lang('DocsCloudBannerMain'),
106
            'banner_button_text' => $plugin->get_lang('DocsCloudBannerButton'),
107
        ]);
108
        $plugin_info['settings_form']->insertElementBefore($connectDemoCheckbox, 'submit_button');
109
        $demoServerMessage = $plugin_info['settings_form']->createElement('html', $demoServerMessageHtml);
110
        $plugin_info['settings_form']->insertElementBefore($demoServerMessage, 'submit_button');
111
        $banner = $plugin_info['settings_form']->createElement('html', $bannerTemplate);
112
        $plugin_info['settings_form']->insertElementBefore($banner, 'submit_button');
113
        return $plugin_info['settings_form'];
114
    }
115
    
116
    /**
117
     * Validate OnlyofficePlugin settings form
118
     *
119
     * @param OnlyofficePlugin $plugin - OnlyofficePlugin
120
     *
121
     * @return OnlyofficePlugin
122
     */
123
    public function validateSettingsForm($plugin) {
124
        $errorMsg = null;
125
        $plugin_info = $plugin->get_info();
126
        $result = $plugin_info['settings_form']->getSubmitValues();
127
        if (!$plugin->selectDemo((bool)$result['connect_demo'] === true)) {
128
            $errorMsg = $plugin->get_lang('demoPeriodIsOver');
129
            self::displayError($errorMsg, $plugin->getConfigLink());
0 ignored issues
show
Bug Best Practice introduced by
The method OnlyofficeSettingsFormBuilder::displayError() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

129
            self::/** @scrutinizer ignore-call */ 
130
                  displayError($errorMsg, $plugin->getConfigLink());
Loading history...
130
        }
131
        $documentserver = $plugin->getDocumentServerUrl();
132
        if (!empty($documentserver)) {
133
            if ((bool)$result['connect_demo'] === false) {
134
                $documentService = new DocumentService($plugin, $result);
135
                list ($error, $version) = $documentService->checkDocServiceUrl();
136
137
                if (!empty($error)) {
138
                    $errorMsg = $plugin->get_lang('connectionError').'('.$error.')'.(!empty($version) ? '(Version '.$version.')' : '');
139
                self::displayError($errorMsg); 
140
                }
141
            }
142
        }
143
        return $plugin;
144
    }
145
}