-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathextension.driver.php
291 lines (239 loc) · 7.27 KB
/
extension.driver.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
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
<?php
class extension_member_last_visit extends Extension
{
/**
*
* Name of the extension field table
* @var string
*
* @since version 2.0.0
*/
const FIELD_TBL_NAME = 'tbl_fields_member_last_visit';
/**
*
* Get subscribed delegates
*
* @since version 1.0.0
*/
public function getSubscribedDelegates()
{
return array(
array(
'page' => '/frontend/',
'delegate' => 'FrontendOutputPostGenerate',
'callback' => 'logVisit'
),
array(
'page' => '/system/preferences/',
'delegate' => 'AddCustomPreferenceFieldsets',
'callback' => 'appendPreferences'
),
array(
'page' => '/system/preferences/',
'delegate' => 'Save',
'callback' => 'savePreferences'
)
);
}
/*-------------------------------------------------------------------------
INSTALL / UPDATE / UNINSTALL
-------------------------------------------------------------------------*/
/**
*
* install the extension
*
* @since version 1.0.0
*/
public function install()
{
return self::createFieldTable();
}
/**
*
* update
*
* @since version 2.0.0
*/
public function update($previousVersion = false)
{
if (version_compare($previousVersion, '2', '<')) {
self::updateFieldTable('2');
self::populateFieldTable();
}
return true;
}
/**
*
* uninstall
*
* @since version 1.0.0
*/
public function uninstall()
{
Symphony::Configuration()->remove('member_last_visit');
Symphony::Configuration()->write();
return self::deleteFieldTable();
}
/**
*
* create the field table
*
* @since version 2.0.0
*/
public static function createFieldTable()
{
$tbl = self::FIELD_TBL_NAME;
return Symphony::Database()->query("
CREATE TABLE IF NOT EXISTS `$tbl` (
`id` int(11) unsigned NOT NULL auto_increment,
`field_id` int(11) unsigned NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `field_id` (`field_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
");
}
/**
*
* update field table
*
* version 1.X of this extension included 4 unecessary columns in the field
* table. this function drops all obsolete columns while updating the extension.
*
* @since version 2.0.0
*/
public static function updateFieldTable($version)
{
$tbl = self::FIELD_TBL_NAME;
if ($version === '2') {
return Symphony::Database()->query("
ALTER TABLE `$tbl`
DROP `allow_multiple_selection`,
DROP `show_association`,
DROP `related_field_id`,
DROP `limit`;
");
}
}
/**
*
* populate field table
*
* version 1.X of this extension didn't include a 'commit'-function, so the
* extension's field table didn't get populated. this function repopulates
* the table while updating the extension.
*
* @since version 2.0.0
*/
public static function populateFieldTable()
{
// get all field-instances of the type 'member_last_visit' from the 'fields'-table
$fields = FieldManager::fetch(null, null, 'ASC', 'sortorder', $type='member_last_visit');
// pupulate the 'fields_member_last_visit'-table
foreach ($fields as $key => $val) {
FieldManager::saveSettings($key, $settings=null);
}
}
/**
*
* delete the field table
*
* @since version 2.0.0
*/
public static function deleteFieldTable()
{
$tbl = self::FIELD_TBL_NAME;
return Symphony::Database()->query("
DROP TABLE IF EXISTS `$tbl`
");
}
/*-------------------------------------------------------------------------
PREFERENCES
-------------------------------------------------------------------------*/
/**
*
* append preferences
* @param array $context
*
* @since version 1.0.0
*/
public function appendPreferences($context)
{
$fieldset = new XMLElement('fieldset');
$fieldset->setAttribute('class', 'settings');
$fieldset->appendChild(new XMLElement('legend', __('Member Last Visit')));
$div = new XMLElement('div');
$label = new XMLElement('label', __('Visit Interval'));
$intervals = array(1,5,15,30,45,60);
$options = array(
array(null, false, null)
);
foreach ($intervals as $value) {
$options[] = array($value, ($value == self::getInterval()), $value . ' minute');
}
$label->appendChild(Widget::Select('settings[member_last_visit][interval]', $options));
$div->appendChild($label);
$div->appendChild(
new XMLElement('p', __('Interval determines how often a Member\'s visit is recorded.'), array('class' => 'help'))
);
$fieldset->appendChild($div);
$context['wrapper']->appendChild($fieldset);
}
/**
*
* save preferences
* @param array $context
*
* @since version 1.0.0
*/
public function savePreferences(array &$context)
{
$settings = $context['settings'];
Symphony::Configuration()->set('interval', $settings['member_last_visit']['interval'], 'member_last_visit');
Symphony::Configuration()->write();
}
/*-------------------------------------------------------------------------
LOG VISIT
-------------------------------------------------------------------------*/
/**
*
* log visit
*
* @since version 1.0.0
*/
public function logVisit()
{
$driver = Symphony::ExtensionManager()->create('members');
if(!$member_id = $driver->getMemberDriver()->getMemberID()) return false;
$cookie = new Cookie(
'member_last_visit', TWO_WEEKS, __SYM_COOKIE_PATH__, null, true
);
if ($last_visit_date = $cookie->get('last-visit')) {
if ( ($last_visit_date + (self::getInterval() * 60)) > time()) {
return false;
} else {
$cookie->set('last-visit', time());
}
} else {
$cookie->set('last-visit', time());
}
$last_visit = FieldManager::fetch(null, $driver::getMembersSection(), 'ASC', 'sortorder', 'member_last_visit');
$last_visit = current($last_visit);
$status = Field::__OK__;
$data = $last_visit->processRawFieldData(
DateTimeObj::get('Y-m-d H:i:s', time()),
$status
);
$data['entry_id'] = $member_id;
Symphony::Database()->insert($data, 'tbl_entries_data_' . $last_visit->get('id'), true);
}
/**
*
* get interval
*
* @since version 1.0.0
*/
public static function getInterval()
{
return Symphony::Configuration()->get('interval', 'member_last_visit');
}
}