3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2013
35 class CRM_Upgrade_ThreeOne_ThreeOne
extends CRM_Upgrade_Form
{
36 function verifyPreDBState(&$errorMessage) {
37 $latestVer = CRM_Utils_System
::version();
39 $errorMessage = ts('Pre-condition failed for upgrade to %1.', array(1 => $latestVer));
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')
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));
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')
71 $errorMessage .= ' Few important tables were found missing.';
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')
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.';
104 function upgrade($rev) {
106 $upgrade = new CRM_Upgrade_Form();
109 $upgrade->processSQL($rev);
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
);
119 if (isset($values['smtpPassword'])) {
120 $values['smtpPassword'] = CRM_Utils_Crypt
::encrypt($values['smtpPassword']);
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'),
127 CRM_Core_DAO
::executeQuery($updateSql, $updateParams);
132 $domain = new CRM_Core_DAO_Domain();
133 $domain->selectAdd();
134 $domain->selectAdd('config_backend');
136 if ($domain->config_backend
) {
137 $defaults = unserialize($domain->config_backend
);
138 if ($dateFormat = CRM_Utils_Array
::value('dateformatQfDate', $defaults)) {
139 $dateFormatArray = explode(" ", $dateFormat);
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')
146 if ($dateFormat == '%b %d %Y') {
147 $defaults['dateInputFormat'] = 'mm/dd/yy';
149 elseif ($dateFormat == '%d-%b-%Y') {
150 $defaults['dateInputFormat'] = 'dd-mm-yy';
152 elseif (in_array('%b', $dateFormatArray)) {
153 $defaults['dateInputFormat'] = 'M d, yy';
155 elseif (in_array('%B', $dateFormatArray)) {
156 $defaults['dateInputFormat'] = 'MM d, yy';
159 $defaults['dateInputFormat'] = 'mm/dd/yy';
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;
170 unset($defaults['dateformatQfDatetime']);
173 unset($defaults['dateformatQfDate']);
174 unset($defaults['dateformatTime']);
175 CRM_Core_BAO_ConfigSetting
::add($defaults);
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')));
186 $fromNamekey = $elementName . '_from';
187 $toNamekey = $elementName . '_to';
189 $fromNameVal = $fromVal[$fromNamekey];
190 $toNameVal = $fromVal[$toNamekey];
191 //check 'choose date range' is set
193 if (CRM_Utils_Date
::isDate($fromNameVal)) {
194 $fromDate = CRM_Utils_Date
::setDateDefaults(CRM_Utils_Date
::format($fromNameVal));
195 $fromNameVal = $fromDate[0];
201 if (CRM_Utils_Date
::isDate($toNameVal)) {
202 $toDate = CRM_Utils_Date
::setDateDefaults(CRM_Utils_Date
::format($toNameVal));
203 $toNameVal = $toDate[0];
213 $fromVal[$fromNamekey] = $fromNameVal;
214 $fromVal[$toNamekey] = $toNameVal;
219 $fromVal = serialize($fromVal);
220 $updateSQL = "UPDATE civicrm_report_instance SET form_values = '{$fromVal}' WHERE id = {$instDAO->id}";
221 CRM_Core_DAO
::executeQuery($updateSQL);
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);
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);
237 $newDateFormat = 'mm/dd/yy';
238 if ($year && $month && $date) {
239 $newDateFormat = 'mm/dd/yy';
241 elseif (!$year && $month && $date) {
242 $newDateFormat = 'mm/dd';
245 $newTimeFormat = 'NULL';
246 if ($timeFormat && $hour == 'h') {
252 $updateSQL = "UPDATE civicrm_custom_field SET date_format = '{$newDateFormat}', time_format = {$newTimeFormat} WHERE id = {$customDAO->id}";
253 CRM_Core_DAO
::executeQuery($updateSQL);
256 $template = CRM_Core_Smarty
::singleton();
257 $afterUpgradeMessage = '';
258 if ($afterUpgradeMessage = $template->get_template_vars('afterUpgradeMessage')) {
259 $afterUpgradeMessage .= "<br/><br/>";
261 $afterUpgradeMessage .= ts("Date Input Format has been set to %1 format. If you want to use a different format please check Administer CiviCRM » Localization » Date Formats.", array(1 => $defaults['dateInputFormat']));
262 $template->assign('afterUpgradeMessage', $afterUpgradeMessage);
265 function upgrade_3_1_3() {
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);
275 $sql = "SELECT ROW_COUNT();";
277 if ($count = CRM_Core_DAO
::singleValueQuery($sql)) {
278 $totalCount +
= $count;
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);
288 if ($count = CRM_Core_DAO
::singleValueQuery($sql)) {
289 $totalCount +
= $count;
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);
299 if ($count = CRM_Core_DAO
::singleValueQuery($sql)) {
300 $totalCount +
= $count;
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);
310 if ($count = CRM_Core_DAO
::singleValueQuery($sql)) {
311 $totalCount +
= $count;
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);
321 if ($count = CRM_Core_DAO
::singleValueQuery($sql)) {
322 $totalCount +
= $count;
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);
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);
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"
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);