3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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-2018
37 class CRM_Core_BAO_Domain
extends CRM_Core_DAO_Domain
{
40 * Cache for the current domain object.
42 static $_domain = NULL;
45 * Cache for a domain's location array
47 private $_location = NULL;
50 * Fetch object based on array of properties.
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.
57 * @return CRM_Core_DAO_Domain
59 public static function retrieve(&$params, &$defaults) {
60 return CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_Domain', $params, $defaults);
68 * @return \CRM_Core_BAO_Domain
69 * @throws \CRM_Core_Exception
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');
84 * @param bool $skipUsingCache
88 public static function version($skipUsingCache = FALSE) {
89 return CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Domain',
90 CRM_Core_Config
::domainID(),
98 * Get the location values of a domain.
101 * Location::getValues
103 public function &getLocationValues() {
104 if ($this->_location
== NULL) {
105 $domain = self
::getDomain(NULL);
107 'contact_id' => $domain->contact_id
,
109 $this->_location
= CRM_Core_BAO_Location
::getValues($params, TRUE);
111 if (empty($this->_location
)) {
112 $this->_location
= NULL;
115 return $this->_location
;
119 * Save the values of a domain.
121 * @param array $params
127 public static function edit(&$params, &$id) {
128 $domain = new CRM_Core_DAO_Domain();
130 $domain->copyValues($params);
136 * Create a new domain.
138 * @param array $params
143 public static function create($params) {
144 $domain = new CRM_Core_DAO_Domain();
145 $domain->copyValues($params, TRUE);
153 public static function multipleDomains() {
154 $session = CRM_Core_Session
::singleton();
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);
162 return $numberDomains > 1 ?
TRUE : FALSE;
166 * @param bool $skipFatal
169 * name & email for domain
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)) {
176 // Return a string like: "Demonstrators Anonymous" <info@example.org>
177 return $fromEmailAddress;
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);
185 return array($fromName, $email);
189 return array(NULL, NULL);
192 $url = CRM_Utils_System
::url('civicrm/admin/options/from_email_address',
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));
197 CRM_Core_Error
::fatal($status);
201 * @param int $contactID
203 * @return bool|null|object|string
205 public static function addContactToDomainGroup($contactID) {
206 $groupID = self
::getGroupId();
209 $contactIDs = array($contactID);
210 CRM_Contact_BAO_GroupContact
::addContactsToGroup($contactIDs, $groupID);
218 * @return bool|null|object|string
220 public static function getGroupId() {
221 static $groupID = NULL;
227 $domainGroupID = Civi
::settings()->get('domain_group_id');
228 $multisite = Civi
::settings()->get('is_enabled');
230 if ($domainGroupID) {
231 $groupID = $domainGroupID;
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'
238 $groupID = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Group',
239 $title, 'id', 'title', TRUE
242 return $groupID ?
$groupID : FALSE;
246 * @param int $groupId
250 public static function isDomainGroup($groupId) {
251 $domainGroupID = self
::getGroupId();
252 return $domainGroupID == $groupId ?
TRUE : FALSE;
258 public static function getChildGroupIds() {
259 $domainGroupID = self
::getGroupId();
260 $childGrps = array();
262 if ($domainGroupID) {
263 $childGrps = CRM_Contact_BAO_GroupNesting
::getChildGroupIds($domainGroupID);
264 $childGrps[] = $domainGroupID;
270 * Retrieve a list of contact-ids that belongs to current domain/site.
274 public static function getContactList() {
275 $siteGroups = CRM_Core_BAO_Domain
::getChildGroupIds();
276 $siteContacts = array();
278 if (!empty($siteGroups)) {
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) . "))";
285 $dao = CRM_Core_DAO
::executeQuery($query);
286 while ($dao->fetch()) {
287 $siteContacts[] = $dao->id
;
290 return $siteContacts;
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
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']);
303 if (!empty($domain['domain_email'])) {
304 return array($domain['name'], $domain['domain_email']);
309 if (!Civi
::settings()->get('allow_mail_from_logged_in_contact')) {
310 return array($userName, $userEmail);
313 $userID = CRM_Core_Session
::singleton()->getLoggedInContactID();
314 if (!empty($userID)) {
315 list($userName, $userEmail) = CRM_Contact_BAO_Contact_Location
::getEmailDetails($userID);
317 // If still empty fall back to the logged in user details.
318 // return empty values no matter what.
319 return array($userName, $userEmail);
323 * Get address to be used for system from addresses when a reply is not expected.
325 public static function getNoReplyEmailAddress() {
326 $emailDomain = CRM_Core_BAO_MailSettings
::defaultDomain();
327 return "do-not-reply@$emailDomain";