Merge pull request #3612 from jitendrapurohit/CRM-14945
[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 * @throws Exception
211 */
212 static function getNameAndEmail($skipFatal = FALSE) {
213 $fromEmailAddress = CRM_Core_OptionGroup::values('from_email_address', NULL, NULL, NULL, ' AND is_default = 1');
214 if (!empty($fromEmailAddress)) {
215 foreach ($fromEmailAddress as $key => $value) {
216 $email = CRM_Utils_Mail::pluckEmailFromHeader($value);
217 $fromArray = explode('"', $value);
218 $fromName = CRM_Utils_Array::value(1, $fromArray);
219 break;
220 }
221 return array($fromName, $email);
222 }
223 elseif ($skipFatal) {
224 return array('', '');
225 }
226
227 $url = CRM_Utils_System::url('civicrm/admin/domain',
228 'action=update&reset=1'
229 );
230 $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));
231
232 CRM_Core_Error::fatal($status);
233 }
234
235 /**
236 * @param $contactID
237 *
238 * @return bool|null|object|string
239 */
240 static function addContactToDomainGroup($contactID) {
241 $groupID = self::getGroupId();
242
243 if ($groupID) {
244 $contactIDs = array($contactID);
245 CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIDs, $groupID);
246
247 return $groupID;
248 }
249 return FALSE;
250 }
251
252 /**
253 * @return bool|null|object|string
254 */
255 static function getGroupId() {
256 static $groupID = NULL;
257
258 if ($groupID) {
259 return $groupID;
260 }
261
262 $domainGroupID = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MULTISITE_PREFERENCES_NAME,
263 'domain_group_id'
264 );
265 $multisite = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MULTISITE_PREFERENCES_NAME,
266 'is_enabled'
267 );
268
269 if ($domainGroupID) {
270 $groupID = $domainGroupID;
271 }
272 elseif ($multisite) {
273 // create a group with that of domain name
274 $title = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain',
275 CRM_Core_Config::domainID(), 'name'
276 );
277 $groupID = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group',
278 $title, 'id', 'title', true
279 );
280 if (empty($groupID) && !empty($title)) {
281 $groupParams = array(
282 'title' => $title,
283 'is_active' => 1,
284 'no_parent' => 1,
285 );
286 $group = CRM_Contact_BAO_Group::create($groupParams);
287 $groupID = $group->id;
288 }
289 }
290 return $groupID ? $groupID : FALSE;
291 }
292
293 /**
294 * @param $groupId
295 *
296 * @return bool
297 */
298 static function isDomainGroup($groupId) {
299 $domainGroupID = self::getGroupId();
300 return $domainGroupID == $groupId ? TRUE : FALSE;
301 }
302
303 /**
304 * @return array
305 */
306 static function getChildGroupIds() {
307 $domainGroupID = self::getGroupId();
308 $childGrps = array();
309
310 if ($domainGroupID) {
311 $childGrps = CRM_Contact_BAO_GroupNesting::getChildGroupIds($domainGroupID);
312 $childGrps[] = $domainGroupID;
313 }
314 return $childGrps;
315 }
316
317 // function to retrieve a list of contact-ids that belongs to current domain/site.
318 /**
319 * @return array
320 */
321 static function getContactList() {
322 $siteGroups = CRM_Core_BAO_Domain::getChildGroupIds();
323 $siteContacts = array();
324
325 if (!empty($siteGroups)) {
326 $query = "
327 SELECT cc.id
328 FROM civicrm_contact cc
329 INNER JOIN civicrm_group_contact gc ON
330 (gc.contact_id = cc.id AND gc.status = 'Added' AND gc.group_id IN (" . implode(',', $siteGroups) . "))";
331
332 $dao = CRM_Core_DAO::executeQuery($query);
333 while ($dao->fetch()) {
334 $siteContacts[] = $dao->id;
335 }
336 }
337 return $siteContacts;
338 }
339 }
340