CRM add missing comment blocks
[civicrm-core.git] / CRM / Core / BAO / Domain.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06b69b18 4 | CiviCRM version 4.5 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
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
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 *
38 */
39class 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 *
77b97be7
EM
72 * @param null $reset
73 *
6a488035
TO
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);
eb40b5a4 106 CRM_Core_Config::singleton(TRUE, TRUE);
6a488035
TO
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 *
77b97be7 113 * @internal param int $domainID id for domain you want to set as current
6a488035
TO
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);
7583c3f3 120 CRM_Core_Config::singleton(TRUE, TRUE);
6a488035
TO
121 }
122
123 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 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 *
da6b46f4
EM
158 * @param $params
159 * @param $id
160 *
6a488035
TO
161 * @return domain array
162 * @access public
163 */
164 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 *
fd31fa4c
EM
175 * @param $params
176 *
6a488035
TO
177 * @return domain array
178 * @access public
179 */
180 static function create($params) {
181 $domain = new CRM_Core_DAO_Domain();
182 $domain->copyValues($params);
183 $domain->save();
184 return $domain;
185 }
186
187 static function multipleDomains() {
188 $session = CRM_Core_Session::singleton();
189
190 $numberDomains = $session->get('numberDomains');
191 if (!$numberDomains) {
192 $query = "SELECT count(*) from civicrm_domain";
193 $numberDomains = CRM_Core_DAO::singleValueQuery($query);
194 $session->set('numberDomains', $numberDomains);
195 }
196 return $numberDomains > 1 ? TRUE : FALSE;
197 }
198
199 static function getNameAndEmail($skipFatal = FALSE) {
200 $fromEmailAddress = CRM_Core_OptionGroup::values('from_email_address', NULL, NULL, NULL, ' AND is_default = 1');
201 if (!empty($fromEmailAddress)) {
202 foreach ($fromEmailAddress as $key => $value) {
203 $email = CRM_Utils_Mail::pluckEmailFromHeader($value);
204 $fromArray = explode('"', $value);
205 $fromName = CRM_Utils_Array::value(1, $fromArray);
206 break;
207 }
208 return array($fromName, $email);
209 }
210 elseif ($skipFatal) {
211 return array('', '');
212 }
213
214 $url = CRM_Utils_System::url('civicrm/admin/domain',
215 'action=update&reset=1'
216 );
217 $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));
218
219 CRM_Core_Error::fatal($status);
220 }
221
222 static function addContactToDomainGroup($contactID) {
223 $groupID = self::getGroupId();
224
225 if ($groupID) {
226 $contactIDs = array($contactID);
227 CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIDs, $groupID);
228
229 return $groupID;
230 }
231 return FALSE;
232 }
233
234 static function getGroupId() {
235 static $groupID = NULL;
236
237 if ($groupID) {
238 return $groupID;
239 }
240
241 $domainGroupID = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MULTISITE_PREFERENCES_NAME,
242 'domain_group_id'
243 );
244 $multisite = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MULTISITE_PREFERENCES_NAME,
245 'is_enabled'
246 );
247
248 if ($domainGroupID) {
249 $groupID = $domainGroupID;
250 }
251 elseif ($multisite) {
252 // create a group with that of domain name
253 $title = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain',
254 CRM_Core_Config::domainID(), 'name'
255 );
256 $groupID = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group',
257 $title, 'id', 'title', true
258 );
259 if (empty($groupID) && !empty($title)) {
260 $groupParams = array(
261 'title' => $title,
262 'is_active' => 1,
263 'no_parent' => 1,
264 );
265 $group = CRM_Contact_BAO_Group::create($groupParams);
266 $groupID = $group->id;
267 }
268 }
269 return $groupID ? $groupID : FALSE;
270 }
271
272 static function isDomainGroup($groupId) {
273 $domainGroupID = self::getGroupId();
274 return $domainGroupID == $groupId ? TRUE : FALSE;
275 }
276
277 static function getChildGroupIds() {
278 $domainGroupID = self::getGroupId();
279 $childGrps = array();
280
281 if ($domainGroupID) {
282 $childGrps = CRM_Contact_BAO_GroupNesting::getChildGroupIds($domainGroupID);
283 $childGrps[] = $domainGroupID;
284 }
285 return $childGrps;
286 }
287
288 // function to retrieve a list of contact-ids that belongs to current domain/site.
289 static function getContactList() {
290 $siteGroups = CRM_Core_BAO_Domain::getChildGroupIds();
291 $siteContacts = array();
292
293 if (!empty($siteGroups)) {
294 $query = "
295 SELECT cc.id
296 FROM civicrm_contact cc
297 INNER JOIN civicrm_group_contact gc ON
298 (gc.contact_id = cc.id AND gc.status = 'Added' AND gc.group_id IN (" . implode(',', $siteGroups) . "))";
299
300 $dao = CRM_Core_DAO::executeQuery($query);
301 while ($dao->fetch()) {
302 $siteContacts[] = $dao->id;
303 }
304 }
305 return $siteContacts;
306 }
307}
308