Merge pull request #13069 from eileenmcnaughton/master
[civicrm-core.git] / CRM / Upgrade / Incremental / php / FourTwo.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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 and the CiviCRM Licensing Exception. |
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 and the CiviCRM Licensing Exception along |
21 | 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 * Upgrade logic for 4.2
30 */
31 class CRM_Upgrade_Incremental_php_FourTwo extends CRM_Upgrade_Incremental_Base {
32 const SETTINGS_SNIPPET_PATTERN = '/CRM_Core_ClassLoader::singleton\(\)-\>register/';
33 const SETTINGS_SNIPPET = "\nrequire_once 'CRM/Core/ClassLoader.php';\nCRM_Core_ClassLoader::singleton()->register();\n";
34
35 /**
36 * Compute any messages which should be displayed beforeupgrade.
37 *
38 * Note: This function is called iteratively for each upcoming
39 * revision to the database.
40 *
41 * @param string $preUpgradeMessage
42 * @param string $rev
43 * a version number, e.g. '4.2.alpha1', '4.2.beta3', '4.2.0'.
44 * @param null $currentVer
45 *
46 * @return bool
47 */
48 public function setPreUpgradeMessage(&$preUpgradeMessage, $rev, $currentVer = NULL) {
49 if ($rev == '4.2.alpha1') {
50 $tables = array('civicrm_contribution_page', 'civicrm_event', 'civicrm_group', 'civicrm_contact');
51 if (!CRM_Core_DAO::schemaRequiresRebuilding($tables)) {
52 $errors = "The upgrade has identified some schema integrity issues in the database. It seems some of your constraints are missing. You will have to rebuild your schema before re-trying the upgrade. Please refer to " . CRM_Utils_System::docURL2("Ensuring Schema Integrity on Upgrades", FALSE, "Ensuring Schema Integrity on Upgrades", NULL, NULL, "wiki");
53 CRM_Core_Error::fatal($errors);
54 return FALSE;
55 }
56
57 // CRM-10613, CRM-11120
58 $query = "
59 SELECT mp.contribution_id, mp.membership_id, mem.membership_type_id, mem.start_date, mem.end_date, mem.status_id, mem.contact_id
60 FROM civicrm_membership_payment mp
61 INNER JOIN ( SELECT cmp.contribution_id
62 FROM civicrm_membership_payment cmp
63 LEFT JOIN civicrm_line_item cli ON cmp.contribution_id=cli.entity_id and cli.entity_table = 'civicrm_contribution'
64 WHERE cli.entity_id IS NULL
65 GROUP BY cmp.contribution_id
66 HAVING COUNT(cmp.membership_id) > 1) submp ON submp.contribution_id = mp.contribution_id
67 INNER JOIN civicrm_membership mem ON mem.id = mp.membership_id
68 ORDER BY mp.contribution_id, mp.membership_id";
69 $invalidData = CRM_Core_DAO::executeQuery($query);
70 if ($invalidData->N) {
71 $invalidDataMessage = "<br /><strong>" . 'The upgrade is being aborted due to data integrity issues in your database. There are multiple membership records linked to the same contribution record. This is unexpected, and some of the membership records may be duplicates. The problem record sets are listed below. Refer to <a href="http://wiki.civicrm.org/confluence/display/CRMDOC42/Repair+database+script+for+4.2+upgrades">this wiki page for instructions on repairing your database</a> so that you can run the upgrade successfully.' . "</strong>";
72 $membershipType = CRM_Member_PseudoConstant::membershipType();
73 $membershipStatus = CRM_Member_PseudoConstant::membershipStatus();
74 $invalidDataMessage .= "<table border=1><tr><th>Contact-ID</th><th>Contribution-ID</th><th>Membership-ID</th><th>Membership Type</th><th>Start Date</th><th>End Date</th><th>Membership Status</th></tr>";
75 while ($invalidData->fetch()) {
76 $invalidDataMessage .= "<tr>";
77 $invalidDataMessage .= "<td>{$invalidData->contact_id}</td>";
78 $invalidDataMessage .= "<td>{$invalidData->contribution_id}</td>";
79 $invalidDataMessage .= "<td>{$invalidData->membership_id}</td>";
80 $invalidDataMessage .= "<td>" . CRM_Utils_Array::value($invalidData->membership_type_id, $membershipType) . "</td>";
81 $invalidDataMessage .= "<td>{$invalidData->start_date}</td>";
82 $invalidDataMessage .= "<td>{$invalidData->end_date}</td>";
83 $invalidDataMessage .= "<td>" . CRM_Utils_Array::value($invalidData->status_id, $membershipStatus) . "</td>";
84 $invalidDataMessage .= "</tr>";
85 }
86 $clickHere = CRM_Utils_System::url('civicrm/upgrade/cleanup425', 'reset=1');
87 $invalidDataMessage .= "</table><p>If you have reviewed the cleanup script documentation on the wiki and you are ready to run the cleanup now - <a href='$clickHere'>click here</a>.</p>";
88 CRM_Core_Error::fatal($invalidDataMessage);
89 return FALSE;
90 }
91 }
92
93 if ($rev == '4.2.beta2') {
94 // note: error conditions are also checked in upgrade_4_2_beta2()
95 if (!defined('CIVICRM_SETTINGS_PATH')) {
96 $preUpgradeMessage .= '<br />' . ts('Could not determine path to civicrm.settings.php. Please manually locate it and add these lines at the bottom: <pre>%1</pre>', array(
97 1 => self::SETTINGS_SNIPPET,
98 ));
99 }
100 elseif (preg_match(self::SETTINGS_SNIPPET_PATTERN, file_get_contents(CIVICRM_SETTINGS_PATH))) {
101 // OK, nothing to do
102 }
103 elseif (!is_writable(CIVICRM_SETTINGS_PATH)) {
104 $preUpgradeMessage .= '<br />' . ts('The settings file (%1) must be updated. Please make it writable or manually add these lines:<pre>%2</pre>', array(
105 1 => CIVICRM_SETTINGS_PATH,
106 2 => self::SETTINGS_SNIPPET,
107 ));
108 }
109 }
110 if ($rev == '4.2.2' && version_compare($currentVer, '3.3.alpha1') >= 0) {
111 $query = " SELECT cli.id
112 FROM `civicrm_line_item` cli
113 INNER JOIN civicrm_membership_payment cmp ON cmp.contribution_id = cli.entity_id AND cli.entity_table = 'civicrm_contribution'
114 INNER JOIN civicrm_price_field_value cpfv ON cpfv.id = cli.price_field_value_id
115 INNER JOIN civicrm_price_field cpf ON cpf.id = cpfv.price_field_id and cpf.id != cli.price_field_id
116 INNER JOIN civicrm_price_set cps ON cps.id = cpf.price_set_id AND cps.name <>'default_membership_type_amount' ";
117 $dao = CRM_Core_DAO::executeQuery($query);
118 if ($dao->N) {
119 $preUpgradeMessage .= "<br /><strong>We have identified extraneous data in your database that a previous upgrade likely introduced. We STRONGLY recommend making a backup of your site before continuing. We also STRONGLY suggest fixing this issue with unneeded records BEFORE you upgrade. You can find more information about this issue and the way to fix it by visiting <a href='http://forum.civicrm.org/index.php/topic,26181.0.html'>http://forum.civicrm.org/index.php/topic,26181.0.html</a>.</strong>";
120 }
121 }
122
123 if (version_compare($rev, '4.2.9') >= 0) {
124 //CRM-11980
125 $sql = "SELECT id FROM civicrm_option_group WHERE name LIKE 'civicrm_price_field.amount.%' LIMIT 1";
126 $dao = CRM_Core_DAO::executeQuery($sql);
127 if ($dao->fetch()) {
128 $errors = "We found unexpected data values from an older version of CiviCRM in your database. The upgrade can not be run until this condition is corrected.<br /><br />Details: One or more rows are present in the civicrm_option_group with name like 'civicrm_price_field.amount.%'. <a href='http://forum.civicrm.org/index.php/topic,27744.msg118748.html#msg118748'>Check here for information on diagnosing and correcting this problem.</a>";
129 CRM_Core_Error::fatal($errors);
130 return FALSE;
131 }
132 }
133 }
134
135 /**
136 * Compute any messages which should be displayed after upgrade.
137 *
138 * @param string $postUpgradeMessage
139 * alterable.
140 * @param string $rev
141 * an intermediate version; note that setPostUpgradeMessage is called repeatedly with different $revs.
142 */
143 public function setPostUpgradeMessage(&$postUpgradeMessage, $rev) {
144 if ($rev == '4.2.beta5') {
145 $config = CRM_Core_Config::singleton();
146 if (!empty($config->extensionsDir)) {
147 $postUpgradeMessage .= '<br />' . ts('Please <a href="%1" target="_blank">configure the Extension Resource URL</a>.', array(
148 1 => CRM_Utils_System::url('civicrm/admin/setting/url', 'reset=1'),
149 ));
150 }
151 }
152 if ($rev == '4.2.7') {
153 $postUpgradeMessage .= '<br />' . ts('If you have configured a report instance to allow anonymous access, you will need to reset the permission to Everyone for that instance (under the Report Settings pane).');
154 }
155 }
156
157 /**
158 * @param $rev
159 */
160 public function upgrade_4_2_alpha1($rev) {
161 //checking whether the foreign key exists before dropping it
162 //drop foreign key queries of CRM-9850
163 $params = array();
164 $tables = array(
165 'civicrm_contribution_page' => 'FK_civicrm_contribution_page_payment_processor_id',
166 'civicrm_event' => 'FK_civicrm_event_payment_processor_id',
167 'civicrm_group' => 'FK_civicrm_group_saved_search_id',
168 );
169 foreach ($tables as $tableName => $fKey) {
170 $foreignKeyExists = CRM_Core_DAO::checkConstraintExists($tableName, $fKey);
171 if ($foreignKeyExists) {
172 CRM_Core_DAO::executeQuery("ALTER TABLE {$tableName} DROP FOREIGN KEY {$fKey}", $params, TRUE, NULL, FALSE, FALSE);
173 CRM_Core_DAO::executeQuery("ALTER TABLE {$tableName} DROP INDEX {$fKey}", $params, TRUE, NULL, FALSE, FALSE);
174 }
175 }
176 // Drop index UI_title for civicrm_price_set
177 $domain = new CRM_Core_DAO_Domain();
178 $domain->find(TRUE);
179 if ($domain->locales) {
180 $locales = explode(CRM_Core_DAO::VALUE_SEPARATOR, $domain->locales);
181 foreach ($locales as $locale) {
182 $query = "SHOW KEYS FROM `civicrm_price_set` WHERE key_name = 'UI_title_{$locale}'";
183 $dao = CRM_Core_DAO::executeQuery($query, $params, TRUE, NULL, FALSE, FALSE);
184 if ($dao->N) {
185 CRM_Core_DAO::executeQuery("ALTER TABLE `civicrm_price_set` DROP INDEX `UI_title_{$locale}`", $params, TRUE, NULL, FALSE, FALSE);
186 }
187 }
188 }
189 else {
190 $query = "SHOW KEYS FROM `civicrm_price_set` WHERE key_name = 'UI_title'";
191 $dao = CRM_Core_DAO::executeQuery($query);
192 if ($dao->N) {
193 CRM_Core_DAO::executeQuery("ALTER TABLE `civicrm_price_set` DROP INDEX `UI_title`");
194 }
195 }
196
197 // Some steps take a long time, so we break them up into separate
198 // tasks and enqueue them separately.
199 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => '4.2.alpha1')), 'runSql', $rev);
200 $this->addTask('Upgrade DB to 4.2.alpha1: Price Sets', 'task_4_2_alpha1_createPriceSets', $rev);
201 self::convertContribution();
202 $this->addTask('Upgrade DB to 4.2.alpha1: Event Profile', 'task_4_2_alpha1_eventProfile');
203 }
204
205 /**
206 * @param $rev
207 */
208 public function upgrade_4_2_beta2($rev) {
209 // note: error conditions are also checked in setPreUpgradeMessage()
210 if (defined('CIVICRM_SETTINGS_PATH')) {
211 if (!preg_match(self::SETTINGS_SNIPPET_PATTERN, file_get_contents(CIVICRM_SETTINGS_PATH))) {
212 if (is_writable(CIVICRM_SETTINGS_PATH)) {
213 file_put_contents(CIVICRM_SETTINGS_PATH, self::SETTINGS_SNIPPET, FILE_APPEND);
214 }
215 }
216 }
217 }
218
219 /**
220 * @param $rev
221 */
222 public function upgrade_4_2_beta3($rev) {
223 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => '4.2.beta3')), 'runSql', $rev);
224 $minParticipantId = CRM_Core_DAO::singleValueQuery('SELECT coalesce(min(id),0) FROM civicrm_participant');
225 $maxParticipantId = CRM_Core_DAO::singleValueQuery('SELECT coalesce(max(id),0) FROM civicrm_participant');
226
227 for ($startId = $minParticipantId; $startId <= $maxParticipantId; $startId += self::BATCH_SIZE) {
228 $endId = $startId + self::BATCH_SIZE - 1;
229 $title = "Upgrade DB to 4.2.alpha1: Participant ($startId => $endId)";
230 $this->addTask($title, 'task_4_2_alpha1_convertParticipants', $startId, $endId);
231 }
232 }
233
234 /**
235 * @param $rev
236 */
237 public function upgrade_4_2_beta5($rev) {
238 // CRM-10629 Create a setting for extension URLs
239 // For some reason, this isn't working when placed in the .sql file
240 CRM_Core_DAO::executeQuery("
241 INSERT INTO civicrm_setting(group_name,name,value,domain_id,is_domain)
242 VALUES ('URL Preferences', 'extensionsURL',NULL,1,1);
243 ");
244 }
245
246 /**
247 * @param $rev
248 */
249 public function upgrade_4_2_0($rev) {
250 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => '4.2.0')), 'runSql', $rev);
251 }
252
253 /**
254 * @param $rev
255 */
256 public function upgrade_4_2_2($rev) {
257 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => '4.2.2')), 'runSql', $rev);
258 //create line items for memberships and participants for api/import
259 self::convertContribution();
260
261 // CRM-10937 Fix the title on civicrm_dedupe_rule_group
262 $upgrade = new CRM_Upgrade_Form();
263 if ($upgrade->multilingual) {
264 // Check if the 'title' field exists
265 $query = "SELECT column_name
266 FROM information_schema.COLUMNS
267 WHERE table_name = 'civicrm_dedupe_rule_group'
268 AND table_schema = DATABASE()
269 AND column_name = 'title'";
270
271 $dao = CRM_Core_DAO::executeQuery($query);
272
273 if (!$dao->N) {
274 $domain = new CRM_Core_DAO_Domain();
275 $domain->find(TRUE);
276
277 if ($domain->locales) {
278 $locales = explode(CRM_Core_DAO::VALUE_SEPARATOR, $domain->locales);
279 $locale = array_shift($locales);
280
281 // Use the first language (they should all have the same value)
282 CRM_Core_DAO::executeQuery("ALTER TABLE `civicrm_dedupe_rule_group` CHANGE `title_{$locale}` `title` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'Label of the rule group'", $params, TRUE, NULL, FALSE, FALSE);
283
284 // Drop remaining the column for the remaining languages
285 foreach ($locales as $locale) {
286 CRM_Core_DAO::executeQuery("ALTER TABLE `civicrm_dedupe_rule_group` DROP `title_{$locale}`", $params, TRUE, NULL, FALSE, FALSE);
287 }
288 }
289 }
290 }
291 }
292
293 /**
294 * @param $rev
295 */
296 public function upgrade_4_2_3($rev) {
297 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => '4.2.3')), 'runSql', $rev);
298 // CRM-10953 Remove duplicate activity type for 'Reminder Sent' which is mistakenly inserted by 4.2.alpha1 upgrade script
299 $queryMin = "
300 SELECT coalesce(min(value),0) from civicrm_option_value ov
301 WHERE ov.option_group_id =
302 (SELECT id from civicrm_option_group og WHERE og.name = 'activity_type') AND
303 ov.name = 'Reminder Sent'";
304
305 $minReminderSent = CRM_Core_DAO::singleValueQuery($queryMin);
306
307 $queryMax = "
308 SELECT coalesce(max(value),0) from civicrm_option_value ov
309 WHERE ov.option_group_id =
310 (SELECT id from civicrm_option_group og WHERE og.name = 'activity_type') AND
311 ov.name = 'Reminder Sent'";
312
313 $maxReminderSent = CRM_Core_DAO::singleValueQuery($queryMax);
314
315 // If we have two different values, replace new value with original in any activities
316 if ($maxReminderSent > $minReminderSent) {
317 $query = "
318 UPDATE civicrm_activity
319 SET activity_type_id = {$minReminderSent}
320 WHERE activity_type_id = {$maxReminderSent}";
321
322 CRM_Core_DAO::executeQuery($query);
323
324 // Then delete the newer (duplicate) option_value row
325 $query = "
326 DELETE from civicrm_option_value
327 WHERE option_group_id =
328 (SELECT id from civicrm_option_group og WHERE og.name = 'activity_type') AND
329 value = '{$maxReminderSent}'";
330
331 CRM_Core_DAO::executeQuery($query);
332 }
333 }
334
335 /**
336 * @param $rev
337 */
338 public function upgrade_4_2_5($rev) {
339 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => '4.2.5')), 'runSql', $rev);
340 //CRM-11077
341 $sql = " SELECT cpse.entity_id, cpse.price_set_id
342 FROM `civicrm_price_set_entity` cpse
343 LEFT JOIN civicrm_price_set cps ON cps.id = cpse.price_set_id
344 LEFT JOIN civicrm_price_set_entity cpse1 ON cpse1.price_set_id = cpse.price_set_id
345 WHERE cpse.entity_table = 'civicrm_event' AND cps.is_quick_config = 1
346 GROUP BY cpse.id
347 HAVING COUNT(cpse.price_set_id) > 1 AND MIN(cpse1.id) <> cpse.id ";
348
349 $dao = CRM_Core_DAO::executeQuery($sql);
350 while ($dao->fetch()) {
351 if ($dao->price_set_id) {
352 $copyPriceSet = &CRM_Upgrade_Snapshot_V4p2_Price_BAO_Set::copy($dao->price_set_id);
353 CRM_Upgrade_Snapshot_V4p2_Price_BAO_Set::addTo('civicrm_event', $dao->entity_id, $copyPriceSet->id);
354 }
355 }
356 }
357
358 public function convertContribution() {
359 $minContributionId = CRM_Core_DAO::singleValueQuery('SELECT coalesce(min(id),0) FROM civicrm_contribution');
360 $maxContributionId = CRM_Core_DAO::singleValueQuery('SELECT coalesce(max(id),0) FROM civicrm_contribution');
361 for ($startId = $minContributionId; $startId <= $maxContributionId; $startId += self::BATCH_SIZE) {
362 $endId = $startId + self::BATCH_SIZE - 1;
363 $title = "Upgrade DB to 4.2.alpha1: Contributions ($startId => $endId)";
364 $this->addTask($title, 'task_4_2_alpha1_convertContributions', $startId, $endId);
365 }
366 $minParticipantId = CRM_Core_DAO::singleValueQuery('SELECT coalesce(min(id),0) FROM civicrm_participant');
367 $maxParticipantId = CRM_Core_DAO::singleValueQuery('SELECT coalesce(max(id),0) FROM civicrm_participant');
368
369 for ($startId = $minParticipantId; $startId <= $maxParticipantId; $startId += self::BATCH_SIZE) {
370 $endId = $startId + self::BATCH_SIZE - 1;
371 $title = "Upgrade DB to 4.2.alpha1: Participant ($startId => $endId)";
372 $this->addTask($title, 'task_4_2_alpha1_convertParticipants', $startId, $endId);
373 }
374 }
375
376 /**
377 * (Queue Task Callback)
378 *
379 * Upgrade code to create priceset for contribution pages and events
380 *
381 * @param \CRM_Queue_TaskContext $ctx
382 * @param string $rev
383 *
384 * @return bool
385 */
386 public static function task_4_2_alpha1_createPriceSets(CRM_Queue_TaskContext $ctx, $rev) {
387 $upgrade = new CRM_Upgrade_Form();
388 $daoName = array(
389 'civicrm_contribution_page' => array(
390 'CRM_Contribute_BAO_ContributionPage',
391 CRM_Core_Component::getComponentID('CiviContribute'),
392 ),
393 'civicrm_event' => array(
394 'CRM_Event_BAO_Event',
395 CRM_Core_Component::getComponentID('CiviEvent'),
396 ),
397 );
398
399 // get all option group used for event and contribution page
400 $query = "
401 SELECT id, name
402 FROM civicrm_option_group
403 WHERE name LIKE '%.amount.%' ";
404 $dao = CRM_Core_DAO::executeQuery($query);
405 while ($dao->fetch()) {
406 $addTo = explode('.', $dao->name);
407 if (!empty($addTo[2])) {
408 $options = array('optionGroup' => $dao->name);
409 self::createPriceSet($daoName, $addTo, $options);
410 }
411 CRM_Core_OptionGroup::deleteAssoc($dao->name);
412 }
413
414 //create pricesets for contribution with only other amount
415 $query = "
416 SELECT ccp.id as contribution_page_id, ccp.is_allow_other_amount, cmb.id as membership_block_id
417 FROM civicrm_contribution_page ccp
418 LEFT JOIN civicrm_membership_block cmb ON cmb.entity_id = ccp.id AND cmb.entity_table = 'civicrm_contribution_page'
419 LEFT JOIN civicrm_price_set_entity cpse ON cpse.entity_id = ccp.id and cpse.entity_table = 'civicrm_contribution_page'
420 WHERE cpse.price_set_id IS NULL";
421 $dao = CRM_Core_DAO::executeQuery($query);
422 $addTo = array('civicrm_contribution_page');
423 while ($dao->fetch()) {
424 $addTo[2] = $dao->contribution_page_id;
425 $options = array(
426 'otherAmount' => $dao->is_allow_other_amount,
427 'membership' => $dao->membership_block_id,
428 );
429 self::createPriceSet($daoName, $addTo, $options);
430 }
431
432 return TRUE;
433 }
434
435 /**
436 * Create price sets.
437 *
438 * @param string $daoName
439 * @param string $addTo
440 * @param array $options
441 */
442 public static function createPriceSet($daoName, $addTo, $options = array()) {
443 $query = "SELECT title FROM {$addTo[0]} where id =%1";
444 $setParams['title'] = CRM_Core_DAO::singleValueQuery($query,
445 array(1 => array($addTo[2], 'Integer'))
446 );
447 $pageTitle = strtolower(CRM_Utils_String::munge($setParams['title'], '_', 245));
448
449 // an event or contrib page has been deleted but left the option group behind - (this may be fixed in later versions?)
450 // we should probably delete the option group - but at least early exit here as the code following it does not fatal
451 // CRM-10298
452 if (empty($pageTitle)) {
453 return;
454 }
455
456 $optionValue = array();
457 if (!empty($options['optionGroup'])) {
458 CRM_Core_OptionGroup::getAssoc($options['optionGroup'], $optionValue);
459 if (empty($optionValue)) {
460 return;
461 }
462 }
463 elseif (empty($options['otherAmount']) && empty($options['membership'])) {
464 //CRM-12273
465 //if options group, otherAmount, membersip is empty then return, contribution should be default price set
466 return;
467 }
468
469 if (!CRM_Core_DAO::getFieldValue('CRM_Upgrade_Snapshot_V4p2_Price_BAO_Set', $pageTitle, 'id', 'name', TRUE)) {
470 $setParams['name'] = $pageTitle;
471 }
472 else {
473 $timeSec = explode(".", microtime(TRUE));
474 $setParams['name'] = $pageTitle . '_' . date('is', $timeSec[0]) . $timeSec[1];
475 }
476 $setParams['extends'] = $daoName[$addTo[0]][1];
477 $setParams['is_quick_config'] = 1;
478 $priceSet = CRM_Upgrade_Snapshot_V4p2_Price_BAO_Set::create($setParams);
479 CRM_Upgrade_Snapshot_V4p2_Price_BAO_Set::addTo($addTo[0], $addTo[2], $priceSet->id, 1);
480
481 $fieldParams['price_set_id'] = $priceSet->id;
482 if (!empty($options['optionGroup'])) {
483 $fieldParams['html_type'] = 'Radio';
484 $fieldParams['is_required'] = 1;
485 if ($addTo[0] == 'civicrm_event') {
486 $query = "SELECT fee_label FROM civicrm_event where id =%1";
487 $fieldParams['name'] = $fieldParams['label'] = CRM_Core_DAO::singleValueQuery($query,
488 array(1 => array($addTo[2], 'Integer'))
489 );
490 $defaultAmountColumn = 'default_fee_id';
491 }
492 else {
493 $options['membership'] = 1;
494 $fieldParams['name'] = strtolower(CRM_Utils_String::munge("Contribution Amount", '_', 245));
495 $fieldParams['label'] = "Contribution Amount";
496 $defaultAmountColumn = 'default_amount_id';
497 $options['otherAmount'] = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', $addTo[2], 'is_allow_other_amount');
498 if (!empty($options['otherAmount'])) {
499 $fieldParams['is_required'] = 0;
500 }
501 }
502 $fieldParams['option_label'] = $optionValue['label'];
503 $fieldParams['option_amount'] = $optionValue['value'];
504 $fieldParams['option_weight'] = $optionValue['weight'];
505 $fieldParams['is_quick_config'] = $setParams['is_quick_config'];
506 if ($defaultAmount = CRM_Core_DAO::getFieldValue($daoName[$addTo[0]][0], $addTo[2], $defaultAmountColumn)) {
507 $fieldParams['default_option'] = array_search($defaultAmount, $optionValue['amount_id']);
508 }
509 $priceField = CRM_Upgrade_Snapshot_V4p2_Price_BAO_Field::create($fieldParams);
510
511 }
512 if (!empty($options['membership'])) {
513 $dao = new CRM_Member_DAO_MembershipBlock();
514 $dao->entity_table = 'civicrm_contribution_page';
515 $dao->entity_id = $addTo[2];
516
517 if ($dao->find(TRUE)) {
518 if ($dao->membership_types) {
519 $fieldParams = array(
520 'name' => strtolower(CRM_Utils_String::munge("Membership Amount", '_', 245)),
521 'label' => "Membership Amount",
522 'is_required' => $dao->is_required,
523 'is_display_amounts' => $dao->display_min_fee,
524 'is_active' => $dao->is_active,
525 'price_set_id' => $priceSet->id,
526 'html_type' => 'Radio',
527 'weight' => 1,
528 );
529 $membershipTypes = unserialize($dao->membership_types);
530 $rowcount = 0;
531 foreach ($membershipTypes as $membershipType => $autoRenew) {
532 $membershipTypeDetail = CRM_Member_BAO_MembershipType::getMembershipTypeDetails($membershipType);
533 $rowcount++;
534 $fieldParams['option_label'][$rowcount] = $membershipTypeDetail['name'];
535 $fieldParams['option_amount'][$rowcount] = $membershipTypeDetail['minimum_fee'];
536 $fieldParams['option_weight'][$rowcount] = $rowcount;
537 $fieldParams['membership_type_id'][$rowcount] = $membershipType;
538 if ($membershipType == $dao->membership_type_default) {
539 $fieldParams['default_option'] = $rowcount;
540 }
541 }
542 $priceField = CRM_Upgrade_Snapshot_V4p2_Price_BAO_Field::create($fieldParams);
543
544 $setParams = array(
545 'id' => $priceSet->id,
546 'extends' => CRM_Core_Component::getComponentID('CiviMember'),
547 'contribution_type_id' => CRM_Core_DAO::getFieldValue($daoName[$addTo[0]][0], $addTo[2], 'contribution_type_id'),
548 );
549 CRM_Upgrade_Snapshot_V4p2_Price_BAO_Set::create($setParams);
550 }
551 }
552 }
553 if (!empty($options['otherAmount'])) {
554
555 $fieldParams = array(
556 'name' => strtolower(CRM_Utils_String::munge("Other Amount", '_', 245)),
557 'label' => "Other Amount",
558 'is_required' => 0,
559 'is_display_amounts' => 0,
560 'is_active' => 1,
561 'price_set_id' => $priceSet->id,
562 'html_type' => 'Text',
563 'weight' => 3,
564 );
565 $fieldParams['option_label'][1] = "Other Amount";
566 $fieldParams['option_amount'][1] = 1;
567 $fieldParams['option_weight'][1] = 1;
568 $priceField = CRM_Upgrade_Snapshot_V4p2_Price_BAO_Field::create($fieldParams);
569 }
570 }
571
572 /**
573 * (Queue Task Callback)
574 *
575 * Find any contribution records and create corresponding line-item
576 * records.
577 *
578 * @param CRM_Queue_TaskContext $ctx
579 * @param int $startId
580 * the first/lowest contribution ID to convert.
581 * @param int $endId
582 * the last/highest contribution ID to convert.
583 *
584 * @return bool
585 */
586 public static function task_4_2_alpha1_convertContributions(CRM_Queue_TaskContext $ctx, $startId, $endId) {
587 $upgrade = new CRM_Upgrade_Form();
588 $query = "
589 INSERT INTO civicrm_line_item(`entity_table` ,`entity_id` ,`price_field_id` ,`label` , `qty` ,`unit_price` ,`line_total` ,`participant_count` ,`price_field_value_id`)
590 SELECT 'civicrm_contribution',cc.id, cpf.id as price_field_id, cpfv.label, 1, cc.total_amount, cc.total_amount line_total, 0, cpfv.id as price_field_value
591 FROM civicrm_membership_payment cmp
592 LEFT JOIN `civicrm_contribution` cc ON cc.id = cmp.contribution_id
593 LEFT JOIN civicrm_line_item cli ON cc.id=cli.entity_id and cli.entity_table = 'civicrm_contribution'
594 LEFT JOIN civicrm_membership cm ON cm.id=cmp.membership_id
595 LEFT JOIN civicrm_membership_type cmt ON cmt.id = cm.membership_type_id
596 LEFT JOIN civicrm_price_field cpf ON BINARY cpf.name = cmt.member_of_contact_id
597 LEFT JOIN civicrm_price_field_value cpfv ON cpfv.membership_type_id = cm.membership_type_id AND cpf.id = cpfv.price_field_id
598 WHERE (cc.id BETWEEN %1 AND %2) AND cli.entity_id IS NULL ;
599 ";
600 $sqlParams = array(
601 1 => array($startId, 'Integer'),
602 2 => array($endId, 'Integer'),
603 );
604 CRM_Core_DAO::executeQuery($query, $sqlParams);
605
606 // create lineitems for contribution done for membership
607 $sql = "
608 SELECT cc.id, cmp.membership_id, cpse.price_set_id, cc.total_amount
609 FROM civicrm_contribution cc
610 LEFT JOIN civicrm_line_item cli ON cc.id=cli.entity_id AND cli.entity_table = 'civicrm_contribution'
611 LEFT JOIN civicrm_membership_payment cmp ON cc.id = cmp.contribution_id
612 LEFT JOIN civicrm_participant_payment cpp ON cc.id = cpp.contribution_id
613 LEFT JOIN civicrm_price_set_entity cpse on cpse.entity_table = 'civicrm_contribution_page' AND cpse.entity_id = cc.contribution_page_id
614 WHERE (cc.id BETWEEN %1 AND %2)
615 AND cli.entity_id IS NULL AND cc.contribution_page_id IS NOT NULL AND cpp.contribution_id IS NULL
616 GROUP BY cc.id, cmp.membership_id
617 ";
618 $result = CRM_Core_DAO::executeQuery($sql, $sqlParams);
619
620 while ($result->fetch()) {
621 $sql = "
622 SELECT cpf.id, cpfv.id as price_field_value_id, cpfv.label, cpfv.amount, cpfv.count
623 FROM civicrm_price_field cpf
624 LEFT JOIN civicrm_price_field_value cpfv ON cpf.id = cpfv.price_field_id
625 WHERE cpf.price_set_id = %1
626 ";
627 $lineParams = array(
628 'entity_table' => 'civicrm_contribution',
629 'entity_id' => $result->id,
630 );
631 if ($result->membership_id) {
632 $sql .= " AND cpf.name = %2 AND cpfv.membership_type_id = %3 ";
633 $params = array(
634 '1' => array($result->price_set_id, 'Integer'),
635 '2' => array('membership_amount', 'String'),
636 '3' => array(
637 CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $result->membership_id, 'membership_type_id'),
638 'Integer',
639 ),
640 );
641 $res = CRM_Core_DAO::executeQuery($sql, $params);
642 if ($res->fetch()) {
643 $lineParams += array(
644 'price_field_id' => $res->id,
645 'label' => $res->label,
646 'qty' => 1,
647 'unit_price' => $res->amount,
648 'line_total' => $res->amount,
649 'participant_count' => $res->count ? $res->count : 0,
650 'price_field_value_id' => $res->price_field_value_id,
651 );
652 }
653 else {
654 $lineParams['price_field_id'] = CRM_Core_DAO::getFieldValue('CRM_Upgrade_Snapshot_V4p2_Price_DAO_Field', $result->price_set_id, 'id', 'price_set_id');
655 $lineParams['label'] = 'Membership Amount';
656 $lineParams['qty'] = 1;
657 $lineParams['unit_price'] = $lineParams['line_total'] = $result->total_amount;
658 $lineParams['participant_count'] = 0;
659 }
660 }
661 else {
662 $sql .= "AND cpfv.amount = %2";
663
664 //CRM-12273
665 //check if price_set_id is exist, if not use the default contribution amount
666 if (isset($result->price_set_id)) {
667 $priceSetId = $result->price_set_id;
668 }
669 else {
670 $defaultPriceSets = CRM_Price_BAO_PriceSet::getDefaultPriceSet();
671 foreach ($defaultPriceSets as $key => $pSet) {
672 if ($pSet['name'] == 'contribution_amount') {
673 $priceSetId = $pSet['setID'];
674 }
675 }
676 }
677
678 $params = array(
679 '1' => array($priceSetId, 'Integer'),
680 '2' => array($result->total_amount, 'String'),
681 );
682 $res = CRM_Core_DAO::executeQuery($sql, $params);
683 if ($res->fetch()) {
684 $lineParams += array(
685 'price_field_id' => $res->id,
686 'label' => $res->label,
687 'qty' => 1,
688 'unit_price' => $res->amount,
689 'line_total' => $res->amount,
690 'participant_count' => $res->count ? $res->count : 0,
691 'price_field_value_id' => $res->price_field_value_id,
692 );
693 }
694 else {
695 $params = array(
696 'price_set_id' => $priceSetId,
697 'name' => 'other_amount',
698 );
699 $defaults = array();
700 CRM_Upgrade_Snapshot_V4p2_Price_BAO_Field::retrieve($params, $defaults);
701 if (!empty($defaults)) {
702 $lineParams['price_field_id'] = $defaults['id'];
703 $lineParams['label'] = $defaults['label'];
704 $lineParams['price_field_value_id']
705 = CRM_Core_DAO::getFieldValue('CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue', $defaults['id'], 'id', 'price_field_id');
706 }
707 else {
708 $lineParams['price_field_id']
709 = CRM_Core_DAO::getFieldValue('CRM_Upgrade_Snapshot_V4p2_Price_DAO_Field', $priceSetId, 'id', 'price_set_id');
710 $lineParams['label'] = 'Contribution Amount';
711 }
712 $lineParams['qty'] = 1;
713 $lineParams['participant_count'] = 0;
714 $lineParams['unit_price'] = $lineParams['line_total'] = $result->total_amount;
715 }
716 }
717 CRM_Upgrade_Snapshot_V4p2_Price_BAO_LineItem::create($lineParams);
718 }
719
720 return TRUE;
721 }
722
723 /**
724 * (Queue Task Callback)
725 *
726 * Find any participant records and create corresponding line-item
727 * records.
728 *
729 * @param CRM_Queue_TaskContext $ctx
730 * @param int $startId
731 * the first/lowest participant ID to convert.
732 * @param int $endId
733 * the last/highest participant ID to convert.
734 *
735 * @return bool
736 */
737 public static function task_4_2_alpha1_convertParticipants(CRM_Queue_TaskContext $ctx, $startId, $endId) {
738 $upgrade = new CRM_Upgrade_Form();
739 //create lineitems for participant in edge cases using default price set for contribution.
740 $query = "
741 SELECT cp.id as participant_id, cp.fee_amount, cp.fee_level,ce.is_monetary,
742 cpse.price_set_id, cpf.id as price_field_id, cpfv.id as price_field_value_id
743 FROM civicrm_participant cp
744 LEFT JOIN civicrm_line_item cli ON cli.entity_id=cp.id and cli.entity_table = 'civicrm_participant'
745 LEFT JOIN civicrm_event ce ON ce.id=cp.event_id
746 LEFT JOIN civicrm_price_set_entity cpse ON cp.event_id = cpse.entity_id and cpse.entity_table = 'civicrm_event'
747 LEFT JOIN civicrm_price_field cpf ON cpf.price_set_id = cpse.price_set_id
748 LEFT JOIN civicrm_price_field_value cpfv ON cpfv.price_field_id = cpf.id AND cpfv.label = cp.fee_level
749 WHERE (cp.id BETWEEN %1 AND %2)
750 AND cli.entity_id IS NULL AND cp.fee_amount IS NOT NULL";
751 $sqlParams = array(
752 1 => array($startId, 'Integer'),
753 2 => array($endId, 'Integer'),
754 );
755 $dao = CRM_Core_DAO::executeQuery($query, $sqlParams);
756 if ($dao->N) {
757 $defaultPriceSetId = CRM_Core_DAO::getFieldValue('CRM_Upgrade_Snapshot_V4p2_Price_DAO_Set', 'default_contribution_amount', 'id', 'name');
758 $priceSets = current(CRM_Upgrade_Snapshot_V4p2_Price_BAO_Set::getSetDetail($defaultPriceSetId));
759 $fieldID = key($priceSets['fields']);
760 }
761
762 while ($dao->fetch()) {
763 $lineParams = array(
764 'entity_table' => 'civicrm_participant',
765 'entity_id' => $dao->participant_id,
766 'label' => $dao->fee_level ? $dao->fee_level : ts('Default'),
767 'qty' => 1,
768 'unit_price' => $dao->fee_amount,
769 'line_total' => $dao->fee_amount,
770 'participant_count' => 1,
771 );
772 if ($dao->is_monetary && $dao->price_field_id) {
773 $lineParams += array(
774 'price_field_id' => $dao->price_field_id,
775 'price_field_value_id' => $dao->price_field_value_id,
776 );
777 $priceSetId = $dao->price_set_id;
778 }
779 else {
780 $lineParams['price_field_id'] = $fieldID;
781 $priceSetId = $defaultPriceSetId;
782 }
783 CRM_Upgrade_Snapshot_V4p2_Price_BAO_LineItem::create($lineParams);
784 }
785 return TRUE;
786 }
787
788 /**
789 * (Queue Task Callback)
790 *
791 * Create an event registration profile with a single email field CRM-9587
792 *
793 * @param \CRM_Queue_TaskContext $ctx
794 *
795 * @return bool
796 */
797 public static function task_4_2_alpha1_eventProfile(CRM_Queue_TaskContext $ctx) {
798 $upgrade = new CRM_Upgrade_Form();
799 $profileTitle = ts('Your Registration Info');
800
801 $sql = "
802 INSERT INTO civicrm_uf_group
803 (is_active, group_type, title, help_pre, help_post, limit_listings_group_id, post_URL, add_to_group_id, add_captcha, is_map, is_edit_link, is_uf_link, is_update_dupe, cancel_URL, is_cms_user, notify, is_reserved, name, created_id, created_date, is_proximity_search)
804 VALUES
805 (1, 'Individual, Contact', %1, NULL, NULL, NULL, NULL, NULL, 0, 0, 0, 0, 0, NULL, 0, NULL, 0, 'event_registration', NULL, NULL, 0);
806 ";
807
808 $params = array(
809 1 => array($profileTitle, 'String'),
810 );
811
812 CRM_Core_DAO::executeQuery($sql, $params);
813
814 $eventRegistrationId = CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()');
815 $sql = "
816 INSERT INTO civicrm_uf_field
817 (uf_group_id, field_name, is_active, is_view, is_required, weight, help_post, help_pre, visibility, in_selector, is_searchable, location_type_id, phone_type_id, label, field_type, is_reserved)
818 VALUES
819 ({$eventRegistrationId}, 'email', 1, 0, 1, 1, NULL, NULL, 'User and User Admin Only', 0, 0, NULL, NULL, 'Email Address', 'Contact', 0);
820 ";
821 CRM_Core_DAO::executeQuery($sql);
822
823 $sql = "SELECT * FROM civicrm_event WHERE is_online_registration = 1;";
824 $events = CRM_Core_DAO::executeQuery($sql);
825 while ($events->fetch()) {
826 // Get next weights for the event registration profile
827 $nextMainWeight = $nextAdditionalWeight = 1;
828 $sql = "
829 SELECT weight
830 FROM civicrm_uf_join
831 WHERE entity_id = {$events->id} AND module = 'CiviEvent'
832 ORDER BY weight DESC LIMIT 1";
833 $weights = CRM_Core_DAO::executeQuery($sql);
834 $weights->fetch();
835 if (isset($weights->weight)) {
836 $nextMainWeight += $weights->weight;
837 }
838 $sql = "
839 SELECT weight
840 FROM civicrm_uf_join
841 WHERE entity_id = {$events->id} AND module = 'CiviEvent_Additional'
842 ORDER BY weight DESC LIMIT 1";
843 $weights = CRM_Core_DAO::executeQuery($sql);
844 $weights->fetch();
845 if (isset($weights->weight)) {
846 $nextAdditionalWeight += $weights->weight;
847 }
848 // Add an event registration profile to the event
849 $sql = "
850 INSERT INTO civicrm_uf_join
851 (is_active, module, entity_table, entity_id, weight, uf_group_id)
852 VALUES
853 (1, 'CiviEvent', 'civicrm_event', {$events->id}, {$nextMainWeight}, {$eventRegistrationId});
854 ";
855 CRM_Core_DAO::executeQuery($sql);
856 $sql = "
857 INSERT INTO civicrm_uf_join
858 (is_active, module, entity_table, entity_id, weight, uf_group_id)
859 VALUES
860 (1, 'CiviEvent_Additional', 'civicrm_event', {$events->id}, {$nextAdditionalWeight}, {$eventRegistrationId});";
861 CRM_Core_DAO::executeQuery($sql);
862 }
863 return TRUE;
864 }
865
866 /**
867 * @return array
868 */
869 public static function deleteInvalidPairs() {
870 require_once 'CRM/Member/PseudoConstant.php';
871 require_once 'CRM/Contribute/PseudoConstant.php';
872 $processedRecords = array();
873
874 $tempTableName1 = CRM_Core_DAO::createTempTableName();
875 // 1. collect all duplicates
876 $sql = "
877 CREATE TEMPORARY TABLE {$tempTableName1} SELECT mp.id as payment_id, mp.contribution_id, mp.membership_id, mem.membership_type_id, mem.start_date, mem.end_date, mem.status_id, mem.contact_id, con.contribution_status_id
878 FROM civicrm_membership_payment mp
879 INNER JOIN ( SELECT cmp.contribution_id
880 FROM civicrm_membership_payment cmp
881 LEFT JOIN civicrm_line_item cli ON cmp.contribution_id=cli.entity_id and cli.entity_table = 'civicrm_contribution'
882 WHERE cli.entity_id IS NULL
883 GROUP BY cmp.contribution_id
884 HAVING COUNT(cmp.membership_id) > 1) submp ON submp.contribution_id = mp.contribution_id
885 INNER JOIN civicrm_membership mem ON mem.id = mp.membership_id
886 INNER JOIN civicrm_contribution con ON con.id = mp.contribution_id
887 ORDER BY mp.contribution_id, mp.membership_id";
888 $dao = CRM_Core_DAO::executeQuery($sql);
889
890 $tempTableName2 = CRM_Core_DAO::createTempTableName();
891 // 2. collect all records that are going to be retained
892 $sql = "
893 CREATE TEMPORARY TABLE {$tempTableName2}
894 SELECT MAX(payment_id) as payment_id FROM {$tempTableName1} GROUP BY contribution_id HAVING COUNT(*) > 1";
895 CRM_Core_DAO::executeQuery($sql);
896
897 // 3. do the un-linking
898 $sql = "
899 DELETE cmp.*
900 FROM civicrm_membership_payment cmp
901 INNER JOIN $tempTableName1 temp1 ON temp1.payment_id = cmp.id
902 LEFT JOIN $tempTableName2 temp2 ON temp1.payment_id = temp2.payment_id
903 WHERE temp2.payment_id IS NULL";
904 CRM_Core_DAO::executeQuery($sql);
905
906 // 4. show all records that were Processed, i.e Retained vs Un-linked
907 $sql = "
908 SELECT temp1.contact_id, temp1.contribution_id, temp1.contribution_status_id, temp1.membership_id, temp1.membership_type_id, temp1.start_date, temp1.end_date, temp1.status_id, temp2.payment_id as retain_id
909 FROM $tempTableName1 temp1
910 LEFT JOIN $tempTableName2 temp2 ON temp1.payment_id = temp2.payment_id";
911 $dao = CRM_Core_DAO::executeQuery($sql);
912 if ($dao->N) {
913 $membershipType = CRM_Member_PseudoConstant::membershipType();
914 $membershipStatus = CRM_Member_PseudoConstant::membershipStatus();
915 $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus();
916 while ($dao->fetch()) {
917 $status = $dao->retain_id ? 'Retained' : 'Un-linked';
918 $memType = CRM_Utils_Array::value($dao->membership_type_id, $membershipType);
919 $memStatus = CRM_Utils_Array::value($dao->status_id, $membershipStatus);
920 $contribStatus = CRM_Utils_Array::value($dao->contribution_status_id, $contributionStatus);
921 $processedRecords[] = array(
922 $dao->contact_id,
923 $dao->contribution_id,
924 $contribStatus,
925 $dao->membership_id,
926 $memType,
927 $dao->start_date,
928 $dao->end_date,
929 $memStatus,
930 $status,
931 );
932 }
933 }
934
935 if (!empty($processedRecords)) {
936 CRM_Core_Error::debug_log_message("deleteInvalidPairs() - The following records have been processed. Membership records with action:");
937 CRM_Core_Error::debug_log_message("Contact ID, ContributionID, Contribution Status, MembershipID, Membership Type, Start Date, End Date, Membership Status, Action");
938 foreach ($processedRecords as $record) {
939 CRM_Core_Error::debug_log_message(implode(', ', $record));
940 }
941 }
942 else {
943 CRM_Core_Error::debug_log_message("deleteInvalidPairs() - Could not find any records to process.");
944 }
945 return $processedRecords;
946 }
947
948 }