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