/issues/core/csv_api.php

https://github.com/osarrat/sigmah-website · PHP · 399 lines · 130 code · 41 blank · 228 comment · 10 complexity · cdb2e0f27d9f801552b56ad34e337cc7 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. * CSV API
  17. * Names for formatting functions are csv_format_*, where * corresponds to the
  18. * field name as return get csv_get_columns() and by the filter api.
  19. *
  20. * @package CoreAPI
  21. * @subpackage CSVAPI
  22. * @copyright Copyright (C) 2000 - 2002 Kenzaburo Ito - kenito@300baud.org
  23. * @copyright Copyright (C) 2002 - 2010 MantisBT Team - mantisbt-dev@lists.sourceforge.net
  24. * @link http://www.mantisbt.org
  25. */
  26. /**
  27. * get the csv file new line, can be moved to config in the future
  28. * @return string containing new line character
  29. * @access public
  30. */
  31. function csv_get_newline() {
  32. return "\r\n";
  33. }
  34. /**
  35. * get the csv file separator, can be moved to config in the future
  36. * @return string
  37. * @access public
  38. */
  39. function csv_get_separator() {
  40. static $s_seperator = null;
  41. if ( $s_seperator === null )
  42. $s_seperator = config_get( 'csv_separator' );
  43. return $s_seperator;
  44. }
  45. /**
  46. * if all projects selected, default to <username>.csv, otherwise default to
  47. * <projectname>.csv.
  48. * @return string filename
  49. * @access public
  50. */
  51. function csv_get_default_filename() {
  52. $t_current_project_id = helper_get_current_project();
  53. if( ALL_PROJECTS == $t_current_project_id ) {
  54. $t_filename = user_get_name( auth_get_current_user_id() );
  55. } else {
  56. $t_filename = project_get_field( $t_current_project_id, 'name' );
  57. }
  58. return $t_filename . '.csv';
  59. }
  60. /**
  61. * escape a string before writing it to csv file.
  62. * @param type $todo TODO
  63. * @return TODO
  64. * @access public
  65. */
  66. function csv_escape_string( $p_str ) {
  67. # enclose strings with separators with quotaiton marks
  68. if( strpos( $p_str, csv_get_separator() ) !== false ) {
  69. $p_str = '"' . str_replace( '"', '""', $p_str ) . '"';
  70. }
  71. # enclose multi-line strings with quotaiton marks
  72. if( strpos( $p_str, "\n" ) !== false ) {
  73. $p_str = '"' . str_replace( '"', '""', $p_str ) . '"';
  74. }
  75. return $p_str;
  76. }
  77. /**
  78. * An array of column names that are used to identify fields to include and in which order.
  79. * @param type $todo TODO
  80. * @return TODO
  81. * @access public
  82. */
  83. function csv_get_columns() {
  84. $t_columns = helper_get_columns_to_view( COLUMNS_TARGET_CSV_PAGE );
  85. return $t_columns;
  86. }
  87. /**
  88. * format bug id
  89. * @param int $p_bug_id bug id
  90. * @return string csv formatted bug
  91. * @access public
  92. */
  93. function csv_format_id( $p_bug_id ) {
  94. return bug_format_id( $p_bug_id );
  95. }
  96. /**
  97. * returns the project name corresponding to the supplied project id.
  98. * @param int $p_project_id project id
  99. * @return string csv formatted project name
  100. * @access public
  101. */
  102. function csv_format_project_id( $p_project_id ) {
  103. return csv_escape_string( project_get_name( $p_project_id ) );
  104. }
  105. /**
  106. * returns the reporter name corresponding to the supplied id.
  107. * @param int $p_reporter_id user id
  108. * @return string formatted user name
  109. * @access public
  110. */
  111. function csv_format_reporter_id( $p_reporter_id ) {
  112. return csv_escape_string( user_get_name( $p_reporter_id ) );
  113. }
  114. /**
  115. * returns the handler name corresponding to the supplied id
  116. * @param int $p_handler_id user id
  117. * @return string formatted user name
  118. * @access public
  119. */
  120. function csv_format_handler_id( $p_handler_id ) {
  121. if( $p_handler_id > 0 ) {
  122. return csv_escape_string( user_get_name( $p_handler_id ) );
  123. }
  124. }
  125. /**
  126. * return the priority string
  127. * @param int $p_priority
  128. * @return string formatted priority string
  129. * @access public
  130. */
  131. function csv_format_priority( $p_priority ) {
  132. return csv_escape_string( get_enum_element( 'priority', $p_priority ) );
  133. }
  134. /**
  135. * return the severity string
  136. * @param int $p_severity
  137. * @return string formatted severity string
  138. * @access public
  139. */
  140. function csv_format_severity( $p_severity ) {
  141. return csv_escape_string( get_enum_element( 'severity', $p_severity ) );
  142. }
  143. /**
  144. * return the reproducability string
  145. * @param int $p_reproducibility
  146. * @return string formatted reproducibility string
  147. * @access public
  148. */
  149. function csv_format_reproducibility( $p_reproducibility ) {
  150. return csv_escape_string( get_enum_element( 'reproducibility', $p_reproducibility ) );
  151. }
  152. /**
  153. * return the version
  154. * @param string $p_version version string
  155. * @return string formatted version string
  156. * @access public
  157. */
  158. function csv_format_version( $p_version ) {
  159. return csv_escape_string( $p_version );
  160. }
  161. /**
  162. * return the fixed_in_version
  163. * @param string $p_fixed_in_version fixed in version string
  164. * @return string formatted fixed in version string
  165. * @access public
  166. */
  167. function csv_format_fixed_in_version( $p_fixed_in_version ) {
  168. return csv_escape_string( $p_fixed_in_version );
  169. }
  170. /**
  171. * return the target_version
  172. * @param string $p_target_version target version string
  173. * @return string formatted target version string
  174. * @access public
  175. */
  176. function csv_format_target_version( $p_target_version ) {
  177. return csv_escape_string( $p_target_version );
  178. }
  179. /**
  180. * return the projection
  181. * @param int $p_projection
  182. * @return string formatted projection string
  183. * @access public
  184. */
  185. function csv_format_projection( $p_projection ) {
  186. return csv_escape_string( get_enum_element( 'projection', $p_projection ) );
  187. }
  188. /**
  189. * return the category
  190. * @param int $p_category_id
  191. * @return string formatted category string
  192. * @access public
  193. */
  194. function csv_format_category_id( $p_category_id ) {
  195. return csv_escape_string( category_full_name( $p_category_id, false ) );
  196. }
  197. /**
  198. * return the date submitted
  199. * @param string $p_date_submitted
  200. * @return string formatted date
  201. * @access public
  202. */
  203. function csv_format_date_submitted( $p_date_submitted ) {
  204. static $s_date_format = null;
  205. if ( $s_date_format === null )
  206. $s_date_format = config_get( 'short_date_format' );
  207. return date( $s_date_format, $p_date_submitted );
  208. }
  209. /**
  210. * return the eta
  211. * @param int $p_eta eta
  212. * @return string formatted eta
  213. * @access public
  214. */
  215. function csv_format_eta( $p_eta ) {
  216. return csv_escape_string( get_enum_element( 'eta', $p_eta ) );
  217. }
  218. /**
  219. * return the operating system
  220. * @param string $p_os operating system
  221. * @return string formatted operating system
  222. * @access public
  223. */
  224. function csv_format_os( $p_os ) {
  225. return csv_escape_string( $p_os );
  226. }
  227. /**
  228. * return the os build (os version)
  229. * @param string $p_os_build operating system build
  230. * @return string formatted operating system build
  231. * @access public
  232. */
  233. function csv_format_os_build( $p_os_build ) {
  234. return csv_escape_string( $p_os_build );
  235. }
  236. /**
  237. * return the build
  238. * @param string $p_build
  239. * @return string formatted build
  240. * @access public
  241. */
  242. function csv_format_build( $p_build ) {
  243. return csv_escape_string( $p_build );
  244. }
  245. /**
  246. * return the platform
  247. * @param string $p_platform platform
  248. * @return string formatted platform
  249. * @access public
  250. */
  251. function csv_format_platform( $p_platform ) {
  252. return csv_escape_string( $p_platform );
  253. }
  254. /**
  255. * return the view state (eg: private / public)
  256. * @param int $p_view_state view state
  257. * @return string formatted view state
  258. * @access public
  259. */
  260. function csv_format_view_state( $p_view_state ) {
  261. return csv_escape_string( get_enum_element( 'view_state', $p_view_state ) );
  262. }
  263. /**
  264. * return the last updated date
  265. * @param string $p_last_updated last updated
  266. * @return string formated last updated string
  267. * @access public
  268. */
  269. function csv_format_last_updated( $p_last_updated ) {
  270. static $s_date_format = null;
  271. if ( $s_date_format === null )
  272. $s_date_format = config_get( 'short_date_format' );
  273. return date( $s_date_format, $p_last_updated );
  274. }
  275. /**
  276. * return the summary
  277. * @param string $p_summary summary
  278. * @return string formatted summary
  279. * @access public
  280. */
  281. function csv_format_summary( $p_summary ) {
  282. return csv_escape_string( $p_summary );
  283. }
  284. /**
  285. * return the description
  286. * @param string $p_description description
  287. * @return string formatted description
  288. * @access public
  289. */
  290. function csv_format_description( $p_description ) {
  291. return csv_escape_string( $p_description );
  292. }
  293. /**
  294. * return the steps to reproduce
  295. * @param string $p_steps_to_reproduce steps to reproduce
  296. * @return string formatted steps to reproduce
  297. * @access public
  298. */
  299. function csv_format_steps_to_reproduce( $p_steps_to_reproduce ) {
  300. return csv_escape_string( $p_steps_to_reproduce );
  301. }
  302. /**
  303. * return the additional information
  304. * @param string $p_additional_information
  305. * @return string formatted additional information
  306. * @access public
  307. */
  308. function csv_format_additional_information( $p_additional_information ) {
  309. return csv_escape_string( $p_additional_information );
  310. }
  311. /**
  312. * return the status string
  313. * @param string $p_status status
  314. * @return string formatted status
  315. * @access public
  316. */
  317. function csv_format_status( $p_status ) {
  318. return csv_escape_string( get_enum_element( 'status', $p_status ) );
  319. }
  320. /**
  321. * return the resolution string
  322. * @param int $p_resolution resolution
  323. * @return string formatted resolution string
  324. * @access public
  325. */
  326. function csv_format_resolution( $p_resolution ) {
  327. return csv_escape_string( get_enum_element( 'resolution', $p_resolution ) );
  328. }
  329. /**
  330. * return the duplicate bug id
  331. * @param int $p_duplicate_id
  332. * @return string formatted bug id
  333. * @access public
  334. */
  335. function csv_format_duplicate_id( $p_duplicate_id ) {
  336. return bug_format_id( $p_duplicate_id );
  337. }
  338. /**
  339. * return the selection
  340. * @param int $p_duplicate_id
  341. * @return string
  342. * @access public
  343. */
  344. function csv_format_selection( $p_duplicate_id ) {
  345. return csv_escape_string( '' );
  346. }
  347. /**
  348. * return the due date column
  349. * @param int $p_due_date
  350. * @return string
  351. * @access public
  352. */
  353. function csv_format_due_date( $p_due_date ) {
  354. static $s_date_format = null;
  355. if ( $s_date_format === null )
  356. $s_date_format = config_get( 'short_date_format' );
  357. return csv_escape_string( date( $s_date_format, $p_due_date ) );
  358. }