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