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);
107 * Reset domain to default (ie. as loaded from settings). This is the
108 * counterpart to CRM_Core_BAO_Domain::setDomain.
110 * @param integer $domainID id for domain you want to set as current
112 * @see CRM_Core_BAO_Domain::setDomain
114 static function resetDomain(){
115 CRM_Core_Config
::domainID(null, true);
116 self
::getDomain(null, true);
119 static function version( $skipUsingCache = false ) {
120 return CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Domain',
121 CRM_Core_Config
::domainID(),
129 * Get the location values of a domain
133 * @return array Location::getValues
136 function &getLocationValues() {
137 if ($this->_location
== NULL) {
138 $domain = self
::getDomain(null);
140 'contact_id' => $domain->contact_id
142 $this->_location
= CRM_Core_BAO_Location
::getValues($params, TRUE);
144 if (empty($this->_location
)) {
145 $this->_location
= NULL;
148 return $this->_location
;
152 * Save the values of a domain
154 * @return domain array
157 static function edit(&$params, &$id) {
158 $domain = new CRM_Core_DAO_Domain();
160 $domain->copyValues($params);
166 * Create a new domain
168 * @return domain array
171 static function create($params) {
172 $domain = new CRM_Core_DAO_Domain();
173 $domain->copyValues($params);
178 static function multipleDomains() {
179 $session = CRM_Core_Session
::singleton();
181 $numberDomains = $session->get('numberDomains');
182 if (!$numberDomains) {
183 $query = "SELECT count(*) from civicrm_domain";
184 $numberDomains = CRM_Core_DAO
::singleValueQuery($query);
185 $session->set('numberDomains', $numberDomains);
187 return $numberDomains > 1 ?
TRUE : FALSE;
190 static function getNameAndEmail($skipFatal = FALSE) {
191 $fromEmailAddress = CRM_Core_OptionGroup
::values('from_email_address', NULL, NULL, NULL, ' AND is_default = 1');
192 if (!empty($fromEmailAddress)) {
193 foreach ($fromEmailAddress as $key => $value) {
194 $email = CRM_Utils_Mail
::pluckEmailFromHeader($value);
195 $fromArray = explode('"', $value);
196 $fromName = CRM_Utils_Array
::value(1, $fromArray);
199 return array($fromName, $email);
201 elseif ($skipFatal) {
202 return array('', '');
205 $url = CRM_Utils_System
::url('civicrm/admin/domain',
206 'action=update&reset=1'
208 $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));
210 CRM_Core_Error
::fatal($status);
213 static function addContactToDomainGroup($contactID) {
214 $groupID = self
::getGroupId();
217 $contactIDs = array($contactID);
218 CRM_Contact_BAO_GroupContact
::addContactsToGroup($contactIDs, $groupID);
225 static function getGroupId() {
226 static $groupID = NULL;
232 $domainGroupID = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::MULTISITE_PREFERENCES_NAME
,
235 $multisite = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::MULTISITE_PREFERENCES_NAME
,
239 if ($domainGroupID) {
240 $groupID = $domainGroupID;
242 elseif ($multisite) {
243 // create a group with that of domain name
244 $title = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Domain',
245 CRM_Core_Config
::domainID(), 'name'
247 $groupID = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Group',
248 $title, 'id', 'title', true
250 if (empty($groupID) && !empty($title)) {
251 $groupParams = array(
256 $group = CRM_Contact_BAO_Group
::create($groupParams);
257 $groupID = $group->id
;
260 return $groupID ?
$groupID : FALSE;
263 static function isDomainGroup($groupId) {
264 $domainGroupID = self
::getGroupId();
265 return $domainGroupID == $groupId ?
TRUE : FALSE;
268 static function getChildGroupIds() {
269 $domainGroupID = self
::getGroupId();
270 $childGrps = array();
272 if ($domainGroupID) {
273 $childGrps = CRM_Contact_BAO_GroupNesting
::getChildGroupIds($domainGroupID);
274 $childGrps[] = $domainGroupID;
279 // function to retrieve a list of contact-ids that belongs to current domain/site.
280 static function getContactList() {
281 $siteGroups = CRM_Core_BAO_Domain
::getChildGroupIds();
282 $siteContacts = array();
284 if (!empty($siteGroups)) {
287 FROM civicrm_contact cc
288 INNER JOIN civicrm_group_contact gc ON
289 (gc.contact_id = cc.id AND gc.status = 'Added' AND gc.group_id IN (" . implode(',', $siteGroups) . "))";
291 $dao = CRM_Core_DAO
::executeQuery($query);
292 while ($dao->fetch()) {
293 $siteContacts[] = $dao->id
;
296 return $siteContacts;