Merge pull request #11250 from agh1/no-update-indices
[civicrm-core.git] / CRM / Core / BAO / Domain.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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-2017
32 */
33
34 /**
35 *
36 */
37 class CRM_Core_BAO_Domain extends CRM_Core_DAO_Domain {
38
39 /**
40 * Cache for the current domain object.
41 */
42 static $_domain = NULL;
43
44 /**
45 * Cache for a domain's location array
46 */
47 private $_location = NULL;
48
49 /**
50 * Fetch object based on array of properties.
51 *
52 * @param array $params
53 * (reference ) an assoc array of name/value pairs.
54 * @param array $defaults
55 * (reference ) an assoc array to hold the flattened values.
56 *
57 * @return CRM_Core_DAO_Domain
58 */
59 public static function retrieve(&$params, &$defaults) {
60 return CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_Domain', $params, $defaults);
61 }
62
63 /**
64 * Get the domain BAO.
65 *
66 * @param bool $reset
67 *
68 * @return \CRM_Core_BAO_Domain
69 * @throws \CRM_Core_Exception
70 */
71 public static function getDomain($reset = NULL) {
72 static $domain = NULL;
73 if (!$domain || $reset) {
74 $domain = new CRM_Core_BAO_Domain();
75 $domain->id = CRM_Core_Config::domainID();
76 if (!$domain->find(TRUE)) {
77 throw new CRM_Core_Exception('No domain in DB');
78 }
79 }
80 return $domain;
81 }
82
83 /**
84 * @param bool $skipUsingCache
85 *
86 * @return null|string
87 */
88 public static function version($skipUsingCache = FALSE) {
89 return CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain',
90 CRM_Core_Config::domainID(),
91 'version',
92 'id',
93 $skipUsingCache
94 );
95 }
96
97 /**
98 * Get the location values of a domain.
99 *
100 * @return array
101 * Location::getValues
102 */
103 public function &getLocationValues() {
104 if ($this->_location == NULL) {
105 $domain = self::getDomain(NULL);
106 $params = array(
107 'contact_id' => $domain->contact_id,
108 );
109 $this->_location = CRM_Core_BAO_Location::getValues($params, TRUE);
110
111 if (empty($this->_location)) {
112 $this->_location = NULL;
113 }
114 }
115 return $this->_location;
116 }
117
118 /**
119 * Save the values of a domain.
120 *
121 * @param array $params
122 * @param int $id
123 *
124 * @return array
125 * domain
126 */
127 public static function edit(&$params, &$id) {
128 $domain = new CRM_Core_DAO_Domain();
129 $domain->id = $id;
130 $domain->copyValues($params);
131 $domain->save();
132 return $domain;
133 }
134
135 /**
136 * Create a new domain.
137 *
138 * @param array $params
139 *
140 * @return array
141 * domain
142 */
143 public static function create($params) {
144 $domain = new CRM_Core_DAO_Domain();
145 $domain->copyValues($params);
146 $domain->save();
147 return $domain;
148 }
149
150 /**
151 * @return bool
152 */
153 public static function multipleDomains() {
154 $session = CRM_Core_Session::singleton();
155
156 $numberDomains = $session->get('numberDomains');
157 if (!$numberDomains) {
158 $query = "SELECT count(*) from civicrm_domain";
159 $numberDomains = CRM_Core_DAO::singleValueQuery($query);
160 $session->set('numberDomains', $numberDomains);
161 }
162 return $numberDomains > 1 ? TRUE : FALSE;
163 }
164
165 /**
166 * @param bool $skipFatal
167 *
168 * @return array
169 * name & email for domain
170 * @throws Exception
171 */
172 public static function getNameAndEmail($skipFatal = FALSE) {
173 $fromEmailAddress = CRM_Core_OptionGroup::values('from_email_address', NULL, NULL, NULL, ' AND is_default = 1');
174 if (!empty($fromEmailAddress)) {
175 foreach ($fromEmailAddress as $key => $value) {
176 $email = CRM_Utils_Mail::pluckEmailFromHeader($value);
177 $fromArray = explode('"', $value);
178 $fromName = CRM_Utils_Array::value(1, $fromArray);
179 break;
180 }
181 return array($fromName, $email);
182 }
183 elseif ($skipFatal) {
184 return array('', '');
185 }
186
187 $url = CRM_Utils_System::url('civicrm/admin/domain',
188 'action=update&reset=1'
189 );
190 $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));
191
192 CRM_Core_Error::fatal($status);
193 }
194
195 /**
196 * @param int $contactID
197 *
198 * @return bool|null|object|string
199 */
200 public static function addContactToDomainGroup($contactID) {
201 $groupID = self::getGroupId();
202
203 if ($groupID) {
204 $contactIDs = array($contactID);
205 CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIDs, $groupID);
206
207 return $groupID;
208 }
209 return FALSE;
210 }
211
212 /**
213 * @return bool|null|object|string
214 */
215 public static function getGroupId() {
216 static $groupID = NULL;
217
218 if ($groupID) {
219 return $groupID;
220 }
221
222 $domainGroupID = Civi::settings()->get('domain_group_id');
223 $multisite = Civi::settings()->get('is_enabled');
224
225 if ($domainGroupID) {
226 $groupID = $domainGroupID;
227 }
228 elseif ($multisite) {
229 // create a group with that of domain name
230 $title = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain',
231 CRM_Core_Config::domainID(), 'name'
232 );
233 $groupID = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group',
234 $title, 'id', 'title', TRUE
235 );
236 }
237 return $groupID ? $groupID : FALSE;
238 }
239
240 /**
241 * @param int $groupId
242 *
243 * @return bool
244 */
245 public static function isDomainGroup($groupId) {
246 $domainGroupID = self::getGroupId();
247 return $domainGroupID == $groupId ? TRUE : FALSE;
248 }
249
250 /**
251 * @return array
252 */
253 public static function getChildGroupIds() {
254 $domainGroupID = self::getGroupId();
255 $childGrps = array();
256
257 if ($domainGroupID) {
258 $childGrps = CRM_Contact_BAO_GroupNesting::getChildGroupIds($domainGroupID);
259 $childGrps[] = $domainGroupID;
260 }
261 return $childGrps;
262 }
263
264 /**
265 * Retrieve a list of contact-ids that belongs to current domain/site.
266 *
267 * @return array
268 */
269 public static function getContactList() {
270 $siteGroups = CRM_Core_BAO_Domain::getChildGroupIds();
271 $siteContacts = array();
272
273 if (!empty($siteGroups)) {
274 $query = "
275 SELECT cc.id
276 FROM civicrm_contact cc
277 INNER JOIN civicrm_group_contact gc ON
278 (gc.contact_id = cc.id AND gc.status = 'Added' AND gc.group_id IN (" . implode(',', $siteGroups) . "))";
279
280 $dao = CRM_Core_DAO::executeQuery($query);
281 while ($dao->fetch()) {
282 $siteContacts[] = $dao->id;
283 }
284 }
285 return $siteContacts;
286 }
287
288 /**
289 * CRM-20308 & CRM-19657
290 * Return domain information / user information for the useage in receipts
291 * Try default from adress then fall back to using logged in user details
292 */
293 public static function getDefaultReceiptFrom() {
294 $domain = civicrm_api3('domain', 'getsingle', array('id' => CRM_Core_Config::domainID()));
295 if (!empty($domain['from_email'])) {
296 return array($domain['from_name'], $domain['from_email']);
297 }
298 if (!empty($domain['domain_email'])) {
299 return array($domain['name'], $domain['domain_email']);
300 }
301 $userID = CRM_Core_Session::singleton()->getLoggedInContactID();
302 $userName = '';
303 $userEmail = '';
304 if (!empty($userID)) {
305 list($userName, $userEmail) = CRM_Contact_BAO_Contact_Location::getEmailDetails($userID);
306 }
307 // If still empty fall back to the logged in user details.
308 // return empty values no matter what.
309 return array($userName, $userEmail);
310 }
311
312 }