PageRenderTime 26ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/CRM/Upgrade/ThreeZero/ThreeZero.php

https://github.com/ksecor/civicrm
PHP | 271 lines | 169 code | 46 blank | 56 comment | 18 complexity | de1f98aa51a42c46c8657d03b022d893 MD5 | raw file
  1. <?php
  2. /*
  3. +--------------------------------------------------------------------+
  4. | CiviCRM version 3.1 |
  5. +--------------------------------------------------------------------+
  6. | Copyright CiviCRM LLC (c) 2004-2009 |
  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. |
  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 along with this program; if not, contact CiviCRM LLC |
  21. | at info[AT]civicrm[DOT]org. If you have questions about the |
  22. | GNU Affero General Public License or the licensing of CiviCRM, |
  23. | see the CiviCRM license FAQ at http://civicrm.org/licensing |
  24. +--------------------------------------------------------------------+
  25. */
  26. /**
  27. *
  28. * @package CRM
  29. * @copyright CiviCRM LLC (c) 2004-2009
  30. * $Id$
  31. *
  32. */
  33. require_once 'CRM/Upgrade/Form.php';
  34. require_once 'CRM/Core/OptionGroup.php';
  35. require_once 'CRM/Core/OptionValue.php';
  36. class CRM_Upgrade_ThreeZero_ThreeZero extends CRM_Upgrade_Form {
  37. function verifyPreDBState( &$errorMessage ) {
  38. $latestVer = CRM_Utils_System::version();
  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. $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 ));
  44. return false;
  45. }
  46. // check table-column, if the db is 3.0
  47. if ( CRM_Core_DAO::checkFieldExists( 'civicrm_menu', 'domain_id' ) &&
  48. CRM_Core_DAO::checkFieldExists( 'civicrm_event', 'created_id' ) &&
  49. CRM_Core_DAO::checkFieldExists( 'civicrm_event', 'is_template' ) &&
  50. CRM_Core_DAO::checkFieldExists( 'civicrm_uf_field', 'is_reserved' ) &&
  51. CRM_Core_DAO::checkFieldExists( 'civicrm_contact', 'email_greeting_id' ) &&
  52. CRM_Core_DAO::checkFieldExists( 'civicrm_payment_processor_type', 'payment_type' ) ) {
  53. $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 ));
  54. return false;
  55. }
  56. //check previous version table e.g 2.2.*
  57. if ( ! CRM_Core_DAO::checkTableExists( 'civicrm_cache' ) ||
  58. ! CRM_Core_DAO::checkTableExists( 'civicrm_pcp_block' ) ||
  59. ! CRM_Core_DAO::checkTableExists( 'civicrm_menu' ) ||
  60. ! CRM_Core_DAO::checkTableExists( 'civicrm_discount' ) ||
  61. ! CRM_Core_DAO::checkTableExists( 'civicrm_pcp' ) ||
  62. ! CRM_Core_DAO::checkTableExists( 'civicrm_pledge_block' ) ||
  63. ! CRM_Core_DAO::checkTableExists( 'civicrm_contribution_soft' ) ) {
  64. $errorMessage .= ' Few important tables were found missing.';
  65. return false;
  66. }
  67. // check fields which MUST be present if a proper 2.2.* db
  68. if ( ! CRM_Core_DAO::checkFieldExists( 'civicrm_activity', 'due_date_time' ) ||
  69. ! CRM_Core_DAO::checkFieldExists( 'civicrm_contact', 'greeting_type_id' ) ||
  70. ! CRM_Core_DAO::checkFieldExists( 'civicrm_contribution', 'check_number' ) ) {
  71. // db looks to have stuck somewhere between 2.1 & 2.2
  72. $errorMessage .= ' Few important fields were found missing in some of the tables.';
  73. return false;
  74. }
  75. return true;
  76. }
  77. function upgrade( $rev ) {
  78. // fix CRM-5270: if civicrm_report_instance.description is localised,
  79. // recreate it based on the first locale’s description_xx_YY contents
  80. // and drop all the description_xx_YY columns
  81. if (!CRM_Core_DAO::checkFieldExists('civicrm_report_instance', 'description')) {
  82. require_once 'CRM/Core/DAO/Domain.php';
  83. $domain = new CRM_Core_DAO_Domain;
  84. $domain->find(true);
  85. $locales = explode(CRM_Core_DAO::VALUE_SEPARATOR, $domain->locales);
  86. CRM_Core_DAO::executeQuery("ALTER TABLE civicrm_report_instance ADD description VARCHAR(255)");
  87. CRM_Core_DAO::executeQuery("UPDATE civicrm_report_instance SET description = description_{$locales[0]}");
  88. CRM_Core_DAO::executeQuery("DROP TRIGGER civicrm_report_instance_before_insert");
  89. foreach ($locales as $locale) {
  90. CRM_Core_DAO::executeQuery("DROP VIEW civicrm_report_instance_$locale");
  91. CRM_Core_DAO::executeQuery("ALTER TABLE civicrm_report_instance DROP description_$locale");
  92. }
  93. }
  94. //We execute some part of php after sql and then again sql
  95. //So using conditions for skipping some part of sql CRM-4575
  96. $upgrade =& new CRM_Upgrade_Form( );
  97. //Run the SQL file (1)
  98. $upgrade->processSQL( $rev );
  99. //replace with ; in report instance
  100. $sql = "UPDATE civicrm_report_instance
  101. SET form_values = REPLACE(form_values,'#',';') ";
  102. CRM_Core_DAO::executeQuery( $sql, CRM_Core_DAO::$_nullArray );
  103. //delete unnecessary activities
  104. $bulkEmailID = CRM_Core_OptionGroup::getValue('activity_type', 'Bulk Email', 'name' );
  105. if ( $bulkEmailID ) {
  106. $mailingActivityIds = array( );
  107. $query = "
  108. SELECT max( ca.id ) as aid,
  109. ca.source_record_id sid
  110. FROM civicrm_activity ca
  111. WHERE ca.activity_type_id = %1
  112. GROUP BY ca.source_record_id";
  113. $params = array( 1 => array( $bulkEmailID, 'Integer' ) );
  114. $dao = CRM_Core_DAO::executeQuery( $query, $params );
  115. while ( $dao->fetch( ) ) {
  116. $updateQuery = "
  117. UPDATE civicrm_activity_target cat, civicrm_activity ca
  118. SET cat.activity_id = {$dao->aid}
  119. WHERE ca.source_record_id IS NOT NULL AND
  120. ca.activity_type_id = %1 AND
  121. ca.id <> {$dao->aid} AND
  122. ca.source_record_id = {$dao->sid} AND
  123. ca.id = cat.activity_id";
  124. $updateParams = array( 1 => array( $bulkEmailID, 'Integer' ) );
  125. CRM_Core_DAO::executeQuery( $updateQuery, $updateParams );
  126. $deleteQuery = "
  127. DELETE ca.*
  128. FROM civicrm_activity ca
  129. WHERE ca.source_record_id IS NOT NULL AND
  130. ca.activity_type_id = %1 AND
  131. ca.id <> {$dao->aid} AND
  132. ca.source_record_id = {$dao->sid}";
  133. $deleteParams = array( 1 => array( $bulkEmailID, 'Integer' ) );
  134. CRM_Core_DAO::executeQuery( $deleteQuery, $deleteParams );
  135. }
  136. }
  137. //CRM-4453
  138. //lets insert column in civicrm_aprticipant table
  139. $query = "
  140. ALTER TABLE `civicrm_participant`
  141. 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`";
  142. CRM_Core_DAO::executeQuery( $query );
  143. //get currency from contribution table if exists/default
  144. //insert currency when fee_amount != NULL or event is paid.
  145. $query = "
  146. SELECT civicrm_participant.id
  147. FROM civicrm_participant
  148. LEFT JOIN civicrm_event
  149. ON ( civicrm_participant.event_id = civicrm_event.id )
  150. WHERE civicrm_participant.fee_amount IS NOT NULL OR
  151. civicrm_event.is_monetary = 1";
  152. $participant = CRM_Core_DAO::executeQuery( $query );
  153. while ( $participant->fetch( ) ) {
  154. $query = "
  155. SELECT civicrm_contribution.currency
  156. FROM civicrm_contribution,
  157. civicrm_participant_payment
  158. WHERE civicrm_contribution.id = civicrm_participant_payment.contribution_id AND
  159. civicrm_participant_payment.participant_id = {$participant->id}";
  160. $currencyID = CRM_Core_DAO::singleValueQuery( $query );
  161. if ( !$currencyID ) {
  162. $config =& CRM_Core_Config::singleton( );
  163. $currencyID = $config->defaultCurrency;
  164. }
  165. //finally update participant record.
  166. CRM_Core_DAO::setFieldValue( 'CRM_Event_DAO_Participant', $participant->id, 'fee_currency', $currencyID );
  167. }
  168. //CRM-4575
  169. //check whether {contact.name} is set in mailing labels
  170. require_once 'CRM/Core/BAO/Preferences.php';
  171. $mailingFormat = CRM_Core_BAO_Preferences::value( 'mailing_format' );
  172. $addNewAddressee = true;
  173. if ( strpos($mailingFormat,'{contact.contact_name}') === false ) {
  174. $addNewAddressee = false;
  175. } else {
  176. //else compare individual name format with default individual addressee.
  177. $individualNameFormat = CRM_Core_BAO_Preferences::value( 'individual_name_format' );
  178. $defaultAddressee = CRM_Core_OptionGroup::values( 'addressee', false, false, false,
  179. " AND v.filter = 1 AND v.is_default = 1", 'label' );
  180. if ( array_search($individualNameFormat, $defaultAddressee) !== false ) {
  181. $addNewAddressee = false;
  182. }
  183. }
  184. require_once 'CRM/Utils/System.php';
  185. $docURL = CRM_Utils_System::docURL2( 'Update Greetings and Address Data for Contacts', false, null, null, 'color: white; text-decoration: underline;');
  186. if ( $addNewAddressee ) {
  187. //otherwise insert new token in addressee and set as a default
  188. $addresseeGroupId = CRM_Core_DAO::getFieldValue( 'CRM_Core_DAO_OptionGroup',
  189. 'addressee',
  190. 'id',
  191. 'name' );
  192. $optionValueParams = array( 'label' => $individualNameFormat,
  193. 'is_active' => 1,
  194. 'contactOptions' => 1,
  195. 'filter' => 1,
  196. 'defaultGreeting'=> 1,
  197. 'is_default' => 1
  198. );
  199. $action = CRM_Core_Action::ADD;
  200. $addresseeGroupParams = array( 'name' => 'addressee' );
  201. $fieldValues = array( 'option_group_id' => $addresseeGroupId );
  202. $weight = CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue', $fieldValues);
  203. $optionValueParams['weight'] = $weight;
  204. $addresseeTokne = CRM_Core_OptionValue::addOptionValue( $optionValueParams, $addresseeGroupParams,
  205. $action, $optionId = null );
  206. $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) ) );
  207. } else {
  208. $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 ) );
  209. }
  210. //replace contact.contact_name with contact.addressee in civicrm_preference.mailing_format
  211. $updateQuery = "
  212. UPDATE civicrm_preferences
  213. SET `mailing_format` =
  214. replace(`mailing_format`, '{contact.contact_name}','{contact.addressee}')";
  215. CRM_Core_DAO::executeQuery( $updateQuery );
  216. //drop column individual_name_format
  217. $alterQuery = "
  218. ALTER TABLE `civicrm_preferences`
  219. DROP `individual_name_format`";
  220. CRM_Core_DAO::executeQuery( $alterQuery );
  221. //set status message for default greetings
  222. $template = & CRM_Core_Smarty::singleton( );
  223. $template->assign('afterUpgradeMessage', $afterUpgradeMessage);
  224. }
  225. }