This repository has been archived by the owner on Oct 13, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSetup.php
273 lines (241 loc) · 9.36 KB
/
Setup.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
<?php
/**
* [VersoBit] Badges
*/
namespace CMTV\Badges;
use CMTV\Badges\Constants as C;
use XF\AddOn\AbstractSetup;
use XF\AddOn\StepRunnerInstallTrait;
use XF\AddOn\StepRunnerUninstallTrait;
use XF\AddOn\StepRunnerUpgradeTrait;
use XF\Db\Schema\Alter;
use XF\Db\Schema\Create;
/**
* Class Setup
*
* @package CMTV\Badges
*/
class Setup extends AbstractSetup
{
use StepRunnerInstallTrait;
use StepRunnerUpgradeTrait;
use StepRunnerUninstallTrait;
//
// INSTALL
//
/* Table: xf_cmtv_badges_badge
Stores individual badges */
public function installStep1()
{
$this->schemaManager()->createTable(C::_table('badge'), function (Create $table) {
$table->addColumn('badge_id', 'int')->autoIncrement();
$table->addColumn('user_criteria', 'mediumblob');
$table->addColumn('badge_category_id', 'int')->setDefault(0);
$table->addColumn('icon_type', 'enum')->values(['fa', 'image']);
$table->addColumn('fa_icon', 'varchar', 256)->setDefault('');
$table->addColumn('image_url', 'varchar', 512)->setDefault('');
$table->addColumn('image_url_2x', 'varchar', 512)->setDefault('');
$table->addColumn('image_url_3x', 'varchar', 512)->setDefault('');
$table->addColumn('image_url_4x', 'varchar', 512)->setDefault('');
$table->addColumn('class', 'varchar', 256)->setDefault('');
$table->addColumn('display_order', 'int')->setDefault(10);
});
}
/* Table: xf_cmtv_badges_badge_category
Stores categories for badges */
public function installStep2()
{
$this->schemaManager()->createTable(C::_table('badge_category'), function (Create $table) {
$table->addColumn('badge_category_id', 'int')->autoIncrement();
$table->addColumn('icon_type', 'enum')->values(['', 'fa', 'image'])->setDefault('');
$table->addColumn('fa_icon', 'varchar', 256)->setDefault('');
$table->addColumn('image_url', 'varchar', 512)->setDefault('');
$table->addColumn('image_url_2x', 'varchar', 512)->setDefault('');
$table->addColumn('image_url_3x', 'varchar', 512)->setDefault('');
$table->addColumn('image_url_4x', 'varchar', 512)->setDefault('');
$table->addColumn('class', 'varchar', 256)->setDefault('');
$table->addColumn('display_order', 'int')->setDefault(10);
});
}
/* Table: xf_cmtv_badges_user_badge
Stores awarded badges into the database */
public function installStep3()
{
$this->schemaManager()->createTable(C::_table('user_badge'), function (Create $table) {
$table->addColumn('user_id', 'int');
$table->addColumn('badge_id', 'int');
$table->addColumn('award_date', 'int');
$table->addColumn('reason', 'text');
$table->addColumn('featured', 'tinyint', 3)->setDefault(0);
$table->addPrimaryKey(['badge_id', 'user_id']);
});
}
public function installStep4()
{
$this->schemaManager()->alterTable('xf_user', function (Alter $table) {
$table->addColumn(C::_column('badge_count'), 'int')->setDefault(0);
});
}
/* Setting default values for permissions */
public function installStep5()
{
$registeredPermissions = [
'manageFeatured'
];
$moderatorPermissions = [
'award',
'takeAway'
];
foreach ($registeredPermissions as $permission) {
$this->applyGlobalPermission(
C::_(),
$permission,
'forum',
'editOwnPost'
);
}
foreach ($moderatorPermissions as $permission) {
$this->applyGlobalPermission(
C::_(),
$permission,
'general',
'editBasicProfile'
);
}
$this->applyGlobalPermissionInt(
C::_(),
'featuredNumber',
4,
'forum',
'editOwnPost'
);
}
/* Add Custom User Field: Preferences (Email Opt-Out) */
public function installStep6()
{
if (!\XF::em()->find('XF:UserField', 'CMTV_Badges_Email_OptOut')) {
$field = \XF::em()->create('XF:UserField');
$title = $field->getMasterPhrase(true);
$title->phrase_text = 'Email Opt-Out';
$field->addCascadedSave($title);
$description = $field->getMasterPhrase(false);
$description->phrase_text = 'Enable to disable email notifications about badges';
$field->addCascadedSave($description);
$field->field_id = 'CMTV_Badges_Email_OptOut';
$field->display_group = 'preferences';
$field->display_order = 1;
$field->field_type = 'checkbox';
$field->field_choices = ['1' => 'Opt-out of emails about badges'];
$field->match_type = 'none';
$field->match_params = [];
$field->max_length = 0;
$field->required = 0;
$field->show_registration = 0;
$field->user_editable = 'yes';
$field->viewable_profile = 0;
$field->viewable_message = 0;
$field->moderator_editable = 0;
$field->save();
}
}
//
// UNINSTALL
//
/* Removing tables and addon columns */
public function uninstallStep1()
{
$this->schemaManager()->dropTable(C::_table('badge'));
$this->schemaManager()->dropTable(C::_table('badge_category'));
$this->schemaManager()->dropTable(C::_table('user_badge'));
$this->schemaManager()->alterTable('xf_user', function (Alter $table) {
$table->dropColumns(C::_column('badge_count'));
});
}
/* Removing phrases */
public function uninstallStep2()
{
$phrases = $this->app->finder('XF:Phrase')->where(['title', 'LIKE', '%CMTV_Badge%'])->fetch();
foreach ($phrases as $phrase) {
$phrase->delete(false);
}
}
/* Removing custom user fields and associated preferences */
public function uninstallStep3()
{
$userFields = $this->app->finder('XF:UserField')->where(['field_id', 'LIKE', '%CMTV_Badge%'])->fetch();
foreach ($userFields as $userField) {
$userField->delete(false);
$userFieldValues = $this->app->finder('XF:UserFieldValue')->where(['field_id', 'LIKE', '%CMTV_Badge%'])->fetch();
foreach ($userFieldValues as $userFieldValue){
$userFieldValue->delete(false);
}
}
}
//
// UPGRADE
//
/* Upgrading to 2000071 (2.0.0|PL1)
Remove vbBadges from database and replace with CMTV_Badges (Standards Cleanup)
*/
public function upgrade2000071Step1()
{
//Remove Phrases
$phrases = $this->app->finder('XF:Phrase')->where(['title', 'LIKE', '%vbBadges%'])->fetch();
foreach ($phrases as $phrase) {
$phrase->delete(false);
}
//Remove UserFields
$userFields = $this->app->finder('XF:UserField')->where(['field_id', 'LIKE', '%vBbadges%'])->fetch();
foreach ($userFields as $userField) {
$userField->delete(false);
}
//Install New UserField
$this->installStep6();
}
public function upgrade2000071Step2(array $stepParams)
{
$finder = \XF::finder('XF:UserFieldValue')->where('field_id', '=', 'vBbadgesEmailOptOut');
$stepData = isset($stepParams[2]) ? $stepParams[2] : [];
if (!isset($stepData['max']))
{
$stepData['max'] = $finder->total();
}
$userFields = $finder->limit(50)->fetch();
if (!$userFields->count())
{
return null;
}
$next = 0;
foreach ($userFields as $userField)
{
$next++;
$userField->field_id = 'CMTV_Badges_Email_OptOut';
$userField->save();
}
return [
$next,
"{$next} / {$stepData['max']}",
$stepData
];
}
public function upgrade2000071Step3()
{
//Upgrade image_url, fa_icon, and class to new VARCHAR length's.
$this->schemaManager()->alterTable(C::_table('badge'), function (Alter $table) {
$table->changeColumn('fa_icon','varchar', 256);
$table->changeColumn('image_url','varchar', 512);
$table->addColumn('image_url_2x', 'varchar', 512)->after('image_url')->setDefault('');
$table->addColumn('image_url_3x', 'varchar', 512)->after('image_url_2x')->setDefault('');
$table->addColumn('image_url_4x', 'varchar', 512)->after('image_url_3x')->setDefault('');
$table->changeColumn('class','varchar', 256);
});
$this->schemaManager()->alterTable(C::_table('badge_category'), function (Alter $table) {
$table->changeColumn('fa_icon','varchar', 256);
$table->changeColumn('image_url','varchar', 512);
$table->addColumn('image_url_2x', 'varchar', 512)->after('image_url')->setDefault('');
$table->addColumn('image_url_3x', 'varchar', 512)->after('image_url_2x')->setDefault('');
$table->addColumn('image_url_4x', 'varchar', 512)->after('image_url_3x')->setDefault('');
$table->changeColumn('class','varchar', 256);
});
}
}