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