bookmarks.lib.php 9.2 KB

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