3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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-2017
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);
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) {
173 $fromEmailAddress = CRM_Core_OptionGroup
::values('from_email_address', NULL, NULL, NULL, ' AND is_default = 1');
174 if (!empty($fromEmailAddress)) {
175 foreach ($fromEmailAddress as $key => $value) {
176 $email = CRM_Utils_Mail
::pluckEmailFromHeader($value);
177 $fromArray = explode('"', $value);
178 $fromName = CRM_Utils_Array
::value(1, $fromArray);
181 return array($fromName, $email);
183 elseif ($skipFatal) {
184 return array('', '');
187 $url = CRM_Utils_System
::url('civicrm/admin/domain',
188 'action=update&reset=1'
190 $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));
192 CRM_Core_Error
::fatal($status);
196 * @param int $contactID
198 * @return bool|null|object|string
200 public static function addContactToDomainGroup($contactID) {
201 $groupID = self
::getGroupId();
204 $contactIDs = array($contactID);
205 CRM_Contact_BAO_GroupContact
::addContactsToGroup($contactIDs, $groupID);
213 * @return bool|null|object|string
215 public static function getGroupId() {
216 static $groupID = NULL;
222 $domainGroupID = Civi
::settings()->get('domain_group_id');
223 $multisite = Civi
::settings()->get('is_enabled');
225 if ($domainGroupID) {
226 $groupID = $domainGroupID;
228 elseif ($multisite) {
229 // create a group with that of domain name
230 $title = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Domain',
231 CRM_Core_Config
::domainID(), 'name'
233 $groupID = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Group',
234 $title, 'id', 'title', TRUE
237 return $groupID ?
$groupID : FALSE;
241 * @param int $groupId
245 public static function isDomainGroup($groupId) {
246 $domainGroupID = self
::getGroupId();
247 return $domainGroupID == $groupId ?
TRUE : FALSE;
253 public static function getChildGroupIds() {
254 $domainGroupID = self
::getGroupId();
255 $childGrps = array();
257 if ($domainGroupID) {
258 $childGrps = CRM_Contact_BAO_GroupNesting
::getChildGroupIds($domainGroupID);
259 $childGrps[] = $domainGroupID;
265 * Retrieve a list of contact-ids that belongs to current domain/site.
269 public static function getContactList() {
270 $siteGroups = CRM_Core_BAO_Domain
::getChildGroupIds();
271 $siteContacts = array();
273 if (!empty($siteGroups)) {
276 FROM civicrm_contact cc
277 INNER JOIN civicrm_group_contact gc ON
278 (gc.contact_id = cc.id AND gc.status = 'Added' AND gc.group_id IN (" . implode(',', $siteGroups) . "))";
280 $dao = CRM_Core_DAO
::executeQuery($query);
281 while ($dao->fetch()) {
282 $siteContacts[] = $dao->id
;
285 return $siteContacts;
289 * CRM-20308 & CRM-19657
290 * Return domain information / user information for the useage in receipts
291 * Try default from adress then fall back to using logged in user details
293 public static function getDefaultReceiptFrom() {
294 $domain = civicrm_api3('domain', 'getsingle', array('id' => CRM_Core_Config
::domainID()));
295 if (!empty($domain['from_email'])) {
296 return array($domain['from_name'], $domain['from_email']);
298 if (!empty($domain['domain_email'])) {
299 return array($domain['name'], $domain['domain_email']);
301 $userID = CRM_Core_Session
::singleton()->getLoggedInContactID();
304 if (!empty($userID)) {
305 list($userName, $userEmail) = CRM_Contact_BAO_Contact_Location
::getEmailDetails($userID);
307 // If still empty fall back to the logged in user details.
308 // return empty values no matter what.
309 return array($userName, $userEmail);