/core/last_visited_api.php

https://github.com/fusenigk/mantisbt-1 · PHP · 102 lines · 35 code · 12 blank · 55 comment · 6 complexity · aeee32120e8a7ba236ed1d927805bcd8 MD5 · raw file

  1. <?php
  2. # MantisBT - A PHP based bugtracking system
  3. # MantisBT is free software: you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation, either version 2 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # MantisBT is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with MantisBT. If not, see <http://www.gnu.org/licenses/>.
  15. /**
  16. * Last Visited API
  17. *
  18. * @package CoreAPI
  19. * @subpackage LastVisitedAPI
  20. * @copyright Copyright (C) 2000 - 2002 Kenzaburo Ito - kenito@300baud.org
  21. * @copyright Copyright (C) 2002 - 2011 MantisBT Team - mantisbt-dev@lists.sourceforge.net
  22. * @link http://www.mantisbt.org
  23. *
  24. * @uses bug_api.php
  25. * @uses config_api.php
  26. * @uses constant_inc.php
  27. * @uses current_user_api.php
  28. * @uses database_api.php
  29. * @uses tokens_api.php
  30. */
  31. require_api( 'bug_api.php' );
  32. require_api( 'config_api.php' );
  33. require_api( 'constant_inc.php' );
  34. require_api( 'current_user_api.php' );
  35. require_api( 'database_api.php' );
  36. require_api( 'tokens_api.php' );
  37. /**
  38. * Determine if last visited feature is enabled
  39. *
  40. * @return true: enabled; false: otherwise.
  41. * @access public
  42. */
  43. function last_visited_enabled() {
  44. return !( OFF == config_get( 'recently_visited' ) || current_user_is_anonymous() );
  45. }
  46. /**
  47. * This method should be called from view, update, print pages for issues,
  48. * mantisconnect.
  49. *
  50. * @param issue_id The issue id that was justed visited.
  51. * @param user_id The user id that visited the issue, or null for current
  52. * logged in user.
  53. * @access public
  54. */
  55. function last_visited_issue( $p_issue_id, $p_user_id = null ) {
  56. if( !last_visited_enabled() ) {
  57. return;
  58. }
  59. $c_issue_id = db_prepare_int( $p_issue_id );
  60. $t_value = token_get_value( TOKEN_LAST_VISITED, $p_user_id );
  61. if( is_null( $t_value ) ) {
  62. $t_value = $c_issue_id;
  63. } else {
  64. $t_ids = explode( ',', $p_issue_id . ',' . $t_value );
  65. $t_ids = array_unique( $t_ids );
  66. $t_ids = array_slice( $t_ids, 0, config_get( 'recently_visited_count' ) );
  67. $t_value = implode( ',', $t_ids );
  68. }
  69. token_set( TOKEN_LAST_VISITED, $t_value, TOKEN_EXPIRY_LAST_VISITED, $p_user_id );
  70. }
  71. /**
  72. * Get an array of the last visited bug ids. We intentionally don't check
  73. * if the ids still exists to avoid performance degradation.
  74. *
  75. * @param user_id The user id to get the last visited issues for,
  76. * or null for current logged in user.
  77. * @return An array of issue ids or an empty array if none found.
  78. * @access public
  79. */
  80. function last_visited_get_array( $p_user_id = null ) {
  81. $t_value = token_get_value( TOKEN_LAST_VISITED, $p_user_id );
  82. if( is_null( $t_value ) ) {
  83. return array();
  84. }
  85. # we don't slice the array here to optimise for performance. If the user reduces the number of recently
  86. # visited to track, then he/she will get the extra entries until visiting an issue.
  87. $t_ids = explode( ',', $t_value );
  88. bug_cache_array_rows( $t_ids );
  89. return $t_ids;
  90. }