Merge remote-tracking branch 'upstream/4.6' into 4.6-master-2015-06-08-21-41-06
[civicrm-core.git] / CRM / Upgrade / Incremental / php / ThreeThree.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
6a488035
TO
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. |
6a488035
TO
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 |
6a488035
TO
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 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
32 * $Id$
33 *
34 */
35class CRM_Upgrade_Incremental_php_ThreeThree {
624e56fa
EM
36 /**
37 * @param $errors
38 *
39 * @return bool
40 */
00be9182 41 public function verifyPreDBstate(&$errors) {
6a488035
TO
42 return TRUE;
43 }
44
624e56fa
EM
45 /**
46 * @param $rev
47 */
00be9182 48 public function upgrade_3_3_alpha1($rev) {
6a488035
TO
49 $config = CRM_Core_Config::singleton();
50 if ($config->userSystem->is_drupal) {
51 // CRM-6426 - make civicrm profiles permissioned on drupal my account
52 $config->userSystem->updateCategories();
53 }
54
55 // CRM-6846
56 // insert name column for custom field table.
57 // make sure name for custom field, group and
58 // profile should be unique and properly munged.
59 $colQuery = 'ALTER TABLE `civicrm_custom_field` ADD `name` VARCHAR( 64 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL AFTER `custom_group_id` ';
60 CRM_Core_DAO::executeQuery($colQuery, CRM_Core_DAO::$_nullArray, TRUE, NULL, FALSE, FALSE);
61
62 $customFldCntQuery = 'select count(*) from civicrm_custom_field where name like %1 and id != %2';
63 $customField = new CRM_Core_DAO_CustomField();
64 $customField->selectAdd();
65 $customField->selectAdd('id, label');
66 $customField->find();
67 while ($customField->fetch()) {
68 $name = CRM_Utils_String::munge($customField->label, '_', 64);
69 $fldCnt = CRM_Core_DAO::singleValueQuery($customFldCntQuery,
e418776c 70 array(
353ffa53 71 1 => array($name, 'String'),
6a488035
TO
72 2 => array($customField->id, 'Integer'),
73 ), TRUE, FALSE
74 );
75 if ($fldCnt) {
76 $name = CRM_Utils_String::munge("{$name}_" . rand(), '_', 64);
77 }
78 $customFieldQuery = "
79Update `civicrm_custom_field`
80SET `name` = %1
81WHERE id = %2
82";
e418776c 83 $customFieldParams = array(
353ffa53 84 1 => array($name, 'String'),
6a488035
TO
85 2 => array($customField->id, 'Integer'),
86 );
87 CRM_Core_DAO::executeQuery($customFieldQuery, $customFieldParams, TRUE, NULL, FALSE, FALSE);
88 }
89 $customField->free();
90
91 $customGrpCntQuery = 'select count(*) from civicrm_custom_group where name like %1 and id != %2';
92 $customGroup = new CRM_Core_DAO_CustomGroup();
93 $customGroup->selectAdd();
94 $customGroup->selectAdd('id, title');
95 $customGroup->find();
96 while ($customGroup->fetch()) {
97 $name = CRM_Utils_String::munge($customGroup->title, '_', 64);
98 $grpCnt = CRM_Core_DAO::singleValueQuery($customGrpCntQuery,
e418776c 99 array(
353ffa53 100 1 => array($name, 'String'),
6a488035
TO
101 2 => array($customGroup->id, 'Integer'),
102 )
103 );
104 if ($grpCnt) {
105 $name = CRM_Utils_String::munge("{$name}_" . rand(), '_', 64);
106 }
107 CRM_Core_DAO::setFieldValue('CRM_Core_DAO_CustomGroup', $customGroup->id, 'name', $name);
108 }
109 $customGroup->free();
110
111 $ufGrpCntQuery = 'select count(*) from civicrm_uf_group where name like %1 and id != %2';
112 $ufGroup = new CRM_Core_DAO_UFGroup();
113 $ufGroup->selectAdd();
114 $ufGroup->selectAdd('id, title');
115 $ufGroup->find();
116 while ($ufGroup->fetch()) {
117 $name = CRM_Utils_String::munge($ufGroup->title, '_', 64);
118 $ufGrpCnt = CRM_Core_DAO::singleValueQuery($ufGrpCntQuery,
e418776c 119 array(
353ffa53 120 1 => array($name, 'String'),
6a488035
TO
121 2 => array($ufGroup->id, 'Integer'),
122 )
123 );
124 if ($ufGrpCnt) {
125 $name = CRM_Utils_String::munge("{$name}_" . rand(), '_', 64);
126 }
127 CRM_Core_DAO::setFieldValue('CRM_Core_DAO_UFGroup', $ufGroup->id, 'name', $name);
128 }
129 $ufGroup->free();
130
131 $upgrade = new CRM_Upgrade_Form();
132 $upgrade->processSQL($rev);
133
134 // now modify the config so that the directories are stored in option group/value
135 // CRM-6914
136 // require_once 'CRM/Core/BAO/ConfigSetting.php';
137 // $params = array( );
138 // CRM_Core_BAO_ConfigSetting::add( $parambs );
139 }
140
624e56fa
EM
141 /**
142 * @param $rev
143 */
00be9182 144 public function upgrade_3_3_beta1($rev) {
6a488035
TO
145 $upgrade = new CRM_Upgrade_Form();
146 $upgrade->processSQL($rev);
147
148 // CRM-6902
149 // Add column price_field_value_id in civicrm_line_item.
150 // Do not drop option_group_id column now since we need it to
151 // update line items.
152 $updateLineItem1 = "ALTER TABLE civicrm_line_item ADD COLUMN price_field_value_id int(10) unsigned default NULL;";
153 CRM_Core_DAO::executeQuery($updateLineItem1);
154
9da8dc8c 155 $priceFieldDAO = new CRM_Price_DAO_PriceField();
6a488035
TO
156 $priceFieldDAO->find();
157 $ids = array();
158 while ($priceFieldDAO->fetch()) {
159
160 $opGroupDAO = new CRM_Core_DAO_OptionGroup();
161 $opGroupDAO->name = 'civicrm_price_field.amount.' . $priceFieldDAO->id;
162
163 if (!$opGroupDAO->find(TRUE)) {
164 $opGroupDAO->free();
165 continue;
166 }
167
168 $opValueDAO = new CRM_Core_DAO_OptionValue();
169 $opValueDAO->option_group_id = $opGroupDAO->id;
170 $opValueDAO->find();
171
172 while ($opValueDAO->fetch()) {
173 // FIX ME: not migrating description(?), there will
174 // be a field description for each option.
175 $fieldValue = array(
176 'price_field_id' => $priceFieldDAO->id,
177 'label' => $opValueDAO->label,
178 'name' => CRM_Utils_String::munge($opValueDAO->label, '_', 64),
179 'amount' => $opValueDAO->name,
180 'weight' => $opValueDAO->weight,
181 'is_default' => $opValueDAO->is_default,
182 'is_active' => $opValueDAO->is_active,
183 );
184
185 if ($priceFieldDAO->count) {
186 // Migrate Participant Counts on option level.
187 // count of each option will be the same
188 // as earlier field count.
189 $fieldValue['count'] = $priceFieldDAO->count;
190 }
191
9da8dc8c 192 $fieldValueDAO = CRM_Price_BAO_PriceFieldValue::add($fieldValue, $ids);
6a488035
TO
193
194 $lineItemDAO = new CRM_Price_DAO_LineItem();
195 $lineItemDAO->option_group_id = $opGroupDAO->id;
196 $lineItemDAO->label = $opValueDAO->label;
197 $lineItemDAO->unit_price = $opValueDAO->name;
198
199 $labelFound = $priceFound = FALSE;
200
201 // check with label and amount
202 if (!$lineItemDAO->find(TRUE)) {
203 $lineItemDAO->free();
204 $lineItemDAO = new CRM_Price_DAO_LineItem();
205 $lineItemDAO->option_group_id = $opGroupDAO->id;
206 $lineItemDAO->label = $opValueDAO->label;
207
208 // check with label only
209 if ($lineItemDAO->find(TRUE)) {
210 $labelFound = TRUE;
211 }
212 }
213 else {
214 $labelFound = TRUE;
215 $priceFound = TRUE;
216 }
217
218 $lineItemDAO->free();
219
220 // update civicrm_line_item for price_field_value_id.
221 // Used query to avoid line by line update.
222 if ($labelFound || $priceFound) {
e418776c 223 $lineItemParams = array(
353ffa53 224 1 => array($fieldValueDAO->id, 'Integer'),
6a488035
TO
225 2 => array($opValueDAO->label, 'String'),
226 );
227 $updateLineItems = "UPDATE civicrm_line_item SET price_field_value_id = %1 WHERE label = %2";
228 if ($priceFound) {
229 $lineItemParams[3] = array($opValueDAO->name, 'Float');
230 $updateLineItems .= " AND unit_price = %3";
231 }
232 CRM_Core_DAO::executeQuery($updateLineItems, $lineItemParams);
233 }
234 }
235
236 $opGroupDAO->delete();
237 $opValueDAO->free();
238 $opGroupDAO->free();
239 }
240
241 $priceFieldDAO->free();
242
243 // Now drop option_group_id column from civicrm_line_item
244 $updateLineItem2 = "ALTER TABLE civicrm_line_item DROP option_group_id,
245 ADD CONSTRAINT `FK_civicrm_price_field_value_id` FOREIGN KEY (price_field_value_id) REFERENCES civicrm_price_field_value(id) ON DELETE SET NULL;";
246 CRM_Core_DAO::executeQuery($updateLineItem2, array(), TRUE, NULL, FALSE, FALSE);
247
248 $updatePriceField = "ALTER TABLE civicrm_price_field DROP count";
249 CRM_Core_DAO::executeQuery($updatePriceField, array(), TRUE, NULL, FALSE, FALSE);
250
251 // as the table 'civicrm_price_field' is localised and column 'count' is dropped
b44e3f84 252 // after the views are rebuild, we need to rebuild views to avoid invalid reference of table.
6a488035
TO
253 if ($upgrade->multilingual) {
254 CRM_Core_I18n_Schema::rebuildMultilingualSchema($upgrade->locales, $rev);
255 }
256 }
257
624e56fa
EM
258 /**
259 * @param $rev
260 */
00be9182 261 public function upgrade_3_3_beta3($rev) {
6a488035
TO
262 // get the duplicate Ids of line item entries
263 $dupeLineItemIds = array();
353ffa53
TO
264 $fields = array('entity_table', 'entity_id', 'price_field_id', 'price_field_value_id');
265 $mainLineItem = new CRM_Price_BAO_LineItem();
6a488035
TO
266 $mainLineItem->find(TRUE);
267 while ($mainLineItem->fetch()) {
268 $dupeLineItem = new CRM_Price_BAO_LineItem();
4e66d748
TO
269 foreach ($fields as $fld) {
270 $dupeLineItem->$fld = $mainLineItem->$fld;
e418776c 271 }
6a488035
TO
272 $dupeLineItem->find(TRUE);
273 $dupeLineItem->addWhere("id != $mainLineItem->id");
274 while ($dupeLineItem->fetch()) {
275 $dupeLineItemIds[$dupeLineItem->id] = $dupeLineItem->id;
276 }
277 $dupeLineItem->free();
278 }
279 $mainLineItem->free();
280
281 //clean line item table.
282 if (!empty($dupeLineItemIds)) {
283 $sql = 'DELETE FROM civicrm_line_item WHERE id IN ( ' . implode(', ', $dupeLineItemIds) . ' )';
284 CRM_Core_DAO::executeQuery($sql);
285 }
286
287 $upgrade = new CRM_Upgrade_Form();
288 $upgrade->processSQL($rev);
289 }
290
624e56fa
EM
291 /**
292 * @param $rev
293 */
00be9182 294 public function upgrade_3_3_0($rev) {
6a488035
TO
295 $upgrade = new CRM_Upgrade_Form();
296 $upgrade->processSQL($rev);
297
298 //CRM-7123 -lets activate needful languages.
299 $config = CRM_Core_Config::singleton();
300 $locales = array();
948d11bf 301 if (is_dir($config->gettextResourceDir) && $dir = opendir($config->gettextResourceDir)) {
6a488035
TO
302 while ($filename = readdir($dir)) {
303 if (preg_match('/^[a-z][a-z]_[A-Z][A-Z]$/', $filename)) {
304 $locales[$filename] = $filename;
305 }
306 }
307 closedir($dir);
308 }
309
310 if (isset($config->languageLimit) && !empty($config->languageLimit)) {
311 //get all already enabled and all l10n languages.
312 $locales = array_merge(array_values($locales), array_keys($config->languageLimit));
313 }
314
315 if (!empty($locales)) {
316 $sql = '
317 UPDATE civicrm_option_value val
318INNER JOIN civicrm_option_group grp ON ( grp.id = val.option_group_id )
319 SET val.is_active = 1
320 WHERE grp.name = %1
321 AND val.name IN ( ' . "'" . implode("', '", $locales) . "' )";
322
323 CRM_Core_DAO::executeQuery($sql,
324 array(1 => array('languages', 'String')),
325 TRUE, NULL, FALSE, FALSE
326 );
327 }
328 }
329
624e56fa
EM
330 /**
331 * @param $rev
332 */
00be9182 333 public function upgrade_3_3_2($rev) {
6a488035
TO
334 $dropMailingIndex = FALSE;
335 $indexes = CRM_Core_DAO::executeQuery('SHOW INDEXES FROM civicrm_mailing_job');
336 while ($indexes->fetch()) {
337 if ($indexes->Key_name == 'parent_id') {
338 $dropMailingIndex = TRUE;
339 break;
340 }
341 }
342 //CRM-7137
343 // get membership type for each membership block.
344 $sql = "SELECT id, membership_types FROM civicrm_membership_block ";
345 $dao = CRM_Core_DAO::executeQuery($sql);
346 while ($dao->fetch()) {
347 $memType = explode(',', $dao->membership_types);
348
349 $memTypeSerialize = array();
350 foreach ($memType as $k => $v) {
351 $memTypeSerialize[$v] = 0;
352 }
353
354 // save membership type as an serialized array along w/ auto_renew defalt value zero.
355 $memBlock = new CRM_Member_DAO_MembershipBlock();
356 $memBlock->id = $dao->id;
357 $memBlock->membership_types = serialize($memTypeSerialize);
358 $memBlock->save();
359 }
360
361 //CRM-7172
362 if (CRM_Mailing_Info::workflowEnabled()) {
363 $config = CRM_Core_Config::singleton();
364 if (is_callable(array(
353ffa53 365 $config->userSystem,
bed98343 366 'replacePermission',
353ffa53
TO
367 ))) {
368 $config->userSystem->replacePermission('access CiviMail', array(
369 'access CiviMail',
370 'create mailings',
371 'approve mailings',
bed98343 372 'schedule mailings',
353ffa53 373 ));
6a488035
TO
374 }
375 }
376
377 $upgrade = new CRM_Upgrade_Form();
378 $upgrade->assign('dropMailingIndex', $dropMailingIndex);
379 $upgrade->processSQL($rev);
380 }
381
624e56fa
EM
382 /**
383 * @param $rev
384 */
00be9182 385 public function upgrade_3_3_7($rev) {
6a488035
TO
386 $dao = new CRM_Contact_DAO_Contact();
387 $dbName = $dao->_database;
388
389 $chkExtQuery = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = %1
390 AND TABLE_NAME = 'civicrm_phone' AND COLUMN_NAME = 'phone_ext'";
391 $extensionExists = CRM_Core_DAO::singleValueQuery($chkExtQuery,
392 array(1 => array($dbName, 'String')),
393 TRUE, FALSE
394 );
395
396 if (!$extensionExists) {
397 $colQuery = 'ALTER TABLE `civicrm_phone` ADD `phone_ext` VARCHAR( 16 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL AFTER `phone` ';
398 CRM_Core_DAO::executeQuery($colQuery);
399 }
400
401 // handle db changes done for CRM-8218
402 $alterContactDashboard = FALSE;
403 $dao = new CRM_Contact_DAO_DashboardContact();
404 $dbName = $dao->_database;
405
406 $chkContentQuery = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = %1
407 AND TABLE_NAME = 'civicrm_dashboard_contact' AND COLUMN_NAME = 'content'";
408 $contentExists = CRM_Core_DAO::singleValueQuery($chkContentQuery,
409 array(1 => array($dbName, 'String')),
410 TRUE, FALSE
411 );
412 if (!$contentExists) {
413 $alterContactDashboard = TRUE;
414 }
415
416 $upgrade = new CRM_Upgrade_Form();
417 $upgrade->assign('alterContactDashboard', $alterContactDashboard);
418 $upgrade->processSQL($rev);
419 }
96025800 420
6a488035 421}