3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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);
72 * @return null|object CRM_Core_BAO_Domain
76 static function &getDomain($reset = null) {
77 static $domain = NULL;
78 if (!$domain ||
$reset) {
79 $domain = new CRM_Core_BAO_Domain();
80 $domain->id
= CRM_Core_Config
::domainID();
81 if (!$domain->find(TRUE)) {
82 CRM_Core_Error
::fatal();
89 * Change active domain (ie. to perform a temporary action) such as changing
90 * config for all domains
92 * Switching around the global domain variable is very risky business. This
93 * is ONLY used as a hack to allow CRM_Core_BAO_Setting::setItems to manipulate
94 * the civicrm_domain.config_backend in multiple domains. When/if config_backend
95 * goes away, this hack should be removed.
97 * @param integer $domainID id for domain you want to set as current
99 * @see http://issues.civicrm.org/jira/browse/CRM-11204
101 static function setDomain($domainID){
102 CRM_Core_Config
::domainID($domainID);
103 self
::getDomain($domainID);
104 CRM_Core_Config
::singleton(TRUE, TRUE);
108 * Reset domain to default (ie. as loaded from settings). This is the
109 * counterpart to CRM_Core_BAO_Domain::setDomain.
111 * @param integer $domainID id for domain you want to set as current
113 * @see CRM_Core_BAO_Domain::setDomain
115 static function resetDomain(){
116 CRM_Core_Config
::domainID(null, true);
117 self
::getDomain(null, true);
118 CRM_Core_Config
::singleton(TRUE, TRUE);
121 static function version( $skipUsingCache = false ) {
122 return CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Domain',
123 CRM_Core_Config
::domainID(),
131 * Get the location values of a domain
135 * @return array Location::getValues
138 function &getLocationValues() {
139 if ($this->_location
== NULL) {
140 $domain = self
::getDomain(null);
142 'contact_id' => $domain->contact_id
144 $this->_location
= CRM_Core_BAO_Location
::getValues($params, TRUE);
146 if (empty($this->_location
)) {
147 $this->_location
= NULL;
150 return $this->_location
;
154 * Save the values of a domain
156 * @return domain array
159 static function edit(&$params, &$id) {
160 $domain = new CRM_Core_DAO_Domain();
162 $domain->copyValues($params);
168 * Create a new domain
170 * @return domain array
173 static function create($params) {
174 $domain = new CRM_Core_DAO_Domain();
175 $domain->copyValues($params);
180 static function multipleDomains() {
181 $session = CRM_Core_Session
::singleton();
183 $numberDomains = $session->get('numberDomains');
184 if (!$numberDomains) {
185 $query = "SELECT count(*) from civicrm_domain";
186 $numberDomains = CRM_Core_DAO
::singleValueQuery($query);
187 $session->set('numberDomains', $numberDomains);
189 return $numberDomains > 1 ?
TRUE : FALSE;
192 static function getNameAndEmail($skipFatal = FALSE) {
193 $fromEmailAddress = CRM_Core_OptionGroup
::values('from_email_address', NULL, NULL, NULL, ' AND is_default = 1');
194 if (!empty($fromEmailAddress)) {
195 foreach ($fromEmailAddress as $key => $value) {
196 $email = CRM_Utils_Mail
::pluckEmailFromHeader($value);
197 $fromArray = explode('"', $value);
198 $fromName = CRM_Utils_Array
::value(1, $fromArray);
201 return array($fromName, $email);
203 elseif ($skipFatal) {
204 return array('', '');
207 $url = CRM_Utils_System
::url('civicrm/admin/domain',
208 'action=update&reset=1'
210 $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));
212 CRM_Core_Error
::fatal($status);
215 static function addContactToDomainGroup($contactID) {
216 $groupID = self
::getGroupId();
219 $contactIDs = array($contactID);
220 CRM_Contact_BAO_GroupContact
::addContactsToGroup($contactIDs, $groupID);
227 static function getGroupId() {
228 static $groupID = NULL;
234 $domainGroupID = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::MULTISITE_PREFERENCES_NAME
,
237 $multisite = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::MULTISITE_PREFERENCES_NAME
,
241 if ($domainGroupID) {
242 $groupID = $domainGroupID;
244 elseif ($multisite) {
245 // create a group with that of domain name
246 $title = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Domain',
247 CRM_Core_Config
::domainID(), 'name'
249 $groupID = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Group',
250 $title, 'id', 'title', true
252 if (empty($groupID) && !empty($title)) {
253 $groupParams = array(
258 $group = CRM_Contact_BAO_Group
::create($groupParams);
259 $groupID = $group->id
;
262 return $groupID ?
$groupID : FALSE;
265 static function isDomainGroup($groupId) {
266 $domainGroupID = self
::getGroupId();
267 return $domainGroupID == $groupId ?
TRUE : FALSE;
270 static function getChildGroupIds() {
271 $domainGroupID = self
::getGroupId();
272 $childGrps = array();
274 if ($domainGroupID) {
275 $childGrps = CRM_Contact_BAO_GroupNesting
::getChildGroupIds($domainGroupID);
276 $childGrps[] = $domainGroupID;
281 // function to retrieve a list of contact-ids that belongs to current domain/site.
282 static function getContactList() {
283 $siteGroups = CRM_Core_BAO_Domain
::getChildGroupIds();
284 $siteContacts = array();
286 if (!empty($siteGroups)) {
289 FROM civicrm_contact cc
290 INNER JOIN civicrm_group_contact gc ON
291 (gc.contact_id = cc.id AND gc.status = 'Added' AND gc.group_id IN (" . implode(',', $siteGroups) . "))";
293 $dao = CRM_Core_DAO
::executeQuery($query);
294 while ($dao->fetch()) {
295 $siteContacts[] = $dao->id
;
298 return $siteContacts;