| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | Copyright CiviCRM LLC. All rights reserved. | |
| 5 | | | |
| 6 | | This work is published under the GNU AGPLv3 license with some | |
| 7 | | permitted exceptions and without any warranty. For full license | |
| 8 | | and copyright information, see https://civicrm.org/licensing | |
| 9 | +--------------------------------------------------------------------+ |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * |
| 14 | * @package CRM |
| 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
| 16 | */ |
| 17 | class CRM_Bridge_OG_Utils { |
| 18 | const aclEnabled = 1, syncFromCiviCRM = 1; |
| 19 | |
| 20 | /** |
| 21 | * @return int |
| 22 | */ |
| 23 | public static function aclEnabled() { |
| 24 | return self::aclEnabled; |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Switch to stop synchronization from CiviCRM. |
| 29 | * This was always false before, and is always true |
| 30 | * now. Most likely, this needs to be a setting. |
| 31 | */ |
| 32 | public static function syncFromCiviCRM() { |
| 33 | // make sure that acls are not enabled |
| 34 | //RMT -- the following makes no f**king sense... |
| 35 | //return ! self::aclEnabled & self::syncFromCiviCRM; |
| 36 | return TRUE; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * @param int $ogID |
| 41 | * |
| 42 | * @return string |
| 43 | */ |
| 44 | public static function ogSyncName($ogID) { |
| 45 | return "OG Sync Group :{$ogID}:"; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * @param int $ogID |
| 50 | * |
| 51 | * @return string |
| 52 | */ |
| 53 | public static function ogSyncACLName($ogID) { |
| 54 | return "OG Sync Group ACL :{$ogID}:"; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * @param int $groupID |
| 59 | * |
| 60 | * @return int|null|string |
| 61 | * @throws Exception |
| 62 | */ |
| 63 | public static function ogID($groupID) { |
| 64 | $source = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', |
| 65 | $groupID, |
| 66 | 'source' |
| 67 | ); |
| 68 | |
| 69 | if (strpos($source, 'OG Sync Group') !== FALSE) { |
| 70 | preg_match('/:(\d+):$/', $source, $matches); |
| 71 | if (is_numeric($matches[1])) { |
| 72 | return $matches[1]; |
| 73 | } |
| 74 | } |
| 75 | return NULL; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * @param int $ufID |
| 80 | * |
| 81 | * @return int |
| 82 | * @throws Exception |
| 83 | */ |
| 84 | public static function contactID($ufID) { |
| 85 | $contactID = CRM_Core_BAO_UFMatch::getContactId($ufID); |
| 86 | if ($contactID) { |
| 87 | return $contactID; |
| 88 | } |
| 89 | // else synchronize contact for this user |
| 90 | |
| 91 | $account = user_load($ufID); |
| 92 | |
| 93 | CRM_Core_BAO_UFMatch::synchronizeUFMatch($account, $ufID, $account->mail, 'Drupal'); |
| 94 | $contactID = CRM_Core_BAO_UFMatch::getContactId($ufID); |
| 95 | if (!$contactID) { |
| 96 | throw new CRM_Core_Exception('no contact found'); |
| 97 | } |
| 98 | return $contactID; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * @param $source |
| 103 | * @param null $title |
| 104 | * @param bool $abort |
| 105 | * |
| 106 | * @return null|string |
| 107 | * @throws \CRM_Core_Exception |
| 108 | */ |
| 109 | public static function groupID($source, $title = NULL, $abort = FALSE) { |
| 110 | $query = " |
| 111 | SELECT id |
| 112 | FROM civicrm_group |
| 113 | WHERE source = %1"; |
| 114 | $params = [1 => [$source, 'String']]; |
| 115 | |
| 116 | if ($title) { |
| 117 | $query .= " OR title = %2"; |
| 118 | $params[2] = [$title, 'String']; |
| 119 | } |
| 120 | |
| 121 | $groupID = CRM_Core_DAO::singleValueQuery($query, $params); |
| 122 | if ($abort && |
| 123 | !$groupID |
| 124 | ) { |
| 125 | throw new CRM_Core_Exception('no group found'); |
| 126 | } |
| 127 | |
| 128 | return $groupID; |
| 129 | } |
| 130 | |
| 131 | } |