Passed
Push — 1.11.x ( 45dcb0...6c301e )
by Yannick
10:41 queued 12s
created

OnlyofficeTools   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 16
eloc 73
dl 0
loc 132
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B getButtonEdit() 0 43 6
B getButtonView() 0 46 7
A getButtonCreateNew() 0 28 3
1
<?php
2
/**
3
 *
4
 * (c) Copyright Ascensio System SIA 2021
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
class OnlyofficeTools {
21
22
    /**
23
     * Return button-link to onlyoffice editor for file
24
     */
25
    public static function getButtonEdit(array $document_data): string
26
    {
27
28
        $plugin = OnlyofficePlugin::create();
29
30
        $isEnable = $plugin->get("enable_onlyoffice_plugin") === "true";
31
        if (!$isEnable) {
32
            return '';
33
        }
34
35
        $urlToEdit = api_get_path(WEB_PLUGIN_PATH) . "onlyoffice/editor.php";
36
37
        $extension = strtolower(pathinfo($document_data["title"], PATHINFO_EXTENSION));
38
39
        $canEdit = in_array($extension, FileUtility::$can_edit_types);
40
        $canView = in_array($extension, FileUtility::$can_view_types);
41
42
        $groupId = api_get_group_id();
43
        if (!empty($groupId)) {
44
            $urlToEdit = $urlToEdit . "?groupId=" . $groupId . "&";
45
        } else {
46
            $urlToEdit = $urlToEdit . "?";
47
        }
48
49
        $documentId = $document_data["id"];
50
        $urlToEdit = $urlToEdit . "docId=" . $documentId;
51
52
        if ($canEdit || $canView) {
53
            $tooltip = $plugin->get_lang('openByOnlyoffice');
54
            if ($extension === "oform") {
55
                $tooltip = $plugin->get_lang('fillInFormInOnlyoffice');
56
            }
57
58
            return Display::url(
59
                Display::return_icon(
60
                    '../../plugin/onlyoffice/resources/onlyoffice_edit.png',
61
                    $tooltip
62
                ),
63
                $urlToEdit
64
            );
65
        }
66
67
        return '';
68
    }
69
70
    /**
71
     * Return button-link to onlyoffice editor for view file
72
     */
73
    public static function getButtonView (array $document_data): string
74
    {
75
76
        $plugin = OnlyofficePlugin::create();
77
78
        $isEnable = $plugin->get("enable_onlyoffice_plugin") === "true";
79
        if (!$isEnable) {
80
            return '';
81
        }
82
83
        $urlToEdit = api_get_path(WEB_PLUGIN_PATH) . "onlyoffice/editor.php";
84
85
        $sessionId = api_get_session_id();
86
        $courseInfo = api_get_course_info();
87
        $documentId = $document_data["id"];
88
        $userId = api_get_user_id();
89
90
        $docInfo = DocumentManager::get_document_data_by_id($documentId, $courseInfo["code"], false, $sessionId);
91
92
        $extension = strtolower(pathinfo($document_data["title"], PATHINFO_EXTENSION));
93
        $canView = in_array($extension, FileUtility::$can_view_types);
94
95
        $isGroupAccess = false;
96
        $groupId = api_get_group_id();
97
        if (!empty($groupId)) {
98
            $groupProperties = GroupManager::get_group_properties($groupId);
99
            $docInfoGroup = api_get_item_property_info(api_get_course_int_id(), 'document', $documentId, $sessionId);
100
            $isGroupAccess = GroupManager::allowUploadEditDocument($userId, $courseInfo["code"], $groupProperties, $docInfoGroup);
101
102
            $urlToEdit = $urlToEdit . "?groupId=" . $groupId . "&";
103
        } else {
104
            $urlToEdit = $urlToEdit . "?";
105
        }
106
107
        $isAllowToEdit = api_is_allowed_to_edit(true, true);
108
        $isMyDir = DocumentManager::is_my_shared_folder($userId, $docInfo["absolute_parent_path"], $sessionId);
109
110
        $accessRights = $isAllowToEdit || $isMyDir || $isGroupAccess;
111
112
        $urlToEdit = $urlToEdit . "docId=" . $documentId;
113
114
        if ($canView && !$accessRights) {
115
            return Display::url(Display::return_icon('../../plugin/onlyoffice/resources/onlyoffice_view.png', $plugin->get_lang('openByOnlyoffice')), $urlToEdit, ["style" => "float:right; margin-right:8px"]);
116
        }
117
118
        return '';
119
    }
120
121
    /**
122
     * Return button-link to onlyoffice create new
123
     */
124
    public static function getButtonCreateNew (): string
125
    {
126
127
        $plugin = OnlyofficePlugin::create();
128
129
        $isEnable = $plugin->get("enable_onlyoffice_plugin") === "true";
130
        if (!$isEnable) {
131
            return '';
132
        }
133
134
        $courseId = api_get_course_int_id();
135
        $sessionId = api_get_session_id();
136
        $groupId = api_get_group_id();
137
        $userId = api_get_user_id();
138
139
        $urlToCreate = api_get_path(WEB_PLUGIN_PATH) . "onlyoffice/create.php"
140
                                                        ."?folderId=" . (empty($_GET["id"])?'0':(int)$_GET["id"])
141
                                                        . "&courseId=" . $courseId
142
                                                        . "&groupId=" . $groupId
143
                                                        . "&sessionId=" . $sessionId
144
                                                        . "&userId=" . $userId;
145
146
        return Display::url(
147
            Display::return_icon(
148
                "../../plugin/onlyoffice/resources/onlyoffice_create.png",
149
                $plugin->get_lang("createNew")
150
            ),
151
            $urlToCreate
152
        );
153
    }
154
}
155