3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
21 class CRM_Core_BAO_Domain
extends CRM_Core_DAO_Domain
{
24 * Cache for the current domain object.
27 public static $_domain = NULL;
30 * Cache for a domain's location array
33 private $_location = NULL;
36 * Fetch object based on array of properties.
38 * @param array $params
39 * (reference ) an assoc array of name/value pairs.
40 * @param array $defaults
41 * (reference ) an assoc array to hold the flattened values.
43 * @return CRM_Core_DAO_Domain
45 public static function retrieve(&$params, &$defaults) {
46 return CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_Domain', $params, $defaults);
54 * @return \CRM_Core_BAO_Domain
55 * @throws \CRM_Core_Exception
57 public static function getDomain($reset = NULL) {
58 static $domain = NULL;
59 if (!$domain ||
$reset) {
60 $domain = new CRM_Core_BAO_Domain();
61 $domain->id
= CRM_Core_Config
::domainID();
62 if (!$domain->find(TRUE)) {
63 throw new CRM_Core_Exception('No domain in DB');
70 * @param bool $skipUsingCache
74 public static function version($skipUsingCache = FALSE) {
75 return CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Domain',
76 CRM_Core_Config
::domainID(),
84 * Get the location values of a domain.
89 public function &getLocationValues() {
90 if ($this->_location
== NULL) {
91 $domain = self
::getDomain(NULL);
93 'contact_id' => $domain->contact_id
,
95 $this->_location
= CRM_Core_BAO_Location
::getValues($params, TRUE);
97 if (empty($this->_location
)) {
98 $this->_location
= NULL;
101 return $this->_location
;
105 * Save the values of a domain.
107 * @param array $params
113 public static function edit(&$params, &$id) {
114 CRM_Utils_Hook
::pre('edit', 'Domain', CRM_Utils_Array
::value('id', $params), $params);
115 $domain = new CRM_Core_DAO_Domain();
117 $domain->copyValues($params);
119 CRM_Utils_Hook
::post('edit', 'Domain', $domain->id
, $domain);
124 * Create a new domain.
126 * @param array $params
131 public static function create($params) {
132 $hook = empty($params['id']) ?
'create' : 'edit';
133 CRM_Utils_Hook
::pre($hook, 'Domain', CRM_Utils_Array
::value('id', $params), $params);
134 $domain = new CRM_Core_DAO_Domain();
135 $domain->copyValues($params);
137 CRM_Utils_Hook
::post($hook, 'Domain', $domain->id
, $domain);
144 public static function multipleDomains() {
145 $session = CRM_Core_Session
::singleton();
147 $numberDomains = $session->get('numberDomains');
148 if (!$numberDomains) {
149 $query = "SELECT count(*) from civicrm_domain";
150 $numberDomains = CRM_Core_DAO
::singleValueQuery($query);
151 $session->set('numberDomains', $numberDomains);
153 return $numberDomains > 1 ?
TRUE : FALSE;
157 * @param bool $skipFatal
158 * @param bool $returnString
160 * name & email for domain
163 public static function getNameAndEmail($skipFatal = FALSE, $returnString = FALSE) {
164 $fromEmailAddress = CRM_Core_OptionGroup
::values('from_email_address', NULL, NULL, NULL, ' AND is_default = 1');
165 if (!empty($fromEmailAddress)) {
167 // Return a string like: "Demonstrators Anonymous" <info@example.org>
168 return $fromEmailAddress;
170 foreach ($fromEmailAddress as $key => $value) {
171 $email = CRM_Utils_Mail
::pluckEmailFromHeader($value);
172 $fromArray = explode('"', $value);
173 $fromName = CRM_Utils_Array
::value(1, $fromArray);
176 return [$fromName, $email];
183 $url = CRM_Utils_System
::url('civicrm/admin/options/from_email_address',
186 $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]);
188 CRM_Core_Error
::fatal($status);
192 * @param int $contactID
194 * @return bool|null|object|string
196 public static function addContactToDomainGroup($contactID) {
197 $groupID = self
::getGroupId();
200 $contactIDs = [$contactID];
201 CRM_Contact_BAO_GroupContact
::addContactsToGroup($contactIDs, $groupID);
209 * @return bool|null|object|string
211 public static function getGroupId() {
212 static $groupID = NULL;
218 $domainGroupID = Civi
::settings()->get('domain_group_id');
219 $multisite = Civi
::settings()->get('is_enabled');
221 if ($domainGroupID) {
222 $groupID = $domainGroupID;
224 elseif ($multisite) {
225 // create a group with that of domain name
226 $title = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Domain',
227 CRM_Core_Config
::domainID(), 'name'
229 $groupID = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Group',
230 $title, 'id', 'title', TRUE
233 return $groupID ?
$groupID : FALSE;
237 * @param int $groupId
241 public static function isDomainGroup($groupId) {
242 $domainGroupID = self
::getGroupId();
243 return $domainGroupID == $groupId ?
TRUE : FALSE;
249 public static function getChildGroupIds() {
250 $domainGroupID = self
::getGroupId();
253 if ($domainGroupID) {
254 $childGrps = CRM_Contact_BAO_GroupNesting
::getChildGroupIds($domainGroupID);
255 $childGrps[] = $domainGroupID;
261 * Retrieve a list of contact-ids that belongs to current domain/site.
265 public static function getContactList() {
266 $siteGroups = CRM_Core_BAO_Domain
::getChildGroupIds();
269 if (!empty($siteGroups)) {
272 FROM civicrm_contact cc
273 INNER JOIN civicrm_group_contact gc ON
274 (gc.contact_id = cc.id AND gc.status = 'Added' AND gc.group_id IN (" . implode(',', $siteGroups) . "))";
276 $dao = CRM_Core_DAO
::executeQuery($query);
277 while ($dao->fetch()) {
278 $siteContacts[] = $dao->id
;
281 return $siteContacts;
285 * CRM-20308 & CRM-19657
286 * Return domain information / user information for the usage in receipts
287 * Try default from address then fall back to using logged in user details
289 public static function getDefaultReceiptFrom() {
290 $domain = civicrm_api3('domain', 'getsingle', ['id' => CRM_Core_Config
::domainID()]);
291 if (!empty($domain['from_email'])) {
292 return [$domain['from_name'], $domain['from_email']];
294 if (!empty($domain['domain_email'])) {
295 return [$domain['name'], $domain['domain_email']];
300 if (!Civi
::settings()->get('allow_mail_from_logged_in_contact')) {
301 return [$userName, $userEmail];
304 $userID = CRM_Core_Session
::singleton()->getLoggedInContactID();
305 if (!empty($userID)) {
306 list($userName, $userEmail) = CRM_Contact_BAO_Contact_Location
::getEmailDetails($userID);
308 // If still empty fall back to the logged in user details.
309 // return empty values no matter what.
310 return [$userName, $userEmail];
314 * Get address to be used for system from addresses when a reply is not expected.
316 public static function getNoReplyEmailAddress() {
317 $emailDomain = CRM_Core_BAO_MailSettings
::defaultDomain();
318 return "do-not-reply@$emailDomain";