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