PageRenderTime 24ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/mantisbt-1.2.8/core/collapse_api.php

#
PHP | 246 lines | 107 code | 45 blank | 94 comment | 27 complexity | a7b28b578607cc4fc836358f1c15afab 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. * Collapse API
  17. *
  18. * collapse_open( 'xyz' ); # marks the start of the expanded section
  19. * :
  20. * ... collapse_icon( 'xyz' ); # this will add the '+' icon
  21. * :
  22. * collapse_closed( 'xyz' ); # marks the start of the collapsed section
  23. * :
  24. * ... collapse_icon( 'xyz' ); # this will add the '-' icon
  25. * :
  26. * collapse_end( 'xyz' ); # marks the end of the whole section
  27. *
  28. * @copyright Copyright (C) 2000 - 2002 Kenzaburo Ito - kenito@300baud.org
  29. * @copyright Copyright (C) 2002 - 2011 MantisBT Team - mantisbt-dev@lists.sourceforge.net
  30. * @link http://www.mantisbt.org
  31. * @package CoreAPI
  32. * @subpackage CollapseAPI
  33. *
  34. * @uses tokens_api.php
  35. * @uses utility_api.php
  36. * @uses config_api.php
  37. * @uses authentiction_api.php
  38. * @uses current_user_api.php
  39. * @uses gpc_api.php
  40. */
  41. /**
  42. * requires tokens_api
  43. */
  44. require_once( 'tokens_api.php' );
  45. /**
  46. *
  47. * @global string $g_current_collapse_section
  48. */
  49. $g_current_collapse_section = null;
  50. /**
  51. *
  52. * @global bool $g_open_collapse_section
  53. */
  54. $g_open_collapse_section = false;
  55. /**
  56. *
  57. * @global string $g_collapse_cache_token
  58. */
  59. $g_collapse_cache_token = null;
  60. /**
  61. * Marks the beginning of a collapse block's open phase.
  62. * This will be visible if the block is expanded, or if
  63. * javascript is disabled.
  64. * @param string Collapse block name
  65. * @param string Collapse block section
  66. */
  67. function collapse_open( $p_name, $p_section = '' ) {
  68. global $g_current_collapse_section, $g_open_collapse_section;
  69. $t_block = ( is_blank( $p_section ) ? $p_name : $p_section . '_' . $p_name );
  70. $t_display = collapse_display( $t_block );
  71. # make sure no other collapse section is started
  72. if( $g_current_collapse_section !== null ) {
  73. trigger_error( ERROR_GENERIC, ERROR );
  74. }
  75. $g_open_collapse_section = true;
  76. $g_current_collapse_section = $t_block;
  77. $t_div_id = $t_block . '_open';
  78. echo '<div id="', $t_div_id, '"', ( $t_display ? '' : ' class="hidden"' ), '>';
  79. }
  80. /**
  81. * Marks the end of a collapse block's open phase and the beginning
  82. * of the block's closed phase. Thi will only be visible if the
  83. * block have been collapsed and javascript is enabled.
  84. * @param string Collapse block name
  85. * @param string Collapse block section
  86. */
  87. function collapse_closed( $p_name, $p_section = '' ) {
  88. global $g_current_collapse_section, $g_open_collapse_section;
  89. $t_block = ( is_blank( $p_section ) ? $p_name : $p_section . '_' . $p_name );
  90. $t_display = !collapse_display( $t_block );
  91. # Make sure a section is opened, and it is the same section.
  92. if( $t_block !== $g_current_collapse_section ) {
  93. trigger_error( ERROR_GENERIC, ERROR );
  94. }
  95. echo '</div>';
  96. $g_open_collapse_section = false;
  97. ob_start();
  98. $t_div_id = $t_block . '_closed';
  99. echo '<div id="', $t_div_id, '"', ( $t_display ? '' : ' class="hidden"' ), '>';
  100. }
  101. /**
  102. * Marks the location where a +/- icon is placed in output
  103. * for the user to toggle the collapse block status.
  104. * This should appear in both the open and closed phase of a block.
  105. * @param string Collapse block name
  106. * @param string Collapse block section
  107. */
  108. function collapse_icon( $p_name, $p_section = '' ) {
  109. if( OFF == config_get( 'use_javascript' ) ) {
  110. return;
  111. }
  112. $t_block = ( is_blank( $p_section ) ? $p_name : $p_section . '_' . $p_name );
  113. global $g_open_collapse_section;
  114. if( $g_open_collapse_section === true ) {
  115. $t_icon = 'minus.png';
  116. $t_alt = '-';
  117. } else {
  118. $t_icon = 'plus.png';
  119. $t_alt = '+';
  120. }
  121. echo "<a href=\"\" onclick=\"ToggleDiv( '$t_block' ); return false;\"
  122. ><img border=\"0\" src=\"images/$t_icon\" alt=\"$t_alt\" /></a>&#160;";
  123. }
  124. /**
  125. * Marks the end of a collaps block's closed phase.
  126. * Closed phase output is discarded if javascript is disabled.
  127. * @param string Collapse block name
  128. * @param string Collapse block section
  129. */
  130. function collapse_end( $p_name, $p_section = '' ) {
  131. global $g_current_collapse_section, $g_open_collapse_section;
  132. $t_block = ( is_blank( $p_section ) ? $p_name : $p_section . '_' . $p_name );
  133. $t_display = collapse_display( $t_block );
  134. # Make sure a section is opened, and it is the same section.
  135. if( $t_block !== $g_current_collapse_section ) {
  136. ob_end_clean();
  137. trigger_error( ERROR_GENERIC, ERROR );
  138. }
  139. echo '</div>';
  140. $g_open_collapse_section = false;
  141. if( ON == config_get( 'use_javascript' ) ) {
  142. ob_end_flush();
  143. } else {
  144. ob_end_clean();
  145. }
  146. $g_current_collapse_section = null;
  147. }
  148. /**
  149. * Determine if a block should be displayed open by default.
  150. * @param string Collapse block
  151. * @return bool
  152. */
  153. function collapse_display( $p_block ) {
  154. global $g_collapse_cache_token;
  155. if( !isset( $g_collapse_cache_token[$p_block] ) || OFF == config_get( 'use_javascript' ) ) {
  156. return true;
  157. }
  158. return( true == $g_collapse_cache_token[$p_block] );
  159. }
  160. /**
  161. * Cache collapse API data from the database for the current user.
  162. * If the collapse cookie has been set, grab the changes and resave
  163. * the token, or touch it otherwise.
  164. */
  165. function collapse_cache_token() {
  166. global $g_collapse_cache_token;
  167. if( !auth_is_user_authenticated() || current_user_is_anonymous() ) {
  168. $g_collapse_cache_token = array();
  169. return;
  170. }
  171. if( isset( $g_collapse_cache_token ) ) {
  172. return;
  173. }
  174. $t_user_id = auth_get_current_user_id();
  175. $t_token = token_get_value( TOKEN_COLLAPSE );
  176. if( !is_null( $t_token ) ) {
  177. $t_data = unserialize( $t_token );
  178. } else {
  179. $t_data = array();
  180. }
  181. $g_collapse_cache_token = $t_data;
  182. $t_cookie = gpc_get_cookie( 'MANTIS_collapse_settings', '' );
  183. if( false !== $t_cookie && !is_blank( $t_cookie ) ) {
  184. $t_update = false;
  185. $t_data = explode( '|', $t_cookie );
  186. foreach( $t_data as $t_pair ) {
  187. $t_pair = explode( ',', $t_pair );
  188. if( false !== $t_pair && count( $t_pair ) == 2 ) {
  189. $g_collapse_cache_token[$t_pair[0]] = ( true == $t_pair[1] );
  190. $t_update = true;
  191. }
  192. }
  193. if( $t_update ) {
  194. $t_token = serialize( $g_collapse_cache_token );
  195. token_set( TOKEN_COLLAPSE, $t_token, TOKEN_EXPIRY_COLLAPSE );
  196. } else {
  197. token_touch( TOKEN_COLLAPSE );
  198. }
  199. gpc_clear_cookie( 'MANTIS_collapse_settings' );
  200. }
  201. }