3 require_once 'multisite.civix.php';
6 * Implementation of hook_civicrm_config
9 function multisite_civicrm_config(&$config) {
10 _multisite_civix_civicrm_config($config);
14 * Implementation of hook_civicrm_xmlMenu
16 * @param $files array(string)
18 function multisite_civicrm_xmlMenu(&$files) {
19 _multisite_civix_civicrm_xmlMenu($files);
23 * Implementation of hook_civicrm_install
25 function multisite_civicrm_install() {
26 return _multisite_civix_civicrm_install();
30 * Implementation of hook_civicrm_uninstall
32 function multisite_civicrm_uninstall() {
33 return _multisite_civix_civicrm_uninstall();
37 * Implementation of hook_civicrm_enable
39 function multisite_civicrm_enable() {
40 return _multisite_civix_civicrm_enable();
44 * Implementation of hook_civicrm_disable
46 function multisite_civicrm_disable() {
47 return _multisite_civix_civicrm_disable();
51 * Implementation of hook_civicrm_upgrade
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
56 * @return mixed based on op. for 'check', returns array(boolean) (TRUE if upgrades are pending)
57 * for 'enqueue', returns void
59 function multisite_civicrm_upgrade($op, CRM_Queue_Queue
$queue = NULL) {
60 return _multisite_civix_civicrm_upgrade($op, $queue);
64 * Implementation of hook_civicrm_managed
66 * Generate a list of entities to create/deactivate/delete when this module
67 * is installed, disabled, uninstalled.
70 function multisite_civicrm_managed(&$entities) {
71 return _multisite_civix_civicrm_managed($entities);
75 * * Implementation of hook_civicrm_post
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
82 * @param string $objectName
83 * @param integer $objectId
84 * @param object $objectRef
86 function multisite_civicrm_post($op, $objectName, $objectId, &$objectRef) {
87 if ($op == 'edit' && $objectName == 'UFMatch') {
88 static $updating = FALSE;
90 return; // prevent recursion
93 $ufs = civicrm_api('uf_match', 'get', array(
95 'contact_id' => $objectRef->contact_id
,
96 'uf_id' => $objectRef->uf_id
,
98 '!=' => $objectRef->id
101 foreach ($ufs['values'] as $ufMatch) {
102 civicrm_api('UFMatch', 'create', array(
104 'id' => $ufMatch['id'],
105 'uf_name' => $objectRef->uf_name
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
118 function multisite_civicrm_aclGroup($type, $contactID, $tableName, &$allGroups, &$currentGroups) {
119 // only process saved search
120 if ($tableName != 'civicrm_saved_search') {
123 $groupID = _multisite_get_domain_group();
127 $currentGroups = _multisite_get_all_child_groups($groupID, FALSE);
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
141 function multisite_civicrm_aclWhereClause($type, &$tables, &$whereTables, &$contactID, &$where) {
145 if(!_multisite_add_permissions($type)){
148 $groupID = _multisite_get_domain_group();
152 $childOrgs = _multisite_get_all_child_groups($groupID);
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";
158 $where = "(multisiteGroupTable.group_id IN (" . implode(',', $childOrgs) . ") AND multisiteGroupTable.status IN ('Added') AND contact_a.is_deleted = 0)";
163 * @param array $permissions
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'),
174 * Implementation of hook_civicrm_config
175 * @param null $metaDataFolders
177 function multisite_civicrm_alterSettingsFolders(&$metaDataFolders = NULL){
178 _multisite_civix_civicrm_alterSettingsFolders($metaDataFolders);
182 * Get all groups that are children of the parent group
183 * (iterate through all levels)
185 * @param integer $groupID
186 * @param boolean $includeParent
187 * @return array:child groups
189 function _multisite_get_all_child_groups($groupID, $includeParent = TRUE) {
190 static $_cache = array();
192 if (! array_key_exists($groupID, $_cache)) {
193 $childGroups = CRM_Core_BAO_Cache
::getItem('descendant groups for an org', $groupID);
195 if (empty($childGroups)) {
196 $childGroups = array();
201 WHERE children IS NOT NULL
204 if (! is_array($groupID)) {
210 while (! empty($groupIDs)) {
211 $groupIDString = implode(',', $groupIDs);
213 $realQuery = $query . " ( $groupIDString )";
214 $dao = CRM_Core_DAO
::executeQuery($realQuery);
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;
229 CRM_Core_BAO_Cache
::setItem($childGroups, 'descendant groups for an org', $groupID);
231 $_cache[$groupID] = $childGroups;
234 if ($includeParent || CRM_Core_Permission
::check('administer Multiple Organizations')) {
235 return array_keys(array(
237 ) +
$_cache[$groupID]);
240 return array_keys($_cache[$groupID]);
246 * @param int $permission
248 * @return NULL|integer $groupID
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
259 // We will check for the possiblility of the acl_enabled setting being deliberately set to 0
261 $aclsEnabled = civicrm_api('setting', 'getvalue', array(
263 'name' => 'multisite_acl_enabled',
264 'group' => 'Multi Site Preferences')
266 if(is_numeric($aclsEnabled) && !$aclsEnabled){
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.
279 * Otherwise all users get these permissions added (4.2 vs 4.3 / other CMS issues)
281 * @param integer $type type of operation
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
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');
300 if(CRM_ACL_BAO_ACL
::matchType($type, 'View') &&
301 CRM_Core_Permission
::check('view all contacts in domain')) {
305 if(CRM_ACL_BAO_ACL
::matchType($type, 'Edit') &&
306 CRM_Core_Permission
::check('edit all contacts in domain')) {