bookmarks.lib.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 <http://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 menu
  24. *
  25. * @param DoliDb $aDb Database handler
  26. * @param Translate $aLangs Object lang
  27. * @return string
  28. */
  29. function printBookmarksList($aDb, $aLangs)
  30. {
  31. global $conf, $user;
  32. $db = $aDb;
  33. $langs = $aLangs;
  34. $ret = '<div class="menu_top"></div>'."\n";
  35. if (! empty($conf->use_javascript_ajax)) { // Bookmark autosubmit can't work when javascript is off.
  36. require_once DOL_DOCUMENT_ROOT.'/bookmarks/class/bookmark.class.php';
  37. if (! isset($conf->global->BOOKMARKS_SHOW_IN_MENU)) $conf->global->BOOKMARKS_SHOW_IN_MENU=5;
  38. $langs->load("bookmarks");
  39. $url= $_SERVER["PHP_SELF"];
  40. if (! empty($_SERVER["QUERY_STRING"]))
  41. {
  42. $url.=(dol_escape_htmltag($_SERVER["QUERY_STRING"])?'?'.dol_escape_htmltag($_SERVER["QUERY_STRING"]):'');
  43. }
  44. else
  45. {
  46. global $sortfield,$sortorder;
  47. $tmpurl='';
  48. // No urlencode, all param $url will be urlencoded later
  49. if ($sortfield) $tmpurl.=($tmpurl?'&':'').'sortfield='.$sortfield;
  50. if ($sortorder) $tmpurl.=($tmpurl?'&':'').'sortorder='.$sortorder;
  51. if (is_array($_POST))
  52. {
  53. foreach($_POST as $key => $val)
  54. {
  55. if (preg_match('/^search_/', $key) && $val != '') $tmpurl.=($tmpurl?'&':'').$key.'='.$val;
  56. }
  57. }
  58. $url.=($tmpurl?'?'.$tmpurl:'');
  59. }
  60. // Menu bookmark
  61. $ret = '<div class="menu_top"></div>'."\n";
  62. $ret.= '<!-- form with POST method by default, will be replaced with GET for external link by js -->'."\n";
  63. $ret.= '<form id="actionbookmark" name="actionbookmark" method="POST" action="">';
  64. $ret.= '<select name="bookmark" id="boxbookmark" class="flat boxcombo vmenusearchselectcombo" alt="Bookmarks">';
  65. $ret.= '<option hidden value="listbookmarks" class="optiongrey" selected rel="'.DOL_URL_ROOT.'/bookmarks/list.php">'.$langs->trans('Bookmarks').'</option>';
  66. $ret.= '<option value="listbookmark" class="optionblue" rel="'.dol_escape_htmltag(DOL_URL_ROOT.'/bookmarks/list.php').'" ';
  67. $ret.= ' data-html="'.dol_escape_htmltag('<span class="fa fa-star-o"></span> '.dol_escape_htmltag($user->rights->bookmark->creer ? $langs->trans('EditBookmarks') : $langs->trans('ListOfBookmarks')).'...').'">';
  68. $ret.= dol_escape_htmltag($user->rights->bookmark->creer ? $langs->trans('EditBookmarks') : $langs->trans('ListOfBookmarks')).'...</option>';
  69. // Url to go on create new bookmark page
  70. if (! empty($user->rights->bookmark->creer))
  71. {
  72. //$urltoadd=DOL_URL_ROOT.'/bookmarks/card.php?action=create&amp;urlsource='.urlencode($url).'&amp;url='.urlencode($url);
  73. $urltoadd=DOL_URL_ROOT.'/bookmarks/card.php?action=create&amp;url='.urlencode($url);
  74. $ret.= '<option value="newbookmark" class="optionblue" rel="'.dol_escape_htmltag($urltoadd).'"';
  75. $ret.= ' data-html="'.dol_escape_htmltag('<span class="fa fa-star-o"></span> '.$langs->trans('AddThisPageToBookmarks').'...').'">'.dol_escape_htmltag($langs->trans('AddThisPageToBookmarks').'...').'</option>';
  76. }
  77. // Menu with all bookmarks
  78. if (! empty($conf->global->BOOKMARKS_SHOW_IN_MENU))
  79. {
  80. $sql = "SELECT rowid, title, url, target FROM ".MAIN_DB_PREFIX."bookmark";
  81. $sql.= " WHERE (fk_user = ".$user->id." OR fk_user is NULL OR fk_user = 0)";
  82. $sql.= " AND entity IN (".getEntity('bookmarks').")";
  83. $sql.= " ORDER BY position";
  84. if ($resql = $db->query($sql) )
  85. {
  86. $i=0;
  87. while ($i < $conf->global->BOOKMARKS_SHOW_IN_MENU && $obj = $db->fetch_object($resql))
  88. {
  89. $ret.='<option name="bookmark'.$obj->rowid.'" value="'.$obj->rowid.'" '.($obj->target == 1?' target="_blank"':'').' rel="'.dol_escape_htmltag($obj->url).'"';
  90. //$ret.=' data-html="'.dol_escape_htmltag('<span class="fa fa-print"></span> '.$obj->title).'"';
  91. $ret.='>';
  92. $ret.=dol_escape_htmltag($obj->title);
  93. $ret.='</option>';
  94. $i++;
  95. }
  96. }
  97. else
  98. {
  99. dol_print_error($db);
  100. }
  101. }
  102. $ret.= '</select>';
  103. $ret.= '</form>';
  104. $ret.=ajax_combobox('boxbookmark');
  105. $ret.='<script type="text/javascript">
  106. $(document).ready(function () {';
  107. $ret.=' jQuery("#boxbookmark").change(function() {
  108. var urlselected = jQuery("#boxbookmark option:selected").attr("rel");
  109. if (! urlselected) console.log("Error, failed to get the URL to jump to from the rel attribute");
  110. var urltarget = jQuery("#boxbookmark option:selected").attr("target");
  111. if (! urltarget) { urltarget=""; }
  112. jQuery("form#actionbookmark").attr("target",urltarget);
  113. jQuery("form#actionbookmark").attr("action",urlselected);
  114. console.log("We change select bookmark. We choose urlselected="+urlselected+" with target="+urltarget);
  115. // Method is POST for internal link, GET for external
  116. if (urlselected.startsWith(\'http\'))
  117. {
  118. var newmethod=\'GET\';
  119. jQuery("form#actionbookmark").attr("method", newmethod);
  120. console.log("We change method to newmethod="+newmethod);
  121. jQuery("#actionbookmark").submit();
  122. console.log("We restore method to POST");
  123. jQuery("form#actionbookmark").attr("method", \'POST\');
  124. }
  125. else
  126. {
  127. jQuery("#actionbookmark").submit();
  128. }
  129. });';
  130. $ret.='})</script>';
  131. }
  132. $ret.= '<div class="menu_end"></div>'."\n";
  133. return $ret;
  134. }