00001 <?php
00007 namespace Habari;
00008
00013 class UserGroups 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 ( UserGroup::default_fields() as $field => $value ) {
00031 $select .= ( '' == $select )
00032 ? "{groups}.$field"
00033 : ", {groups}.$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 if ( isset( $paramarray['where'] ) && is_string( $paramarray['where'] ) ) {
00053 $wheres[] = $paramarray['where'];
00054 }
00055 else {
00056 foreach ( $wheresets as $paramset ) {
00057
00058 $where = array();
00059 $paramset = array_merge( (array) $paramarray, (array) $paramset );
00060
00061 $default_fields = UserGroup::default_fields();
00062 foreach ( UserGroup::default_fields() as $field => $scrap ) {
00063 if ( !isset( $paramset[$field] ) ) {
00064 continue;
00065 }
00066 switch ( $field ) {
00067 case 'id':
00068 if ( !is_numeric( $paramset[$field] ) ) {
00069 continue;
00070 }
00071 default:
00072 $where[] = "{$field} = ?";
00073 $params[] = $paramset[$field];
00074 }
00075 }
00076
00077 if ( count( $where ) > 0 ) {
00078 $wheres[] = ' (' . implode( ' AND ', $where ) . ') ';
00079 }
00080 }
00081 }
00082
00083
00084 $possible = array( 'fetch_fn', 'count', 'nolimit', 'limit', 'offset' );
00085 foreach ( $possible as $varname ) {
00086 if ( isset( $paramarray[$varname] ) ) {
00087 $$varname = $paramarray[$varname];
00088 }
00089 }
00090
00091 if ( isset( $fetch_fn ) ) {
00092 if ( ! in_array( $fetch_fn, $fns ) ) {
00093 $fetch_fn = $fns[0];
00094 }
00095 }
00096 else {
00097 $fetch_fn = $fns[0];
00098 }
00099
00100
00101 if ( isset( $count ) ) {
00102 $select = "COUNT($count)";
00103 $fetch_fn = 'get_value';
00104 $orderby = '';
00105 }
00106
00107 $single = false;
00108 if ( isset( $limit ) ) {
00109 $single = ($limit == 1);
00110 $limit = " LIMIT $limit";
00111 if ( isset( $offset ) ) {
00112 $limit .= " OFFSET $offset";
00113 }
00114 }
00115 if ( isset( $nolimit ) ) {
00116 $limit = '';
00117 }
00118
00119 $query = ' SELECT ' . $select . ' FROM {groups} ' . $join;
00120
00121 if ( count( $wheres ) > 0 ) {
00122 $query .= ' WHERE ' . implode( " \nOR\n ", $wheres );
00123 }
00124 $query .= ( ($orderby == '') ? '' : ' ORDER BY ' . $orderby ) . $limit;
00125
00126 DB::set_fetch_mode( \PDO::FETCH_CLASS );
00127
00128 $results = DB::$fetch_fn( $query, $params, 'UserGroup' );
00129
00130 if ( 'get_results' != $fetch_fn ) {
00131
00132 return $results;
00133 }
00134 elseif ( is_array( $results ) ) {
00135 $c = __CLASS__;
00136 $return_value = new $c( $results );
00137 $return_value->get_param_cache = $paramarray;
00138 return $return_value;
00139 }
00140 }
00141
00147 public static function get_all()
00148 {
00149
00150 $params = array(
00151 'orderby' => 'name ASC'
00152 );
00153
00154 return self::get( $params );
00155 }
00156
00157 }
00158 ?>