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