00001 <?php
00007 namespace Habari;
00008
00013 class Users extends \ArrayObject
00014 {
00015 protected $get_param_cache;
00016
00024 public static function get( $paramarray = array() )
00025 {
00026 $params = array();
00027 $fns = array( 'get_results', 'get_row', 'get_value' );
00028 $select = '';
00029
00030 foreach ( User::default_fields() as $field => $value ) {
00031 $select .= ( '' == $select )
00032 ? "{users}.$field"
00033 : ", {users}.$field";
00034 }
00035
00036 $orderby = 'id ASC';
00037 $nolimit = true;
00038
00039
00040 $paramarray = Utils::get_params( $paramarray );
00041
00042
00043 if ( isset( $paramarray['where'] ) && is_array( $paramarray['where'] ) ) {
00044 $wheresets = $paramarray['where'];
00045 }
00046 else {
00047 $wheresets = array( array() );
00048 }
00049
00050 $wheres = array();
00051 $join = '';
00052
00053 if( isset($paramarray['orderby']) ) {
00054 $orderby = $paramarray['orderby'];
00055 }
00056
00057 if ( isset( $paramarray['where'] ) && is_string( $paramarray['where'] ) ) {
00058 $wheres[] = $paramarray['where'];
00059 }
00060 else {
00061 foreach ( $wheresets as $paramset ) {
00062
00063 $where = array();
00064 $paramset = array_merge( (array) $paramarray, (array) $paramset );
00065
00066 $default_fields = User::default_fields();
00067 unset($default_fields['id']);
00068
00069 foreach ( $default_fields as $field => $scrap ) {
00070 if ( !isset( $paramset[$field] ) ) {
00071 continue;
00072 }
00073
00074 switch ( $field ) {
00075 default:
00076 $where[] = "{$field} = ?";
00077 $params[] = $paramset[$field];
00078 }
00079 }
00080
00081 if ( isset( $paramset['info'] ) && is_array( $paramset['info'] ) ) {
00082 $join .= 'INNER JOIN {userinfo} ON {users}.id = {userinfo}.user_id';
00083 foreach ( $paramset['info'] as $info_name => $info_value ) {
00084 $where[] = '{userinfo}.name = ? AND {userinfo}.value = ?';
00085 $params[] = $info_name;
00086 $params[] = $info_value;
00087 }
00088 }
00089
00090 if ( isset( $paramset['group'] ) && is_array( $paramset['group'] ) ) {
00091 $join .= ' INNER JOIN {users_groups} ON {users}.id = {users_groups}.user_id';
00092 foreach ( $paramset['group'] as $group ) {
00093 $group_id = UserGroup::get_by_name( $group )->id;
00094 $where[] = '{users_groups}.group_id = ?';
00095 $params[] = $group_id;
00096 }
00097 }
00098
00099 if( isset( $paramset['id']) ) {
00100 if( is_array($paramset['id']) ) {
00101 array_walk( $paramset['id'], function(&$a) {$a = intval( $a );} );
00102 $where[] = "{users}.id IN (" . implode( ',', array_fill( 0, count( $paramset['id'] ), '?' ) ) . ")";
00103 $params = array_merge( $params, $paramset['id'] );
00104 } else {
00105 $where[] = "{users}.id = ?";
00106 $params[] = (int) $paramset['id'];
00107 }
00108 }
00109
00110 if ( isset( $paramset['not:id'] ) ) {
00111 if ( is_array( $paramset['not:id'] ) ) {
00112 array_walk( $paramset['not:id'], function(&$a) {$a = intval( $a );} );
00113 $where[] = "{users}.id NOT IN (" . implode( ',', array_fill( 0, count( $paramset['not:id'] ), '?' ) ) . ")";
00114 $params = array_merge( $params, $paramset['not:id'] );
00115 }
00116 else {
00117 $where[] = "{users}.id != ?";
00118 $params[] = (int) $paramset['not:id'];
00119 }
00120 }
00121
00122 if ( isset( $paramset['criteria'] ) ) {
00123 if ( isset( $paramset['criteria_fields'] ) ) {
00124
00125 if ( !is_array( $paramset['criteria_fields'] ) && is_string( $paramset['criteria_fields'] ) ) {
00126 $paramset['criteria_fields'] = explode( ',', $paramset['criteria_fields'] );
00127 }
00128 }
00129 else {
00130 $paramset['criteria_fields'] = array( 'username' );
00131 }
00132 $paramset['criteria_fields'] = array_unique( $paramset['criteria_fields'] );
00133
00134
00135 preg_match_all( '/(?<=")([\p{L}\p{N}]+[^"]*)(?=")|([\p{L}\p{N}]+)/u', $paramset['criteria'], $matches );
00136 $where_search = array();
00137 foreach ( $matches[0] as $word ) {
00138 foreach ( $paramset['criteria_fields'] as $criteria_field ) {
00139 $where_search[] .= "( LOWER( {users}.$criteria_field ) LIKE ? )";
00140 $params[] = '%' . MultiByte::strtolower( $word ) . '%';
00141 }
00142 }
00143 if ( count( $where_search ) > 0 ) {
00144 $where[] = '(' . implode( " \nOR\n ", $where_search ).')';
00145 }
00146 }
00147
00148 if ( count( $where ) > 0 ) {
00149 $wheres[] = ' (' . implode( ' AND ', $where ) . ') ';
00150 }
00151 }
00152 }
00153
00154
00155 $possible = array( 'fetch_fn', 'count', 'nolimit', 'limit', 'offset' );
00156 foreach ( $possible as $varname ) {
00157 if ( isset( $paramarray[$varname] ) ) {
00158 $$varname = $paramarray[$varname];
00159 }
00160 }
00161
00162 if ( isset( $fetch_fn ) ) {
00163 if ( ! in_array( $fetch_fn, $fns ) ) {
00164 $fetch_fn = $fns[0];
00165 }
00166 }
00167 else {
00168 $fetch_fn = $fns[0];
00169 }
00170
00171
00172 if ( isset( $count ) ) {
00173 $select = "COUNT($count)";
00174 $fetch_fn = 'get_value';
00175 $orderby = '';
00176 }
00177 if ( isset( $limit ) ) {
00178 unset( $nolimit );
00179 $limit = " LIMIT $limit";
00180 if ( isset( $offset ) ) {
00181 $limit .= " OFFSET $offset";
00182 }
00183 }
00184 if ( isset( $nolimit ) ) {
00185 $limit = '';
00186 }
00187
00188 $query = '
00189 SELECT ' . $select
00190 . ' FROM {users} '
00191 . $join;
00192
00193 if ( count( $wheres ) > 0 ) {
00194 $query .= ' WHERE ' . implode( " \nOR\n ", $wheres );
00195 }
00196 $query .= ( ( $orderby == '' ) ? '' : ' ORDER BY ' . $orderby ) . $limit;
00197
00198
00199 DB::set_fetch_mode( \PDO::FETCH_CLASS );
00200 DB::set_fetch_class( 'User' );
00201 $results = DB::$fetch_fn( $query, $params, 'User' );
00202
00203 if ( 'get_results' != $fetch_fn ) {
00204
00205 return $results;
00206 }
00207 elseif ( is_array( $results ) ) {
00208 $c = __CLASS__;
00209 $return_value = new $c( $results );
00210 $return_value->get_param_cache = $paramarray;
00211 return $return_value;
00212 }
00213 }
00214
00222 public static function get_by_info( $key, $value = null )
00223 {
00224
00225 if ( null === $value ) {
00226 if ( is_array( $key ) ) {
00227 $params['info'] = $key;
00228 }
00229 else {
00230
00231 return false;
00232 }
00233 }
00234 else {
00235 $params['info'] = array( $key => $value );
00236 }
00237
00238 return self::get( $params );
00239 }
00240
00246 public static function get_all()
00247 {
00248 $params = array(
00249 'orderby' => 'username ASC'
00250 );
00251
00252 return self::get( $params );
00253 }
00254
00255 }
00256 ?>