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