Merge pull request #1108 from pradpnayak/CRM-12929
[civicrm-core.git] / CRM / Upgrade / ThreeZero / ThreeZero.php
CommitLineData
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 */
35class CRM_Upgrade_ThreeZero_ThreeZero 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 // check table, if the db is 3.0
41 if (CRM_Core_DAO::checkTableExists('civicrm_navigation') &&
42 CRM_Core_DAO::checkTableExists('civicrm_participant_status_type')
43 ) {
44 $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 db and you will need to reload the correct db on which upgrade was never tried.", array(1 => $latestVer));
45 return FALSE;
46 }
47 // check table-column, if the db is 3.0
48 if (CRM_Core_DAO::checkFieldExists('civicrm_menu', 'domain_id') &&
49 CRM_Core_DAO::checkFieldExists('civicrm_event', 'created_id') &&
50 CRM_Core_DAO::checkFieldExists('civicrm_event', 'is_template') &&
51 CRM_Core_DAO::checkFieldExists('civicrm_uf_field', 'is_reserved') &&
52 CRM_Core_DAO::checkFieldExists('civicrm_contact', 'email_greeting_id') &&
53 CRM_Core_DAO::checkFieldExists('civicrm_payment_processor_type', 'payment_type')
54 ) {
55
56 $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 db and you will need to reload the correct db on which upgrade was never tried.", array(1 => $latestVer));
57 return FALSE;
58 }
59
60 //check previous version table e.g 2.2.*
61 if (!CRM_Core_DAO::checkTableExists('civicrm_cache') ||
62 !CRM_Core_DAO::checkTableExists('civicrm_pcp_block') ||
63 !CRM_Core_DAO::checkTableExists('civicrm_menu') ||
64 !CRM_Core_DAO::checkTableExists('civicrm_discount') ||
65 !CRM_Core_DAO::checkTableExists('civicrm_pcp') ||
66 !CRM_Core_DAO::checkTableExists('civicrm_pledge_block') ||
67 !CRM_Core_DAO::checkTableExists('civicrm_contribution_soft')
68 ) {
69
70 $errorMessage .= ' Few important tables were found missing.';
71 return FALSE;
72 }
73
74 // check fields which MUST be present if a proper 2.2.* db
75 if (!CRM_Core_DAO::checkFieldExists('civicrm_activity', 'due_date_time') ||
76 !CRM_Core_DAO::checkFieldExists('civicrm_contact', 'greeting_type_id') ||
77 !CRM_Core_DAO::checkFieldExists('civicrm_contribution', 'check_number')
78 ) {
79 // db looks to have stuck somewhere between 2.1 & 2.2
80 $errorMessage .= ' Few important fields were found missing in some of the tables.';
81 return FALSE;
82 }
83
84 return TRUE;
85 }
86
87 function upgrade($rev) {
88
89 // fix CRM-5270: if civicrm_report_instance.description is localised,
90 // recreate it based on the first locale’s description_xx_YY contents
91 // and drop all the description_xx_YY columns
92 if (!CRM_Core_DAO::checkFieldExists('civicrm_report_instance', 'description')) {
93 $domain = new CRM_Core_DAO_Domain;
94 $domain->find(TRUE);
95 $locales = explode(CRM_Core_DAO::VALUE_SEPARATOR, $domain->locales);
96
97 CRM_Core_DAO::executeQuery("ALTER TABLE civicrm_report_instance ADD description VARCHAR(255)");
98 CRM_Core_DAO::executeQuery("UPDATE civicrm_report_instance SET description = description_{$locales[0]}");
99
100 CRM_Core_DAO::executeQuery("DROP TRIGGER IF EXISTS civicrm_report_instance_before_insert");
101 foreach ($locales as $locale) {
102 CRM_Core_DAO::executeQuery("DROP VIEW civicrm_report_instance_$locale");
103 CRM_Core_DAO::executeQuery("ALTER TABLE civicrm_report_instance DROP description_$locale");
104 }
105 }
106
107 //We execute some part of php after sql and then again sql
108 //So using conditions for skipping some part of sql CRM-4575
109
110 $upgrade = new CRM_Upgrade_Form();
111 //Run the SQL file (1)
112 $upgrade->processSQL($rev);
113 //replace with ; in report instance
114 $sql = "UPDATE civicrm_report_instance
115 SET form_values = REPLACE(form_values,'#',';') ";
116 CRM_Core_DAO::executeQuery($sql, CRM_Core_DAO::$_nullArray);
117
118 //delete unnecessary activities
119 $bulkEmailID = CRM_Core_OptionGroup::getValue('activity_type', 'Bulk Email', 'name');
120
121 if ($bulkEmailID) {
122
123 $mailingActivityIds = array();
124 $query = "
125 SELECT max( ca.id ) as aid,
126 ca.source_record_id sid
127 FROM civicrm_activity ca
128 WHERE ca.activity_type_id = %1
129 GROUP BY ca.source_record_id";
130
131 $params = array(1 => array($bulkEmailID, 'Integer'));
132 $dao = CRM_Core_DAO::executeQuery($query, $params);
133
134 while ($dao->fetch()) {
135 $updateQuery = "
136 UPDATE civicrm_activity_target cat, civicrm_activity ca
137 SET cat.activity_id = {$dao->aid}
138 WHERE ca.source_record_id IS NOT NULL AND
139 ca.activity_type_id = %1 AND
140 ca.id <> {$dao->aid} AND
141 ca.source_record_id = {$dao->sid} AND
142 ca.id = cat.activity_id";
143
144 $updateParams = array(1 => array($bulkEmailID, 'Integer'));
145 CRM_Core_DAO::executeQuery($updateQuery, $updateParams);
146
147 $deleteQuery = "
148 DELETE ca.*
149 FROM civicrm_activity ca
150 WHERE ca.source_record_id IS NOT NULL AND
151 ca.activity_type_id = %1 AND
152 ca.id <> {$dao->aid} AND
153 ca.source_record_id = {$dao->sid}";
154
155 $deleteParams = array(1 => array($bulkEmailID, 'Integer'));
156 CRM_Core_DAO::executeQuery($deleteQuery, $deleteParams);
157 }
158 }
159
160 //CRM-4453
161 //lets insert column in civicrm_aprticipant table
162 $query = "
163 ALTER TABLE `civicrm_participant`
164 ADD `fee_currency` VARCHAR( 64 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL COMMENT '3 character string, value derived from config setting.' AFTER `discount_id`";
165 CRM_Core_DAO::executeQuery($query);
166
167 //get currency from contribution table if exists/default
168 //insert currency when fee_amount != NULL or event is paid.
169 $query = "
170 SELECT civicrm_participant.id
171 FROM civicrm_participant
172 LEFT JOIN civicrm_event
173 ON ( civicrm_participant.event_id = civicrm_event.id )
174 WHERE civicrm_participant.fee_amount IS NOT NULL OR
175 civicrm_event.is_monetary = 1";
176
177 $participant = CRM_Core_DAO::executeQuery($query);
178 while ($participant->fetch()) {
179 $query = "
180 SELECT civicrm_contribution.currency
181 FROM civicrm_contribution,
182 civicrm_participant_payment
183 WHERE civicrm_contribution.id = civicrm_participant_payment.contribution_id AND
184 civicrm_participant_payment.participant_id = {$participant->id}";
185
186 $currencyID = CRM_Core_DAO::singleValueQuery($query);
187 if (!$currencyID) {
188 $config = CRM_Core_Config::singleton();
189 $currencyID = $config->defaultCurrency;
190 }
191
192 //finally update participant record.
193 CRM_Core_DAO::setFieldValue('CRM_Event_DAO_Participant', $participant->id, 'fee_currency', $currencyID);
194 }
195
196 //CRM-4575
197 //check whether {contact.name} is set in mailing labels
198 $mailingFormat = self::getPreference('mailing_format');
199 $addNewAddressee = TRUE;
200
201 if (strpos($mailingFormat, '{contact.contact_name}') === FALSE) {
202 $addNewAddressee = FALSE;
203 }
204 else {
205 //else compare individual name format with default individual addressee.
206 $individualNameFormat = self::getPreference('individual_name_format');
207
208 $defaultAddressee = CRM_Core_OptionGroup::values('addressee', FALSE, FALSE, FALSE,
209 " AND v.filter = 1 AND v.is_default = 1", 'label'
210 );
211
212 if (array_search($individualNameFormat, $defaultAddressee) !== FALSE) {
213 $addNewAddressee = FALSE;
214 }
215 }
216
217 $docURL = CRM_Utils_System::docURL2('Update Greetings and Address Data for Contacts', FALSE, NULL, NULL, 'color: white; text-decoration: underline;', "wiki");
218
219 if ($addNewAddressee) {
220 //otherwise insert new token in addressee and set as a default
221 $addresseeGroupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup',
222 'addressee',
223 'id',
224 'name'
225 );
226
227 $optionValueParams = array(
228 'label' => $individualNameFormat,
229 'is_active' => 1,
230 'contactOptions' => 1,
231 'filter' => 1,
232 'is_default' => 1,
233 'reset_default_for' => array('filter' => "0, 1"),
234 );
235
236 $action = CRM_Core_Action::ADD;
237 $addresseeGroupParams = array('name' => 'addressee');
238 $fieldValues = array('option_group_id' => $addresseeGroupId);
239 $weight = CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue', $fieldValues);
240
241 $optionValueParams['weight'] = $weight;
242 $addresseeToken = CRM_Core_OptionValue::addOptionValue($optionValueParams, $addresseeGroupParams,
243 $action, $optionId = NULL
244 );
245
246 $afterUpgradeMessage = ts("During this upgrade, Postal Addressee values have been stored for each contact record using the system default format - %2.You will need to run the included command-line script to update your Individual contact records to use the \"Individual Name Format\" previously specified for your site %1", array(1 => $docURL, 2 => array_pop($defaultAddressee)));
247 }
248 else {
249 $afterUpgradeMessage = ts("Email Greeting, Postal Greeting and Postal Addressee values have been stored for all contact records based on the system default formats. If you want to use a different format for any of these contact fields - you can run the provided command line script to update contacts to a different format %1 ", array(1 => $docURL));
250 }
251
252 //replace contact.contact_name with contact.addressee in civicrm_preference.mailing_format
253 $updateQuery = "
254 UPDATE civicrm_preferences
255 SET `mailing_format` =
256 replace(`mailing_format`, '{contact.contact_name}','{contact.addressee}')";
257
258 CRM_Core_DAO::executeQuery($updateQuery);
259
260 //drop column individual_name_format
261 $alterQuery = "
262 ALTER TABLE `civicrm_preferences`
263 DROP `individual_name_format`";
264
265 CRM_Core_DAO::executeQuery($alterQuery);
266
267 //set status message for default greetings
268 $template = CRM_Core_Smarty::singleton();
269 $template->assign('afterUpgradeMessage', $afterUpgradeMessage);
270 }
271
272 /**
273 * Load a preference
274 *
275 * This is replaces the defunct CRM_Core_BAO_Preferences::value()
276 */
277 static
278 function getPreference($name) {
279 $sql = "SELECT $name FROM civicrm_preferences WHERE domain_id = %1 AND is_domain = 1 AND contact_id IS NULL";
280 $params = array(
281 1 => array(CRM_Core_Config::domainID(), 'Integer'),
282 );
283 return CRM_Core_DAO::singleValueQuery($sql, $params);
284 }
285}
286