Merge pull request #15199 from civicrm/5.17
[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 CRM_Utils_Hook::pre('edit', 'Domain', CRM_Utils_Array::value('id', $params), $params);
131 $domain = new CRM_Core_DAO_Domain();
132 $domain->id = $id;
133 $domain->copyValues($params);
134 $domain->save();
135 CRM_Utils_Hook::post('edit', 'Domain', $domain->id, $domain);
136 return $domain;
137 }
138
139 /**
140 * Create a new domain.
141 *
142 * @param array $params
143 *
144 * @return array
145 * domain
146 */
147 public static function create($params) {
148 $hook = empty($params['id']) ? 'create' : 'edit';
149 CRM_Utils_Hook::pre($hook, 'Domain', CRM_Utils_Array::value('id', $params), $params);
150 $domain = new CRM_Core_DAO_Domain();
151 $domain->copyValues($params, TRUE);
152 $domain->save();
153 CRM_Utils_Hook::post($hook, 'Domain', $domain->id, $domain);
154 return $domain;
155 }
156
157 /**
158 * @return bool
159 */
160 public static function multipleDomains() {
161 $session = CRM_Core_Session::singleton();
162
163 $numberDomains = $session->get('numberDomains');
164 if (!$numberDomains) {
165 $query = "SELECT count(*) from civicrm_domain";
166 $numberDomains = CRM_Core_DAO::singleValueQuery($query);
167 $session->set('numberDomains', $numberDomains);
168 }
169 return $numberDomains > 1 ? TRUE : FALSE;
170 }
171
172 /**
173 * @param bool $skipFatal
174 * @param bool $returnString
175 * @return array
176 * name & email for domain
177 * @throws Exception
178 */
179 public static function getNameAndEmail($skipFatal = FALSE, $returnString = FALSE) {
180 $fromEmailAddress = CRM_Core_OptionGroup::values('from_email_address', NULL, NULL, NULL, ' AND is_default = 1');
181 if (!empty($fromEmailAddress)) {
182 if ($returnString) {
183 // Return a string like: "Demonstrators Anonymous" <info@example.org>
184 return $fromEmailAddress;
185 }
186 foreach ($fromEmailAddress as $key => $value) {
187 $email = CRM_Utils_Mail::pluckEmailFromHeader($value);
188 $fromArray = explode('"', $value);
189 $fromName = CRM_Utils_Array::value(1, $fromArray);
190 break;
191 }
192 return [$fromName, $email];
193 }
194
195 if ($skipFatal) {
196 return [NULL, NULL];
197 }
198
199 $url = CRM_Utils_System::url('civicrm/admin/options/from_email_address',
200 'reset=1'
201 );
202 $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]);
203
204 CRM_Core_Error::fatal($status);
205 }
206
207 /**
208 * @param int $contactID
209 *
210 * @return bool|null|object|string
211 */
212 public static function addContactToDomainGroup($contactID) {
213 $groupID = self::getGroupId();
214
215 if ($groupID) {
216 $contactIDs = [$contactID];
217 CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIDs, $groupID);
218
219 return $groupID;
220 }
221 return FALSE;
222 }
223
224 /**
225 * @return bool|null|object|string
226 */
227 public static function getGroupId() {
228 static $groupID = NULL;
229
230 if ($groupID) {
231 return $groupID;
232 }
233
234 $domainGroupID = Civi::settings()->get('domain_group_id');
235 $multisite = Civi::settings()->get('is_enabled');
236
237 if ($domainGroupID) {
238 $groupID = $domainGroupID;
239 }
240 elseif ($multisite) {
241 // create a group with that of domain name
242 $title = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain',
243 CRM_Core_Config::domainID(), 'name'
244 );
245 $groupID = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group',
246 $title, 'id', 'title', TRUE
247 );
248 }
249 return $groupID ? $groupID : FALSE;
250 }
251
252 /**
253 * @param int $groupId
254 *
255 * @return bool
256 */
257 public static function isDomainGroup($groupId) {
258 $domainGroupID = self::getGroupId();
259 return $domainGroupID == $groupId ? TRUE : FALSE;
260 }
261
262 /**
263 * @return array
264 */
265 public static function getChildGroupIds() {
266 $domainGroupID = self::getGroupId();
267 $childGrps = [];
268
269 if ($domainGroupID) {
270 $childGrps = CRM_Contact_BAO_GroupNesting::getChildGroupIds($domainGroupID);
271 $childGrps[] = $domainGroupID;
272 }
273 return $childGrps;
274 }
275
276 /**
277 * Retrieve a list of contact-ids that belongs to current domain/site.
278 *
279 * @return array
280 */
281 public static function getContactList() {
282 $siteGroups = CRM_Core_BAO_Domain::getChildGroupIds();
283 $siteContacts = [];
284
285 if (!empty($siteGroups)) {
286 $query = "
287 SELECT cc.id
288 FROM civicrm_contact cc
289 INNER JOIN civicrm_group_contact gc ON
290 (gc.contact_id = cc.id AND gc.status = 'Added' AND gc.group_id IN (" . implode(',', $siteGroups) . "))";
291
292 $dao = CRM_Core_DAO::executeQuery($query);
293 while ($dao->fetch()) {
294 $siteContacts[] = $dao->id;
295 }
296 }
297 return $siteContacts;
298 }
299
300 /**
301 * CRM-20308 & CRM-19657
302 * Return domain information / user information for the usage in receipts
303 * Try default from address then fall back to using logged in user details
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 }