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 // We want to clear out any cached tokens.
37 // Editing a domain is so rare we can risk being heavy handed.
38 Civi
::cache('metadata')->clear();
39 Civi
::$statics[__CLASS__
]['current'] = NULL;
43 * Retrieve DB object and copy to defaults array.
45 * @param array $params
46 * Array of criteria values.
47 * @param array $defaults
48 * Array to be populated with found values.
51 * The DAO object, if found.
55 public static function retrieve($params, &$defaults) {
56 return self
::commonRetrieve(self
::class, $params, $defaults);
60 * Get the current domain.
62 * @return \CRM_Core_BAO_Domain
63 * @throws \CRM_Core_Exception
65 public static function getDomain() {
66 $domain = Civi
::$statics[__CLASS__
]['current'] ??
NULL;
68 $domain = new CRM_Core_BAO_Domain();
69 $domain->id
= CRM_Core_Config
::domainID();
70 if (!$domain->find(TRUE)) {
71 throw new CRM_Core_Exception('No domain in DB');
73 Civi
::$statics[__CLASS__
]['current'] = $domain;
79 * @param bool $skipUsingCache
83 * @throws \CRM_Core_Exception
85 public static function version($skipUsingCache = FALSE) {
86 if ($skipUsingCache) {
87 Civi
::$statics[__CLASS__
]['current'] = NULL;
90 return self
::getDomain()->version
;
94 * Is a database update required to apply latest schema changes.
98 * @throws \CRM_Core_Exception
100 public static function isDBUpdateRequired() {
101 $dbVersion = self
::version();
102 $codeVersion = CRM_Utils_System
::version();
103 return version_compare($dbVersion, $codeVersion) < 0;
107 * Checks that the current DB schema is at least $min version
109 * @param string|int $min
112 public static function isDBVersionAtLeast($min) {
113 return version_compare(self
::version(), $min, '>=');
119 protected static function getMissingDomainFromEmailMessage(): string {
120 $url = CRM_Utils_System
::url('civicrm/admin/options/from_email_address',
123 $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]);
128 * Get the location values of a domain.
130 * @return CRM_Core_BAO_Location[]|NULL
132 public function getLocationValues() {
133 if ($this->_location
== NULL) {
135 'contact_id' => $this->contact_id
,
137 $this->_location
= CRM_Core_BAO_Location
::getValues($params, TRUE);
139 if (empty($this->_location
)) {
140 $this->_location
= NULL;
143 return $this->_location
;
149 * @param array $params
152 * @return CRM_Core_DAO_Domain
153 * @throws \CRM_Core_Exception
155 public static function edit($params, $id): CRM_Core_DAO_Domain
{
157 return self
::writeRecord($params);
161 * Create or update domain.
163 * @param array $params
164 * @return CRM_Core_DAO_Domain
166 public static function create($params) {
167 return self
::writeRecord($params);
173 public static function multipleDomains() {
174 $session = CRM_Core_Session
::singleton();
176 $numberDomains = $session->get('numberDomains');
177 if (!$numberDomains) {
178 $query = 'SELECT count(*) from civicrm_domain';
179 $numberDomains = CRM_Core_DAO
::singleValueQuery($query);
180 $session->set('numberDomains', $numberDomains);
182 return $numberDomains > 1;
186 * @param bool $skipFatal
187 * @param bool $returnString
188 * If you are using this second parameter you probably are better
189 * calling `getFromEmail()` which will return an actual string.
192 * name & email for domain
194 * @throws \CRM_Core_Exception
196 public static function getNameAndEmail($skipFatal = FALSE, $returnString = FALSE) {
197 $fromEmailAddress = CRM_Core_OptionGroup
::values('from_email_address', NULL, NULL, NULL, ' AND is_default = 1');
198 if (!empty($fromEmailAddress)) {
200 // Return a string like: "Demonstrators Anonymous" <info@example.org>
201 return $fromEmailAddress;
203 foreach ($fromEmailAddress as $key => $value) {
204 $email = CRM_Utils_Mail
::pluckEmailFromHeader($value);
205 $fromArray = explode('"', $value);
206 $fromName = $fromArray[1] ??
NULL;
209 return [$fromName, $email];
216 $status = self
::getMissingDomainFromEmailMessage();
218 throw new CRM_Core_Exception($status);
222 * Get the domain email in a format suitable for using as the from address.
225 * @throws \CRM_Core_Exception
227 public static function getFromEmail(): string {
228 $email = CRM_Core_OptionGroup
::values('from_email_address', NULL, NULL, NULL, ' AND is_default = 1');
229 $email = current($email);
231 throw new CRM_Core_Exception(self
::getMissingDomainFromEmailMessage());
237 * @param int $contactID
239 * @return bool|null|object|string
241 * @throws \CRM_Core_Exception
243 public static function addContactToDomainGroup($contactID) {
244 $groupID = self
::getGroupId();
247 $contactIDs = [$contactID];
248 CRM_Contact_BAO_GroupContact
::addContactsToGroup($contactIDs, $groupID);
256 * @return bool|null|object|string
258 * @throws \CRM_Core_Exception
260 public static function getGroupId() {
261 static $groupID = NULL;
267 $domainGroupID = Civi
::settings()->get('domain_group_id');
268 $multisite = Civi
::settings()->get('is_enabled');
270 if ($domainGroupID) {
271 $groupID = $domainGroupID;
273 elseif ($multisite) {
274 // create a group with that of domain name
275 $title = self
::getDomain()->name
;
276 $groupID = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Group',
277 $title, 'id', 'title', TRUE
280 return $groupID ?
$groupID : FALSE;
284 * @param int $groupId
288 * @throws \CRM_Core_Exception
290 public static function isDomainGroup($groupId) {
291 $domainGroupID = self
::getGroupId();
292 return $domainGroupID == (bool) $groupId;
298 * @throws \CRM_Core_Exception
300 public static function getChildGroupIds() {
301 $domainGroupID = self
::getGroupId();
304 if ($domainGroupID) {
305 $childGrps = CRM_Contact_BAO_GroupNesting
::getChildGroupIds($domainGroupID);
306 $childGrps[] = $domainGroupID;
312 * Retrieve a list of contact-ids that belongs to current domain/site.
316 * @throws \CRM_Core_Exception
318 public static function getContactList() {
319 $siteGroups = CRM_Core_BAO_Domain
::getChildGroupIds();
322 if (!empty($siteGroups)) {
325 FROM civicrm_contact cc
326 INNER JOIN civicrm_group_contact gc ON
327 (gc.contact_id = cc.id AND gc.status = 'Added' AND gc.group_id IN (" . implode(',', $siteGroups) . "))";
329 $dao = CRM_Core_DAO
::executeQuery($query);
330 while ($dao->fetch()) {
331 $siteContacts[] = $dao->id
;
334 return $siteContacts;
338 * CRM-20308 & CRM-19657
339 * Return domain information / user information for the usage in receipts
340 * Try default from address then fall back to using logged in user details
342 * @throws \CiviCRM_API3_Exception
344 public static function getDefaultReceiptFrom() {
345 $domain = civicrm_api3('domain', 'getsingle', ['id' => CRM_Core_Config
::domainID()]);
346 if (!empty($domain['from_email'])) {
347 return [$domain['from_name'], $domain['from_email']];
349 if (!empty($domain['domain_email'])) {
350 return [$domain['name'], $domain['domain_email']];
355 if (!Civi
::settings()->get('allow_mail_from_logged_in_contact')) {
356 return [$userName, $userEmail];
359 $userID = CRM_Core_Session
::getLoggedInContactID();
360 if (!empty($userID)) {
361 [$userName, $userEmail] = CRM_Contact_BAO_Contact_Location
::getEmailDetails($userID);
363 // If still empty fall back to the logged in user details.
364 // return empty values no matter what.
365 return [$userName, $userEmail];
369 * Get address to be used for system from addresses when a reply is not expected.
371 public static function getNoReplyEmailAddress() {
372 $emailDomain = CRM_Core_BAO_MailSettings
::defaultDomain();
373 return "do-not-reply@$emailDomain";