(NFC) Update CRM/Core CRM/Custom CRM/Dedupe to match the new coder style
[civicrm-core.git] / CRM / Core / BAO / Domain.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
6a488035
TO
32 */
33
34/**
35 *
36 */
37class CRM_Core_BAO_Domain extends CRM_Core_DAO_Domain {
38
39 /**
fe482240 40 * Cache for the current domain object.
518fa0ee 41 * @var object
6a488035 42 */
518fa0ee 43 public static $_domain = NULL;
6a488035
TO
44
45 /**
46 * Cache for a domain's location array
518fa0ee 47 * @var array
6a488035
TO
48 */
49 private $_location = NULL;
50
51 /**
fe482240 52 * Fetch object based on array of properties.
6a488035 53 *
6a0b768e
TO
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.
6a488035 58 *
16b10e64 59 * @return CRM_Core_DAO_Domain
6a488035 60 */
00be9182 61 public static function retrieve(&$params, &$defaults) {
6a488035
TO
62 return CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_Domain', $params, $defaults);
63 }
64
65 /**
fe482240 66 * Get the domain BAO.
6a488035 67 *
2149f4bd 68 * @param bool $reset
77b97be7 69 *
2149f4bd 70 * @return \CRM_Core_BAO_Domain
71 * @throws \CRM_Core_Exception
6a488035 72 */
c9dbc47c 73 public static function getDomain($reset = NULL) {
6a488035
TO
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)) {
2149f4bd 79 throw new CRM_Core_Exception('No domain in DB');
6a488035
TO
80 }
81 }
82 return $domain;
83 }
84
b5c2afd0
EM
85 /**
86 * @param bool $skipUsingCache
87 *
88 * @return null|string
89 */
2aa397bc 90 public static function version($skipUsingCache = FALSE) {
6a488035
TO
91 return CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain',
92 CRM_Core_Config::domainID(),
c490a46a
CW
93 'version',
94 'id',
95 $skipUsingCache
6a488035
TO
96 );
97 }
98
99 /**
fe482240 100 * Get the location values of a domain.
6a488035 101 *
a6c01b45
CW
102 * @return array
103 * Location::getValues
6a488035 104 */
00be9182 105 public function &getLocationValues() {
6a488035 106 if ($this->_location == NULL) {
2aa397bc 107 $domain = self::getDomain(NULL);
be2fb01f 108 $params = [
21dfd5f5 109 'contact_id' => $domain->contact_id,
be2fb01f 110 ];
6a488035
TO
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 /**
fe482240 121 * Save the values of a domain.
6a488035 122 *
c490a46a 123 * @param array $params
100fef9d 124 * @param int $id
da6b46f4 125 *
a6c01b45
CW
126 * @return array
127 * domain
6a488035 128 */
00be9182 129 public static function edit(&$params, &$id) {
6a488035
TO
130 $domain = new CRM_Core_DAO_Domain();
131 $domain->id = $id;
132 $domain->copyValues($params);
133 $domain->save();
134 return $domain;
135 }
136
137 /**
fe482240 138 * Create a new domain.
6a488035 139 *
c490a46a 140 * @param array $params
fd31fa4c 141 *
a6c01b45
CW
142 * @return array
143 * domain
6a488035 144 */
00be9182 145 public static function create($params) {
6a488035 146 $domain = new CRM_Core_DAO_Domain();
1237d8d7 147 $domain->copyValues($params, TRUE);
6a488035
TO
148 $domain->save();
149 return $domain;
150 }
151
b5c2afd0
EM
152 /**
153 * @return bool
154 */
00be9182 155 public static function multipleDomains() {
6a488035
TO
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
b5c2afd0
EM
167 /**
168 * @param bool $skipFatal
518fa0ee 169 * @param bool $returnString
a6c01b45
CW
170 * @return array
171 * name & email for domain
b5c2afd0
EM
172 * @throws Exception
173 */
beac1417 174 public static function getNameAndEmail($skipFatal = FALSE, $returnString = FALSE) {
6a488035
TO
175 $fromEmailAddress = CRM_Core_OptionGroup::values('from_email_address', NULL, NULL, NULL, ' AND is_default = 1');
176 if (!empty($fromEmailAddress)) {
beac1417
MW
177 if ($returnString) {
178 // Return a string like: "Demonstrators Anonymous" <info@example.org>
179 return $fromEmailAddress;
180 }
6a488035 181 foreach ($fromEmailAddress as $key => $value) {
353ffa53 182 $email = CRM_Utils_Mail::pluckEmailFromHeader($value);
6a488035 183 $fromArray = explode('"', $value);
353ffa53 184 $fromName = CRM_Utils_Array::value(1, $fromArray);
6a488035
TO
185 break;
186 }
be2fb01f 187 return [$fromName, $email];
6a488035 188 }
beac1417
MW
189
190 if ($skipFatal) {
be2fb01f 191 return [NULL, NULL];
6a488035
TO
192 }
193
beac1417
MW
194 $url = CRM_Utils_System::url('civicrm/admin/options/from_email_address',
195 'reset=1'
6a488035 196 );
be2fb01f 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]);
6a488035
TO
198
199 CRM_Core_Error::fatal($status);
200 }
201
b5c2afd0 202 /**
100fef9d 203 * @param int $contactID
b5c2afd0
EM
204 *
205 * @return bool|null|object|string
206 */
00be9182 207 public static function addContactToDomainGroup($contactID) {
6a488035
TO
208 $groupID = self::getGroupId();
209
210 if ($groupID) {
be2fb01f 211 $contactIDs = [$contactID];
6a488035
TO
212 CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIDs, $groupID);
213
214 return $groupID;
215 }
216 return FALSE;
217 }
218
b5c2afd0
EM
219 /**
220 * @return bool|null|object|string
221 */
00be9182 222 public static function getGroupId() {
6a488035
TO
223 static $groupID = NULL;
224
225 if ($groupID) {
226 return $groupID;
227 }
228
aaffa79f 229 $domainGroupID = Civi::settings()->get('domain_group_id');
230 $multisite = Civi::settings()->get('is_enabled');
6a488035
TO
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',
2aa397bc 241 $title, 'id', 'title', TRUE
6a488035 242 );
6a488035
TO
243 }
244 return $groupID ? $groupID : FALSE;
245 }
246
b5c2afd0 247 /**
100fef9d 248 * @param int $groupId
b5c2afd0
EM
249 *
250 * @return bool
251 */
00be9182 252 public static function isDomainGroup($groupId) {
6a488035
TO
253 $domainGroupID = self::getGroupId();
254 return $domainGroupID == $groupId ? TRUE : FALSE;
255 }
256
b5c2afd0
EM
257 /**
258 * @return array
259 */
00be9182 260 public static function getChildGroupIds() {
6a488035 261 $domainGroupID = self::getGroupId();
be2fb01f 262 $childGrps = [];
6a488035
TO
263
264 if ($domainGroupID) {
265 $childGrps = CRM_Contact_BAO_GroupNesting::getChildGroupIds($domainGroupID);
266 $childGrps[] = $domainGroupID;
267 }
268 return $childGrps;
269 }
270
b5c2afd0 271 /**
100fef9d 272 * Retrieve a list of contact-ids that belongs to current domain/site.
c490a46a 273 *
b5c2afd0
EM
274 * @return array
275 */
00be9182 276 public static function getContactList() {
6a488035 277 $siteGroups = CRM_Core_BAO_Domain::getChildGroupIds();
be2fb01f 278 $siteContacts = [];
6a488035
TO
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 }
96025800 294
b5bfb58f
SL
295 /**
296 * CRM-20308 & CRM-19657
beac1417
MW
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
b5bfb58f 299 */
b1273714 300 public static function getDefaultReceiptFrom() {
be2fb01f 301 $domain = civicrm_api3('domain', 'getsingle', ['id' => CRM_Core_Config::domainID()]);
b5bfb58f 302 if (!empty($domain['from_email'])) {
be2fb01f 303 return [$domain['from_name'], $domain['from_email']];
b5bfb58f
SL
304 }
305 if (!empty($domain['domain_email'])) {
be2fb01f 306 return [$domain['name'], $domain['domain_email']];
b5bfb58f 307 }
b5bfb58f
SL
308 $userName = '';
309 $userEmail = '';
4c981f37
MW
310
311 if (!Civi::settings()->get('allow_mail_from_logged_in_contact')) {
be2fb01f 312 return [$userName, $userEmail];
4c981f37
MW
313 }
314
315 $userID = CRM_Core_Session::singleton()->getLoggedInContactID();
b5bfb58f
SL
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.
be2fb01f 321 return [$userName, $userEmail];
b5bfb58f
SL
322 }
323
576fcb9c 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
6a488035 332}