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