be57068bc52564920edf8ef2c1e732b743bfd8ac
[civicrm-core.git] / CRM / Core / BAO / Domain.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2014
32 * $Id$
33 *
34 */
35
36 /**
37 *
38 */
39 class 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 /**
52 * Fetch object based on array of properties
53 *
54 * @param array $params (reference ) an assoc array of name/value pairs
55 * @param array $defaults (reference ) an assoc array to hold the flattened values
56 *
57 * @return CRM_Core_DAO_Domain object
58 * @static
59 */
60 public static function retrieve(&$params, &$defaults) {
61 return CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_Domain', $params, $defaults);
62 }
63
64 /**
65 * Get the domain BAO
66 *
67 * @param null $reset
68 *
69 * @return null|object CRM_Core_BAO_Domain
70 * @static
71 */
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();
79 }
80 }
81 return $domain;
82 }
83
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 integer $domainID id for domain you want to set as current
94 * @deprecated
95 * @see http://issues.civicrm.org/jira/browse/CRM-11204
96 */
97 public static function setDomain($domainID) {
98 CRM_Core_Config::domainID($domainID);
99 self::getDomain($domainID);
100 CRM_Core_Config::singleton(TRUE, TRUE);
101 }
102
103 /**
104 * Reset domain to default (ie. as loaded from settings). This is the
105 * counterpart to CRM_Core_BAO_Domain::setDomain.
106 *
107 * @deprecated
108 * @see CRM_Core_BAO_Domain::setDomain
109 */
110 public static function resetDomain() {
111 CRM_Core_Config::domainID(null, true);
112 self::getDomain(null, true);
113 CRM_Core_Config::singleton(TRUE, TRUE);
114 }
115
116 /**
117 * @param bool $skipUsingCache
118 *
119 * @return null|string
120 */
121 public static function version($skipUsingCache = false) {
122 return CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain',
123 CRM_Core_Config::domainID(),
124 'version',
125 'id',
126 $skipUsingCache
127 );
128 }
129
130 /**
131 * Get the location values of a domain
132 *
133 * @param NULL
134 *
135 * @return array Location::getValues
136 */
137 public function &getLocationValues() {
138 if ($this->_location == NULL) {
139 $domain = self::getDomain(null);
140 $params = array(
141 'contact_id' => $domain->contact_id
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 *
155 * @param array $params
156 * @param int $id
157 *
158 * @return array domain
159 */
160 public static function edit(&$params, &$id) {
161 $domain = new CRM_Core_DAO_Domain();
162 $domain->id = $id;
163 $domain->copyValues($params);
164 $domain->save();
165 return $domain;
166 }
167
168 /**
169 * Create a new domain
170 *
171 * @param array $params
172 *
173 * @return array domain
174 */
175 public static function create($params) {
176 $domain = new CRM_Core_DAO_Domain();
177 $domain->copyValues($params);
178 $domain->save();
179 return $domain;
180 }
181
182 /**
183 * @return bool
184 */
185 public static function multipleDomains() {
186 $session = CRM_Core_Session::singleton();
187
188 $numberDomains = $session->get('numberDomains');
189 if (!$numberDomains) {
190 $query = "SELECT count(*) from civicrm_domain";
191 $numberDomains = CRM_Core_DAO::singleValueQuery($query);
192 $session->set('numberDomains', $numberDomains);
193 }
194 return $numberDomains > 1 ? TRUE : FALSE;
195 }
196
197 /**
198 * @param bool $skipFatal
199 *
200 * @return array name & email for domain
201 * @throws Exception
202 */
203 public static function getNameAndEmail($skipFatal = FALSE) {
204 $fromEmailAddress = CRM_Core_OptionGroup::values('from_email_address', NULL, NULL, NULL, ' AND is_default = 1');
205 if (!empty($fromEmailAddress)) {
206 foreach ($fromEmailAddress as $key => $value) {
207 $email = CRM_Utils_Mail::pluckEmailFromHeader($value);
208 $fromArray = explode('"', $value);
209 $fromName = CRM_Utils_Array::value(1, $fromArray);
210 break;
211 }
212 return array($fromName, $email);
213 }
214 elseif ($skipFatal) {
215 return array('', '');
216 }
217
218 $url = CRM_Utils_System::url('civicrm/admin/domain',
219 'action=update&reset=1'
220 );
221 $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));
222
223 CRM_Core_Error::fatal($status);
224 }
225
226 /**
227 * @param int $contactID
228 *
229 * @return bool|null|object|string
230 */
231 public static function addContactToDomainGroup($contactID) {
232 $groupID = self::getGroupId();
233
234 if ($groupID) {
235 $contactIDs = array($contactID);
236 CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIDs, $groupID);
237
238 return $groupID;
239 }
240 return FALSE;
241 }
242
243 /**
244 * @return bool|null|object|string
245 */
246 public static function getGroupId() {
247 static $groupID = NULL;
248
249 if ($groupID) {
250 return $groupID;
251 }
252
253 $domainGroupID = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MULTISITE_PREFERENCES_NAME,
254 'domain_group_id'
255 );
256 $multisite = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MULTISITE_PREFERENCES_NAME,
257 'is_enabled'
258 );
259
260 if ($domainGroupID) {
261 $groupID = $domainGroupID;
262 }
263 elseif ($multisite) {
264 // create a group with that of domain name
265 $title = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain',
266 CRM_Core_Config::domainID(), 'name'
267 );
268 $groupID = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group',
269 $title, 'id', 'title', true
270 );
271 if (empty($groupID) && !empty($title)) {
272 $groupParams = array(
273 'title' => $title,
274 'is_active' => 1,
275 'no_parent' => 1,
276 );
277 $group = CRM_Contact_BAO_Group::create($groupParams);
278 $groupID = $group->id;
279 }
280 }
281 return $groupID ? $groupID : FALSE;
282 }
283
284 /**
285 * @param int $groupId
286 *
287 * @return bool
288 */
289 public static function isDomainGroup($groupId) {
290 $domainGroupID = self::getGroupId();
291 return $domainGroupID == $groupId ? TRUE : FALSE;
292 }
293
294 /**
295 * @return array
296 */
297 public static function getChildGroupIds() {
298 $domainGroupID = self::getGroupId();
299 $childGrps = array();
300
301 if ($domainGroupID) {
302 $childGrps = CRM_Contact_BAO_GroupNesting::getChildGroupIds($domainGroupID);
303 $childGrps[] = $domainGroupID;
304 }
305 return $childGrps;
306 }
307
308 /**
309 * Retrieve a list of contact-ids that belongs to current domain/site.
310 *
311 * @return array
312 */
313 public static function getContactList() {
314 $siteGroups = CRM_Core_BAO_Domain::getChildGroupIds();
315 $siteContacts = array();
316
317 if (!empty($siteGroups)) {
318 $query = "
319 SELECT cc.id
320 FROM civicrm_contact cc
321 INNER JOIN civicrm_group_contact gc ON
322 (gc.contact_id = cc.id AND gc.status = 'Added' AND gc.group_id IN (" . implode(',', $siteGroups) . "))";
323
324 $dao = CRM_Core_DAO::executeQuery($query);
325 while ($dao->fetch()) {
326 $siteContacts[] = $dao->id;
327 }
328 }
329 return $siteContacts;
330 }
331 }
332