PageRenderTime 97ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/titania/includes/tools/count.php

https://github.com/bogtom/customisation-db
PHP | 212 lines | 126 code | 30 blank | 56 comment | 15 complexity | fe8d4038d465e14e81e46dc5270b2302 MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * @package Titania
  5. * @version $Id$
  6. * @copyright (c) 2008 phpBB Customisation Database Team
  7. * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
  8. *
  9. */
  10. /**
  11. * @ignore
  12. */
  13. if (!defined('IN_TITANIA'))
  14. {
  15. exit;
  16. }
  17. /**
  18. * Titania class to build and get the values for count fields stored in the DB
  19. */
  20. class titania_count
  21. {
  22. private static $fields = array(
  23. 'teams' => 0,
  24. 'authors' => 0,
  25. 'public' => 0,
  26. 'deleted' => 0,
  27. 'unapproved' => 0,
  28. );
  29. /**
  30. * Get the flags for the current viewing user to get the count
  31. *
  32. * @param int $access_level
  33. * @param bool $deleted Can or cannot view deleted items
  34. * @param bool $unapproved Can or cannot view unapproved
  35. */
  36. public static function get_flags($access_level, $deleted = false, $unapproved = false)
  37. {
  38. $flags = array();
  39. switch ($access_level)
  40. {
  41. case TITANIA_ACCESS_TEAMS :
  42. $flags[] = 'teams';
  43. case TITANIA_ACCESS_AUTHORS :
  44. $flags[] = 'authors';
  45. default :
  46. $flags[] = 'public';
  47. }
  48. if ($deleted)
  49. {
  50. $flags[] = 'deleted';
  51. }
  52. if ($unapproved)
  53. {
  54. $flags[] = 'unapproved';
  55. }
  56. return $flags;
  57. }
  58. /**
  59. * Get the flags for the update call (increment, decrement)
  60. *
  61. * @param int $access_level
  62. * @param bool $deleted Can or cannot view deleted items
  63. * @param bool $unapproved Can or cannot view unapproved
  64. */
  65. public static function update_flags($access_level, $deleted = false, $unapproved = false)
  66. {
  67. $flags = array();
  68. if ($deleted)
  69. {
  70. $flags[] = 'deleted';
  71. }
  72. else if ($unapproved)
  73. {
  74. $flags[] = 'unapproved';
  75. }
  76. else
  77. {
  78. switch ($access_level)
  79. {
  80. case TITANIA_ACCESS_TEAMS :
  81. $flags[] = 'teams';
  82. break;
  83. case TITANIA_ACCESS_AUTHORS :
  84. $flags[] = 'authors';
  85. break;
  86. default :
  87. $flags[] = 'public';
  88. break;
  89. }
  90. }
  91. return $flags;
  92. }
  93. /**
  94. * Get the count from the db field
  95. *
  96. * @param string $from_db The field from the database
  97. * @param array|bool $flags The flags to check for (get_flags function) or false for the whole array
  98. */
  99. public static function from_db($from_db, $flags)
  100. {
  101. self::reset_fields();
  102. $count = 0;
  103. $from_db = explode(':', $from_db);
  104. for ($i = 0; $i < sizeof($from_db) - 1; $i += 2)
  105. {
  106. $field_name = $from_db[$i];
  107. $field_value = $from_db[($i + 1)];
  108. self::$fields[$field_name] = $field_value;
  109. if (is_array($flags) && in_array($field_name, $flags))
  110. {
  111. $count += $field_value;
  112. }
  113. }
  114. return ($flags === false) ? self::$fields : $count;
  115. }
  116. /**
  117. * Increment one to the raw db field
  118. *
  119. * @param array $flags Should have the flag from update_flags()
  120. * @return string to_db()
  121. */
  122. public static function increment($from_db, $flags)
  123. {
  124. if (sizeof($flags) != 1)
  125. {
  126. throw new exception('Only increment one field at a time (you are using the field incorrectly if you increment more than one field per item)');
  127. }
  128. // Get the count array from the data
  129. $cnt_ary = self::from_db($from_db, false);
  130. // Increment the appropriate fields
  131. foreach ($flags as $flag)
  132. {
  133. $cnt_ary[$flag] = (!isset($cnt_ary[$flag])) ? 1 : ((int) $cnt_ary[$flag] + 1);
  134. }
  135. // Return to_data() version
  136. return self::to_db($cnt_ary);
  137. }
  138. /**
  139. * Decrement one to the raw db field
  140. *
  141. * @param array $flags Should have the flag from update_flags()
  142. * @return string to_db()
  143. */
  144. public static function decrement($from_db, $flags)
  145. {
  146. if (sizeof($flags) != 1)
  147. {
  148. throw new exception('Only decrement one field at a time (you are using the field incorrectly if you decrement more than one field per item)');
  149. }
  150. // Get the count array from the data
  151. $cnt_ary = self::from_db($from_db, false);
  152. // Decrement the appropriate fields
  153. foreach ($flags as $flag)
  154. {
  155. $cnt_ary[$flag] = (!isset($cnt_ary[$flag])) ? -1 : ((int) $cnt_ary[$flag] - 1);
  156. }
  157. // Return to_data() version
  158. return self::to_db($cnt_ary);
  159. }
  160. /**
  161. * Prepare the count to go to the db field
  162. */
  163. public static function to_db($data)
  164. {
  165. self::reset_fields();
  166. self::$fields = array_merge(self::$fields, $data);
  167. $to_db = array();
  168. foreach (self::$fields as $field_name => $field_value)
  169. {
  170. $to_db[] = $field_name . ':' . $field_value;
  171. }
  172. return implode(':', $to_db);
  173. }
  174. public static function reset_fields()
  175. {
  176. // Reset the fields
  177. foreach (self::$fields as $field => $value)
  178. {
  179. self::$fields[$field] = 0;
  180. }
  181. }
  182. }