/api/soap/mc_enum_api.php

https://github.com/fusenigk/mantisbt-1 · PHP · 322 lines · 141 code · 41 blank · 140 comment · 30 complexity · 7c5d8a021a9f1c0199fabb7e7d916df5 MD5 · raw file

  1. <?php
  2. # MantisConnect - A webservice interface to Mantis Bug Tracker
  3. # Copyright (C) 2004-2011 Victor Boctor - vboctor@users.sourceforge.net
  4. # This program is distributed under dual licensing. These include
  5. # GPL and a commercial licenses. Victor Boctor reserves the right to
  6. # change the license of future releases.
  7. # See docs/ folder for more details
  8. /**
  9. * Get all available status.
  10. *
  11. * @param string $p_username The name of the user trying to access the enumeration.
  12. * @param string $p_password The password of the user.
  13. * @return Array The requested enumeration
  14. */
  15. function mc_enum_status( $p_username, $p_password ) {
  16. if ( !mci_validate_enum_access( $p_username, $p_password ) ) {
  17. return new soap_fault( 'Client', '', 'Access Denied' );
  18. }
  19. return mci_explode_to_objectref( 'status' );
  20. }
  21. /**
  22. * Get all available priorities.
  23. *
  24. * @param string $p_username The name of the user trying to access the enumeration.
  25. * @param string $p_password The password of the user.
  26. * @return Array The requested enumeration
  27. */
  28. function mc_enum_priorities( $p_username, $p_password ) {
  29. if ( !mci_validate_enum_access( $p_username, $p_password ) ) {
  30. return new soap_fault( 'Client', '', 'Access Denied' );
  31. }
  32. return mci_explode_to_objectref( 'priority' );
  33. }
  34. /**
  35. * Get all available severities.
  36. *
  37. * @param string $p_username The name of the user trying to access the enumeration.
  38. * @param string $p_password The password of the user.
  39. * @return Array The requested enumeration
  40. */
  41. function mc_enum_severities( $p_username, $p_password ) {
  42. if ( !mci_validate_enum_access( $p_username, $p_password ) ) {
  43. return new soap_fault( 'Client', '', 'Access Denied' );
  44. }
  45. return mci_explode_to_objectref( 'severity' );
  46. }
  47. /**
  48. * Get all available reproducibilities.
  49. *
  50. * @param string $p_username The name of the user trying to access the enumeration.
  51. * @param string $p_password The password of the user.
  52. * @return Array The requested enumeration
  53. */
  54. function mc_enum_reproducibilities( $p_username, $p_password ) {
  55. if ( !mci_validate_enum_access( $p_username, $p_password ) ) {
  56. return new soap_fault( 'Client', '', 'Access Denied' );
  57. }
  58. return mci_explode_to_objectref( 'reproducibility' );
  59. }
  60. /**
  61. * Get all available projections.
  62. *
  63. * @param string $p_username The name of the user trying to access the enumeration.
  64. * @param string $p_password The password of the user.
  65. * @return Array The requested enumeration
  66. */
  67. function mc_enum_projections( $p_username, $p_password ) {
  68. if ( !mci_validate_enum_access( $p_username, $p_password ) ) {
  69. return new soap_fault( 'Client', '', 'Access Denied' );
  70. }
  71. return mci_explode_to_objectref( 'projection' );
  72. }
  73. /**
  74. * Get all available etas.
  75. *
  76. * @param string $p_username The name of the user trying to access the enumeration.
  77. * @param string $p_password The password of the user.
  78. * @return Array The requested enumeration
  79. */
  80. function mc_enum_etas( $p_username, $p_password ) {
  81. if ( !mci_validate_enum_access( $p_username, $p_password ) ) {
  82. return new soap_fault( 'Client', '', 'Access Denied' );
  83. }
  84. return mci_explode_to_objectref( 'eta' );
  85. }
  86. /**
  87. * Get all available resolutions.
  88. *
  89. * @param string $p_username The name of the user trying to access the enumeration.
  90. * @param string $p_password The password of the user.
  91. * @return Array The requested enumeration
  92. */
  93. function mc_enum_resolutions( $p_username, $p_password ) {
  94. if ( !mci_validate_enum_access( $p_username, $p_password ) ) {
  95. return new soap_fault( 'Client', '', 'Access Denied' );
  96. }
  97. return mci_explode_to_objectref( 'resolution' );
  98. }
  99. /**
  100. * Get all available access levels.
  101. *
  102. * @param string $p_username The name of the user trying to access the enumeration.
  103. * @param string $p_password The password of the user.
  104. * @return Array The requested enumeration
  105. */
  106. function mc_enum_access_levels( $p_username, $p_password ) {
  107. if ( !mci_validate_enum_access( $p_username, $p_password ) ) {
  108. return new soap_fault( 'Client', '', 'Access Denied' );
  109. }
  110. return mci_explode_to_objectref( 'access_levels' );
  111. }
  112. /**
  113. * Get all available project status.
  114. *
  115. * @param string $p_username The name of the user trying to access the enumeration.
  116. * @param string $p_password The password of the user.
  117. * @return Array The requested enumeration
  118. */
  119. function mc_enum_project_status( $p_username, $p_password ) {
  120. if ( !mci_validate_enum_access( $p_username, $p_password ) ) {
  121. return new soap_fault( 'Client', '', 'Access Denied' );
  122. }
  123. return mci_explode_to_objectref( 'project_status' );
  124. }
  125. /**
  126. * Get all available project view states.
  127. *
  128. * @param string $p_username The name of the user trying to access the enumeration.
  129. * @param string $p_password The password of the user.
  130. * @return Array The requested enumeration
  131. */
  132. function mc_enum_project_view_states( $p_username, $p_password ) {
  133. if ( !mci_validate_enum_access( $p_username, $p_password ) ) {
  134. return new soap_fault( 'Client', '', 'Access Denied' );
  135. }
  136. return mci_explode_to_objectref( 'project_view_state' );
  137. }
  138. /**
  139. * Get all available view states.
  140. *
  141. * @param string $p_username The name of the user trying to access the enumeration.
  142. * @param string $p_password The password of the user.
  143. * @return Array The requested enumeration
  144. */
  145. function mc_enum_view_states( $p_username, $p_password ) {
  146. if ( !mci_validate_enum_access( $p_username, $p_password ) ) {
  147. return new soap_fault( 'Client', '', 'Access Denied' );
  148. }
  149. return mci_explode_to_objectref( 'view_state' );
  150. }
  151. /**
  152. * Get all available custom field types.
  153. *
  154. * @param string $p_username The name of the user trying to access the enumeration.
  155. * @param string $p_password The password of the user.
  156. * @return Array The requested enumeration
  157. */
  158. function mc_enum_custom_field_types( $p_username, $p_password ) {
  159. if ( !mci_validate_enum_access( $p_username, $p_password ) ) {
  160. return new soap_fault( 'Client', '', 'Access Denied' );
  161. }
  162. return mci_explode_to_objectref( 'custom_field_type' );
  163. }
  164. /**
  165. * Get an appropriate enumeration. (Should become an internal function.)
  166. *
  167. * @param string $p_username The name of the user trying to access the enumeration.
  168. * @param string $p_password The password of the user.
  169. * @param string $p_enumeration The enumeration to get.
  170. * @return string The requested enumeration.
  171. */
  172. function mc_enum_get( $p_username, $p_password, $p_enumeration ) {
  173. if ( ! mci_validate_enum_access($p_username, $p_password)) {
  174. return new soap_fault( 'Client', '', 'Access Denied' );
  175. }
  176. // safe to call directly after login checks
  177. $t_user_id = auth_get_current_user_id();
  178. $t_lang = mci_get_user_lang( $t_user_id );
  179. return lang_get( $p_enumeration . '_enum_string', $t_lang );
  180. }
  181. /**
  182. * Explode a configuration enumeration name into an array structure that can
  183. * be safely converted into an ObjectRef structure.
  184. *
  185. * @param string $p_enumeration_name The name of the enumeration to convert
  186. * @return Array The converted enumeration
  187. */
  188. function mci_explode_to_objectref( $p_enumeration_name ) {
  189. $t_config_var_name = $p_enumeration_name . '_enum_string';
  190. $t_config_var_value = config_get( $t_config_var_name );
  191. $t_translated_values = lang_get( $t_config_var_name, mci_get_user_lang( auth_get_current_user_id() ) );
  192. $t_enum_values = MantisEnum::getValues( $t_config_var_value );
  193. $t_result = array();
  194. foreach ( $t_enum_values as $t_key ) {
  195. $t_translated = MantisEnum::getLocalizedLabel( $t_config_var_value, $t_translated_values, $t_key );
  196. $t_result[] = array(
  197. 'id' => $t_key,
  198. 'name' => $t_translated,
  199. );
  200. }
  201. return $t_result;
  202. }
  203. /**
  204. * Validates that the user has access to the enumeration values
  205. *
  206. * @param string $p_username
  207. * @param string $p_password
  208. * @return boolean true if the user has access, false otherwise
  209. */
  210. function mci_validate_enum_access($p_username, $p_password) {
  211. $t_user_id = mci_check_login( $p_username, $p_password );
  212. if( $t_user_id === false ) {
  213. return false;
  214. }
  215. if( !mci_has_readonly_access( $t_user_id ) ) {
  216. return false;
  217. }
  218. return true;
  219. }
  220. /**
  221. * Get a localized enumeration element.
  222. *
  223. * @param integer $p_enum_id The id of the enumeration element to retrieve.
  224. * @param String $p_enum_type The type of the enumeration element (e.g. 'status', 'reproducibility' etc.).
  225. * @param String $p_lang The language for the name field.
  226. * @return Array an Array containing the id and the name of the enumeration element.
  227. */
  228. function mci_enum_get_array_by_id( $p_enum_id, $p_enum_type, $p_lang ) {
  229. $t_result = array();
  230. $t_result['id'] = $p_enum_id;
  231. $t_result['name'] = mci_get_enum_element( $p_enum_type, $p_enum_id, $p_lang );
  232. return $t_result;
  233. }
  234. /**
  235. * Get the enum id given the enum label.
  236. *
  237. * @param $p_enum_string The enum string to search in.
  238. * @param $p_label The label to search for.
  239. *
  240. * @return The id corresponding to the given label, or 0 if not found.
  241. */
  242. function mci_get_enum_value_from_label( $p_enum_string, $p_label ) {
  243. $t_value = MantisEnum::getValue( $p_enum_string, $p_label );
  244. if ( $t_value === false ) {
  245. return 0;
  246. }
  247. return $t_value;
  248. }
  249. /**
  250. * Get the enumeration id given an object ref. The id is set based on the following algorithm:
  251. * - id from objectref.
  252. * - id corresponding to name in object ref.
  253. * - default value for the specified enumeration, if exists in configuration.
  254. * - first id, if object ref doesn't contain an id or a name.
  255. *
  256. * @param string $p_enum The name of the enumeration as in the MantisBT configuration file
  257. * @param ObjectRef $p_object_ref An associate array with "id" and "name" keys.
  258. * @return enum id
  259. */
  260. function mci_get_enum_id_from_objectref( $p_enum, $p_object_ref ) {
  261. if( !is_null( $p_object_ref ) && isset( $p_object_ref['id'] ) && (int) $p_object_ref['id'] != 0 ) {
  262. $t_id = (int) $p_object_ref['id'];
  263. } else {
  264. $t_enum = config_get( $p_enum . '_enum_string' );
  265. if( !is_null( $p_object_ref ) && isset( $p_object_ref['name'] ) && !is_blank( $p_object_ref['name'] ) ) {
  266. $t_id = mci_get_enum_value_from_label( $t_enum, $p_object_ref['name'] );
  267. if( $t_id == 0 ) {
  268. $t_id = config_get( 'mc_' . $p_enum . '_enum_default_when_not_found' );
  269. }
  270. } else {
  271. $t_default_id = config_get( 'default_bug_' . $p_enum, 0 );
  272. if( $t_default_id == 0 ) {
  273. $t_array = mci_explode_to_objectref( $p_enum );
  274. $t_id = (int) $t_array[0]['id'];
  275. } else {
  276. $t_id = $t_default_id;
  277. }
  278. }
  279. }
  280. return $t_id;
  281. }