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