version fixes
[civicrm-core.git] / CRM / Upgrade / Incremental / php / FourSix.php
CommitLineData
d18df7f3 1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7.alpha1 |
d18df7f3 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
d18df7f3 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 |
c73475ea 12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
d18df7f3 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 |
c73475ea
WA
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
d18df7f3 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 +--------------------------------------------------------------------+
d25dd0ee 26 */
d18df7f3 27
28/**
bf6a5362 29 * Upgrade logic for 4.6
d18df7f3 30 */
bf6a5362 31class CRM_Upgrade_Incremental_php_FourSix extends CRM_Upgrade_Incremental_Base {
d18df7f3 32
33 /**
fe482240 34 * Compute any messages which should be displayed after upgrade.
d18df7f3 35 *
5a4f6742
CW
36 * @param string $postUpgradeMessage
37 * alterable.
38 * @param string $rev
39 * an intermediate version; note that setPostUpgradeMessage is called repeatedly with different $revs.
d18df7f3 40 * @return void
41 */
00be9182 42 public function setPostUpgradeMessage(&$postUpgradeMessage, $rev) {
e418776c 43 if ($rev == '4.6.alpha1') {
d18df7f3 44 $postUpgradeMessage .= '<br /><br />' . ts('Default versions of the following System Workflow Message Templates have been modified to handle new functionality: <ul><li>Events - Registration Confirmation and Receipt (on-line)</li><li>Events - Registration Confirmation and Receipt (off-line)</li><li>Contributions - Receipt (on-line)</li><li>Contributions - Receipt (off-line)</li><li>Memberships - Receipt (on-line)</li><li>Memberships - Signup and Renewal Receipts (off-line)</li></ul> If you have modified these templates, please review the new default versions and implement updates as needed to your copies (Administer > Communications > Message Templates > System Workflow Messages).');
353ffa53 45 }
f7bfe544
DG
46 if ($rev == '4.6.alpha3') {
47 $postUpgradeMessage .= '<br /><br />' . ts('A new permission has been added for editing message templates. Previously, users needed the "administer CiviCRM" permission. Now, users need the new permission called "edit message templates." Please check your CMS permissions to ensure that users who should be able to edit message templates are assigned this new permission.');
48 }
d18df7f3 49 }
50
2e2605fe 51 /**
aac793a0
CW
52 * CRM-16846 - This function incorrectly omits running the 4.6.alpha3 sql file.
53 *
54 * Instead of correcting it here (which would not run again for sites already on 4.6),
55 * the file is re-run conditionally during 4.6.6
56 * @see upgrade_4_6_6
2e2605fe
EM
57 *
58 * @param string $rev
59 */
96cca285 60 public function upgrade_4_6_alpha3($rev) {
2e2605fe 61 // Task to process sql.
d2868251 62 $this->addTask(ts('Add and update reference_date column for Schedule Reminders'), 'updateReferenceDate');
5c4d6559 63 }
64
2e2605fe
EM
65 /**
66 * Add new column reference_date to civicrm_action_log in order to track.
67 *
68 * CRM-15728, actual action_start_date for membership entity for only those schedule reminders which are not repeatable
69 *
70 * @param \CRM_Queue_TaskContext $ctx
71 *
72 * @return bool
73 */
96cca285
EM
74 public static function updateReferenceDate(CRM_Queue_TaskContext $ctx) {
75 //Add column civicrm_action_log.reference_date if not exists.
ef6c3df3 76 $sql = "SELECT count(*) FROM information_schema.columns WHERE table_schema = database() AND table_name = 'civicrm_action_log' AND COLUMN_NAME = 'reference_date' ";
77 $res = CRM_Core_DAO::singleValueQuery($sql);
78
79 if ($res <= 0) {
80 $query = "ALTER TABLE `civicrm_action_log`
81 ADD COLUMN `reference_date` date COMMENT 'Stores the date from the entity which triggered this reminder action (e.g. membership.end_date for most membership renewal reminders)'";
82 CRM_Core_DAO::executeQuery($query);
83 }
5c4d6559 84
96cca285 85 //Retrieve schedule reminders for membership entity and is not repeatable and no absolute date chosen.
5c4d6559 86 $query = "SELECT schedule.* FROM civicrm_action_schedule schedule
36e8f052 87 INNER JOIN civicrm_action_mapping mapper ON mapper.id = schedule.mapping_id AND
9c0fe051 88 mapper.entity = 'civicrm_membership' AND
89 schedule.is_repeat = 0 AND
90 schedule.start_action_date IS NOT NULL";
d2868251 91
92 // construct basic where clauses
93 $where = array(
96cca285 94 'reminder.action_date_time >= DATE_SUB(reminder.action_date_time, INTERVAL 9 MONTH)',
9c0fe051 95 ); //choose reminder older then 9 months
5c4d6559 96 $dao = CRM_Core_DAO::executeQuery($query);
96cca285 97 while ($dao->fetch()) {
5c4d6559 98
99 $referenceColumn = str_replace('membership_', "m.", $dao->start_action_date);
5c4d6559 100 $value = implode(', ', explode(CRM_Core_DAO::VALUE_SEPARATOR, trim($dao->entity_value, CRM_Core_DAO::VALUE_SEPARATOR)));
101 if (!empty($value)) {
102 $where[] = "m.membership_type_id IN ({$value})";
103 }
104 else {
105 $where[] = "m.membership_type_id IS NULL";
106 }
107
9c0fe051 108 //Create new action_log records where action_start_date changes and exclude reminders for additional contacts
109 //and select contacts are active
5c4d6559 110 $sql = "UPDATE civicrm_action_log reminder
9c0fe051 111 LEFT JOIN civicrm_membership m
112 ON reminder.entity_id = m.id AND
113 reminder.entity_table = 'civicrm_membership' AND
114 ( m.is_override IS NULL OR m.is_override = 0 )
115 INNER JOIN civicrm_contact c
5a5f7454 116 ON c.id = m.contact_id AND
9c0fe051 117 c.is_deleted = 0 AND c.is_deceased = 0
5c4d6559 118 SET reminder.reference_date = {$referenceColumn}
5c4d6559 119 WHERE " . implode(" AND ", $where);
120 CRM_Core_DAO::executeQuery($sql);
121 }
122
123 return TRUE;
124 }
96cca285 125
c348f26d
DG
126 /**
127 * Upgrade function.
128 *
129 * @param string $rev
130 */
131 public function upgrade_4_6_1($rev) {
132 // CRM-16289 - Fix invalid data in log_civicrm_case.case_type_id.
133 $this->addTask(ts('Cleanup case type id data in log table.'), 'fixCaseLog');
134 }
386597e2 135
aac793a0
CW
136 /**
137 * Upgrade function.
138 *
139 * @param string $rev
140 */
141 public function upgrade_4_6_6($rev) {
142 // CRM-16846 - This sql file may have been previously skipped. Conditionally run it again if it doesn't appear to have run before.
143 if (!CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_state_province WHERE abbreviation = '100' AND country_id = 1193")) {
bf6a5362 144 $this->addTask('Update Slovenian municipalities', 'runSql', '4.6.alpha3');
aac793a0
CW
145 }
146 // CRM-16846 - This sql file may have been previously skipped. No harm in running it again because it's just UPDATE statements.
bf6a5362 147 $this->addTask('State-province update from 4.4.7', 'runSql', '4.4.7');
aac793a0 148
bf6a5362 149 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
aac793a0
CW
150 }
151
386597e2 152 /**
394851c5
DG
153 * Remove special characters from case_type_id column in log_civicrm_case.
154 *
155 * CRM-16289 - If logging enabled and upgrading from 4.4 or earlier, log_civicrm_case.case_type_id will contain special characters.
156 * This will cause ALTER TABLE to fail when changing this column to an INT
157 *
158 * @param \CRM_Queue_TaskContext $ctx
159 *
160 * @return bool
161 */
c348f26d
DG
162 public static function fixCaseLog(CRM_Queue_TaskContext $ctx) {
163 $sql = "SELECT count(*) FROM information_schema.columns WHERE table_schema = database() AND table_name = 'log_civicrm_case'";
164 $res = CRM_Core_DAO::singleValueQuery($sql);
165
166 if ($res) {
386597e2 167 // executeQuery doesn't like running multiple engine changes in one pass, so have to break it up. dgg
c348f26d
DG
168 $query = "ALTER TABLE `log_civicrm_case` ENGINE = InnoDB;";
169 CRM_Core_DAO::executeQuery($query);
170 $query = "UPDATE log_civicrm_case SET case_type_id = replace(case_type_id, 0x01, '');";
171 CRM_Core_DAO::executeQuery($query);
172 $query = "ALTER TABLE `log_civicrm_case` ENGINE = ARCHIVE;";
173 CRM_Core_DAO::executeQuery($query);
174 $query = "ALTER TABLE log_civicrm_case MODIFY `case_type_id` int(10) unsigned DEFAULT NULL COMMENT 'FK to civicrm_case_type.id';";
386597e2 175 CRM_Core_DAO::executeQuery($query);
c348f26d
DG
176 }
177 return TRUE;
178 }
386597e2 179
c4fc1d50
CW
180 /**
181 * Queue Task Callback for CRM-16846
182 *
183 * Run a sql file without resetting locale to that version
184 */
185 public static function task_4_6_x_runOnlySql(CRM_Queue_TaskContext $ctx, $rev) {
186 $upgrade = new CRM_Upgrade_Form();
187 $smarty = CRM_Core_Smarty::singleton();
188 $smarty->assign('domainID', CRM_Core_Config::domainID());
189
77fb8212 190 $fileName = dirname(__DIR__) . "/sql/$rev.mysql.tpl";
c4fc1d50
CW
191
192 $upgrade->source($smarty->fetch($fileName), TRUE);
193
194 return TRUE;
195 }
386597e2 196
d18df7f3 197}