Merge pull request #13006 from seamuslee001/lab_core_472
[civicrm-core.git] / CRM / Bridge / OG / Drupal.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
8c9251b3 6 | Copyright CiviCRM LLC (c) 2004-2018 |
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
8c9251b3 31 * @copyright CiviCRM LLC (c) 2004-2018
6a488035
TO
32 */
33
c866eb5f
TO
34/**
35 * d6 compatible?
36 */
6a488035
TO
37class CRM_Bridge_OG_Drupal {
38
e0ef6999 39 /**
c490a46a 40 * @param array $params
e0ef6999
EM
41 * @param $op
42 */
00be9182 43 public static function nodeapi(&$params, $op) {
6a488035
TO
44
45 $transaction = new CRM_Core_Transaction();
46
47 // first create or update the CiviCRM group
48 $groupParams = $params;
49 $groupParams['source'] = CRM_Bridge_OG_Utils::ogSyncName($params['og_id']);
50 $groupParams['group_type'] = array('2' => 1);
51 self::updateCiviGroup($groupParams, $op);
52
53 if (CRM_Bridge_OG_Utils::aclEnabled()) {
54 // next create or update the CiviCRM ACL group
55 $aclParams = $params;
56 $aclParams['name'] = $aclParams['title'] = "{$aclParams['name']}: Administrator";
57 $aclParams['source'] = CRM_Bridge_OG_Utils::ogSyncACLName($params['og_id']);
58 $aclParams['group_type'] = array('1');
59 self::updateCiviGroup($aclParams, $op);
60
61 $aclParams['acl_group_id'] = $aclParams['group_id'];
62 $aclParams['civicrm_group_id'] = $groupParams['group_id'];
63
64 self::updateCiviACLTables($aclParams, $op);
65 }
66
67 $transaction->commit();
68 }
69
e0ef6999 70 /**
c490a46a 71 * @param array $params
e0ef6999
EM
72 * @param $op
73 * @param null $groupType
74 */
00be9182 75 public static function updateCiviGroup(&$params, $op, $groupType = NULL) {
353ffa53 76 $abort = FALSE;
6a488035 77 $params['version'] = 3;
353ffa53 78 $params['id'] = CRM_Bridge_OG_Utils::groupID($params['source'], $params['title'], $abort);
6a488035
TO
79
80 if ($op == 'add') {
81 if ($groupType) {
82 $params['group_type'] = $groupType;
83 }
84
85 $group = civicrm_api('group', 'create', $params);
86 if (!civicrm_error($group)) {
87 $params['group_id'] = $group['id'];
88 }
89 }
90 else {
91 // do this only if we have a valid id
92 if ($params['id']) {
93 CRM_Contact_BAO_Group::discard($params['id']);
94 $params['group_id'] = $params['id'];
95 }
96 }
97 unset($params['id']);
98 }
99
e0ef6999 100 /**
100fef9d 101 * @param array $aclParams
e0ef6999
EM
102 * @param $op
103 */
00be9182 104 public static function updateCiviACLTables($aclParams, $op) {
6a488035
TO
105 if ($op == 'delete') {
106 self::updateCiviACL($aclParams, $op);
107 self::updateCiviACLEntityRole($aclParams, $op);
108 self::updateCiviACLRole($aclParams, $op);
109 }
110 else {
111 self::updateCiviACLRole($aclParams, $op);
112 self::updateCiviACLEntityRole($aclParams, $op);
113 self::updateCiviACL($aclParams, $op);
114 }
115 }
116
e0ef6999 117 /**
c490a46a 118 * @param array $params
e0ef6999
EM
119 * @param $op
120 */
00be9182 121 public static function updateCiviACLRole(&$params, $op) {
6a488035
TO
122
123 $optionGroupID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup',
124 'acl_role',
125 'id',
126 'name'
127 );
128
129 $dao = new CRM_Core_DAO_OptionValue();
130 $dao->option_group_id = $optionGroupID;
131 $dao->description = $params['source'];
132
133 if ($op == 'delete') {
134 $dao->delete();
135 return;
136 }
137
138 $dao->label = $params['title'];
139 $dao->is_active = 1;
140
141 $weightParams = array('option_group_id' => $optionGroupID);
142 $dao->weight = CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue',
143 $weightParams
144 );
145 $dao->value = CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue',
146 $weightParams,
147 'value'
148 );
149
150 $query = "
151SELECT v.id
152 FROM civicrm_option_value v
153 WHERE v.option_group_id = %1
154 AND v.description = %2
155";
3e19efc1 156 $queryParams = array(
353ffa53 157 1 => array($optionGroupID, 'Integer'),
6a488035
TO
158 2 => array($params['source'], 'String'),
159 );
160 $dao->id = CRM_Core_DAO::singleValueQuery($query, $queryParams);
161 $dao->save();
162 $params['acl_role_id'] = $dao->value;
163 }
164
e0ef6999 165 /**
c490a46a 166 * @param array $params
e0ef6999
EM
167 * @param $op
168 */
00be9182 169 public static function updateCiviACLEntityRole(&$params, $op) {
6a488035
TO
170 $dao = new CRM_ACL_DAO_EntityRole();
171
172 $dao->entity_table = 'civicrm_group';
173 $dao->entity_id = $params['acl_group_id'];
174 if ($op == 'delete') {
175 $dao->delete();
176 return;
177 }
178
179 $dao->acl_role_id = $params['acl_role_id'];
180
181 $dao->find(TRUE);
182 $dao->is_active = TRUE;
183 $dao->save();
184 $params['acl_entity_role_id'] = $dao->id;
185 }
186
e0ef6999 187 /**
c490a46a 188 * @param array $params
e0ef6999
EM
189 * @param $op
190 */
00be9182 191 public static function updateCiviACL(&$params, $op) {
6a488035
TO
192 $dao = new CRM_ACL_DAO_ACL();
193
194 $dao->object_table = 'civicrm_saved_search';
195 $dao->object_id = $params['civicrm_group_id'];
196
197 if ($op == 'delete') {
198 $dao->delete();
199 return;
200 }
201
202 $dao->find(TRUE);
203
204 $dao->entity_table = 'civicrm_acl_role';
353ffa53
TO
205 $dao->entity_id = $params['acl_role_id'];
206 $dao->operation = 'Edit';
6a488035
TO
207
208 $dao->is_active = TRUE;
209 $dao->save();
210 $params['acl_id'] = $dao->id;
211 }
212
e0ef6999 213 /**
c490a46a 214 * @param array $params
e0ef6999
EM
215 * @param $op
216 *
217 * @throws Exception
218 */
00be9182 219 public static function og(&$params, $op) {
6a488035
TO
220
221 $contactID = CRM_Bridge_OG_Utils::contactID($params['uf_id']);
222 if (!$contactID) {
223 CRM_Core_Error::fatal();
224 }
225
226 // get the group id of this OG
227 $groupID = CRM_Bridge_OG_Utils::groupID(CRM_Bridge_OG_Utils::ogSyncName($params['og_id']),
228 NULL, TRUE
229 );
230
231 $groupParams = array(
232 'contact_id' => $contactID,
233 'group_id' => $groupID,
234 'version' => 3,
235 );
236
237 if ($op == 'add') {
238 $groupParams['status'] = $params['is_active'] ? 'Added' : 'Pending';
239 civicrm_api('GroupContact', 'Create', $groupParams);
240 }
241 else {
242 $groupParams['status'] = 'Removed';
243 civicrm_api('GroupContact', 'Delete', $groupParams);
244 }
245
246 if (CRM_Bridge_OG_Utils::aclEnabled() &&
247 $params['is_admin'] !== NULL
248 ) {
249 // get the group ID of the acl group
250 $groupID = CRM_Bridge_OG_Utils::groupID(CRM_Bridge_OG_Utils::ogSyncACLName($params['og_id']),
251 NULL, TRUE
252 );
253
254 $groupParams = array(
255 'contact_id' => $contactID,
256 'group_id' => $groupID,
257 'status' => $params['is_admin'] ? 'Added' : 'Removed',
258 'version' => 3,
259 );
260
261 if ($params['is_admin']) {
262 civicrm_api('GroupContact', 'Create', $groupParams);
263 }
264 else {
265 civicrm_api('GroupContact', 'Delete', $groupParams);
266 }
267 }
268 }
96025800 269
6a488035 270}