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