3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
39 class CRM_Core_BAO_Domain
extends CRM_Core_DAO_Domain
{
42 * Cache for the current domain object.
44 static $_domain = NULL;
47 * 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|null
72 public static function &getDomain($reset = NULL) {
73 static $domain = NULL;
74 if (!$domain ||
$reset) {
75 $domain = new CRM_Core_BAO_Domain();
76 $domain->id
= CRM_Core_Config
::domainID();
77 if (!$domain->find(TRUE)) {
78 CRM_Core_Error
::fatal();
85 * Change active domain (ie. to perform a temporary action) such as changing
86 * config for all domains
88 * Switching around the global domain variable is very risky business. This
89 * is ONLY used as a hack to allow CRM_Core_BAO_Setting::setItems to manipulate
90 * the civicrm_domain.config_backend in multiple domains. When/if config_backend
91 * goes away, this hack should be removed.
93 * @param int $domainID
94 * Id for domain you want to set as current.
96 * @see http://issues.civicrm.org/jira/browse/CRM-11204
98 public static function setDomain($domainID) {
99 CRM_Core_Config
::domainID($domainID);
100 self
::getDomain($domainID);
101 CRM_Core_Config
::singleton(TRUE, TRUE);
105 * Reset domain to default (ie. as loaded from settings). This is the
106 * counterpart to CRM_Core_BAO_Domain::setDomain.
109 * @see CRM_Core_BAO_Domain::setDomain
111 public static function resetDomain() {
112 CRM_Core_Config
::domainID(NULL, TRUE);
113 self
::getDomain(NULL, TRUE);
114 CRM_Core_Config
::singleton(TRUE, TRUE);
118 * @param bool $skipUsingCache
120 * @return null|string
122 public static function version($skipUsingCache = FALSE) {
123 return CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Domain',
124 CRM_Core_Config
::domainID(),
132 * Get the location values of a domain.
135 * Location::getValues
137 public function &getLocationValues() {
138 if ($this->_location
== NULL) {
139 $domain = self
::getDomain(NULL);
141 'contact_id' => $domain->contact_id
,
143 $this->_location
= CRM_Core_BAO_Location
::getValues($params, TRUE);
145 if (empty($this->_location
)) {
146 $this->_location
= NULL;
149 return $this->_location
;
153 * Save the values of a domain.
155 * @param array $params
161 public static function edit(&$params, &$id) {
162 $domain = new CRM_Core_DAO_Domain();
164 $domain->copyValues($params);
170 * Create a new domain.
172 * @param array $params
177 public static function create($params) {
178 $domain = new CRM_Core_DAO_Domain();
179 $domain->copyValues($params);
187 public static function multipleDomains() {
188 $session = CRM_Core_Session
::singleton();
190 $numberDomains = $session->get('numberDomains');
191 if (!$numberDomains) {
192 $query = "SELECT count(*) from civicrm_domain";
193 $numberDomains = CRM_Core_DAO
::singleValueQuery($query);
194 $session->set('numberDomains', $numberDomains);
196 return $numberDomains > 1 ?
TRUE : FALSE;
200 * @param bool $skipFatal
203 * name & email for domain
206 public static function getNameAndEmail($skipFatal = FALSE) {
207 $fromEmailAddress = CRM_Core_OptionGroup
::values('from_email_address', NULL, NULL, NULL, ' AND is_default = 1');
208 if (!empty($fromEmailAddress)) {
209 foreach ($fromEmailAddress as $key => $value) {
210 $email = CRM_Utils_Mail
::pluckEmailFromHeader($value);
211 $fromArray = explode('"', $value);
212 $fromName = CRM_Utils_Array
::value(1, $fromArray);
215 return array($fromName, $email);
217 elseif ($skipFatal) {
218 return array('', '');
221 $url = CRM_Utils_System
::url('civicrm/admin/domain',
222 'action=update&reset=1'
224 $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));
226 CRM_Core_Error
::fatal($status);
230 * @param int $contactID
232 * @return bool|null|object|string
234 public static function addContactToDomainGroup($contactID) {
235 $groupID = self
::getGroupId();
238 $contactIDs = array($contactID);
239 CRM_Contact_BAO_GroupContact
::addContactsToGroup($contactIDs, $groupID);
247 * @return bool|null|object|string
249 public static function getGroupId() {
250 static $groupID = NULL;
256 $domainGroupID = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::MULTISITE_PREFERENCES_NAME
,
259 $multisite = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::MULTISITE_PREFERENCES_NAME
,
263 if ($domainGroupID) {
264 $groupID = $domainGroupID;
266 elseif ($multisite) {
267 // create a group with that of domain name
268 $title = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Domain',
269 CRM_Core_Config
::domainID(), 'name'
271 $groupID = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Group',
272 $title, 'id', 'title', TRUE
274 if (empty($groupID) && !empty($title)) {
275 $groupParams = array(
280 $group = CRM_Contact_BAO_Group
::create($groupParams);
281 $groupID = $group->id
;
284 return $groupID ?
$groupID : FALSE;
288 * @param int $groupId
292 public static function isDomainGroup($groupId) {
293 $domainGroupID = self
::getGroupId();
294 return $domainGroupID == $groupId ?
TRUE : FALSE;
300 public static function getChildGroupIds() {
301 $domainGroupID = self
::getGroupId();
302 $childGrps = array();
304 if ($domainGroupID) {
305 $childGrps = CRM_Contact_BAO_GroupNesting
::getChildGroupIds($domainGroupID);
306 $childGrps[] = $domainGroupID;
312 * Retrieve a list of contact-ids that belongs to current domain/site.
316 public static function getContactList() {
317 $siteGroups = CRM_Core_BAO_Domain
::getChildGroupIds();
318 $siteContacts = array();
320 if (!empty($siteGroups)) {
323 FROM civicrm_contact cc
324 INNER JOIN civicrm_group_contact gc ON
325 (gc.contact_id = cc.id AND gc.status = 'Added' AND gc.group_id IN (" . implode(',', $siteGroups) . "))";
327 $dao = CRM_Core_DAO
::executeQuery($query);
328 while ($dao->fetch()) {
329 $siteContacts[] = $dao->id
;
332 return $siteContacts;