3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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 * Takes a bunch of params that are needed to match certain criteria and
53 * retrieves the relevant objects. Typically the valid params are only
54 * contact_id. We'll tweak this function to be more full featured over a period
55 * of time. This is the inverse function of create. It also stores all the retrieved
56 * values in the default array
58 * @param array $params (reference ) an assoc array of name/value pairs
59 * @param array $defaults (reference ) an assoc array to hold the flattened values
61 * @return object CRM_Core_DAO_Domain object
65 static function retrieve(&$params, &$defaults) {
66 return CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_Domain', $params, $defaults);
74 * @return null|object CRM_Core_BAO_Domain
78 static function &getDomain($reset = null) {
79 static $domain = NULL;
80 if (!$domain ||
$reset) {
81 $domain = new CRM_Core_BAO_Domain();
82 $domain->id
= CRM_Core_Config
::domainID();
83 if (!$domain->find(TRUE)) {
84 CRM_Core_Error
::fatal();
91 * Change active domain (ie. to perform a temporary action) such as changing
92 * config for all domains
94 * Switching around the global domain variable is very risky business. This
95 * is ONLY used as a hack to allow CRM_Core_BAO_Setting::setItems to manipulate
96 * the civicrm_domain.config_backend in multiple domains. When/if config_backend
97 * goes away, this hack should be removed.
99 * @param integer $domainID id for domain you want to set as current
101 * @see http://issues.civicrm.org/jira/browse/CRM-11204
103 static function setDomain($domainID){
104 CRM_Core_Config
::domainID($domainID);
105 self
::getDomain($domainID);
106 CRM_Core_Config
::singleton(TRUE, TRUE);
110 * Reset domain to default (ie. as loaded from settings). This is the
111 * counterpart to CRM_Core_BAO_Domain::setDomain.
113 * @internal param int $domainID id for domain you want to set as current
115 * @see CRM_Core_BAO_Domain::setDomain
117 static function resetDomain(){
118 CRM_Core_Config
::domainID(null, true);
119 self
::getDomain(null, true);
120 CRM_Core_Config
::singleton(TRUE, TRUE);
124 * @param bool $skipUsingCache
126 * @return null|string
128 static function version( $skipUsingCache = false ) {
129 return CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Domain',
130 CRM_Core_Config
::domainID(),
138 * Get the location values of a domain
142 * @return array Location::getValues
145 function &getLocationValues() {
146 if ($this->_location
== NULL) {
147 $domain = self
::getDomain(null);
149 'contact_id' => $domain->contact_id
151 $this->_location
= CRM_Core_BAO_Location
::getValues($params, TRUE);
153 if (empty($this->_location
)) {
154 $this->_location
= NULL;
157 return $this->_location
;
161 * Save the values of a domain
166 * @return domain array
169 static function edit(&$params, &$id) {
170 $domain = new CRM_Core_DAO_Domain();
172 $domain->copyValues($params);
178 * Create a new domain
182 * @return domain array
185 static function create($params) {
186 $domain = new CRM_Core_DAO_Domain();
187 $domain->copyValues($params);
195 static function multipleDomains() {
196 $session = CRM_Core_Session
::singleton();
198 $numberDomains = $session->get('numberDomains');
199 if (!$numberDomains) {
200 $query = "SELECT count(*) from civicrm_domain";
201 $numberDomains = CRM_Core_DAO
::singleValueQuery($query);
202 $session->set('numberDomains', $numberDomains);
204 return $numberDomains > 1 ?
TRUE : FALSE;
208 * @param bool $skipFatal
212 static function getNameAndEmail($skipFatal = FALSE) {
213 $fromEmailAddress = CRM_Core_OptionGroup
::values('from_email_address', NULL, NULL, NULL, ' AND is_default = 1');
214 if (!empty($fromEmailAddress)) {
215 foreach ($fromEmailAddress as $key => $value) {
216 $email = CRM_Utils_Mail
::pluckEmailFromHeader($value);
217 $fromArray = explode('"', $value);
218 $fromName = CRM_Utils_Array
::value(1, $fromArray);
221 return array($fromName, $email);
223 elseif ($skipFatal) {
224 return array('', '');
227 $url = CRM_Utils_System
::url('civicrm/admin/domain',
228 'action=update&reset=1'
230 $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));
232 CRM_Core_Error
::fatal($status);
238 * @return bool|null|object|string
240 static function addContactToDomainGroup($contactID) {
241 $groupID = self
::getGroupId();
244 $contactIDs = array($contactID);
245 CRM_Contact_BAO_GroupContact
::addContactsToGroup($contactIDs, $groupID);
253 * @return bool|null|object|string
255 static function getGroupId() {
256 static $groupID = NULL;
262 $domainGroupID = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::MULTISITE_PREFERENCES_NAME
,
265 $multisite = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::MULTISITE_PREFERENCES_NAME
,
269 if ($domainGroupID) {
270 $groupID = $domainGroupID;
272 elseif ($multisite) {
273 // create a group with that of domain name
274 $title = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Domain',
275 CRM_Core_Config
::domainID(), 'name'
277 $groupID = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Group',
278 $title, 'id', 'title', true
280 if (empty($groupID) && !empty($title)) {
281 $groupParams = array(
286 $group = CRM_Contact_BAO_Group
::create($groupParams);
287 $groupID = $group->id
;
290 return $groupID ?
$groupID : FALSE;
298 static function isDomainGroup($groupId) {
299 $domainGroupID = self
::getGroupId();
300 return $domainGroupID == $groupId ?
TRUE : FALSE;
306 static function getChildGroupIds() {
307 $domainGroupID = self
::getGroupId();
308 $childGrps = array();
310 if ($domainGroupID) {
311 $childGrps = CRM_Contact_BAO_GroupNesting
::getChildGroupIds($domainGroupID);
312 $childGrps[] = $domainGroupID;
317 // function to retrieve a list of contact-ids that belongs to current domain/site.
321 static function getContactList() {
322 $siteGroups = CRM_Core_BAO_Domain
::getChildGroupIds();
323 $siteContacts = array();
325 if (!empty($siteGroups)) {
328 FROM civicrm_contact cc
329 INNER JOIN civicrm_group_contact gc ON
330 (gc.contact_id = cc.id AND gc.status = 'Added' AND gc.group_id IN (" . implode(',', $siteGroups) . "))";
332 $dao = CRM_Core_DAO
::executeQuery($query);
333 while ($dao->fetch()) {
334 $siteContacts[] = $dao->id
;
337 return $siteContacts;