forked from ForumHulp/emailonbirthday
/
ext.php
177 lines (149 loc) · 5.79 KB
/
ext.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
<?php
/**
*
* @package E-mail on birthday
* @copyright (c) 2015 ForumHulp.com
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
namespace forumhulp\emailonbirthday;
/**
* This ext class is optional and can be omitted if left empty.
* However you can add special (un)installation commands in the
* methods enable_step(), disable_step() and purge_step(). As it is,
* these methods are defined in \phpbb\extension\base, which this
* class extends, but you can overwrite them to give special
* instructions for those cases.
*/
class ext extends \phpbb\extension\base
{
public function is_enableable()
{
if (!class_exists('forumhulp\helper\helper'))
{
$this->container->get('user')->add_lang_ext('forumhulp/emailonbirthday', 'info_acp_emailonbirthday');
trigger_error($this->container->get('user')->lang['FH_HELPER_NOTICE'], E_USER_WARNING);
}
if (!$this->container->get('ext.manager')->is_enabled('forumhulp/helper'))
{
$this->container->get('ext.manager')->enable('forumhulp/helper');
}
return class_exists('forumhulp\helper\helper');
}
public function enable_step($old_state)
{
switch ($old_state)
{
case '': // Empty means nothing has run yet
if (empty($old_state))
{
$this->container->get('user')->add_lang_ext('forumhulp/emailonbirthday', 'info_acp_emailonbirthday');
$this->container->get('template')->assign_var('L_EXTENSION_ENABLE_SUCCESS', $this->container->get('user')->lang['EXTENSION_ENABLE_SUCCESS'] .
(isset($this->container->get('user')->lang['E_MAIL_ON_BIRTHDAY_NOTICE']) ?
sprintf($this->container->get('user')->lang['E_MAIL_ON_BIRTHDAY_NOTICE'],
$this->container->get('user')->lang['ACP_CAT_GENERAL'],
$this->container->get('user')->lang['ACP_BOARD_CONFIGURATION'],
$this->container->get('user')->lang['ACP_BOARD_FEATURES']) : ''));
}
// Enable birthday system notifications
$config = $this->container->get('config');
$path = $this->container->get('ext.manager')->get_extension_path('forumhulp/emailonbirthday', true);
if (version_compare($config['version'], '3.2.*', '<'))
{
copy($path . 'config/notifications31.yml', $path . 'config/notifications.yml');
copy($path . 'notification/type/birthday31.php', $path . 'notification/type/birthday.php');
} else
{
copy($path . 'config/notifications32.yml', $path . 'config/notifications.yml');
copy($path . 'notification/type/birthday32.php', $path . 'notification/type/birthday.php');
}
$phpbb_notifications = $this->container->get('notification_manager');
$phpbb_notifications->enable_notifications('forumhulp.emailonbirthday.notification.type.birthday');
$sql = 'SELECT COUNT(item_type) AS total
FROM ' . $this->container->getParameter('tables.user_notifications') . "
WHERE item_type = '" . $this->container->get('dbal.conn')->sql_escape('forumhulp.emailonbirthday.notification.type.birthday') . "'";
$this->container->get('dbal.conn')->sql_query($sql);
$total = $this->container->get('dbal.conn')->sql_fetchfield('total');
if (!$total)
{
$insert_buffer = new \phpbb\db\sql_insert_buffer($this->container->get('dbal.conn'), $this->container->getParameter('tables.user_notifications'));
$method = (version_compare($config['version'], '3.2.*', '<')) ? '' : 'notification.method.board';
$sql = 'SELECT user_id FROM ' . USERS_TABLE . ' WHERE ' . $this->container->get('dbal.conn')->sql_in_set('user_type', array(USER_INACTIVE, USER_IGNORE), true);
$result = $this->container->get('dbal.conn')->sql_query($sql);
while ($row = $this->container->get('dbal.conn')->sql_fetchrow($result))
{
$insert_buffer->insert(array(
'item_type' => 'forumhulp.emailonbirthday.notification.type.birthday',
'item_id' => 0,
'user_id' => $row['user_id'],
'method' => $method,
'notify' => 1)
);
$insert_buffer->insert(array(
'item_type' => 'forumhulp.emailonbirthday.notification.type.birthday',
'item_id' => 0,
'user_id' => $row['user_id'],
'method' => 'notification.method.email',
'notify' => 1)
);
}
// Flush the buffer
$insert_buffer->flush();
}
return 'notifications';
break;
default:
// Run parent enable step method
return parent::enable_step($old_state);
break;
}
}
/**
* Overwrite disable_step to disable notifications
* before the extension is disabled.
*
* @param mixed $old_state State returned by previous call of this method
* @return mixed Returns false after last step, otherwise temporary state
* @access public
*/
public function disable_step($old_state)
{
switch ($old_state)
{
case '': // Empty means nothing has run yet
// Disable board rules notifications
$phpbb_notifications = $this->container->get('notification_manager');
$phpbb_notifications->disable_notifications('forumhulp.emailonbirthday.notification.type.birthday');
return 'notifications';
break;
default:
// Run parent disable step method
return parent::disable_step($old_state);
break;
}
}
/**
* Overwrite purge_step to purge notifications before
* any included and installed migrations are reverted.
*
* @param mixed $old_state State returned by previous call of this method
* @return mixed Returns false after last step, otherwise temporary state
* @access public
*/
public function purge_step($old_state)
{
switch ($old_state)
{
case '': // Empty means nothing has run yet
// Purge notifications
$phpbb_notifications = $this->container->get('notification_manager');
$phpbb_notifications->purge_notifications('forumhulp.emailonbirthday.notification.type.birthday');
return 'notifications';
break;
default:
// Run parent purge step method
return parent::purge_step($old_state);
break;
}
}
}