| [ Index ] |
PHP Cross Reference of MantisBT |
[Summary view] [Print] [Text view]
1 <?php 2 # MantisBT - A PHP based bugtracking system 3 4 # MantisBT is free software: you can redistribute it and/or modify 5 # it under the terms of the GNU General Public License as published by 6 # the Free Software Foundation, either version 2 of the License, or 7 # (at your option) any later version. 8 # 9 # MantisBT is distributed in the hope that it will be useful, 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 # GNU General Public License for more details. 13 # 14 # You should have received a copy of the GNU General Public License 15 # along with MantisBT. If not, see <http://www.gnu.org/licenses/>. 16 17 /** 18 * Profile API 19 * 20 * @package CoreAPI 21 * @subpackage ProfileAPI 22 * @copyright Copyright (C) 2000 - 2002 Kenzaburo Ito - kenito@300baud.org 23 * @copyright Copyright (C) 2002 - 2011 MantisBT Team - mantisbt-dev@lists.sourceforge.net 24 * @link http://www.mantisbt.org 25 * 26 * @uses authentication_api.php 27 * @uses constant_inc.php 28 * @uses database_api.php 29 * @uses error_api.php 30 * @uses helper_api.php 31 * @uses lang_api.php 32 * @uses user_api.php 33 * @uses utility_api.php 34 */ 35 36 require_api( 'authentication_api.php' ); 37 require_api( 'constant_inc.php' ); 38 require_api( 'database_api.php' ); 39 require_api( 'error_api.php' ); 40 require_api( 'helper_api.php' ); 41 require_api( 'lang_api.php' ); 42 require_api( 'user_api.php' ); 43 require_api( 'utility_api.php' ); 44 45 /** 46 * Create a new profile for the user, return the ID of the new profile 47 * @param int $p_user_id 48 * @param string $p_platform 49 * @param string $p_os 50 * @param string $p_os_build 51 * @param string $p_description 52 * @return int 53 */ 54 function profile_create( $p_user_id, $p_platform, $p_os, $p_os_build, $p_description ) { 55 $p_user_id = (int)$p_user_id; 56 57 if( ALL_USERS != $p_user_id ) { 58 user_ensure_unprotected( $p_user_id ); 59 } 60 61 # platform cannot be blank 62 if( is_blank( $p_platform ) ) { 63 error_parameters( lang_get( 'platform' ) ); 64 trigger_error( ERROR_EMPTY_FIELD, ERROR ); 65 } 66 67 # os cannot be blank 68 if( is_blank( $p_os ) ) { 69 error_parameters( lang_get( 'operating_system' ) ); 70 trigger_error( ERROR_EMPTY_FIELD, ERROR ); 71 } 72 73 # os_build cannot be blank 74 if( is_blank( $p_os_build ) ) { 75 error_parameters( lang_get( 'version' ) ); 76 trigger_error( ERROR_EMPTY_FIELD, ERROR ); 77 } 78 79 $t_user_profile_table = db_get_table( 'user_profile' ); 80 81 # Add profile 82 $query = "INSERT INTO $t_user_profile_table 83 ( user_id, platform, os, os_build, description ) 84 VALUES 85 ( " . db_param() . ', ' . db_param() . ', ' . db_param() . ', ' . db_param() . ', ' . db_param() . ' )'; 86 db_query_bound( $query, Array( $p_user_id, $p_platform, $p_os, $p_os_build, $p_description ) ); 87 88 return db_insert_id( $t_user_profile_table ); 89 } 90 91 /** 92 * Delete a profile for the user 93 * 94 * Note that although profile IDs are currently globally unique, the existing 95 * code included the user_id in the query and I have chosen to keep that for 96 * this API as it hides the details of id implementation from users of the API 97 * @param int $p_user_id 98 * @param int $p_profile_id 99 * @return true 100 */ 101 function profile_delete( $p_user_id, $p_profile_id ) { 102 $c_user_id = db_prepare_int( $p_user_id ); 103 $c_profile_id = db_prepare_int( $p_profile_id ); 104 105 if( ALL_USERS != $p_user_id ) { 106 user_ensure_unprotected( $p_user_id ); 107 } 108 109 $t_user_profile_table = db_get_table( 'user_profile' ); 110 111 # Delete the profile 112 $query = "DELETE FROM $t_user_profile_table 113 WHERE id=" . db_param() . " AND user_id=" . db_param(); 114 db_query_bound( $query, Array( $c_profile_id, $c_user_id ) ); 115 116 # db_query errors on failure so: 117 return true; 118 } 119 120 /** 121 * Update a profile for the user 122 * @param int $p_user_id 123 * @param int $p_profile_id 124 * @param string $p_platform 125 * @param string $p_os 126 * @param string $p_os_build 127 * @param string $p_description 128 * @return true 129 */ 130 function profile_update( $p_user_id, $p_profile_id, $p_platform, $p_os, $p_os_build, $p_description ) { 131 $c_user_id = db_prepare_int( $p_user_id ); 132 $c_profile_id = db_prepare_int( $p_profile_id ); 133 134 if( ALL_USERS != $p_user_id ) { 135 user_ensure_unprotected( $p_user_id ); 136 } 137 138 # platform cannot be blank 139 if( is_blank( $p_platform ) ) { 140 error_parameters( lang_get( 'platform' ) ); 141 trigger_error( ERROR_EMPTY_FIELD, ERROR ); 142 } 143 144 # os cannot be blank 145 if( is_blank( $p_os ) ) { 146 error_parameters( lang_get( 'operating_system' ) ); 147 trigger_error( ERROR_EMPTY_FIELD, ERROR ); 148 } 149 150 # os_build cannot be blank 151 if( is_blank( $p_os_build ) ) { 152 error_parameters( lang_get( 'version' ) ); 153 trigger_error( ERROR_EMPTY_FIELD, ERROR ); 154 } 155 156 $t_user_profile_table = db_get_table( 'user_profile' ); 157 158 # Add item 159 $query = "UPDATE $t_user_profile_table 160 SET platform=" . db_param() . ", 161 os=" . db_param() . ", 162 os_build=" . db_param() . ", 163 description=" . db_param() . " 164 WHERE id=" . db_param() . " AND user_id=" . db_param(); 165 $result = db_query_bound( $query, Array( $p_platform, $p_os, $p_os_build, $p_description, $c_profile_id, $c_user_id ) ); 166 167 # db_query errors on failure so: 168 return true; 169 } 170 171 /** 172 * Return a profile row from the database 173 * @param int $p_user_id 174 * @param int $p_profile_id 175 * @return array 176 */ 177 function profile_get_row( $p_user_id, $p_profile_id ) { 178 $c_user_id = db_prepare_int( $p_user_id ); 179 $c_profile_id = db_prepare_int( $p_profile_id ); 180 181 $t_user_profile_table = db_get_table( 'user_profile' ); 182 183 $query = "SELECT * 184 FROM $t_user_profile_table 185 WHERE id=" . db_param() . " AND user_id=" . db_param(); 186 $result = db_query_bound( $query, Array( $c_profile_id, $c_user_id ) ); 187 188 return db_fetch_array( $result ); 189 } 190 191 /** 192 * Return a profile row from the database 193 * @param int $p_profile_id 194 * @return array 195 * @todo relationship of this function to profile_get_row? 196 */ 197 function profile_get_row_direct( $p_profile_id ) { 198 $c_profile_id = db_prepare_int( $p_profile_id ); 199 200 $t_user_profile_table = db_get_table( 'user_profile' ); 201 202 $query = "SELECT * 203 FROM $t_user_profile_table 204 WHERE id=" . db_param(); 205 $result = db_query_bound( $query, Array( $c_profile_id ) ); 206 207 return db_fetch_array( $result ); 208 } 209 210 /** 211 * Return an array containing all rows for a given user 212 * @param int $p_user_id 213 * @return array 214 */ 215 function profile_get_all_rows( $p_user_id ) { 216 $c_user_id = db_prepare_int( $p_user_id ); 217 218 $t_user_profile_table = db_get_table( 'user_profile' ); 219 220 $query = "SELECT * 221 FROM $t_user_profile_table 222 WHERE user_id=" . db_param() . " 223 ORDER BY platform, os, os_build"; 224 $result = db_query_bound( $query, Array( $c_user_id ) ); 225 226 $t_rows = array(); 227 $t_row_count = db_num_rows( $result ); 228 229 for( $i = 0;$i < $t_row_count;$i++ ) { 230 array_push( $t_rows, db_fetch_array( $result ) ); 231 } 232 233 return $t_rows; 234 } 235 236 /** 237 * Return an array containing all profiles for a given user, 238 * including global profiles 239 * @param int $p_user_id 240 * @return array 241 */ 242 function profile_get_all_for_user( $p_user_id ) { 243 if( ALL_USERS == $p_user_id ) { 244 return profile_get_all_rows( ALL_USERS ); 245 } else { 246 $t_profiles_array = array_merge( profile_get_all_rows( ALL_USERS ), profile_get_all_rows( $p_user_id ) ); 247 asort( $t_profiles_array ); 248 return $t_profiles_array; 249 } 250 } 251 252 /** 253 * Return an array of strings containing unique values for the specified field based 254 * on private and public profiles accessible to the specified user. 255 * @param string $p_field 256 * @param int $p_user_id 257 * @return array 258 */ 259 function profile_get_field_all_for_user( $p_field, $p_user_id = null ) { 260 $c_user_id = ( $p_user_id === null ) ? auth_get_current_user_id() : db_prepare_int( $p_user_id ); 261 262 switch( $p_field ) { 263 case 'id': 264 case 'user_id': 265 case 'platform': 266 case 'os': 267 case 'os_build': 268 case 'description': 269 $c_field = $p_field; 270 break; 271 default: 272 trigger_error( ERROR_GENERIC, ERROR ); 273 } 274 275 $t_user_profile_table = db_get_table( 'user_profile' ); 276 277 $query = "SELECT DISTINCT $c_field 278 FROM $t_user_profile_table 279 WHERE ( user_id=" . db_param() . " ) OR ( user_id = 0 ) 280 ORDER BY $c_field"; 281 $result = db_query_bound( $query, Array( $c_user_id ) ); 282 283 $t_rows = array(); 284 285 $t_row_count = db_num_rows( $result ); 286 287 for( $i = 0;$i < $t_row_count;$i++ ) { 288 $t_row = db_fetch_array( $result ); 289 array_push( $t_rows, $t_row[$c_field] ); 290 } 291 292 return $t_rows; 293 } 294 295 /** 296 * Return an array containing all profiles used in a given project 297 * @param int $p_project_id 298 * @return array 299 */ 300 function profile_get_all_for_project( $p_project_id ) { 301 $t_project_where = helper_project_specific_where( $p_project_id ); 302 303 $t_bug_table = db_get_table( 'bug' ); 304 $t_user_profile_table = db_get_table( 'user_profile' ); 305 306 # using up.* causes an SQL error on MS SQL since up.description is of type text 307 $query = "SELECT DISTINCT(up.id), up.user_id, up.platform, up.os, up.os_build 308 FROM $t_user_profile_table up, $t_bug_table b 309 WHERE $t_project_where 310 AND up.id = b.profile_id 311 ORDER BY platform, os, os_build"; 312 $result = db_query_bound( $query ); 313 314 $t_rows = array(); 315 $t_row_count = db_num_rows( $result ); 316 317 for( $i = 0;$i < $t_row_count;$i++ ) { 318 array_push( $t_rows, db_fetch_array( $result ) ); 319 } 320 321 return $t_rows; 322 } 323 324 /** 325 * Returns the default profile 326 * @param int $p_user_id 327 * @return string 328 */ 329 function profile_get_default( $p_user_id ) { 330 $c_user_id = db_prepare_int( $p_user_id ); 331 $t_mantis_user_pref_table = db_get_table( 'user_pref' ); 332 333 $query = "SELECT default_profile 334 FROM $t_mantis_user_pref_table 335 WHERE user_id=" . db_param(); 336 $result = db_query_bound( $query, Array( $c_user_id ) ); 337 338 $t_default_profile = db_result( $result, 0, 0 ); 339 340 return $t_default_profile; 341 } 342 343 /** 344 * Returns whether the specified profile is global 345 * @param int $p_profile_id 346 * @return bool 347 */ 348 function profile_is_global( $p_profile_id ) { 349 $t_row = profile_get_row( ALL_USERS, $p_profile_id ); 350 return( $t_row !== false ); 351 } 352
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Thu Jul 28 15:48:31 2011 | Cross-referenced by PHPXref 0.7 |