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