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 * Get the location values of a domain.
91 * @throws \CRM_Core_Exception
93 public function &getLocationValues() {
94 if ($this->_location
== NULL) {
95 $domain = self
::getDomain(NULL);
97 'contact_id' => $domain->contact_id
,
99 $this->_location
= CRM_Core_BAO_Location
::getValues($params, TRUE);
101 if (empty($this->_location
)) {
102 $this->_location
= NULL;
105 return $this->_location
;
111 * @param array $params
114 * @return CRM_Core_DAO_Domain
116 public static function edit($params, $id) {
118 return self
::writeRecord($params);
122 * Create or update domain.
124 * @param array $params
125 * @return CRM_Core_DAO_Domain
127 public static function create($params) {
128 return self
::writeRecord($params);
134 public static function multipleDomains() {
135 $session = CRM_Core_Session
::singleton();
137 $numberDomains = $session->get('numberDomains');
138 if (!$numberDomains) {
139 $query = 'SELECT count(*) from civicrm_domain';
140 $numberDomains = CRM_Core_DAO
::singleValueQuery($query);
141 $session->set('numberDomains', $numberDomains);
143 return $numberDomains > 1;
147 * @param bool $skipFatal
148 * @param bool $returnString
151 * name & email for domain
153 * @throws \CRM_Core_Exception
155 public static function getNameAndEmail($skipFatal = FALSE, $returnString = FALSE) {
156 $fromEmailAddress = CRM_Core_OptionGroup
::values('from_email_address', NULL, NULL, NULL, ' AND is_default = 1');
157 if (!empty($fromEmailAddress)) {
159 // Return a string like: "Demonstrators Anonymous" <info@example.org>
160 return $fromEmailAddress;
162 foreach ($fromEmailAddress as $key => $value) {
163 $email = CRM_Utils_Mail
::pluckEmailFromHeader($value);
164 $fromArray = explode('"', $value);
165 $fromName = $fromArray[1] ??
NULL;
168 return [$fromName, $email];
175 $url = CRM_Utils_System
::url('civicrm/admin/options/from_email_address',
178 $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]);
180 throw new CRM_Core_Exception($status);
184 * @param int $contactID
186 * @return bool|null|object|string
188 * @throws \CRM_Core_Exception
190 public static function addContactToDomainGroup($contactID) {
191 $groupID = self
::getGroupId();
194 $contactIDs = [$contactID];
195 CRM_Contact_BAO_GroupContact
::addContactsToGroup($contactIDs, $groupID);
203 * @return bool|null|object|string
205 * @throws \CRM_Core_Exception
207 public static function getGroupId() {
208 static $groupID = NULL;
214 $domainGroupID = Civi
::settings()->get('domain_group_id');
215 $multisite = Civi
::settings()->get('is_enabled');
217 if ($domainGroupID) {
218 $groupID = $domainGroupID;
220 elseif ($multisite) {
221 // create a group with that of domain name
222 $title = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Domain',
223 CRM_Core_Config
::domainID(), 'name'
225 $groupID = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Group',
226 $title, 'id', 'title', TRUE
229 return $groupID ?
$groupID : FALSE;
233 * @param int $groupId
237 * @throws \CRM_Core_Exception
239 public static function isDomainGroup($groupId) {
240 $domainGroupID = self
::getGroupId();
241 return $domainGroupID == (bool) $groupId;
247 * @throws \CRM_Core_Exception
249 public static function getChildGroupIds() {
250 $domainGroupID = self
::getGroupId();
253 if ($domainGroupID) {
254 $childGrps = CRM_Contact_BAO_GroupNesting
::getChildGroupIds($domainGroupID);
255 $childGrps[] = $domainGroupID;
261 * Retrieve a list of contact-ids that belongs to current domain/site.
265 * @throws \CRM_Core_Exception
267 public static function getContactList() {
268 $siteGroups = CRM_Core_BAO_Domain
::getChildGroupIds();
271 if (!empty($siteGroups)) {
274 FROM civicrm_contact cc
275 INNER JOIN civicrm_group_contact gc ON
276 (gc.contact_id = cc.id AND gc.status = 'Added' AND gc.group_id IN (" . implode(',', $siteGroups) . "))";
278 $dao = CRM_Core_DAO
::executeQuery($query);
279 while ($dao->fetch()) {
280 $siteContacts[] = $dao->id
;
283 return $siteContacts;
287 * CRM-20308 & CRM-19657
288 * Return domain information / user information for the usage in receipts
289 * Try default from address then fall back to using logged in user details
291 * @throws \CiviCRM_API3_Exception
293 public static function getDefaultReceiptFrom() {
294 $domain = civicrm_api3('domain', 'getsingle', ['id' => CRM_Core_Config
::domainID()]);
295 if (!empty($domain['from_email'])) {
296 return [$domain['from_name'], $domain['from_email']];
298 if (!empty($domain['domain_email'])) {
299 return [$domain['name'], $domain['domain_email']];
304 if (!Civi
::settings()->get('allow_mail_from_logged_in_contact')) {
305 return [$userName, $userEmail];
308 $userID = CRM_Core_Session
::singleton()->getLoggedInContactID();
309 if (!empty($userID)) {
310 list($userName, $userEmail) = CRM_Contact_BAO_Contact_Location
::getEmailDetails($userID);
312 // If still empty fall back to the logged in user details.
313 // return empty values no matter what.
314 return [$userName, $userEmail];
318 * Get address to be used for system from addresses when a reply is not expected.
320 public static function getNoReplyEmailAddress() {
321 $emailDomain = CRM_Core_BAO_MailSettings
::defaultDomain();
322 return "do-not-reply@$emailDomain";