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