dolibarr 21.0.0-alpha
bookmarks.lib.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2009 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
32{
33 global $user, $db, $langs, $sortfield, $sortorder;
34
35 require_once DOL_DOCUMENT_ROOT.'/bookmarks/class/bookmark.class.php';
36 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions.lib.php';
37
38 $langs->load("bookmarks");
39
40 $authorized_var=array('limit','optioncss','contextpage');
41 $url = $_SERVER["PHP_SELF"];
42 $url_param = array();
43 if (!empty($_SERVER["QUERY_STRING"])) {
44 if (is_array($_GET)) { // Parse the original GET URL. So we must keep $_GET here.
45 foreach ($_GET as $key => $val) {
46 if (is_array($val)) {
47 foreach ($val as $tmpsubval) {
48 $url_param[] = http_build_query(array(dol_escape_htmltag($key).'[]' => dol_escape_htmltag($tmpsubval)));
49 }
50 } elseif ($val != '') {
51 $url_param[$key] = http_build_query(array(dol_escape_htmltag($key) => dol_escape_htmltag($val)));
52 }
53 }
54 }
55 }
56
57 $tmpurl = '';
58 // No urlencode, all param $url will be urlencoded later
59 if ($sortfield) {
60 $tmpurl .= ($tmpurl ? '&' : '').'sortfield='.urlencode($sortfield);
61 }
62 if ($sortorder) {
63 $tmpurl .= ($tmpurl ? '&' : '').'sortorder='.urlencode($sortorder);
64 }
65 if (!empty($_POST) && is_array($_POST)) {
66 foreach ($_POST as $key => $val) {
67 if ((preg_match('/^search_/', $key) || in_array($key, $authorized_var))
68 && $val != ''
69 && !array_key_exists($key, $url_param)) {
70 if (is_array($val)) {
71 foreach ($val as $tmpsubval) {
72 $url_param[] = http_build_query(array(dol_escape_htmltag($key).'[]' => dol_escape_htmltag($tmpsubval)));
73 }
74 } elseif ($val != '') {
75 $url_param[$key] = http_build_query(array(dol_escape_htmltag($key) => dol_escape_htmltag($val)));
76 }
77 }
78 }
79 }
80
81 $url .= ($tmpurl ? '?'.$tmpurl : '');
82 if (!empty($url_param)) {
83 $url .= (strpos($url, '?') > 0 ? '&' : '?').implode('&', $url_param);
84 }
85
86 $searchForm = '<!-- form with POST method by default, will be replaced with GET for external link by js -->'."\n";
87 $searchForm .= '<form id="top-menu-action-bookmark" name="actionbookmark" method="POST" action=""'.(!getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER') ? ' onsubmit="return false"' : '').'>';
88 $searchForm .= '<input type="hidden" name="token" value="'.newToken().'">';
89
90 // Url to go on create new bookmark page
91 $newbtn = '';
92 if ($user->hasRight('bookmark', 'creer')) {
93 if (!preg_match('/bookmarks\/card.php/', $_SERVER['PHP_SELF'])) {
94 //$urltoadd=DOL_URL_ROOT.'/bookmarks/card.php?action=create&amp;urlsource='.urlencode($url).'&amp;url='.urlencode($url);
95 $urltoadd = DOL_URL_ROOT.'/bookmarks/card.php?action=create&amp;url='.urlencode($url);
96 $newbtn .= '<a class="top-menu-dropdown-link" title="'.$langs->trans('AddThisPageToBookmarks').'" href="'.dol_escape_htmltag($urltoadd).'" >';
97 $newbtn .= img_picto('', 'add', '', 0, 0, 0, '', 'pictofixedwidth paddingright').dol_escape_htmltag($langs->trans('AddThisPageToBookmarks')).'</a>';
98 }
99 }
100
101 // Url to list/edit bookmark
102 $listbtn = '<a class="top-menu-dropdown-link" title="'.dol_escape_htmltag($langs->trans('Bookmarks')).'" href="'.DOL_URL_ROOT.'/bookmarks/list.php">';
103 $listbtn .= img_picto('', 'edit', 'class="pictofixedwidth paddingright opacitymedium"').$langs->trans('EditBookmarks').'</a>';
104
105 $bookmarkList = '';
106 $bookmarkNb = 0;
107 // Menu with list of bookmarks
108 $sql = "SELECT rowid, title, url, target FROM ".MAIN_DB_PREFIX."bookmark";
109 $sql .= " WHERE (fk_user = ".((int) $user->id)." OR fk_user is NULL OR fk_user = 0)";
110 $sql .= " AND entity IN (".getEntity('bookmarks').")";
111 $sql .= " ORDER BY position";
112 if ($resql = $db->query($sql)) {
113 if (!getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
114 $bookmarkList = '<div id="dropdown-bookmarks-list" >';
115 $i = 0;
116 while ((!getDolGlobalString('BOOKMARKS_SHOW_IN_MENU') || $i < getDolGlobalInt('BOOKMARKS_SHOW_IN_MENU')) && $obj = $db->fetch_object($resql)) {
117 $bookmarkList .= '<a class="dropdown-item bookmark-item'.(strpos($obj->url, 'http') === 0 ? ' bookmark-item-external' : '').'" id="bookmark-item-'.$obj->rowid.'" data-id="'.$obj->rowid.'" '.($obj->target == 1 ? ' target="_blank" rel="noopener noreferrer"' : '').' href="'.dol_escape_htmltag($obj->url).'" >';
118 $bookmarkList .= dol_escape_htmltag($obj->title);
119 $bookmarkList .= '</a>';
120 $i++;
121 $bookmarkNb++;
122 }
123 $bookmarkList .= '</div>';
124
125 $searchForm .= '<input name="bookmark" id="top-bookmark-search-input" class="dropdown-search-input" placeholder="'.$langs->trans('Bookmarks').'" autocomplete="off" >';
126 } else {
127 $searchForm .= '<select name="bookmark" id="boxbookmark" class="topmenu-bookmark-dropdown .dropdown-toggle maxwidth100">';
128 $searchForm .= '<option hidden value="listbookmarks" class="optiongrey" selected rel="'.DOL_URL_ROOT.'/bookmarks/list.php">'.$langs->trans('Bookmarks').'</option>';
129 $searchForm .= '<option value="listbookmark" class="optionblue" rel="'.dol_escape_htmltag(DOL_URL_ROOT.'/bookmarks/list.php').'" ';
130 $searchForm .= ' data-html="'.dol_escape_htmltag(img_picto('', 'bookmark').' '.($user->hasRight('bookmark', 'creer') ? $langs->trans('EditBookmarks') : $langs->trans('ListOfBookmarks')).'...').'">';
131 $searchForm .= dol_escape_htmltag($user->hasRight('bookmark', 'creer') ? $langs->trans('EditBookmarks') : $langs->trans('ListOfBookmarks')).'...</option>';
132 // Url to go on create new bookmark page
133 if ($user->hasRight('bookmark', 'creer')) {
134 if (!preg_match('/bookmarks\/card.php/', $_SERVER['PHP_SELF'])) {
135 $urltoadd = DOL_URL_ROOT.'/bookmarks/card.php?action=create&amp;url='.urlencode($url);
136 $searchForm .= '<option value="newbookmark" class="optionblue" rel="'.dol_escape_htmltag($urltoadd).'"';
137 $searchForm .= ' data-html="'.dol_escape_htmltag(img_picto('', 'bookmark').' '.$langs->trans('AddThisPageToBookmarks').'...').'">'.dol_escape_htmltag($langs->trans('AddThisPageToBookmarks').'...').'</option>';
138 }
139 }
140 $i = 0;
141 while ((!getDolGlobalString('BOOKMARKS_SHOW_IN_MENU') || $i < getDolGlobalInt('BOOKMARKS_SHOW_IN_MENU')) && $obj = $db->fetch_object($resql)) {
142 $searchForm .= '<option name="bookmark'.$obj->rowid.'" value="'.$obj->rowid.'" '.($obj->target == 1 ? ' target="_blank" rel="noopener noreferrer"' : '').' rel="'.dol_escape_htmltag($obj->url).'" >';
143 $searchForm .= dol_escape_htmltag($obj->title);
144 $searchForm .= '</option>';
145 $i++;
146 $bookmarkNb++;
147 }
148 $searchForm .= '</select>';
149 }
150 } else {
151 dol_print_error($db);
152 }
153
154 $searchForm .= '</form>';
155
156 // Generate the return string
157 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
158 $html = $searchForm;
159
160 $html .= '<!-- script to open selected choice -->
161 <script>
162 $(document).ready(function () {
163 jQuery("#boxbookmark").change(function() {
164 var urlselected = jQuery("#boxbookmark option:selected").attr("rel");
165 if (! urlselected) console.log("Error, failed to get the URL to jump to from the rel attribute");
166 var urltarget = jQuery("#boxbookmark option:selected").attr("target");
167 if (! urltarget) { urltarget=""; }
168 jQuery("form#top-menu-action-bookmark").attr("target",urltarget);
169 jQuery("form#top-menu-action-bookmark").attr("action",urlselected);
170
171 console.log("We change select bookmark. We choose urlselected="+urlselected+" with target="+urltarget);
172
173 // Method is POST for internal link, GET for external
174 if (urlselected.startsWith(\'http\'))
175 {
176 var newmethod=\'GET\';
177 jQuery("form#top-menu-action-bookmark").attr("method", newmethod);
178 console.log("We change method to newmethod="+newmethod);
179 jQuery("form#top-menu-action-bookmark").submit();
180 console.log("We restore method to POST");
181 jQuery("form#top-menu-action-bookmark").attr("method", \'POST\');
182 }
183 else
184 {
185 jQuery("form#top-menu-action-bookmark").submit();
186 }
187 });
188 })
189 </script>';
190 } else {
191 $html = '
192 <!-- search input -->
193 <div class="dropdown-header bookmark-header">
194 ' . $searchForm.'
195 </div>
196 ';
197
198 $html .= '
199 <!-- Menu bookmark tools-->
200 <div class="bookmark-footer">
201 '.$newbtn.$listbtn.'
202 <div class="clearboth"></div>
203 </div>
204 ';
205
206 $html .= '
207 <!-- Menu Body bookmarks -->
208 <div class="bookmark-body dropdown-body">'.$bookmarkList.'
209 <span id="top-bookmark-search-nothing-found" class="'.($bookmarkNb ? 'hidden-search-result ' : '').'opacitymedium">'.dol_escape_htmltag($langs->trans("NoBookmarkFound")).'</span>
210 </div>
211 ';
212
213 $html .= '<!-- script to open/close the popup -->
214 <script>
215 jQuery(document).on("keyup", "#top-bookmark-search-input", function () {
216 console.log("keyup in bookmark search input");
217
218 var filter = $(this).val(), count = 0;
219 jQuery("#dropdown-bookmarks-list .bookmark-item").each(function () {
220 if ($(this).text().search(new RegExp(filter, "i")) < 0) {
221 $(this).addClass("hidden-search-result");
222 } else {
223 $(this).removeClass("hidden-search-result");
224 count++;
225 }
226 });
227 jQuery("#top-bookmark-search-filter-count").text(count);
228 if (count == 0) {
229 jQuery("#top-bookmark-search-nothing-found").removeClass("hidden-search-result");
230 } else {
231 jQuery("#top-bookmark-search-nothing-found").addClass("hidden-search-result");
232 }
233 });
234 </script>';
235 }
236
237 return $html;
238}
printDropdownBookmarksList()
Add area with bookmarks in top menu.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...