posts   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
eloc 30
dl 0
loc 110
rs 10
c 6
b 0
f 0
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A get_condition_type() 0 3 1
A get_condition_type_name() 0 3 1
A get_users_for_condition() 0 35 2
A get_condition_field() 0 3 1
A check() 0 21 3
1
<?php
2
/**
3
*
4
* Auto Groups extension for the phpBB Forum Software package.
5
*
6
* @copyright (c) 2014 phpBB Limited <https://www.phpbb.com>
7
* @license GNU General Public License, version 2 (GPL-2.0)
8
*
9
*/
10
11
namespace phpbb\autogroups\conditions\type;
12
13
/**
14
 * Auto Groups Posts class
15
 */
16
class posts extends \phpbb\autogroups\conditions\type\base
17
{
18
	/**
19
	 * Get condition type
20
	 *
21
	 * @return string Condition type
22
	 * @access public
23
	 */
24
	public function get_condition_type()
25
	{
26
		return 'phpbb.autogroups.type.posts';
27
	}
28
29
	/**
30
	 * Get condition field (this is the field to check)
31
	 *
32
	 * @return string Condition field name
33
	 * @access public
34
	 */
35
	public function get_condition_field()
36
	{
37
		return 'user_posts';
38
	}
39
40
	/**
41
	 * Get condition type name
42
	 *
43
	 * @return string Condition type name
44
	 * @access public
45
	 */
46
	public function get_condition_type_name()
47
	{
48
		return $this->language->lang('AUTOGROUPS_TYPE_POSTS');
49
	}
50
51
	/**
52
	 * Get users to apply to this condition
53
	 * Posts expects to receive user_id(s) or it will return empty,
54
	 * except during a 'sync' action which will return all users.
55
	 *
56
	 * @param array $options Array of optional data
57
	 * @return array Array of users ids as keys and their condition data as values
58
	 * @access public
59
	 */
60
	public function get_users_for_condition($options = array())
61
	{
62
		// The user data this condition needs to check
63
		$condition_data = array(
64
			$this->get_condition_field(),
65
		);
66
67
		// Merge default options, empty user array as the default
68
		$options = array_merge(array(
69
			'users'		=> array(),
70
			'action'	=> '',
71
		), $options);
72
73
		// Prepare the user ids data for use in the query
74
		$user_ids = $this->helper->prepare_users_for_query($options['users']);
75
76
		// Is this a sync action? If so, we want to get all users
77
		// by setting the $negate arg to true in sql_in_set for 1=1
78
		$sync = $options['action'] === 'sync';
79
80
		// Get data for the users to be checked (exclude bots and guests)
81
		$sql = 'SELECT user_id, ' . implode(', ', $condition_data) . '
82
			FROM ' . USERS_TABLE . '
0 ignored issues
show
Bug introduced by
The constant phpbb\autogroups\conditions\type\USERS_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
83
			WHERE ' . $this->db->sql_in_set('user_id', $user_ids, $sync, true) . '
84
				AND user_type <> ' . USER_IGNORE;
0 ignored issues
show
Bug introduced by
The constant phpbb\autogroups\conditions\type\USER_IGNORE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
85
		$result = $this->db->sql_query($sql);
86
87
		$user_data = array();
88
		while ($row = $this->db->sql_fetchrow($result))
89
		{
90
			$user_data[$row['user_id']] = $row;
91
		}
92
		$this->db->sql_freeresult($result);
93
94
		return $user_data;
95
	}
96
97
	/**
98
	 * Check condition
99
	 *
100
	 * @param array $user_row Array of user data to perform checks on
101
	 * @param array $options  Array of optional data
102
	 * @return void
103
	 * @access public
104
	 */
105
	public function check($user_row, $options = array())
106
	{
107
		// Merge default options
108
		$options = array_merge(array(
109
			'action'	=> '',
110
		), $options);
111
112
		// We need to decrease the user's post count during post deletion
113
		// because the database does not yet have updated post counts.
114
		if ($options['action'] === 'delete')
115
		{
116
			foreach ($user_row as &$user_data)
117
			{
118
				$user_data['user_posts']--;
119
			}
120
121
			// Always unset a variable passed by reference in a foreach loop
122
			unset($user_data);
123
		}
124
125
		parent::check($user_row, $options);
126
	}
127
}
128