3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
21 class CRM_Core_BAO_Domain
extends CRM_Core_DAO_Domain
{
24 * Cache for a domain's location array
27 private $_location = NULL;
30 * Flushes the cache set by getDomain.
32 * @see CRM_Core_BAO_Domain::getDomain()
33 * @param CRM_Core_DAO_Domain $domain
35 public static function onPostSave($domain) {
36 Civi
::$statics[__CLASS__
]['current'] = NULL;
40 * Fetch object based on array of properties.
42 * @param array $params
43 * (reference ) an assoc array of name/value pairs.
44 * @param array $defaults
45 * (reference ) an assoc array to hold the flattened values.
47 * @return CRM_Core_DAO_Domain
49 public static function retrieve(&$params, &$defaults) {
50 return CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_Domain', $params, $defaults);
54 * Get the current domain.
56 * @return \CRM_Core_BAO_Domain
57 * @throws \CRM_Core_Exception
59 public static function getDomain() {
60 $domain = Civi
::$statics[__CLASS__
]['current'] ??
NULL;
62 $domain = new CRM_Core_BAO_Domain();
63 $domain->id
= CRM_Core_Config
::domainID();
64 if (!$domain->find(TRUE)) {
65 throw new CRM_Core_Exception('No domain in DB');
67 Civi
::$statics[__CLASS__
]['current'] = $domain;
73 * @param bool $skipUsingCache
77 * @throws \CRM_Core_Exception
79 public static function version($skipUsingCache = FALSE) {
80 if ($skipUsingCache) {
81 Civi
::$statics[__CLASS__
]['current'] = NULL;
84 return self
::getDomain()->version
;
88 * Is a database update required to apply latest schema changes.
92 * @throws \CRM_Core_Exception
94 public static function isDBUpdateRequired() {
95 $dbVersion = self
::version();
96 $codeVersion = CRM_Utils_System
::version();
97 return version_compare($dbVersion, $codeVersion) < 0;
101 * Checks that the current DB schema is at least $min version
103 * @param string|number $min
106 public static function isDBVersionAtLeast($min) {
107 return version_compare(self
::version(), $min, '>=');
111 * Get the location values of a domain.
113 * @return CRM_Core_BAO_Location[]|NULL
115 public function getLocationValues() {
116 if ($this->_location
== NULL) {
118 'contact_id' => $this->contact_id
,
120 $this->_location
= CRM_Core_BAO_Location
::getValues($params, TRUE);
122 if (empty($this->_location
)) {
123 $this->_location
= NULL;
126 return $this->_location
;
132 * @param array $params
135 * @return CRM_Core_DAO_Domain
137 public static function edit($params, $id) {
139 return self
::writeRecord($params);
143 * Create or update domain.
145 * @param array $params
146 * @return CRM_Core_DAO_Domain
148 public static function create($params) {
149 return self
::writeRecord($params);
155 public static function multipleDomains() {
156 $session = CRM_Core_Session
::singleton();
158 $numberDomains = $session->get('numberDomains');
159 if (!$numberDomains) {
160 $query = 'SELECT count(*) from civicrm_domain';
161 $numberDomains = CRM_Core_DAO
::singleValueQuery($query);
162 $session->set('numberDomains', $numberDomains);
164 return $numberDomains > 1;
168 * @param bool $skipFatal
169 * @param bool $returnString
172 * name & email for domain
174 * @throws \CRM_Core_Exception
176 public static function getNameAndEmail($skipFatal = FALSE, $returnString = FALSE) {
177 $fromEmailAddress = CRM_Core_OptionGroup
::values('from_email_address', NULL, NULL, NULL, ' AND is_default = 1');
178 if (!empty($fromEmailAddress)) {
180 // Return a string like: "Demonstrators Anonymous" <info@example.org>
181 return $fromEmailAddress;
183 foreach ($fromEmailAddress as $key => $value) {
184 $email = CRM_Utils_Mail
::pluckEmailFromHeader($value);
185 $fromArray = explode('"', $value);
186 $fromName = $fromArray[1] ??
NULL;
189 return [$fromName, $email];
196 $url = CRM_Utils_System
::url('civicrm/admin/options/from_email_address',
199 $status = ts("There is no valid default from email address configured for the domain. You can configure here <a href='%1'>Configure From Email Address.</a>", [1 => $url]);
201 throw new CRM_Core_Exception($status);
205 * @param int $contactID
207 * @return bool|null|object|string
209 * @throws \CRM_Core_Exception
211 public static function addContactToDomainGroup($contactID) {
212 $groupID = self
::getGroupId();
215 $contactIDs = [$contactID];
216 CRM_Contact_BAO_GroupContact
::addContactsToGroup($contactIDs, $groupID);
224 * @return bool|null|object|string
226 * @throws \CRM_Core_Exception
228 public static function getGroupId() {
229 static $groupID = NULL;
235 $domainGroupID = Civi
::settings()->get('domain_group_id');
236 $multisite = Civi
::settings()->get('is_enabled');
238 if ($domainGroupID) {
239 $groupID = $domainGroupID;
241 elseif ($multisite) {
242 // create a group with that of domain name
243 $title = self
::getDomain()->name
;
244 $groupID = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Group',
245 $title, 'id', 'title', TRUE
248 return $groupID ?
$groupID : FALSE;
252 * @param int $groupId
256 * @throws \CRM_Core_Exception
258 public static function isDomainGroup($groupId) {
259 $domainGroupID = self
::getGroupId();
260 return $domainGroupID == (bool) $groupId;
266 * @throws \CRM_Core_Exception
268 public static function getChildGroupIds() {
269 $domainGroupID = self
::getGroupId();
272 if ($domainGroupID) {
273 $childGrps = CRM_Contact_BAO_GroupNesting
::getChildGroupIds($domainGroupID);
274 $childGrps[] = $domainGroupID;
280 * Retrieve a list of contact-ids that belongs to current domain/site.
284 * @throws \CRM_Core_Exception
286 public static function getContactList() {
287 $siteGroups = CRM_Core_BAO_Domain
::getChildGroupIds();
290 if (!empty($siteGroups)) {
293 FROM civicrm_contact cc
294 INNER JOIN civicrm_group_contact gc ON
295 (gc.contact_id = cc.id AND gc.status = 'Added' AND gc.group_id IN (" . implode(',', $siteGroups) . "))";
297 $dao = CRM_Core_DAO
::executeQuery($query);
298 while ($dao->fetch()) {
299 $siteContacts[] = $dao->id
;
302 return $siteContacts;
306 * CRM-20308 & CRM-19657
307 * Return domain information / user information for the usage in receipts
308 * Try default from address then fall back to using logged in user details
310 * @throws \CiviCRM_API3_Exception
312 public static function getDefaultReceiptFrom() {
313 $domain = civicrm_api3('domain', 'getsingle', ['id' => CRM_Core_Config
::domainID()]);
314 if (!empty($domain['from_email'])) {
315 return [$domain['from_name'], $domain['from_email']];
317 if (!empty($domain['domain_email'])) {
318 return [$domain['name'], $domain['domain_email']];
323 if (!Civi
::settings()->get('allow_mail_from_logged_in_contact')) {
324 return [$userName, $userEmail];
327 $userID = CRM_Core_Session
::getLoggedInContactID();
328 if (!empty($userID)) {
329 list($userName, $userEmail) = CRM_Contact_BAO_Contact_Location
::getEmailDetails($userID);
331 // If still empty fall back to the logged in user details.
332 // return empty values no matter what.
333 return [$userName, $userEmail];
337 * Get address to be used for system from addresses when a reply is not expected.
339 public static function getNoReplyEmailAddress() {
340 $emailDomain = CRM_Core_BAO_MailSettings
::defaultDomain();
341 return "do-not-reply@$emailDomain";