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); | |
117 | } | |
118 | ||
119 | static function version( $skipUsingCache = false ) { | |
120 | return CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain', | |
121 | CRM_Core_Config::domainID(), | |
122 | 'version', | |
123 | 'id', | |
124 | $skipUsingCache | |
125 | ); | |
126 | } | |
127 | ||
128 | /** | |
129 | * Get the location values of a domain | |
130 | * | |
131 | * @param NULL | |
132 | * | |
133 | * @return array Location::getValues | |
134 | * @access public | |
135 | */ | |
136 | function &getLocationValues() { | |
137 | if ($this->_location == NULL) { | |
138 | $domain = self::getDomain(null); | |
139 | $params = array( | |
140 | 'contact_id' => $domain->contact_id | |
141 | ); | |
142 | $this->_location = CRM_Core_BAO_Location::getValues($params, TRUE); | |
143 | ||
144 | if (empty($this->_location)) { | |
145 | $this->_location = NULL; | |
146 | } | |
147 | } | |
148 | return $this->_location; | |
149 | } | |
150 | ||
151 | /** | |
152 | * Save the values of a domain | |
153 | * | |
154 | * @return domain array | |
155 | * @access public | |
156 | */ | |
157 | static function edit(&$params, &$id) { | |
158 | $domain = new CRM_Core_DAO_Domain(); | |
159 | $domain->id = $id; | |
160 | $domain->copyValues($params); | |
161 | $domain->save(); | |
162 | return $domain; | |
163 | } | |
164 | ||
165 | /** | |
166 | * Create a new domain | |
167 | * | |
168 | * @return domain array | |
169 | * @access public | |
170 | */ | |
171 | static function create($params) { | |
172 | $domain = new CRM_Core_DAO_Domain(); | |
173 | $domain->copyValues($params); | |
174 | $domain->save(); | |
175 | return $domain; | |
176 | } | |
177 | ||
178 | static function multipleDomains() { | |
179 | $session = CRM_Core_Session::singleton(); | |
180 | ||
181 | $numberDomains = $session->get('numberDomains'); | |
182 | if (!$numberDomains) { | |
183 | $query = "SELECT count(*) from civicrm_domain"; | |
184 | $numberDomains = CRM_Core_DAO::singleValueQuery($query); | |
185 | $session->set('numberDomains', $numberDomains); | |
186 | } | |
187 | return $numberDomains > 1 ? TRUE : FALSE; | |
188 | } | |
189 | ||
190 | static function getNameAndEmail($skipFatal = FALSE) { | |
191 | $fromEmailAddress = CRM_Core_OptionGroup::values('from_email_address', NULL, NULL, NULL, ' AND is_default = 1'); | |
192 | if (!empty($fromEmailAddress)) { | |
193 | foreach ($fromEmailAddress as $key => $value) { | |
194 | $email = CRM_Utils_Mail::pluckEmailFromHeader($value); | |
195 | $fromArray = explode('"', $value); | |
196 | $fromName = CRM_Utils_Array::value(1, $fromArray); | |
197 | break; | |
198 | } | |
199 | return array($fromName, $email); | |
200 | } | |
201 | elseif ($skipFatal) { | |
202 | return array('', ''); | |
203 | } | |
204 | ||
205 | $url = CRM_Utils_System::url('civicrm/admin/domain', | |
206 | 'action=update&reset=1' | |
207 | ); | |
208 | $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)); | |
209 | ||
210 | CRM_Core_Error::fatal($status); | |
211 | } | |
212 | ||
213 | static function addContactToDomainGroup($contactID) { | |
214 | $groupID = self::getGroupId(); | |
215 | ||
216 | if ($groupID) { | |
217 | $contactIDs = array($contactID); | |
218 | CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIDs, $groupID); | |
219 | ||
220 | return $groupID; | |
221 | } | |
222 | return FALSE; | |
223 | } | |
224 | ||
225 | static function getGroupId() { | |
226 | static $groupID = NULL; | |
227 | ||
228 | if ($groupID) { | |
229 | return $groupID; | |
230 | } | |
231 | ||
232 | $domainGroupID = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MULTISITE_PREFERENCES_NAME, | |
233 | 'domain_group_id' | |
234 | ); | |
235 | $multisite = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MULTISITE_PREFERENCES_NAME, | |
236 | 'is_enabled' | |
237 | ); | |
238 | ||
239 | if ($domainGroupID) { | |
240 | $groupID = $domainGroupID; | |
241 | } | |
242 | elseif ($multisite) { | |
243 | // create a group with that of domain name | |
244 | $title = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain', | |
245 | CRM_Core_Config::domainID(), 'name' | |
246 | ); | |
247 | $groupID = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', | |
248 | $title, 'id', 'title', true | |
249 | ); | |
250 | if (empty($groupID) && !empty($title)) { | |
251 | $groupParams = array( | |
252 | 'title' => $title, | |
253 | 'is_active' => 1, | |
254 | 'no_parent' => 1, | |
255 | ); | |
256 | $group = CRM_Contact_BAO_Group::create($groupParams); | |
257 | $groupID = $group->id; | |
258 | } | |
259 | } | |
260 | return $groupID ? $groupID : FALSE; | |
261 | } | |
262 | ||
263 | static function isDomainGroup($groupId) { | |
264 | $domainGroupID = self::getGroupId(); | |
265 | return $domainGroupID == $groupId ? TRUE : FALSE; | |
266 | } | |
267 | ||
268 | static function getChildGroupIds() { | |
269 | $domainGroupID = self::getGroupId(); | |
270 | $childGrps = array(); | |
271 | ||
272 | if ($domainGroupID) { | |
273 | $childGrps = CRM_Contact_BAO_GroupNesting::getChildGroupIds($domainGroupID); | |
274 | $childGrps[] = $domainGroupID; | |
275 | } | |
276 | return $childGrps; | |
277 | } | |
278 | ||
279 | // function to retrieve a list of contact-ids that belongs to current domain/site. | |
280 | static function getContactList() { | |
281 | $siteGroups = CRM_Core_BAO_Domain::getChildGroupIds(); | |
282 | $siteContacts = array(); | |
283 | ||
284 | if (!empty($siteGroups)) { | |
285 | $query = " | |
286 | SELECT cc.id | |
287 | FROM civicrm_contact cc | |
288 | INNER JOIN civicrm_group_contact gc ON | |
289 | (gc.contact_id = cc.id AND gc.status = 'Added' AND gc.group_id IN (" . implode(',', $siteGroups) . "))"; | |
290 | ||
291 | $dao = CRM_Core_DAO::executeQuery($query); | |
292 | while ($dao->fetch()) { | |
293 | $siteContacts[] = $dao->id; | |
294 | } | |
295 | } | |
296 | return $siteContacts; | |
297 | } | |
298 | } | |
299 |