PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/count.php

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