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 | ||
85 | /** | |
fe482240 | 86 | * Get the location values of a domain. |
6a488035 | 87 | * |
a6c01b45 CW |
88 | * @return array |
89 | * Location::getValues | |
527745f0 | 90 | * |
91 | * @throws \CRM_Core_Exception | |
6a488035 | 92 | */ |
00be9182 | 93 | public function &getLocationValues() { |
6a488035 | 94 | if ($this->_location == NULL) { |
2aa397bc | 95 | $domain = self::getDomain(NULL); |
be2fb01f | 96 | $params = [ |
21dfd5f5 | 97 | 'contact_id' => $domain->contact_id, |
be2fb01f | 98 | ]; |
6a488035 TO |
99 | $this->_location = CRM_Core_BAO_Location::getValues($params, TRUE); |
100 | ||
101 | if (empty($this->_location)) { | |
102 | $this->_location = NULL; | |
103 | } | |
104 | } | |
105 | return $this->_location; | |
106 | } | |
107 | ||
108 | /** | |
6a5fec96 | 109 | * Update a domain. |
6a488035 | 110 | * |
c490a46a | 111 | * @param array $params |
100fef9d | 112 | * @param int $id |
da6b46f4 | 113 | * |
527745f0 | 114 | * @return CRM_Core_DAO_Domain |
6a488035 | 115 | */ |
6a5fec96 CW |
116 | public static function edit($params, $id) { |
117 | $params['id'] = $id; | |
118 | return self::writeRecord($params); | |
6a488035 TO |
119 | } |
120 | ||
121 | /** | |
6a5fec96 | 122 | * Create or update domain. |
6a488035 | 123 | * |
c490a46a | 124 | * @param array $params |
527745f0 | 125 | * @return CRM_Core_DAO_Domain |
6a488035 | 126 | */ |
00be9182 | 127 | public static function create($params) { |
6a5fec96 | 128 | return self::writeRecord($params); |
6a488035 TO |
129 | } |
130 | ||
b5c2afd0 EM |
131 | /** |
132 | * @return bool | |
133 | */ | |
00be9182 | 134 | public static function multipleDomains() { |
6a488035 TO |
135 | $session = CRM_Core_Session::singleton(); |
136 | ||
137 | $numberDomains = $session->get('numberDomains'); | |
138 | if (!$numberDomains) { | |
527745f0 | 139 | $query = 'SELECT count(*) from civicrm_domain'; |
6a488035 TO |
140 | $numberDomains = CRM_Core_DAO::singleValueQuery($query); |
141 | $session->set('numberDomains', $numberDomains); | |
142 | } | |
63d76404 | 143 | return $numberDomains > 1; |
6a488035 TO |
144 | } |
145 | ||
b5c2afd0 EM |
146 | /** |
147 | * @param bool $skipFatal | |
518fa0ee | 148 | * @param bool $returnString |
527745f0 | 149 | * |
a6c01b45 CW |
150 | * @return array |
151 | * name & email for domain | |
527745f0 | 152 | * |
153 | * @throws \CRM_Core_Exception | |
b5c2afd0 | 154 | */ |
beac1417 | 155 | public static function getNameAndEmail($skipFatal = FALSE, $returnString = FALSE) { |
6a488035 TO |
156 | $fromEmailAddress = CRM_Core_OptionGroup::values('from_email_address', NULL, NULL, NULL, ' AND is_default = 1'); |
157 | if (!empty($fromEmailAddress)) { | |
beac1417 MW |
158 | if ($returnString) { |
159 | // Return a string like: "Demonstrators Anonymous" <info@example.org> | |
160 | return $fromEmailAddress; | |
161 | } | |
6a488035 | 162 | foreach ($fromEmailAddress as $key => $value) { |
353ffa53 | 163 | $email = CRM_Utils_Mail::pluckEmailFromHeader($value); |
6a488035 | 164 | $fromArray = explode('"', $value); |
9c1bc317 | 165 | $fromName = $fromArray[1] ?? NULL; |
6a488035 TO |
166 | break; |
167 | } | |
be2fb01f | 168 | return [$fromName, $email]; |
6a488035 | 169 | } |
beac1417 MW |
170 | |
171 | if ($skipFatal) { | |
be2fb01f | 172 | return [NULL, NULL]; |
6a488035 TO |
173 | } |
174 | ||
beac1417 MW |
175 | $url = CRM_Utils_System::url('civicrm/admin/options/from_email_address', |
176 | 'reset=1' | |
6a488035 | 177 | ); |
be2fb01f | 178 | $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 | 179 | |
527745f0 | 180 | throw new CRM_Core_Exception($status); |
6a488035 TO |
181 | } |
182 | ||
b5c2afd0 | 183 | /** |
100fef9d | 184 | * @param int $contactID |
b5c2afd0 EM |
185 | * |
186 | * @return bool|null|object|string | |
527745f0 | 187 | * |
188 | * @throws \CRM_Core_Exception | |
b5c2afd0 | 189 | */ |
00be9182 | 190 | public static function addContactToDomainGroup($contactID) { |
6a488035 TO |
191 | $groupID = self::getGroupId(); |
192 | ||
193 | if ($groupID) { | |
be2fb01f | 194 | $contactIDs = [$contactID]; |
6a488035 TO |
195 | CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIDs, $groupID); |
196 | ||
197 | return $groupID; | |
198 | } | |
199 | return FALSE; | |
200 | } | |
201 | ||
b5c2afd0 EM |
202 | /** |
203 | * @return bool|null|object|string | |
527745f0 | 204 | * |
205 | * @throws \CRM_Core_Exception | |
b5c2afd0 | 206 | */ |
00be9182 | 207 | public static function getGroupId() { |
6a488035 TO |
208 | static $groupID = NULL; |
209 | ||
210 | if ($groupID) { | |
211 | return $groupID; | |
212 | } | |
213 | ||
aaffa79f | 214 | $domainGroupID = Civi::settings()->get('domain_group_id'); |
215 | $multisite = Civi::settings()->get('is_enabled'); | |
6a488035 TO |
216 | |
217 | if ($domainGroupID) { | |
218 | $groupID = $domainGroupID; | |
219 | } | |
220 | elseif ($multisite) { | |
221 | // create a group with that of domain name | |
222 | $title = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain', | |
223 | CRM_Core_Config::domainID(), 'name' | |
224 | ); | |
225 | $groupID = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', | |
2aa397bc | 226 | $title, 'id', 'title', TRUE |
6a488035 | 227 | ); |
6a488035 TO |
228 | } |
229 | return $groupID ? $groupID : FALSE; | |
230 | } | |
231 | ||
b5c2afd0 | 232 | /** |
100fef9d | 233 | * @param int $groupId |
b5c2afd0 EM |
234 | * |
235 | * @return bool | |
527745f0 | 236 | * |
237 | * @throws \CRM_Core_Exception | |
b5c2afd0 | 238 | */ |
00be9182 | 239 | public static function isDomainGroup($groupId) { |
6a488035 | 240 | $domainGroupID = self::getGroupId(); |
63d76404 | 241 | return $domainGroupID == (bool) $groupId; |
6a488035 TO |
242 | } |
243 | ||
b5c2afd0 EM |
244 | /** |
245 | * @return array | |
527745f0 | 246 | * |
247 | * @throws \CRM_Core_Exception | |
b5c2afd0 | 248 | */ |
00be9182 | 249 | public static function getChildGroupIds() { |
6a488035 | 250 | $domainGroupID = self::getGroupId(); |
be2fb01f | 251 | $childGrps = []; |
6a488035 TO |
252 | |
253 | if ($domainGroupID) { | |
254 | $childGrps = CRM_Contact_BAO_GroupNesting::getChildGroupIds($domainGroupID); | |
255 | $childGrps[] = $domainGroupID; | |
256 | } | |
257 | return $childGrps; | |
258 | } | |
259 | ||
b5c2afd0 | 260 | /** |
100fef9d | 261 | * Retrieve a list of contact-ids that belongs to current domain/site. |
c490a46a | 262 | * |
b5c2afd0 | 263 | * @return array |
527745f0 | 264 | * |
265 | * @throws \CRM_Core_Exception | |
b5c2afd0 | 266 | */ |
00be9182 | 267 | public static function getContactList() { |
6a488035 | 268 | $siteGroups = CRM_Core_BAO_Domain::getChildGroupIds(); |
be2fb01f | 269 | $siteContacts = []; |
6a488035 TO |
270 | |
271 | if (!empty($siteGroups)) { | |
272 | $query = " | |
273 | SELECT cc.id | |
274 | FROM civicrm_contact cc | |
275 | INNER JOIN civicrm_group_contact gc ON | |
276 | (gc.contact_id = cc.id AND gc.status = 'Added' AND gc.group_id IN (" . implode(',', $siteGroups) . "))"; | |
277 | ||
278 | $dao = CRM_Core_DAO::executeQuery($query); | |
279 | while ($dao->fetch()) { | |
280 | $siteContacts[] = $dao->id; | |
281 | } | |
282 | } | |
283 | return $siteContacts; | |
284 | } | |
96025800 | 285 | |
b5bfb58f SL |
286 | /** |
287 | * CRM-20308 & CRM-19657 | |
beac1417 MW |
288 | * Return domain information / user information for the usage in receipts |
289 | * Try default from address then fall back to using logged in user details | |
527745f0 | 290 | * |
291 | * @throws \CiviCRM_API3_Exception | |
b5bfb58f | 292 | */ |
b1273714 | 293 | public static function getDefaultReceiptFrom() { |
be2fb01f | 294 | $domain = civicrm_api3('domain', 'getsingle', ['id' => CRM_Core_Config::domainID()]); |
b5bfb58f | 295 | if (!empty($domain['from_email'])) { |
be2fb01f | 296 | return [$domain['from_name'], $domain['from_email']]; |
b5bfb58f SL |
297 | } |
298 | if (!empty($domain['domain_email'])) { | |
be2fb01f | 299 | return [$domain['name'], $domain['domain_email']]; |
b5bfb58f | 300 | } |
b5bfb58f SL |
301 | $userName = ''; |
302 | $userEmail = ''; | |
4c981f37 MW |
303 | |
304 | if (!Civi::settings()->get('allow_mail_from_logged_in_contact')) { | |
be2fb01f | 305 | return [$userName, $userEmail]; |
4c981f37 MW |
306 | } |
307 | ||
308 | $userID = CRM_Core_Session::singleton()->getLoggedInContactID(); | |
b5bfb58f SL |
309 | if (!empty($userID)) { |
310 | list($userName, $userEmail) = CRM_Contact_BAO_Contact_Location::getEmailDetails($userID); | |
311 | } | |
312 | // If still empty fall back to the logged in user details. | |
313 | // return empty values no matter what. | |
be2fb01f | 314 | return [$userName, $userEmail]; |
b5bfb58f SL |
315 | } |
316 | ||
576fcb9c | 317 | /** |
318 | * Get address to be used for system from addresses when a reply is not expected. | |
319 | */ | |
320 | public static function getNoReplyEmailAddress() { | |
321 | $emailDomain = CRM_Core_BAO_MailSettings::defaultDomain(); | |
322 | return "do-not-reply@$emailDomain"; | |
323 | } | |
324 | ||
6a488035 | 325 | } |