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