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