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