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