Merge pull request #13324 from agileware/CIVICRM-947
[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 */
42 static $_domain = NULL;
43
44 /**
45 * Cache for a domain's location array
46 */
47 private $_location = NULL;
48
49 /**
50 * Fetch object based on array of properties.
51 *
52 * @param array $params
53 * (reference ) an assoc array of name/value pairs.
54 * @param array $defaults
55 * (reference ) an assoc array to hold the flattened values.
56 *
57 * @return CRM_Core_DAO_Domain
58 */
59 public static function retrieve(&$params, &$defaults) {
60 return CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_Domain', $params, $defaults);
61 }
62
63 /**
64 * Get the domain BAO.
65 *
66 * @param bool $reset
67 *
68 * @return \CRM_Core_BAO_Domain
69 * @throws \CRM_Core_Exception
70 */
71 public static function getDomain($reset = NULL) {
72 static $domain = NULL;
73 if (!$domain || $reset) {
74 $domain = new CRM_Core_BAO_Domain();
75 $domain->id = CRM_Core_Config::domainID();
76 if (!$domain->find(TRUE)) {
77 throw new CRM_Core_Exception('No domain in DB');
78 }
79 }
80 return $domain;
81 }
82
83 /**
84 * @param bool $skipUsingCache
85 *
86 * @return null|string
87 */
88 public static function version($skipUsingCache = FALSE) {
89 return CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain',
90 CRM_Core_Config::domainID(),
91 'version',
92 'id',
93 $skipUsingCache
94 );
95 }
96
97 /**
98 * Get the location values of a domain.
99 *
100 * @return array
101 * Location::getValues
102 */
103 public function &getLocationValues() {
104 if ($this->_location == NULL) {
105 $domain = self::getDomain(NULL);
106 $params = array(
107 'contact_id' => $domain->contact_id,
108 );
109 $this->_location = CRM_Core_BAO_Location::getValues($params, TRUE);
110
111 if (empty($this->_location)) {
112 $this->_location = NULL;
113 }
114 }
115 return $this->_location;
116 }
117
118 /**
119 * Save the values of a domain.
120 *
121 * @param array $params
122 * @param int $id
123 *
124 * @return array
125 * domain
126 */
127 public static function edit(&$params, &$id) {
128 $domain = new CRM_Core_DAO_Domain();
129 $domain->id = $id;
130 $domain->copyValues($params);
131 $domain->save();
132 return $domain;
133 }
134
135 /**
136 * Create a new domain.
137 *
138 * @param array $params
139 *
140 * @return array
141 * domain
142 */
143 public static function create($params) {
144 $domain = new CRM_Core_DAO_Domain();
145 $domain->copyValues($params, TRUE);
146 $domain->save();
147 return $domain;
148 }
149
150 /**
151 * @return bool
152 */
153 public static function multipleDomains() {
154 $session = CRM_Core_Session::singleton();
155
156 $numberDomains = $session->get('numberDomains');
157 if (!$numberDomains) {
158 $query = "SELECT count(*) from civicrm_domain";
159 $numberDomains = CRM_Core_DAO::singleValueQuery($query);
160 $session->set('numberDomains', $numberDomains);
161 }
162 return $numberDomains > 1 ? TRUE : FALSE;
163 }
164
165 /**
166 * @param bool $skipFatal
167 *
168 * @return array
169 * name & email for domain
170 * @throws Exception
171 */
172 public static function getNameAndEmail($skipFatal = FALSE, $returnString = FALSE) {
173 $fromEmailAddress = CRM_Core_OptionGroup::values('from_email_address', NULL, NULL, NULL, ' AND is_default = 1');
174 if (!empty($fromEmailAddress)) {
175 if ($returnString) {
176 // Return a string like: "Demonstrators Anonymous" <info@example.org>
177 return $fromEmailAddress;
178 }
179 foreach ($fromEmailAddress as $key => $value) {
180 $email = CRM_Utils_Mail::pluckEmailFromHeader($value);
181 $fromArray = explode('"', $value);
182 $fromName = CRM_Utils_Array::value(1, $fromArray);
183 break;
184 }
185 return array($fromName, $email);
186 }
187
188 if ($skipFatal) {
189 return array(NULL, NULL);
190 }
191
192 $url = CRM_Utils_System::url('civicrm/admin/options/from_email_address',
193 'reset=1'
194 );
195 $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>", array(1 => $url));
196
197 CRM_Core_Error::fatal($status);
198 }
199
200 /**
201 * @param int $contactID
202 *
203 * @return bool|null|object|string
204 */
205 public static function addContactToDomainGroup($contactID) {
206 $groupID = self::getGroupId();
207
208 if ($groupID) {
209 $contactIDs = array($contactID);
210 CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIDs, $groupID);
211
212 return $groupID;
213 }
214 return FALSE;
215 }
216
217 /**
218 * @return bool|null|object|string
219 */
220 public static function getGroupId() {
221 static $groupID = NULL;
222
223 if ($groupID) {
224 return $groupID;
225 }
226
227 $domainGroupID = Civi::settings()->get('domain_group_id');
228 $multisite = Civi::settings()->get('is_enabled');
229
230 if ($domainGroupID) {
231 $groupID = $domainGroupID;
232 }
233 elseif ($multisite) {
234 // create a group with that of domain name
235 $title = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain',
236 CRM_Core_Config::domainID(), 'name'
237 );
238 $groupID = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group',
239 $title, 'id', 'title', TRUE
240 );
241 }
242 return $groupID ? $groupID : FALSE;
243 }
244
245 /**
246 * @param int $groupId
247 *
248 * @return bool
249 */
250 public static function isDomainGroup($groupId) {
251 $domainGroupID = self::getGroupId();
252 return $domainGroupID == $groupId ? TRUE : FALSE;
253 }
254
255 /**
256 * @return array
257 */
258 public static function getChildGroupIds() {
259 $domainGroupID = self::getGroupId();
260 $childGrps = array();
261
262 if ($domainGroupID) {
263 $childGrps = CRM_Contact_BAO_GroupNesting::getChildGroupIds($domainGroupID);
264 $childGrps[] = $domainGroupID;
265 }
266 return $childGrps;
267 }
268
269 /**
270 * Retrieve a list of contact-ids that belongs to current domain/site.
271 *
272 * @return array
273 */
274 public static function getContactList() {
275 $siteGroups = CRM_Core_BAO_Domain::getChildGroupIds();
276 $siteContacts = array();
277
278 if (!empty($siteGroups)) {
279 $query = "
280 SELECT cc.id
281 FROM civicrm_contact cc
282 INNER JOIN civicrm_group_contact gc ON
283 (gc.contact_id = cc.id AND gc.status = 'Added' AND gc.group_id IN (" . implode(',', $siteGroups) . "))";
284
285 $dao = CRM_Core_DAO::executeQuery($query);
286 while ($dao->fetch()) {
287 $siteContacts[] = $dao->id;
288 }
289 }
290 return $siteContacts;
291 }
292
293 /**
294 * CRM-20308 & CRM-19657
295 * Return domain information / user information for the usage in receipts
296 * Try default from address then fall back to using logged in user details
297 */
298 public static function getDefaultReceiptFrom() {
299 $domain = civicrm_api3('domain', 'getsingle', array('id' => CRM_Core_Config::domainID()));
300 if (!empty($domain['from_email'])) {
301 return array($domain['from_name'], $domain['from_email']);
302 }
303 if (!empty($domain['domain_email'])) {
304 return array($domain['name'], $domain['domain_email']);
305 }
306 $userName = '';
307 $userEmail = '';
308
309 if (!Civi::settings()->get('allow_mail_from_logged_in_contact')) {
310 return array($userName, $userEmail);
311 }
312
313 $userID = CRM_Core_Session::singleton()->getLoggedInContactID();
314 if (!empty($userID)) {
315 list($userName, $userEmail) = CRM_Contact_BAO_Contact_Location::getEmailDetails($userID);
316 }
317 // If still empty fall back to the logged in user details.
318 // return empty values no matter what.
319 return array($userName, $userEmail);
320 }
321
322 /**
323 * Get address to be used for system from addresses when a reply is not expected.
324 */
325 public static function getNoReplyEmailAddress() {
326 $emailDomain = CRM_Core_BAO_MailSettings::defaultDomain();
327 return "do-not-reply@$emailDomain";
328 }
329
330 }