fix version in header
[civicrm-core.git] / CRM / Bridge / OG / Drupal.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2014
32 * $Id$
33 *
34 */
35
36 // d6 compatible
37 class CRM_Bridge_OG_Drupal {
38
39 /**
40 * @param array $params
41 * @param $op
42 */
43 static function nodeapi(&$params, $op) {
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
70 /**
71 * @param array $params
72 * @param $op
73 * @param null $groupType
74 */
75 static function updateCiviGroup(&$params, $op, $groupType = NULL) {
76 $abort = false;
77 $params['version'] = 3;
78 $params['id'] = CRM_Bridge_OG_Utils::groupID($params['source'], $params['title'], $abort);
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
100 /**
101 * @param array $aclParams
102 * @param $op
103 */
104 static function updateCiviACLTables($aclParams, $op) {
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
117 /**
118 * @param array $params
119 * @param $op
120 */
121 static function updateCiviACLRole(&$params, $op) {
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 = "
151 SELECT v.id
152 FROM civicrm_option_value v
153 WHERE v.option_group_id = %1
154 AND v.description = %2
155 ";
156 $queryParams = array(1 => array($optionGroupID, 'Integer'),
157 2 => array($params['source'], 'String'),
158 );
159 $dao->id = CRM_Core_DAO::singleValueQuery($query, $queryParams);
160 $dao->save();
161 $params['acl_role_id'] = $dao->value;
162 }
163
164 /**
165 * @param array $params
166 * @param $op
167 */
168 static function updateCiviACLEntityRole(&$params, $op) {
169 $dao = new CRM_ACL_DAO_EntityRole();
170
171 $dao->entity_table = 'civicrm_group';
172 $dao->entity_id = $params['acl_group_id'];
173 if ($op == 'delete') {
174 $dao->delete();
175 return;
176 }
177
178 $dao->acl_role_id = $params['acl_role_id'];
179
180 $dao->find(TRUE);
181 $dao->is_active = TRUE;
182 $dao->save();
183 $params['acl_entity_role_id'] = $dao->id;
184 }
185
186 /**
187 * @param array $params
188 * @param $op
189 */
190 static function updateCiviACL(&$params, $op) {
191 $dao = new CRM_ACL_DAO_ACL();
192
193 $dao->object_table = 'civicrm_saved_search';
194 $dao->object_id = $params['civicrm_group_id'];
195
196 if ($op == 'delete') {
197 $dao->delete();
198 return;
199 }
200
201 $dao->find(TRUE);
202
203 $dao->entity_table = 'civicrm_acl_role';
204 $dao->entity_id = $params['acl_role_id'];
205 $dao->operation = 'Edit';
206
207 $dao->is_active = TRUE;
208 $dao->save();
209 $params['acl_id'] = $dao->id;
210 }
211
212 /**
213 * @param array $params
214 * @param $op
215 *
216 * @throws Exception
217 */
218 static function og(&$params, $op) {
219
220 $contactID = CRM_Bridge_OG_Utils::contactID($params['uf_id']);
221 if (!$contactID) {
222 CRM_Core_Error::fatal();
223 }
224
225 // get the group id of this OG
226 $groupID = CRM_Bridge_OG_Utils::groupID(CRM_Bridge_OG_Utils::ogSyncName($params['og_id']),
227 NULL, TRUE
228 );
229
230 $groupParams = array(
231 'contact_id' => $contactID,
232 'group_id' => $groupID,
233 'version' => 3,
234 );
235
236 if ($op == 'add') {
237 $groupParams['status'] = $params['is_active'] ? 'Added' : 'Pending';
238 civicrm_api('GroupContact', 'Create', $groupParams);
239 }
240 else {
241 $groupParams['status'] = 'Removed';
242 civicrm_api('GroupContact', 'Delete', $groupParams);
243 }
244
245 if (CRM_Bridge_OG_Utils::aclEnabled() &&
246 $params['is_admin'] !== NULL
247 ) {
248 // get the group ID of the acl group
249 $groupID = CRM_Bridge_OG_Utils::groupID(CRM_Bridge_OG_Utils::ogSyncACLName($params['og_id']),
250 NULL, TRUE
251 );
252
253 $groupParams = array(
254 'contact_id' => $contactID,
255 'group_id' => $groupID,
256 'status' => $params['is_admin'] ? 'Added' : 'Removed',
257 'version' => 3,
258 );
259
260 if ($params['is_admin']) {
261 civicrm_api('GroupContact', 'Create', $groupParams);
262 }
263 else {
264 civicrm_api('GroupContact', 'Delete', $groupParams);
265 }
266 }
267 }
268 }
269