Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
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 | */ |
2aa397bc | 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 | ||
2aa397bc TO |
84 | /** |
85 | * Change active domain (ie. to perform a temporary action) such as changing | |
86 | * config for all domains | |
87 | * | |
88 | * Switching around the global domain variable is very risky business. This | |
89 | * is ONLY used as a hack to allow CRM_Core_BAO_Setting::setItems to manipulate | |
90 | * the civicrm_domain.config_backend in multiple domains. When/if config_backend | |
91 | * goes away, this hack should be removed. | |
92 | * | |
93 | * @param int $domainID | |
94 | * Id for domain you want to set as current. | |
95 | * @deprecated | |
96 | * @see http://issues.civicrm.org/jira/browse/CRM-11204 | |
97 | */ | |
00be9182 | 98 | public static function setDomain($domainID) { |
6a488035 TO |
99 | CRM_Core_Config::domainID($domainID); |
100 | self::getDomain($domainID); | |
eb40b5a4 | 101 | CRM_Core_Config::singleton(TRUE, TRUE); |
6a488035 TO |
102 | } |
103 | ||
104 | /** | |
105 | * Reset domain to default (ie. as loaded from settings). This is the | |
106 | * counterpart to CRM_Core_BAO_Domain::setDomain. | |
107 | * | |
6a488035 TO |
108 | * @deprecated |
109 | * @see CRM_Core_BAO_Domain::setDomain | |
110 | */ | |
00be9182 | 111 | public static function resetDomain() { |
2aa397bc TO |
112 | CRM_Core_Config::domainID(NULL, TRUE); |
113 | self::getDomain(NULL, TRUE); | |
7583c3f3 | 114 | CRM_Core_Config::singleton(TRUE, TRUE); |
6a488035 TO |
115 | } |
116 | ||
b5c2afd0 EM |
117 | /** |
118 | * @param bool $skipUsingCache | |
119 | * | |
120 | * @return null|string | |
121 | */ | |
2aa397bc | 122 | public static function version($skipUsingCache = FALSE) { |
6a488035 TO |
123 | return CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain', |
124 | CRM_Core_Config::domainID(), | |
c490a46a CW |
125 | 'version', |
126 | 'id', | |
127 | $skipUsingCache | |
6a488035 TO |
128 | ); |
129 | } | |
130 | ||
131 | /** | |
fe482240 | 132 | * Get the location values of a domain. |
6a488035 | 133 | * |
a6c01b45 CW |
134 | * @return array |
135 | * Location::getValues | |
6a488035 | 136 | */ |
00be9182 | 137 | public function &getLocationValues() { |
6a488035 | 138 | if ($this->_location == NULL) { |
2aa397bc | 139 | $domain = self::getDomain(NULL); |
6a488035 | 140 | $params = array( |
21dfd5f5 | 141 | 'contact_id' => $domain->contact_id, |
6a488035 TO |
142 | ); |
143 | $this->_location = CRM_Core_BAO_Location::getValues($params, TRUE); | |
144 | ||
145 | if (empty($this->_location)) { | |
146 | $this->_location = NULL; | |
147 | } | |
148 | } | |
149 | return $this->_location; | |
150 | } | |
151 | ||
152 | /** | |
fe482240 | 153 | * Save the values of a domain. |
6a488035 | 154 | * |
c490a46a | 155 | * @param array $params |
100fef9d | 156 | * @param int $id |
da6b46f4 | 157 | * |
a6c01b45 CW |
158 | * @return array |
159 | * domain | |
6a488035 | 160 | */ |
00be9182 | 161 | public static function edit(&$params, &$id) { |
6a488035 TO |
162 | $domain = new CRM_Core_DAO_Domain(); |
163 | $domain->id = $id; | |
164 | $domain->copyValues($params); | |
165 | $domain->save(); | |
166 | return $domain; | |
167 | } | |
168 | ||
169 | /** | |
fe482240 | 170 | * Create a new domain. |
6a488035 | 171 | * |
c490a46a | 172 | * @param array $params |
fd31fa4c | 173 | * |
a6c01b45 CW |
174 | * @return array |
175 | * domain | |
6a488035 | 176 | */ |
00be9182 | 177 | public static function create($params) { |
6a488035 TO |
178 | $domain = new CRM_Core_DAO_Domain(); |
179 | $domain->copyValues($params); | |
180 | $domain->save(); | |
181 | return $domain; | |
182 | } | |
183 | ||
b5c2afd0 EM |
184 | /** |
185 | * @return bool | |
186 | */ | |
00be9182 | 187 | public static function multipleDomains() { |
6a488035 TO |
188 | $session = CRM_Core_Session::singleton(); |
189 | ||
190 | $numberDomains = $session->get('numberDomains'); | |
191 | if (!$numberDomains) { | |
192 | $query = "SELECT count(*) from civicrm_domain"; | |
193 | $numberDomains = CRM_Core_DAO::singleValueQuery($query); | |
194 | $session->set('numberDomains', $numberDomains); | |
195 | } | |
196 | return $numberDomains > 1 ? TRUE : FALSE; | |
197 | } | |
198 | ||
b5c2afd0 EM |
199 | /** |
200 | * @param bool $skipFatal | |
201 | * | |
a6c01b45 CW |
202 | * @return array |
203 | * name & email for domain | |
b5c2afd0 EM |
204 | * @throws Exception |
205 | */ | |
00be9182 | 206 | public static function getNameAndEmail($skipFatal = FALSE) { |
6a488035 TO |
207 | $fromEmailAddress = CRM_Core_OptionGroup::values('from_email_address', NULL, NULL, NULL, ' AND is_default = 1'); |
208 | if (!empty($fromEmailAddress)) { | |
209 | foreach ($fromEmailAddress as $key => $value) { | |
353ffa53 | 210 | $email = CRM_Utils_Mail::pluckEmailFromHeader($value); |
6a488035 | 211 | $fromArray = explode('"', $value); |
353ffa53 | 212 | $fromName = CRM_Utils_Array::value(1, $fromArray); |
6a488035 TO |
213 | break; |
214 | } | |
215 | return array($fromName, $email); | |
216 | } | |
217 | elseif ($skipFatal) { | |
218 | return array('', ''); | |
219 | } | |
220 | ||
221 | $url = CRM_Utils_System::url('civicrm/admin/domain', | |
222 | 'action=update&reset=1' | |
223 | ); | |
224 | $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)); | |
225 | ||
226 | CRM_Core_Error::fatal($status); | |
227 | } | |
228 | ||
b5c2afd0 | 229 | /** |
100fef9d | 230 | * @param int $contactID |
b5c2afd0 EM |
231 | * |
232 | * @return bool|null|object|string | |
233 | */ | |
00be9182 | 234 | public static function addContactToDomainGroup($contactID) { |
6a488035 TO |
235 | $groupID = self::getGroupId(); |
236 | ||
237 | if ($groupID) { | |
238 | $contactIDs = array($contactID); | |
239 | CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIDs, $groupID); | |
240 | ||
241 | return $groupID; | |
242 | } | |
243 | return FALSE; | |
244 | } | |
245 | ||
b5c2afd0 EM |
246 | /** |
247 | * @return bool|null|object|string | |
248 | */ | |
00be9182 | 249 | public static function getGroupId() { |
6a488035 TO |
250 | static $groupID = NULL; |
251 | ||
252 | if ($groupID) { | |
253 | return $groupID; | |
254 | } | |
255 | ||
aaffa79f | 256 | $domainGroupID = Civi::settings()->get('domain_group_id'); |
257 | $multisite = Civi::settings()->get('is_enabled'); | |
6a488035 TO |
258 | |
259 | if ($domainGroupID) { | |
260 | $groupID = $domainGroupID; | |
261 | } | |
262 | elseif ($multisite) { | |
263 | // create a group with that of domain name | |
264 | $title = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain', | |
265 | CRM_Core_Config::domainID(), 'name' | |
266 | ); | |
267 | $groupID = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', | |
2aa397bc | 268 | $title, 'id', 'title', TRUE |
6a488035 | 269 | ); |
6a488035 TO |
270 | } |
271 | return $groupID ? $groupID : FALSE; | |
272 | } | |
273 | ||
b5c2afd0 | 274 | /** |
100fef9d | 275 | * @param int $groupId |
b5c2afd0 EM |
276 | * |
277 | * @return bool | |
278 | */ | |
00be9182 | 279 | public static function isDomainGroup($groupId) { |
6a488035 TO |
280 | $domainGroupID = self::getGroupId(); |
281 | return $domainGroupID == $groupId ? TRUE : FALSE; | |
282 | } | |
283 | ||
b5c2afd0 EM |
284 | /** |
285 | * @return array | |
286 | */ | |
00be9182 | 287 | public static function getChildGroupIds() { |
6a488035 TO |
288 | $domainGroupID = self::getGroupId(); |
289 | $childGrps = array(); | |
290 | ||
291 | if ($domainGroupID) { | |
292 | $childGrps = CRM_Contact_BAO_GroupNesting::getChildGroupIds($domainGroupID); | |
293 | $childGrps[] = $domainGroupID; | |
294 | } | |
295 | return $childGrps; | |
296 | } | |
297 | ||
b5c2afd0 | 298 | /** |
100fef9d | 299 | * Retrieve a list of contact-ids that belongs to current domain/site. |
c490a46a | 300 | * |
b5c2afd0 EM |
301 | * @return array |
302 | */ | |
00be9182 | 303 | public static function getContactList() { |
6a488035 TO |
304 | $siteGroups = CRM_Core_BAO_Domain::getChildGroupIds(); |
305 | $siteContacts = array(); | |
306 | ||
307 | if (!empty($siteGroups)) { | |
308 | $query = " | |
309 | SELECT cc.id | |
310 | FROM civicrm_contact cc | |
311 | INNER JOIN civicrm_group_contact gc ON | |
312 | (gc.contact_id = cc.id AND gc.status = 'Added' AND gc.group_id IN (" . implode(',', $siteGroups) . "))"; | |
313 | ||
314 | $dao = CRM_Core_DAO::executeQuery($query); | |
315 | while ($dao->fetch()) { | |
316 | $siteContacts[] = $dao->id; | |
317 | } | |
318 | } | |
319 | return $siteContacts; | |
320 | } | |
96025800 | 321 | |
6a488035 | 322 | } |