INFRA-132 - CRM/Upgrade - phpcbf (plus fixup)
[civicrm-core.git] / CRM / Upgrade / Incremental / php / ThreeThree.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.6 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
32 * $Id$
33 *
34 */
35 class CRM_Upgrade_Incremental_php_ThreeThree {
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_3_alpha1($rev) {
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,
70 array(
71 1 => array($name, 'String'),
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 = "
79 Update `civicrm_custom_field`
80 SET `name` = %1
81 WHERE id = %2
82 ";
83 $customFieldParams = array(
84 1 => array($name, 'String'),
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,
99 array(
100 1 => array($name, 'String'),
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,
119 array(
120 1 => array($name, 'String'),
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
141 /**
142 * @param $rev
143 */
144 public function upgrade_3_3_beta1($rev) {
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
155 $priceFieldDAO = new CRM_Price_DAO_PriceField();
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
192 $fieldValueDAO = CRM_Price_BAO_PriceFieldValue::add($fieldValue, $ids);
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) {
223 $lineItemParams = array(
224 1 => array($fieldValueDAO->id, 'Integer'),
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
252 // after the views are rebuild, we need to rebuild views to avoid invalid refrence of table.
253 if ($upgrade->multilingual) {
254 CRM_Core_I18n_Schema::rebuildMultilingualSchema($upgrade->locales, $rev);
255 }
256 }
257
258 /**
259 * @param $rev
260 */
261 public function upgrade_3_3_beta3($rev) {
262 // get the duplicate Ids of line item entries
263 $dupeLineItemIds = array();
264 $fields = array('entity_table', 'entity_id', 'price_field_id', 'price_field_value_id');
265 $mainLineItem = new CRM_Price_BAO_LineItem();
266 $mainLineItem->find(TRUE);
267 while ($mainLineItem->fetch()) {
268 $dupeLineItem = new CRM_Price_BAO_LineItem();
269 foreach ($fields as $fld) { $dupeLineItem->$fld = $mainLineItem->$fld;
270 }
271 $dupeLineItem->find(TRUE);
272 $dupeLineItem->addWhere("id != $mainLineItem->id");
273 while ($dupeLineItem->fetch()) {
274 $dupeLineItemIds[$dupeLineItem->id] = $dupeLineItem->id;
275 }
276 $dupeLineItem->free();
277 }
278 $mainLineItem->free();
279
280 //clean line item table.
281 if (!empty($dupeLineItemIds)) {
282 $sql = 'DELETE FROM civicrm_line_item WHERE id IN ( ' . implode(', ', $dupeLineItemIds) . ' )';
283 CRM_Core_DAO::executeQuery($sql);
284 }
285
286 $upgrade = new CRM_Upgrade_Form();
287 $upgrade->processSQL($rev);
288 }
289
290 /**
291 * @param $rev
292 */
293 public function upgrade_3_3_0($rev) {
294 $upgrade = new CRM_Upgrade_Form();
295 $upgrade->processSQL($rev);
296
297 //CRM-7123 -lets activate needful languages.
298 $config = CRM_Core_Config::singleton();
299 $locales = array();
300 if (is_dir($config->gettextResourceDir) && $dir = opendir($config->gettextResourceDir)) {
301 while ($filename = readdir($dir)) {
302 if (preg_match('/^[a-z][a-z]_[A-Z][A-Z]$/', $filename)) {
303 $locales[$filename] = $filename;
304 }
305 }
306 closedir($dir);
307 }
308
309 if (isset($config->languageLimit) && !empty($config->languageLimit)) {
310 //get all already enabled and all l10n languages.
311 $locales = array_merge(array_values($locales), array_keys($config->languageLimit));
312 }
313
314 if (!empty($locales)) {
315 $sql = '
316 UPDATE civicrm_option_value val
317 INNER JOIN civicrm_option_group grp ON ( grp.id = val.option_group_id )
318 SET val.is_active = 1
319 WHERE grp.name = %1
320 AND val.name IN ( ' . "'" . implode("', '", $locales) . "' )";
321
322 CRM_Core_DAO::executeQuery($sql,
323 array(1 => array('languages', 'String')),
324 TRUE, NULL, FALSE, FALSE
325 );
326 }
327 }
328
329 /**
330 * @param $rev
331 */
332 public function upgrade_3_3_2($rev) {
333 $dropMailingIndex = FALSE;
334 $indexes = CRM_Core_DAO::executeQuery('SHOW INDEXES FROM civicrm_mailing_job');
335 while ($indexes->fetch()) {
336 if ($indexes->Key_name == 'parent_id') {
337 $dropMailingIndex = TRUE;
338 break;
339 }
340 }
341 //CRM-7137
342 // get membership type for each membership block.
343 $sql = "SELECT id, membership_types FROM civicrm_membership_block ";
344 $dao = CRM_Core_DAO::executeQuery($sql);
345 while ($dao->fetch()) {
346 $memType = explode(',', $dao->membership_types);
347
348 $memTypeSerialize = array();
349 foreach ($memType as $k => $v) {
350 $memTypeSerialize[$v] = 0;
351 }
352
353 // save membership type as an serialized array along w/ auto_renew defalt value zero.
354 $memBlock = new CRM_Member_DAO_MembershipBlock();
355 $memBlock->id = $dao->id;
356 $memBlock->membership_types = serialize($memTypeSerialize);
357 $memBlock->save();
358 }
359
360 //CRM-7172
361 if (CRM_Mailing_Info::workflowEnabled()) {
362 $config = CRM_Core_Config::singleton();
363 if (is_callable(array(
364 $config->userSystem, 'replacePermission'))) {
365 $config->userSystem->replacePermission('access CiviMail', array('access CiviMail', 'create mailings', 'approve mailings', 'schedule mailings'));
366 }
367 }
368
369 $upgrade = new CRM_Upgrade_Form();
370 $upgrade->assign('dropMailingIndex', $dropMailingIndex);
371 $upgrade->processSQL($rev);
372 }
373
374 /**
375 * @param $rev
376 */
377 public function upgrade_3_3_7($rev) {
378 $dao = new CRM_Contact_DAO_Contact();
379 $dbName = $dao->_database;
380
381 $chkExtQuery = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = %1
382 AND TABLE_NAME = 'civicrm_phone' AND COLUMN_NAME = 'phone_ext'";
383 $extensionExists = CRM_Core_DAO::singleValueQuery($chkExtQuery,
384 array(1 => array($dbName, 'String')),
385 TRUE, FALSE
386 );
387
388 if (!$extensionExists) {
389 $colQuery = 'ALTER TABLE `civicrm_phone` ADD `phone_ext` VARCHAR( 16 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL AFTER `phone` ';
390 CRM_Core_DAO::executeQuery($colQuery);
391 }
392
393 // handle db changes done for CRM-8218
394 $alterContactDashboard = FALSE;
395 $dao = new CRM_Contact_DAO_DashboardContact();
396 $dbName = $dao->_database;
397
398 $chkContentQuery = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = %1
399 AND TABLE_NAME = 'civicrm_dashboard_contact' AND COLUMN_NAME = 'content'";
400 $contentExists = CRM_Core_DAO::singleValueQuery($chkContentQuery,
401 array(1 => array($dbName, 'String')),
402 TRUE, FALSE
403 );
404 if (!$contentExists) {
405 $alterContactDashboard = TRUE;
406 }
407
408 $upgrade = new CRM_Upgrade_Form();
409 $upgrade->assign('alterContactDashboard', $alterContactDashboard);
410 $upgrade->processSQL($rev);
411 }
412 }