aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/phpbb/groupposition/teampage.php
blob: 4cd2ae7fa8aaac0c3a2705c6ce8f32f6715df56a (plain)
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
<?php
/**
*
* @package phpBB3
* @copyright (c) 2012 phpBB Group
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/

namespace phpbb\groupposition;

/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
	exit;
}

/**
* Teampage group position class
*
* Teampage position is an ascending list 1, 2, ..., n for items which are displayed. 1 is the first item, n the last.
*
* @package phpBB3
*/
class teampage implements \phpbb\groupposition\groupposition_interface
{
	/**
	* Group is not displayed
	*/
	const GROUP_DISABLED = 0;

	/**
	* No parent item
	*/
	const NO_PARENT = 0;

	/**
	* Database object
	* @var \phpbb\db\driver\driver
	*/
	protected $db;

	/**
	* User object
	* @var \phpbb\user
	*/
	protected $user;

	/**
	* Cache object
	* @var \phpbb\cache\driver\driver_interface
	*/
	protected $cache;

	/**
	* Constructor
	*
	* @param \phpbb\db\driver\driver				$db		Database object
	* @param \phpbb\user						$user	User object
	* @param \phpbb\cache\driver\driver_interface	$cache	Cache object
	*/
	public function __construct(\phpbb\db\driver\driver $db, \phpbb\user $user, \phpbb\cache\driver\driver_interface $cache)
	{
		$this->db = $db;
		$this->user = $user;
		$this->cache = $cache;
	}

	/**
	* Returns the teampage position for a given group, if the group exists.
	*
	* {@inheritDoc}
	*/
	public function get_group_value($group_id)
	{
		// The join is required to ensure that the group itself exists
		$sql = 'SELECT g.group_id, t.teampage_position
			FROM ' . GROUPS_TABLE . ' g
			LEFT JOIN ' . TEAMPAGE_TABLE . ' t
				ON (t.group_id = g.group_id)
			WHERE g.group_id = ' . (int) $group_id;
		$result = $this->db->sql_query($sql);
		$row = $this->db->sql_fetchrow($result);
		$this->db->sql_freeresult($result);

		if ($row === false)
		{
			// Group not found.
			throw new \phpbb\groupposition\exception('NO_GROUP');
		}

		return (int) $row['teampage_position'];
	}

	/**
	* Returns the row for a given group, if the group exists.
	*
	* @param	int		$group_id	group_id of the group to be selected
	* @return	array			Data row of the group
	*/
	public function get_group_values($group_id)
	{
		// The join is required to ensure that the group itself exists
		$sql = 'SELECT *
			FROM ' . GROUPS_TABLE . ' g
			LEFT JOIN ' . TEAMPAGE_TABLE . ' t
				ON (t.group_id = g.group_id)
			WHERE g.group_id = ' . (int) $group_id;
		$result = $this->db->sql_query($sql);
		$row = $this->db->sql_fetchrow($result);
		$this->db->sql_freeresult($result);

		if ($row === false)
		{
			// Group not found.
			throw new \phpbb\groupposition\exception('NO_GROUP');
		}

		return $row;
	}

	/**
	* Returns the teampage position for a given teampage item, if the item exists.
	*
	* @param	int		$teampage_id	Teampage_id of the selected item
	* @return	int			Teampage position of the item
	*/
	public function get_teampage_value($teampage_id)
	{
		$sql = 'SELECT teampage_position
			FROM ' . TEAMPAGE_TABLE . '
			WHERE teampage_id = ' . (int) $teampage_id;
		$result = $this->db->sql_query($sql);
		$current_value = $this->db->sql_fetchfield('teampage_position');
		$this->db->sql_freeresult($result);

		if ($current_value === false)
		{
			// Group not found.
			throw new \phpbb\groupposition\exception('NO_GROUP');
		}

		return (int) $current_value;
	}

	/**
	* Returns the teampage row for a given teampage item, if the item exists.
	*
	* @param	int		$teampage_id	Teampage_id of the selected item
	* @return	array			Teampage row of the item
	*/
	public function get_teampage_values($teampage_id)
	{
		$sql = 'SELECT teampage_position, teampage_parent
			FROM ' . TEAMPAGE_TABLE . '
			WHERE teampage_id = ' . (int) $teampage_id;
		$result = $this->db->sql_query($sql);
		$row = $this->db->sql_fetchrow($result);
		$this->db->sql_freeresult($result);

		if ($row === false)
		{
			// Group not found.
			throw new \phpbb\groupposition\exception('NO_GROUP');
		}

		return $row;
	}


	/**
	* Get number of items displayed
	*
	* {@inheritDoc}
	*/
	public function get_group_count()
	{
		$sql = 'SELECT teampage_position
			FROM ' . TEAMPAGE_TABLE . '
			ORDER BY teampage_position DESC';
		$result = $this->db->sql_query_limit($sql, 1);
		$group_count = (int) $this->db->sql_fetchfield('teampage_position');
		$this->db->sql_freeresult($result);

		return $group_count;
	}

	/**
	* Adds a group by group_id
	*
	* {@inheritDoc}
	*/
	public function add_group($group_id)
	{
		return $this->add_group_teampage($group_id, self::NO_PARENT);
	}

	/**
	* Adds a group by group_id
	*
	* @param	int		$group_id	group_id of the group to be added
	* @param	int		$parent_id	Teampage ID of the parent item
	* @return	bool		True if the group was added successfully
	*/
	public function add_group_teampage($group_id, $parent_id)
	{
		$current_value = $this->get_group_value($group_id);

		if ($current_value == self::GROUP_DISABLED)
		{
			if ($parent_id != self::NO_PARENT)
			{
				// Check, whether the given parent is a category
				$sql = 'SELECT teampage_id
					FROM ' . TEAMPAGE_TABLE . '
					WHERE group_id = 0
						AND teampage_id = ' . (int) $parent_id;
				$result = $this->db->sql_query_limit($sql, 1);
				$parent_is_category = (bool) $this->db->sql_fetchfield('teampage_id');
				$this->db->sql_freeresult($result);

				if ($parent_is_category)
				{
					// Get value of last child from this parent and add group there
					$sql = 'SELECT teampage_position
						FROM ' . TEAMPAGE_TABLE . '
						WHERE teampage_parent = ' . (int) $parent_id . '
							OR teampage_id = ' . (int) $parent_id . '
						ORDER BY teampage_position DESC';
					$result = $this->db->sql_query_limit($sql, 1);
					$new_position = (int) $this->db->sql_fetchfield('teampage_position');
					$this->db->sql_freeresult($result);

					$sql = 'UPDATE ' . TEAMPAGE_TABLE . '
						SET teampage_position = teampage_position + 1
						WHERE teampage_position > ' . $new_position;
					$this->db->sql_query($sql);
				}
			}
			else
			{
				// Add group at the end
				$new_position = $this->get_group_count();
			}

			$sql_ary = array(
				'group_id'			=> $group_id,
				'teampage_position'	=> $new_position + 1,
				'teampage_parent'	=> $parent_id,
			);

			$sql = 'INSERT INTO ' . TEAMPAGE_TABLE . ' ' . $this->db->sql_build_array('INSERT', $sql_ary);
			$this->db->sql_query($sql);

			$this->cache->destroy('sql', TEAMPAGE_TABLE);
			return true;
		}

		$this->cache->destroy('sql', TEAMPAGE_TABLE);
		return false;
	}

	/**
	* Adds a new \category
	*
	* @param	string	$category_name	Name of the category to be added
	* @return	bool		True if the category was added successfully
	*/
	public function add_category_teampage($category_name)
	{
		if ($category_name === '')
		{
			return false;
		}

		$num_entries = $this->get_group_count();

		$sql_ary = array(
			'group_id'			=> 0,
			'teampage_position'	=> $num_entries + 1,
			'teampage_parent'	=> 0,
			'teampage_name'		=> truncate_string($category_name, 255, 255),
		);

		$sql = 'INSERT INTO ' . TEAMPAGE_TABLE . ' ' . $this->db->sql_build_array('INSERT', $sql_ary);
		$this->db->sql_query($sql);

		$this->cache->destroy('sql', TEAMPAGE_TABLE);
		return true;
	}

	/**
	* Deletes a group from the list and closes the gap in the position list.
	*
	* {@inheritDoc}
	*/
	public function delete_group($group_id, $skip_group = false)
	{
		$current_value = $this->get_group_value($group_id);

		if ($current_value != self::GROUP_DISABLED)
		{
			$sql = 'UPDATE ' . TEAMPAGE_TABLE . '
				SET teampage_position = teampage_position - 1
				WHERE teampage_position > ' . $current_value;
			$this->db->sql_query($sql);

			$sql = 'DELETE FROM ' . TEAMPAGE_TABLE . '
				WHERE group_id = ' . $group_id;
			$this->db->sql_query($sql);

			$this->cache->destroy('sql', TEAMPAGE_TABLE);
			return true;
		}

		$this->cache->destroy('sql', TEAMPAGE_TABLE);
		return false;
	}

	/**
	* Deletes an item from the list and closes the gap in the position list.
	*
	* @param	int		$teampage_id	teampage_id of the item to be deleted
	* @param	bool	$skip_group		Skip setting the group to GROUP_DISABLED, to save the query, when you need to update it anyway.
	* @return	bool		True if the item was deleted successfully
	*/
	public function delete_teampage($teampage_id, $skip_group = false)
	{
		$current_value = $this->get_teampage_value($teampage_id);

		if ($current_value != self::GROUP_DISABLED)
		{
			$sql = 'DELETE FROM ' . TEAMPAGE_TABLE . '
				WHERE teampage_id = ' . $teampage_id . '
					OR teampage_parent = ' . $teampage_id;
			$this->db->sql_query($sql);

			$delta = (int) $this->db->sql_affectedrows();

			$sql = 'UPDATE ' . TEAMPAGE_TABLE . '
				SET teampage_position = teampage_position - ' . $delta . '
				WHERE teampage_position > ' . $current_value;
			$this->db->sql_query($sql);

			$this->cache->destroy('sql', TEAMPAGE_TABLE);
			return true;
		}

		$this->cache->destroy('sql', TEAMPAGE_TABLE);
		return false;
	}

	/**
	* Moves a group up by group_id
	*
	* {@inheritDoc}
	*/
	public function move_up($group_id)
	{
		return $this->move($group_id, 1);
	}

	/**
	* Moves an item up by teampage_id
	*
	* @param	int		$group_id	group_id of the group to be moved
	* @return	bool		True if the group was moved successfully
	*/
	public function move_up_teampage($teampage_id)
	{
		return $this->move_teampage($teampage_id, 1);
	}

	/**
	* Moves a group down by group_id
	*
	* {@inheritDoc}
	*/
	public function move_down($group_id)
	{
		return $this->move($group_id, -1);
	}

	/**
	* Movesan item down by teampage_id
	*
	* @param	int		$group_id	group_id of the group to be moved
	* @return	bool		True if the group was moved successfully
	*/
	public function move_down_teampage($teampage_id)
	{
		return $this->move_teampage($teampage_id, -1);
	}

	/**
	* Moves a group up/down
	*
	* {@inheritDoc}
	*/
	public function move($group_id, $delta)
	{
		$delta = (int) $delta;
		if (!$delta)
		{
			return false;
		}

		$move_up = ($delta > 0) ? true : false;
		$data = $this->get_group_values($group_id);

		$current_value = (int) $data['teampage_position'];
		if ($current_value != self::GROUP_DISABLED)
		{
			$this->db->sql_transaction('begin');

			if (!$move_up && $data['teampage_parent'] == self::NO_PARENT)
			{
				// If we move items down, we need to grab the one sibling more,
				// so we do not ignore the children of the previous sibling.
				// We will remove the additional sibling later on.
				$delta = abs($delta) + 1;
			}

			$sql = 'SELECT teampage_position
				FROM ' . TEAMPAGE_TABLE . '
				WHERE teampage_parent = ' . (int) $data['teampage_parent'] . '
					AND teampage_position' . (($move_up) ? ' < ' : ' > ') . $current_value . '
				ORDER BY teampage_position' . (($move_up) ? ' DESC' : ' ASC');
			$result = $this->db->sql_query_limit($sql, $delta);

			$sibling_count = 0;
			$sibling_limit = $delta;

			// Reset the delta, as we recalculate the new \real delta
			$delta = 0;
			while ($row = $this->db->sql_fetchrow($result))
			{
				$sibling_count++;
				$delta = $current_value - $row['teampage_position'];

				if (!$move_up && $data['teampage_parent'] == self::NO_PARENT && $sibling_count == $sibling_limit)
				{
					// Remove the additional sibling we added previously
					$delta++;
				}
			}
			$this->db->sql_freeresult($result);

			if ($delta)
			{
				// First we move all items between our current value and the target value up/down 1,
				// so we have a gap for our item to move.
				$sql = 'UPDATE ' . TEAMPAGE_TABLE . '
					SET teampage_position = teampage_position' . (($move_up) ? ' + 1' : ' - 1') . '
					WHERE teampage_position' . (($move_up) ? ' >= ' : ' <= ') . ($current_value - $delta) . '
						AND teampage_position' . (($move_up) ? ' < ' : ' > ') . $current_value;
				$this->db->sql_query($sql);

				// And now finally, when we moved some other items and built a gap,
				// we can move the desired item to it.
				$sql = 'UPDATE ' . TEAMPAGE_TABLE . '
					SET teampage_position = teampage_position ' . (($move_up) ? ' - ' : ' + ') . abs($delta) . '
					WHERE group_id = ' . (int) $group_id;
				$this->db->sql_query($sql);

				$this->db->sql_transaction('commit');
				$this->cache->destroy('sql', TEAMPAGE_TABLE);

				return true;
			}

			$this->db->sql_transaction('commit');
		}

		$this->cache->destroy('sql', TEAMPAGE_TABLE);
		return false;
	}

	/**
	* Moves an item up/down
	*
	* @param	int		$teampage_id	teampage_id of the item to be moved
	* @param	int		$delta		number of steps:
	*								- positive = move up
	*								- negative = move down
	* @return	bool		True if the group was moved successfully
	*/
	public function move_teampage($teampage_id, $delta)
	{
		$delta = (int) $delta;
		if (!$delta)
		{
			return false;
		}

		$move_up = ($delta > 0) ? true : false;
		$data = $this->get_teampage_values($teampage_id);

		$current_value = (int) $data['teampage_position'];
		if ($current_value != self::GROUP_DISABLED)
		{
			$this->db->sql_transaction('begin');

			if (!$move_up && $data['teampage_parent'] == self::NO_PARENT)
			{
				// If we move items down, we need to grab the one sibling more,
				// so we do not ignore the children of the previous sibling.
				// We will remove the additional sibling later on.
				$delta = abs($delta) + 1;
			}

			$sql = 'SELECT teampage_id, teampage_position
				FROM ' . TEAMPAGE_TABLE . '
				WHERE teampage_parent = ' . (int) $data['teampage_parent'] . '
					AND teampage_position' . (($move_up) ? ' < ' : ' > ') . $current_value . '
				ORDER BY teampage_position' . (($move_up) ? ' DESC' : ' ASC');
			$result = $this->db->sql_query_limit($sql, $delta);

			$sibling_count = 0;
			$sibling_limit = $delta;

			// Reset the delta, as we recalculate the new \real delta
			$delta = 0;
			while ($row = $this->db->sql_fetchrow($result))
			{
				$sibling_count++;
				$delta = $current_value - $row['teampage_position'];

				// Remove the additional sibling we added previously
				// But only, if we included it, this is not be the case
				// when we reached the end of our list
				if (!$move_up && $data['teampage_parent'] == self::NO_PARENT && $sibling_count == $sibling_limit)
				{
					$delta++;
				}
			}
			$this->db->sql_freeresult($result);

			if ($delta)
			{
				$sql = 'SELECT COUNT(teampage_id) as num_items
					FROM ' . TEAMPAGE_TABLE . '
					WHERE teampage_id = ' . (int) $teampage_id . '
						OR teampage_parent = ' . (int) $teampage_id;
				$result = $this->db->sql_query($sql);
				$num_items = (int) $this->db->sql_fetchfield('num_items');
				$this->db->sql_freeresult($result);

				// First we move all items between our current value and the target value up/down 1,
				// so we have a gap for our item to move.
				$sql = 'UPDATE ' . TEAMPAGE_TABLE . '
					SET teampage_position = teampage_position' . (($move_up) ? ' + ' : ' - ') . $num_items . '
					WHERE teampage_position' . (($move_up) ? ' >= ' : ' <= ') . ($current_value - $delta) . '
						AND teampage_position' . (($move_up) ? ' < ' : ' > ') . $current_value . '
						AND NOT (teampage_id = ' . (int) $teampage_id . '
							OR teampage_parent = ' . (int) $teampage_id . ')';
				$this->db->sql_query($sql);

				$delta = (!$move_up && $data['teampage_parent'] == self::NO_PARENT) ? (abs($delta) - ($num_items - 1)) : abs($delta);

				// And now finally, when we moved some other items and built a gap,
				// we can move the desired item to it.
				$sql = 'UPDATE ' . TEAMPAGE_TABLE . '
					SET teampage_position = teampage_position ' . (($move_up) ? ' - ' : ' + ') . $delta . '
					WHERE teampage_id = ' . (int) $teampage_id . '
						OR teampage_parent = ' . (int) $teampage_id;
				$this->db->sql_query($sql);

				$this->db->sql_transaction('commit');
				$this->cache->destroy('sql', TEAMPAGE_TABLE);

				return true;
			}

			$this->db->sql_transaction('commit');
		}

		$this->cache->destroy('sql', TEAMPAGE_TABLE);
		return false;
	}

	/**
	* Get group type language var
	*
	* @param	int		$group_type	group_type from the groups-table
	* @return	string		name of the language variable for the given group-type.
	*/
	static public function group_type_language($group_type)
	{
		switch ($group_type)
		{
			case GROUP_OPEN:
				return 'GROUP_REQUEST';
			case GROUP_CLOSED:
				return 'GROUP_CLOSED';
			case GROUP_HIDDEN:
				return 'GROUP_HIDDEN';
			case GROUP_SPECIAL:
				return 'GROUP_SPECIAL';
			case GROUP_FREE:
				return 'GROUP_OPEN';
		}
	}
}