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 Birthdays class |
15
|
|
|
*/ |
16
|
|
|
class birthdays 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.birthdays'; |
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_birthday'; |
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_BIRTHDAYS'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Get users to apply to this condition |
53
|
|
|
* Birthdays is typically called via cron with no $options arguments. |
54
|
|
|
* By default, get all users, otherwise use user_id(s) supplied in $options arg. |
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
|
|
|
* @throws \Exception |
59
|
|
|
* @access public |
60
|
|
|
*/ |
61
|
|
|
public function get_users_for_condition($options = array()) |
62
|
|
|
{ |
63
|
|
|
// The user data this condition needs to check |
64
|
|
|
$condition_data = array( |
65
|
|
|
$this->get_condition_field(), |
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
// Merge default options, empty user array as the default |
69
|
|
|
$options = array_merge(array( |
70
|
|
|
'users' => array(), |
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
|
|
|
// Get data for the users to be checked (exclude bots, guests and inactive users) |
77
|
|
|
$sql = 'SELECT user_id, ' . implode(', ', $condition_data) . ' |
78
|
|
|
FROM ' . USERS_TABLE . ' |
|
|
|
|
79
|
|
|
WHERE ' . $this->db->sql_in_set('user_type', $this->ignore_user_types(), true) . ' |
80
|
|
|
AND ' . $this->db->sql_in_set('user_id', $user_ids, !count($user_ids), true); |
81
|
|
|
$result = $this->db->sql_query($sql); |
82
|
|
|
|
83
|
|
|
$user_data = array(); |
84
|
|
|
while ($row = $this->db->sql_fetchrow($result)) |
85
|
|
|
{ |
86
|
|
|
$user_data[$row['user_id']] = array( |
87
|
|
|
$this->get_condition_field() => $this->get_user_age($row['user_birthday']) |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
$this->db->sql_freeresult($result); |
91
|
|
|
|
92
|
|
|
return $user_data; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Helper to get the users current age |
97
|
|
|
* |
98
|
|
|
* @param string $user_birthday The users birth date (e.g.: 20-10-1990) |
99
|
|
|
* @return int The users age in years |
100
|
|
|
* @throws \Exception |
101
|
|
|
*/ |
102
|
|
|
protected function get_user_age($user_birthday) |
103
|
|
|
{ |
104
|
|
|
static $now; |
105
|
|
|
|
106
|
|
|
if (!isset($now)) |
107
|
|
|
{ |
108
|
|
|
$now = new \DateTime('now'); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$age = 0; |
112
|
|
|
|
113
|
|
|
$birthday_year = (int) substr($user_birthday, -4); |
114
|
|
|
if ($birthday_year) |
115
|
|
|
{ |
116
|
|
|
try |
117
|
|
|
{ |
118
|
|
|
$birthday_datetime = new \DateTime(str_replace(' ', '', $user_birthday)); |
119
|
|
|
$diff = $birthday_datetime->diff($now); |
120
|
|
|
$age = (int) $diff->format('%y'); |
121
|
|
|
} |
122
|
|
|
catch (\Exception $e) |
123
|
|
|
{ |
124
|
|
|
// fail silently, like if user birthday is invalid datetime |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $age; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|