commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-new / civicrm / CRM / Bridge / OG / Drupal.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
32 * $Id$
33 *
34 */
35
36 /**
37 * d6 compatible?
38 */
39 class CRM_Bridge_OG_Drupal {
40
41 /**
42 * @param array $params
43 * @param $op
44 */
45 public static function nodeapi(&$params, $op) {
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
72 /**
73 * @param array $params
74 * @param $op
75 * @param null $groupType
76 */
77 public static function updateCiviGroup(&$params, $op, $groupType = NULL) {
78 $abort = FALSE;
79 $params['version'] = 3;
80 $params['id'] = CRM_Bridge_OG_Utils::groupID($params['source'], $params['title'], $abort);
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
102 /**
103 * @param array $aclParams
104 * @param $op
105 */
106 public static function updateCiviACLTables($aclParams, $op) {
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
119 /**
120 * @param array $params
121 * @param $op
122 */
123 public static function updateCiviACLRole(&$params, $op) {
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 ";
158 $queryParams = array(
159 1 => array($optionGroupID, 'Integer'),
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
167 /**
168 * @param array $params
169 * @param $op
170 */
171 public static function updateCiviACLEntityRole(&$params, $op) {
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
189 /**
190 * @param array $params
191 * @param $op
192 */
193 public static function updateCiviACL(&$params, $op) {
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';
207 $dao->entity_id = $params['acl_role_id'];
208 $dao->operation = 'Edit';
209
210 $dao->is_active = TRUE;
211 $dao->save();
212 $params['acl_id'] = $dao->id;
213 }
214
215 /**
216 * @param array $params
217 * @param $op
218 *
219 * @throws Exception
220 */
221 public static function og(&$params, $op) {
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 }
271
272 }