Verzeichnisstruktur phpBB-3.1.0
- Veröffentlicht
- 27.10.2014
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
legend.php
001 <?php
002 /**
003 *
004 * This file is part of the phpBB Forum Software package.
005 *
006 * @copyright (c) phpBB Limited <https://www.phpbb.com>
007 * @license GNU General Public License, version 2 (GPL-2.0)
008 *
009 * For full copyright and license information, please see
010 * the docs/CREDITS.txt file.
011 *
012 */
013
014 namespace phpbb\groupposition;
015
016 /**
017 * Legend group position class
018 *
019 * group_legend is an ascending list 1, 2, ..., n for groups which are displayed. 1 is the first group, n the last.
020 * If the value is 0 (self::GROUP_DISABLED) the group is not displayed.
021 */
022 class legend implements \phpbb\groupposition\groupposition_interface
023 {
024 /**
025 * Group is not displayed
026 */
027 const GROUP_DISABLED = 0;
028
029 /**
030 * Database object
031 * @var \phpbb\db\driver\driver_interface
032 */
033 protected $db;
034
035 /**
036 * User object
037 * @var \phpbb\user
038 */
039 protected $user;
040
041 /**
042 * Constructor
043 *
044 * @param \phpbb\db\driver\driver_interface $db Database object
045 * @param \phpbb\user $user User object
046 */
047 public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\user $user)
048 {
049 $this->db = $db;
050 $this->user = $user;
051 }
052
053 /**
054 * Returns the group_legend for a given group, if the group exists.
055 *
056 * @param int $group_id group_id of the group to be selected
057 * @return int position of the group
058 * @throws \phpbb\groupposition\exception
059 */
060 public function get_group_value($group_id)
061 {
062 $sql = 'SELECT group_legend
063 FROM ' . GROUPS_TABLE . '
064 WHERE group_id = ' . (int) $group_id;
065 $result = $this->db->sql_query($sql);
066 $current_value = $this->db->sql_fetchfield('group_legend');
067 $this->db->sql_freeresult($result);
068
069 if ($current_value === false)
070 {
071 // Group not found.
072 throw new \phpbb\groupposition\exception('NO_GROUP');
073 }
074
075 return (int) $current_value;
076 }
077
078 /**
079 * Get number of groups, displayed on the legend
080 *
081 * @return int value of the last item displayed
082 */
083 public function get_group_count()
084 {
085 $sql = 'SELECT group_legend
086 FROM ' . GROUPS_TABLE . '
087 ORDER BY group_legend DESC';
088 $result = $this->db->sql_query_limit($sql, 1);
089 $group_count = (int) $this->db->sql_fetchfield('group_legend');
090 $this->db->sql_freeresult($result);
091
092 return $group_count;
093 }
094
095 /**
096 * {@inheritDoc}
097 */
098 public function add_group($group_id)
099 {
100 $current_value = $this->get_group_value($group_id);
101
102 if ($current_value == self::GROUP_DISABLED)
103 {
104 // Group is currently not displayed, add it at the end.
105 $next_value = 1 + $this->get_group_count();
106
107 $sql = 'UPDATE ' . GROUPS_TABLE . '
108 SET group_legend = ' . $next_value . '
109 WHERE group_legend = ' . self::GROUP_DISABLED . '
110 AND group_id = ' . (int) $group_id;
111 $this->db->sql_query($sql);
112 return true;
113 }
114
115 return false;
116 }
117
118 /**
119 * Deletes a group by setting the field to self::GROUP_DISABLED and closing the gap in the list.
120 *
121 * @param int $group_id group_id of the group to be deleted
122 * @param bool $skip_group Skip setting the value for this group, to save the query, when you need to update it anyway.
123 * @return bool True if the group was deleted successfully
124 */
125 public function delete_group($group_id, $skip_group = false)
126 {
127 $current_value = $this->get_group_value($group_id);
128
129 if ($current_value != self::GROUP_DISABLED)
130 {
131 $this->db->sql_transaction('begin');
132
133 $sql = 'UPDATE ' . GROUPS_TABLE . '
134 SET group_legend = group_legend - 1
135 WHERE group_legend > ' . $current_value;
136 $this->db->sql_query($sql);
137
138 if (!$skip_group)
139 {
140 $sql = 'UPDATE ' . GROUPS_TABLE . '
141 SET group_legend = ' . self::GROUP_DISABLED . '
142 WHERE group_id = ' . (int) $group_id;
143 $this->db->sql_query($sql);
144 }
145
146 $this->db->sql_transaction('commit');
147
148 return true;
149 }
150
151 return false;
152 }
153
154 /**
155 * {@inheritDoc}
156 */
157 public function move_up($group_id)
158 {
159 return $this->move($group_id, 1);
160 }
161
162 /**
163 * {@inheritDoc}
164 */
165 public function move_down($group_id)
166 {
167 return $this->move($group_id, -1);
168 }
169
170 /**
171 * {@inheritDoc}
172 */
173 public function move($group_id, $delta)
174 {
175 $delta = (int) $delta;
176 if (!$delta)
177 {
178 return false;
179 }
180
181 $move_up = ($delta > 0) ? true : false;
182 $current_value = $this->get_group_value($group_id);
183
184 if ($current_value != self::GROUP_DISABLED)
185 {
186 $this->db->sql_transaction('begin');
187
188 // First we move all groups between our current value and the target value up/down 1,
189 // so we have a gap for our group to move.
190 $sql = 'UPDATE ' . GROUPS_TABLE . '
191 SET group_legend = group_legend' . (($move_up) ? ' + 1' : ' - 1') . '
192 WHERE group_legend > ' . self::GROUP_DISABLED . '
193 AND group_legend' . (($move_up) ? ' >= ' : ' <= ') . ($current_value - $delta) . '
194 AND group_legend' . (($move_up) ? ' < ' : ' > ') . $current_value;
195 $this->db->sql_query($sql);
196
197 // Because there might be fewer groups above/below the group than we wanted to move,
198 // we use the number of changed groups, to update the group.
199 $delta = (int) $this->db->sql_affectedrows();
200
201 if ($delta)
202 {
203 // And now finally, when we moved some other groups and built a gap,
204 // we can move the desired group to it.
205 $sql = 'UPDATE ' . GROUPS_TABLE . '
206 SET group_legend = group_legend ' . (($move_up) ? ' - ' : ' + ') . $delta . '
207 WHERE group_id = ' . (int) $group_id;
208 $this->db->sql_query($sql);
209
210 $this->db->sql_transaction('commit');
211
212 return true;
213 }
214
215 $this->db->sql_transaction('commit');
216 }
217
218 return false;
219 }
220
221 /**
222 * Get group type language var
223 *
224 * @param int $group_type group_type from the groups-table
225 * @return string name of the language variable for the given group-type.
226 */
227 static public function group_type_language($group_type)
228 {
229 switch ($group_type)
230 {
231 case GROUP_OPEN:
232 return 'GROUP_REQUEST';
233 case GROUP_CLOSED:
234 return 'GROUP_CLOSED';
235 case GROUP_HIDDEN:
236 return 'GROUP_HIDDEN';
237 case GROUP_SPECIAL:
238 return 'GROUP_SPECIAL';
239 case GROUP_FREE:
240 return 'GROUP_OPEN';
241 }
242 }
243 }
244