Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
18 | /** | |
19 | * | |
20 | */ | |
21 | class CRM_Core_BAO_Domain extends CRM_Core_DAO_Domain { | |
22 | ||
23 | /** | |
fe482240 | 24 | * Cache for the current domain object. |
518fa0ee | 25 | * @var object |
6a488035 | 26 | */ |
518fa0ee | 27 | public static $_domain = NULL; |
6a488035 TO |
28 | |
29 | /** | |
30 | * Cache for a domain's location array | |
518fa0ee | 31 | * @var array |
6a488035 TO |
32 | */ |
33 | private $_location = NULL; | |
34 | ||
35 | /** | |
fe482240 | 36 | * Fetch object based on array of properties. |
6a488035 | 37 | * |
6a0b768e TO |
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. | |
6a488035 | 42 | * |
16b10e64 | 43 | * @return CRM_Core_DAO_Domain |
6a488035 | 44 | */ |
00be9182 | 45 | public static function retrieve(&$params, &$defaults) { |
6a488035 TO |
46 | return CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_Domain', $params, $defaults); |
47 | } | |
48 | ||
49 | /** | |
fe482240 | 50 | * Get the domain BAO. |
6a488035 | 51 | * |
2149f4bd | 52 | * @param bool $reset |
77b97be7 | 53 | * |
2149f4bd | 54 | * @return \CRM_Core_BAO_Domain |
55 | * @throws \CRM_Core_Exception | |
6a488035 | 56 | */ |
c9dbc47c | 57 | public static function getDomain($reset = NULL) { |
6a488035 TO |
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)) { | |
2149f4bd | 63 | throw new CRM_Core_Exception('No domain in DB'); |
6a488035 TO |
64 | } |
65 | } | |
66 | return $domain; | |
67 | } | |
68 | ||
b5c2afd0 EM |
69 | /** |
70 | * @param bool $skipUsingCache | |
71 | * | |
72 | * @return null|string | |
527745f0 | 73 | * |
74 | * @throws \CRM_Core_Exception | |
b5c2afd0 | 75 | */ |
2aa397bc | 76 | public static function version($skipUsingCache = FALSE) { |
6a488035 TO |
77 | return CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain', |
78 | CRM_Core_Config::domainID(), | |
c490a46a CW |
79 | 'version', |
80 | 'id', | |
81 | $skipUsingCache | |
6a488035 TO |
82 | ); |
83 | } | |
84 | ||
1fbda76b | 85 | /** |
86 | * Is a database update required to apply latest schema changes. | |
87 | * | |
88 | * @return bool | |
89 | * | |
90 | * @throws \CRM_Core_Exception | |
91 | */ | |
92 | public static function isDBUpdateRequired() { | |
93 | $dbVersion = CRM_Core_BAO_Domain::version(); | |
94 | $codeVersion = CRM_Utils_System::version(); | |
95 | return version_compare($dbVersion, $codeVersion) < 0; | |
96 | } | |
97 | ||
d51a6a62 CW |
98 | /** |
99 | * Checks that the current DB schema is at least $min version | |
100 | * | |
101 | * @param string|number $min | |
102 | * @return bool | |
103 | */ | |
104 | public static function isDBVersionAtLeast($min) { | |
105 | return version_compare(self::version(), $min, '>='); | |
106 | } | |
107 | ||
6a488035 | 108 | /** |
fe482240 | 109 | * Get the location values of a domain. |
6a488035 | 110 | * |
a6c01b45 CW |
111 | * @return array |
112 | * Location::getValues | |
527745f0 | 113 | * |
114 | * @throws \CRM_Core_Exception | |
6a488035 | 115 | */ |
00be9182 | 116 | public function &getLocationValues() { |
6a488035 | 117 | if ($this->_location == NULL) { |
2aa397bc | 118 | $domain = self::getDomain(NULL); |
be2fb01f | 119 | $params = [ |
21dfd5f5 | 120 | 'contact_id' => $domain->contact_id, |
be2fb01f | 121 | ]; |
6a488035 TO |
122 | $this->_location = CRM_Core_BAO_Location::getValues($params, TRUE); |
123 | ||
124 | if (empty($this->_location)) { | |
125 | $this->_location = NULL; | |
126 | } | |
127 | } | |
128 | return $this->_location; | |
129 | } | |
130 | ||
131 | /** | |
6a5fec96 | 132 | * Update a domain. |
6a488035 | 133 | * |
c490a46a | 134 | * @param array $params |
100fef9d | 135 | * @param int $id |
da6b46f4 | 136 | * |
527745f0 | 137 | * @return CRM_Core_DAO_Domain |
6a488035 | 138 | */ |
6a5fec96 CW |
139 | public static function edit($params, $id) { |
140 | $params['id'] = $id; | |
141 | return self::writeRecord($params); | |
6a488035 TO |
142 | } |
143 | ||
144 | /** | |
6a5fec96 | 145 | * Create or update domain. |
6a488035 | 146 | * |
c490a46a | 147 | * @param array $params |
527745f0 | 148 | * @return CRM_Core_DAO_Domain |
6a488035 | 149 | */ |
00be9182 | 150 | public static function create($params) { |
6a5fec96 | 151 | return self::writeRecord($params); |
6a488035 TO |
152 | } |
153 | ||
b5c2afd0 EM |
154 | /** |
155 | * @return bool | |
156 | */ | |
00be9182 | 157 | public static function multipleDomains() { |
6a488035 TO |
158 | $session = CRM_Core_Session::singleton(); |
159 | ||
160 | $numberDomains = $session->get('numberDomains'); | |
161 | if (!$numberDomains) { | |
527745f0 | 162 | $query = 'SELECT count(*) from civicrm_domain'; |
6a488035 TO |
163 | $numberDomains = CRM_Core_DAO::singleValueQuery($query); |
164 | $session->set('numberDomains', $numberDomains); | |
165 | } | |
63d76404 | 166 | return $numberDomains > 1; |
6a488035 TO |
167 | } |
168 | ||
b5c2afd0 EM |
169 | /** |
170 | * @param bool $skipFatal | |
518fa0ee | 171 | * @param bool $returnString |
527745f0 | 172 | * |
a6c01b45 CW |
173 | * @return array |
174 | * name & email for domain | |
527745f0 | 175 | * |
176 | * @throws \CRM_Core_Exception | |
b5c2afd0 | 177 | */ |
beac1417 | 178 | public static function getNameAndEmail($skipFatal = FALSE, $returnString = FALSE) { |
6a488035 TO |
179 | $fromEmailAddress = CRM_Core_OptionGroup::values('from_email_address', NULL, NULL, NULL, ' AND is_default = 1'); |
180 | if (!empty($fromEmailAddress)) { | |
beac1417 MW |
181 | if ($returnString) { |
182 | // Return a string like: "Demonstrators Anonymous" <info@example.org> | |
183 | return $fromEmailAddress; | |
184 | } | |
6a488035 | 185 | foreach ($fromEmailAddress as $key => $value) { |
353ffa53 | 186 | $email = CRM_Utils_Mail::pluckEmailFromHeader($value); |
6a488035 | 187 | $fromArray = explode('"', $value); |
9c1bc317 | 188 | $fromName = $fromArray[1] ?? NULL; |
6a488035 TO |
189 | break; |
190 | } | |
be2fb01f | 191 | return [$fromName, $email]; |
6a488035 | 192 | } |
beac1417 MW |
193 | |
194 | if ($skipFatal) { | |
be2fb01f | 195 | return [NULL, NULL]; |
6a488035 TO |
196 | } |
197 | ||
beac1417 MW |
198 | $url = CRM_Utils_System::url('civicrm/admin/options/from_email_address', |
199 | 'reset=1' | |
6a488035 | 200 | ); |
be2fb01f | 201 | $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]); |
6a488035 | 202 | |
527745f0 | 203 | throw new CRM_Core_Exception($status); |
6a488035 TO |
204 | } |
205 | ||
b5c2afd0 | 206 | /** |
100fef9d | 207 | * @param int $contactID |
b5c2afd0 EM |
208 | * |
209 | * @return bool|null|object|string | |
527745f0 | 210 | * |
211 | * @throws \CRM_Core_Exception | |
b5c2afd0 | 212 | */ |
00be9182 | 213 | public static function addContactToDomainGroup($contactID) { |
6a488035 TO |
214 | $groupID = self::getGroupId(); |
215 | ||
216 | if ($groupID) { | |
be2fb01f | 217 | $contactIDs = [$contactID]; |
6a488035 TO |
218 | CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIDs, $groupID); |
219 | ||
220 | return $groupID; | |
221 | } | |
222 | return FALSE; | |
223 | } | |
224 | ||
b5c2afd0 EM |
225 | /** |
226 | * @return bool|null|object|string | |
527745f0 | 227 | * |
228 | * @throws \CRM_Core_Exception | |
b5c2afd0 | 229 | */ |
00be9182 | 230 | public static function getGroupId() { |
6a488035 TO |
231 | static $groupID = NULL; |
232 | ||
233 | if ($groupID) { | |
234 | return $groupID; | |
235 | } | |
236 | ||
aaffa79f | 237 | $domainGroupID = Civi::settings()->get('domain_group_id'); |
238 | $multisite = Civi::settings()->get('is_enabled'); | |
6a488035 TO |
239 | |
240 | if ($domainGroupID) { | |
241 | $groupID = $domainGroupID; | |
242 | } | |
243 | elseif ($multisite) { | |
244 | // create a group with that of domain name | |
245 | $title = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain', | |
246 | CRM_Core_Config::domainID(), 'name' | |
247 | ); | |
248 | $groupID = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', | |
2aa397bc | 249 | $title, 'id', 'title', TRUE |
6a488035 | 250 | ); |
6a488035 TO |
251 | } |
252 | return $groupID ? $groupID : FALSE; | |
253 | } | |
254 | ||
b5c2afd0 | 255 | /** |
100fef9d | 256 | * @param int $groupId |
b5c2afd0 EM |
257 | * |
258 | * @return bool | |
527745f0 | 259 | * |
260 | * @throws \CRM_Core_Exception | |
b5c2afd0 | 261 | */ |
00be9182 | 262 | public static function isDomainGroup($groupId) { |
6a488035 | 263 | $domainGroupID = self::getGroupId(); |
63d76404 | 264 | return $domainGroupID == (bool) $groupId; |
6a488035 TO |
265 | } |
266 | ||
b5c2afd0 EM |
267 | /** |
268 | * @return array | |
527745f0 | 269 | * |
270 | * @throws \CRM_Core_Exception | |
b5c2afd0 | 271 | */ |
00be9182 | 272 | public static function getChildGroupIds() { |
6a488035 | 273 | $domainGroupID = self::getGroupId(); |
be2fb01f | 274 | $childGrps = []; |
6a488035 TO |
275 | |
276 | if ($domainGroupID) { | |
277 | $childGrps = CRM_Contact_BAO_GroupNesting::getChildGroupIds($domainGroupID); | |
278 | $childGrps[] = $domainGroupID; | |
279 | } | |
280 | return $childGrps; | |
281 | } | |
282 | ||
b5c2afd0 | 283 | /** |
100fef9d | 284 | * Retrieve a list of contact-ids that belongs to current domain/site. |
c490a46a | 285 | * |
b5c2afd0 | 286 | * @return array |
527745f0 | 287 | * |
288 | * @throws \CRM_Core_Exception | |
b5c2afd0 | 289 | */ |
00be9182 | 290 | public static function getContactList() { |
6a488035 | 291 | $siteGroups = CRM_Core_BAO_Domain::getChildGroupIds(); |
be2fb01f | 292 | $siteContacts = []; |
6a488035 TO |
293 | |
294 | if (!empty($siteGroups)) { | |
295 | $query = " | |
296 | SELECT cc.id | |
297 | FROM civicrm_contact cc | |
298 | INNER JOIN civicrm_group_contact gc ON | |
299 | (gc.contact_id = cc.id AND gc.status = 'Added' AND gc.group_id IN (" . implode(',', $siteGroups) . "))"; | |
300 | ||
301 | $dao = CRM_Core_DAO::executeQuery($query); | |
302 | while ($dao->fetch()) { | |
303 | $siteContacts[] = $dao->id; | |
304 | } | |
305 | } | |
306 | return $siteContacts; | |
307 | } | |
96025800 | 308 | |
b5bfb58f SL |
309 | /** |
310 | * CRM-20308 & CRM-19657 | |
beac1417 MW |
311 | * Return domain information / user information for the usage in receipts |
312 | * Try default from address then fall back to using logged in user details | |
527745f0 | 313 | * |
314 | * @throws \CiviCRM_API3_Exception | |
b5bfb58f | 315 | */ |
b1273714 | 316 | public static function getDefaultReceiptFrom() { |
be2fb01f | 317 | $domain = civicrm_api3('domain', 'getsingle', ['id' => CRM_Core_Config::domainID()]); |
b5bfb58f | 318 | if (!empty($domain['from_email'])) { |
be2fb01f | 319 | return [$domain['from_name'], $domain['from_email']]; |
b5bfb58f SL |
320 | } |
321 | if (!empty($domain['domain_email'])) { | |
be2fb01f | 322 | return [$domain['name'], $domain['domain_email']]; |
b5bfb58f | 323 | } |
b5bfb58f SL |
324 | $userName = ''; |
325 | $userEmail = ''; | |
4c981f37 MW |
326 | |
327 | if (!Civi::settings()->get('allow_mail_from_logged_in_contact')) { | |
be2fb01f | 328 | return [$userName, $userEmail]; |
4c981f37 MW |
329 | } |
330 | ||
2dbdb9b9 | 331 | $userID = CRM_Core_Session::getLoggedInContactID(); |
b5bfb58f SL |
332 | if (!empty($userID)) { |
333 | list($userName, $userEmail) = CRM_Contact_BAO_Contact_Location::getEmailDetails($userID); | |
334 | } | |
335 | // If still empty fall back to the logged in user details. | |
336 | // return empty values no matter what. | |
be2fb01f | 337 | return [$userName, $userEmail]; |
b5bfb58f SL |
338 | } |
339 | ||
576fcb9c | 340 | /** |
341 | * Get address to be used for system from addresses when a reply is not expected. | |
342 | */ | |
343 | public static function getNoReplyEmailAddress() { | |
344 | $emailDomain = CRM_Core_BAO_MailSettings::defaultDomain(); | |
345 | return "do-not-reply@$emailDomain"; | |
346 | } | |
347 | ||
6a488035 | 348 | } |