Изменения документа Загрузить документ

Редактировал(а) HRlink 30.05.2025

От версии 74.1
отредактировано HRlink
на 30.05.2025
Изменить комментарий: К данной версии нет комментариев
К версии 78.1
отредактировано HRlink
на 30.05.2025
Изменить комментарий: К данной версии нет комментариев

Сводка

Подробности

Свойства страницы
Содержимое
... ... @@ -143,40 +143,76 @@
143 143  [[Добавьте к документу сотрудников>>https://wiki.myhrlink.ru/bin/view/Main/Кадровик/Документы/Добавить%20в%20документ%20нескольких%20сотрудников%20/]] или отправьте загруженные документы на [[подпись>>https://wiki.myhrlink.ru/bin/view/Main/Кадровик/Документы/Отправить%20документ%20на%20подпись%20/]]
144 144  
145 145  ----
146 +
146 146  {{velocity}}
147 -#set($likesKey = "$doc.fullName_LikeCount")
148 -#set($cookieName = "like_given_${likesKey}")
149 -#set($hasLiked = $request.getCookie($cookieName))
150 -#set($likesDoc = $xwiki.getDocument("XWiki.XWikiPreferences"))
151 -#set($obj = $likesDoc.getObject("XWiki.LikesStorageClass", "key", $likesKey))
152 -#if(!$obj)
153 - #set($discard = $likesDoc.newObject("XWiki.LikesStorageClass"))
154 - #set($obj = $likesDoc.getObject("XWiki.LikesStorageClass", "key", $likesKey))
155 - $obj.set("key", $likesKey)
156 - $obj.set("value", 0)
148 +#set($likesKey = $doc.fullName)
149 +#set($likesDoc = $xwiki.getDocument("XWiki.LikesStorage"))
150 +#set($likesObject = $likesDoc.getObject("XWiki.LikesStorageClass", "key", $likesKey))
151 +
152 +#if(!$likesObject)
153 + #set($likesObject = $likesDoc.newObject("XWiki.LikesStorageClass"))
154 + $likesObject.set("key", $likesKey)
155 + $likesObject.set("value", 0)
157 157   $xwiki.saveDocument($likesDoc)
158 158  #end
159 159  
160 -#set($likes = $obj.getIntValue("value"))
161 -
162 -## Увеличение, если кнопка нажата и не было лайка
163 -#if($request.getParameter("like") && !$hasLiked)
164 - #set($likes = $likes + 1)
165 - $obj.set("value", $likes)
159 +#if($request.getParameter("like") == "1")
160 + #set($likes = $likesObject.get("value"))
161 + $likesObject.set("value", $math.add($likes, 1))
166 166   $xwiki.saveDocument($likesDoc)
167 - $response.setCookie($cookieName, "true", 365*24*60*60)
168 168  #end
169 -{{/velocity}}
170 170  
165 +#set($likes = $likesObject.get("value"))
166 +#set($liked = $request.getCookie("liked-$likesKey"))
167 +#set($userLiked = false)
168 +#if("$!liked" == "true")
169 + #set($userLiked = true)
170 +#end
171 +
171 171  {{html clean="false"}}
172 -<div style="margin-top:30px; display:flex; align-items:center; gap:10px; font-size:16px;">
173 - <form method="post">
174 - <input type="hidden" name="like" value="1"/>
175 - <button type="submit" title="Поставить лайк" style="background:none; border:none; font-size:24px; cursor:pointer;">❤️</button>
176 - </form>
177 - <span style="font-size:18px;">$likes</span>
178 - <span>Нравится статья?</span>
173 +<div id="like-container" style="margin-top:20px; display:flex; align-items:center; gap:10px;">
174 + <button id="like-button" style="cursor:pointer; font-size: 20px; background: none; border: none;" title="Нравится">
175 + <span id="heart" style="color:#e74c3c;">❤️</span>
176 + </button>
177 + <span id="like-count" style="font-size:18px;">$likes</span>
178 + <span style="font-size:14px; color: #555;">Нравится статья?</span>
179 179  </div>
180 -{{/html}}
180 +<script>
181 + document.addEventListener('DOMContentLoaded', function () {
182 + const key = "$likesKey";
183 + const likeButton = document.getElementById('like-button');
184 + const likeCount = document.getElementById('like-count');
181 181  
182 -
186 + function setCookie(name, value, days) {
187 + const d = new Date();
188 + d.setTime(d.getTime() + (days*24*60*60*1000));
189 + document.cookie = name + "=" + value + "; expires=" + d.toUTCString() + "; path=/";
190 + }
191 +
192 + function getCookie(name) {
193 + const value = `; ${document.cookie}`;
194 + const parts = value.split(`; ${name}=`);
195 + if (parts.length === 2) return parts.pop().split(';').shift();
196 + }
197 +
198 + if (getCookie("liked-" + key) === "true") {
199 + likeButton.disabled = true;
200 + likeButton.style.opacity = 0.6;
201 + }
202 +
203 + likeButton.addEventListener('click', function () {
204 + if (getCookie("liked-" + key) !== "true") {
205 + fetch(window.location.href + "?like=1", { method: 'GET' })
206 + .then(() => {
207 + let current = parseInt(likeCount.innerText);
208 + likeCount.innerText = current + 1;
209 + setCookie("liked-" + key, "true", 365);
210 + likeButton.disabled = true;
211 + likeButton.style.opacity = 0.6;
212 + });
213 + }
214 + });
215 + });
216 +</script>
217 +{{/html}}
218 +{{/velocity}}