forked from eileenmcnaughton/org.civicrm.multisite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
groupDeNest.sql
170 lines (153 loc) · 5.58 KB
/
groupDeNest.sql
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
#####
INSERT INTO civicrm_group_contact (contact_id, group_id, `status`)
SELECT DISTINCT child_group_contact.contact_id, domain_group.domain_group_id, 'Added'
FROM civicrm_group_organization go RIGHT JOIN (
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(value,'";',1),':"',-1) AS domain_group_id,
value, domain_id
FROM civicrm_setting s
WHERE group_name = 'Multi Site Preferences'
AND name = 'domain_group_id'
AND SUBSTRING_INDEX(SUBSTRING_INDEX(value,'";',1),':"',-1) > 0
) as domain_group
ON domain_group.domain_group_id = go.group_id
LEFT JOIN civicrm_group child_group ON go.group_id = child_group.parents
LEFT JOIN civicrm_group_organization cgo ON child_group.id = cgo.group_id
LEFT JOIN civicrm_group_contact child_group_contact ON child_group_contact.group_id = child_group.id AND child_group_contact.`status` = 'Added'
LEFT JOIN civicrm_group_contact parent_group_contact ON domain_group.domain_group_id = parent_group_contact.group_id
AND child_group_contact.contact_id = parent_group_contact.contact_id
WHERE
child_group.id IS NOT NULL
AND cgo.organization_id IS NULL
AND parent_group_contact.id IS NULL
AND child_group_contact.id IS NOT NULL
AND domain_group.domain_group_id IS NOT NULL
AND child_group.parents NOT LIKE '%,%'
;
##
# Set Status on parent group to reflect child group
###
UPDATE
civicrm_group_organization go RIGHT JOIN (
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(value,'";',1),':"',-1) AS domain_group_id,
value, domain_id
FROM civicrm_setting s
WHERE group_name = 'Multi Site Preferences'
AND name = 'domain_group_id'
AND SUBSTRING_INDEX(SUBSTRING_INDEX(value,'";',1),':"',-1) > 0
) as domain_group
ON domain_group.domain_group_id = go.group_id
LEFT JOIN civicrm_group child_group ON go.group_id = child_group.parents
LEFT JOIN civicrm_group_organization cgo ON child_group.id = cgo.group_id
LEFT JOIN civicrm_group_contact child_group_contact ON child_group_contact.group_id = child_group.id AND child_group_contact.`status` = 'Added'
LEFT JOIN civicrm_group_contact parent_group_contact ON domain_group.domain_group_id = parent_group_contact.group_id
AND child_group_contact.contact_id = parent_group_contact.contact_id
SET parent_group_contact.`status` = child_group_contact.`status`
WHERE
child_group.id IS NOT NULL
AND cgo.organization_id IS NULL
AND parent_group_contact.`status` <> 'Added'
AND child_group_contact.id IS NOT NULL
AND domain_group.domain_group_id IS NOT NULL
AND child_group.parents NOT LIKE '%,%'
;
###############################################################################################
##
## associated all groups with same org as their parent (domain) org
##
###############################################################################################
INSERT INTO civicrm_group_organization (group_id, organization_id)
SELECT child_group.id as group_id, go.organization_id as organization_id
FROM civicrm_group_organization go RIGHT JOIN (
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(value,'";',1),':"',-1) AS domain_group_id,
value, domain_id
FROM civicrm_setting s
WHERE group_name = 'Multi Site Preferences'
AND name = 'domain_group_id'
AND SUBSTRING_INDEX(SUBSTRING_INDEX(value,'";',1),':"',-1) > 0
) as se
ON se.domain_group_id = go.group_id
LEFT JOIN civicrm_group child_group ON go.group_id = child_group.parents
LEFT JOIN civicrm_group_organization cgo ON child_group.id = cgo.group_id
WHERE
child_group.id IS NOT NULL
AND cgo.organization_id IS NULL
AND se.domain_group_id IS NOT NULL
AND child_group.parents NOT LIKE '%,%'
;
DELETE gn FROM civicrm_group_nesting gn RIGHT JOIN (
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(value,'";',1),':"',-1) AS domain_group_id,
value, domain_id
FROM civicrm_setting s
WHERE group_name = 'Multi Site Preferences'
AND name = 'domain_group_id'
AND SUBSTRING_INDEX(SUBSTRING_INDEX(value,'";',1),':"',-1) > 0
) as se
ON se.domain_group_id = gn.parent_group_id
LEFT JOIN civicrm_group child_group ON child_group_id = child_group.id
WHERE child_group_id IS NOT NULL
AND child_group.parents NOT LIKE '%,%';
UPDATE civicrm_group g RIGHT JOIN (
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(value,'";',1),':"',-1) AS domain_group_id,
value, domain_id
FROM civicrm_setting s
WHERE group_name = 'Multi Site Preferences'
AND name = 'domain_group_id'
AND SUBSTRING_INDEX(SUBSTRING_INDEX(value,'";',1),':"',-1) > 0
) as se
ON se.domain_group_id = g.parents
SET parents = NULL
WHERE se.domain_group_id IS NOT NULL
AND parents NOT LIKE '%,%'
;
UPDATE civicrm_group g RIGHT JOIN (
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(value,'";',1),':"',-1) AS domain_group_id,
value, domain_id
FROM civicrm_setting s
WHERE group_name = 'Multi Site Preferences'
AND name = 'domain_group_id'
AND SUBSTRING_INDEX(SUBSTRING_INDEX(value,'";',1),':"',-1) > 0
) as se
ON se.domain_group_id = g.id
SET g.children = NULL
WHERE se.domain_group_id IS NOT NULL
;
truncate civicrm_cache;
UPDATE civicrm_setting SET
value = 'i:0;'
WHERE
group_name = 'Multi Site Preferences'
AND name = 'is_enabled'
AND domain_id = 1;
UPDATE civicrm_setting SET
value = 's:i:"0";'
WHERE
group_name = 'Multi Site Preferences'
AND name = 'domain_group_id'
AND domain_id = 1;
INSERT INTO `civicrm_setting` (
`group_name`,
`name`,
`value`,
`domain_id`,
`is_domain`)
VALUES (
'Multi Site Preferences',
'multisite_acl_enabled',
'i:0;',
1,
1);
INSERT INTO `civicrm_setting` (
`group_name`,
`name`,
`value`,
`domain_id`,
`is_domain`)
SELECT
'Multi Site Preferences' as group_name,
'multisite_acl_enabled' as name,
'i:1;' as value,
domain.id as domain_id,
0 as is_domain
FROM
civicrm_domain domain LEFT JOIN civicrm_setting s ON domain.id = domain_id AND s.name = 'multisite_acl_enabled'
WHERE s.id IS NULL;