Merge pull request #23939 from civicrm/5.51
[civicrm-core.git] / CRM / Core / BAO / Domain.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
19 *
20 */
21class CRM_Core_BAO_Domain extends CRM_Core_DAO_Domain {
22
6a488035
TO
23 /**
24 * Cache for a domain's location array
518fa0ee 25 * @var array
6a488035
TO
26 */
27 private $_location = NULL;
28
d357f225
CW
29 /**
30 * Flushes the cache set by getDomain.
31 *
32 * @see CRM_Core_BAO_Domain::getDomain()
33 * @param CRM_Core_DAO_Domain $domain
34 */
35 public static function onPostSave($domain) {
acd447a1
EM
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();
d357f225
CW
39 Civi::$statics[__CLASS__]['current'] = NULL;
40 }
41
6a488035 42 /**
4f940304 43 * Retrieve DB object and copy to defaults array.
6a488035 44 *
6a0b768e 45 * @param array $params
4f940304 46 * Array of criteria values.
6a0b768e 47 * @param array $defaults
4f940304 48 * Array to be populated with found values.
6a488035 49 *
4f940304
CW
50 * @return self|null
51 * The DAO object, if found.
52 *
53 * @deprecated
6a488035 54 */
4f940304
CW
55 public static function retrieve($params, &$defaults) {
56 return self::commonRetrieve(self::class, $params, $defaults);
6a488035
TO
57 }
58
59 /**
d357f225 60 * Get the current domain.
77b97be7 61 *
2149f4bd 62 * @return \CRM_Core_BAO_Domain
63 * @throws \CRM_Core_Exception
6a488035 64 */
d357f225
CW
65 public static function getDomain() {
66 $domain = Civi::$statics[__CLASS__]['current'] ?? NULL;
67 if (!$domain) {
6a488035
TO
68 $domain = new CRM_Core_BAO_Domain();
69 $domain->id = CRM_Core_Config::domainID();
70 if (!$domain->find(TRUE)) {
2149f4bd 71 throw new CRM_Core_Exception('No domain in DB');
6a488035 72 }
d357f225 73 Civi::$statics[__CLASS__]['current'] = $domain;
6a488035
TO
74 }
75 return $domain;
76 }
77
b5c2afd0
EM
78 /**
79 * @param bool $skipUsingCache
80 *
d357f225 81 * @return string
527745f0 82 *
83 * @throws \CRM_Core_Exception
b5c2afd0 84 */
2aa397bc 85 public static function version($skipUsingCache = FALSE) {
d357f225
CW
86 if ($skipUsingCache) {
87 Civi::$statics[__CLASS__]['current'] = NULL;
88 }
89
90 return self::getDomain()->version;
6a488035
TO
91 }
92
1fbda76b 93 /**
94 * Is a database update required to apply latest schema changes.
95 *
96 * @return bool
97 *
98 * @throws \CRM_Core_Exception
99 */
100 public static function isDBUpdateRequired() {
d357f225 101 $dbVersion = self::version();
1fbda76b 102 $codeVersion = CRM_Utils_System::version();
103 return version_compare($dbVersion, $codeVersion) < 0;
104 }
105
d51a6a62
CW
106 /**
107 * Checks that the current DB schema is at least $min version
108 *
aec7c57d 109 * @param string|int $min
d51a6a62
CW
110 * @return bool
111 */
112 public static function isDBVersionAtLeast($min) {
113 return version_compare(self::version(), $min, '>=');
114 }
115
bd6ce89b
EM
116 /**
117 * @return string
118 */
119 protected static function getMissingDomainFromEmailMessage(): string {
120 $url = CRM_Utils_System::url('civicrm/admin/options/from_email_address',
121 'reset=1'
122 );
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]);
124 return $status;
125 }
126
6a488035 127 /**
fe482240 128 * Get the location values of a domain.
6a488035 129 *
d357f225 130 * @return CRM_Core_BAO_Location[]|NULL
6a488035 131 */
d357f225 132 public function getLocationValues() {
6a488035 133 if ($this->_location == NULL) {
be2fb01f 134 $params = [
d357f225 135 'contact_id' => $this->contact_id,
be2fb01f 136 ];
6a488035
TO
137 $this->_location = CRM_Core_BAO_Location::getValues($params, TRUE);
138
139 if (empty($this->_location)) {
140 $this->_location = NULL;
141 }
142 }
143 return $this->_location;
144 }
145
146 /**
6a5fec96 147 * Update a domain.
6a488035 148 *
c490a46a 149 * @param array $params
100fef9d 150 * @param int $id
da6b46f4 151 *
527745f0 152 * @return CRM_Core_DAO_Domain
acd447a1 153 * @throws \CRM_Core_Exception
6a488035 154 */
acd447a1 155 public static function edit($params, $id): CRM_Core_DAO_Domain {
6a5fec96
CW
156 $params['id'] = $id;
157 return self::writeRecord($params);
6a488035
TO
158 }
159
160 /**
6a5fec96 161 * Create or update domain.
6a488035 162 *
c490a46a 163 * @param array $params
527745f0 164 * @return CRM_Core_DAO_Domain
6a488035 165 */
00be9182 166 public static function create($params) {
6a5fec96 167 return self::writeRecord($params);
6a488035
TO
168 }
169
b5c2afd0
EM
170 /**
171 * @return bool
172 */
00be9182 173 public static function multipleDomains() {
6a488035
TO
174 $session = CRM_Core_Session::singleton();
175
176 $numberDomains = $session->get('numberDomains');
177 if (!$numberDomains) {
527745f0 178 $query = 'SELECT count(*) from civicrm_domain';
6a488035
TO
179 $numberDomains = CRM_Core_DAO::singleValueQuery($query);
180 $session->set('numberDomains', $numberDomains);
181 }
63d76404 182 return $numberDomains > 1;
6a488035
TO
183 }
184
b5c2afd0
EM
185 /**
186 * @param bool $skipFatal
518fa0ee 187 * @param bool $returnString
bd6ce89b
EM
188 * If you are using this second parameter you probably are better
189 * calling `getFromEmail()` which will return an actual string.
527745f0 190 *
a6c01b45
CW
191 * @return array
192 * name & email for domain
527745f0 193 *
194 * @throws \CRM_Core_Exception
b5c2afd0 195 */
beac1417 196 public static function getNameAndEmail($skipFatal = FALSE, $returnString = FALSE) {
6a488035
TO
197 $fromEmailAddress = CRM_Core_OptionGroup::values('from_email_address', NULL, NULL, NULL, ' AND is_default = 1');
198 if (!empty($fromEmailAddress)) {
beac1417
MW
199 if ($returnString) {
200 // Return a string like: "Demonstrators Anonymous" <info@example.org>
201 return $fromEmailAddress;
202 }
6a488035 203 foreach ($fromEmailAddress as $key => $value) {
353ffa53 204 $email = CRM_Utils_Mail::pluckEmailFromHeader($value);
6a488035 205 $fromArray = explode('"', $value);
9c1bc317 206 $fromName = $fromArray[1] ?? NULL;
6a488035
TO
207 break;
208 }
be2fb01f 209 return [$fromName, $email];
6a488035 210 }
beac1417
MW
211
212 if ($skipFatal) {
be2fb01f 213 return [NULL, NULL];
6a488035
TO
214 }
215
bd6ce89b 216 $status = self::getMissingDomainFromEmailMessage();
6a488035 217
527745f0 218 throw new CRM_Core_Exception($status);
6a488035
TO
219 }
220
bd6ce89b
EM
221 /**
222 * Get the domain email in a format suitable for using as the from address.
223 *
224 * @return string
225 * @throws \CRM_Core_Exception
226 */
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);
230 if (!$email) {
231 throw new CRM_Core_Exception(self::getMissingDomainFromEmailMessage());
232 }
233 return $email;
234 }
235
b5c2afd0 236 /**
100fef9d 237 * @param int $contactID
b5c2afd0
EM
238 *
239 * @return bool|null|object|string
527745f0 240 *
241 * @throws \CRM_Core_Exception
b5c2afd0 242 */
00be9182 243 public static function addContactToDomainGroup($contactID) {
6a488035
TO
244 $groupID = self::getGroupId();
245
246 if ($groupID) {
be2fb01f 247 $contactIDs = [$contactID];
6a488035
TO
248 CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIDs, $groupID);
249
250 return $groupID;
251 }
252 return FALSE;
253 }
254
b5c2afd0
EM
255 /**
256 * @return bool|null|object|string
527745f0 257 *
258 * @throws \CRM_Core_Exception
b5c2afd0 259 */
00be9182 260 public static function getGroupId() {
6a488035
TO
261 static $groupID = NULL;
262
263 if ($groupID) {
264 return $groupID;
265 }
266
aaffa79f 267 $domainGroupID = Civi::settings()->get('domain_group_id');
268 $multisite = Civi::settings()->get('is_enabled');
6a488035
TO
269
270 if ($domainGroupID) {
271 $groupID = $domainGroupID;
272 }
273 elseif ($multisite) {
274 // create a group with that of domain name
d357f225 275 $title = self::getDomain()->name;
6a488035 276 $groupID = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group',
2aa397bc 277 $title, 'id', 'title', TRUE
6a488035 278 );
6a488035
TO
279 }
280 return $groupID ? $groupID : FALSE;
281 }
282
b5c2afd0 283 /**
100fef9d 284 * @param int $groupId
b5c2afd0
EM
285 *
286 * @return bool
527745f0 287 *
288 * @throws \CRM_Core_Exception
b5c2afd0 289 */
00be9182 290 public static function isDomainGroup($groupId) {
6a488035 291 $domainGroupID = self::getGroupId();
63d76404 292 return $domainGroupID == (bool) $groupId;
6a488035
TO
293 }
294
b5c2afd0
EM
295 /**
296 * @return array
527745f0 297 *
298 * @throws \CRM_Core_Exception
b5c2afd0 299 */
00be9182 300 public static function getChildGroupIds() {
6a488035 301 $domainGroupID = self::getGroupId();
be2fb01f 302 $childGrps = [];
6a488035
TO
303
304 if ($domainGroupID) {
305 $childGrps = CRM_Contact_BAO_GroupNesting::getChildGroupIds($domainGroupID);
306 $childGrps[] = $domainGroupID;
307 }
308 return $childGrps;
309 }
310
b5c2afd0 311 /**
100fef9d 312 * Retrieve a list of contact-ids that belongs to current domain/site.
c490a46a 313 *
b5c2afd0 314 * @return array
527745f0 315 *
316 * @throws \CRM_Core_Exception
b5c2afd0 317 */
00be9182 318 public static function getContactList() {
6a488035 319 $siteGroups = CRM_Core_BAO_Domain::getChildGroupIds();
be2fb01f 320 $siteContacts = [];
6a488035
TO
321
322 if (!empty($siteGroups)) {
323 $query = "
324 SELECT cc.id
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) . "))";
328
329 $dao = CRM_Core_DAO::executeQuery($query);
330 while ($dao->fetch()) {
331 $siteContacts[] = $dao->id;
332 }
333 }
334 return $siteContacts;
335 }
96025800 336
b5bfb58f
SL
337 /**
338 * CRM-20308 & CRM-19657
beac1417
MW
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
527745f0 341 *
342 * @throws \CiviCRM_API3_Exception
b5bfb58f 343 */
b1273714 344 public static function getDefaultReceiptFrom() {
be2fb01f 345 $domain = civicrm_api3('domain', 'getsingle', ['id' => CRM_Core_Config::domainID()]);
b5bfb58f 346 if (!empty($domain['from_email'])) {
be2fb01f 347 return [$domain['from_name'], $domain['from_email']];
b5bfb58f
SL
348 }
349 if (!empty($domain['domain_email'])) {
be2fb01f 350 return [$domain['name'], $domain['domain_email']];
b5bfb58f 351 }
b5bfb58f
SL
352 $userName = '';
353 $userEmail = '';
4c981f37
MW
354
355 if (!Civi::settings()->get('allow_mail_from_logged_in_contact')) {
be2fb01f 356 return [$userName, $userEmail];
4c981f37
MW
357 }
358
2dbdb9b9 359 $userID = CRM_Core_Session::getLoggedInContactID();
b5bfb58f 360 if (!empty($userID)) {
bd6ce89b 361 [$userName, $userEmail] = CRM_Contact_BAO_Contact_Location::getEmailDetails($userID);
b5bfb58f
SL
362 }
363 // If still empty fall back to the logged in user details.
364 // return empty values no matter what.
be2fb01f 365 return [$userName, $userEmail];
b5bfb58f
SL
366 }
367
576fcb9c 368 /**
369 * Get address to be used for system from addresses when a reply is not expected.
370 */
371 public static function getNoReplyEmailAddress() {
372 $emailDomain = CRM_Core_BAO_MailSettings::defaultDomain();
373 return "do-not-reply@$emailDomain";
374 }
375
6a488035 376}