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\organization\queue\jobs; |
10
|
|
|
|
11
|
|
|
use Craft; |
12
|
|
|
use craft\db\Query; |
13
|
|
|
use craft\queue\BaseJob; |
14
|
|
|
use flipbox\organization\records\User; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @author Flipbox Factory <[email protected]> |
18
|
|
|
* @since 1.0.0 |
19
|
|
|
*/ |
20
|
|
|
class LocalizeRelations extends BaseJob |
21
|
|
|
{ |
22
|
|
|
// Properties |
23
|
|
|
// ========================================================================= |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var int|null The field ID whose data should be localized |
27
|
|
|
*/ |
28
|
|
|
public $userId; |
29
|
|
|
|
30
|
|
|
// Public Methods |
31
|
|
|
// ========================================================================= |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @inheritdoc |
35
|
|
|
*/ |
36
|
|
|
public function execute($queue) |
37
|
|
|
{ |
38
|
|
|
|
39
|
|
|
$relations = (new Query()) |
40
|
|
|
->select(['id', 'userId', 'siteId', 'organizationId', 'sortOrder']) |
41
|
|
|
->from([User::tableName()]) |
42
|
|
|
->where([ |
43
|
|
|
'userId' => $this->userId, |
44
|
|
|
'sourceSiteId' => null |
45
|
|
|
]) |
46
|
|
|
->all(); |
47
|
|
|
|
48
|
|
|
$totalRelations = count($relations); |
49
|
|
|
$allSiteIds = Craft::$app->getSites()->getAllSiteIds(); |
50
|
|
|
$primarySiteId = array_shift($allSiteIds); |
51
|
|
|
$db = Craft::$app->getDb(); |
52
|
|
|
|
53
|
|
|
foreach ($relations as $i => $relation) { |
54
|
|
|
$this->setProgress($queue, $i / $totalRelations); |
55
|
|
|
|
56
|
|
|
// Set the existing relation to the primary site |
57
|
|
|
$db->createCommand() |
58
|
|
|
->update( |
59
|
|
|
User::tableName(), |
60
|
|
|
['siteId' => $primarySiteId], |
61
|
|
|
['id' => $relation['id']]) |
62
|
|
|
->execute(); |
63
|
|
|
|
64
|
|
|
// Duplicate it for the other sites |
65
|
|
|
foreach ($allSiteIds as $siteId) { |
66
|
|
|
$db->createCommand() |
67
|
|
|
->insert( |
68
|
|
|
User::tableName(), |
69
|
|
|
[ |
70
|
|
|
'userId' => $this->userId, |
71
|
|
|
'siteId' => $siteId, |
72
|
|
|
'organizationId' => $relation['organizationId'], |
73
|
|
|
'sortOrder' => $relation['sortOrder'], |
74
|
|
|
]) |
75
|
|
|
->execute(); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
// Protected Methods |
82
|
|
|
// ========================================================================= |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @inheritdoc |
86
|
|
|
*/ |
87
|
|
|
protected function defaultDescription(): string |
88
|
|
|
{ |
89
|
|
|
return Craft::t('organization', 'Localizing organization relations'); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|