PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/core/date_api.php

https://github.com/fusenigk/mantisbt-1
PHP | 270 lines | 141 code | 27 blank | 102 comment | 47 complexity | 7cc319bb348f21d7f075388610e19c6e 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. * Date API
  17. *
  18. * @package CoreAPI
  19. * @subpackage DateAPI
  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 config_api.php
  25. * @uses constant_inc.php
  26. * @uses helper_api.php
  27. * @uses html_api.php
  28. * @uses lang_api.php
  29. */
  30. require_api( 'config_api.php' );
  31. require_api( 'constant_inc.php' );
  32. require_api( 'helper_api.php' );
  33. require_api( 'html_api.php' );
  34. require_api( 'lang_api.php' );
  35. /**
  36. * Keeps track of whether the external files required for jscalendar to work
  37. * have already been included in the output sent to the client. jscalendar
  38. * will not work correctly if it is included multiple times on the same page.
  39. * @global bool $g_jscalendar_included_already
  40. */
  41. $g_calendar_already_imported = false;
  42. /**
  43. * checks if date is null
  44. * @param int $p_date
  45. * @return bool
  46. * @access public
  47. */
  48. function date_is_null( $p_date ) {
  49. return $p_date == date_get_null();
  50. }
  51. /**
  52. * gets null date
  53. * @return int
  54. * @access public
  55. */
  56. function date_get_null() {
  57. return 1;
  58. }
  59. $g_cache_timezone = array();
  60. /**
  61. * set new timezone
  62. * @return null
  63. * @access public
  64. */
  65. function date_set_timezone( $p_timezone ) {
  66. global $g_cache_timezone;
  67. array_push( $g_cache_timezone, date_default_timezone_get() );
  68. if( !date_default_timezone_set( $p_timezone ) ) {
  69. // unable to set timezone
  70. trigger_error( ERROR_UPDATING_TIMEZONE, WARNING );
  71. }
  72. }
  73. /**
  74. * restore previous timezone
  75. * @return null
  76. * @access public
  77. */
  78. function date_restore_timezone( ) {
  79. global $g_cache_timezone;
  80. $t_timezone = array_pop( $g_cache_timezone );
  81. if( $t_timezone === null ) {
  82. return;
  83. }
  84. if( !date_default_timezone_set( $t_timezone ) ) {
  85. // unable to set timezone
  86. trigger_error( ERROR_UPDATING_TIMEZONE, WARNING );
  87. }
  88. }
  89. /**
  90. *
  91. * @param int $p_month
  92. * @return null
  93. * @access public
  94. */
  95. function print_month_option_list( $p_month = 0 ) {
  96. for( $i = 1;$i <= 12;$i++ ) {
  97. $month_name = date( 'F', mktime( 0, 0, 0, $i, 1, 2000 ) );
  98. if( $i == $p_month ) {
  99. echo "<option value=\"$i\" selected=\"selected\">" . lang_get( 'month_' . utf8_strtolower($month_name)) . "</option>";
  100. } else {
  101. echo "<option value=\"$i\">" . lang_get( 'month_' . utf8_strtolower($month_name)) . "</option>";
  102. }
  103. }
  104. }
  105. /**
  106. *
  107. *
  108. * @param int $p_month
  109. * @return null
  110. * @access public
  111. */
  112. function print_numeric_month_option_list( $p_month = 0 ) {
  113. for( $i = 1;$i <= 12;$i++ ) {
  114. if( $i == $p_month ) {
  115. echo "<option value=\"$i\" selected=\"selected\">$i</option>";
  116. } else {
  117. echo "<option value=\"$i\">$i</option>";
  118. }
  119. }
  120. }
  121. /**
  122. *
  123. * @param int $p_day
  124. * @return null
  125. * @access public
  126. */
  127. function print_day_option_list( $p_day = 0 ) {
  128. for( $i = 1;$i <= 31;$i++ ) {
  129. if( $i == $p_day ) {
  130. echo "<option value=\"$i\" selected=\"selected\">$i</option>";
  131. } else {
  132. echo "<option value=\"$i\">$i</option>";
  133. }
  134. }
  135. }
  136. /**
  137. *
  138. * @param int $p_year
  139. * @return null
  140. * @access public
  141. */
  142. function print_year_option_list( $p_year = 0 ) {
  143. $current_year = date( "Y" );
  144. for( $i = $current_year;$i > 1999;$i-- ) {
  145. if( $i == $p_year ) {
  146. echo "<option value=\"$i\" selected=\"selected\">$i</option>";
  147. } else {
  148. echo "<option value=\"$i\">$i</option>";
  149. }
  150. }
  151. }
  152. /**
  153. *
  154. * @param int $p_year
  155. * @param int $p_start
  156. * @param int $p_end
  157. * @return null
  158. * @access public
  159. */
  160. function print_year_range_option_list( $p_year = 0, $p_start = 0, $p_end = 0 ) {
  161. $t_current = date( 'Y' );
  162. $t_forward_years = config_get( 'forward_year_count' );
  163. $t_start_year = $p_start;
  164. if( $t_start_year == 0 ) {
  165. $t_backward_years = config_get( 'backward_year_count' );
  166. $t_start_year = $t_current - $t_backward_years;
  167. }
  168. if(( $p_year < $t_start_year ) && ( $p_year != 0 ) ) {
  169. $t_start_year = $p_year;
  170. }
  171. $t_end_year = $p_end;
  172. if( $t_end_year == 0 ) {
  173. $t_end_year = $t_current + $t_forward_years;
  174. }
  175. if( $p_year > $t_end_year ) {
  176. $t_end_year = $p_year + $t_forward_years;
  177. }
  178. for( $i = $t_start_year;$i <= $t_end_year;$i++ ) {
  179. if( $i == $p_year ) {
  180. echo "<option value=\"$i\" selected=\"selected\">$i</option>";
  181. } else {
  182. echo "<option value=\"$i\">$i</option>";
  183. }
  184. }
  185. }
  186. /**
  187. *
  188. * @param string $p_name
  189. * @param string $p_format
  190. * @param int $p_date
  191. * @param bool $p_default_disable
  192. * @param bool $p_allow_blank
  193. * @param int $p_year_start
  194. * @param int $p_year_end
  195. * @return null
  196. * @access public
  197. */
  198. function print_date_selection_set( $p_name, $p_format, $p_date = 0, $p_default_disable = false, $p_allow_blank = false, $p_year_start = 0, $p_year_end = 0 ) {
  199. $t_chars = preg_split( '//', $p_format, -1, PREG_SPLIT_NO_EMPTY );
  200. if( $p_date != 0 ) {
  201. $t_date = preg_split( '/-/', date( 'Y-m-d', $p_date ), -1, PREG_SPLIT_NO_EMPTY );
  202. } else {
  203. $t_date = array(
  204. 0,
  205. 0,
  206. 0,
  207. );
  208. }
  209. $t_disable = '';
  210. if( $p_default_disable == true ) {
  211. $t_disable = ' disabled="disabled"';
  212. }
  213. $t_blank_line = '';
  214. if( $p_allow_blank == true ) {
  215. $t_blank_line = "<option value=\"0\"></option>";
  216. }
  217. foreach( $t_chars as $t_char ) {
  218. if( strcmp( $t_char, "M" ) == 0 ) {
  219. echo "<select ", helper_get_tab_index(), " name=\"" . $p_name . "_month\"$t_disable>";
  220. echo $t_blank_line;
  221. print_month_option_list( $t_date[1] );
  222. echo "</select>\n";
  223. }
  224. if( strcmp( $t_char, "m" ) == 0 ) {
  225. echo "<select ", helper_get_tab_index(), " name=\"" . $p_name . "_month\"$t_disable>";
  226. echo $t_blank_line;
  227. print_numeric_month_option_list( $t_date[1] );
  228. echo "</select>\n";
  229. }
  230. if( strcasecmp( $t_char, "D" ) == 0 ) {
  231. echo "<select ", helper_get_tab_index(), " name=\"" . $p_name . "_day\"$t_disable>";
  232. echo $t_blank_line;
  233. print_day_option_list( $t_date[2] );
  234. echo "</select>\n";
  235. }
  236. if( strcasecmp( $t_char, "Y" ) == 0 ) {
  237. echo "<select ", helper_get_tab_index(), " name=\"" . $p_name . "_year\"$t_disable>";
  238. echo $t_blank_line;
  239. print_year_range_option_list( $t_date[0], $p_year_start, $p_year_end );
  240. echo "</select>\n";
  241. }
  242. }
  243. }