Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.3 | | |
5 | +--------------------------------------------------------------------+ | |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
31 | * @copyright CiviCRM LLC (c) 2004-2013 | |
32 | * $Id$ | |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * | |
38 | */ | |
39 | class CRM_Core_BAO_Domain extends CRM_Core_DAO_Domain { | |
40 | ||
41 | /** | |
42 | * Cache for the current domain object | |
43 | */ | |
44 | static $_domain = NULL; | |
45 | ||
46 | /** | |
47 | * Cache for a domain's location array | |
48 | */ | |
49 | private $_location = NULL; | |
50 | ||
51 | /** | |
52 | * Takes a bunch of params that are needed to match certain criteria and | |
53 | * retrieves the relevant objects. Typically the valid params are only | |
54 | * contact_id. We'll tweak this function to be more full featured over a period | |
55 | * of time. This is the inverse function of create. It also stores all the retrieved | |
56 | * values in the default array | |
57 | * | |
58 | * @param array $params (reference ) an assoc array of name/value pairs | |
59 | * @param array $defaults (reference ) an assoc array to hold the flattened values | |
60 | * | |
61 | * @return object CRM_Core_DAO_Domain object | |
62 | * @access public | |
63 | * @static | |
64 | */ | |
65 | static function retrieve(&$params, &$defaults) { | |
66 | return CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_Domain', $params, $defaults); | |
67 | } | |
68 | ||
69 | /** | |
70 | * Get the domain BAO | |
71 | * | |
72 | * @return null|object CRM_Core_BAO_Domain | |
73 | * @access public | |
74 | * @static | |
75 | */ | |
76 | static function &getDomain($reset = null) { | |
77 | static $domain = NULL; | |
78 | if (!$domain || $reset) { | |
79 | $domain = new CRM_Core_BAO_Domain(); | |
80 | $domain->id = CRM_Core_Config::domainID(); | |
81 | if (!$domain->find(TRUE)) { | |
82 | CRM_Core_Error::fatal(); | |
83 | } | |
84 | } | |
85 | return $domain; | |
86 | } | |
87 | ||
88 | /** | |
89 | * Change active domain (ie. to perform a temporary action) such as changing | |
90 | * config for all domains | |
91 | * | |
92 | * Switching around the global domain variable is very risky business. This | |
93 | * is ONLY used as a hack to allow CRM_Core_BAO_Setting::setItems to manipulate | |
94 | * the civicrm_domain.config_backend in multiple domains. When/if config_backend | |
95 | * goes away, this hack should be removed. | |
96 | * | |
97 | * @param integer $domainID id for domain you want to set as current | |
98 | * @deprecated | |
99 | * @see http://issues.civicrm.org/jira/browse/CRM-11204 | |
100 | */ | |
101 | static function setDomain($domainID){ | |
102 | CRM_Core_Config::domainID($domainID); | |
103 | self::getDomain($domainID); | |
104 | } | |
105 | ||
106 | /** | |
107 | * Reset domain to default (ie. as loaded from settings). This is the | |
108 | * counterpart to CRM_Core_BAO_Domain::setDomain. | |
109 | * | |
110 | * @param integer $domainID id for domain you want to set as current | |
111 | * @deprecated | |
112 | * @see CRM_Core_BAO_Domain::setDomain | |
113 | */ | |
114 | static function resetDomain(){ | |
115 | CRM_Core_Config::domainID(null, true); | |
116 | self::getDomain(null, true); | |
7583c3f3 | 117 | CRM_Core_Config::singleton(TRUE, TRUE); |
6a488035 TO |
118 | } |
119 | ||
120 | static function version( $skipUsingCache = false ) { | |
121 | return CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain', | |
122 | CRM_Core_Config::domainID(), | |
123 | 'version', | |
124 | 'id', | |
125 | $skipUsingCache | |
126 | ); | |
127 | } | |
128 | ||
129 | /** | |
130 | * Get the location values of a domain | |
131 | * | |
132 | * @param NULL | |
133 | * | |
134 | * @return array Location::getValues | |
135 | * @access public | |
136 | */ | |
137 | function &getLocationValues() { | |
138 | if ($this->_location == NULL) { | |
139 | $domain = self::getDomain(null); | |
140 | $params = array( | |
141 | 'contact_id' => $domain->contact_id | |
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 | /** | |
153 | * Save the values of a domain | |
154 | * | |
155 | * @return domain array | |
156 | * @access public | |
157 | */ | |
158 | static function edit(&$params, &$id) { | |
159 | $domain = new CRM_Core_DAO_Domain(); | |
160 | $domain->id = $id; | |
161 | $domain->copyValues($params); | |
162 | $domain->save(); | |
163 | return $domain; | |
164 | } | |
165 | ||
166 | /** | |
167 | * Create a new domain | |
168 | * | |
169 | * @return domain array | |
170 | * @access public | |
171 | */ | |
172 | static function create($params) { | |
173 | $domain = new CRM_Core_DAO_Domain(); | |
174 | $domain->copyValues($params); | |
175 | $domain->save(); | |
176 | return $domain; | |
177 | } | |
178 | ||
179 | static function multipleDomains() { | |
180 | $session = CRM_Core_Session::singleton(); | |
181 | ||
182 | $numberDomains = $session->get('numberDomains'); | |
183 | if (!$numberDomains) { | |
184 | $query = "SELECT count(*) from civicrm_domain"; | |
185 | $numberDomains = CRM_Core_DAO::singleValueQuery($query); | |
186 | $session->set('numberDomains', $numberDomains); | |
187 | } | |
188 | return $numberDomains > 1 ? TRUE : FALSE; | |
189 | } | |
190 | ||
191 | static function getNameAndEmail($skipFatal = FALSE) { | |
192 | $fromEmailAddress = CRM_Core_OptionGroup::values('from_email_address', NULL, NULL, NULL, ' AND is_default = 1'); | |
193 | if (!empty($fromEmailAddress)) { | |
194 | foreach ($fromEmailAddress as $key => $value) { | |
195 | $email = CRM_Utils_Mail::pluckEmailFromHeader($value); | |
196 | $fromArray = explode('"', $value); | |
197 | $fromName = CRM_Utils_Array::value(1, $fromArray); | |
198 | break; | |
199 | } | |
200 | return array($fromName, $email); | |
201 | } | |
202 | elseif ($skipFatal) { | |
203 | return array('', ''); | |
204 | } | |
205 | ||
206 | $url = CRM_Utils_System::url('civicrm/admin/domain', | |
207 | 'action=update&reset=1' | |
208 | ); | |
209 | $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)); | |
210 | ||
211 | CRM_Core_Error::fatal($status); | |
212 | } | |
213 | ||
214 | static function addContactToDomainGroup($contactID) { | |
215 | $groupID = self::getGroupId(); | |
216 | ||
217 | if ($groupID) { | |
218 | $contactIDs = array($contactID); | |
219 | CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIDs, $groupID); | |
220 | ||
221 | return $groupID; | |
222 | } | |
223 | return FALSE; | |
224 | } | |
225 | ||
226 | static function getGroupId() { | |
227 | static $groupID = NULL; | |
228 | ||
229 | if ($groupID) { | |
230 | return $groupID; | |
231 | } | |
232 | ||
233 | $domainGroupID = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MULTISITE_PREFERENCES_NAME, | |
234 | 'domain_group_id' | |
235 | ); | |
236 | $multisite = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MULTISITE_PREFERENCES_NAME, | |
237 | 'is_enabled' | |
238 | ); | |
239 | ||
240 | if ($domainGroupID) { | |
241 | $groupID = $domainGroupID; | |
242 | } | |
243 | elseif ($multisite) { | |
244 | // create a group with that of domain name | |
245 | $title = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain', | |
246 | CRM_Core_Config::domainID(), 'name' | |
247 | ); | |
248 | $groupID = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', | |
249 | $title, 'id', 'title', true | |
250 | ); | |
251 | if (empty($groupID) && !empty($title)) { | |
252 | $groupParams = array( | |
253 | 'title' => $title, | |
254 | 'is_active' => 1, | |
255 | 'no_parent' => 1, | |
256 | ); | |
257 | $group = CRM_Contact_BAO_Group::create($groupParams); | |
258 | $groupID = $group->id; | |
259 | } | |
260 | } | |
261 | return $groupID ? $groupID : FALSE; | |
262 | } | |
263 | ||
264 | static function isDomainGroup($groupId) { | |
265 | $domainGroupID = self::getGroupId(); | |
266 | return $domainGroupID == $groupId ? TRUE : FALSE; | |
267 | } | |
268 | ||
269 | static function getChildGroupIds() { | |
270 | $domainGroupID = self::getGroupId(); | |
271 | $childGrps = array(); | |
272 | ||
273 | if ($domainGroupID) { | |
274 | $childGrps = CRM_Contact_BAO_GroupNesting::getChildGroupIds($domainGroupID); | |
275 | $childGrps[] = $domainGroupID; | |
276 | } | |
277 | return $childGrps; | |
278 | } | |
279 | ||
280 | // function to retrieve a list of contact-ids that belongs to current domain/site. | |
281 | static function getContactList() { | |
282 | $siteGroups = CRM_Core_BAO_Domain::getChildGroupIds(); | |
283 | $siteContacts = array(); | |
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 | } | |
299 | } | |
300 |