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