bookmarks.lib.php 9.6 KB

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