PageRenderTime 66ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/core/custom_field_api.php

https://github.com/rlerdorf/mantisbt
PHP | 1398 lines | 777 code | 193 blank | 428 comment | 142 complexity | 91f1e039040a9ed1dc1013f526e4684f 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 CustomFieldAPI
  18. * @copyright Copyright (C) 2000 - 2002 Kenzaburo Ito - kenito@300baud.org
  19. * @copyright Copyright (C) 2002 - 2013 MantisBT Team - mantisbt-dev@lists.sourceforge.net
  20. * @link http://www.mantisbt.org
  21. */
  22. /**
  23. * requires bug_api
  24. */
  25. require_once( 'bug_api.php' );
  26. /**
  27. * requires helper_api
  28. */
  29. require_once( 'helper_api.php' );
  30. /**
  31. * requires date_api
  32. */
  33. require_once( 'date_api.php' );
  34. # ## Custom Fields API ###
  35. # *******************************************
  36. # TODO
  37. # - add an object to store field data like BugData and UserPrefs ?
  38. # - add caching functions like user, bug, etc
  39. # - make existing api functions use caching functions
  40. # - add functions to return individual db columns for a field definition
  41. # *******************************************
  42. $g_custom_field_types[CUSTOM_FIELD_TYPE_STRING] = 'standard';
  43. $g_custom_field_types[CUSTOM_FIELD_TYPE_NUMERIC] = 'standard';
  44. $g_custom_field_types[CUSTOM_FIELD_TYPE_FLOAT] = 'standard';
  45. $g_custom_field_types[CUSTOM_FIELD_TYPE_ENUM] = 'standard';
  46. $g_custom_field_types[CUSTOM_FIELD_TYPE_EMAIL] = 'standard';
  47. $g_custom_field_types[CUSTOM_FIELD_TYPE_CHECKBOX] = 'standard';
  48. $g_custom_field_types[CUSTOM_FIELD_TYPE_LIST] = 'standard';
  49. $g_custom_field_types[CUSTOM_FIELD_TYPE_MULTILIST] = 'standard';
  50. $g_custom_field_types[CUSTOM_FIELD_TYPE_DATE] = 'standard';
  51. foreach( $g_custom_field_types as $type ) {
  52. require_once( 'cfdefs' . DIRECTORY_SEPARATOR . 'cfdef_' . $type . '.php' );
  53. }
  54. function custom_field_allow_manage_display( $p_type, $p_display ) {
  55. global $g_custom_field_type_definition;
  56. if( isset( $g_custom_field_type_definition[$p_type]['#display_' . $p_display] ) ) {
  57. return $g_custom_field_type_definition[$p_type]['#display_' . $p_display];
  58. }
  59. return false;
  60. }
  61. # ########################################
  62. # SECURITY NOTE: cache globals are initialized here to prevent them
  63. # being spoofed if register_globals is turned on
  64. $g_cache_custom_field = array();
  65. $g_cache_cf_list = NULL;
  66. $g_cache_cf_linked = array();
  67. $g_cache_name_to_id_map = array();
  68. /**
  69. * Cache a custom field row if necessary and return the cached copy
  70. * If the second parameter is true (default), trigger an error
  71. * if the field can't be found. If the second parameter is
  72. * false, return false if the field can't be found.
  73. * @param int $p_field_id integer representing custom field id
  74. * @param bool $p_trigger_errors indicates whether to trigger an error if the field is not found
  75. * @return array array representing custom field
  76. * @access public
  77. */
  78. function custom_field_cache_row( $p_field_id, $p_trigger_errors = true ) {
  79. global $g_cache_custom_field, $g_cache_name_to_id_map;
  80. $c_field_id = db_prepare_int( $p_field_id );
  81. $t_custom_field_table = db_get_table( 'mantis_custom_field_table' );
  82. if( isset( $g_cache_custom_field[$c_field_id] ) ) {
  83. return $g_cache_custom_field[$c_field_id];
  84. }
  85. $query = "SELECT *
  86. FROM $t_custom_field_table
  87. WHERE id=" . db_param();
  88. $result = db_query_bound( $query, Array( $c_field_id ) );
  89. if( 0 == db_num_rows( $result ) ) {
  90. if( $p_trigger_errors ) {
  91. error_parameters( 'Custom ' . $p_field_id );
  92. trigger_error( ERROR_CUSTOM_FIELD_NOT_FOUND, ERROR );
  93. } else {
  94. return false;
  95. }
  96. }
  97. $row = db_fetch_array( $result );
  98. $g_cache_custom_field[$c_field_id] = $row;
  99. $g_cache_name_to_id_map[$row['name']] = $c_field_id;
  100. return $row;
  101. }
  102. /**
  103. * Cache custom fields contained within an array of field id's
  104. * @param array $p_cf_id_array array of custom field id's
  105. * @return null
  106. * @access public
  107. */
  108. function custom_field_cache_array_rows( $p_cf_id_array ) {
  109. global $g_cache_custom_field;
  110. $c_cf_id_array = array();
  111. foreach( $p_cf_id_array as $t_cf_id ) {
  112. if( !isset( $g_cache_custom_field[(int) $t_cf_id] ) ) {
  113. $c_cf_id_array[] = (int) $t_cf_id;
  114. }
  115. }
  116. if( empty( $c_cf_id_array ) ) {
  117. return;
  118. }
  119. $t_custom_field_table = db_get_table( 'mantis_custom_field_table' );
  120. $query = "SELECT *
  121. FROM $t_custom_field_table
  122. WHERE id IN (" . implode( ',', $c_cf_id_array ) . ')';
  123. $result = db_query_bound( $query );
  124. while( $row = db_fetch_array( $result ) ) {
  125. $g_cache_custom_field[(int) $row['id']] = $row;
  126. }
  127. return;
  128. }
  129. /**
  130. * Clear the custom field cache (or just the given id if specified)
  131. * @param int $p_field_id custom field id
  132. * @return bool
  133. * @access public
  134. */
  135. function custom_field_clear_cache( $p_field_id = null ) {
  136. global $g_cache_custom_field, $g_cached_custom_field_lists;
  137. $g_cached_custom_field_lists = null;
  138. if( null === $p_field_id ) {
  139. $g_cache_custom_field = array();
  140. } else {
  141. $c_field_id = db_prepare_int( $p_field_id );
  142. unset( $g_cache_custom_field[$c_field_id] );
  143. }
  144. return true;
  145. }
  146. /**
  147. * Check to see whether the field is included in the given project
  148. * return true if the field is included, false otherwise
  149. * @param int $p_field_id custom field id
  150. * @param int $p_project_id project id
  151. * @return bool
  152. * @access public
  153. */
  154. function custom_field_is_linked( $p_field_id, $p_project_id ) {
  155. global $g_cache_cf_linked;
  156. $c_project_id = db_prepare_int( $p_project_id );
  157. $c_field_id = db_prepare_int( $p_field_id );
  158. if( isset( $g_cache_cf_linked[$c_project_id] ) ) {
  159. if( in_array( $c_field_id, $g_cache_cf_linked[$p_project_id] ) ) {
  160. return true;
  161. }
  162. return false;
  163. }
  164. # figure out if this bug_id/field_id combination exists
  165. $t_custom_field_project_table = db_get_table( 'mantis_custom_field_project_table' );
  166. $query = "SELECT COUNT(*)
  167. FROM $t_custom_field_project_table
  168. WHERE field_id=" . db_param() . " AND
  169. project_id=" . db_param();
  170. $result = db_query_bound( $query, Array( $c_field_id, $c_project_id ) );
  171. $count = db_result( $result );
  172. if( $count > 0 ) {
  173. return true;
  174. } else {
  175. return false;
  176. }
  177. }
  178. /**
  179. * Check to see whether the field id is defined
  180. * return true if the field is defined, false otherwise
  181. * @param int $p_field_id custom field id
  182. * @return bool
  183. * @access public
  184. */
  185. function custom_field_exists( $p_field_id ) {
  186. if( false == custom_field_cache_row( $p_field_id, false ) ) {
  187. return false;
  188. } else {
  189. return true;
  190. }
  191. }
  192. /**
  193. * Return the type of a custom field if it exists.
  194. * @param int $p_field_id custom field id
  195. * @return int custom field type
  196. * @access public
  197. */
  198. function custom_field_type( $p_field_id ) {
  199. $t_field = custom_field_cache_row( $p_field_id, false );
  200. if( $t_field == false ) {
  201. return - 1;
  202. } else {
  203. return $t_field['type'];
  204. }
  205. }
  206. /**
  207. * Check to see whether the field id is defined
  208. * return true if the field is defined, error otherwise
  209. * @param int $p_field_id custom field id
  210. * @return bool
  211. * @access public
  212. */
  213. function custom_field_ensure_exists( $p_field_id ) {
  214. if( custom_field_exists( $p_field_id ) ) {
  215. return true;
  216. } else {
  217. error_parameters( 'Custom ' . $p_field_id );
  218. trigger_error( ERROR_CUSTOM_FIELD_NOT_FOUND, ERROR );
  219. }
  220. }
  221. /**
  222. * Check to see whether the name is unique
  223. * return false if a field with the name already exists, true otherwise
  224. * if an id is specified, then the corresponding record is excluded from the
  225. * uniqueness test.
  226. * @param string $p_name custom field name
  227. * @param int $p_custom_field_id custom field id
  228. * @return bool
  229. * @access public
  230. */
  231. function custom_field_is_name_unique( $p_name, $p_custom_field_id = null ) {
  232. $t_custom_field_table = db_get_table( 'mantis_custom_field_table' );
  233. $query = "SELECT COUNT(*)
  234. FROM $t_custom_field_table
  235. WHERE name=" . db_param();
  236. if( $p_custom_field_id !== null ) {
  237. $c_id = db_prepare_int( $p_custom_field_id );
  238. $query .= ' AND (id <> ' . db_param() . ')';
  239. }
  240. $result = db_query_bound( $query, ( ($p_custom_field_id !== null) ? Array( $p_name, $c_id ) : Array( $p_name ) ) );
  241. $count = db_result( $result );
  242. if( $count > 0 ) {
  243. return false;
  244. } else {
  245. return true;
  246. }
  247. }
  248. /**
  249. * Check to see whether the name is unique
  250. * return true if the name has not been used, error otherwise
  251. * @param string $p_name Custom field name
  252. * @return bool
  253. * @access public
  254. */
  255. function custom_field_ensure_name_unique( $p_name ) {
  256. if( custom_field_is_name_unique( $p_name ) ) {
  257. return true;
  258. } else {
  259. trigger_error( ERROR_CUSTOM_FIELD_NAME_NOT_UNIQUE, ERROR );
  260. }
  261. }
  262. /**
  263. * Return true if the user can read the value of the field for the given bug,
  264. * false otherwise.
  265. * @param int $p_field_id custom field id
  266. * @param int $p_bug_id bug id
  267. * @param int $p_user_id user id
  268. * @return bool
  269. * @access public
  270. */
  271. function custom_field_has_read_access( $p_field_id, $p_bug_id, $p_user_id = null ) {
  272. custom_field_ensure_exists( $p_field_id );
  273. if( null === $p_user_id ) {
  274. $p_user_id = auth_get_current_user_id();
  275. }
  276. $t_access_level_r = custom_field_get_field( $p_field_id, 'access_level_r' );
  277. $t_project_id = bug_get_field( $p_bug_id, 'project_id' );
  278. return access_has_project_level( $t_access_level_r, $t_project_id, $p_user_id );
  279. }
  280. /**
  281. * Return true if the user can read the value of the field for the given project,
  282. * false otherwise.
  283. * @param int $p_field_id custom field id
  284. * @param int $p_project_id bug id
  285. * @param int $p_user_id user id
  286. * @return bool
  287. * @access public
  288. */
  289. function custom_field_has_read_access_by_project_id( $p_field_id, $p_project_id, $p_user_id = null ) {
  290. custom_field_ensure_exists( $p_field_id );
  291. if( null === $p_user_id ) {
  292. $p_user_id = auth_get_current_user_id();
  293. }
  294. $t_access_level_r = custom_field_get_field( $p_field_id, 'access_level_r' );
  295. return access_has_project_level( $t_access_level_r, $p_project_id, $p_user_id );
  296. }
  297. /**
  298. * Return true if the user can modify the value of the field for the given project,
  299. * false otherwise.
  300. * @param int $p_field_id custom field id
  301. * @param int $p_project_id bug id
  302. * @param int $p_user_id user id
  303. * @return bool
  304. * @access public
  305. */
  306. function custom_field_has_write_access_to_project( $p_field_id, $p_project_id, $p_user_id = null ) {
  307. custom_field_ensure_exists( $p_field_id );
  308. if( null === $p_user_id ) {
  309. $p_user_id = auth_get_current_user_id();
  310. }
  311. $t_access_level_rw = custom_field_get_field( $p_field_id, 'access_level_rw' );
  312. return access_has_project_level( $t_access_level_rw, $p_project_id, $p_user_id );
  313. }
  314. /**
  315. * Return true if the user can modify the value of the field for the given bug,
  316. * false otherwise.
  317. * @param int $p_field_id custom field id
  318. * @param int $p_bug_id bug id
  319. * @param int $p_user_id user id
  320. * @return bool
  321. * @access public
  322. */
  323. function custom_field_has_write_access( $p_field_id, $p_bug_id, $p_user_id = null ) {
  324. $t_project_id = bug_get_field( $p_bug_id, 'project_id' );
  325. return( custom_field_has_write_access_to_project( $p_field_id, $t_project_id, $p_user_id ) );
  326. }
  327. /**
  328. * create a new custom field with the name $p_name
  329. * the definition are the default values and can be changes later
  330. * return the ID of the new definition
  331. * @param string $p_name custom field name
  332. * @return int custom field id
  333. * @access public
  334. */
  335. function custom_field_create( $p_name ) {
  336. if( string_contains_scripting_chars( $p_name ) ) {
  337. error_parameters( lang_get( 'custom_field_name' ) );
  338. trigger_error( ERROR_CUSTOM_FIELD_INVALID_PROPERTY, ERROR );
  339. }
  340. $c_name = trim( $p_name );
  341. if( is_blank( $c_name ) ) {
  342. error_parameters( 'name' );
  343. trigger_error( ERROR_EMPTY_FIELD, ERROR );
  344. }
  345. custom_field_ensure_name_unique( $c_name );
  346. $t_custom_field_table = db_get_table( 'mantis_custom_field_table' );
  347. $query = "INSERT INTO $t_custom_field_table
  348. ( name, possible_values )
  349. VALUES
  350. ( " . db_param() . ',' . db_param() . ')';
  351. db_query_bound( $query, Array( $c_name, '' ) );
  352. return db_insert_id( $t_custom_field_table );
  353. }
  354. /**
  355. * Update the field definition
  356. * return true on success, false on failure
  357. * @param int $p_field_id custom field id
  358. * @param array custom field definition
  359. * @return bool
  360. * @access public
  361. */
  362. function custom_field_update( $p_field_id, $p_def_array ) {
  363. if( string_contains_scripting_chars( $p_def_array['name'] ) ) {
  364. error_parameters( lang_get( 'custom_field_name' ) );
  365. trigger_error( ERROR_CUSTOM_FIELD_INVALID_PROPERTY, ERROR );
  366. }
  367. if( is_blank( $p_def_array['name'] ) ) {
  368. error_parameters( 'name' );
  369. trigger_error( ERROR_EMPTY_FIELD, ERROR );
  370. }
  371. if( $p_def_array['access_level_rw'] < $p_def_array['access_level_r'] ) {
  372. error_parameters(
  373. lang_get( 'custom_field_access_level_r' ) . ', ' .
  374. lang_get( 'custom_field_access_level_rw' )
  375. );
  376. trigger_error( ERROR_CUSTOM_FIELD_INVALID_PROPERTY, ERROR );
  377. }
  378. if ( $p_def_array['length_min'] < 0
  379. || ( $p_def_array['length_max'] != 0 && $p_def_array['length_min'] > $p_def_array['length_max'] )
  380. ) {
  381. error_parameters( lang_get( 'custom_field_length_min' ) . ', ' . lang_get( 'custom_field_length_max' ));
  382. trigger_error( ERROR_CUSTOM_FIELD_INVALID_PROPERTY, ERROR );
  383. }
  384. if( !custom_field_is_name_unique( $p_def_array['name'], $p_field_id ) ) {
  385. trigger_error( ERROR_CUSTOM_FIELD_NAME_NOT_UNIQUE, ERROR );
  386. }
  387. # Build fields update statement
  388. $t_update = '';
  389. foreach( $p_def_array as $field => $value ) {
  390. $t_update .= "$field = " . db_param() . ', ';
  391. $t_params[] = is_bool( $value ) ? db_prepare_bool( $value ) : $value;
  392. }
  393. # If there are fields to update, execute SQL
  394. if( $t_update !== '' ) {
  395. $t_mantis_custom_field_table = db_get_table( 'mantis_custom_field_table' );
  396. $t_query = "
  397. UPDATE $t_mantis_custom_field_table
  398. SET " . rtrim( $t_update, ', ' ) . "
  399. WHERE id = " . db_param();
  400. $t_params[] = $p_field_id;
  401. db_query_bound( $t_query, $t_params );
  402. custom_field_clear_cache( $p_field_id );
  403. # db_query errors on failure so:
  404. return true;
  405. }
  406. return false;
  407. }
  408. /**
  409. * Add a custom field to a project
  410. * return true on success, false on failure or if already added
  411. * @param int $p_field_id custom field id
  412. * @param int $p_project_id project id
  413. * @return bool
  414. * @access public
  415. */
  416. function custom_field_link( $p_field_id, $p_project_id ) {
  417. $c_field_id = db_prepare_int( $p_field_id );
  418. $c_project_id = db_prepare_int( $p_project_id );
  419. custom_field_ensure_exists( $p_field_id );
  420. project_ensure_exists( $p_project_id );
  421. if( custom_field_is_linked( $p_field_id, $p_project_id ) ) {
  422. return false;
  423. }
  424. $t_custom_field_project_table = db_get_table( 'mantis_custom_field_project_table' );
  425. $query = "INSERT INTO $t_custom_field_project_table
  426. ( field_id, project_id )
  427. VALUES
  428. ( " . db_param() . ', ' . db_param() . ')';
  429. db_query_bound( $query, Array( $c_field_id, $c_project_id ) );
  430. # db_query errors on failure so:
  431. return true;
  432. }
  433. /**
  434. * Remove a custom field from a project
  435. * return true on success, false on failure
  436. *
  437. * The values for the custom fields are not deleted. This is to allow for the
  438. * case where a bug is moved to another project that has the field, or the
  439. * field is linked again to the project.
  440. * @param int $p_field_id custom field id
  441. * @param int $p_project_id project id
  442. * @return bool
  443. * @access public
  444. */
  445. function custom_field_unlink( $p_field_id, $p_project_id ) {
  446. $c_field_id = db_prepare_int( $p_field_id );
  447. $c_project_id = db_prepare_int( $p_project_id );
  448. $t_custom_field_project_table = db_get_table( 'mantis_custom_field_project_table' );
  449. $query = "DELETE FROM $t_custom_field_project_table
  450. WHERE field_id = " . db_param() . " AND
  451. project_id = " . db_param();
  452. db_query_bound( $query, Array( $c_field_id, $c_project_id ) );
  453. # db_query errors on failure so:
  454. return true;
  455. }
  456. /**
  457. * Delete the field definition and all associated values and project associations
  458. * return true on success, false on failure
  459. * @param int $p_field_id custom field id
  460. * @return bool
  461. * @access public
  462. */
  463. function custom_field_destroy( $p_field_id ) {
  464. $c_field_id = db_prepare_int( $p_field_id );
  465. # delete all values
  466. $t_custom_field_string_table = db_get_table( 'mantis_custom_field_string_table' );
  467. $query = "DELETE FROM $t_custom_field_string_table
  468. WHERE field_id=" . db_param();
  469. db_query_bound( $query, Array( $c_field_id ) );
  470. # delete all project associations
  471. $t_custom_field_project_table = db_get_table( 'mantis_custom_field_project_table' );
  472. $query = "DELETE FROM $t_custom_field_project_table
  473. WHERE field_id=" . db_param();
  474. db_query_bound( $query, Array( $c_field_id ) );
  475. $t_custom_field_table = db_get_table( 'mantis_custom_field_table' );
  476. # delete the definition
  477. $query = "DELETE FROM $t_custom_field_table
  478. WHERE id=" . db_param();
  479. db_query_bound( $query, Array( $c_field_id ) );
  480. custom_field_clear_cache( $p_field_id );
  481. # db_query errors on failure so:
  482. return true;
  483. }
  484. /**
  485. * Delete all associations of custom fields to the specified project
  486. * return true on success, false on failure
  487. *
  488. * To be called from within project_delete().
  489. * @param int $p_project_id project id
  490. * @return bool
  491. * @access public
  492. */
  493. function custom_field_unlink_all( $p_project_id ) {
  494. $c_project_id = db_prepare_int( $p_project_id );
  495. # delete all project associations
  496. $t_custom_field_project_table = db_get_table( 'mantis_custom_field_project_table' );
  497. $query = "DELETE FROM $t_custom_field_project_table
  498. WHERE project_id=" . db_param();
  499. db_query_bound( $query, Array( $c_project_id ) );
  500. # db_query errors on failure so:
  501. return true;
  502. }
  503. /**
  504. * Delete all custom values associated with the specified bug.
  505. * return true on success, false on failure
  506. *
  507. * To be called from bug_delete().
  508. * @param int $p_bug_id bug id
  509. * @return bool
  510. * @access public
  511. */
  512. function custom_field_delete_all_values( $p_bug_id ) {
  513. $c_bug_id = db_prepare_int( $p_bug_id );
  514. $t_custom_field_string_table = db_get_table( 'mantis_custom_field_string_table' );
  515. $query = "DELETE FROM $t_custom_field_string_table
  516. WHERE bug_id=" . db_param();
  517. db_query_bound( $query, Array( $c_bug_id ) );
  518. # db_query errors on failure so:
  519. return true;
  520. }
  521. /**
  522. * Get the id of the custom field with the specified name.
  523. * false is returned if no custom field found with the specified name.
  524. * @param string $p_field_name custom field name
  525. * @param int $p_truncated_length
  526. * @return bool|int false or custom field id
  527. * @access public
  528. */
  529. function custom_field_get_id_from_name( $p_field_name, $p_truncated_length = null ) {
  530. global $g_cache_name_to_id_map;
  531. if ( is_blank( $p_field_name ) ) {
  532. return false;
  533. }
  534. if ( isset( $g_cache_name_to_id_map[$p_field_name] ) ) {
  535. return $g_cache_name_to_id_map[$p_field_name];
  536. }
  537. $t_custom_field_table = db_get_table( 'mantis_custom_field_table' );
  538. if(( null === $p_truncated_length ) || ( utf8_strlen( $p_field_name ) != $p_truncated_length ) ) {
  539. $query = "SELECT id FROM $t_custom_field_table WHERE name = " . db_param();
  540. $c_field_name = $p_field_name;
  541. } else {
  542. # This is to handle the case where we potentially only have a
  543. # truncated part of the custom field name. This happens when we
  544. # are getting the field from the history logs (as the history's
  545. # field_name column used to be 32 while custom field name is 64).
  546. # This is needed to handle legacy database entries, as any
  547. # history record created after 1.1.0a4 has the correct field
  548. # size (see #8002)
  549. $query = "SELECT id FROM $t_custom_field_table WHERE name LIKE " . db_param();
  550. $c_field_name = $p_field_name . '%';
  551. }
  552. $t_result = db_query_bound( $query, array( $c_field_name ) );
  553. if( db_num_rows( $t_result ) == 0 ) {
  554. $g_cache_name_to_id_map[$p_field_name] = false;
  555. return false;
  556. }
  557. $row = db_fetch_array( $t_result );
  558. $g_cache_name_to_id_map[$p_field_name] = $row['id'];
  559. return $row['id'];
  560. }
  561. /**
  562. * Return an array of ids of custom fields bound to the specified project
  563. *
  564. * The ids will be sorted based on the sequence number associated with the binding
  565. * @param int $p_project_id project id
  566. * @return array
  567. * @access public
  568. */
  569. function custom_field_get_linked_ids( $p_project_id = ALL_PROJECTS ) {
  570. global $g_cache_cf_linked, $g_cache_custom_field;
  571. if( !isset( $g_cache_cf_linked[$p_project_id] ) ) {
  572. $t_custom_field_table = db_get_table( 'mantis_custom_field_table' );
  573. $t_custom_field_project_table = db_get_table( 'mantis_custom_field_project_table' );
  574. if( ALL_PROJECTS == $p_project_id ) {
  575. $t_project_user_list_table = db_get_table( 'mantis_project_user_list_table' );
  576. $t_project_table = db_get_table( 'mantis_project_table' );
  577. $t_user_table = db_get_table( 'mantis_user_table' );
  578. $t_user_id = auth_get_current_user_id();
  579. # Select only the ids of custom fields in projects the user has access to
  580. # - all custom fields in public projects,
  581. # - those in private projects where the user is listed
  582. # - in private projects where the user is implicitly listed
  583. $t_query = "
  584. SELECT DISTINCT cft.id
  585. FROM $t_custom_field_table cft
  586. JOIN $t_custom_field_project_table cfpt ON cfpt.field_id = cft.id
  587. JOIN $t_project_table pt
  588. ON pt.id = cfpt.project_id AND pt.enabled = " . db_prepare_bool( true ) . "
  589. LEFT JOIN $t_project_user_list_table pult
  590. ON pult.project_id = cfpt.project_id AND pult.user_id = " . db_param() . "
  591. , $t_user_table ut
  592. WHERE ut.id = " . db_param() . "
  593. AND ( pt.view_state = " . VS_PUBLIC . "
  594. OR pult.user_id = ut.id
  595. ";
  596. $t_params = array( $t_user_id, $t_user_id );
  597. # Add private access clause and related parameter
  598. $t_private_access = config_get( 'private_project_threshold' );
  599. if( is_array( $t_private_access ) ) {
  600. if( 1 == count( $t_private_access ) ) {
  601. $t_access_clause = '= ' . db_param();
  602. $t_params[] = array_shift( $t_private_access );
  603. } else {
  604. $t_access_clause = 'IN (';
  605. foreach( $t_private_access as $t_elem ) {
  606. $t_access_clause .= db_param() . ',';
  607. $t_params[] = $t_elem;
  608. }
  609. $t_access_clause = rtrim( $t_access_clause, ',') . ')';
  610. }
  611. } else {
  612. $t_access_clause = '>=' . db_param();
  613. $t_params[] = $t_private_access;
  614. }
  615. $t_query .= "OR ( pult.user_id IS NULL AND ut.access_level $t_access_clause ) )";
  616. } else {
  617. if( is_array( $p_project_id ) ) {
  618. if( 1 == count( $p_project_id ) ) {
  619. $t_project_clause = '= ' . db_param();
  620. $t_params[] = array_shift( $p_project_id );
  621. } else {
  622. $t_project_clause = 'IN (';
  623. foreach( $p_project_id as $t_project ) {
  624. $t_project_clause .= db_param() . ',';
  625. $t_params[] = $t_project;
  626. }
  627. $t_project_clause = rtrim( $t_project_clause, ',') . ')';
  628. }
  629. } else {
  630. $t_project_clause = '= ' . db_param();
  631. $t_params[] = $p_project_id;
  632. }
  633. $t_query = "
  634. SELECT cft.id
  635. FROM $t_custom_field_table cft
  636. JOIN $t_custom_field_project_table cfpt ON cfpt.field_id = cft.id
  637. WHERE cfpt.project_id $t_project_clause
  638. ORDER BY sequence ASC, name ASC";
  639. }
  640. $result = db_query_bound( $t_query, $t_params );
  641. $t_row_count = db_num_rows( $result );
  642. $t_ids = array();
  643. for( $i = 0;$i < $t_row_count;$i++ ) {
  644. $row = db_fetch_array( $result );
  645. array_push( $t_ids, $row['id'] );
  646. }
  647. custom_field_cache_array_rows( $t_ids );
  648. $g_cache_cf_linked[$p_project_id] = $t_ids;
  649. } else {
  650. $t_ids = $g_cache_cf_linked[$p_project_id];
  651. }
  652. return $t_ids;
  653. }
  654. /**
  655. * Return an array all custom field ids sorted by name
  656. * @return array
  657. * @access public
  658. */
  659. function custom_field_get_ids() {
  660. global $g_cache_cf_list, $g_cache_custom_field;
  661. if( $g_cache_cf_list === NULL ) {
  662. $t_custom_field_table = db_get_table( 'mantis_custom_field_table' );
  663. $query = "SELECT *
  664. FROM $t_custom_field_table
  665. ORDER BY name ASC";
  666. $result = db_query_bound( $query );
  667. $t_row_count = db_num_rows( $result );
  668. $t_ids = array();
  669. for( $i = 0;$i < $t_row_count;$i++ ) {
  670. $row = db_fetch_array( $result );
  671. $g_cache_custom_field[(int) $row['id']] = $row;
  672. array_push( $t_ids, $row['id'] );
  673. }
  674. $g_cache_cf_list = $t_ids;
  675. } else {
  676. $t_ids = $g_cache_cf_list;
  677. }
  678. return $t_ids;
  679. }
  680. /**
  681. * Return an array of ids of projects related to the specified custom field
  682. * (the array may be empty)
  683. * @param int $p_field_id custom field id
  684. * @return array
  685. * @access public
  686. */
  687. function custom_field_get_project_ids( $p_field_id ) {
  688. $c_field_id = db_prepare_int( $p_field_id );
  689. $t_custom_field_project_table = db_get_table( 'mantis_custom_field_project_table' );
  690. $query = "SELECT project_id
  691. FROM $t_custom_field_project_table
  692. WHERE field_id = " . db_param();
  693. $result = db_query_bound( $query, Array( $c_field_id ) );
  694. $t_row_count = db_num_rows( $result );
  695. $t_ids = array();
  696. for( $i = 0;$i < $t_row_count;$i++ ) {
  697. $row = db_fetch_array( $result );
  698. array_push( $t_ids, $row['project_id'] );
  699. }
  700. return $t_ids;
  701. }
  702. /**
  703. * Return a field definition row for the field or error if the field does not exist
  704. * @param int $p_field_id custom field id
  705. * @return array custom field definition
  706. * @access public
  707. */
  708. function custom_field_get_definition( $p_field_id ) {
  709. return custom_field_cache_row( $p_field_id );
  710. }
  711. /**
  712. * Return a single database field from a custom field definition row for the field
  713. * if the database field does not exist, display a warning and return ''
  714. * @param int $p_field_id custom field id
  715. * @param int $p_field_name custom field name
  716. * @return string
  717. * @access public
  718. */
  719. function custom_field_get_field( $p_field_id, $p_field_name ) {
  720. $row = custom_field_get_definition( $p_field_id );
  721. if( isset( $row[$p_field_name] ) ) {
  722. return $row[$p_field_name];
  723. } else {
  724. error_parameters( $p_field_name );
  725. trigger_error( ERROR_DB_FIELD_NOT_FOUND, WARNING );
  726. return '';
  727. }
  728. }
  729. /**
  730. * Get the value of a custom field for the given bug
  731. * @todo return values are unclear... should we error when access is denied
  732. * and provide an api to check whether it will be?
  733. * @param int $p_field_id custom field id
  734. * @param int $p_bug_id bug id
  735. * @return mixed: value is defined, null: no value is defined, false: read access is denied
  736. * @access public
  737. */
  738. function custom_field_get_value( $p_field_id, $p_bug_id ) {
  739. $c_field_id = db_prepare_int( $p_field_id );
  740. $c_bug_id = db_prepare_int( $p_bug_id );
  741. $row = custom_field_cache_row( $p_field_id );
  742. $t_access_level_r = $row['access_level_r'];
  743. $t_default_value = $row['default_value'];
  744. if( !custom_field_has_read_access( $p_field_id, $p_bug_id, auth_get_current_user_id() ) ) {
  745. return false;
  746. }
  747. $t_custom_field_string_table = db_get_table( 'mantis_custom_field_string_table' );
  748. $query = "SELECT value
  749. FROM $t_custom_field_string_table
  750. WHERE bug_id=" . db_param() . " AND
  751. field_id=" . db_param();
  752. $result = db_query_bound( $query, Array( $c_bug_id, $c_field_id ) );
  753. if( db_num_rows( $result ) > 0 ) {
  754. return custom_field_database_to_value( db_result( $result ), $row['type'] );
  755. } else {
  756. return null;
  757. }
  758. }
  759. /**
  760. * Gets the custom fields array for the given bug readable by specified level.
  761. * Array keys are custom field names. Array is sorted by custom field sequence number;
  762. * Array items are arrays with the next keys:
  763. * 'type', 'value', 'access_level_r'
  764. * @param int $p_bug_id bug id
  765. * @param int $p_user_access_level Access level
  766. * @return array
  767. * @access public
  768. */
  769. function custom_field_get_linked_fields( $p_bug_id, $p_user_access_level ) {
  770. $t_custom_fields = custom_field_get_all_linked_fields( $p_bug_id );
  771. # removing restricted fields
  772. foreach( $t_custom_fields as $t_custom_field_name => $t_custom_field_data ) {
  773. if( $p_user_access_level < $t_custom_field_data['access_level_r'] ) {
  774. unset( $t_custom_fields[$t_custom_field_name] );
  775. }
  776. }
  777. return $t_custom_fields;
  778. }
  779. /**
  780. * Gets the custom fields array for the given bug. Array keys are custom field names.
  781. * Array is sorted by custom field sequence number; Array items are arrays with the next keys:
  782. * 'type', 'value', 'access_level_r'
  783. * @param int $p_bug_id bug id
  784. * @return array
  785. * @access public
  786. */
  787. function custom_field_get_all_linked_fields( $p_bug_id ) {
  788. global $g_cached_custom_field_lists;
  789. if( !is_array( $g_cached_custom_field_lists ) ) {
  790. $g_cached_custom_field_lists = array();
  791. }
  792. # is the list in cache ?
  793. if( !array_key_exists( $p_bug_id, $g_cached_custom_field_lists ) ) {
  794. $t_custom_field_project_table = db_get_table( 'mantis_custom_field_project_table' );
  795. $t_custom_field_table = db_get_table( 'mantis_custom_field_table' );
  796. $t_custom_field_string_table = db_get_table( 'mantis_custom_field_string_table' );
  797. $query = "
  798. SELECT f.name, f.type, f.access_level_r, f.default_value, f.type, s.value
  799. FROM $t_custom_field_project_table p
  800. INNER JOIN $t_custom_field_table f ON f.id = p.field_id
  801. LEFT JOIN $t_custom_field_string_table s
  802. ON s.field_id = p.field_id AND s.bug_id = " . db_param() . "
  803. WHERE p.project_id = " . db_param() . "
  804. ORDER BY p.sequence ASC, f.name ASC";
  805. $t_params = array(
  806. (int)$p_bug_id,
  807. bug_get_field( $p_bug_id, 'project_id' )
  808. );
  809. $result = db_query_bound( $query, $t_params );
  810. $t_row_count = db_num_rows( $result );
  811. $t_custom_fields = array();
  812. for( $i = 0;$i < $t_row_count;++$i ) {
  813. $row = db_fetch_array( $result );
  814. if( is_null( $row['value'] ) ) {
  815. $t_value = $row['default_value'];
  816. } else {
  817. $t_value = custom_field_database_to_value( $row['value'], $row['type'] );
  818. }
  819. $t_custom_fields[$row['name']] = array(
  820. 'type' => $row['type'],
  821. 'value' => $t_value,
  822. 'access_level_r' => $row['access_level_r'],
  823. );
  824. }
  825. $g_cached_custom_field_lists[$p_bug_id] = $t_custom_fields;
  826. }
  827. return $g_cached_custom_field_lists[$p_bug_id];
  828. }
  829. /**
  830. * Gets the sequence number for the specified custom field for the specified
  831. * project. Returns false in case of error.
  832. * @param int $p_field_id custom field id
  833. * @param int $p_project_id project id
  834. * @return int|bool
  835. * @access public
  836. */
  837. function custom_field_get_sequence( $p_field_id, $p_project_id ) {
  838. $c_field_id = db_prepare_int( $p_field_id );
  839. $c_project_id = db_prepare_int( $p_project_id );
  840. $t_custom_field_project_table = db_get_table( 'mantis_custom_field_project_table' );
  841. $query = "SELECT sequence
  842. FROM $t_custom_field_project_table
  843. WHERE field_id=" . db_param() . " AND
  844. project_id=" . db_param();
  845. $result = db_query_bound( $query, Array( $c_field_id, $c_project_id ), 1 );
  846. if( 0 == db_num_rows( $result ) ) {
  847. return false;
  848. }
  849. $t_row = db_fetch_array( $result );
  850. return $t_row['sequence'];
  851. }
  852. /**
  853. * Allows the validation of a custom field value without setting it
  854. * or needing a bug to exist.
  855. * @param int $p_field_id custom field id
  856. * @param string $p_value custom field value
  857. * @return bool
  858. * @access public
  859. */
  860. function custom_field_validate( $p_field_id, $p_value ) {
  861. $c_field_id = db_prepare_int( $p_field_id );
  862. custom_field_ensure_exists( $p_field_id );
  863. $t_custom_field_table = db_get_table( 'mantis_custom_field_table' );
  864. $query = "SELECT name, type, possible_values, valid_regexp,
  865. access_level_rw, length_min, length_max, default_value
  866. FROM $t_custom_field_table
  867. WHERE id=" . db_param();
  868. $result = db_query_bound( $query, Array( $c_field_id ) );
  869. $row = db_fetch_array( $result );
  870. $t_name = $row['name'];
  871. $t_type = $row['type'];
  872. $t_possible_values = $row['possible_values'];
  873. $t_valid_regexp = $row['valid_regexp'];
  874. $t_length_min = $row['length_min'];
  875. $t_length_max = $row['length_max'];
  876. $t_default_value = $row['default_value'];
  877. $t_valid = true;
  878. $t_length = utf8_strlen( $p_value );
  879. switch ($t_type) {
  880. case CUSTOM_FIELD_TYPE_STRING:
  881. # Empty fields are valid
  882. if( $t_length == 0 ) {
  883. break;
  884. }
  885. # Regular expression string validation
  886. if( !is_blank( $t_valid_regexp ) ) {
  887. $t_valid &= preg_match( "/$t_valid_regexp/", $p_value );
  888. }
  889. # Check the length of the string
  890. $t_valid &= ( 0 == $t_length_min ) || ( $t_length >= $t_length_min );
  891. $t_valid &= ( 0 == $t_length_max ) || ( $t_length <= $t_length_max );
  892. break;
  893. case CUSTOM_FIELD_TYPE_NUMERIC:
  894. # Empty fields are valid
  895. if( $t_length == 0 ) {
  896. break;
  897. }
  898. $t_valid &= is_numeric( $p_value );
  899. # Check the length of the number
  900. $t_valid &= ( 0 == $t_length_min ) || ( $t_length >= $t_length_min );
  901. $t_valid &= ( 0 == $t_length_max ) || ( $t_length <= $t_length_max );
  902. break;
  903. case CUSTOM_FIELD_TYPE_FLOAT:
  904. # Empty fields are valid
  905. if( $t_length == 0 ) {
  906. break;
  907. }
  908. # Allow both integer and float numbers
  909. $t_valid &= is_numeric( $p_value ) || is_float( $p_value );
  910. # Check the length of the number
  911. $t_valid &= ( 0 == $t_length_min ) || ( $t_length >= $t_length_min );
  912. $t_valid &= ( 0 == $t_length_max ) || ( $t_length <= $t_length_max );
  913. break;
  914. case CUSTOM_FIELD_TYPE_DATE:
  915. # gpc_get_cf for date returns the value from strftime
  916. # Either false (php >= 5.1) or -1 (php < 5.1) for failure
  917. $t_valid &= ( $p_value == null ) || ( ( $p_value !== false ) && ( $p_value > 0 ) );
  918. break;
  919. case CUSTOM_FIELD_TYPE_CHECKBOX:
  920. case CUSTOM_FIELD_TYPE_MULTILIST:
  921. # Checkbox fields can hold a null value (when no checkboxes are ticked)
  922. if ( $p_value === '' ) {
  923. break;
  924. }
  925. # If checkbox field value is not null then we need to validate it... (note: no "break" statement here!)
  926. $t_values = explode( '|', $p_value );
  927. $t_possible_values = custom_field_prepare_possible_values( $row['possible_values'] );
  928. $t_possible_values = explode( '|', $t_possible_values );
  929. $t_invalid_values = array_diff( $t_values, $t_possible_values );
  930. $t_valid &= ( count( $t_invalid_values ) == 0 );
  931. break;
  932. case CUSTOM_FIELD_TYPE_LIST:
  933. case CUSTOM_FIELD_TYPE_ENUM:
  934. case CUSTOM_FIELD_TYPE_RADIO:
  935. # List fields can be empty (when they are not shown on the form or shown with no default values and never clicked)
  936. if ( is_blank( $p_value ) ) {
  937. break;
  938. }
  939. # If list field value is not empty then we need to validate it... (note: no "break" statement here!)
  940. $t_possible_values = custom_field_prepare_possible_values( $row['possible_values'] );
  941. $t_values_arr = explode( '|', $t_possible_values );
  942. $t_valid &= in_array( $p_value, $t_values_arr );
  943. break;
  944. case CUSTOM_FIELD_TYPE_EMAIL:
  945. if ( $p_value !== '' ) {
  946. $t_valid &= email_is_valid( $p_value );
  947. }
  948. break;
  949. default:
  950. break;
  951. }
  952. return (bool)$t_valid;
  953. }
  954. /**
  955. * $p_possible_values: possible values to be pre-processed. If it has enum values,
  956. * it will be left as is. If it has a method, it will be replaced by the list.
  957. * @param string $p_possible_values
  958. * @return string|array
  959. * @access public
  960. */
  961. function custom_field_prepare_possible_values( $p_possible_values ) {
  962. $t_possible_values = $p_possible_values;
  963. if( !is_blank( $t_possible_values ) && ( $t_possible_values[0] == '=' ) ) {
  964. $t_possible_values = helper_call_custom_function( 'enum_' . utf8_substr( $t_possible_values, 1 ), array() );
  965. }
  966. return $t_possible_values;
  967. }
  968. /**
  969. * Get All Possible Values for a Field.
  970. * @param array $p_field_def custom field definition
  971. * @param int $p_project_id project id
  972. * @return bool|array
  973. * @access public
  974. */
  975. function custom_field_distinct_values( $p_field_def, $p_project_id = ALL_PROJECTS ) {
  976. global $g_custom_field_type_definition;
  977. $t_custom_field_string_table = db_get_table( 'mantis_custom_field_string_table' );
  978. $t_mantis_bug_table = db_get_table( 'mantis_bug_table' );
  979. $t_return_arr = array();
  980. # If an enumeration type, we get all possible values, not just used values
  981. if( isset( $g_custom_field_type_definition[$p_field_def['type']]['#function_return_distinct_values'] ) ) {
  982. return call_user_func( $g_custom_field_type_definition[$p_field_def['type']]['#function_return_distinct_values'], $p_field_def );
  983. } else {
  984. $t_from = "$t_custom_field_string_table cfst";
  985. $t_where1 = 'cfst.field_id = ' . db_param();
  986. $t_params[] = $p_field_def['id'];
  987. if( ALL_PROJECTS != $p_project_id ) {
  988. $t_from .= " JOIN $t_mantis_bug_table bt ON bt.id = cfst.bug_id";
  989. $t_where2 = 'AND bt.project_id = ' . db_param();
  990. $t_params[] = $p_project_id;
  991. } else {
  992. $t_where2 = '';
  993. }
  994. $t_query = "
  995. SELECT DISTINCT cfst.value
  996. FROM $t_from
  997. WHERE $t_where1 $t_where2
  998. ORDER BY cfst.value";
  999. $t_result = db_query_bound( $t_query, $t_params );
  1000. $t_row_count = db_num_rows( $t_result );
  1001. if( 0 == $t_row_count ) {
  1002. return false;
  1003. }
  1004. for( $i = 0;$i < $t_row_count;$i++ ) {
  1005. $row = db_fetch_array( $t_result );
  1006. if( !is_blank( trim( $row['value'] ) ) ) {
  1007. array_push( $t_return_arr, $row['value'] );
  1008. }
  1009. }
  1010. }
  1011. return $t_return_arr;
  1012. }
  1013. /**
  1014. * Convert the value to save it into the database, depending of the type
  1015. * return value for database
  1016. * @param mixed $p_value
  1017. * @param int $p_type
  1018. * @return mixed
  1019. * @access public
  1020. */
  1021. function custom_field_value_to_database( $p_value, $p_type ) {
  1022. global $g_custom_field_type_definition;
  1023. if( isset( $g_custom_field_type_definition[$p_type]['#function_value_to_database'] ) ) {
  1024. return call_user_func( $g_custom_field_type_definition[$p_type]['#function_value_to_database'], $p_value );
  1025. }
  1026. return $p_value;
  1027. }
  1028. /**
  1029. * Convert the database-value to value, depending of the type
  1030. * return value for further operation
  1031. * @param mixed $p_value
  1032. * @param int $p_type
  1033. * @return mixed
  1034. * @access public
  1035. */
  1036. function custom_field_database_to_value( $p_value, $p_type ) {
  1037. global $g_custom_field_type_definition;
  1038. if( isset( $g_custom_field_type_definition[$p_type]['#function_database_to_value'] ) ) {
  1039. return call_user_func( $g_custom_field_type_definition[$p_type]['#function_database_to_value'], $p_value );
  1040. }
  1041. return $p_value;
  1042. }
  1043. /**
  1044. * Convert the default-value to value depending on the type. For example, in case of date, this
  1045. * would translate 'tomorrow' to tomorrow's date.
  1046. * @param mixed $p_value
  1047. * @param int $p_type
  1048. * @return mixed
  1049. * @access public
  1050. */
  1051. function custom_field_default_to_value( $p_value, $p_type ) {
  1052. global $g_custom_field_type_definition;
  1053. if( isset( $g_custom_field_type_definition[$p_type]['#function_default_to_value'] ) ) {
  1054. return call_user_func( $g_custom_field_type_definition[$p_type]['#function_default_to_value'], $p_value );
  1055. }
  1056. return $p_value;
  1057. }
  1058. /**
  1059. * Set the value of a custom field for a given bug
  1060. * return true on success, false on failure
  1061. * @param int $p_field_id custom field id
  1062. * @param int $p_bug_id bug id
  1063. * @param mixed $p_value
  1064. * @param boolean $p_log create history logs for new values
  1065. * @return bool
  1066. * @access public
  1067. */
  1068. function custom_field_set_value( $p_field_id, $p_bug_id, $p_value, $p_log_insert=true ) {
  1069. $c_field_id = db_prepare_int( $p_field_id );
  1070. $c_bug_id = db_prepare_int( $p_bug_id );
  1071. custom_field_ensure_exists( $p_field_id );
  1072. if ( !custom_field_validate( $p_field_id, $p_value ) )
  1073. return false;
  1074. $t_name = custom_field_get_field( $p_field_id, 'name' );
  1075. $t_type = custom_field_get_field( $p_field_id, 'type' );
  1076. $t_custom_field_string_table = db_get_table( 'mantis_custom_field_string_table' );
  1077. # Determine whether an existing value needs to be updated or a new value inserted
  1078. $query = "SELECT value
  1079. FROM $t_custom_field_string_table
  1080. WHERE field_id=" . db_param() . " AND
  1081. bug_id=" . db_param();
  1082. $result = db_query_bound( $query, Array( $c_field_id, $c_bug_id ) );
  1083. if( db_num_rows( $result ) > 0 ) {
  1084. $query = "UPDATE $t_custom_field_string_table
  1085. SET value=" . db_param() . "
  1086. WHERE field_id=" . db_param() . " AND
  1087. bug_id=" . db_param();
  1088. db_query_bound( $query, Array( custom_field_value_to_database( $p_value, $t_type ), $c_field_id, $c_bug_id ) );
  1089. $row = db_fetch_array( $result );
  1090. history_log_event_direct( $c_bug_id, $t_name, custom_field_database_to_value( $row['value'], $t_type ), $p_value );
  1091. } else {
  1092. $query = "INSERT INTO $t_custom_field_string_table
  1093. ( field_id, bug_id, value )
  1094. VALUES
  1095. ( " . db_param() . ', ' . db_param() . ', ' . db_param() . ')';
  1096. db_query_bound( $query, Array( $c_field_id, $c_bug_id, custom_field_value_to_database( $p_value, $t_type ) ) );
  1097. # Don't log history events for new bug reports or on other special occasions
  1098. if ( $p_log_insert ) {
  1099. history_log_event_direct( $c_bug_id, $t_name, '', $p_value );
  1100. }
  1101. }
  1102. custom_field_clear_cache( $p_field_id );
  1103. # db_query errors on failure so:
  1104. return true;
  1105. }
  1106. /**
  1107. * Sets the sequence number for the specified custom field for the specified
  1108. * project.
  1109. * @param int $p_field_id custom field id
  1110. * @param int $p_project_id project id
  1111. * @param int $p_sequence
  1112. * @return bool
  1113. * @access public
  1114. */
  1115. function custom_field_set_sequence( $p_field_id, $p_project_id, $p_sequence ) {
  1116. $c_field_id = db_prepare_int( $p_field_id );
  1117. $c_project_id = db_prepare_int( $p_project_id );
  1118. $c_sequence = db_prepare_int( $p_sequence );
  1119. $t_custom_field_project_table = db_get_table( 'mantis_custom_field_project_table' );
  1120. $query = "UPDATE $t_custom_field_project_table
  1121. SET sequence=" . db_param() . "
  1122. WHERE field_id=" . db_param() . " AND
  1123. project_id=" . db_param();
  1124. $result = db_query_bound( $query, Array( $c_sequence, $c_field_id, $c_project_id ) );
  1125. custom_field_clear_cache( $p_field_id );
  1126. return true;
  1127. }
  1128. /**
  1129. * Print an input field
  1130. * $p_field_def contains the definition of the custom field (including it's field id
  1131. * $p_bug_id contains the bug where this field belongs to. If it's left
  1132. * away, it'll default to 0 and thus belongs to a new (i.e. non-existant) bug
  1133. * NOTE: This probably belongs in the print_api.php
  1134. * @param array $p_field_def custom field definition
  1135. * @param int $p_bug_id bug id
  1136. * @access public
  1137. */
  1138. function print_custom_field_input( $p_field_def, $p_bug_id = null ) {
  1139. if( null === $p_bug_id ) {
  1140. $t_custom_field_value = custom_field_default_to_value( $p_field_def['default_value'], $p_field_def['type'] );
  1141. } else {
  1142. $t_custom_field_value = custom_field_get_value( $p_field_def['id'], $p_bug_id );
  1143. # If the custom field value is undefined and the field cannot hold a null value, use the default value instead
  1144. if( $t_custom_field_value === null &&
  1145. ( $p_field_def['type'] == CUSTOM_FIELD_TYPE_ENUM ||
  1146. $p_field_def['type'] == CUSTOM_FIELD_TYPE_LIST ||
  1147. $p_field_def['type'] == CUSTOM_FIELD_TYPE_MULTILIST ||
  1148. $p_field_def['type'] == CUSTOM_FIELD_TYPE_RADIO ) ) {
  1149. $t_custom_field_value = custom_field_default_to_value( $p_field_def['default_value'], $p_field_def['type'] );
  1150. }
  1151. }
  1152. global $g_custom_field_type_definition;
  1153. if( isset( $g_custom_field_type_definition[$p_field_def['type']]['#function_print_input'] ) ) {
  1154. call_user_func( $g_custom_field_type_definition[$p_field_def['type']]['#function_print_input'], $p_field_def, $t_custom_field_value );
  1155. } else {
  1156. trigger_error( ERROR_CUSTOM_FIELD_INVALID_DEFINITION, ERROR );
  1157. }
  1158. }
  1159. /**
  1160. * Prepare a string containing a custom field value for display
  1161. * @todo This probably belongs in the string_api.php
  1162. * @param array $p_def contains the definition of the custom field
  1163. * @param int $p_field_id contains the id of the field
  1164. * @param int $p_bug_id contains the bug id to display the custom field value for
  1165. * @return string
  1166. * @access public
  1167. */
  1168. function string_custom_field_value( $p_def, $p_field_id, $p_bug_id ) {
  1169. $t_custom_field_value = custom_field_get_value( $p_field_id, $p_bug_id );
  1170. if( $t_custom_field_value === null ) {
  1171. return '';
  1172. }
  1173. global $g_custom_field_type_definition;
  1174. if( isset( $g_custom_field_type_definition[$p_def['type']]['#function_string_value'] ) ) {
  1175. return call_user_func( $g_custom_field_type_definition[$p_def['type']]['#function_string_value'], $t_custom_field_value );
  1176. }
  1177. return string_display_links( $t_custom_field_value );
  1178. }
  1179. /**
  1180. * Print a custom field value for display
  1181. * NOTE: This probably belongs in the print_api.php
  1182. * @param array $p_def contains the definition of the custom field
  1183. * @param int $p_field_id contains the id of the field
  1184. * @param int $p_bug_id contains the bug id to display the custom field value for
  1185. * @return null
  1186. * @access public
  1187. */
  1188. function print_custom_field_value( $p_def, $p_field_id, $p_bug_id ) {
  1189. echo string_custom_field_value( $p_def, $p_field_id, $p_bug_id );
  1190. }
  1191. /**
  1192. * Prepare a string containing a custom field value for email
  1193. * NOTE: This probably belongs in the string_api.php
  1194. * @param string $p_value value of custom field
  1195. * @param int $p_type type of custom field
  1196. * @return string value ready for sending via email
  1197. * @access public
  1198. */
  1199. function string_custom_field_value_for_email( $p_value, $p_type ) {
  1200. global $g_custom_field_type_definition;
  1201. if( isset( $g_custom_field_type_definition[$p_type]['#function_string_value_for_email'] ) ) {
  1202. return call_user_func( $g_custom_field_type_definition[$p_type]['#function_string_value_for_email'], $p_value );
  1203. }
  1204. return $p_value;
  1205. }