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