bookmarks.lib.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. /**
  18. * \file htdocs/bookmarks/bookmarks.lib.php
  19. * \ingroup bookmarks
  20. * \brief File with library for bookmark module
  21. */
  22. /**
  23. * Add area with bookmarks in top menu
  24. *
  25. * @return string
  26. */
  27. function printDropdownBookmarksList()
  28. {
  29. global $conf, $user, $db, $langs;
  30. require_once DOL_DOCUMENT_ROOT.'/bookmarks/class/bookmark.class.php';
  31. require_once DOL_DOCUMENT_ROOT.'/core/lib/functions.lib.php';
  32. $langs->load("bookmarks");
  33. $url = $_SERVER["PHP_SELF"];
  34. if (!empty($_SERVER["QUERY_STRING"])) {
  35. $url .= (dol_escape_htmltag($_SERVER["QUERY_STRING"]) ? '?'.dol_escape_htmltag($_SERVER["QUERY_STRING"]) : '');
  36. } else {
  37. global $sortfield, $sortorder;
  38. $tmpurl = '';
  39. // No urlencode, all param $url will be urlencoded later
  40. if ($sortfield) {
  41. $tmpurl .= ($tmpurl ? '&' : '').'sortfield='.urlencode($sortfield);
  42. }
  43. if ($sortorder) {
  44. $tmpurl .= ($tmpurl ? '&' : '').'sortorder='.urlencode($sortorder);
  45. }
  46. if (is_array($_POST)) {
  47. foreach ($_POST as $key => $val) {
  48. if (preg_match('/^search_/', $key) && $val != '') {
  49. $tmpurl .= ($tmpurl ? '&' : '').http_build_query(array($key => $val));
  50. }
  51. }
  52. }
  53. $url .= ($tmpurl ? '?'.$tmpurl : '');
  54. }
  55. $searchForm = '<!-- form with POST method by default, will be replaced with GET for external link by js -->'."\n";
  56. $searchForm .= '<form id="top-menu-action-bookmark" name="actionbookmark" method="POST" action=""'.(empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER) ? ' onsubmit="return false"' : '').'>';
  57. $searchForm .= '<input type="hidden" name="token" value="'.newToken().'">';
  58. // Url to list bookmark
  59. $listbtn = '<a class="top-menu-dropdown-link" title="'.dol_escape_htmltag($langs->trans('Bookmarks')).'" href="'.DOL_URL_ROOT.'/bookmarks/list.php">';
  60. $listbtn .= img_picto('', 'bookmark', 'class="paddingright"').$langs->trans('Bookmarks').'</a>';
  61. // Url to go on create new bookmark page
  62. $newbtn = '';
  63. if (!empty($user->rights->bookmark->creer)) {
  64. if (!preg_match('/bookmarks\/card.php/', $_SERVER['PHP_SELF'])) {
  65. //$urltoadd=DOL_URL_ROOT.'/bookmarks/card.php?action=create&amp;urlsource='.urlencode($url).'&amp;url='.urlencode($url);
  66. $urltoadd = DOL_URL_ROOT.'/bookmarks/card.php?action=create&amp;url='.urlencode($url);
  67. $newbtn .= '<a class="top-menu-dropdown-link" title="'.$langs->trans('AddThisPageToBookmarks').'" href="'.dol_escape_htmltag($urltoadd).'" >';
  68. $newbtn .= img_picto('', 'add', '', false, 0, 0, '', 'paddingright').dol_escape_htmltag($langs->trans('AddThisPageToBookmarks')).'</a>';
  69. }
  70. }
  71. // Menu with list of bookmarks
  72. $sql = "SELECT rowid, title, url, target FROM ".MAIN_DB_PREFIX."bookmark";
  73. $sql .= " WHERE (fk_user = ".((int) $user->id)." OR fk_user is NULL OR fk_user = 0)";
  74. $sql .= " AND entity IN (".getEntity('bookmarks').")";
  75. $sql .= " ORDER BY position";
  76. if ($resql = $db->query($sql)) {
  77. if (empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
  78. $bookmarkList = '<div id="dropdown-bookmarks-list" >';
  79. $i = 0;
  80. while ((empty($conf->global->BOOKMARKS_SHOW_IN_MENU) || $i < $conf->global->BOOKMARKS_SHOW_IN_MENU) && $obj = $db->fetch_object($resql)) {
  81. $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).'" >';
  82. $bookmarkList .= dol_escape_htmltag($obj->title);
  83. $bookmarkList .= '</a>';
  84. $i++;
  85. }
  86. $bookmarkList .= '</div>';
  87. $searchForm .= '<input name="bookmark" id="top-bookmark-search-input" class="dropdown-search-input" placeholder="'.$langs->trans('Bookmarks').'" autocomplete="off" >';
  88. } else {
  89. $searchForm .= '<select name"=bookmark" id="boxbookmark" class="topmenu-bookmark-dropdown .dropdown-toggle">';
  90. //$searchForm .= '<option>--'.$langs->trans("Bookmarks").'--</option>';
  91. $searchForm .= '<option hidden value="listbookmarks" class="optiongrey" selected rel="'.DOL_URL_ROOT.'/bookmarks/list.php">'.$langs->trans('Bookmarks').'</option>';
  92. $searchForm .= '<option value="listbookmark" class="optionblue" rel="'.dol_escape_htmltag(DOL_URL_ROOT.'/bookmarks/list.php').'" ';
  93. $searchForm .= ' data-html="'.dol_escape_htmltag(img_picto('', 'bookmark').' '.($user->rights->bookmark->creer ? $langs->trans('EditBookmarks') : $langs->trans('ListOfBookmarks')).'...').'">';
  94. $searchForm .= dol_escape_htmltag($user->rights->bookmark->creer ? $langs->trans('EditBookmarks') : $langs->trans('ListOfBookmarks')).'...</option>';
  95. // Url to go on create new bookmark page
  96. if (!empty($user->rights->bookmark->creer)) {
  97. if (!preg_match('/bookmarks\/card.php/', $_SERVER['PHP_SELF'])) {
  98. $urltoadd = DOL_URL_ROOT.'/bookmarks/card.php?action=create&amp;url='.urlencode($url);
  99. $searchForm .= '<option value="newbookmark" class="optionblue" rel="'.dol_escape_htmltag($urltoadd).'"';
  100. $searchForm .= ' data-html="'.dol_escape_htmltag(img_picto('', 'bookmark').' '.$langs->trans('AddThisPageToBookmarks').'...').'">'.dol_escape_htmltag($langs->trans('AddThisPageToBookmarks').'...').'</option>';
  101. }
  102. }
  103. $i = 0;
  104. while ((empty($conf->global->BOOKMARKS_SHOW_IN_MENU) || $i < $conf->global->BOOKMARKS_SHOW_IN_MENU) && $obj = $db->fetch_object($resql)) {
  105. $searchForm .= '<option name="bookmark'.$obj->rowid.'" value="'.$obj->rowid.'" '.($obj->target == 1 ? ' target="_blank" rel="noopener noreferrer"' : '').' rel="'.dol_escape_htmltag($obj->url).'" >';
  106. $searchForm .= dol_escape_htmltag($obj->title);
  107. $searchForm .= '</option>';
  108. $i++;
  109. }
  110. $searchForm .= '</select>';
  111. }
  112. } else {
  113. dol_print_error($db);
  114. }
  115. $searchForm .= '</form>';
  116. // Generate the return string
  117. if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
  118. $html = $searchForm;
  119. $html .= '<!-- script to open selected choice -->
  120. <script>
  121. $(document).ready(function () {
  122. jQuery("#boxbookmark").change(function() {
  123. var urlselected = jQuery("#boxbookmark option:selected").attr("rel");
  124. if (! urlselected) console.log("Error, failed to get the URL to jump to from the rel attribute");
  125. var urltarget = jQuery("#boxbookmark option:selected").attr("target");
  126. if (! urltarget) { urltarget=""; }
  127. jQuery("form#top-menu-action-bookmark").attr("target",urltarget);
  128. jQuery("form#top-menu-action-bookmark").attr("action",urlselected);
  129. console.log("We change select bookmark. We choose urlselected="+urlselected+" with target="+urltarget);
  130. // Method is POST for internal link, GET for external
  131. if (urlselected.startsWith(\'http\'))
  132. {
  133. var newmethod=\'GET\';
  134. jQuery("form#top-menu-action-bookmark").attr("method", newmethod);
  135. console.log("We change method to newmethod="+newmethod);
  136. jQuery("form#top-menu-action-bookmark").submit();
  137. console.log("We restore method to POST");
  138. jQuery("form#top-menu-action-bookmark").attr("method", \'POST\');
  139. }
  140. else
  141. {
  142. jQuery("form#top-menu-action-bookmark").submit();
  143. }
  144. });
  145. })
  146. </script>';
  147. } else {
  148. $html = '
  149. <!-- search input -->
  150. <div class="dropdown-header bookmark-header">
  151. ' . $searchForm.'
  152. </div>
  153. ';
  154. $html .= '
  155. <!-- Menu Body -->
  156. <div class="bookmark-body dropdown-body">
  157. '.$bookmarkList.'
  158. </div>
  159. ';
  160. $html .= '
  161. <!-- Menu Footer-->
  162. <div class="bookmark-footer">
  163. '.$newbtn.$listbtn.'
  164. <div style="clear:both;"></div>
  165. </div>
  166. ';
  167. $html .= '<!-- script to open/close the popup -->
  168. <script>
  169. $( document ).on("keyup", "#top-bookmark-search-input", function () {
  170. var filter = $(this).val(), count = 0;
  171. $("#dropdown-bookmarks-list .bookmark-item").each(function () {
  172. if ($(this).text().search(new RegExp(filter, "i")) < 0) {
  173. $(this).addClass("hidden-search-result");
  174. } else {
  175. $(this).removeClass("hidden-search-result");
  176. count++;
  177. }
  178. });
  179. $("#top-bookmark-search-filter-count").text(count);
  180. });
  181. </script>';
  182. }
  183. return $html;
  184. }