Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
0f03f337 | 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
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 | |
0f03f337 | 31 | * @copyright CiviCRM LLC (c) 2004-2017 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * | |
38 | */ | |
39 | class CRM_Core_BAO_Domain extends CRM_Core_DAO_Domain { | |
40 | ||
41 | /** | |
fe482240 | 42 | * Cache for the current domain object. |
6a488035 TO |
43 | */ |
44 | static $_domain = NULL; | |
45 | ||
46 | /** | |
47 | * Cache for a domain's location array | |
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 | * |
77b97be7 EM |
68 | * @param null $reset |
69 | * | |
72b3a70c | 70 | * @return CRM_Core_BAO_Domain|null |
6a488035 | 71 | */ |
c9dbc47c | 72 | public static function getDomain($reset = NULL) { |
6a488035 TO |
73 | static $domain = NULL; |
74 | if (!$domain || $reset) { | |
75 | $domain = new CRM_Core_BAO_Domain(); | |
76 | $domain->id = CRM_Core_Config::domainID(); | |
77 | if (!$domain->find(TRUE)) { | |
78 | CRM_Core_Error::fatal(); | |
79 | } | |
80 | } | |
81 | return $domain; | |
82 | } | |
83 | ||
b5c2afd0 EM |
84 | /** |
85 | * @param bool $skipUsingCache | |
86 | * | |
87 | * @return null|string | |
88 | */ | |
2aa397bc | 89 | public static function version($skipUsingCache = FALSE) { |
6a488035 TO |
90 | return CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain', |
91 | CRM_Core_Config::domainID(), | |
c490a46a CW |
92 | 'version', |
93 | 'id', | |
94 | $skipUsingCache | |
6a488035 TO |
95 | ); |
96 | } | |
97 | ||
98 | /** | |
fe482240 | 99 | * Get the location values of a domain. |
6a488035 | 100 | * |
a6c01b45 CW |
101 | * @return array |
102 | * Location::getValues | |
6a488035 | 103 | */ |
00be9182 | 104 | public function &getLocationValues() { |
6a488035 | 105 | if ($this->_location == NULL) { |
2aa397bc | 106 | $domain = self::getDomain(NULL); |
6a488035 | 107 | $params = array( |
21dfd5f5 | 108 | 'contact_id' => $domain->contact_id, |
6a488035 TO |
109 | ); |
110 | $this->_location = CRM_Core_BAO_Location::getValues($params, TRUE); | |
111 | ||
112 | if (empty($this->_location)) { | |
113 | $this->_location = NULL; | |
114 | } | |
115 | } | |
116 | return $this->_location; | |
117 | } | |
118 | ||
119 | /** | |
fe482240 | 120 | * Save the values of a domain. |
6a488035 | 121 | * |
c490a46a | 122 | * @param array $params |
100fef9d | 123 | * @param int $id |
da6b46f4 | 124 | * |
a6c01b45 CW |
125 | * @return array |
126 | * domain | |
6a488035 | 127 | */ |
00be9182 | 128 | public static function edit(&$params, &$id) { |
6a488035 TO |
129 | $domain = new CRM_Core_DAO_Domain(); |
130 | $domain->id = $id; | |
131 | $domain->copyValues($params); | |
132 | $domain->save(); | |
133 | return $domain; | |
134 | } | |
135 | ||
136 | /** | |
fe482240 | 137 | * Create a new domain. |
6a488035 | 138 | * |
c490a46a | 139 | * @param array $params |
fd31fa4c | 140 | * |
a6c01b45 CW |
141 | * @return array |
142 | * domain | |
6a488035 | 143 | */ |
00be9182 | 144 | public static function create($params) { |
6a488035 TO |
145 | $domain = new CRM_Core_DAO_Domain(); |
146 | $domain->copyValues($params); | |
147 | $domain->save(); | |
148 | return $domain; | |
149 | } | |
150 | ||
b5c2afd0 EM |
151 | /** |
152 | * @return bool | |
153 | */ | |
00be9182 | 154 | public static function multipleDomains() { |
6a488035 TO |
155 | $session = CRM_Core_Session::singleton(); |
156 | ||
157 | $numberDomains = $session->get('numberDomains'); | |
158 | if (!$numberDomains) { | |
159 | $query = "SELECT count(*) from civicrm_domain"; | |
160 | $numberDomains = CRM_Core_DAO::singleValueQuery($query); | |
161 | $session->set('numberDomains', $numberDomains); | |
162 | } | |
163 | return $numberDomains > 1 ? TRUE : FALSE; | |
164 | } | |
165 | ||
b5c2afd0 EM |
166 | /** |
167 | * @param bool $skipFatal | |
168 | * | |
a6c01b45 CW |
169 | * @return array |
170 | * name & email for domain | |
b5c2afd0 EM |
171 | * @throws Exception |
172 | */ | |
00be9182 | 173 | public static function getNameAndEmail($skipFatal = FALSE) { |
6a488035 TO |
174 | $fromEmailAddress = CRM_Core_OptionGroup::values('from_email_address', NULL, NULL, NULL, ' AND is_default = 1'); |
175 | if (!empty($fromEmailAddress)) { | |
176 | foreach ($fromEmailAddress as $key => $value) { | |
353ffa53 | 177 | $email = CRM_Utils_Mail::pluckEmailFromHeader($value); |
6a488035 | 178 | $fromArray = explode('"', $value); |
353ffa53 | 179 | $fromName = CRM_Utils_Array::value(1, $fromArray); |
6a488035 TO |
180 | break; |
181 | } | |
182 | return array($fromName, $email); | |
183 | } | |
184 | elseif ($skipFatal) { | |
185 | return array('', ''); | |
186 | } | |
187 | ||
188 | $url = CRM_Utils_System::url('civicrm/admin/domain', | |
189 | 'action=update&reset=1' | |
190 | ); | |
191 | $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)); | |
192 | ||
193 | CRM_Core_Error::fatal($status); | |
194 | } | |
195 | ||
b5c2afd0 | 196 | /** |
100fef9d | 197 | * @param int $contactID |
b5c2afd0 EM |
198 | * |
199 | * @return bool|null|object|string | |
200 | */ | |
00be9182 | 201 | public static function addContactToDomainGroup($contactID) { |
6a488035 TO |
202 | $groupID = self::getGroupId(); |
203 | ||
204 | if ($groupID) { | |
205 | $contactIDs = array($contactID); | |
206 | CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIDs, $groupID); | |
207 | ||
208 | return $groupID; | |
209 | } | |
210 | return FALSE; | |
211 | } | |
212 | ||
b5c2afd0 EM |
213 | /** |
214 | * @return bool|null|object|string | |
215 | */ | |
00be9182 | 216 | public static function getGroupId() { |
6a488035 TO |
217 | static $groupID = NULL; |
218 | ||
219 | if ($groupID) { | |
220 | return $groupID; | |
221 | } | |
222 | ||
aaffa79f | 223 | $domainGroupID = Civi::settings()->get('domain_group_id'); |
224 | $multisite = Civi::settings()->get('is_enabled'); | |
6a488035 TO |
225 | |
226 | if ($domainGroupID) { | |
227 | $groupID = $domainGroupID; | |
228 | } | |
229 | elseif ($multisite) { | |
230 | // create a group with that of domain name | |
231 | $title = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain', | |
232 | CRM_Core_Config::domainID(), 'name' | |
233 | ); | |
234 | $groupID = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', | |
2aa397bc | 235 | $title, 'id', 'title', TRUE |
6a488035 | 236 | ); |
6a488035 TO |
237 | } |
238 | return $groupID ? $groupID : FALSE; | |
239 | } | |
240 | ||
b5c2afd0 | 241 | /** |
100fef9d | 242 | * @param int $groupId |
b5c2afd0 EM |
243 | * |
244 | * @return bool | |
245 | */ | |
00be9182 | 246 | public static function isDomainGroup($groupId) { |
6a488035 TO |
247 | $domainGroupID = self::getGroupId(); |
248 | return $domainGroupID == $groupId ? TRUE : FALSE; | |
249 | } | |
250 | ||
b5c2afd0 EM |
251 | /** |
252 | * @return array | |
253 | */ | |
00be9182 | 254 | public static function getChildGroupIds() { |
6a488035 TO |
255 | $domainGroupID = self::getGroupId(); |
256 | $childGrps = array(); | |
257 | ||
258 | if ($domainGroupID) { | |
259 | $childGrps = CRM_Contact_BAO_GroupNesting::getChildGroupIds($domainGroupID); | |
260 | $childGrps[] = $domainGroupID; | |
261 | } | |
262 | return $childGrps; | |
263 | } | |
264 | ||
b5c2afd0 | 265 | /** |
100fef9d | 266 | * Retrieve a list of contact-ids that belongs to current domain/site. |
c490a46a | 267 | * |
b5c2afd0 EM |
268 | * @return array |
269 | */ | |
00be9182 | 270 | public static function getContactList() { |
6a488035 TO |
271 | $siteGroups = CRM_Core_BAO_Domain::getChildGroupIds(); |
272 | $siteContacts = array(); | |
273 | ||
274 | if (!empty($siteGroups)) { | |
275 | $query = " | |
276 | SELECT cc.id | |
277 | FROM civicrm_contact cc | |
278 | INNER JOIN civicrm_group_contact gc ON | |
279 | (gc.contact_id = cc.id AND gc.status = 'Added' AND gc.group_id IN (" . implode(',', $siteGroups) . "))"; | |
280 | ||
281 | $dao = CRM_Core_DAO::executeQuery($query); | |
282 | while ($dao->fetch()) { | |
283 | $siteContacts[] = $dao->id; | |
284 | } | |
285 | } | |
286 | return $siteContacts; | |
287 | } | |
96025800 | 288 | |
b5bfb58f SL |
289 | /** |
290 | * CRM-20308 & CRM-19657 | |
291 | * Return domain information / user information for the useage in receipts | |
292 | * Try default from adress then fall back to using logged in user details | |
293 | */ | |
294 | public function getDefaultReceiptFrom() { | |
295 | $domain = civicrm_api3('domain', 'getsingle', array('id' => CRM_Core_Config::domainID())); | |
296 | if (!empty($domain['from_email'])) { | |
297 | return array($domain['from_name'], $domain['from_email']); | |
298 | } | |
299 | if (!empty($domain['domain_email'])) { | |
300 | return array($domain['name'], $domain['domain_email']); | |
301 | } | |
302 | $userID = CRM_Core_Session::singleton()->getLoggedInContactID(); | |
303 | $userName = ''; | |
304 | $userEmail = ''; | |
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 array($userName, $userEmail); | |
311 | } | |
312 | ||
6a488035 | 313 | } |