fix CRM-12002
[civicrm-core.git] / CRM / Upgrade / Incremental / php / ThreeFour.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.3 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License along 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 */
35 class CRM_Upgrade_Incremental_php_ThreeFour {
36 function verifyPreDBstate(&$errors) {
37 return TRUE;
38 }
39
40 function upgrade_3_4_alpha3($rev) {
41 // CRM-7681, update report instance criteria.
42 $modifiedReportIds = array('contact/summary', 'contact/detail', 'event/participantListing', 'member/summary', 'pledge/summary', 'pledge/pbnp', 'member/detail', 'member/lapse', 'grant/detail', 'contribute/bookkeeping', 'contribute/lybunt', 'contribute/summary', 'contribute/repeat', 'contribute/detail', 'contribute/organizationSummary', 'contribute/sybunt', 'contribute/householdSummary', 'contact/relationship', 'contact/currentEmployer', 'case/demographics', 'walklist', 'case/detail', 'contact/log', 'activitySummary', 'case/timespent', 'case/summary');
43
44 $instances = CRM_Core_DAO::executeQuery("SELECT id, form_values, report_id FROM civicrm_report_instance WHERE report_id IN ('" . implode("','", $modifiedReportIds) . "')");
45
46 while ($instances->fetch()) {
47 $formValues = unserialize($instances->form_values);
48
49 // replace display_name fields by sort_name
50 if (CRM_Utils_Array::value('fields', $formValues) && isset($formValues['fields']['display_name'])) {
51 $formValues['fields']['sort_name'] = $formValues['fields']['display_name'];
52 unset($formValues['fields']['display_name']);
53 }
54
55 // replace display_name filters by sort_name
56 if (isset($formValues['display_name_op'])) {
57 $formValues['sort_name_op'] = $formValues['display_name_op'];
58 unset($formValues['display_name_op']);
59 }
60 if (isset($formValues['display_name_value'])) {
61 $formValues['sort_name_value'] = $formValues['display_name_value'];
62 unset($formValues['display_name_value']);
63 }
64
65 // for report id 'contact/log' replace field
66 // display_name_touched by sort_name_touched
67 if ($instances->report_id == 'contact/log' && isset($formValues['fields']['display_name_touched'])) {
68 $formValues['fields']['sort_name_touched'] = $formValues['fields']['display_name_touched'];
69 unset($formValues['fields']['display_name_touched']);
70 }
71
72 // for report id 'contact/relationship' replace field
73 // display_name_a by sort_name_a and display_name_b by sort_name_b
74 if ($instances->report_id == 'contact/relationship') {
75 if (isset($formValues['fields']['display_name_a'])) {
76 $formValues['fields']['sort_name_a'] = $formValues['fields']['display_name_a'];
77 unset($formValues['fields']['display_name_a']);
78 }
79
80 if (isset($formValues['fields']['display_name_b'])) {
81 $formValues['fields']['sort_name_b'] = $formValues['fields']['display_name_b'];
82 unset($formValues['fields']['display_name_b']);
83 }
84 }
85
86 // save updated instance criteria
87 $dao = new CRM_Report_DAO_Instance();
88 $dao->id = $instances->id;
89 $dao->form_values = serialize($formValues);
90 $dao->save();
91 $dao->free();
92 }
93
94 // Handled for typo in 3.3.2.mysql.tpl, rename column visibilty to
95 // visibility in table civicrm_mailing
96 $renameColumnVisibility = CRM_Core_DAO::checkFieldExists('civicrm_mailing', 'visibilty');
97
98 $upgrade = new CRM_Upgrade_Form();
99 $upgrade->assign('renameColumnVisibility', $renameColumnVisibility);
100 $upgrade->processSQL($rev);
101 }
102
103 function upgrade_3_4_beta2($rev) {
104 $addPetitionOptionGroup = !(boolean) CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'msg_tpl_workflow_petition', 'id', 'name');
105 $upgrade = new CRM_Upgrade_Form();
106 $upgrade->assign('addPetitionOptionGroup', $addPetitionOptionGroup);
107 $upgrade->processSQL($rev);
108 }
109
110 function upgrade_3_4_beta3($rev) {
111 // do the regular upgrade
112 $upgrade = new CRM_Upgrade_Form;
113 $upgrade->processSQL($rev);
114
115 if ($upgrade->multilingual) {
116
117 // rebuild schema, because due to a CRM-7854 mis-fix some indices might be missing
118 CRM_Core_I18n_Schema::rebuildMultilingualSchema($upgrade->locales, $rev);
119
120 // turn a set of columns singlelingual
121 $config = CRM_Core_Config::singleton();
122 $tables = array('civicrm_address', 'civicrm_contact', 'civicrm_mailing', 'civicrm_mailing_component');
123 $triggers = array(array('when' => 'before', 'event' => 'update'), array('when' => 'before', 'event' => 'insert'));
124
125 // FIXME: Doing require_once is a must here because a call like CRM_Core_I18n_SchemaStructure_3_4_beta2 makes
126 // class loader look for file like - CRM/Core/I18n/SchemaStructure/3/4/beta2.php which is not what we want to be loaded
127 require_once "CRM/Core/I18n/SchemaStructure_3_4_beta2.php";
128 foreach ($tables as $table) {
129 CRM_Core_I18n_Schema::makeSinglelingualTable($config->lcMessages, $table, 'CRM_Core_I18n_SchemaStructure_3_4_beta2', $triggers);
130 }
131 }
132 }
133
134 function upgrade_3_4_3($rev) {
135 // CRM-8147, update group_type for uf groups, check and add component field types
136 $ufGroups = new CRM_Core_DAO_UFGroup();
137 $ufGroups->find();
138 $skipGroupTypes = array('Individual,Contact', 'Organization,Contact', 'Household,Contact', 'Contact', 'Individual', 'Organization', 'Household');
139 while ($ufGroups->fetch()) {
140 if (!in_array($ufGroups->group_type, $skipGroupTypes)) {
141 $groupTypes = CRM_Core_BAO_UFGroup::calculateGroupType($ufGroups->id, TRUE);
142 CRM_Core_BAO_UFGroup::updateGroupTypes($ufGroups->id, $groupTypes);
143 }
144 }
145 $ufGroups->free();
146
147 // CRM-8134 add phone_ext column if it wasn't already added for this site in 3.3.7 upgrade (3.3.7 was released after 3.4.0)
148 $dao = new CRM_Contact_DAO_Contact();
149 $dbName = $dao->_database;
150
151 $chkExtQuery = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = %1
152 AND TABLE_NAME = 'civicrm_phone' AND COLUMN_NAME = 'phone_ext'";
153 $extensionExists = CRM_Core_DAO::singleValueQuery($chkExtQuery,
154 array(1 => array($dbName, 'String')),
155 TRUE, FALSE
156 );
157
158 if (!$extensionExists) {
159 $colQuery = 'ALTER TABLE `civicrm_phone` ADD `phone_ext` VARCHAR( 16 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL AFTER `phone` ';
160 CRM_Core_DAO::executeQuery($colQuery);
161 }
162
163 $sql = "SELECT id FROM civicrm_location_type WHERE name = 'Main'";
164 if (!CRM_Core_DAO::singleValueQuery($sql)) {
165 $query = "
166 INSERT INTO civicrm_location_type ( name, description, is_reserved, is_active )
167 VALUES ( 'Main', 'Main office location', 0, 1 );";
168 CRM_Core_DAO::executeQuery($query);
169 }
170
171 $upgrade = new CRM_Upgrade_Form;
172 $upgrade->processSQL($rev);
173 }
174
175 function upgrade_3_4_4($rev) {
176 // CRM-8315, update report instance criteria.
177 $modifiedReportIds = array('member/summary', 'member/detail');
178
179 $instances = CRM_Core_DAO::executeQuery("SELECT id, form_values, report_id FROM civicrm_report_instance WHERE report_id IN ('" . implode("','", $modifiedReportIds) . "')");
180
181 while ($instances->fetch()) {
182 $formValues = unserialize($instances->form_values);
183
184 // replace display_name fields by sort_name
185 if (!isset($formValues['membership_start_date_relative']) &&
186 !isset($formValues['membership_end_date_relative'])
187 ) {
188 $formValues['membership_start_date_relative'] = '0';
189 $formValues['membership_start_date_from'] = '';
190 $formValues['membership_start_date_to'] = '';
191 $formValues['membership_end_date_relative'] = '0';
192 $formValues['membership_end_date_from'] = '';
193 $formValues['membership_end_date_to'] = '';
194 }
195
196 // save updated instance criteria
197 $dao = new CRM_Report_DAO_Instance();
198 $dao->id = $instances->id;
199 $dao->form_values = serialize($formValues);
200 $dao->save();
201 $dao->free();
202 }
203
204 $upgrade = new CRM_Upgrade_Form();
205 $upgrade->processSQL($rev);
206 }
207
208 function upgrade_3_4_5($rev) {
209 // handle db changes done for CRM-8218
210 $alterContactDashboard = FALSE;
211 $dao = new CRM_Contact_DAO_DashboardContact();
212 $dbName = $dao->_database;
213
214 $chkContentQuery = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = %1
215 AND TABLE_NAME = 'civicrm_dashboard_contact' AND COLUMN_NAME = 'content'";
216 $contentExists = CRM_Core_DAO::singleValueQuery($chkContentQuery,
217 array(1 => array($dbName, 'String')),
218 TRUE, FALSE
219 );
220 if (!$contentExists) {
221 $alterContactDashboard = TRUE;
222 }
223
224 $upgrade = new CRM_Upgrade_Form();
225 $upgrade->assign('alterContactDashboard', $alterContactDashboard);
226 $upgrade->processSQL($rev);
227 }
228
229 function upgrade_3_4_6($rev) {
230 $modifiedReportIds = array('event/summary', 'activity', 'Mailing/bounce', 'Mailing/clicks', 'Mailing/opened');
231
232 $instances = CRM_Core_DAO::executeQuery("SELECT id, form_values, report_id FROM civicrm_report_instance WHERE report_id IN ('" . implode("','", $modifiedReportIds) . "')");
233 while ($instances->fetch()) {
234 $formValues = unserialize($instances->form_values);
235
236 switch ($instances->report_id) {
237 case 'event/summary':
238 $eventDates = array('event_start_date_from', 'event_start_date_to', 'event_end_date_from', 'event_end_date_to');
239 foreach ($eventDates as $date) {
240 if (isset($formValues[$date]) && $formValues[$date] == ' ') {
241 $formValues[$date] = '';
242 }
243 }
244 break;
245
246 case 'activity':
247 if (isset($formValues['group_bys'])) {
248 if (is_array($formValues['group_bys'])) {
249 $orderBy = array();
250 $count = 0;
251 foreach ($formValues['group_bys'] as $col => $isSet) {
252 if (!$isSet) {
253 continue;
254 }
255
256 $orderBy[++$count] = array(
257 'column' => $col,
258 'order' => 'ASC',
259 );
260 }
261 if (!empty($orderBy)) {
262 $formValues['order_bys'] = $orderBy;
263 }
264 }
265 unset($formValues['group_bys']);
266 }
267 break;
268
269 case 'Mailing/bounce':
270 case 'Mailing/clicks':
271 case 'Mailing/opened':
272 $formValues['fields']['mailing_name'] = 1;
273 break;
274 }
275
276 // save updated instance criteria
277 $dao = new CRM_Report_DAO_Instance();
278 $dao->id = $instances->id;
279 $dao->form_values = serialize($formValues);
280 $dao->save();
281 $dao->free();
282 }
283
284 $bulkEmailActivityType = CRM_Core_DAO::singleValueQuery("
285 SELECT v.id
286 FROM civicrm_option_value v,
287 civicrm_option_group g
288 WHERE v.option_group_id = g.id
289 AND g.name = %1
290 AND g.is_active = 1
291 AND v.name = %2", array(1 => array('activity_type', 'String'),
292 2 => array('Bulk Email', 'String'),
293 ));
294
295 // CRM-8852, reset contact field cache
296 CRM_Core_BAO_Cache::deleteGroup('contact fields');
297
298 $upgrade = new CRM_Upgrade_Form();
299 $upgrade->assign('bulkEmailActivityType', $bulkEmailActivityType);
300
301 $upgrade->processSQL($rev);
302 }
303
304 function upgrade_3_4_7($rev) {
305 $onBehalfProfileId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', 'on_behalf_organization', 'id', 'name');
306 if (!$onBehalfProfileId) {
307 CRM_Core_Error::fatal();
308 }
309
310 $pages = CRM_Core_DAO::executeQuery("
311 SELECT civicrm_contribution_page.id
312 FROM civicrm_contribution_page
313 LEFT JOIN civicrm_uf_join ON entity_table = 'civicrm_contribution_page' AND entity_id = civicrm_contribution_page.id AND module = 'OnBehalf'
314 WHERE is_for_organization = 1
315 AND civicrm_uf_join.id IS NULL
316 ");
317
318 while ($pages->fetch()) {
319 $query = "
320 INSERT INTO civicrm_uf_join
321 (is_active, module, entity_table, entity_id, weight, uf_group_id)
322 VALUES
323 (1, 'OnBehalf', 'civicrm_contribution_page', %1, 1, %2)";
324
325 $params = array(1 => array($pages->id, 'Integer'),
326 2 => array($onBehalfProfileId, 'Integer'),
327 );
328 CRM_Core_DAO::executeQuery($query, $params);
329 }
330
331 // CRM-8774
332 $config = CRM_Core_Config::singleton();
333 if ($config->userFramework == 'Drupal' || $config->userFramework == 'Drupal6') {
334 db_query("UPDATE {system} SET weight = 100 WHERE name = 'civicrm'");
335 drupal_flush_all_caches();
336 }
337
338 $upgrade = new CRM_Upgrade_Form();
339 $upgrade->processSQL($rev);
340 }
341 }
342