bookmarks.lib.php 9.9 KB

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