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