/classes/usergroups.php

https://github.com/itnaegele/system · PHP · 156 lines · 108 code · 19 blank · 29 comment · 24 complexity · d377feeb90d9f8b6138fbf84ed1b6839 MD5 · raw file

  1. <?php
  2. /**
  3. * @package Habari
  4. *
  5. */
  6. /**
  7. * Habari UserGroups Class
  8. *
  9. */
  10. class UserGroups extends ArrayObject
  11. {
  12. protected $get_param_cache; // Stores info about the last set of data fetched that was not a single value
  13. /**
  14. * Returns a group or grops based on supplied parameters.
  15. * @todo This class should cache query results!
  16. *
  17. * @param array $paramarray An associated array of parameters, or a querystring
  18. * @return array An array of UserGroup objects, or a single UserGroup object, depending on request
  19. */
  20. public static function get( $paramarray = array() )
  21. {
  22. $params = array();
  23. $fns = array( 'get_results', 'get_row', 'get_value' );
  24. $select = '';
  25. // what to select -- by default, everything
  26. foreach ( UserGroup::default_fields() as $field => $value ) {
  27. $select .= ( '' == $select )
  28. ? "{groups}.$field"
  29. : ", {groups}.$field";
  30. }
  31. // defaults
  32. $orderby = 'id ASC';
  33. $nolimit = true;
  34. // Put incoming parameters into the local scope
  35. $paramarray = Utils::get_params( $paramarray );
  36. // Transact on possible multiple sets of where information that is to be OR'ed
  37. if ( isset( $paramarray['where'] ) && is_array( $paramarray['where'] ) ) {
  38. $wheresets = $paramarray['where'];
  39. }
  40. else {
  41. $wheresets = array( array() );
  42. }
  43. $wheres = array();
  44. $join = '';
  45. if ( isset( $paramarray['where'] ) && is_string( $paramarray['where'] ) ) {
  46. $wheres[] = $paramarray['where'];
  47. }
  48. else {
  49. foreach ( $wheresets as $paramset ) {
  50. // safety mechanism to prevent empty queries
  51. $where = array();
  52. $paramset = array_merge( (array) $paramarray, (array) $paramset );
  53. $default_fields = UserGroup::default_fields();
  54. foreach ( UserGroup::default_fields() as $field => $scrap ) {
  55. if ( !isset( $paramset[$field] ) ) {
  56. continue;
  57. }
  58. switch ( $field ) {
  59. case 'id':
  60. if ( !is_numeric( $paramset[$field] ) ) {
  61. continue;
  62. }
  63. default:
  64. $where[] = "{$field} = ?";
  65. $params[] = $paramset[$field];
  66. }
  67. }
  68. if ( count( $where ) > 0 ) {
  69. $wheres[] = ' (' . implode( ' AND ', $where ) . ') ';
  70. }
  71. }
  72. }
  73. // Get any full-query parameters
  74. $possible = array( 'fetch_fn', 'count', 'nolimit', 'limit', 'offset' );
  75. foreach ( $possible as $varname ) {
  76. if ( isset( $paramarray[$varname] ) ) {
  77. $$varname = $paramarray[$varname];
  78. }
  79. }
  80. if ( isset( $fetch_fn ) ) {
  81. if ( ! in_array( $fetch_fn, $fns ) ) {
  82. $fetch_fn = $fns[0];
  83. }
  84. }
  85. else {
  86. $fetch_fn = $fns[0];
  87. }
  88. // is a count being request?
  89. if ( isset( $count ) ) {
  90. $select = "COUNT($count)";
  91. $fetch_fn = 'get_value';
  92. $orderby = '';
  93. }
  94. // Are we asking for a single UserGroup or possibly multiple UserGroups
  95. $single = false;
  96. if ( isset( $limit ) ) {
  97. $single = ($limit == 1);
  98. $limit = " LIMIT $limit";
  99. if ( isset( $offset ) ) {
  100. $limit .= " OFFSET $offset";
  101. }
  102. }
  103. if ( isset( $nolimit ) ) {
  104. $limit = '';
  105. }
  106. $query = ' SELECT ' . $select . ' FROM {groups} ' . $join;
  107. if ( count( $wheres ) > 0 ) {
  108. $query .= ' WHERE ' . implode( " \nOR\n ", $wheres );
  109. }
  110. $query .= ( ($orderby == '') ? '' : ' ORDER BY ' . $orderby ) . $limit;
  111. DB::set_fetch_mode( PDO::FETCH_CLASS );
  112. $results = DB::$fetch_fn( $query, $params, 'UserGroup' );
  113. if ( 'get_results' != $fetch_fn ) {
  114. // return the results
  115. return $results;
  116. }
  117. elseif ( is_array( $results ) ) {
  118. $c = __CLASS__;
  119. $return_value = new $c( $results );
  120. $return_value->get_param_cache = $paramarray;
  121. return $return_value;
  122. }
  123. }
  124. /**
  125. * Select all groups from the database
  126. *
  127. * @return Groups
  128. */
  129. public static function get_all()
  130. {
  131. $params = array(
  132. 'orderby' => 'name ASC'
  133. );
  134. return self::get( $params );
  135. }
  136. }
  137. ?>