a few comment fixes
[civicrm-core.git] / CRM / Core / BAO / Domain.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 *
38 */
39class CRM_Core_BAO_Domain extends CRM_Core_DAO_Domain {
40
41 /**
42 * Cache for the current domain object
43 */
44 static $_domain = NULL;
45
46 /**
47 * Cache for a domain's location array
48 */
49 private $_location = NULL;
50
51 /**
c490a46a 52 * Fetch object based on array of properties
6a488035 53 *
6a0b768e
TO
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.
6a488035 58 *
16b10e64 59 * @return CRM_Core_DAO_Domain
6a488035 60 */
00be9182 61 public static function retrieve(&$params, &$defaults) {
6a488035
TO
62 return CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_Domain', $params, $defaults);
63 }
64
65 /**
66 * Get the domain BAO
67 *
77b97be7
EM
68 * @param null $reset
69 *
72b3a70c 70 * @return CRM_Core_BAO_Domain|null
6a488035 71 */
2aa397bc 72 public static function &getDomain($reset = NULL) {
6a488035
TO
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();
79 }
80 }
81 return $domain;
82 }
83
2aa397bc
TO
84 /**
85 * Change active domain (ie. to perform a temporary action) such as changing
86 * config for all domains
87 *
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.
92 *
93 * @param int $domainID
94 * Id for domain you want to set as current.
95 * @deprecated
96 * @see http://issues.civicrm.org/jira/browse/CRM-11204
97 */
00be9182 98 public static function setDomain($domainID) {
6a488035
TO
99 CRM_Core_Config::domainID($domainID);
100 self::getDomain($domainID);
eb40b5a4 101 CRM_Core_Config::singleton(TRUE, TRUE);
6a488035
TO
102 }
103
104 /**
105 * Reset domain to default (ie. as loaded from settings). This is the
106 * counterpart to CRM_Core_BAO_Domain::setDomain.
107 *
6a488035
TO
108 * @deprecated
109 * @see CRM_Core_BAO_Domain::setDomain
110 */
00be9182 111 public static function resetDomain() {
2aa397bc
TO
112 CRM_Core_Config::domainID(NULL, TRUE);
113 self::getDomain(NULL, TRUE);
7583c3f3 114 CRM_Core_Config::singleton(TRUE, TRUE);
6a488035
TO
115 }
116
b5c2afd0
EM
117 /**
118 * @param bool $skipUsingCache
119 *
120 * @return null|string
121 */
2aa397bc 122 public static function version($skipUsingCache = FALSE) {
6a488035
TO
123 return CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain',
124 CRM_Core_Config::domainID(),
c490a46a
CW
125 'version',
126 'id',
127 $skipUsingCache
6a488035
TO
128 );
129 }
130
131 /**
132 * Get the location values of a domain
133 *
a6c01b45
CW
134 * @return array
135 * Location::getValues
6a488035 136 */
00be9182 137 public function &getLocationValues() {
6a488035 138 if ($this->_location == NULL) {
2aa397bc 139 $domain = self::getDomain(NULL);
6a488035 140 $params = array(
21dfd5f5 141 'contact_id' => $domain->contact_id,
6a488035
TO
142 );
143 $this->_location = CRM_Core_BAO_Location::getValues($params, TRUE);
144
145 if (empty($this->_location)) {
146 $this->_location = NULL;
147 }
148 }
149 return $this->_location;
150 }
151
152 /**
153 * Save the values of a domain
154 *
c490a46a 155 * @param array $params
100fef9d 156 * @param int $id
da6b46f4 157 *
a6c01b45
CW
158 * @return array
159 * domain
6a488035 160 */
00be9182 161 public static function edit(&$params, &$id) {
6a488035
TO
162 $domain = new CRM_Core_DAO_Domain();
163 $domain->id = $id;
164 $domain->copyValues($params);
165 $domain->save();
166 return $domain;
167 }
168
169 /**
170 * Create a new domain
171 *
c490a46a 172 * @param array $params
fd31fa4c 173 *
a6c01b45
CW
174 * @return array
175 * domain
6a488035 176 */
00be9182 177 public static function create($params) {
6a488035
TO
178 $domain = new CRM_Core_DAO_Domain();
179 $domain->copyValues($params);
180 $domain->save();
181 return $domain;
182 }
183
b5c2afd0
EM
184 /**
185 * @return bool
186 */
00be9182 187 public static function multipleDomains() {
6a488035
TO
188 $session = CRM_Core_Session::singleton();
189
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);
195 }
196 return $numberDomains > 1 ? TRUE : FALSE;
197 }
198
b5c2afd0
EM
199 /**
200 * @param bool $skipFatal
201 *
a6c01b45
CW
202 * @return array
203 * name & email for domain
b5c2afd0
EM
204 * @throws Exception
205 */
00be9182 206 public static function getNameAndEmail($skipFatal = FALSE) {
6a488035
TO
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) {
353ffa53 210 $email = CRM_Utils_Mail::pluckEmailFromHeader($value);
6a488035 211 $fromArray = explode('"', $value);
353ffa53 212 $fromName = CRM_Utils_Array::value(1, $fromArray);
6a488035
TO
213 break;
214 }
215 return array($fromName, $email);
216 }
217 elseif ($skipFatal) {
218 return array('', '');
219 }
220
221 $url = CRM_Utils_System::url('civicrm/admin/domain',
222 'action=update&reset=1'
223 );
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));
225
226 CRM_Core_Error::fatal($status);
227 }
228
b5c2afd0 229 /**
100fef9d 230 * @param int $contactID
b5c2afd0
EM
231 *
232 * @return bool|null|object|string
233 */
00be9182 234 public static function addContactToDomainGroup($contactID) {
6a488035
TO
235 $groupID = self::getGroupId();
236
237 if ($groupID) {
238 $contactIDs = array($contactID);
239 CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIDs, $groupID);
240
241 return $groupID;
242 }
243 return FALSE;
244 }
245
b5c2afd0
EM
246 /**
247 * @return bool|null|object|string
248 */
00be9182 249 public static function getGroupId() {
6a488035
TO
250 static $groupID = NULL;
251
252 if ($groupID) {
253 return $groupID;
254 }
255
256 $domainGroupID = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MULTISITE_PREFERENCES_NAME,
257 'domain_group_id'
258 );
259 $multisite = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MULTISITE_PREFERENCES_NAME,
260 'is_enabled'
261 );
262
263 if ($domainGroupID) {
264 $groupID = $domainGroupID;
265 }
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'
270 );
271 $groupID = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group',
2aa397bc 272 $title, 'id', 'title', TRUE
6a488035
TO
273 );
274 if (empty($groupID) && !empty($title)) {
275 $groupParams = array(
276 'title' => $title,
277 'is_active' => 1,
278 'no_parent' => 1,
279 );
280 $group = CRM_Contact_BAO_Group::create($groupParams);
281 $groupID = $group->id;
282 }
283 }
284 return $groupID ? $groupID : FALSE;
285 }
286
b5c2afd0 287 /**
100fef9d 288 * @param int $groupId
b5c2afd0
EM
289 *
290 * @return bool
291 */
00be9182 292 public static function isDomainGroup($groupId) {
6a488035
TO
293 $domainGroupID = self::getGroupId();
294 return $domainGroupID == $groupId ? TRUE : FALSE;
295 }
296
b5c2afd0
EM
297 /**
298 * @return array
299 */
00be9182 300 public static function getChildGroupIds() {
6a488035
TO
301 $domainGroupID = self::getGroupId();
302 $childGrps = array();
303
304 if ($domainGroupID) {
305 $childGrps = CRM_Contact_BAO_GroupNesting::getChildGroupIds($domainGroupID);
306 $childGrps[] = $domainGroupID;
307 }
308 return $childGrps;
309 }
310
b5c2afd0 311 /**
100fef9d 312 * Retrieve a list of contact-ids that belongs to current domain/site.
c490a46a 313 *
b5c2afd0
EM
314 * @return array
315 */
00be9182 316 public static function getContactList() {
6a488035
TO
317 $siteGroups = CRM_Core_BAO_Domain::getChildGroupIds();
318 $siteContacts = array();
319
320 if (!empty($siteGroups)) {
321 $query = "
322 SELECT cc.id
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) . "))";
326
327 $dao = CRM_Core_DAO::executeQuery($query);
328 while ($dao->fetch()) {
329 $siteContacts[] = $dao->id;
330 }
331 }
332 return $siteContacts;
333 }
96025800 334
6a488035 335}