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 the current domain object.
27 public static $_domain = NULL;
30 * Cache for a domain's location array
33 private $_location = NULL;
36 * Fetch object based on array of properties.
38 * @param array $params
39 * (reference ) an assoc array of name/value pairs.
40 * @param array $defaults
41 * (reference ) an assoc array to hold the flattened values.
43 * @return CRM_Core_DAO_Domain
45 public static function retrieve(&$params, &$defaults) {
46 return CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_Domain', $params, $defaults);
54 * @return \CRM_Core_BAO_Domain
55 * @throws \CRM_Core_Exception
57 public static function getDomain($reset = NULL) {
58 static $domain = NULL;
59 if (!$domain ||
$reset) {
60 $domain = new CRM_Core_BAO_Domain();
61 $domain->id
= CRM_Core_Config
::domainID();
62 if (!$domain->find(TRUE)) {
63 throw new CRM_Core_Exception('No domain in DB');
70 * @param bool $skipUsingCache
74 * @throws \CRM_Core_Exception
76 public static function version($skipUsingCache = FALSE) {
77 return CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Domain',
78 CRM_Core_Config
::domainID(),
86 * Is a database update required to apply latest schema changes.
90 * @throws \CRM_Core_Exception
92 public static function isDBUpdateRequired() {
93 $dbVersion = CRM_Core_BAO_Domain
::version();
94 $codeVersion = CRM_Utils_System
::version();
95 return version_compare($dbVersion, $codeVersion) < 0;
99 * Checks that the current DB schema is at least $min version
101 * @param string|number $min
104 public static function isDBVersionAtLeast($min) {
105 return version_compare(self
::version(), $min, '>=');
109 * Get the location values of a domain.
112 * Location::getValues
114 * @throws \CRM_Core_Exception
116 public function &getLocationValues() {
117 if ($this->_location
== NULL) {
118 $domain = self
::getDomain(NULL);
120 'contact_id' => $domain->contact_id
,
122 $this->_location
= CRM_Core_BAO_Location
::getValues($params, TRUE);
124 if (empty($this->_location
)) {
125 $this->_location
= NULL;
128 return $this->_location
;
134 * @param array $params
137 * @return CRM_Core_DAO_Domain
139 public static function edit($params, $id) {
141 return self
::writeRecord($params);
145 * Create or update domain.
147 * @param array $params
148 * @return CRM_Core_DAO_Domain
150 public static function create($params) {
151 return self
::writeRecord($params);
157 public static function multipleDomains() {
158 $session = CRM_Core_Session
::singleton();
160 $numberDomains = $session->get('numberDomains');
161 if (!$numberDomains) {
162 $query = 'SELECT count(*) from civicrm_domain';
163 $numberDomains = CRM_Core_DAO
::singleValueQuery($query);
164 $session->set('numberDomains', $numberDomains);
166 return $numberDomains > 1;
170 * @param bool $skipFatal
171 * @param bool $returnString
174 * name & email for domain
176 * @throws \CRM_Core_Exception
178 public static function getNameAndEmail($skipFatal = FALSE, $returnString = FALSE) {
179 $fromEmailAddress = CRM_Core_OptionGroup
::values('from_email_address', NULL, NULL, NULL, ' AND is_default = 1');
180 if (!empty($fromEmailAddress)) {
182 // Return a string like: "Demonstrators Anonymous" <info@example.org>
183 return $fromEmailAddress;
185 foreach ($fromEmailAddress as $key => $value) {
186 $email = CRM_Utils_Mail
::pluckEmailFromHeader($value);
187 $fromArray = explode('"', $value);
188 $fromName = $fromArray[1] ??
NULL;
191 return [$fromName, $email];
198 $url = CRM_Utils_System
::url('civicrm/admin/options/from_email_address',
201 $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]);
203 throw new CRM_Core_Exception($status);
207 * @param int $contactID
209 * @return bool|null|object|string
211 * @throws \CRM_Core_Exception
213 public static function addContactToDomainGroup($contactID) {
214 $groupID = self
::getGroupId();
217 $contactIDs = [$contactID];
218 CRM_Contact_BAO_GroupContact
::addContactsToGroup($contactIDs, $groupID);
226 * @return bool|null|object|string
228 * @throws \CRM_Core_Exception
230 public static function getGroupId() {
231 static $groupID = NULL;
237 $domainGroupID = Civi
::settings()->get('domain_group_id');
238 $multisite = Civi
::settings()->get('is_enabled');
240 if ($domainGroupID) {
241 $groupID = $domainGroupID;
243 elseif ($multisite) {
244 // create a group with that of domain name
245 $title = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Domain',
246 CRM_Core_Config
::domainID(), 'name'
248 $groupID = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Group',
249 $title, 'id', 'title', TRUE
252 return $groupID ?
$groupID : FALSE;
256 * @param int $groupId
260 * @throws \CRM_Core_Exception
262 public static function isDomainGroup($groupId) {
263 $domainGroupID = self
::getGroupId();
264 return $domainGroupID == (bool) $groupId;
270 * @throws \CRM_Core_Exception
272 public static function getChildGroupIds() {
273 $domainGroupID = self
::getGroupId();
276 if ($domainGroupID) {
277 $childGrps = CRM_Contact_BAO_GroupNesting
::getChildGroupIds($domainGroupID);
278 $childGrps[] = $domainGroupID;
284 * Retrieve a list of contact-ids that belongs to current domain/site.
288 * @throws \CRM_Core_Exception
290 public static function getContactList() {
291 $siteGroups = CRM_Core_BAO_Domain
::getChildGroupIds();
294 if (!empty($siteGroups)) {
297 FROM civicrm_contact cc
298 INNER JOIN civicrm_group_contact gc ON
299 (gc.contact_id = cc.id AND gc.status = 'Added' AND gc.group_id IN (" . implode(',', $siteGroups) . "))";
301 $dao = CRM_Core_DAO
::executeQuery($query);
302 while ($dao->fetch()) {
303 $siteContacts[] = $dao->id
;
306 return $siteContacts;
310 * CRM-20308 & CRM-19657
311 * Return domain information / user information for the usage in receipts
312 * Try default from address then fall back to using logged in user details
314 * @throws \CiviCRM_API3_Exception
316 public static function getDefaultReceiptFrom() {
317 $domain = civicrm_api3('domain', 'getsingle', ['id' => CRM_Core_Config
::domainID()]);
318 if (!empty($domain['from_email'])) {
319 return [$domain['from_name'], $domain['from_email']];
321 if (!empty($domain['domain_email'])) {
322 return [$domain['name'], $domain['domain_email']];
327 if (!Civi
::settings()->get('allow_mail_from_logged_in_contact')) {
328 return [$userName, $userEmail];
331 $userID = CRM_Core_Session
::getLoggedInContactID();
332 if (!empty($userID)) {
333 list($userName, $userEmail) = CRM_Contact_BAO_Contact_Location
::getEmailDetails($userID);
335 // If still empty fall back to the logged in user details.
336 // return empty values no matter what.
337 return [$userName, $userEmail];
341 * Get address to be used for system from addresses when a reply is not expected.
343 public static function getNoReplyEmailAddress() {
344 $emailDomain = CRM_Core_BAO_MailSettings
::defaultDomain();
345 return "do-not-reply@$emailDomain";