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 CRM_Utils_Hook
::pre('edit', 'Domain', CRM_Utils_Array
::value('id', $params), $params);
131 $domain = new CRM_Core_DAO_Domain();
133 $domain->copyValues($params);
135 CRM_Utils_Hook
::post('edit', 'Domain', $domain->id
, $domain);
140 * Create a new domain.
142 * @param array $params
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);
153 CRM_Utils_Hook
::post($hook, 'Domain', $domain->id
, $domain);
160 public static function multipleDomains() {
161 $session = CRM_Core_Session
::singleton();
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);
169 return $numberDomains > 1 ?
TRUE : FALSE;
173 * @param bool $skipFatal
174 * @param bool $returnString
176 * name & email for domain
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)) {
183 // Return a string like: "Demonstrators Anonymous" <info@example.org>
184 return $fromEmailAddress;
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);
192 return [$fromName, $email];
199 $url = CRM_Utils_System
::url('civicrm/admin/options/from_email_address',
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]);
204 CRM_Core_Error
::fatal($status);
208 * @param int $contactID
210 * @return bool|null|object|string
212 public static function addContactToDomainGroup($contactID) {
213 $groupID = self
::getGroupId();
216 $contactIDs = [$contactID];
217 CRM_Contact_BAO_GroupContact
::addContactsToGroup($contactIDs, $groupID);
225 * @return bool|null|object|string
227 public static function getGroupId() {
228 static $groupID = NULL;
234 $domainGroupID = Civi
::settings()->get('domain_group_id');
235 $multisite = Civi
::settings()->get('is_enabled');
237 if ($domainGroupID) {
238 $groupID = $domainGroupID;
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'
245 $groupID = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Group',
246 $title, 'id', 'title', TRUE
249 return $groupID ?
$groupID : FALSE;
253 * @param int $groupId
257 public static function isDomainGroup($groupId) {
258 $domainGroupID = self
::getGroupId();
259 return $domainGroupID == $groupId ?
TRUE : FALSE;
265 public static function getChildGroupIds() {
266 $domainGroupID = self
::getGroupId();
269 if ($domainGroupID) {
270 $childGrps = CRM_Contact_BAO_GroupNesting
::getChildGroupIds($domainGroupID);
271 $childGrps[] = $domainGroupID;
277 * Retrieve a list of contact-ids that belongs to current domain/site.
281 public static function getContactList() {
282 $siteGroups = CRM_Core_BAO_Domain
::getChildGroupIds();
285 if (!empty($siteGroups)) {
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) . "))";
292 $dao = CRM_Core_DAO
::executeQuery($query);
293 while ($dao->fetch()) {
294 $siteContacts[] = $dao->id
;
297 return $siteContacts;
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
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']];
310 if (!empty($domain['domain_email'])) {
311 return [$domain['name'], $domain['domain_email']];
316 if (!Civi
::settings()->get('allow_mail_from_logged_in_contact')) {
317 return [$userName, $userEmail];
320 $userID = CRM_Core_Session
::singleton()->getLoggedInContactID();
321 if (!empty($userID)) {
322 list($userName, $userEmail) = CRM_Contact_BAO_Contact_Location
::getEmailDetails($userID);
324 // If still empty fall back to the logged in user details.
325 // return empty values no matter what.
326 return [$userName, $userEmail];
330 * Get address to be used for system from addresses when a reply is not expected.
332 public static function getNoReplyEmailAddress() {
333 $emailDomain = CRM_Core_BAO_MailSettings
::defaultDomain();
334 return "do-not-reply@$emailDomain";