bulk comment fix
[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 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 *
158 * @return domain array
159 * @access public
160 */
161 static function edit(&$params, &$id) {
162 $domain = new CRM_Core_DAO_Domain();
163 $domain->id = $id;
164 $domain->copyValues($params);
165 $domain->save();
166 return $domain;
167 }
168
169 /**
170 * Create a new domain
171 *
172 * @param $params
173 *
174 * @return domain array
175 * @access public
176 */
177 static function create($params) {
178 $domain = new CRM_Core_DAO_Domain();
179 $domain->copyValues($params);
180 $domain->save();
181 return $domain;
182 }
183
184 static function multipleDomains() {
185 $session = CRM_Core_Session::singleton();
186
187 $numberDomains = $session->get('numberDomains');
188 if (!$numberDomains) {
189 $query = "SELECT count(*) from civicrm_domain";
190 $numberDomains = CRM_Core_DAO::singleValueQuery($query);
191 $session->set('numberDomains', $numberDomains);
192 }
193 return $numberDomains > 1 ? TRUE : FALSE;
194 }
195
196 static function getNameAndEmail($skipFatal = FALSE) {
197 $fromEmailAddress = CRM_Core_OptionGroup::values('from_email_address', NULL, NULL, NULL, ' AND is_default = 1');
198 if (!empty($fromEmailAddress)) {
199 foreach ($fromEmailAddress as $key => $value) {
200 $email = CRM_Utils_Mail::pluckEmailFromHeader($value);
201 $fromArray = explode('"', $value);
202 $fromName = CRM_Utils_Array::value(1, $fromArray);
203 break;
204 }
205 return array($fromName, $email);
206 }
207 elseif ($skipFatal) {
208 return array('', '');
209 }
210
211 $url = CRM_Utils_System::url('civicrm/admin/domain',
212 'action=update&reset=1'
213 );
214 $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));
215
216 CRM_Core_Error::fatal($status);
217 }
218
219 static function addContactToDomainGroup($contactID) {
220 $groupID = self::getGroupId();
221
222 if ($groupID) {
223 $contactIDs = array($contactID);
224 CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIDs, $groupID);
225
226 return $groupID;
227 }
228 return FALSE;
229 }
230
231 static function getGroupId() {
232 static $groupID = NULL;
233
234 if ($groupID) {
235 return $groupID;
236 }
237
238 $domainGroupID = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MULTISITE_PREFERENCES_NAME,
239 'domain_group_id'
240 );
241 $multisite = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MULTISITE_PREFERENCES_NAME,
242 'is_enabled'
243 );
244
245 if ($domainGroupID) {
246 $groupID = $domainGroupID;
247 }
248 elseif ($multisite) {
249 // create a group with that of domain name
250 $title = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain',
251 CRM_Core_Config::domainID(), 'name'
252 );
253 $groupID = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group',
254 $title, 'id', 'title', true
255 );
256 if (empty($groupID) && !empty($title)) {
257 $groupParams = array(
258 'title' => $title,
259 'is_active' => 1,
260 'no_parent' => 1,
261 );
262 $group = CRM_Contact_BAO_Group::create($groupParams);
263 $groupID = $group->id;
264 }
265 }
266 return $groupID ? $groupID : FALSE;
267 }
268
269 static function isDomainGroup($groupId) {
270 $domainGroupID = self::getGroupId();
271 return $domainGroupID == $groupId ? TRUE : FALSE;
272 }
273
274 static function getChildGroupIds() {
275 $domainGroupID = self::getGroupId();
276 $childGrps = array();
277
278 if ($domainGroupID) {
279 $childGrps = CRM_Contact_BAO_GroupNesting::getChildGroupIds($domainGroupID);
280 $childGrps[] = $domainGroupID;
281 }
282 return $childGrps;
283 }
284
285 // function to retrieve a list of contact-ids that belongs to current domain/site.
286 static function getContactList() {
287 $siteGroups = CRM_Core_BAO_Domain::getChildGroupIds();
288 $siteContacts = array();
289
290 if (!empty($siteGroups)) {
291 $query = "
292 SELECT cc.id
293 FROM civicrm_contact cc
294 INNER JOIN civicrm_group_contact gc ON
295 (gc.contact_id = cc.id AND gc.status = 'Added' AND gc.group_id IN (" . implode(',', $siteGroups) . "))";
296
297 $dao = CRM_Core_DAO::executeQuery($query);
298 while ($dao->fetch()) {
299 $siteContacts[] = $dao->id;
300 }
301 }
302 return $siteContacts;
303 }
304 }
305