/issues/core/profile_api.php

https://github.com/osarrat/sigmah-website · PHP · 332 lines · 168 code · 58 blank · 106 comment · 18 complexity · 3c8c3540af14a5c77d560c077535569e 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. * @package CoreAPI
  17. * @subpackage ProfileAPI
  18. * @copyright Copyright (C) 2000 - 2002 Kenzaburo Ito - kenito@300baud.org
  19. * @copyright Copyright (C) 2002 - 2010 MantisBT Team - mantisbt-dev@lists.sourceforge.net
  20. * @link http://www.mantisbt.org
  21. */
  22. /**
  23. * Create a new profile for the user, return the ID of the new profile
  24. * @param int $p_user_id
  25. * @param string $p_platform
  26. * @param string $p_os
  27. * @param string $p_os_build
  28. * @param string $p_description
  29. * @return int
  30. */
  31. function profile_create( $p_user_id, $p_platform, $p_os, $p_os_build, $p_description ) {
  32. $p_user_id = (int)$p_user_id;
  33. if( ALL_USERS != $p_user_id ) {
  34. user_ensure_unprotected( $p_user_id );
  35. }
  36. # platform cannot be blank
  37. if( is_blank( $p_platform ) ) {
  38. error_parameters( lang_get( 'platform' ) );
  39. trigger_error( ERROR_EMPTY_FIELD, ERROR );
  40. }
  41. # os cannot be blank
  42. if( is_blank( $p_os ) ) {
  43. error_parameters( lang_get( 'operating_system' ) );
  44. trigger_error( ERROR_EMPTY_FIELD, ERROR );
  45. }
  46. # os_build cannot be blank
  47. if( is_blank( $p_os_build ) ) {
  48. error_parameters( lang_get( 'version' ) );
  49. trigger_error( ERROR_EMPTY_FIELD, ERROR );
  50. }
  51. $t_user_profile_table = db_get_table( 'mantis_user_profile_table' );
  52. # Add profile
  53. $query = "INSERT INTO $t_user_profile_table
  54. ( user_id, platform, os, os_build, description )
  55. VALUES
  56. ( " . db_param() . ', ' . db_param() . ', ' . db_param() . ', ' . db_param() . ', ' . db_param() . ' )';
  57. db_query_bound( $query, Array( $p_user_id, $p_platform, $p_os, $p_os_build, $p_description ) );
  58. return db_insert_id( $t_user_profile_table );
  59. }
  60. /**
  61. * Delete a profile for the user
  62. *
  63. * Note that although profile IDs are currently globally unique, the existing
  64. * code included the user_id in the query and I have chosen to keep that for
  65. * this API as it hides the details of id implementation from users of the API
  66. * @param int $p_user_id
  67. * @param int $p_profile_id
  68. * @return true
  69. */
  70. function profile_delete( $p_user_id, $p_profile_id ) {
  71. $c_user_id = db_prepare_int( $p_user_id );
  72. $c_profile_id = db_prepare_int( $p_profile_id );
  73. if( ALL_USERS != $p_user_id ) {
  74. user_ensure_unprotected( $p_user_id );
  75. }
  76. $t_user_profile_table = db_get_table( 'mantis_user_profile_table' );
  77. # Delete the profile
  78. $query = "DELETE FROM $t_user_profile_table
  79. WHERE id=" . db_param() . " AND user_id=" . db_param();
  80. db_query_bound( $query, Array( $c_profile_id, $c_user_id ) );
  81. # db_query errors on failure so:
  82. return true;
  83. }
  84. /**
  85. * Update a profile for the user
  86. * @param int $p_user_id
  87. * @param int $p_profile_id
  88. * @param string $p_platform
  89. * @param string $p_os
  90. * @param string $p_os_build
  91. * @param string $p_description
  92. * @return true
  93. */
  94. function profile_update( $p_user_id, $p_profile_id, $p_platform, $p_os, $p_os_build, $p_description ) {
  95. $c_user_id = db_prepare_int( $p_user_id );
  96. $c_profile_id = db_prepare_int( $p_profile_id );
  97. if( ALL_USERS != $p_user_id ) {
  98. user_ensure_unprotected( $p_user_id );
  99. }
  100. # platform cannot be blank
  101. if( is_blank( $p_platform ) ) {
  102. error_parameters( lang_get( 'platform' ) );
  103. trigger_error( ERROR_EMPTY_FIELD, ERROR );
  104. }
  105. # os cannot be blank
  106. if( is_blank( $p_os ) ) {
  107. error_parameters( lang_get( 'operating_system' ) );
  108. trigger_error( ERROR_EMPTY_FIELD, ERROR );
  109. }
  110. # os_build cannot be blank
  111. if( is_blank( $p_os_build ) ) {
  112. error_parameters( lang_get( 'version' ) );
  113. trigger_error( ERROR_EMPTY_FIELD, ERROR );
  114. }
  115. $t_user_profile_table = db_get_table( 'mantis_user_profile_table' );
  116. # Add item
  117. $query = "UPDATE $t_user_profile_table
  118. SET platform=" . db_param() . ",
  119. os=" . db_param() . ",
  120. os_build=" . db_param() . ",
  121. description=" . db_param() . "
  122. WHERE id=" . db_param() . " AND user_id=" . db_param();
  123. $result = db_query_bound( $query, Array( $p_platform, $p_os, $p_os_build, $p_description, $c_profile_id, $c_user_id ) );
  124. # db_query errors on failure so:
  125. return true;
  126. }
  127. /**
  128. * Return a profile row from the database
  129. * @param int $p_user_id
  130. * @param int $p_profile_id
  131. * @return array
  132. */
  133. function profile_get_row( $p_user_id, $p_profile_id ) {
  134. $c_user_id = db_prepare_int( $p_user_id );
  135. $c_profile_id = db_prepare_int( $p_profile_id );
  136. $t_user_profile_table = db_get_table( 'mantis_user_profile_table' );
  137. $query = "SELECT *
  138. FROM $t_user_profile_table
  139. WHERE id=" . db_param() . " AND user_id=" . db_param();
  140. $result = db_query_bound( $query, Array( $c_profile_id, $c_user_id ) );
  141. return db_fetch_array( $result );
  142. }
  143. /**
  144. * Return a profile row from the database
  145. * @param int $p_profile_id
  146. * @return array
  147. * @todo relationship of this function to profile_get_row?
  148. */
  149. function profile_get_row_direct( $p_profile_id ) {
  150. $c_profile_id = db_prepare_int( $p_profile_id );
  151. $t_user_profile_table = db_get_table( 'mantis_user_profile_table' );
  152. $query = "SELECT *
  153. FROM $t_user_profile_table
  154. WHERE id=" . db_param();
  155. $result = db_query_bound( $query, Array( $c_profile_id ) );
  156. return db_fetch_array( $result );
  157. }
  158. /**
  159. * Return an array containing all rows for a given user
  160. * @param int $p_user_id
  161. * @return array
  162. */
  163. function profile_get_all_rows( $p_user_id ) {
  164. $c_user_id = db_prepare_int( $p_user_id );
  165. $t_user_profile_table = db_get_table( 'mantis_user_profile_table' );
  166. $query = "SELECT *
  167. FROM $t_user_profile_table
  168. WHERE user_id=" . db_param() . "
  169. ORDER BY platform, os, os_build";
  170. $result = db_query_bound( $query, Array( $c_user_id ) );
  171. $t_rows = array();
  172. $t_row_count = db_num_rows( $result );
  173. for( $i = 0;$i < $t_row_count;$i++ ) {
  174. array_push( $t_rows, db_fetch_array( $result ) );
  175. }
  176. return $t_rows;
  177. }
  178. /**
  179. * Return an array containing all profiles for a given user,
  180. * including global profiles
  181. * @param int $p_user_id
  182. * @return array
  183. */
  184. function profile_get_all_for_user( $p_user_id ) {
  185. if( ALL_USERS == $p_user_id ) {
  186. return profile_get_all_rows( ALL_USERS );
  187. } else {
  188. $t_profiles_array = array_merge( profile_get_all_rows( ALL_USERS ), profile_get_all_rows( $p_user_id ) );
  189. asort( $t_profiles_array );
  190. return $t_profiles_array;
  191. }
  192. }
  193. /**
  194. * Return an array of strings containing unique values for the specified field based
  195. * on private and public profiles accessible to the specified user.
  196. * @param string $p_field
  197. * @param int $p_user_id
  198. * @return array
  199. */
  200. function profile_get_field_all_for_user( $p_field, $p_user_id = null ) {
  201. $c_user_id = ( $p_user_id === null ) ? auth_get_current_user_id() : db_prepare_int( $p_user_id );
  202. switch( $p_field ) {
  203. case 'id':
  204. case 'user_id':
  205. case 'platform':
  206. case 'os':
  207. case 'os_build':
  208. case 'description':
  209. $c_field = $p_field;
  210. break;
  211. default:
  212. trigger_error( ERROR_GENERIC, ERROR );
  213. }
  214. $t_user_profile_table = db_get_table( 'mantis_user_profile_table' );
  215. $query = "SELECT DISTINCT $c_field
  216. FROM $t_user_profile_table
  217. WHERE ( user_id=" . db_param() . " ) OR ( user_id = 0 )
  218. ORDER BY $c_field";
  219. $result = db_query_bound( $query, Array( $c_user_id ) );
  220. $t_rows = array();
  221. $t_row_count = db_num_rows( $result );
  222. for( $i = 0;$i < $t_row_count;$i++ ) {
  223. $t_row = db_fetch_array( $result );
  224. array_push( $t_rows, $t_row[$c_field] );
  225. }
  226. return $t_rows;
  227. }
  228. /**
  229. * Return an array containing all profiles used in a given project
  230. * @param int $p_project_id
  231. * @return array
  232. */
  233. function profile_get_all_for_project( $p_project_id ) {
  234. $t_project_where = helper_project_specific_where( $p_project_id );
  235. $t_bug_table = db_get_table( 'mantis_bug_table' );
  236. $t_user_profile_table = db_get_table( 'mantis_user_profile_table' );
  237. # using up.* causes an SQL error on MS SQL since up.description is of type text
  238. $query = "SELECT DISTINCT(up.id), up.user_id, up.platform, up.os, up.os_build
  239. FROM $t_user_profile_table up, $t_bug_table b
  240. WHERE $t_project_where
  241. AND up.id = b.profile_id
  242. ORDER BY platform, os, os_build";
  243. $result = db_query_bound( $query );
  244. $t_rows = array();
  245. $t_row_count = db_num_rows( $result );
  246. for( $i = 0;$i < $t_row_count;$i++ ) {
  247. array_push( $t_rows, db_fetch_array( $result ) );
  248. }
  249. return $t_rows;
  250. }
  251. /**
  252. * Returns the default profile
  253. * @param int $p_user_id
  254. * @return string
  255. */
  256. function profile_get_default( $p_user_id ) {
  257. $c_user_id = db_prepare_int( $p_user_id );
  258. $t_mantis_user_pref_table = db_get_table( 'mantis_user_pref_table' );
  259. $query = "SELECT default_profile
  260. FROM $t_mantis_user_pref_table
  261. WHERE user_id=" . db_param();
  262. $result = db_query_bound( $query, Array( $c_user_id ) );
  263. $t_default_profile = db_result( $result, 0, 0 );
  264. return $t_default_profile;
  265. }
  266. /**
  267. * Returns whether the specified profile is global
  268. * @param int $p_profile_id
  269. * @return bool
  270. */
  271. function profile_is_global( $p_profile_id ) {
  272. $t_row = profile_get_row( ALL_USERS, $p_profile_id );
  273. return( $t_row !== false );
  274. }