Merge pull request #6371 from colemanw/CRM-16891
[civicrm-core.git] / CRM / Upgrade / Incremental / php / FourSix.php
CommitLineData
d18df7f3 1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6.alpha1 |
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/**
29 *
30 * @package CRM
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
d18df7f3 32 * $Id$
d18df7f3 33 */
34class CRM_Upgrade_Incremental_php_FourSix {
35 const BATCH_SIZE = 5000;
36
37 /**
96cca285
EM
38 * Verify DB state.
39 *
d18df7f3 40 * @param $errors
41 *
42 * @return bool
43 */
00be9182 44 public function verifyPreDBstate(&$errors) {
d18df7f3 45 return TRUE;
46 }
47
48 /**
96cca285 49 * Compute any messages which should be displayed before upgrade.
d18df7f3 50 *
51 * Note: This function is called iteratively for each upcoming
52 * revision to the database.
53 *
54 * @param $preUpgradeMessage
5a4f6742
CW
55 * @param string $rev
56 * a version number, e.g. '4.4.alpha1', '4.4.beta3', '4.4.0'.
d18df7f3 57 * @param null $currentVer
d18df7f3 58 */
00be9182 59 public function setPreUpgradeMessage(&$preUpgradeMessage, $rev, $currentVer = NULL) {
d18df7f3 60 }
61
62 /**
fe482240 63 * Compute any messages which should be displayed after upgrade.
d18df7f3 64 *
5a4f6742
CW
65 * @param string $postUpgradeMessage
66 * alterable.
67 * @param string $rev
68 * an intermediate version; note that setPostUpgradeMessage is called repeatedly with different $revs.
d18df7f3 69 * @return void
70 */
00be9182 71 public function setPostUpgradeMessage(&$postUpgradeMessage, $rev) {
e418776c 72 if ($rev == '4.6.alpha1') {
d18df7f3 73 $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 74 }
f7bfe544
DG
75 if ($rev == '4.6.alpha3') {
76 $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.');
77 }
d18df7f3 78 }
79
80
d18df7f3 81 /**
82 * (Queue Task Callback)
83 */
00be9182 84 public static function task_4_6_x_runSql(CRM_Queue_TaskContext $ctx, $rev) {
d18df7f3 85 $upgrade = new CRM_Upgrade_Form();
86 $upgrade->processSQL($rev);
87
88 return TRUE;
89 }
90
91 /**
92 * Syntactic sugar for adding a task which (a) is in this class and (b) has
93 * a high priority.
94 *
95 * After passing the $funcName, you can also pass parameters that will go to
96 * the function. Note that all params must be serializable.
97 */
98 protected function addTask($title, $funcName) {
99 $queue = CRM_Queue_Service::singleton()->load(array(
100 'type' => 'Sql',
101 'name' => CRM_Upgrade_Form::QUEUE_NAME,
102 ));
103
104 $args = func_get_args();
105 $title = array_shift($args);
106 $funcName = array_shift($args);
107 $task = new CRM_Queue_Task(
108 array(get_class($this), $funcName),
109 $args,
110 $title
111 );
112 $queue->createItem($task, array('weight' => -1));
113 }
96025800 114
2e2605fe 115 /**
aac793a0
CW
116 * CRM-16846 - This function incorrectly omits running the 4.6.alpha3 sql file.
117 *
118 * Instead of correcting it here (which would not run again for sites already on 4.6),
119 * the file is re-run conditionally during 4.6.6
120 * @see upgrade_4_6_6
2e2605fe
EM
121 *
122 * @param string $rev
123 */
96cca285 124 public function upgrade_4_6_alpha3($rev) {
2e2605fe 125 // Task to process sql.
d2868251 126 $this->addTask(ts('Add and update reference_date column for Schedule Reminders'), 'updateReferenceDate');
5c4d6559 127 }
128
2e2605fe
EM
129 /**
130 * Add new column reference_date to civicrm_action_log in order to track.
131 *
132 * CRM-15728, actual action_start_date for membership entity for only those schedule reminders which are not repeatable
133 *
134 * @param \CRM_Queue_TaskContext $ctx
135 *
136 * @return bool
137 */
96cca285
EM
138 public static function updateReferenceDate(CRM_Queue_TaskContext $ctx) {
139 //Add column civicrm_action_log.reference_date if not exists.
ef6c3df3 140 $sql = "SELECT count(*) FROM information_schema.columns WHERE table_schema = database() AND table_name = 'civicrm_action_log' AND COLUMN_NAME = 'reference_date' ";
141 $res = CRM_Core_DAO::singleValueQuery($sql);
142
143 if ($res <= 0) {
144 $query = "ALTER TABLE `civicrm_action_log`
145 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)'";
146 CRM_Core_DAO::executeQuery($query);
147 }
5c4d6559 148
96cca285 149 //Retrieve schedule reminders for membership entity and is not repeatable and no absolute date chosen.
5c4d6559 150 $query = "SELECT schedule.* FROM civicrm_action_schedule schedule
36e8f052 151 INNER JOIN civicrm_action_mapping mapper ON mapper.id = schedule.mapping_id AND
9c0fe051 152 mapper.entity = 'civicrm_membership' AND
153 schedule.is_repeat = 0 AND
154 schedule.start_action_date IS NOT NULL";
d2868251 155
156 // construct basic where clauses
157 $where = array(
96cca285 158 'reminder.action_date_time >= DATE_SUB(reminder.action_date_time, INTERVAL 9 MONTH)',
9c0fe051 159 ); //choose reminder older then 9 months
5c4d6559 160 $dao = CRM_Core_DAO::executeQuery($query);
96cca285 161 while ($dao->fetch()) {
5c4d6559 162
163 $referenceColumn = str_replace('membership_', "m.", $dao->start_action_date);
5c4d6559 164 $value = implode(', ', explode(CRM_Core_DAO::VALUE_SEPARATOR, trim($dao->entity_value, CRM_Core_DAO::VALUE_SEPARATOR)));
165 if (!empty($value)) {
166 $where[] = "m.membership_type_id IN ({$value})";
167 }
168 else {
169 $where[] = "m.membership_type_id IS NULL";
170 }
171
9c0fe051 172 //Create new action_log records where action_start_date changes and exclude reminders for additional contacts
173 //and select contacts are active
5c4d6559 174 $sql = "UPDATE civicrm_action_log reminder
9c0fe051 175 LEFT JOIN civicrm_membership m
176 ON reminder.entity_id = m.id AND
177 reminder.entity_table = 'civicrm_membership' AND
178 ( m.is_override IS NULL OR m.is_override = 0 )
179 INNER JOIN civicrm_contact c
5a5f7454 180 ON c.id = m.contact_id AND
9c0fe051 181 c.is_deleted = 0 AND c.is_deceased = 0
5c4d6559 182 SET reminder.reference_date = {$referenceColumn}
5c4d6559 183 WHERE " . implode(" AND ", $where);
184 CRM_Core_DAO::executeQuery($sql);
185 }
186
187 return TRUE;
188 }
96cca285 189
c348f26d
DG
190 /**
191 * Upgrade function.
192 *
193 * @param string $rev
194 */
195 public function upgrade_4_6_1($rev) {
196 // CRM-16289 - Fix invalid data in log_civicrm_case.case_type_id.
197 $this->addTask(ts('Cleanup case type id data in log table.'), 'fixCaseLog');
198 }
386597e2 199
aac793a0
CW
200 /**
201 * Upgrade function.
202 *
203 * @param string $rev
204 */
205 public function upgrade_4_6_6($rev) {
206 // CRM-16846 - This sql file may have been previously skipped. Conditionally run it again if it doesn't appear to have run before.
207 if (!CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_state_province WHERE abbreviation = '100' AND country_id = 1193")) {
208 $this->addTask('Update Slovenian municipalities', 'task_4_6_x_runSql', '4.6.alpha3');
209 }
210 // CRM-16846 - This sql file may have been previously skipped. No harm in running it again because it's just UPDATE statements.
211 $this->addTask('State-province update from 4.4.7', 'task_4_6_x_runSql', '4.4.7');
212
213 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'task_4_6_x_runSql', $rev);
214 }
215
386597e2 216 /**
394851c5
DG
217 * Remove special characters from case_type_id column in log_civicrm_case.
218 *
219 * CRM-16289 - If logging enabled and upgrading from 4.4 or earlier, log_civicrm_case.case_type_id will contain special characters.
220 * This will cause ALTER TABLE to fail when changing this column to an INT
221 *
222 * @param \CRM_Queue_TaskContext $ctx
223 *
224 * @return bool
225 */
c348f26d
DG
226 public static function fixCaseLog(CRM_Queue_TaskContext $ctx) {
227 $sql = "SELECT count(*) FROM information_schema.columns WHERE table_schema = database() AND table_name = 'log_civicrm_case'";
228 $res = CRM_Core_DAO::singleValueQuery($sql);
229
230 if ($res) {
386597e2 231 // executeQuery doesn't like running multiple engine changes in one pass, so have to break it up. dgg
c348f26d
DG
232 $query = "ALTER TABLE `log_civicrm_case` ENGINE = InnoDB;";
233 CRM_Core_DAO::executeQuery($query);
234 $query = "UPDATE log_civicrm_case SET case_type_id = replace(case_type_id, 0x01, '');";
235 CRM_Core_DAO::executeQuery($query);
236 $query = "ALTER TABLE `log_civicrm_case` ENGINE = ARCHIVE;";
237 CRM_Core_DAO::executeQuery($query);
238 $query = "ALTER TABLE log_civicrm_case MODIFY `case_type_id` int(10) unsigned DEFAULT NULL COMMENT 'FK to civicrm_case_type.id';";
386597e2 239 CRM_Core_DAO::executeQuery($query);
c348f26d
DG
240 }
241 return TRUE;
242 }
386597e2 243
d18df7f3 244}