dev/financial#148 fully deprecate validateData function
[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 a domain's location array
25 * @var array
26 */
27 private $_location = NULL;
28
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
39 /**
40 * Fetch object based on array of properties.
41 *
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.
46 *
47 * @return CRM_Core_DAO_Domain
48 */
49 public static function retrieve(&$params, &$defaults) {
50 return CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_Domain', $params, $defaults);
51 }
52
53 /**
54 * Get the current domain.
55 *
56 * @return \CRM_Core_BAO_Domain
57 * @throws \CRM_Core_Exception
58 */
59 public static function getDomain() {
60 $domain = Civi::$statics[__CLASS__]['current'] ?? NULL;
61 if (!$domain) {
62 $domain = new CRM_Core_BAO_Domain();
63 $domain->id = CRM_Core_Config::domainID();
64 if (!$domain->find(TRUE)) {
65 throw new CRM_Core_Exception('No domain in DB');
66 }
67 Civi::$statics[__CLASS__]['current'] = $domain;
68 }
69 return $domain;
70 }
71
72 /**
73 * @param bool $skipUsingCache
74 *
75 * @return string
76 *
77 * @throws \CRM_Core_Exception
78 */
79 public static function version($skipUsingCache = FALSE) {
80 if ($skipUsingCache) {
81 Civi::$statics[__CLASS__]['current'] = NULL;
82 }
83
84 return self::getDomain()->version;
85 }
86
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() {
95 $dbVersion = self::version();
96 $codeVersion = CRM_Utils_System::version();
97 return version_compare($dbVersion, $codeVersion) < 0;
98 }
99
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
110 /**
111 * Get the location values of a domain.
112 *
113 * @return CRM_Core_BAO_Location[]|NULL
114 */
115 public function getLocationValues() {
116 if ($this->_location == NULL) {
117 $params = [
118 'contact_id' => $this->contact_id,
119 ];
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 /**
130 * Update a domain.
131 *
132 * @param array $params
133 * @param int $id
134 *
135 * @return CRM_Core_DAO_Domain
136 */
137 public static function edit($params, $id) {
138 $params['id'] = $id;
139 return self::writeRecord($params);
140 }
141
142 /**
143 * Create or update domain.
144 *
145 * @param array $params
146 * @return CRM_Core_DAO_Domain
147 */
148 public static function create($params) {
149 return self::writeRecord($params);
150 }
151
152 /**
153 * @return bool
154 */
155 public static function multipleDomains() {
156 $session = CRM_Core_Session::singleton();
157
158 $numberDomains = $session->get('numberDomains');
159 if (!$numberDomains) {
160 $query = 'SELECT count(*) from civicrm_domain';
161 $numberDomains = CRM_Core_DAO::singleValueQuery($query);
162 $session->set('numberDomains', $numberDomains);
163 }
164 return $numberDomains > 1;
165 }
166
167 /**
168 * @param bool $skipFatal
169 * @param bool $returnString
170 *
171 * @return array
172 * name & email for domain
173 *
174 * @throws \CRM_Core_Exception
175 */
176 public static function getNameAndEmail($skipFatal = FALSE, $returnString = FALSE) {
177 $fromEmailAddress = CRM_Core_OptionGroup::values('from_email_address', NULL, NULL, NULL, ' AND is_default = 1');
178 if (!empty($fromEmailAddress)) {
179 if ($returnString) {
180 // Return a string like: "Demonstrators Anonymous" <info@example.org>
181 return $fromEmailAddress;
182 }
183 foreach ($fromEmailAddress as $key => $value) {
184 $email = CRM_Utils_Mail::pluckEmailFromHeader($value);
185 $fromArray = explode('"', $value);
186 $fromName = $fromArray[1] ?? NULL;
187 break;
188 }
189 return [$fromName, $email];
190 }
191
192 if ($skipFatal) {
193 return [NULL, NULL];
194 }
195
196 $url = CRM_Utils_System::url('civicrm/admin/options/from_email_address',
197 'reset=1'
198 );
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]);
200
201 throw new CRM_Core_Exception($status);
202 }
203
204 /**
205 * @param int $contactID
206 *
207 * @return bool|null|object|string
208 *
209 * @throws \CRM_Core_Exception
210 */
211 public static function addContactToDomainGroup($contactID) {
212 $groupID = self::getGroupId();
213
214 if ($groupID) {
215 $contactIDs = [$contactID];
216 CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIDs, $groupID);
217
218 return $groupID;
219 }
220 return FALSE;
221 }
222
223 /**
224 * @return bool|null|object|string
225 *
226 * @throws \CRM_Core_Exception
227 */
228 public static function getGroupId() {
229 static $groupID = NULL;
230
231 if ($groupID) {
232 return $groupID;
233 }
234
235 $domainGroupID = Civi::settings()->get('domain_group_id');
236 $multisite = Civi::settings()->get('is_enabled');
237
238 if ($domainGroupID) {
239 $groupID = $domainGroupID;
240 }
241 elseif ($multisite) {
242 // create a group with that of domain name
243 $title = self::getDomain()->name;
244 $groupID = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group',
245 $title, 'id', 'title', TRUE
246 );
247 }
248 return $groupID ? $groupID : FALSE;
249 }
250
251 /**
252 * @param int $groupId
253 *
254 * @return bool
255 *
256 * @throws \CRM_Core_Exception
257 */
258 public static function isDomainGroup($groupId) {
259 $domainGroupID = self::getGroupId();
260 return $domainGroupID == (bool) $groupId;
261 }
262
263 /**
264 * @return array
265 *
266 * @throws \CRM_Core_Exception
267 */
268 public static function getChildGroupIds() {
269 $domainGroupID = self::getGroupId();
270 $childGrps = [];
271
272 if ($domainGroupID) {
273 $childGrps = CRM_Contact_BAO_GroupNesting::getChildGroupIds($domainGroupID);
274 $childGrps[] = $domainGroupID;
275 }
276 return $childGrps;
277 }
278
279 /**
280 * Retrieve a list of contact-ids that belongs to current domain/site.
281 *
282 * @return array
283 *
284 * @throws \CRM_Core_Exception
285 */
286 public static function getContactList() {
287 $siteGroups = CRM_Core_BAO_Domain::getChildGroupIds();
288 $siteContacts = [];
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 }
304
305 /**
306 * CRM-20308 & CRM-19657
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
309 *
310 * @throws \CiviCRM_API3_Exception
311 */
312 public static function getDefaultReceiptFrom() {
313 $domain = civicrm_api3('domain', 'getsingle', ['id' => CRM_Core_Config::domainID()]);
314 if (!empty($domain['from_email'])) {
315 return [$domain['from_name'], $domain['from_email']];
316 }
317 if (!empty($domain['domain_email'])) {
318 return [$domain['name'], $domain['domain_email']];
319 }
320 $userName = '';
321 $userEmail = '';
322
323 if (!Civi::settings()->get('allow_mail_from_logged_in_contact')) {
324 return [$userName, $userEmail];
325 }
326
327 $userID = CRM_Core_Session::getLoggedInContactID();
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.
333 return [$userName, $userEmail];
334 }
335
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
344 }