dolibarr 25.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-2025 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
37 $langs->load("bookmarks");
38
39 $authorized_var=array('limit','optioncss','contextpage');
40 $url = $_SERVER["PHP_SELF"];
41 $url_param = array();
42 if (!empty($_SERVER["QUERY_STRING"])) {
43 if (is_array($_GET)) { // Parse the original GET URL. So we must keep $_GET here.
44 foreach ($_GET as $key => $val) {
45 if (is_array($val)) {
46 foreach ($val as $tmpsubval) {
47 $url_param[] = http_build_query(array(dol_escape_htmltag($key).'[]' => dol_escape_htmltag($tmpsubval)));
48 }
49 } elseif ($val != '') {
50 $url_param[$key] = http_build_query(array(dol_escape_htmltag($key) => dol_escape_htmltag($val)));
51 }
52 }
53 }
54 }
55
56 $tmpurl = '';
57 // No urlencode, all param $url will be urlencoded later
58 if ($sortfield) {
59 $tmpurl .= /* ($tmpurl ? '&' : ''). */'sortfield='.urlencode($sortfield);
60 }
61 if ($sortorder) {
62 $tmpurl .= ($tmpurl ? '&' : '').'sortorder='.urlencode($sortorder);
63 }
64 if (!empty($_POST) && is_array($_POST)) {
65 foreach ($_POST as $key => $val) {
66 if ((preg_match('/^search_/', $key) || in_array($key, $authorized_var))
67 && $val != ''
68 && !array_key_exists($key, $url_param)) {
69 if (is_array($val)) {
70 foreach ($val as $tmpsubval) {
71 $url_param[] = http_build_query(array(dol_escape_htmltag($key).'[]' => dol_escape_htmltag($tmpsubval)));
72 }
73 } elseif ($val != '') {
74 $url_param[$key] = http_build_query(array(dol_escape_htmltag($key) => dol_escape_htmltag($val)));
75 }
76 }
77 }
78 }
79
80 $url .= ($tmpurl ? '?'.$tmpurl : '');
81 if (!empty($url_param)) {
82 $url .= (strpos($url, '?') > 0 ? '&' : '?').implode('&', $url_param);
83 }
84
85 $searchForm = '<!-- form with POST method by default, will be replaced with GET for external link by js -->'."\n";
86 $searchForm .= '<form id="top-menu-action-bookmark" name="actionbookmark" method="POST" action=""'.(!getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER') ? ' onsubmit="return false"' : '').'>';
87 $searchForm .= '<input type="hidden" name="token" value="'.newToken().'">';
88
89 // Url to go on create new bookmark page
90 $newbtn = '';
91 if ($user->hasRight('bookmark', 'creer')) {
92 if (!preg_match('/bookmarks\/card.php/', $_SERVER['PHP_SELF'])) {
93 //$urltoadd=DOL_URL_ROOT.'/bookmarks/card.php?action=create&amp;url='.urlencode($url); // With &amp; the GETPOST('url') will fail.
94 $urltoadd = DOL_URL_ROOT.'/bookmarks/card.php?action=create&url='.urlencode($url);
95 $newbtn .= '<a class="top-menu-dropdown-link" title="'.$langs->trans('AddThisPageToBookmarks').'" href="'.dol_escape_htmltag($urltoadd).'" >';
96 $newbtn .= img_picto('', 'add', '', 0, 0, 0, '', 'pictofixedwidth paddingright').dol_escape_htmltag($langs->trans('AddThisPageToBookmarks')).'</a>';
97 }
98 }
99
100 // Url to list/edit bookmark
101 $listbtn = '<a class="top-menu-dropdown-link" title="'.dol_escape_htmltag($langs->trans('Bookmarks')).'" href="'.DOL_URL_ROOT.'/bookmarks/list.php">';
102 $listbtn .= img_picto('', 'edit', 'class="pictofixedwidth paddingright opacitymedium"').$langs->trans('EditBookmarks').'</a>';
103
104 $bookmarkList = '';
105 $bookmarkNb = 0;
106 // Menu with list of bookmarks
107 $sql = "SELECT rowid, title, url, target FROM ".MAIN_DB_PREFIX."bookmark";
108 $sql .= " WHERE (fk_user = ".((int) $user->id)." OR fk_user is NULL OR fk_user = 0)";
109 $sql .= " AND entity IN (".getEntity('bookmarks').")";
110 $sql .= " ORDER BY position";
111 if ($resql = $db->query($sql)) {
112 if (!getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
113 $bookmarkList = '<div id="dropdown-bookmarks-list" >';
114 $i = 0;
115 while ((!getDolGlobalString('BOOKMARKS_SHOW_IN_MENU') || $i < getDolGlobalInt('BOOKMARKS_SHOW_IN_MENU')) && $obj = $db->fetch_object($resql)) {
116 $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).'" >';
117 $bookmarkList .= dol_escape_htmltag($obj->title);
118 $bookmarkList .= '</a>';
119 $i++;
120 $bookmarkNb++;
121 }
122 $bookmarkList .= '</div>';
123
124 $searchForm .= '<input ';
125 $searchForm .= ' name="bookmark" ';
126 $searchForm .= ' id="top-bookmark-search-input" ';
127 $searchForm .= ' class="dropdown-search-input" ';
128 $searchForm .= ' placeholder="' . $langs->trans('Bookmarks') . '" ';
129 $searchForm .= ' data-search-tool-target="#dropdown-bookmarks-list .bookmark-item" ';
130 $searchForm .= ' data-counter-target="#top-bookmark-search-filter-count" ';
131 $searchForm .= ' data-no-item-target="#top-bookmark-search-nothing-found" ';
132 $searchForm .= ' autocomplete="off" ';
133 $searchForm .= ' >';
134 } else {
135 $searchForm .= '<select name="bookmark" id="boxbookmark" class="topmenu-bookmark-dropdown .dropdown-toggle maxwidth100">';
136 $searchForm .= '<option hidden value="listbookmarks" class="optiongrey" selected rel="'.DOL_URL_ROOT.'/bookmarks/list.php">'.$langs->trans('Bookmarks').'</option>';
137 $searchForm .= '<option value="listbookmark" class="optionblue" rel="'.dol_escape_htmltag(DOL_URL_ROOT.'/bookmarks/list.php').'" ';
138 $searchForm .= ' data-html="'.dol_escape_htmltag(img_picto('', 'bookmark').' '.($user->hasRight('bookmark', 'creer') ? $langs->trans('EditBookmarks') : $langs->trans('ListOfBookmarks')).'...').'">';
139 $searchForm .= dol_escape_htmltag($user->hasRight('bookmark', 'creer') ? $langs->trans('EditBookmarks') : $langs->trans('ListOfBookmarks')).'...</option>';
140 // Url to go on create new bookmark page
141 if ($user->hasRight('bookmark', 'creer')) {
142 if (!preg_match('/bookmarks\/card.php/', $_SERVER['PHP_SELF'])) {
143 $urltoadd = DOL_URL_ROOT.'/bookmarks/card.php?action=create&amp;url='.urlencode($url);
144 $searchForm .= '<option value="newbookmark" class="optionblue" rel="'.dol_escape_htmltag($urltoadd).'"';
145 $searchForm .= ' data-html="'.dol_escape_htmltag(img_picto('', 'bookmark').' '.$langs->trans('AddThisPageToBookmarks').'...').'">'.dol_escape_htmltag($langs->trans('AddThisPageToBookmarks').'...').'</option>';
146 }
147 }
148 $i = 0;
149 while ((!getDolGlobalString('BOOKMARKS_SHOW_IN_MENU') || $i < getDolGlobalInt('BOOKMARKS_SHOW_IN_MENU')) && $obj = $db->fetch_object($resql)) {
150 $searchForm .= '<option name="bookmark'.$obj->rowid.'" value="'.$obj->rowid.'" '.($obj->target == 1 ? ' target="_blank" rel="noopener noreferrer"' : '').' rel="'.dol_escape_htmltag($obj->url).'" >';
151 $searchForm .= dol_escape_htmltag($obj->title);
152 $searchForm .= '</option>';
153 $i++;
154 $bookmarkNb++;
155 }
156 $searchForm .= '</select>';
157 }
158 } else {
160 }
161
162 $searchForm .= '</form>';
163
164 // Generate the return string
165 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
166 $html = $searchForm;
167
168 $html .= '<!-- script to open selected choice -->
169 <script>
170 $(document).ready(function () {
171 jQuery("#boxbookmark").change(function() {
172 var urlselected = jQuery("#boxbookmark option:selected").attr("rel");
173 if (! urlselected) console.log("Error, failed to get the URL to jump to from the rel attribute");
174 var urltarget = jQuery("#boxbookmark option:selected").attr("target");
175 if (! urltarget) { urltarget=""; }
176 jQuery("form#top-menu-action-bookmark").attr("target",urltarget);
177 jQuery("form#top-menu-action-bookmark").attr("action",urlselected);
178
179 console.log("We change select bookmark. We choose urlselected="+urlselected+" with target="+urltarget);
180
181 // Method is POST for internal link, GET for external
182 if (urlselected.startsWith(\'http\'))
183 {
184 var newmethod=\'GET\';
185 jQuery("form#top-menu-action-bookmark").attr("method", newmethod);
186 console.log("We change method to newmethod="+newmethod);
187 jQuery("form#top-menu-action-bookmark").submit();
188 console.log("We restore method to POST");
189 jQuery("form#top-menu-action-bookmark").attr("method", \'POST\');
190 }
191 else
192 {
193 jQuery("form#top-menu-action-bookmark").submit();
194 }
195 });
196 })
197 </script>';
198 } else {
199 $html = '
200 <!-- search input -->
201 <div class="dropdown-header bookmark-header">
202 ' . $searchForm.'
203 </div>
204 ';
205
206 $html .= '
207 <!-- Menu bookmark tools-->
208 <div class="bookmark-footer">
209 '.$newbtn.$listbtn.'
210 <div class="clearboth"></div>
211 </div>
212 ';
213
214 $html .= '
215 <!-- Menu Body bookmarks -->
216 <div class="bookmark-body dropdown-body">'.$bookmarkList.'
217 <span id="top-bookmark-search-nothing-found" class="'.($bookmarkNb ? 'hidden-search-result ' : '').'opacitymedium">'.dol_escape_htmltag($langs->trans("NoBookmarkFound")).'</span>
218 </div>
219 ';
220 }
221
222 return $html;
223}
printDropdownBookmarksList()
Add area with bookmarks in top menu.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2, $allowothertags=array())
Show picto whatever it's its name (generic function)
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
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...
Definition html.lib.php:172