INFRA-132 - Docblock formatting fixes
[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 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 |
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
06b69b18 30 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
31 * $Id$
32 *
33 */
34class CRM_Upgrade_Incremental_php_ThreeThree {
624e56fa
EM
35 /**
36 * @param $errors
37 *
38 * @return bool
39 */
00be9182 40 public function verifyPreDBstate(&$errors) {
6a488035
TO
41 return TRUE;
42 }
43
624e56fa
EM
44 /**
45 * @param $rev
46 */
00be9182 47 public function upgrade_3_3_alpha1($rev) {
6a488035
TO
48 $config = CRM_Core_Config::singleton();
49 if ($config->userSystem->is_drupal) {
50 // CRM-6426 - make civicrm profiles permissioned on drupal my account
51 $config->userSystem->updateCategories();
52 }
53
54 // CRM-6846
55 // insert name column for custom field table.
56 // make sure name for custom field, group and
57 // profile should be unique and properly munged.
58 $colQuery = 'ALTER TABLE `civicrm_custom_field` ADD `name` VARCHAR( 64 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL AFTER `custom_group_id` ';
59 CRM_Core_DAO::executeQuery($colQuery, CRM_Core_DAO::$_nullArray, TRUE, NULL, FALSE, FALSE);
60
61 $customFldCntQuery = 'select count(*) from civicrm_custom_field where name like %1 and id != %2';
62 $customField = new CRM_Core_DAO_CustomField();
63 $customField->selectAdd();
64 $customField->selectAdd('id, label');
65 $customField->find();
66 while ($customField->fetch()) {
67 $name = CRM_Utils_String::munge($customField->label, '_', 64);
68 $fldCnt = CRM_Core_DAO::singleValueQuery($customFldCntQuery,
e418776c 69 array(
353ffa53 70 1 => array($name, 'String'),
6a488035
TO
71 2 => array($customField->id, 'Integer'),
72 ), TRUE, FALSE
73 );
74 if ($fldCnt) {
75 $name = CRM_Utils_String::munge("{$name}_" . rand(), '_', 64);
76 }
77 $customFieldQuery = "
78Update `civicrm_custom_field`
79SET `name` = %1
80WHERE id = %2
81";
e418776c 82 $customFieldParams = array(
353ffa53 83 1 => array($name, 'String'),
6a488035
TO
84 2 => array($customField->id, 'Integer'),
85 );
86 CRM_Core_DAO::executeQuery($customFieldQuery, $customFieldParams, TRUE, NULL, FALSE, FALSE);
87 }
88 $customField->free();
89
90 $customGrpCntQuery = 'select count(*) from civicrm_custom_group where name like %1 and id != %2';
91 $customGroup = new CRM_Core_DAO_CustomGroup();
92 $customGroup->selectAdd();
93 $customGroup->selectAdd('id, title');
94 $customGroup->find();
95 while ($customGroup->fetch()) {
96 $name = CRM_Utils_String::munge($customGroup->title, '_', 64);
97 $grpCnt = CRM_Core_DAO::singleValueQuery($customGrpCntQuery,
e418776c 98 array(
353ffa53 99 1 => array($name, 'String'),
6a488035
TO
100 2 => array($customGroup->id, 'Integer'),
101 )
102 );
103 if ($grpCnt) {
104 $name = CRM_Utils_String::munge("{$name}_" . rand(), '_', 64);
105 }
106 CRM_Core_DAO::setFieldValue('CRM_Core_DAO_CustomGroup', $customGroup->id, 'name', $name);
107 }
108 $customGroup->free();
109
110 $ufGrpCntQuery = 'select count(*) from civicrm_uf_group where name like %1 and id != %2';
111 $ufGroup = new CRM_Core_DAO_UFGroup();
112 $ufGroup->selectAdd();
113 $ufGroup->selectAdd('id, title');
114 $ufGroup->find();
115 while ($ufGroup->fetch()) {
116 $name = CRM_Utils_String::munge($ufGroup->title, '_', 64);
117 $ufGrpCnt = CRM_Core_DAO::singleValueQuery($ufGrpCntQuery,
e418776c 118 array(
353ffa53 119 1 => array($name, 'String'),
6a488035
TO
120 2 => array($ufGroup->id, 'Integer'),
121 )
122 );
123 if ($ufGrpCnt) {
124 $name = CRM_Utils_String::munge("{$name}_" . rand(), '_', 64);
125 }
126 CRM_Core_DAO::setFieldValue('CRM_Core_DAO_UFGroup', $ufGroup->id, 'name', $name);
127 }
128 $ufGroup->free();
129
130 $upgrade = new CRM_Upgrade_Form();
131 $upgrade->processSQL($rev);
132
133 // now modify the config so that the directories are stored in option group/value
134 // CRM-6914
135 // require_once 'CRM/Core/BAO/ConfigSetting.php';
136 // $params = array( );
137 // CRM_Core_BAO_ConfigSetting::add( $parambs );
138 }
139
624e56fa
EM
140 /**
141 * @param $rev
142 */
00be9182 143 public function upgrade_3_3_beta1($rev) {
6a488035
TO
144 $upgrade = new CRM_Upgrade_Form();
145 $upgrade->processSQL($rev);
146
147 // CRM-6902
148 // Add column price_field_value_id in civicrm_line_item.
149 // Do not drop option_group_id column now since we need it to
150 // update line items.
151 $updateLineItem1 = "ALTER TABLE civicrm_line_item ADD COLUMN price_field_value_id int(10) unsigned default NULL;";
152 CRM_Core_DAO::executeQuery($updateLineItem1);
153
9da8dc8c 154 $priceFieldDAO = new CRM_Price_DAO_PriceField();
6a488035
TO
155 $priceFieldDAO->find();
156 $ids = array();
157 while ($priceFieldDAO->fetch()) {
158
159 $opGroupDAO = new CRM_Core_DAO_OptionGroup();
160 $opGroupDAO->name = 'civicrm_price_field.amount.' . $priceFieldDAO->id;
161
162 if (!$opGroupDAO->find(TRUE)) {
163 $opGroupDAO->free();
164 continue;
165 }
166
167 $opValueDAO = new CRM_Core_DAO_OptionValue();
168 $opValueDAO->option_group_id = $opGroupDAO->id;
169 $opValueDAO->find();
170
171 while ($opValueDAO->fetch()) {
172 // FIX ME: not migrating description(?), there will
173 // be a field description for each option.
174 $fieldValue = array(
175 'price_field_id' => $priceFieldDAO->id,
176 'label' => $opValueDAO->label,
177 'name' => CRM_Utils_String::munge($opValueDAO->label, '_', 64),
178 'amount' => $opValueDAO->name,
179 'weight' => $opValueDAO->weight,
180 'is_default' => $opValueDAO->is_default,
181 'is_active' => $opValueDAO->is_active,
182 );
183
184 if ($priceFieldDAO->count) {
185 // Migrate Participant Counts on option level.
186 // count of each option will be the same
187 // as earlier field count.
188 $fieldValue['count'] = $priceFieldDAO->count;
189 }
190
9da8dc8c 191 $fieldValueDAO = CRM_Price_BAO_PriceFieldValue::add($fieldValue, $ids);
6a488035
TO
192
193 $lineItemDAO = new CRM_Price_DAO_LineItem();
194 $lineItemDAO->option_group_id = $opGroupDAO->id;
195 $lineItemDAO->label = $opValueDAO->label;
196 $lineItemDAO->unit_price = $opValueDAO->name;
197
198 $labelFound = $priceFound = FALSE;
199
200 // check with label and amount
201 if (!$lineItemDAO->find(TRUE)) {
202 $lineItemDAO->free();
203 $lineItemDAO = new CRM_Price_DAO_LineItem();
204 $lineItemDAO->option_group_id = $opGroupDAO->id;
205 $lineItemDAO->label = $opValueDAO->label;
206
207 // check with label only
208 if ($lineItemDAO->find(TRUE)) {
209 $labelFound = TRUE;
210 }
211 }
212 else {
213 $labelFound = TRUE;
214 $priceFound = TRUE;
215 }
216
217 $lineItemDAO->free();
218
219 // update civicrm_line_item for price_field_value_id.
220 // Used query to avoid line by line update.
221 if ($labelFound || $priceFound) {
e418776c 222 $lineItemParams = array(
353ffa53 223 1 => array($fieldValueDAO->id, 'Integer'),
6a488035
TO
224 2 => array($opValueDAO->label, 'String'),
225 );
226 $updateLineItems = "UPDATE civicrm_line_item SET price_field_value_id = %1 WHERE label = %2";
227 if ($priceFound) {
228 $lineItemParams[3] = array($opValueDAO->name, 'Float');
229 $updateLineItems .= " AND unit_price = %3";
230 }
231 CRM_Core_DAO::executeQuery($updateLineItems, $lineItemParams);
232 }
233 }
234
235 $opGroupDAO->delete();
236 $opValueDAO->free();
237 $opGroupDAO->free();
238 }
239
240 $priceFieldDAO->free();
241
242 // Now drop option_group_id column from civicrm_line_item
243 $updateLineItem2 = "ALTER TABLE civicrm_line_item DROP option_group_id,
244 ADD CONSTRAINT `FK_civicrm_price_field_value_id` FOREIGN KEY (price_field_value_id) REFERENCES civicrm_price_field_value(id) ON DELETE SET NULL;";
245 CRM_Core_DAO::executeQuery($updateLineItem2, array(), TRUE, NULL, FALSE, FALSE);
246
247 $updatePriceField = "ALTER TABLE civicrm_price_field DROP count";
248 CRM_Core_DAO::executeQuery($updatePriceField, array(), TRUE, NULL, FALSE, FALSE);
249
250 // as the table 'civicrm_price_field' is localised and column 'count' is dropped
251 // after the views are rebuild, we need to rebuild views to avoid invalid refrence of table.
252 if ($upgrade->multilingual) {
253 CRM_Core_I18n_Schema::rebuildMultilingualSchema($upgrade->locales, $rev);
254 }
255 }
256
624e56fa
EM
257 /**
258 * @param $rev
259 */
00be9182 260 public function upgrade_3_3_beta3($rev) {
6a488035
TO
261 // get the duplicate Ids of line item entries
262 $dupeLineItemIds = array();
353ffa53
TO
263 $fields = array('entity_table', 'entity_id', 'price_field_id', 'price_field_value_id');
264 $mainLineItem = new CRM_Price_BAO_LineItem();
6a488035
TO
265 $mainLineItem->find(TRUE);
266 while ($mainLineItem->fetch()) {
267 $dupeLineItem = new CRM_Price_BAO_LineItem();
4e66d748
TO
268 foreach ($fields as $fld) {
269 $dupeLineItem->$fld = $mainLineItem->$fld;
e418776c 270 }
6a488035
TO
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
624e56fa
EM
290 /**
291 * @param $rev
292 */
00be9182 293 public function upgrade_3_3_0($rev) {
6a488035
TO
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();
948d11bf 300 if (is_dir($config->gettextResourceDir) && $dir = opendir($config->gettextResourceDir)) {
6a488035
TO
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
317INNER 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
624e56fa
EM
329 /**
330 * @param $rev
331 */
00be9182 332 public function upgrade_3_3_2($rev) {
6a488035
TO
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(
353ffa53
TO
364 $config->userSystem,
365 'replacePermission'
366 ))) {
367 $config->userSystem->replacePermission('access CiviMail', array(
368 'access CiviMail',
369 'create mailings',
370 'approve mailings',
371 'schedule mailings'
372 ));
6a488035
TO
373 }
374 }
375
376 $upgrade = new CRM_Upgrade_Form();
377 $upgrade->assign('dropMailingIndex', $dropMailingIndex);
378 $upgrade->processSQL($rev);
379 }
380
624e56fa
EM
381 /**
382 * @param $rev
383 */
00be9182 384 public function upgrade_3_3_7($rev) {
6a488035
TO
385 $dao = new CRM_Contact_DAO_Contact();
386 $dbName = $dao->_database;
387
388 $chkExtQuery = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = %1
389 AND TABLE_NAME = 'civicrm_phone' AND COLUMN_NAME = 'phone_ext'";
390 $extensionExists = CRM_Core_DAO::singleValueQuery($chkExtQuery,
391 array(1 => array($dbName, 'String')),
392 TRUE, FALSE
393 );
394
395 if (!$extensionExists) {
396 $colQuery = 'ALTER TABLE `civicrm_phone` ADD `phone_ext` VARCHAR( 16 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL AFTER `phone` ';
397 CRM_Core_DAO::executeQuery($colQuery);
398 }
399
400 // handle db changes done for CRM-8218
401 $alterContactDashboard = FALSE;
402 $dao = new CRM_Contact_DAO_DashboardContact();
403 $dbName = $dao->_database;
404
405 $chkContentQuery = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = %1
406 AND TABLE_NAME = 'civicrm_dashboard_contact' AND COLUMN_NAME = 'content'";
407 $contentExists = CRM_Core_DAO::singleValueQuery($chkContentQuery,
408 array(1 => array($dbName, 'String')),
409 TRUE, FALSE
410 );
411 if (!$contentExists) {
412 $alterContactDashboard = TRUE;
413 }
414
415 $upgrade = new CRM_Upgrade_Form();
416 $upgrade->assign('alterContactDashboard', $alterContactDashboard);
417 $upgrade->processSQL($rev);
418 }
419}