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
;
109 * Save the values of a domain.
111 * @param array $params
114 * @return CRM_Core_DAO_Domain
116 public static function edit(&$params, &$id) {
117 CRM_Utils_Hook
::pre('edit', 'Domain', CRM_Utils_Array
::value('id', $params), $params);
118 $domain = new CRM_Core_DAO_Domain();
120 $domain->copyValues($params);
122 CRM_Utils_Hook
::post('edit', 'Domain', $domain->id
, $domain);
127 * Create a new domain.
129 * @param array $params
131 * @return CRM_Core_DAO_Domain
133 public static function create($params) {
134 $hook = empty($params['id']) ?
'create' : 'edit';
135 CRM_Utils_Hook
::pre($hook, 'Domain', CRM_Utils_Array
::value('id', $params), $params);
136 $domain = new CRM_Core_DAO_Domain();
137 $domain->copyValues($params);
139 CRM_Utils_Hook
::post($hook, 'Domain', $domain->id
, $domain);
146 public static function multipleDomains() {
147 $session = CRM_Core_Session
::singleton();
149 $numberDomains = $session->get('numberDomains');
150 if (!$numberDomains) {
151 $query = 'SELECT count(*) from civicrm_domain';
152 $numberDomains = CRM_Core_DAO
::singleValueQuery($query);
153 $session->set('numberDomains', $numberDomains);
155 return $numberDomains > 1;
159 * @param bool $skipFatal
160 * @param bool $returnString
163 * name & email for domain
165 * @throws \CRM_Core_Exception
167 public static function getNameAndEmail($skipFatal = FALSE, $returnString = FALSE) {
168 $fromEmailAddress = CRM_Core_OptionGroup
::values('from_email_address', NULL, NULL, NULL, ' AND is_default = 1');
169 if (!empty($fromEmailAddress)) {
171 // Return a string like: "Demonstrators Anonymous" <info@example.org>
172 return $fromEmailAddress;
174 foreach ($fromEmailAddress as $key => $value) {
175 $email = CRM_Utils_Mail
::pluckEmailFromHeader($value);
176 $fromArray = explode('"', $value);
177 $fromName = $fromArray[1] ??
NULL;
180 return [$fromName, $email];
187 $url = CRM_Utils_System
::url('civicrm/admin/options/from_email_address',
190 $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]);
192 throw new CRM_Core_Exception($status);
196 * @param int $contactID
198 * @return bool|null|object|string
200 * @throws \CRM_Core_Exception
202 public static function addContactToDomainGroup($contactID) {
203 $groupID = self
::getGroupId();
206 $contactIDs = [$contactID];
207 CRM_Contact_BAO_GroupContact
::addContactsToGroup($contactIDs, $groupID);
215 * @return bool|null|object|string
217 * @throws \CRM_Core_Exception
219 public static function getGroupId() {
220 static $groupID = NULL;
226 $domainGroupID = Civi
::settings()->get('domain_group_id');
227 $multisite = Civi
::settings()->get('is_enabled');
229 if ($domainGroupID) {
230 $groupID = $domainGroupID;
232 elseif ($multisite) {
233 // create a group with that of domain name
234 $title = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Domain',
235 CRM_Core_Config
::domainID(), 'name'
237 $groupID = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Group',
238 $title, 'id', 'title', TRUE
241 return $groupID ?
$groupID : FALSE;
245 * @param int $groupId
249 * @throws \CRM_Core_Exception
251 public static function isDomainGroup($groupId) {
252 $domainGroupID = self
::getGroupId();
253 return $domainGroupID == (bool) $groupId;
259 * @throws \CRM_Core_Exception
261 public static function getChildGroupIds() {
262 $domainGroupID = self
::getGroupId();
265 if ($domainGroupID) {
266 $childGrps = CRM_Contact_BAO_GroupNesting
::getChildGroupIds($domainGroupID);
267 $childGrps[] = $domainGroupID;
273 * Retrieve a list of contact-ids that belongs to current domain/site.
277 * @throws \CRM_Core_Exception
279 public static function getContactList() {
280 $siteGroups = CRM_Core_BAO_Domain
::getChildGroupIds();
283 if (!empty($siteGroups)) {
286 FROM civicrm_contact cc
287 INNER JOIN civicrm_group_contact gc ON
288 (gc.contact_id = cc.id AND gc.status = 'Added' AND gc.group_id IN (" . implode(',', $siteGroups) . "))";
290 $dao = CRM_Core_DAO
::executeQuery($query);
291 while ($dao->fetch()) {
292 $siteContacts[] = $dao->id
;
295 return $siteContacts;
299 * CRM-20308 & CRM-19657
300 * Return domain information / user information for the usage in receipts
301 * Try default from address then fall back to using logged in user details
303 * @throws \CiviCRM_API3_Exception
305 public static function getDefaultReceiptFrom() {
306 $domain = civicrm_api3('domain', 'getsingle', ['id' => CRM_Core_Config
::domainID()]);
307 if (!empty($domain['from_email'])) {
308 return [$domain['from_name'], $domain['from_email']];
310 if (!empty($domain['domain_email'])) {
311 return [$domain['name'], $domain['domain_email']];
316 if (!Civi
::settings()->get('allow_mail_from_logged_in_contact')) {
317 return [$userName, $userEmail];
320 $userID = CRM_Core_Session
::singleton()->getLoggedInContactID();
321 if (!empty($userID)) {
322 list($userName, $userEmail) = CRM_Contact_BAO_Contact_Location
::getEmailDetails($userID);
324 // If still empty fall back to the logged in user details.
325 // return empty values no matter what.
326 return [$userName, $userEmail];
330 * Get address to be used for system from addresses when a reply is not expected.
332 public static function getNoReplyEmailAddress() {
333 $emailDomain = CRM_Core_BAO_MailSettings
::defaultDomain();
334 return "do-not-reply@$emailDomain";