Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
c866eb5f TO |
18 | /** |
19 | * d6 compatible? | |
20 | */ | |
6a488035 TO |
21 | class CRM_Bridge_OG_Drupal { |
22 | ||
e0ef6999 | 23 | /** |
c490a46a | 24 | * @param array $params |
e0ef6999 EM |
25 | * @param $op |
26 | */ | |
00be9182 | 27 | public static function nodeapi(&$params, $op) { |
6a488035 TO |
28 | |
29 | $transaction = new CRM_Core_Transaction(); | |
30 | ||
31 | // first create or update the CiviCRM group | |
32 | $groupParams = $params; | |
33 | $groupParams['source'] = CRM_Bridge_OG_Utils::ogSyncName($params['og_id']); | |
be2fb01f | 34 | $groupParams['group_type'] = ['2' => 1]; |
6a488035 TO |
35 | self::updateCiviGroup($groupParams, $op); |
36 | ||
37 | if (CRM_Bridge_OG_Utils::aclEnabled()) { | |
38 | // next create or update the CiviCRM ACL group | |
39 | $aclParams = $params; | |
40 | $aclParams['name'] = $aclParams['title'] = "{$aclParams['name']}: Administrator"; | |
41 | $aclParams['source'] = CRM_Bridge_OG_Utils::ogSyncACLName($params['og_id']); | |
be2fb01f | 42 | $aclParams['group_type'] = ['1']; |
6a488035 TO |
43 | self::updateCiviGroup($aclParams, $op); |
44 | ||
45 | $aclParams['acl_group_id'] = $aclParams['group_id']; | |
46 | $aclParams['civicrm_group_id'] = $groupParams['group_id']; | |
47 | ||
48 | self::updateCiviACLTables($aclParams, $op); | |
49 | } | |
50 | ||
51 | $transaction->commit(); | |
52 | } | |
53 | ||
e0ef6999 | 54 | /** |
c490a46a | 55 | * @param array $params |
e0ef6999 EM |
56 | * @param $op |
57 | * @param null $groupType | |
58 | */ | |
00be9182 | 59 | public static function updateCiviGroup(&$params, $op, $groupType = NULL) { |
353ffa53 | 60 | $abort = FALSE; |
6a488035 | 61 | $params['version'] = 3; |
353ffa53 | 62 | $params['id'] = CRM_Bridge_OG_Utils::groupID($params['source'], $params['title'], $abort); |
6a488035 TO |
63 | |
64 | if ($op == 'add') { | |
65 | if ($groupType) { | |
66 | $params['group_type'] = $groupType; | |
67 | } | |
68 | ||
69 | $group = civicrm_api('group', 'create', $params); | |
70 | if (!civicrm_error($group)) { | |
71 | $params['group_id'] = $group['id']; | |
72 | } | |
73 | } | |
74 | else { | |
75 | // do this only if we have a valid id | |
76 | if ($params['id']) { | |
77 | CRM_Contact_BAO_Group::discard($params['id']); | |
78 | $params['group_id'] = $params['id']; | |
79 | } | |
80 | } | |
81 | unset($params['id']); | |
82 | } | |
83 | ||
e0ef6999 | 84 | /** |
100fef9d | 85 | * @param array $aclParams |
e0ef6999 EM |
86 | * @param $op |
87 | */ | |
00be9182 | 88 | public static function updateCiviACLTables($aclParams, $op) { |
6a488035 TO |
89 | if ($op == 'delete') { |
90 | self::updateCiviACL($aclParams, $op); | |
91 | self::updateCiviACLEntityRole($aclParams, $op); | |
92 | self::updateCiviACLRole($aclParams, $op); | |
93 | } | |
94 | else { | |
95 | self::updateCiviACLRole($aclParams, $op); | |
96 | self::updateCiviACLEntityRole($aclParams, $op); | |
97 | self::updateCiviACL($aclParams, $op); | |
98 | } | |
99 | } | |
100 | ||
e0ef6999 | 101 | /** |
c490a46a | 102 | * @param array $params |
e0ef6999 EM |
103 | * @param $op |
104 | */ | |
00be9182 | 105 | public static function updateCiviACLRole(&$params, $op) { |
6a488035 TO |
106 | |
107 | $optionGroupID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', | |
108 | 'acl_role', | |
109 | 'id', | |
110 | 'name' | |
111 | ); | |
112 | ||
113 | $dao = new CRM_Core_DAO_OptionValue(); | |
114 | $dao->option_group_id = $optionGroupID; | |
115 | $dao->description = $params['source']; | |
116 | ||
117 | if ($op == 'delete') { | |
118 | $dao->delete(); | |
119 | return; | |
120 | } | |
121 | ||
122 | $dao->label = $params['title']; | |
123 | $dao->is_active = 1; | |
124 | ||
be2fb01f | 125 | $weightParams = ['option_group_id' => $optionGroupID]; |
6a488035 TO |
126 | $dao->weight = CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue', |
127 | $weightParams | |
128 | ); | |
129 | $dao->value = CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue', | |
130 | $weightParams, | |
131 | 'value' | |
132 | ); | |
133 | ||
134 | $query = " | |
135 | SELECT v.id | |
136 | FROM civicrm_option_value v | |
137 | WHERE v.option_group_id = %1 | |
138 | AND v.description = %2 | |
139 | "; | |
be2fb01f CW |
140 | $queryParams = [ |
141 | 1 => [$optionGroupID, 'Integer'], | |
142 | 2 => [$params['source'], 'String'], | |
143 | ]; | |
6a488035 TO |
144 | $dao->id = CRM_Core_DAO::singleValueQuery($query, $queryParams); |
145 | $dao->save(); | |
146 | $params['acl_role_id'] = $dao->value; | |
147 | } | |
148 | ||
e0ef6999 | 149 | /** |
c490a46a | 150 | * @param array $params |
e0ef6999 EM |
151 | * @param $op |
152 | */ | |
00be9182 | 153 | public static function updateCiviACLEntityRole(&$params, $op) { |
6a488035 TO |
154 | $dao = new CRM_ACL_DAO_EntityRole(); |
155 | ||
156 | $dao->entity_table = 'civicrm_group'; | |
157 | $dao->entity_id = $params['acl_group_id']; | |
158 | if ($op == 'delete') { | |
159 | $dao->delete(); | |
160 | return; | |
161 | } | |
162 | ||
163 | $dao->acl_role_id = $params['acl_role_id']; | |
164 | ||
165 | $dao->find(TRUE); | |
166 | $dao->is_active = TRUE; | |
167 | $dao->save(); | |
168 | $params['acl_entity_role_id'] = $dao->id; | |
169 | } | |
170 | ||
e0ef6999 | 171 | /** |
c490a46a | 172 | * @param array $params |
e0ef6999 EM |
173 | * @param $op |
174 | */ | |
00be9182 | 175 | public static function updateCiviACL(&$params, $op) { |
6a488035 TO |
176 | $dao = new CRM_ACL_DAO_ACL(); |
177 | ||
178 | $dao->object_table = 'civicrm_saved_search'; | |
179 | $dao->object_id = $params['civicrm_group_id']; | |
180 | ||
181 | if ($op == 'delete') { | |
182 | $dao->delete(); | |
183 | return; | |
184 | } | |
185 | ||
186 | $dao->find(TRUE); | |
187 | ||
188 | $dao->entity_table = 'civicrm_acl_role'; | |
353ffa53 TO |
189 | $dao->entity_id = $params['acl_role_id']; |
190 | $dao->operation = 'Edit'; | |
6a488035 TO |
191 | |
192 | $dao->is_active = TRUE; | |
193 | $dao->save(); | |
194 | $params['acl_id'] = $dao->id; | |
195 | } | |
196 | ||
e0ef6999 | 197 | /** |
c490a46a | 198 | * @param array $params |
e0ef6999 EM |
199 | * @param $op |
200 | * | |
201 | * @throws Exception | |
202 | */ | |
00be9182 | 203 | public static function og(&$params, $op) { |
6a488035 TO |
204 | |
205 | $contactID = CRM_Bridge_OG_Utils::contactID($params['uf_id']); | |
206 | if (!$contactID) { | |
ba968e38 | 207 | throw new CRM_Core_Exception(' no contact found'); |
6a488035 TO |
208 | } |
209 | ||
210 | // get the group id of this OG | |
211 | $groupID = CRM_Bridge_OG_Utils::groupID(CRM_Bridge_OG_Utils::ogSyncName($params['og_id']), | |
212 | NULL, TRUE | |
213 | ); | |
214 | ||
be2fb01f | 215 | $groupParams = [ |
6a488035 TO |
216 | 'contact_id' => $contactID, |
217 | 'group_id' => $groupID, | |
218 | 'version' => 3, | |
be2fb01f | 219 | ]; |
6a488035 TO |
220 | |
221 | if ($op == 'add') { | |
222 | $groupParams['status'] = $params['is_active'] ? 'Added' : 'Pending'; | |
223 | civicrm_api('GroupContact', 'Create', $groupParams); | |
224 | } | |
225 | else { | |
226 | $groupParams['status'] = 'Removed'; | |
227 | civicrm_api('GroupContact', 'Delete', $groupParams); | |
228 | } | |
229 | ||
230 | if (CRM_Bridge_OG_Utils::aclEnabled() && | |
231 | $params['is_admin'] !== NULL | |
232 | ) { | |
233 | // get the group ID of the acl group | |
234 | $groupID = CRM_Bridge_OG_Utils::groupID(CRM_Bridge_OG_Utils::ogSyncACLName($params['og_id']), | |
235 | NULL, TRUE | |
236 | ); | |
237 | ||
be2fb01f | 238 | $groupParams = [ |
6a488035 TO |
239 | 'contact_id' => $contactID, |
240 | 'group_id' => $groupID, | |
241 | 'status' => $params['is_admin'] ? 'Added' : 'Removed', | |
242 | 'version' => 3, | |
be2fb01f | 243 | ]; |
6a488035 TO |
244 | |
245 | if ($params['is_admin']) { | |
246 | civicrm_api('GroupContact', 'Create', $groupParams); | |
247 | } | |
248 | else { | |
249 | civicrm_api('GroupContact', 'Delete', $groupParams); | |
250 | } | |
251 | } | |
252 | } | |
96025800 | 253 | |
6a488035 | 254 | } |