3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
37 class CRM_Core_BAO_Domain
extends CRM_Core_DAO_Domain
{
40 * Cache for the current domain object.
43 public static $_domain = NULL;
46 * Cache for a domain's location array
49 private $_location = NULL;
52 * Fetch object based on array of properties.
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.
59 * @return CRM_Core_DAO_Domain
61 public static function retrieve(&$params, &$defaults) {
62 return CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_Domain', $params, $defaults);
70 * @return \CRM_Core_BAO_Domain
71 * @throws \CRM_Core_Exception
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');
86 * @param bool $skipUsingCache
90 public static function version($skipUsingCache = FALSE) {
91 return CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Domain',
92 CRM_Core_Config
::domainID(),
100 * Get the location values of a domain.
103 * Location::getValues
105 public function &getLocationValues() {
106 if ($this->_location
== NULL) {
107 $domain = self
::getDomain(NULL);
109 'contact_id' => $domain->contact_id
,
111 $this->_location
= CRM_Core_BAO_Location
::getValues($params, TRUE);
113 if (empty($this->_location
)) {
114 $this->_location
= NULL;
117 return $this->_location
;
121 * Save the values of a domain.
123 * @param array $params
129 public static function edit(&$params, &$id) {
130 $domain = new CRM_Core_DAO_Domain();
132 $domain->copyValues($params);
138 * Create a new domain.
140 * @param array $params
145 public static function create($params) {
146 $domain = new CRM_Core_DAO_Domain();
147 $domain->copyValues($params, TRUE);
155 public static function multipleDomains() {
156 $session = CRM_Core_Session
::singleton();
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);
164 return $numberDomains > 1 ?
TRUE : FALSE;
168 * @param bool $skipFatal
169 * @param bool $returnString
171 * name & email for domain
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)) {
178 // Return a string like: "Demonstrators Anonymous" <info@example.org>
179 return $fromEmailAddress;
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);
187 return [$fromName, $email];
194 $url = CRM_Utils_System
::url('civicrm/admin/options/from_email_address',
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]);
199 CRM_Core_Error
::fatal($status);
203 * @param int $contactID
205 * @return bool|null|object|string
207 public static function addContactToDomainGroup($contactID) {
208 $groupID = self
::getGroupId();
211 $contactIDs = [$contactID];
212 CRM_Contact_BAO_GroupContact
::addContactsToGroup($contactIDs, $groupID);
220 * @return bool|null|object|string
222 public static function getGroupId() {
223 static $groupID = NULL;
229 $domainGroupID = Civi
::settings()->get('domain_group_id');
230 $multisite = Civi
::settings()->get('is_enabled');
232 if ($domainGroupID) {
233 $groupID = $domainGroupID;
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'
240 $groupID = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Group',
241 $title, 'id', 'title', TRUE
244 return $groupID ?
$groupID : FALSE;
248 * @param int $groupId
252 public static function isDomainGroup($groupId) {
253 $domainGroupID = self
::getGroupId();
254 return $domainGroupID == $groupId ?
TRUE : FALSE;
260 public static function getChildGroupIds() {
261 $domainGroupID = self
::getGroupId();
264 if ($domainGroupID) {
265 $childGrps = CRM_Contact_BAO_GroupNesting
::getChildGroupIds($domainGroupID);
266 $childGrps[] = $domainGroupID;
272 * Retrieve a list of contact-ids that belongs to current domain/site.
276 public static function getContactList() {
277 $siteGroups = CRM_Core_BAO_Domain
::getChildGroupIds();
280 if (!empty($siteGroups)) {
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) . "))";
287 $dao = CRM_Core_DAO
::executeQuery($query);
288 while ($dao->fetch()) {
289 $siteContacts[] = $dao->id
;
292 return $siteContacts;
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
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']];
305 if (!empty($domain['domain_email'])) {
306 return [$domain['name'], $domain['domain_email']];
311 if (!Civi
::settings()->get('allow_mail_from_logged_in_contact')) {
312 return [$userName, $userEmail];
315 $userID = CRM_Core_Session
::singleton()->getLoggedInContactID();
316 if (!empty($userID)) {
317 list($userName, $userEmail) = CRM_Contact_BAO_Contact_Location
::getEmailDetails($userID);
319 // If still empty fall back to the logged in user details.
320 // return empty values no matter what.
321 return [$userName, $userEmail];
325 * Get address to be used for system from addresses when a reply is not expected.
327 public static function getNoReplyEmailAddress() {
328 $emailDomain = CRM_Core_BAO_MailSettings
::defaultDomain();
329 return "do-not-reply@$emailDomain";