copyright and version fixes
[civicrm-core.git] / CRM / Upgrade / ThreeOne / ThreeOne.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
32 * $Id$
33 *
34 */
35 class CRM_Upgrade_ThreeOne_ThreeOne extends CRM_Upgrade_Form {
36 function verifyPreDBState(&$errorMessage) {
37 $latestVer = CRM_Utils_System::version();
38
39 $errorMessage = ts('Pre-condition failed for upgrade to %1.', array(1 => $latestVer));
40
41 // check tables and table-columns, if the db is already 3.1
42 if (CRM_Core_DAO::checkTableExists('civicrm_acl_contact_cache') ||
43 CRM_Core_DAO::checkTableExists('civicrm_contact_type') ||
44 CRM_Core_DAO::checkTableExists('civicrm_dashboard') ||
45 CRM_Core_DAO::checkTableExists('civicrm_dashboard_contact') ||
46 CRM_Core_DAO::checkFieldExists('civicrm_country', 'is_province_abbreviated') ||
47 CRM_Core_DAO::checkFieldExists('civicrm_custom_field', 'date_format') ||
48 CRM_Core_DAO::checkFieldExists('civicrm_custom_field', 'time_format') ||
49 CRM_Core_DAO::checkFieldExists('civicrm_mail_settings', 'domain_id') ||
50 CRM_Core_DAO::checkFieldExists('civicrm_msg_template', 'workflow_id') ||
51 CRM_Core_DAO::checkFieldExists('civicrm_msg_template', 'is_default') ||
52 CRM_Core_DAO::checkFieldExists('civicrm_msg_template', 'is_reserved') ||
53 CRM_Core_DAO::checkFieldExists('civicrm_option_value', 'domain_id') ||
54 CRM_Core_DAO::checkFieldExists('civicrm_preferences', 'contact_autocomplete_options') ||
55 CRM_Core_DAO::checkFieldExists('civicrm_preferences_date', 'date_format') ||
56 CRM_Core_DAO::checkFieldExists('civicrm_preferences_date', 'time_format') ||
57 CRM_Core_DAO::checkFieldExists('civicrm_price_set', 'domain_id') ||
58 CRM_Core_DAO::checkFieldExists('civicrm_price_set', 'extends') ||
59 CRM_Core_DAO::checkFieldExists('civicrm_relationship_type', 'contact_sub_type_a') ||
60 CRM_Core_DAO::checkFieldExists('civicrm_relationship_type', 'contact_sub_type_b') ||
61 CRM_Core_DAO::checkFieldExists('civicrm_report_instance', 'domain_id')
62 ) {
63 $errorMessage = ts("Database check failed - it looks like you have already upgraded to the latest version (v%1) of the database. OR If you think this message is wrong, it is very likely that this a partially upgraded database and you will need to reload the correct database from backup on which upgrade was never tried.", array(1 => $latestVer));
64 return FALSE;
65 }
66
67 //check previous version tables e.g 3.0.*
68 if (!CRM_Core_DAO::checkTableExists('civicrm_participant_status_type') ||
69 !CRM_Core_DAO::checkTableExists('civicrm_navigation')
70 ) {
71 $errorMessage .= ' Few important tables were found missing.';
72 return FALSE;
73 }
74
75 // check fields which MUST be present if a proper 3.0.* db
76 if (!CRM_Core_DAO::checkFieldExists('civicrm_contact', 'email_greeting_id') ||
77 !CRM_Core_DAO::checkFieldExists('civicrm_contribution_page', 'created_id') ||
78 !CRM_Core_DAO::checkFieldExists('civicrm_custom_group', 'created_date') ||
79 !CRM_Core_DAO::checkFieldExists('civicrm_event', 'is_template') ||
80 !CRM_Core_DAO::checkFieldExists('civicrm_event', 'created_id') ||
81 !CRM_Core_DAO::checkFieldExists('civicrm_mailing', 'created_date') ||
82 !CRM_Core_DAO::checkFieldExists('civicrm_mapping_field', 'im_provider_id') ||
83 !CRM_Core_DAO::checkFieldExists('civicrm_membership_type', 'domain_id') ||
84 !CRM_Core_DAO::checkFieldExists('civicrm_menu', 'domain_id') ||
85 !CRM_Core_DAO::checkFieldExists('civicrm_participant', 'fee_currency') ||
86 !CRM_Core_DAO::checkFieldExists('civicrm_payment_processor', 'domain_id') ||
87 !CRM_Core_DAO::checkFieldExists('civicrm_payment_processor_type', 'payment_type') ||
88 !CRM_Core_DAO::checkFieldExists('civicrm_preferences', 'domain_id') ||
89 !CRM_Core_DAO::checkFieldExists('civicrm_preferences', 'navigation') ||
90 !CRM_Core_DAO::checkFieldExists('civicrm_relationship_type', 'label_a_b') ||
91 !CRM_Core_DAO::checkFieldExists('civicrm_report_instance', 'navigation_id') ||
92 !CRM_Core_DAO::checkFieldExists('civicrm_uf_field', 'is_reserved') ||
93 !CRM_Core_DAO::checkFieldExists('civicrm_uf_group', 'created_id') ||
94 !CRM_Core_DAO::checkFieldExists('civicrm_uf_match', 'domain_id')
95 ) {
96 // db looks to have stuck somewhere between 3.0 & 3.1
97 $errorMessage .= ' Few important fields were found missing in some of the tables.';
98 return FALSE;
99 }
100
101 return TRUE;
102 }
103
104 function upgrade($rev) {
105
106 $upgrade = new CRM_Upgrade_Form();
107
108 //Run the SQL file
109 $upgrade->processSQL($rev);
110
111 // fix for CRM-5162
112 // we need to encrypt all smtpPasswords if present
113 $sql = 'SELECT id, mailing_backend FROM civicrm_preferences';
114 $mailingDomain = CRM_Core_DAO::executeQuery($sql);
115 while ($mailingDomain->fetch()) {
116 if ($mailingDomain->mailing_backend) {
117 $values = unserialize($mailingDomain->mailing_backend);
118
119 if (isset($values['smtpPassword'])) {
120 $values['smtpPassword'] = CRM_Utils_Crypt::encrypt($values['smtpPassword']);
121
122 $updateSql = 'UPDATE civicrm_preferences SET mailing_backend = %1 WHERE id = %2';
123 $updateParams = array(
124 1 => array(serialize($values), 'String'),
125 2 => array($mailingDomain->id, 'Integer'),
126 );
127 CRM_Core_DAO::executeQuery($updateSql, $updateParams);
128 }
129 }
130 }
131
132 $domain = new CRM_Core_DAO_Domain();
133 $domain->selectAdd();
134 $domain->selectAdd('config_backend');
135 $domain->find(TRUE);
136 if ($domain->config_backend) {
137 $defaults = unserialize($domain->config_backend);
138 if ($dateFormat = CRM_Utils_Array::value('dateformatQfDate', $defaults)) {
139 $dateFormatArray = explode(" ", $dateFormat);
140
141 //replace new date format based on previous month format
142 //%b month name [abbreviated]
143 //%B full month name ('January'..'December')
144 //%m decimal number, 0-padded ('01'..'12')
145
146 if ($dateFormat == '%b %d %Y') {
147 $defaults['dateInputFormat'] = 'mm/dd/yy';
148 }
149 elseif ($dateFormat == '%d-%b-%Y') {
150 $defaults['dateInputFormat'] = 'dd-mm-yy';
151 }
152 elseif (in_array('%b', $dateFormatArray)) {
153 $defaults['dateInputFormat'] = 'M d, yy';
154 }
155 elseif (in_array('%B', $dateFormatArray)) {
156 $defaults['dateInputFormat'] = 'MM d, yy';
157 }
158 else {
159 $defaults['dateInputFormat'] = 'mm/dd/yy';
160 }
161 }
162 // %p - lowercase ante/post meridiem ('am', 'pm')
163 // %P - uppercase ante/post meridiem ('AM', 'PM')
164 if ($dateTimeFormat = CRM_Utils_Array::value('dateformatQfDatetime', $defaults)) {
165 $defaults['timeInputFormat'] = 2;
166 $dateTimeFormatArray = explode(" ", $dateFormat);
167 if (in_array('%P', $dateTimeFormatArray) || in_array('%p', $dateTimeFormatArray)) {
168 $defaults['timeInputFormat'] = 1;
169 }
170 unset($defaults['dateformatQfDatetime']);
171 }
172
173 unset($defaults['dateformatQfDate']);
174 unset($defaults['dateformatTime']);
175 CRM_Core_BAO_ConfigSetting::add($defaults);
176 }
177
178 $sql = "SELECT id, form_values FROM civicrm_report_instance";
179 $instDAO = CRM_Core_DAO::executeQuery($sql);
180 while ($instDAO->fetch()) {
181 $fromVal = @unserialize($instDAO->form_values);
182 foreach ((array)$fromVal as $key => $value) {
183 if (strstr($key, '_relative')) {
184 $elementName = substr($key, 0, (strlen($key) - strlen('_relative')));
185
186 $fromNamekey = $elementName . '_from';
187 $toNamekey = $elementName . '_to';
188
189 $fromNameVal = $fromVal[$fromNamekey];
190 $toNameVal = $fromVal[$toNamekey];
191 //check 'choose date range' is set
192 if ($value == '0') {
193 if (CRM_Utils_Date::isDate($fromNameVal)) {
194 $fromDate = CRM_Utils_Date::setDateDefaults(CRM_Utils_Date::format($fromNameVal));
195 $fromNameVal = $fromDate[0];
196 }
197 else {
198 $fromNameVal = '';
199 }
200
201 if (CRM_Utils_Date::isDate($toNameVal)) {
202 $toDate = CRM_Utils_Date::setDateDefaults(CRM_Utils_Date::format($toNameVal));
203 $toNameVal = $toDate[0];
204 }
205 else {
206 $toNameVal = '';
207 }
208 }
209 else {
210 $fromNameVal = '';
211 $toNameVal = '';
212 }
213 $fromVal[$fromNamekey] = $fromNameVal;
214 $fromVal[$toNamekey] = $toNameVal;
215 continue;
216 }
217 }
218
219 $fromVal = serialize($fromVal);
220 $updateSQL = "UPDATE civicrm_report_instance SET form_values = '{$fromVal}' WHERE id = {$instDAO->id}";
221 CRM_Core_DAO::executeQuery($updateSQL);
222 }
223
224 $customFieldSQL = "SELECT id, date_format FROM civicrm_custom_field WHERE data_type = 'Date' ";
225 $customDAO = CRM_Core_DAO::executeQuery($customFieldSQL);
226 while ($customDAO->fetch()) {
227 $datePartKey = $dateParts = explode(CRM_Core_DAO::VALUE_SEPARATOR, $customDAO->date_format);
228 $dateParts = array_combine($datePartKey, $dateParts);
229
230 $year = CRM_Utils_Array::value('Y', $dateParts);
231 $month = CRM_Utils_Array::value('M', $dateParts);
232 $date = CRM_Utils_Array::value('d', $dateParts);
233 $hour = CRM_Utils_Array::value('h', $dateParts);
234 $minute = CRM_Utils_Array::value('i', $dateParts);
235 $timeFormat = CRM_Utils_Array::value('A', $dateParts);
236
237 $newDateFormat = 'mm/dd/yy';
238 if ($year && $month && $date) {
239 $newDateFormat = 'mm/dd/yy';
240 }
241 elseif (!$year && $month && $date) {
242 $newDateFormat = 'mm/dd';
243 }
244
245 $newTimeFormat = 'NULL';
246 if ($timeFormat && $hour == 'h') {
247 $newTimeFormat = 1;
248 }
249 elseif ($hour) {
250 $newTimeFormat = 2;
251 }
252 $updateSQL = "UPDATE civicrm_custom_field SET date_format = '{$newDateFormat}', time_format = {$newTimeFormat} WHERE id = {$customDAO->id}";
253 CRM_Core_DAO::executeQuery($updateSQL);
254 }
255
256 $template = CRM_Core_Smarty::singleton();
257 $afterUpgradeMessage = '';
258 if ($afterUpgradeMessage = $template->get_template_vars('afterUpgradeMessage')) {
259 $afterUpgradeMessage .= "<br/><br/>";
260 }
261 $afterUpgradeMessage .= ts("Date Input Format has been set to %1 format. If you want to use a different format please check Administer CiviCRM &raquo; Localization &raquo; Date Formats.", array(1 => $defaults['dateInputFormat']));
262 $template->assign('afterUpgradeMessage', $afterUpgradeMessage);
263 }
264
265 function upgrade_3_1_3() {
266 $count = 0;
267 $totalCount = 0;
268 $addressQuery = "
269 UPDATE civicrm_address as address
270 INNER JOIN ( SELECT id, contact_id FROM civicrm_address WHERE is_primary = 1 GROUP BY contact_id HAVING count( id ) > 1 ) as dup_address
271 ON ( address.contact_id = dup_address.contact_id AND address.id != dup_address.id )
272 SET address.is_primary = 0";
273 CRM_Core_DAO::executeQuery($addressQuery);
274
275 $sql = "SELECT ROW_COUNT();";
276
277 if ($count = CRM_Core_DAO::singleValueQuery($sql)) {
278 $totalCount += $count;
279 }
280
281 $emailQuery = "
282 UPDATE civicrm_email as email
283 INNER JOIN ( SELECT id, contact_id FROM civicrm_email WHERE is_primary = 1 GROUP BY contact_id HAVING count( id ) > 1 ) as dup_email
284 ON ( email.contact_id = dup_email.contact_id AND email.id != dup_email.id )
285 SET email.is_primary = 0";
286 CRM_Core_DAO::executeQuery($emailQuery);
287
288 if ($count = CRM_Core_DAO::singleValueQuery($sql)) {
289 $totalCount += $count;
290 }
291
292 $phoneQuery = "
293 UPDATE civicrm_phone as phone
294 INNER JOIN ( SELECT id, contact_id FROM civicrm_phone WHERE is_primary = 1 GROUP BY contact_id HAVING count( id ) > 1 ) as dup_phone
295 ON ( phone.contact_id = dup_phone.contact_id AND phone.id != dup_phone.id )
296 SET phone.is_primary = 0";
297 CRM_Core_DAO::executeQuery($phoneQuery);
298
299 if ($count = CRM_Core_DAO::singleValueQuery($sql)) {
300 $totalCount += $count;
301 }
302
303 $imQuery = "
304 UPDATE civicrm_im as im
305 INNER JOIN ( SELECT id, contact_id FROM civicrm_im WHERE is_primary = 1 GROUP BY contact_id HAVING count( id ) > 1 ) as dup_im
306 ON ( im.contact_id = dup_im.contact_id AND im.id != dup_im.id )
307 SET im.is_primary = 0";
308 CRM_Core_DAO::executeQuery($imQuery);
309
310 if ($count = CRM_Core_DAO::singleValueQuery($sql)) {
311 $totalCount += $count;
312 }
313
314 $openidQuery = "
315 UPDATE civicrm_openid as openid
316 INNER JOIN ( SELECT id, contact_id FROM civicrm_openid WHERE is_primary = 1 GROUP BY contact_id HAVING count( id ) > 1 ) as dup_openid
317 ON ( openid.contact_id = dup_openid.contact_id AND openid.id != dup_openid.id )
318 SET openid.is_primary = 0";
319 CRM_Core_DAO::executeQuery($openidQuery);
320
321 if ($count = CRM_Core_DAO::singleValueQuery($sql)) {
322 $totalCount += $count;
323 }
324
325 $afterUpgradeMessage = '';
326 if (!empty($totalCount)) {
327 $template = CRM_Core_Smarty::singleton();
328 $afterUpgradeMessage = $template->get_template_vars('afterUpgradeMessage');
329 $afterUpgradeMessage .= "<br/><br/>";
330 $afterUpgradeMessage .= ts("%1 records have been updated so that each contact record should contain only one Address, Email, Phone, Instant Messanger and openID as primary.", array(1 => $totalCount));
331 $template->assign('afterUpgradeMessage', $afterUpgradeMessage);
332 }
333 }
334
335 function upgrade_3_1_4() {
336 $query = "SELECT id FROM civicrm_payment_processor WHERE payment_processor_type = 'Moneris' LIMIT 1";
337 $isMoneris = CRM_Core_DAO::singleValueQuery($query);
338
339 if ($isMoneris) {
340 $template = CRM_Core_Smarty::singleton();
341 $afterUpgradeMessage = $template->get_template_vars('afterUpgradeMessage');
342 $docURL = CRM_Utils_System::docURL2('Moneris Configuration Guide', FALSE, 'download and install',
343 NULL, 'color: white; text-decoration: underline;', "wiki"
344 );
345
346 $afterUpgradeMessage .= "<br/>" . ts("Please %1 mpgClasses.php in packages/Services in order to continue using Moneris payment processor. That file is no longer included in the CiviCRM distribution.", array(1 => $docURL));
347 $template->assign('afterUpgradeMessage', $afterUpgradeMessage);
348 }
349 }
350 }
351