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