94d0af354c60617fa342bcc9dcf9fc401f8ff375
[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
55 * (reference ) an assoc array of name/value pairs.
56 * @param array $defaults
57 * (reference ) an assoc array to hold the flattened values.
58 *
59 * @return CRM_Core_DAO_Domain
60 * @static
61 */
62 public static function retrieve(&$params, &$defaults) {
63 return CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_Domain', $params, $defaults);
64 }
65
66 /**
67 * Get the domain BAO
68 *
69 * @param null $reset
70 *
71 * @return CRM_Core_BAO_Domain|null
72 * @static
73 */
74 public static function &getDomain($reset = NULL) {
75 static $domain = NULL;
76 if (!$domain || $reset) {
77 $domain = new CRM_Core_BAO_Domain();
78 $domain->id = CRM_Core_Config::domainID();
79 if (!$domain->find(TRUE)) {
80 CRM_Core_Error::fatal();
81 }
82 }
83 return $domain;
84 }
85
86 /**
87 * Change active domain (ie. to perform a temporary action) such as changing
88 * config for all domains
89 *
90 * Switching around the global domain variable is very risky business. This
91 * is ONLY used as a hack to allow CRM_Core_BAO_Setting::setItems to manipulate
92 * the civicrm_domain.config_backend in multiple domains. When/if config_backend
93 * goes away, this hack should be removed.
94 *
95 * @param int $domainID
96 * Id for domain you want to set as current.
97 * @deprecated
98 * @see http://issues.civicrm.org/jira/browse/CRM-11204
99 */
100 public static function setDomain($domainID) {
101 CRM_Core_Config::domainID($domainID);
102 self::getDomain($domainID);
103 CRM_Core_Config::singleton(TRUE, TRUE);
104 }
105
106 /**
107 * Reset domain to default (ie. as loaded from settings). This is the
108 * counterpart to CRM_Core_BAO_Domain::setDomain.
109 *
110 * @deprecated
111 * @see CRM_Core_BAO_Domain::setDomain
112 */
113 public static function resetDomain() {
114 CRM_Core_Config::domainID(NULL, TRUE);
115 self::getDomain(NULL, TRUE);
116 CRM_Core_Config::singleton(TRUE, TRUE);
117 }
118
119 /**
120 * @param bool $skipUsingCache
121 *
122 * @return null|string
123 */
124 public static function version($skipUsingCache = FALSE) {
125 return CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain',
126 CRM_Core_Config::domainID(),
127 'version',
128 'id',
129 $skipUsingCache
130 );
131 }
132
133 /**
134 * Get the location values of a domain
135 *
136 * @param NULL
137 *
138 * @return array
139 * Location::getValues
140 */
141 public function &getLocationValues() {
142 if ($this->_location == NULL) {
143 $domain = self::getDomain(NULL);
144 $params = array(
145 'contact_id' => $domain->contact_id,
146 );
147 $this->_location = CRM_Core_BAO_Location::getValues($params, TRUE);
148
149 if (empty($this->_location)) {
150 $this->_location = NULL;
151 }
152 }
153 return $this->_location;
154 }
155
156 /**
157 * Save the values of a domain
158 *
159 * @param array $params
160 * @param int $id
161 *
162 * @return array
163 * domain
164 */
165 public static function edit(&$params, &$id) {
166 $domain = new CRM_Core_DAO_Domain();
167 $domain->id = $id;
168 $domain->copyValues($params);
169 $domain->save();
170 return $domain;
171 }
172
173 /**
174 * Create a new domain
175 *
176 * @param array $params
177 *
178 * @return array
179 * domain
180 */
181 public static function create($params) {
182 $domain = new CRM_Core_DAO_Domain();
183 $domain->copyValues($params);
184 $domain->save();
185 return $domain;
186 }
187
188 /**
189 * @return bool
190 */
191 public static function multipleDomains() {
192 $session = CRM_Core_Session::singleton();
193
194 $numberDomains = $session->get('numberDomains');
195 if (!$numberDomains) {
196 $query = "SELECT count(*) from civicrm_domain";
197 $numberDomains = CRM_Core_DAO::singleValueQuery($query);
198 $session->set('numberDomains', $numberDomains);
199 }
200 return $numberDomains > 1 ? TRUE : FALSE;
201 }
202
203 /**
204 * @param bool $skipFatal
205 *
206 * @return array
207 * name & email for domain
208 * @throws Exception
209 */
210 public static function getNameAndEmail($skipFatal = FALSE) {
211 $fromEmailAddress = CRM_Core_OptionGroup::values('from_email_address', NULL, NULL, NULL, ' AND is_default = 1');
212 if (!empty($fromEmailAddress)) {
213 foreach ($fromEmailAddress as $key => $value) {
214 $email = CRM_Utils_Mail::pluckEmailFromHeader($value);
215 $fromArray = explode('"', $value);
216 $fromName = CRM_Utils_Array::value(1, $fromArray);
217 break;
218 }
219 return array($fromName, $email);
220 }
221 elseif ($skipFatal) {
222 return array('', '');
223 }
224
225 $url = CRM_Utils_System::url('civicrm/admin/domain',
226 'action=update&reset=1'
227 );
228 $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));
229
230 CRM_Core_Error::fatal($status);
231 }
232
233 /**
234 * @param int $contactID
235 *
236 * @return bool|null|object|string
237 */
238 public static function addContactToDomainGroup($contactID) {
239 $groupID = self::getGroupId();
240
241 if ($groupID) {
242 $contactIDs = array($contactID);
243 CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIDs, $groupID);
244
245 return $groupID;
246 }
247 return FALSE;
248 }
249
250 /**
251 * @return bool|null|object|string
252 */
253 public static function getGroupId() {
254 static $groupID = NULL;
255
256 if ($groupID) {
257 return $groupID;
258 }
259
260 $domainGroupID = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MULTISITE_PREFERENCES_NAME,
261 'domain_group_id'
262 );
263 $multisite = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MULTISITE_PREFERENCES_NAME,
264 'is_enabled'
265 );
266
267 if ($domainGroupID) {
268 $groupID = $domainGroupID;
269 }
270 elseif ($multisite) {
271 // create a group with that of domain name
272 $title = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain',
273 CRM_Core_Config::domainID(), 'name'
274 );
275 $groupID = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group',
276 $title, 'id', 'title', TRUE
277 );
278 if (empty($groupID) && !empty($title)) {
279 $groupParams = array(
280 'title' => $title,
281 'is_active' => 1,
282 'no_parent' => 1,
283 );
284 $group = CRM_Contact_BAO_Group::create($groupParams);
285 $groupID = $group->id;
286 }
287 }
288 return $groupID ? $groupID : FALSE;
289 }
290
291 /**
292 * @param int $groupId
293 *
294 * @return bool
295 */
296 public static function isDomainGroup($groupId) {
297 $domainGroupID = self::getGroupId();
298 return $domainGroupID == $groupId ? TRUE : FALSE;
299 }
300
301 /**
302 * @return array
303 */
304 public static function getChildGroupIds() {
305 $domainGroupID = self::getGroupId();
306 $childGrps = array();
307
308 if ($domainGroupID) {
309 $childGrps = CRM_Contact_BAO_GroupNesting::getChildGroupIds($domainGroupID);
310 $childGrps[] = $domainGroupID;
311 }
312 return $childGrps;
313 }
314
315 /**
316 * Retrieve a list of contact-ids that belongs to current domain/site.
317 *
318 * @return array
319 */
320 public static function getContactList() {
321 $siteGroups = CRM_Core_BAO_Domain::getChildGroupIds();
322 $siteContacts = array();
323
324 if (!empty($siteGroups)) {
325 $query = "
326 SELECT cc.id
327 FROM civicrm_contact cc
328 INNER JOIN civicrm_group_contact gc ON
329 (gc.contact_id = cc.id AND gc.status = 'Added' AND gc.group_id IN (" . implode(',', $siteGroups) . "))";
330
331 $dao = CRM_Core_DAO::executeQuery($query);
332 while ($dao->fetch()) {
333 $siteContacts[] = $dao->id;
334 }
335 }
336 return $siteContacts;
337 }
338 }