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