PageRenderTime 30ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/mantisbt-1.2.8/core/last_visited_api.php

#
PHP | 91 lines | 30 code | 12 blank | 49 comment | 6 complexity | 73848d516f1eb1c7cdf185e3556f3802 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  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. * @package CoreAPI
  17. * @subpackage LastVisitedAPI
  18. * @copyright Copyright (C) 2000 - 2002 Kenzaburo Ito - kenito@300baud.org
  19. * @copyright Copyright (C) 2002 - 2011 MantisBT Team - mantisbt-dev@lists.sourceforge.net
  20. * @link http://www.mantisbt.org
  21. */
  22. /**
  23. * requires tokens_api
  24. */
  25. require_once( 'tokens_api.php' );
  26. /**
  27. * Determine if last visited feature is enabled
  28. *
  29. * @return true: enabled; false: otherwise.
  30. * @access public
  31. */
  32. function last_visited_enabled() {
  33. return !( OFF == config_get( 'recently_visited' ) || current_user_is_anonymous() );
  34. }
  35. /**
  36. * This method should be called from view, update, print pages for issues,
  37. * mantisconnect.
  38. *
  39. * @param issue_id The issue id that was justed visited.
  40. * @param user_id The user id that visited the issue, or null for current
  41. * logged in user.
  42. * @access public
  43. */
  44. function last_visited_issue( $p_issue_id, $p_user_id = null ) {
  45. if( !last_visited_enabled() ) {
  46. return;
  47. }
  48. $c_issue_id = db_prepare_int( $p_issue_id );
  49. $t_value = token_get_value( TOKEN_LAST_VISITED, $p_user_id );
  50. if( is_null( $t_value ) ) {
  51. $t_value = $c_issue_id;
  52. } else {
  53. $t_ids = explode( ',', $p_issue_id . ',' . $t_value );
  54. $t_ids = array_unique( $t_ids );
  55. $t_ids = array_slice( $t_ids, 0, config_get( 'recently_visited_count' ) );
  56. $t_value = implode( ',', $t_ids );
  57. }
  58. token_set( TOKEN_LAST_VISITED, $t_value, TOKEN_EXPIRY_LAST_VISITED, $p_user_id );
  59. }
  60. /**
  61. * Get an array of the last visited bug ids. We intentionally don't check
  62. * if the ids still exists to avoid performance degradation.
  63. *
  64. * @param user_id The user id to get the last visited issues for,
  65. * or null for current logged in user.
  66. * @return An array of issue ids or an empty array if none found.
  67. * @access public
  68. */
  69. function last_visited_get_array( $p_user_id = null ) {
  70. $t_value = token_get_value( TOKEN_LAST_VISITED, $p_user_id );
  71. if( is_null( $t_value ) ) {
  72. return array();
  73. }
  74. # we don't slice the array here to optimise for performance. If the user reduces the number of recently
  75. # visited to track, then he/she will get the extra entries until visiting an issue.
  76. $t_ids = explode( ',', $t_value );
  77. bug_cache_array_rows( $t_ids );
  78. return $t_ids;
  79. }