Cms::editDocumentLink()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
cc 3
eloc 5
c 1
b 1
f 1
nc 3
nop 1
dl 0
loc 7
rs 10
1
<?php
2
/**
3
 * Created by: Jens
4
 * Date: 9-1-2018
5
 */
6
7
namespace CloudControl\Cms\util;
8
9
10
use CloudControl\Cms\cc\Request;
11
use CloudControl\Cms\components\cms\CmsConstants;
12
13
class Cms
14
{
15
    public static $assetsIncluded = false;
16
17
    /**
18
     * Returns a button with a link for editing a document
19
     * @param $path
20
     * @return string
21
     * @throws \Exception
22
     */
23
    public static function editDocument($path)
24
    {
25
        if (self::isLoggedIn()) {
26
            $return = self::getAssetsIfNotIncluded();
27
            return $return . '<a title="Edit Document" data-href="' . self::editDocumentLink($path) . '" class="ccEditDocumentButton"></a>';
28
        } else {
29
            return '';
30
        }
31
    }
32
33
    /**
34
     * Returns the cms link for editing a document
35
     * @param $path
36
     * @return string
37
     * @throws \Exception
38
     */
39
    public static function editDocumentLink($path)
40
    {
41
        if (self::isLoggedIn()) {
42
            $path = 0 === strpos($path, '/') ? substr($path, 1) : $path;
43
            return Request::$subfolders . 'cms/documents/edit-document?slug=' . urlencode($path);
44
        } else {
45
            return '';
46
        }
47
    }
48
49
    /**
50
     * Returns a button with a link for creating a new document
51
     * @param string $path
52
     * @param string $documentType
53
     * @return string
54
     * @throws \Exception
55
     */
56
    public static function newDocument($path = '/', $documentType = '')
57
    {
58
        if (self::isLoggedIn()) {
59
            $return = self::getAssetsIfNotIncluded();
60
            return $return . '<a title="New Document" data-href="' . self::newDocumentLink($path,
61
                    $documentType) . '" class="ccEditDocumentButton ccNewDocumentButton"></a>';
62
        } else {
63
            return '';
64
        }
65
    }
66
67
    /**
68
     * Returns the cms link for creating a new document
69
     * @param string $path
70
     * @param string $documentType
71
     * @return string
72
     * @throws \Exception
73
     */
74
    public static function newDocumentLink($path = '/', $documentType = '')
75
    {
76
        if (self::isLoggedIn()) {
77
            $path = 0 === strpos($path, '/') ? $path : '/' . $path;
78
            $linkPostFix = '';
79
            if ($documentType !== '') {
80
                $linkPostFix = '&amp;documentType=' . $documentType;
81
            }
82
            return Request::$subfolders . 'cms/documents/new-document?path=' . urlencode($path) . $linkPostFix;
83
        } else {
84
            return '';
85
        }
86
    }
87
88
89
    /**
90
     * See if a user is logged or wants to log in and
91
     * takes appropriate actions.
92
     *
93
     * @throws \Exception
94
     */
95
    private
96
    static function isLoggedIn()
97
    {
98
        return isset($_SESSION[CmsConstants::SESSION_PARAMETER_CLOUD_CONTROL]);
99
    }
100
101
    private
102
    static function getAssetsIfNotIncluded()
103
    {
104
        if (!self::$assetsIncluded) {
105
            self::$assetsIncluded = true;
106
            return '<style>
107
            .ccEditDocumentButton{
108
                opacity:0;
109
                -webkit-transition: opacity 0.5s;-moz-transition: opacity 0.5s;-ms-transition: opacity 0.5s;-o-transition: opacity 0.5s;transition: opacity 0.5s;
110
            }
111
            
112
            .ccEditDocumentButton.active {
113
                display:block;
114
                position:absolute;
115
                line-height:50px;
116
                width:50px;
117
                height:50px;                
118
                background: rgb(0, 106, 193) url("data:image/svg+xml;utf8,<svg width=\'25\' height=\'25\' viewBox=\'0 0 1792 1792\' xmlns=\'http://www.w3.org/2000/svg\'><path d=\'M491 1536l91-91-235-235-91 91v107h128v128h107zm523-928q0-22-22-22-10 0-17 7l-542 542q-7 7-7 17 0 22 22 22 10 0 17-7l542-542q7-7 7-17zm-54-192l416 416-832 832h-416v-416zm683 96q0 53-37 90l-166 166-416-416 166-165q36-38 90-38 53 0 91 38l235 234q37 39 37 91z\' fill=\'#fff\'/></svg>") no-repeat center;
119
                border-radius:50%;
120
                color:#fff;
121
                font-family:Arial, sans-serif;
122
                text-align:center;
123
                cursor:pointer;
124
                box-shadow: 5px 5px 5px rgba(128, 128, 128, 0.5);
125
                z-index:255;
126
                opacity:0.7;
127
            }
128
            
129
            @media all and (-ms-high-contrast: none), (-ms-high-contrast: active) {
130
              /* IE10+ CSS styles go here */
131
              .ccEditDocumentButton.active:before {
132
                content:\'edit\';
133
                font-family:Arial, sans-serif;
134
                font-size:12px;
135
                line-height:50px;
136
              }
137
            }
138
            
139
            .ccEditDocumentButton.active:hover {
140
                color:rgb(0, 106, 193);
141
                background: #fff url("data:image/svg+xml;utf8,<svg width=\'25\' height=\'25\' viewBox=\'0 0 1792 1792\' xmlns=\'http://www.w3.org/2000/svg\'><path d=\'M491 1536l91-91-235-235-91 91v107h128v128h107zm523-928q0-22-22-22-10 0-17 7l-542 542q-7 7-7 17 0 22 22 22 10 0 17-7l542-542q7-7 7-17zm-54-192l416 416-832 832h-416v-416zm683 96q0 53-37 90l-166 166-416-416 166-165q36-38 90-38 53 0 91 38l235 234q37 39 37 91z\' fill=\'#006AC1\'/></svg>") no-repeat center;
142
                opacity:1;
143
            }
144
            
145
            .ccEditDocumentButton.active.ccNewDocumentButton {
146
              background-image:none;
147
            }
148
            
149
            .ccEditDocumentButton.active.ccNewDocumentButton:before {
150
                content:\'+\';
151
                font-family:Arial, sans-serif;
152
                font-size:20px;
153
                line-height:50px;
154
              }
155
            
156
            .ccDocumentEditorHidden {
157
                transform: translateX(100%);
158
            }
159
            
160
            @keyframes slideInFromRight {
161
              0% {
162
                transform: translateX(100%);
163
              }
164
              100% {
165
                transform: translateX(0);
166
              }
167
            }
168
            
169
            @keyframes slideOutToRight {
170
              0% {
171
                transform: translateX(0);
172
              }
173
              100% {
174
                transform: translateX(100%);
175
              }
176
            }
177
            </style>' .
178
                '<script src="' . Request::$subfolders . 'js/cms.js"></script>';
179
        }
180
        return '';
181
    }
182
}
183
184