Merge in 5.16
[civicrm-core.git] / CRM / Core / BAO / Domain.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2019
32 */
33
34 /**
35 *
36 */
37 class CRM_Core_BAO_Domain extends CRM_Core_DAO_Domain {
38
39 /**
40 * Cache for the current domain object.
41 * @var object
42 */
43 public static $_domain = NULL;
44
45 /**
46 * Cache for a domain's location array
47 * @var array
48 */
49 private $_location = NULL;
50
51 /**
52 * Fetch object based on array of properties.
53 *
54 * @param array $params
55 * (reference ) an assoc array of name/value pairs.
56 * @param array $defaults
57 * (reference ) an assoc array to hold the flattened values.
58 *
59 * @return CRM_Core_DAO_Domain
60 */
61 public static function retrieve(&$params, &$defaults) {
62 return CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_Domain', $params, $defaults);
63 }
64
65 /**
66 * Get the domain BAO.
67 *
68 * @param bool $reset
69 *
70 * @return \CRM_Core_BAO_Domain
71 * @throws \CRM_Core_Exception
72 */
73 public static function getDomain($reset = NULL) {
74 static $domain = NULL;
75 if (!$domain || $reset) {
76 $domain = new CRM_Core_BAO_Domain();
77 $domain->id = CRM_Core_Config::domainID();
78 if (!$domain->find(TRUE)) {
79 throw new CRM_Core_Exception('No domain in DB');
80 }
81 }
82 return $domain;
83 }
84
85 /**
86 * @param bool $skipUsingCache
87 *
88 * @return null|string
89 */
90 public static function version($skipUsingCache = FALSE) {
91 return CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain',
92 CRM_Core_Config::domainID(),
93 'version',
94 'id',
95 $skipUsingCache
96 );
97 }
98
99 /**
100 * Get the location values of a domain.
101 *
102 * @return array
103 * Location::getValues
104 */
105 public function &getLocationValues() {
106 if ($this->_location == NULL) {
107 $domain = self::getDomain(NULL);
108 $params = [
109 'contact_id' => $domain->contact_id,
110 ];
111 $this->_location = CRM_Core_BAO_Location::getValues($params, TRUE);
112
113 if (empty($this->_location)) {
114 $this->_location = NULL;
115 }
116 }
117 return $this->_location;
118 }
119
120 /**
121 * Save the values of a domain.
122 *
123 * @param array $params
124 * @param int $id
125 *
126 * @return array
127 * domain
128 */
129 public static function edit(&$params, &$id) {
130 $domain = new CRM_Core_DAO_Domain();
131 $domain->id = $id;
132 $domain->copyValues($params);
133 $domain->save();
134 return $domain;
135 }
136
137 /**
138 * Create a new domain.
139 *
140 * @param array $params
141 *
142 * @return array
143 * domain
144 */
145 public static function create($params) {
146 $domain = new CRM_Core_DAO_Domain();
147 $domain->copyValues($params, TRUE);
148 $domain->save();
149 return $domain;
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 ? TRUE : FALSE;
165 }
166
167 /**
168 * @param bool $skipFatal
169 * @param bool $returnString
170 * @return array
171 * name & email for domain
172 * @throws Exception
173 */
174 public static function getNameAndEmail($skipFatal = FALSE, $returnString = FALSE) {
175 $fromEmailAddress = CRM_Core_OptionGroup::values('from_email_address', NULL, NULL, NULL, ' AND is_default = 1');
176 if (!empty($fromEmailAddress)) {
177 if ($returnString) {
178 // Return a string like: "Demonstrators Anonymous" <info@example.org>
179 return $fromEmailAddress;
180 }
181 foreach ($fromEmailAddress as $key => $value) {
182 $email = CRM_Utils_Mail::pluckEmailFromHeader($value);
183 $fromArray = explode('"', $value);
184 $fromName = CRM_Utils_Array::value(1, $fromArray);
185 break;
186 }
187 return [$fromName, $email];
188 }
189
190 if ($skipFatal) {
191 return [NULL, NULL];
192 }
193
194 $url = CRM_Utils_System::url('civicrm/admin/options/from_email_address',
195 'reset=1'
196 );
197 $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]);
198
199 CRM_Core_Error::fatal($status);
200 }
201
202 /**
203 * @param int $contactID
204 *
205 * @return bool|null|object|string
206 */
207 public static function addContactToDomainGroup($contactID) {
208 $groupID = self::getGroupId();
209
210 if ($groupID) {
211 $contactIDs = [$contactID];
212 CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIDs, $groupID);
213
214 return $groupID;
215 }
216 return FALSE;
217 }
218
219 /**
220 * @return bool|null|object|string
221 */
222 public static function getGroupId() {
223 static $groupID = NULL;
224
225 if ($groupID) {
226 return $groupID;
227 }
228
229 $domainGroupID = Civi::settings()->get('domain_group_id');
230 $multisite = Civi::settings()->get('is_enabled');
231
232 if ($domainGroupID) {
233 $groupID = $domainGroupID;
234 }
235 elseif ($multisite) {
236 // create a group with that of domain name
237 $title = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain',
238 CRM_Core_Config::domainID(), 'name'
239 );
240 $groupID = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group',
241 $title, 'id', 'title', TRUE
242 );
243 }
244 return $groupID ? $groupID : FALSE;
245 }
246
247 /**
248 * @param int $groupId
249 *
250 * @return bool
251 */
252 public static function isDomainGroup($groupId) {
253 $domainGroupID = self::getGroupId();
254 return $domainGroupID == $groupId ? TRUE : FALSE;
255 }
256
257 /**
258 * @return array
259 */
260 public static function getChildGroupIds() {
261 $domainGroupID = self::getGroupId();
262 $childGrps = [];
263
264 if ($domainGroupID) {
265 $childGrps = CRM_Contact_BAO_GroupNesting::getChildGroupIds($domainGroupID);
266 $childGrps[] = $domainGroupID;
267 }
268 return $childGrps;
269 }
270
271 /**
272 * Retrieve a list of contact-ids that belongs to current domain/site.
273 *
274 * @return array
275 */
276 public static function getContactList() {
277 $siteGroups = CRM_Core_BAO_Domain::getChildGroupIds();
278 $siteContacts = [];
279
280 if (!empty($siteGroups)) {
281 $query = "
282 SELECT cc.id
283 FROM civicrm_contact cc
284 INNER JOIN civicrm_group_contact gc ON
285 (gc.contact_id = cc.id AND gc.status = 'Added' AND gc.group_id IN (" . implode(',', $siteGroups) . "))";
286
287 $dao = CRM_Core_DAO::executeQuery($query);
288 while ($dao->fetch()) {
289 $siteContacts[] = $dao->id;
290 }
291 }
292 return $siteContacts;
293 }
294
295 /**
296 * CRM-20308 & CRM-19657
297 * Return domain information / user information for the usage in receipts
298 * Try default from address then fall back to using logged in user details
299 */
300 public static function getDefaultReceiptFrom() {
301 $domain = civicrm_api3('domain', 'getsingle', ['id' => CRM_Core_Config::domainID()]);
302 if (!empty($domain['from_email'])) {
303 return [$domain['from_name'], $domain['from_email']];
304 }
305 if (!empty($domain['domain_email'])) {
306 return [$domain['name'], $domain['domain_email']];
307 }
308 $userName = '';
309 $userEmail = '';
310
311 if (!Civi::settings()->get('allow_mail_from_logged_in_contact')) {
312 return [$userName, $userEmail];
313 }
314
315 $userID = CRM_Core_Session::singleton()->getLoggedInContactID();
316 if (!empty($userID)) {
317 list($userName, $userEmail) = CRM_Contact_BAO_Contact_Location::getEmailDetails($userID);
318 }
319 // If still empty fall back to the logged in user details.
320 // return empty values no matter what.
321 return [$userName, $userEmail];
322 }
323
324 /**
325 * Get address to be used for system from addresses when a reply is not expected.
326 */
327 public static function getNoReplyEmailAddress() {
328 $emailDomain = CRM_Core_BAO_MailSettings::defaultDomain();
329 return "do-not-reply@$emailDomain";
330 }
331
332 }