bookmarks.lib.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. require_once DOL_DOCUMENT_ROOT.'/bookmarks/class/bookmark.class.php';
  35. if (! isset($conf->global->BOOKMARKS_SHOW_IN_MENU)) $conf->global->BOOKMARKS_SHOW_IN_MENU=5;
  36. $langs->load("bookmarks");
  37. $url= $_SERVER["PHP_SELF"].(! empty($_SERVER["QUERY_STRING"])?'?'.$_SERVER["QUERY_STRING"]:'');
  38. $ret = '';
  39. // Menu bookmark
  40. $ret.= '<div class="menu_top"></div>'."\n";
  41. $ret.= '<!-- form with POST method by default, will be replaced with GET for external link by js -->'."\n";
  42. $ret.= '<form id="actionbookmark" name="actionbookmark" method="POST" action="">';
  43. $ret.= '<select name="bookmark" id="boxbookmark" class="flat boxcombo vmenusearchselectcombo" alt="Bookmarks">';
  44. $ret.= '<option hidden value="listbookmarks" class="optiongrey" selected rel="'.DOL_URL_ROOT.'/bookmarks/list.php">'.$langs->trans('Bookmarks').'</option>';
  45. $ret.= '<option value="listbookmark" class="optionblue" rel="'.dol_escape_htmltag(DOL_URL_ROOT.'/bookmarks/list.php').'">'.dol_escape_htmltag($user->rights->bookmark->creer ? $langs->trans('EditBookmarks') : $langs->trans('ListOfBookmarks')).'...</option>';
  46. // Url to go on create new bookmark page
  47. if ($user->rights->bookmark->creer)
  48. {
  49. $urltoadd=DOL_URL_ROOT.'/bookmarks/card.php?action=create&amp;urlsource='.urlencode($url).'&amp;url='.urlencode($url);
  50. $ret.= '<option value="newbookmark" class="optionblue" rel="'.dol_escape_htmltag($urltoadd).'">'.dol_escape_htmltag($langs->trans('AddThisPageToBookmarks')).'...</option>';
  51. }
  52. // Menu with all bookmarks
  53. if (! empty($conf->global->BOOKMARKS_SHOW_IN_MENU))
  54. {
  55. $sql = "SELECT rowid, title, url, target FROM ".MAIN_DB_PREFIX."bookmark";
  56. $sql.= " WHERE (fk_user = ".$user->id." OR fk_user is NULL OR fk_user = 0)";
  57. $sql.= " AND entity IN (".getEntity('bookmarks',1).")";
  58. $sql.= " ORDER BY position";
  59. if ($resql = $db->query($sql) )
  60. {
  61. $i=0;
  62. while ($i < $conf->global->BOOKMARKS_SHOW_IN_MENU && $obj = $db->fetch_object($resql))
  63. {
  64. $ret.='<option name="bookmark'.$obj->rowid.'" value="'.$obj->rowid.'" '.($obj->target == 1?' target="_blank"':'').' rel="'.dol_escape_htmltag($obj->url).'">';
  65. //$ret.='<span class="fa fa-print">aa</span>';
  66. $ret.=img_picto('','object_bookmark').' ';
  67. $ret.=$obj->title;
  68. $ret.='</option>';
  69. $i++;
  70. }
  71. }
  72. else
  73. {
  74. dol_print_error($db);
  75. }
  76. }
  77. $ret.= '</select>';
  78. $ret.= '</form>';
  79. $ret.=ajax_combobox('boxbookmark');
  80. $ret.='<script type="text/javascript">
  81. $(document).ready(function () {';
  82. $ret.=' jQuery("#boxbookmark").change(function() {
  83. var urlselected = jQuery("#boxbookmark option:selected").attr("rel");
  84. var urltarget = jQuery("#boxbookmark option:selected").attr("target");
  85. if (! urltarget) { urltarget=""; }
  86. jQuery("form#actionbookmark").attr("target",urltarget);
  87. jQuery("form#actionbookmark").attr("action",urlselected);
  88. console.log("We change select bookmark. We choose urlselected="+urlselected+" with target="+urltarget);
  89. // Method is POST for internal link, GET for external
  90. if (urlselected.startsWith(\'http\'))
  91. {
  92. var newmethod=\'GET\';
  93. jQuery("form#actionbookmark").attr("method",newmethod);
  94. console.log("We change method to newmethod="+newmethod);
  95. }
  96. jQuery("#actionbookmark").submit();
  97. });';
  98. $ret.='})</script>';
  99. $ret .= '<div class="menu_end"></div>';
  100. return $ret;
  101. }