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