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