|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
|
5
|
|
|
* @license https://flipboxfactory.com/software/organization/license |
|
6
|
|
|
* @link https://www.flipboxfactory.com/software/organization/ |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace flipbox\organizations\migrations; |
|
10
|
|
|
|
|
11
|
|
|
use craft\db\Migration; |
|
12
|
|
|
use craft\records\Element as ElementRecord; |
|
13
|
|
|
use craft\records\FieldLayout as FieldLayoutRecord; |
|
14
|
|
|
use craft\records\Site as SiteRecord; |
|
15
|
|
|
use craft\records\User as UserRecord; |
|
16
|
|
|
use flipbox\organizations\Organizations; |
|
17
|
|
|
use flipbox\organizations\records\Organization as OrganizationRecord; |
|
18
|
|
|
use flipbox\organizations\records\OrganizationType as OrganizationTypeRecord; |
|
19
|
|
|
use flipbox\organizations\records\OrganizationTypeAssociation as OrganizationTypeAssociationRecord; |
|
20
|
|
|
use flipbox\organizations\records\OrganizationTypeSiteSettings as OrganizationTypeSiteSettingsRecord; |
|
21
|
|
|
use flipbox\organizations\records\UserAssociation as OrganizationUserRecord; |
|
22
|
|
|
use flipbox\organizations\records\UserType as OrganizationUserTypeRecord; |
|
23
|
|
|
use flipbox\organizations\records\UserTypeAssociation as OrganizationUserTypeAssociationRecord; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @author Flipbox Factory <[email protected]> |
|
27
|
|
|
* @since 1.0.0 |
|
28
|
|
|
*/ |
|
29
|
|
|
class Install extends Migration |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* @inheritdoc |
|
33
|
|
|
*/ |
|
34
|
|
|
public function safeUp() |
|
35
|
|
|
{ |
|
36
|
|
|
$this->createTables(); |
|
37
|
|
|
$this->createIndexes(); |
|
38
|
|
|
$this->addForeignKeys(); |
|
39
|
|
|
|
|
40
|
|
|
return true; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @inheritdoc |
|
45
|
|
|
*/ |
|
46
|
|
|
public function safeDown() |
|
47
|
|
|
{ |
|
48
|
|
|
$this->dropTableIfExists(OrganizationUserTypeAssociationRecord::tableName()); |
|
49
|
|
|
$this->dropTableIfExists(OrganizationUserTypeRecord::tableName()); |
|
50
|
|
|
$this->dropTableIfExists(OrganizationUserRecord::tableName()); |
|
51
|
|
|
$this->dropTableIfExists(OrganizationTypeAssociationRecord::tableName()); |
|
52
|
|
|
$this->dropTableIfExists(OrganizationTypeSiteSettingsRecord::tableName()); |
|
53
|
|
|
$this->dropTableIfExists(OrganizationTypeRecord::tableName()); |
|
54
|
|
|
$this->dropTableIfExists(OrganizationRecord::tableName()); |
|
55
|
|
|
|
|
56
|
|
|
return true; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Creates the tables. |
|
61
|
|
|
* |
|
62
|
|
|
* @return void |
|
63
|
|
|
*/ |
|
64
|
|
|
protected function createTables() |
|
65
|
|
|
{ |
|
66
|
|
|
$settings = Organizations::getInstance()->getSettings(); |
|
67
|
|
|
|
|
68
|
|
|
$this->createTable(OrganizationRecord::tableName(), [ |
|
69
|
|
|
'id' => $this->primaryKey(), |
|
70
|
|
|
'dateJoined' => $this->dateTime()->notNull(), |
|
71
|
|
|
'dateCreated' => $this->dateTime()->notNull(), |
|
72
|
|
|
'dateUpdated' => $this->dateTime()->notNull(), |
|
73
|
|
|
'uid' => $this->uid() |
|
74
|
|
|
]); |
|
75
|
|
|
|
|
76
|
|
|
$this->createTable(OrganizationTypeRecord::tableName(), [ |
|
77
|
|
|
'id' => $this->primaryKey(), |
|
78
|
|
|
'handle' => $this->string()->notNull(), |
|
79
|
|
|
'name' => $this->string()->notNull(), |
|
80
|
|
|
'fieldLayoutId' => $this->integer(), |
|
81
|
|
|
'dateCreated' => $this->dateTime()->notNull(), |
|
82
|
|
|
'dateUpdated' => $this->dateTime()->notNull(), |
|
83
|
|
|
'uid' => $this->uid() |
|
84
|
|
|
]); |
|
85
|
|
|
|
|
86
|
|
|
$this->createTable(OrganizationTypeSiteSettingsRecord::tableName(), [ |
|
87
|
|
|
'typeId' => $this->integer()->notNull(), |
|
88
|
|
|
'siteId' => $this->integer()->notNull(), |
|
89
|
|
|
'enabledByDefault' => $this->boolean()->defaultValue(true)->notNull(), |
|
90
|
|
|
'hasUrls' => $this->boolean()->defaultValue(true)->notNull(), |
|
91
|
|
|
'uriFormat' => $this->text(), |
|
92
|
|
|
'template' => $this->string(500), |
|
93
|
|
|
'dateCreated' => $this->dateTime()->notNull(), |
|
94
|
|
|
'dateUpdated' => $this->dateTime()->notNull(), |
|
95
|
|
|
'uid' => $this->uid() |
|
96
|
|
|
]); |
|
97
|
|
|
|
|
98
|
|
|
$this->createTable(OrganizationTypeAssociationRecord::tableName(), [ |
|
99
|
|
|
'typeId' => $this->integer()->notNull(), |
|
100
|
|
|
'organizationId' => $this->integer()->notNull(), |
|
101
|
|
|
'sortOrder' => $this->smallInteger()->unsigned(), |
|
102
|
|
|
'dateCreated' => $this->dateTime()->notNull(), |
|
103
|
|
|
'dateUpdated' => $this->dateTime()->notNull(), |
|
104
|
|
|
'uid' => $this->uid() |
|
105
|
|
|
]); |
|
106
|
|
|
|
|
107
|
|
|
$this->createTable(OrganizationUserRecord::tableName(), [ |
|
108
|
|
|
'id' => $this->primaryKey(), |
|
109
|
|
|
'userId' => $this->integer()->notNull(), |
|
110
|
|
|
'organizationId' => $this->integer()->notNull(), |
|
111
|
|
|
'state' => $this->enum('state', array_keys($settings->getUserStates())) |
|
112
|
|
|
->defaultValue($settings->getDefaultUserState()) |
|
113
|
|
|
->notNull(), |
|
114
|
|
|
'userOrder' => $this->smallInteger()->unsigned(), |
|
115
|
|
|
'organizationOrder' => $this->smallInteger()->unsigned(), |
|
116
|
|
|
'dateCreated' => $this->dateTime()->notNull(), |
|
117
|
|
|
'dateUpdated' => $this->dateTime()->notNull(), |
|
118
|
|
|
'uid' => $this->uid() |
|
119
|
|
|
]); |
|
120
|
|
|
|
|
121
|
|
|
$this->createTable(OrganizationUserTypeRecord::tableName(), [ |
|
122
|
|
|
'id' => $this->primaryKey(), |
|
123
|
|
|
'handle' => $this->string()->notNull(), |
|
124
|
|
|
'name' => $this->string()->notNull(), |
|
125
|
|
|
'dateCreated' => $this->dateTime()->notNull(), |
|
126
|
|
|
'dateUpdated' => $this->dateTime()->notNull(), |
|
127
|
|
|
'uid' => $this->uid() |
|
128
|
|
|
]); |
|
129
|
|
|
|
|
130
|
|
|
$this->createTable(OrganizationUserTypeAssociationRecord::tableName(), [ |
|
131
|
|
|
'userId' => $this->integer()->notNull(), |
|
132
|
|
|
'typeId' => $this->integer()->notNull(), |
|
133
|
|
|
'sortOrder' => $this->smallInteger()->unsigned(), |
|
134
|
|
|
'dateCreated' => $this->dateTime()->notNull(), |
|
135
|
|
|
'dateUpdated' => $this->dateTime()->notNull(), |
|
136
|
|
|
'uid' => $this->uid() |
|
137
|
|
|
]); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Creates the indexes. |
|
142
|
|
|
* |
|
143
|
|
|
* @return void |
|
144
|
|
|
*/ |
|
145
|
|
|
protected function createIndexes() |
|
146
|
|
|
{ |
|
147
|
|
|
$this->createIndex( |
|
148
|
|
|
$this->db->getIndexName( |
|
149
|
|
|
OrganizationTypeRecord::tableName(), |
|
150
|
|
|
'handle', |
|
151
|
|
|
true |
|
152
|
|
|
), |
|
153
|
|
|
OrganizationTypeRecord::tableName(), |
|
154
|
|
|
'handle', |
|
155
|
|
|
true |
|
156
|
|
|
); |
|
157
|
|
|
$this->createIndex( |
|
158
|
|
|
$this->db->getIndexName( |
|
159
|
|
|
OrganizationTypeRecord::tableName(), |
|
160
|
|
|
'fieldLayoutId', |
|
161
|
|
|
false, |
|
162
|
|
|
true |
|
163
|
|
|
), |
|
164
|
|
|
OrganizationTypeRecord::tableName(), |
|
165
|
|
|
'fieldLayoutId', |
|
166
|
|
|
false |
|
167
|
|
|
); |
|
168
|
|
|
|
|
169
|
|
|
$this->addPrimaryKey( |
|
170
|
|
|
$this->db->getPrimaryKeyName( |
|
171
|
|
|
OrganizationTypeSiteSettingsRecord::tableName(), |
|
172
|
|
|
['typeId', 'siteId'] |
|
173
|
|
|
), |
|
174
|
|
|
OrganizationTypeSiteSettingsRecord::tableName(), |
|
175
|
|
|
['typeId', 'siteId'] |
|
176
|
|
|
); |
|
177
|
|
|
$this->createIndex( |
|
178
|
|
|
$this->db->getIndexName( |
|
179
|
|
|
OrganizationTypeSiteSettingsRecord::tableName(), |
|
180
|
|
|
'typeId', |
|
181
|
|
|
false, |
|
182
|
|
|
true |
|
183
|
|
|
), |
|
184
|
|
|
OrganizationTypeSiteSettingsRecord::tableName(), |
|
185
|
|
|
'typeId', |
|
186
|
|
|
false |
|
187
|
|
|
); |
|
188
|
|
|
$this->createIndex( |
|
189
|
|
|
$this->db->getIndexName( |
|
190
|
|
|
OrganizationTypeSiteSettingsRecord::tableName(), |
|
191
|
|
|
'siteId', |
|
192
|
|
|
false, |
|
193
|
|
|
true |
|
194
|
|
|
), |
|
195
|
|
|
OrganizationTypeSiteSettingsRecord::tableName(), |
|
196
|
|
|
'siteId', |
|
197
|
|
|
false |
|
198
|
|
|
); |
|
199
|
|
|
$this->createIndex( |
|
200
|
|
|
$this->db->getIndexName( |
|
201
|
|
|
OrganizationTypeSiteSettingsRecord::tableName(), |
|
202
|
|
|
'typeId,siteId', |
|
203
|
|
|
true |
|
204
|
|
|
), |
|
205
|
|
|
OrganizationTypeSiteSettingsRecord::tableName(), |
|
206
|
|
|
'typeId,siteId', |
|
207
|
|
|
true |
|
208
|
|
|
); |
|
209
|
|
|
|
|
210
|
|
|
$this->addPrimaryKey( |
|
211
|
|
|
$this->db->getPrimaryKeyName( |
|
212
|
|
|
OrganizationTypeAssociationRecord::tableName(), |
|
213
|
|
|
['typeId', 'organizationId'] |
|
214
|
|
|
), |
|
215
|
|
|
OrganizationTypeAssociationRecord::tableName(), |
|
216
|
|
|
['typeId', 'organizationId'] |
|
217
|
|
|
); |
|
218
|
|
|
$this->createIndex( |
|
219
|
|
|
$this->db->getIndexName( |
|
220
|
|
|
OrganizationTypeAssociationRecord::tableName(), |
|
221
|
|
|
'typeId', |
|
222
|
|
|
false, |
|
223
|
|
|
true |
|
224
|
|
|
), |
|
225
|
|
|
OrganizationTypeAssociationRecord::tableName(), |
|
226
|
|
|
'typeId', |
|
227
|
|
|
false |
|
228
|
|
|
); |
|
229
|
|
|
$this->createIndex( |
|
230
|
|
|
$this->db->getIndexName( |
|
231
|
|
|
OrganizationTypeAssociationRecord::tableName(), |
|
232
|
|
|
'organizationId', |
|
233
|
|
|
false, |
|
234
|
|
|
true |
|
235
|
|
|
), |
|
236
|
|
|
OrganizationTypeAssociationRecord::tableName(), |
|
237
|
|
|
'organizationId', |
|
238
|
|
|
false |
|
239
|
|
|
); |
|
240
|
|
|
$this->createIndex( |
|
241
|
|
|
$this->db->getIndexName( |
|
242
|
|
|
OrganizationTypeAssociationRecord::tableName(), |
|
243
|
|
|
'typeId,organizationId', |
|
244
|
|
|
true |
|
245
|
|
|
), |
|
246
|
|
|
OrganizationTypeAssociationRecord::tableName(), |
|
247
|
|
|
'typeId,organizationId', |
|
248
|
|
|
true |
|
249
|
|
|
); |
|
250
|
|
|
|
|
251
|
|
|
$this->createIndex( |
|
252
|
|
|
$this->db->getIndexName( |
|
253
|
|
|
OrganizationUserRecord::tableName(), |
|
254
|
|
|
'userId', |
|
255
|
|
|
false, |
|
256
|
|
|
true |
|
257
|
|
|
), |
|
258
|
|
|
OrganizationUserRecord::tableName(), |
|
259
|
|
|
'userId', |
|
260
|
|
|
false |
|
261
|
|
|
); |
|
262
|
|
|
$this->createIndex( |
|
263
|
|
|
$this->db->getIndexName( |
|
264
|
|
|
OrganizationUserRecord::tableName(), |
|
265
|
|
|
'organizationId', |
|
266
|
|
|
false, |
|
267
|
|
|
true |
|
268
|
|
|
), |
|
269
|
|
|
OrganizationUserRecord::tableName(), |
|
270
|
|
|
'organizationId', |
|
271
|
|
|
false |
|
272
|
|
|
); |
|
273
|
|
|
$this->createIndex( |
|
274
|
|
|
$this->db->getIndexName( |
|
275
|
|
|
OrganizationUserRecord::tableName(), |
|
276
|
|
|
'userId,organizationId', |
|
277
|
|
|
true |
|
278
|
|
|
), |
|
279
|
|
|
OrganizationUserRecord::tableName(), |
|
280
|
|
|
'userId,organizationId', |
|
281
|
|
|
true |
|
282
|
|
|
); |
|
283
|
|
|
|
|
284
|
|
|
$this->createIndex( |
|
285
|
|
|
$this->db->getIndexName( |
|
286
|
|
|
OrganizationUserTypeRecord::tableName(), |
|
287
|
|
|
'handle', |
|
288
|
|
|
true |
|
289
|
|
|
), |
|
290
|
|
|
OrganizationUserTypeRecord::tableName(), |
|
291
|
|
|
'handle', |
|
292
|
|
|
true |
|
293
|
|
|
); |
|
294
|
|
|
|
|
295
|
|
|
$this->addPrimaryKey( |
|
296
|
|
|
$this->db->getPrimaryKeyName( |
|
297
|
|
|
OrganizationUserTypeAssociationRecord::tableName(), |
|
298
|
|
|
['userId', 'typeId'] |
|
299
|
|
|
), |
|
300
|
|
|
OrganizationUserTypeAssociationRecord::tableName(), |
|
301
|
|
|
['userId', 'typeId'] |
|
302
|
|
|
); |
|
303
|
|
|
$this->createIndex( |
|
304
|
|
|
$this->db->getIndexName( |
|
305
|
|
|
OrganizationUserTypeAssociationRecord::tableName(), |
|
306
|
|
|
'userId', |
|
307
|
|
|
false, |
|
308
|
|
|
true |
|
309
|
|
|
), |
|
310
|
|
|
OrganizationUserTypeAssociationRecord::tableName(), |
|
311
|
|
|
'userId', |
|
312
|
|
|
false |
|
313
|
|
|
); |
|
314
|
|
|
$this->createIndex( |
|
315
|
|
|
$this->db->getIndexName( |
|
316
|
|
|
OrganizationUserTypeAssociationRecord::tableName(), |
|
317
|
|
|
'typeId', |
|
318
|
|
|
false, |
|
319
|
|
|
true |
|
320
|
|
|
), |
|
321
|
|
|
OrganizationUserTypeAssociationRecord::tableName(), |
|
322
|
|
|
'typeId', |
|
323
|
|
|
false |
|
324
|
|
|
); |
|
325
|
|
|
$this->createIndex( |
|
326
|
|
|
$this->db->getIndexName( |
|
327
|
|
|
OrganizationUserTypeAssociationRecord::tableName(), |
|
328
|
|
|
'userId,typeId', |
|
329
|
|
|
true |
|
330
|
|
|
), |
|
331
|
|
|
OrganizationUserTypeAssociationRecord::tableName(), |
|
332
|
|
|
'userId,typeId', |
|
333
|
|
|
true |
|
334
|
|
|
); |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
|
|
/** |
|
338
|
|
|
* Adds the foreign keys. |
|
339
|
|
|
* |
|
340
|
|
|
* @return void |
|
341
|
|
|
*/ |
|
342
|
|
|
protected function addForeignKeys() |
|
343
|
|
|
{ |
|
344
|
|
|
|
|
345
|
|
|
$this->addForeignKey( |
|
346
|
|
|
$this->db->getForeignKeyName( |
|
347
|
|
|
OrganizationRecord::tableName(), |
|
348
|
|
|
'id' |
|
349
|
|
|
), |
|
350
|
|
|
OrganizationRecord::tableName(), |
|
351
|
|
|
'id', |
|
352
|
|
|
ElementRecord::tableName(), |
|
353
|
|
|
'id', |
|
354
|
|
|
'CASCADE', |
|
355
|
|
|
null |
|
356
|
|
|
); |
|
357
|
|
|
|
|
358
|
|
|
$this->addForeignKey( |
|
359
|
|
|
$this->db->getForeignKeyName( |
|
360
|
|
|
OrganizationTypeRecord::tableName(), |
|
361
|
|
|
'fieldLayoutId' |
|
362
|
|
|
), |
|
363
|
|
|
OrganizationTypeRecord::tableName(), |
|
364
|
|
|
'fieldLayoutId', |
|
365
|
|
|
FieldLayoutRecord::tableName(), |
|
366
|
|
|
'id', |
|
367
|
|
|
'SET NULL', |
|
368
|
|
|
null |
|
369
|
|
|
); |
|
370
|
|
|
|
|
371
|
|
|
$this->addForeignKey( |
|
372
|
|
|
$this->db->getForeignKeyName( |
|
373
|
|
|
OrganizationTypeSiteSettingsRecord::tableName(), |
|
374
|
|
|
'typeId' |
|
375
|
|
|
), |
|
376
|
|
|
OrganizationTypeSiteSettingsRecord::tableName(), |
|
377
|
|
|
'typeId', |
|
378
|
|
|
OrganizationTypeRecord::tableName(), |
|
379
|
|
|
'id', |
|
380
|
|
|
'CASCADE', |
|
381
|
|
|
'CASCADE' |
|
382
|
|
|
); |
|
383
|
|
|
$this->addForeignKey( |
|
384
|
|
|
$this->db->getForeignKeyName( |
|
385
|
|
|
OrganizationTypeSiteSettingsRecord::tableName(), |
|
386
|
|
|
'siteId' |
|
387
|
|
|
), |
|
388
|
|
|
OrganizationTypeSiteSettingsRecord::tableName(), |
|
389
|
|
|
'siteId', |
|
390
|
|
|
SiteRecord::tableName(), |
|
391
|
|
|
'id', |
|
392
|
|
|
'CASCADE', |
|
393
|
|
|
'CASCADE' |
|
394
|
|
|
); |
|
395
|
|
|
|
|
396
|
|
|
$this->addForeignKey( |
|
397
|
|
|
$this->db->getForeignKeyName( |
|
398
|
|
|
OrganizationTypeAssociationRecord::tableName(), |
|
399
|
|
|
'typeId' |
|
400
|
|
|
), |
|
401
|
|
|
OrganizationTypeAssociationRecord::tableName(), |
|
402
|
|
|
'typeId', |
|
403
|
|
|
OrganizationTypeRecord::tableName(), |
|
404
|
|
|
'id', |
|
405
|
|
|
'CASCADE', |
|
406
|
|
|
'CASCADE' |
|
407
|
|
|
); |
|
408
|
|
|
$this->addForeignKey( |
|
409
|
|
|
$this->db->getForeignKeyName( |
|
410
|
|
|
OrganizationTypeAssociationRecord::tableName(), |
|
411
|
|
|
'organizationId' |
|
412
|
|
|
), |
|
413
|
|
|
OrganizationTypeAssociationRecord::tableName(), |
|
414
|
|
|
'organizationId', |
|
415
|
|
|
OrganizationRecord::tableName(), |
|
416
|
|
|
'id', |
|
417
|
|
|
'CASCADE', |
|
418
|
|
|
'CASCADE' |
|
419
|
|
|
); |
|
420
|
|
|
|
|
421
|
|
|
$this->addForeignKey( |
|
422
|
|
|
$this->db->getForeignKeyName( |
|
423
|
|
|
OrganizationUserRecord::tableName(), |
|
424
|
|
|
'userId' |
|
425
|
|
|
), |
|
426
|
|
|
OrganizationUserRecord::tableName(), |
|
427
|
|
|
'userId', |
|
428
|
|
|
UserRecord::tableName(), |
|
429
|
|
|
'id', |
|
430
|
|
|
'CASCADE', |
|
431
|
|
|
'CASCADE' |
|
432
|
|
|
); |
|
433
|
|
|
$this->addForeignKey( |
|
434
|
|
|
$this->db->getForeignKeyName( |
|
435
|
|
|
OrganizationUserRecord::tableName(), |
|
436
|
|
|
'organizationId' |
|
437
|
|
|
), |
|
438
|
|
|
OrganizationUserRecord::tableName(), |
|
439
|
|
|
'organizationId', |
|
440
|
|
|
OrganizationRecord::tableName(), |
|
441
|
|
|
'id', |
|
442
|
|
|
'CASCADE', |
|
443
|
|
|
'CASCADE' |
|
444
|
|
|
); |
|
445
|
|
|
|
|
446
|
|
|
$this->addForeignKey( |
|
447
|
|
|
$this->db->getForeignKeyName( |
|
448
|
|
|
OrganizationUserTypeAssociationRecord::tableName(), |
|
449
|
|
|
'typeId' |
|
450
|
|
|
), |
|
451
|
|
|
OrganizationUserTypeAssociationRecord::tableName(), |
|
452
|
|
|
'typeId', |
|
453
|
|
|
OrganizationUserTypeRecord::tableName(), |
|
454
|
|
|
'id', |
|
455
|
|
|
'CASCADE', |
|
456
|
|
|
'CASCADE' |
|
457
|
|
|
); |
|
458
|
|
|
$this->addForeignKey( |
|
459
|
|
|
$this->db->getForeignKeyName( |
|
460
|
|
|
OrganizationUserTypeAssociationRecord::tableName(), |
|
461
|
|
|
'userId' |
|
462
|
|
|
), |
|
463
|
|
|
OrganizationUserTypeAssociationRecord::tableName(), |
|
464
|
|
|
'userId', |
|
465
|
|
|
OrganizationUserRecord::tableName(), |
|
466
|
|
|
'id', |
|
467
|
|
|
'CASCADE', |
|
468
|
|
|
'CASCADE' |
|
469
|
|
|
); |
|
470
|
|
|
} |
|
471
|
|
|
} |
|
472
|
|
|
|