commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-new / civicrm / tools / extensions / org.civicrm.multisite / multisite.php
1 <?php
2
3 require_once 'multisite.civix.php';
4
5 /**
6 * Implementation of hook_civicrm_config
7 * @param $config
8 */
9 function multisite_civicrm_config(&$config) {
10 _multisite_civix_civicrm_config($config);
11 }
12
13 /**
14 * Implementation of hook_civicrm_xmlMenu
15 *
16 * @param $files array(string)
17 */
18 function multisite_civicrm_xmlMenu(&$files) {
19 _multisite_civix_civicrm_xmlMenu($files);
20 }
21
22 /**
23 * Implementation of hook_civicrm_install
24 */
25 function multisite_civicrm_install() {
26 return _multisite_civix_civicrm_install();
27 }
28
29 /**
30 * Implementation of hook_civicrm_uninstall
31 */
32 function multisite_civicrm_uninstall() {
33 return _multisite_civix_civicrm_uninstall();
34 }
35
36 /**
37 * Implementation of hook_civicrm_enable
38 */
39 function multisite_civicrm_enable() {
40 return _multisite_civix_civicrm_enable();
41 }
42
43 /**
44 * Implementation of hook_civicrm_disable
45 */
46 function multisite_civicrm_disable() {
47 return _multisite_civix_civicrm_disable();
48 }
49
50 /**
51 * Implementation of hook_civicrm_upgrade
52 *
53 * @param $op string, the type of operation being performed; 'check' or 'enqueue'
54 * @param $queue CRM_Queue_Queue, (for 'enqueue') the modifiable list of pending up upgrade tasks
55 *
56 * @return mixed based on op. for 'check', returns array(boolean) (TRUE if upgrades are pending)
57 * for 'enqueue', returns void
58 */
59 function multisite_civicrm_upgrade($op, CRM_Queue_Queue $queue = NULL) {
60 return _multisite_civix_civicrm_upgrade($op, $queue);
61 }
62
63 /**
64 * Implementation of hook_civicrm_managed
65 *
66 * Generate a list of entities to create/deactivate/delete when this module
67 * is installed, disabled, uninstalled.
68 * @param $entities
69 */
70 function multisite_civicrm_managed(&$entities) {
71 return _multisite_civix_civicrm_managed($entities);
72 }
73
74 /**
75 * * Implementation of hook_civicrm_post
76 *
77 * Current implemtation assumes shared user table for all sites -
78 * a more sophisticated version will be able to cope with a combination of shared user tables
79 * and separate user tables
80 *
81 * @param string $op
82 * @param string $objectName
83 * @param integer $objectId
84 * @param object $objectRef
85 */
86 function multisite_civicrm_post($op, $objectName, $objectId, &$objectRef) {
87 if ($op == 'edit' && $objectName == 'UFMatch') {
88 static $updating = FALSE;
89 if ($updating) {
90 return; // prevent recursion
91 }
92 $updating = TRUE;
93 $ufs = civicrm_api('uf_match', 'get', array(
94 'version' => 3,
95 'contact_id' => $objectRef->contact_id,
96 'uf_id' => $objectRef->uf_id,
97 'id' => array(
98 '!=' => $objectRef->id
99 )
100 ));
101 foreach ($ufs['values'] as $ufMatch) {
102 civicrm_api('UFMatch', 'create', array(
103 'version' => 3,
104 'id' => $ufMatch['id'],
105 'uf_name' => $objectRef->uf_name
106 ));
107 }
108 }
109 }
110 /**
111 *
112 * @param unknown_type $type
113 * @param unknown_type $contactID
114 * @param unknown_type $tableName
115 * @param unknown_type $allGroups
116 * @param unknown_type $currentGroups
117 */
118 function multisite_civicrm_aclGroup($type, $contactID, $tableName, &$allGroups, &$currentGroups) {
119 // only process saved search
120 if ($tableName != 'civicrm_saved_search') {
121 return;
122 }
123 $groupID = _multisite_get_domain_group();
124 if(!$groupID){
125 return;
126 }
127 $currentGroups = _multisite_get_all_child_groups($groupID, FALSE);
128 }
129
130 /**
131 *
132 * @param string $type
133 * @param array $tables tables to be included in query
134 * @param array $whereTables tables required for where portion of query
135 * @param integer $contactID contact for whom where clause is being composed
136 * @param string $where Where clause The completed clause will look like
137 * (multisiteGroupTable.group_id IN ("1,2,3,4,5") AND multisiteGroupTable.status IN ('Added') AND contact_a.is_deleted = 0)
138 * where the child groups are groups the contact is potentially a member of
139 *
140 */
141 function multisite_civicrm_aclWhereClause($type, &$tables, &$whereTables, &$contactID, &$where) {
142 if (! $contactID) {
143 return;
144 }
145 if(!_multisite_add_permissions($type)){
146 return;
147 }
148 $groupID = _multisite_get_domain_group();
149 if(!$groupID){
150 return;
151 }
152 $childOrgs = _multisite_get_all_child_groups($groupID);
153
154 if (! empty($childOrgs)) {
155 $groupTable = 'civicrm_group_contact';
156 $tables[$groupTable] = $whereTables[$groupTable] = "LEFT JOIN {$groupTable} multisiteGroupTable ON contact_a.id = multisiteGroupTable.contact_id";
157
158 $where = "(multisiteGroupTable.group_id IN (" . implode(',', $childOrgs) . ") AND multisiteGroupTable.status IN ('Added') AND contact_a.is_deleted = 0)";
159 }
160 }
161 /**
162 *
163 * @param array $permissions
164 */
165 function multisite_civicrm_permissions(&$permissions){
166 $prefix = ts('CiviCRM Multisite') . ': ';
167 $permissions = $permissions + array(
168 'view all contacts in domain' => $prefix . ts('view all contacts in domain'),
169 'edit all contacts in domain' => $prefix . ts('edit all contacts in domain'),
170 );
171 }
172
173 /**
174 * Implementation of hook_civicrm_config
175 * @param null $metaDataFolders
176 */
177 function multisite_civicrm_alterSettingsFolders(&$metaDataFolders = NULL){
178 _multisite_civix_civicrm_alterSettingsFolders($metaDataFolders);
179 }
180
181 /**
182 * Get all groups that are children of the parent group
183 * (iterate through all levels)
184 *
185 * @param integer $groupID
186 * @param boolean $includeParent
187 * @return array:child groups
188 */
189 function _multisite_get_all_child_groups($groupID, $includeParent = TRUE) {
190 static $_cache = array();
191
192 if (! array_key_exists($groupID, $_cache)) {
193 $childGroups = CRM_Core_BAO_Cache::getItem('descendant groups for an org', $groupID);
194
195 if (empty($childGroups)) {
196 $childGroups = array();
197
198 $query = "
199 SELECT children
200 FROM civicrm_group
201 WHERE children IS NOT NULL
202 AND id IN ";
203
204 if (! is_array($groupID)) {
205 $groupIDs = array(
206 $groupID
207 );
208 }
209
210 while (! empty($groupIDs)) {
211 $groupIDString = implode(',', $groupIDs);
212
213 $realQuery = $query . " ( $groupIDString )";
214 $dao = CRM_Core_DAO::executeQuery($realQuery);
215 $groupIDs = array();
216 while ($dao->fetch()) {
217 if ($dao->children) {
218 $childIDs = explode(',', $dao->children);
219 foreach ($childIDs as $childID) {
220 if (! array_key_exists($childID, $childGroups)) {
221 $childGroups[$childID] = 1;
222 $groupIDs[] = $childID;
223 }
224 }
225 }
226 }
227 }
228
229 CRM_Core_BAO_Cache::setItem($childGroups, 'descendant groups for an org', $groupID);
230 }
231 $_cache[$groupID] = $childGroups;
232 }
233
234 if ($includeParent || CRM_Core_Permission::check('administer Multiple Organizations')) {
235 return array_keys(array(
236 $groupID => 1
237 ) + $_cache[$groupID]);
238 }
239 else {
240 return array_keys($_cache[$groupID]);
241 }
242 }
243
244 /**
245 *
246 * @param int $permission
247 *
248 * @return NULL|integer $groupID
249 */
250 function _multisite_get_domain_group($permission = 1) {
251 $groupID = CRM_Core_BAO_Domain::getGroupId();
252 if(empty($groupID) || !is_numeric($groupID)){
253 /* domain group not defined - we could let people know but
254 * it is acceptable for some domains not to be in the multisite
255 * so should probably check enabled before we spring an error
256 */
257 return NULL;
258 }
259 // We will check for the possiblility of the acl_enabled setting being deliberately set to 0
260 if($permission){
261 $aclsEnabled = civicrm_api('setting', 'getvalue', array(
262 'version' => 3,
263 'name' => 'multisite_acl_enabled',
264 'group' => 'Multi Site Preferences')
265 );
266 if(is_numeric($aclsEnabled) && !$aclsEnabled){
267 return NULL;
268 }
269 }
270
271 return $groupID;
272 }
273
274 /**
275 * Should we be adding ACLs in this instance. If we don't add them the user
276 * will not be able to see anything. We check if the install has the permissions
277 * hook implemented correctly & if so only allow view & edit based on those.
278 *
279 * Otherwise all users get these permissions added (4.2 vs 4.3 / other CMS issues)
280 *
281 * @param integer $type type of operation
282 *
283 * @return bool
284 */
285 function _multisite_add_permissions($type){
286 $hookclass = 'CRM_Utils_Hook';
287 if(!method_exists($hookclass, 'permissions')){
288 // ie. unpatched 4.2 so we can't check for extra declared permissions
289 // & default to applying this to all
290 return TRUE;
291 }
292 // extra check to make sure that hook is properly implemented
293 // if not we won't check for it. NB view all contacts in domain is enough checking
294 $declaredPermissions = CRM_Core_Permission::getCorePermissions();
295 if(!array_key_exists('view all contacts in domain', $declaredPermissions)){
296 drupal_set_message('here');
297 return TRUE;
298 }
299
300 if(CRM_ACL_BAO_ACL::matchType($type, 'View') &&
301 CRM_Core_Permission::check('view all contacts in domain')) {
302 return TRUE;
303 }
304
305 if(CRM_ACL_BAO_ACL::matchType($type, 'Edit') &&
306 CRM_Core_Permission::check('edit all contacts in domain')) {
307 return TRUE;
308 }
309 return FALSE;
310 }
311