ts() usage fixes. c.f. http://wiki.civicrm.org/confluence/display/CRMDOC43/Internatio...
[civicrm-core.git] / CRM / Upgrade / Incremental / php / FourThree.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_FourThree {
36 const BATCH_SIZE = 5000;
37
38 function verifyPreDBstate(&$errors) {
39 return TRUE;
40 }
41
42 /**
43 * Compute any messages which should be displayed beforeupgrade
44 *
45 * Note: This function is called iteratively for each upcoming
46 * revision to the database.
47 *
48 * @param $postUpgradeMessage string, alterable
49 * @param $rev string, a version number, e.g. '4.3.alpha1', '4.3.beta3', '4.3.0'
50 * @return void
51 */
52 function setPreUpgradeMessage(&$preUpgradeMessage, $rev, $currentVer = NULL) {
53 if ($rev == '4.3.beta3') {
54 //CRM-12084
55 //sql for checking orphaned contribution records
56 $sql = "SELECT COUNT(ct.id) FROM civicrm_contribution ct LEFT JOIN civicrm_contact c ON ct.contact_id = c.id WHERE c.id IS NULL";
57 $count = CRM_Core_DAO::singleValueQuery($sql, array(), TRUE, FALSE);
58
59 if ($count > 0) {
60 $error = ts("There is a data integrity issue with this CiviCRM database. It contains %1 contribution records which are linked to contact records that have been deleted. You will need to correct this manually before you can run the upgrade. Use the following MySQL query to identify the problem records: %2 These records will need to be deleted or linked to an existing contact record.", array(1 => $count, 2 => '<em>SELECT ct.* FROM civicrm_contribution ct LEFT JOIN civicrm_contact c ON ct.contact_id = c.id WHERE c.id IS NULL;</em>'));
61 CRM_Core_Error::fatal($error);
62 return FALSE;
63 }
64 }
65 if ($rev == '4.3.beta4' && CRM_Utils_Constant::value('CIVICRM_UF', FALSE) == 'Drupal6') {
66 // CRM-11823 - Make sure the D6 HTML HEAD technique will work on upgrade pages
67 theme('item_list', array()); // force-load theme registry
68 $theme_registry = theme_get_registry();
69 if (
70 !isset($theme_registry['page']['preprocess functions']) ||
71 FALSE === array_search('civicrm_preprocess_page_inject', $theme_registry['page']['preprocess functions'])
72 ) {
73 CRM_Core_Error::fatal('Please reset the Drupal cache (Administer => Site Configuration => Performance => Clear cached data))');
74 }
75 }
76 }
77
78 /**
79 * Compute any messages which should be displayed after upgrade
80 *
81 * @param $postUpgradeMessage string, alterable
82 * @param $rev string, an intermediate version; note that setPostUpgradeMessage is called repeatedly with different $revs
83 * @return void
84 */
85 function setPostUpgradeMessage(&$postUpgradeMessage, $rev) {
86 if ($rev == '4.3.alpha1') {
87 // check if CiviMember component is enabled
88 $config = CRM_Core_Config::singleton();
89 if (in_array('CiviMember', $config->enableComponents)) {
90 $postUpgradeMessage .= '<br />' . ts('Membership renewal reminders must now be configured using the Schedule Reminders feature, which supports multiple renewal reminders (Administer > Communications > Schedule Reminders). The Update Membership Statuses scheduled job will no longer send membershp renewal reminders. You can use your existing renewal reminder message template(s) with the Schedule Reminders feature.');
91 $postUpgradeMessage .= '<br />' . ts('The Set Membership Reminder Dates scheduled job has been deleted since membership reminder dates stored in the membership table are no longer in use.');
92 }
93
94 //CRM-11636
95 //here we do the financial type check and migration
96 $isDefaultsModified = self::_checkAndMigrateDefaultFinancialTypes();
97 if($isDefaultsModified) {
98 $postUpgradeMessage .= '<br />' . ts('Please review all price set financial type assignments.');
99 }
100 list($context, $orgName) = self::createDomainContacts();
101 if ($context == 'added') {
102 $postUpgradeMessage .= '<br />' . ts("A new organization contact has been added as the default domain contact using the information from your Organization Address and Contact Info settings: '%1'.", array(1 => $orgName));
103 }
104 elseif ($context == 'merged') {
105 $postUpgradeMessage .= '<br />' . ts("The existing organization contact record for '%1' has been marked as the default domain contact, and has been updated with information from your Organization Address and Contact Info settings.", array(1 => $orgName));
106 }
107
108 $providerExists = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_sms_provider LIMIT 1");
109 if ($providerExists) {
110 $postUpgradeMessage .= '<br />' . ts('SMS providers were found to setup. Please note Clickatell / Twilio are now shipped as extensions and will require installing them to continue working. Extension could be downloaded and installed from <a href="%1">github</a>.', array(1 => 'https://github.com/civicrm/civicrm-core/tree/master/tools/extensions'));
111 }
112 }
113
114 if ($rev == '4.3.alpha2') {
115 $sql = "
116 SELECT title, id
117 FROM civicrm_action_schedule
118 WHERE entity_value = '' OR entity_value IS NULL
119 ";
120
121 $dao = CRM_Core_DAO::executeQuery($sql);
122 $reminder = array();
123 $list = '';
124 while ($dao->fetch()) {
125 $reminder[$dao->id] = $dao->title;
126 $list .= "<li>{$dao->title}</li>";
127 }
128 if (!empty($reminder)) {
129 $list = "<br /><ul>" . $list . "</ul>";
130 $postUpgradeMessage .= '<br />' .ts("Scheduled Reminders must be linked to one or more 'entities' (Events, Event Templates, Activity Types, Membership Types). The following reminders are not configured properly and will not be run. Please review them and update or delete them: %1", array(1 => $list));
131 }
132 }
133
134 if ($rev == '4.3.beta2') {
135 $postUpgradeMessage .= '<br />' . ts('Default versions of the following System Workflow Message Templates have been modified to handle new functionality: <ul><li>Events - Registration Confirmation and Receipt (on-line)</li><li>Events - Registration Confirmation and Receipt (off-line)</li><li>Pledges - Acknowledgement</li><li>Pledges - Payment Reminder</li><li>Contributions - Receipt (off-line)</li><li>Contributions - Receipt (on-line)</li><li>Memberships - Signup and Renewal Receipts (off-line)</li><li>Memberships - Receipt (on-line)</li><li>Personal Campaign Pages - Admin Notification</li></ul> If you have modified these templates, please review the new default versions and implement updates as needed to your copies (Administer > Communications > Message Templates > System Workflow Messages).');
136 }
137
138 if ($rev == '4.3.beta5') {
139 $postUpgradeMessage .= '<br />' . ts("If you are interested in trying out the new Accounting Integration features, please review user permissions and assign the new 'manual batch' permissions as appropriate.");
140
141 // CRM-12155
142 $query = "SELECT ceft.id FROM `civicrm_financial_trxn` cft
143 LEFT JOIN civicrm_entity_financial_trxn ceft
144 ON ceft.financial_trxn_id = cft.id AND ceft.entity_table = 'civicrm_contribution'
145 LEFT JOIN civicrm_contribution cc ON cc.id = ceft.entity_id AND ceft.entity_table = 'civicrm_contribution'
146 WHERE cc.id IS NULL";
147
148 $dao = CRM_Core_DAO::executeQuery($query);
149 $isOrphanData = TRUE;
150 if (!$dao->N) {
151 $query = "SELECT cli.id FROM civicrm_line_item cli
152 LEFT JOIN civicrm_contribution cc ON cli.entity_id = cc.id AND cli.entity_table = 'civicrm_contribution'
153 LEFT JOIN civicrm_participant cp ON cli.entity_id = cp.id AND cli.entity_table = 'civicrm_participant'
154 WHERE CASE WHEN cli.entity_table = 'civicrm_contribution'
155 THEN cc.id IS NULL
156 ELSE cp.id IS NULL
157 END";
158 $dao = CRM_Core_DAO::executeQuery($query);
159 if (!$dao->N) {
160 $revPattern = '/^((\d{1,2})\.\d{1,2})\.(\d{1,2}|\w{4,7})?$/i';
161 preg_match($revPattern, $currentVer, $version);
162 if ($version[1] >= 4.3) {
163 $query = "SELECT cfi.id FROM civicrm_financial_item cfi
164 LEFT JOIN civicrm_entity_financial_trxn ceft ON ceft.entity_table = 'civicrm_financial_item' and cfi.id = ceft.entity_id
165 WHERE ceft.entity_id IS NULL;";
166 $dao = CRM_Core_DAO::executeQuery($query);
167 if (!$dao->N) {
168 $isOrphanData = FALSE;
169 }
170 }
171 else {
172 $isOrphanData = FALSE;
173 }
174 }
175 }
176
177 if ($isOrphanData) {
178 $postUpgradeMessage .= "</br> <strong>" . ts('Your database contains extraneous financial records related to deleted contacts and contributions. These records should not affect the site and will not appear in reports, search results or exports. However you may wish to clean them up. Refer to <a href="%1">this wiki page for details</a>.
179 ', array( 1 => 'http://wiki.civicrm.org/confluence/display/CRMDOC/Clean+up+extraneous+financial+data+-+4.3+upgrades')) . "</strong>";
180 }
181 }
182 }
183
184 function upgrade_4_3_alpha1($rev) {
185 self::task_4_3_alpha1_checkDBConstraints();
186
187 // add indexes for civicrm_entity_financial_trxn
188 // CRM-12141
189 $this->addTask(ts('Check/Add indexes for civicrm_entity_financial_trxn'), 'task_4_3_x_checkIndexes', $rev);
190 // task to process sql
191 $this->addTask(ts('Upgrade DB to 4.3.alpha1: SQL'), 'task_4_3_x_runSql', $rev);
192
193 //CRM-11636
194 $this->addTask(ts('Populate financial type values for price records'), 'assignFinancialTypeToPriceRecords');
195 //CRM-11514 create financial records for contributions
196 $this->addTask(ts('Create financial records for contributions'), 'createFinancialRecords');
197
198 $minId = CRM_Core_DAO::singleValueQuery('SELECT coalesce(min(id),0) FROM civicrm_contact');
199 $maxId = CRM_Core_DAO::singleValueQuery('SELECT coalesce(max(id),0) FROM civicrm_contact');
200 for ($startId = $minId; $startId <= $maxId; $startId += self::BATCH_SIZE) {
201 $endId = $startId + self::BATCH_SIZE - 1;
202 $title = ts('Upgrade timestamps (%1 => %2)', array(1 => $startId, 2 => $endId));
203 $this->addTask($title, 'convertTimestamps', $startId, $endId);
204 }
205
206 // CRM-10893
207 // fix WP access control
208 $config = CRM_Core_Config::singleton( );
209 if ($config->userFramework == 'WordPress') {
210 civicrm_wp_set_capabilities( );
211 }
212
213 // Update phones CRM-11292.
214 $this->addTask(ts('Upgrade Phone Numbers'), 'phoneNumeric');
215
216 return TRUE;
217 }
218
219 function upgrade_4_3_alpha2($rev) {
220 //CRM-11847
221 $isColumnPresent = CRM_Core_DAO::checkFieldExists('civicrm_dedupe_rule_group', 'is_default');
222 if ($isColumnPresent) {
223 CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_dedupe_rule_group DROP COLUMN is_default');
224 }
225 $this->addTask(ts('Upgrade DB to 4.3.alpha2: SQL'), 'task_4_3_x_runSql', $rev);
226 }
227
228 function upgrade_4_3_alpha3($rev) {
229 $this->addTask(ts('Upgrade DB to 4.3.alpha3: SQL'), 'task_4_3_x_runSql', $rev);
230 }
231
232 function upgrade_4_3_beta2($rev) {
233 $this->addTask(ts('Upgrade DB to 4.3.beta2: SQL'), 'task_4_3_x_runSql', $rev);
234
235 // CRM-12002
236 if (
237 CRM_Core_DAO::checkTableExists('log_civicrm_line_item') &&
238 CRM_Core_DAO::checkFieldExists('log_civicrm_line_item', 'label')
239 ) {
240 CRM_Core_DAO::executeQuery('ALTER TABLE `log_civicrm_line_item` CHANGE `label` `label` VARCHAR(255) NULL DEFAULT NULL');
241 }
242 }
243
244 function upgrade_4_3_beta3($rev) {
245 $this->addTask(ts('Upgrade DB to 4.3.beta3: SQL'), 'task_4_3_x_runSql', $rev);
246 // CRM-12065
247 $query = "SELECT id, form_values FROM civicrm_report_instance WHERE form_values LIKE '%contribution_type%'";
248 $this->addTask('Replace contribution_type to financial_type in table civicrm_report_instance', 'replaceContributionTypeId', $query, 'reportInstance');
249 $query = "SELECT * FROM civicrm_saved_search WHERE form_values LIKE '%contribution_type%'";
250 $this->addTask('Replace contribution_type to financial_type in table civicrm_saved_search', 'replaceContributionTypeId', $query, 'savedSearch');
251 }
252
253 function upgrade_4_3_beta4($rev) {
254 $this->addTask(ts('Upgrade DB to 4.3.beta4: SQL'), 'task_4_3_x_runSql', $rev);
255 // add indexes for civicrm_entity_financial_trxn
256 // CRM-12141
257 $this->addTask(ts('Check/Add indexes for civicrm_entity_financial_trxn'), 'task_4_3_x_checkIndexes', $rev);
258 }
259
260 function upgrade_4_3_beta5($rev) {
261 // CRM-12205
262 if (
263 CRM_Core_DAO::checkTableExists('log_civicrm_financial_trxn') &&
264 CRM_Core_DAO::checkFieldExists('log_civicrm_financial_trxn', 'trxn_id')
265 ) {
266 CRM_Core_DAO::executeQuery('ALTER TABLE `log_civicrm_financial_trxn` CHANGE `trxn_id` `trxn_id` VARCHAR(255) NULL DEFAULT NULL');
267 }
268 // CRM-12142 - some sites didn't get this column added yet, and sites which installed 4.3 from scratch will already have it
269 if (
270 !CRM_Core_DAO::checkFieldExists('civicrm_premiums', 'premiums_nothankyou_label')
271 ) {
272 CRM_Core_DAO::executeQuery('ALTER TABLE `civicrm_premiums` ADD COLUMN premiums_nothankyou_label varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT "Label displayed for No Thank-you option in premiums block (e.g. No thank you)"');
273 }
274 $this->addTask(ts('Upgrade DB to 4.3.beta5: SQL'), 'task_4_3_x_runSql', $rev);
275 }
276
277 //CRM-11636
278 function assignFinancialTypeToPriceRecords() {
279 $upgrade = new CRM_Upgrade_Form();
280 //here we update price set entries
281 $sqlFinancialIds = "SELECT id, name FROM civicrm_financial_type
282 WHERE name IN ('Donation', 'Event Fee', 'Member Dues');";
283 $daoFinancialIds = CRM_Core_DAO::executeQuery($sqlFinancialIds);
284 while($daoFinancialIds->fetch()) {
285 $financialIds[$daoFinancialIds->name] = $daoFinancialIds->id;
286 }
287 $sqlPriceSetUpdate = "UPDATE civicrm_price_set ps
288 SET ps.financial_type_id = CASE
289 WHEN ps.extends LIKE '%1%' THEN {$financialIds['Event Fee']}
290 WHEN ps.extends LIKE '2' THEN {$financialIds['Donation']}
291 WHEN ps.extends LIKE '3' THEN {$financialIds['Member Dues']}
292 END
293 WHERE financial_type_id IS NULL";
294 CRM_Core_DAO::executeQuery($sqlPriceSetUpdate);
295
296 //here we update price field value rows
297 $sqlPriceFieldValueUpdate = "UPDATE civicrm_price_field_value pfv
298 LEFT JOIN civicrm_membership_type mt ON (pfv.membership_type_id = mt.id)
299 INNER JOIN civicrm_price_field pf ON (pfv.price_field_id = pf.id)
300 INNER JOIN civicrm_price_set ps ON (pf.price_set_id = ps.id)
301 SET pfv.financial_type_id = CASE
302 WHEN pfv.membership_type_id IS NOT NULL THEN mt.financial_type_id
303 WHEN pfv.membership_type_id IS NULL THEN ps.financial_type_id
304 END";
305 CRM_Core_DAO::executeQuery($sqlPriceFieldValueUpdate);
306
307 return TRUE;
308 }
309
310 static function _checkAndMigrateDefaultFinancialTypes() {
311 $modifiedDefaults = FALSE;
312 //insert types if not exists
313 $sqlFetchTypes = "SELECT id, name FROM civicrm_contribution_type
314 WHERE name IN ('Donation', 'Event Fee', 'Member Dues') AND is_active =1;";
315 $daoFetchTypes = CRM_Core_DAO::executeQuery($sqlFetchTypes);
316
317 if ($daoFetchTypes->N < 3) {
318 $modifiedDefaults = TRUE;
319 $insertStatments = array (
320 'Donation' => "('Donation', 0, 1, 1)",
321 'Member' => "('Member Dues', 0, 1, 1)",
322 'Event Fee' => "('Event Fee', 0, 1, 0)",
323 );
324 foreach ($insertStatments as $values) {
325 $query = "INSERT INTO civicrm_contribution_type (name, is_reserved, is_active, is_deductible)
326 VALUES $values
327 ON DUPLICATE KEY UPDATE is_active = 1;";
328 CRM_Core_DAO::executeQuery($query);
329 }
330 }
331 return $modifiedDefaults;
332 }
333
334 function createFinancialRecords() {
335 $upgrade = new CRM_Upgrade_Form();
336
337 // update civicrm_entity_financial_trxn.amount = civicrm_financial_trxn.total_amount
338 $query = "UPDATE civicrm_entity_financial_trxn ceft
339 LEFT JOIN civicrm_financial_trxn cft ON cft.id = ceft.financial_trxn_id
340 SET ceft.amount = total_amount
341 WHERE cft.net_amount IS NOT NULL AND ceft.entity_table = 'civicrm_contribution';";
342 CRM_Core_DAO::executeQuery($query);
343
344 $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
345 $completedStatus = array_search('Completed', $contributionStatus);
346 $pendingStatus = array_search('Pending', $contributionStatus);
347 $cancelledStatus = array_search('Cancelled', $contributionStatus);
348 $queryParams = array(
349 1 => array($completedStatus, 'Integer'),
350 2 => array($pendingStatus, 'Integer'),
351 3 => array($cancelledStatus, 'Integer')
352 );
353
354 $accountType = key(CRM_Core_PseudoConstant::accountOptionValues('financial_account_type', NULL, " AND v.name = 'Asset' "));
355 $financialAccountId =
356 CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_financial_account WHERE is_default = 1 AND financial_account_type_id = {$accountType}");
357
358 $accountRelationsips = CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL);
359
360 $accountsReceivableAccount = array_search('Accounts Receivable Account is', $accountRelationsips);
361 $incomeAccountIs = array_search('Income Account is', $accountRelationsips);
362 $assetAccountIs = array_search('Asset Account is', $accountRelationsips);
363 $expenseAccountIs = array_search('Expense Account is', $accountRelationsips);
364
365 $financialItemStatus = CRM_Core_PseudoConstant::accountOptionValues('financial_item_status');
366 $unpaidStatus = array_search('Unpaid', $financialItemStatus);
367 $paidStatus = array_search('Paid', $financialItemStatus);
368
369 $validCurrencyCodes = CRM_Core_PseudoConstant::currencyCode();
370 $validCurrencyCodes = implode("','", $validCurrencyCodes);
371 $config = CRM_Core_Config::singleton();
372 $defaultCurrency = $config->defaultCurrency;
373 $now = date( 'YmdHis' );
374
375 //adding financial_trxn records and entity_financial_trxn records related to contribution
376 //Add temp column for easy entry in entity_financial_trxn
377 $sql = "ALTER TABLE civicrm_financial_trxn ADD COLUMN contribution_id INT DEFAULT NULL";
378 CRM_Core_DAO::executeQuery($sql);
379
380 //pending pay later status handling
381 $sql = "
382 INSERT INTO civicrm_financial_trxn
383 (contribution_id, payment_instrument_id, currency, total_amount, net_amount, fee_amount, trxn_id, status_id,
384 check_number, to_financial_account_id, from_financial_account_id, trxn_date)
385
386 SELECT con.id as contribution_id, con.payment_instrument_id, IF(con.currency IN ('{$validCurrencyCodes}'), con.currency, '{$defaultCurrency}')
387 as currency, con.total_amount, con.net_amount, con.fee_amount, con.trxn_id, con.contribution_status_id,
388 con.check_number, efa.financial_account_id as to_financial_account_id, NULL as from_financial_account_id,
389 REPLACE(REPLACE(REPLACE(
390 CASE
391 WHEN con.receive_date IS NOT NULL THEN
392 con.receive_date
393 WHEN con.receipt_date IS NOT NULL THEN
394 con.receipt_date
395 ELSE
396 {$now}
397 END
398 , '-', ''), ':', ''), ' ', '') as trxn_date
399 FROM civicrm_contribution con
400 LEFT JOIN civicrm_entity_financial_account efa
401 ON (con.financial_type_id = efa.entity_id AND efa.entity_table = 'civicrm_financial_type'
402 AND efa.account_relationship = {$accountsReceivableAccount})
403 WHERE con.is_pay_later = 1 AND con.contribution_status_id = {$pendingStatus}";
404 CRM_Core_DAO::executeQuery($sql);
405
406 //create a temp table to hold financial account id related to payment instruments
407 $tempTableName1 = CRM_Core_DAO::createTempTableName();
408
409 $sql = "CREATE TEMPORARY TABLE {$tempTableName1}
410
411 SELECT ceft.financial_account_id financial_account_id, cov.value as instrument_id
412 FROM civicrm_entity_financial_account ceft
413 INNER JOIN civicrm_option_value cov ON cov.id = ceft.entity_id AND ceft.entity_table = 'civicrm_option_value'
414 INNER JOIN civicrm_option_group cog ON cog.id = cov.option_group_id
415 WHERE cog.name = 'payment_instrument'";
416 CRM_Core_DAO::executeQuery($sql);
417 //CRM-12141
418 $sql = "ALTER TABLE {$tempTableName1} ADD INDEX index_instrument_id (instrument_id);";
419 CRM_Core_DAO::executeQuery($sql);
420
421 //create temp table to process completed / cancelled contribution
422 $tempTableName2 = CRM_Core_DAO::createTempTableName();
423 $sql = "CREATE TEMPORARY TABLE {$tempTableName2}
424
425 SELECT con.id as contribution_id, con.payment_instrument_id, IF(con.currency IN ('{$validCurrencyCodes}'), con.currency, '{$defaultCurrency}') as currency,
426 con.total_amount, con.net_amount, con.fee_amount, con.trxn_id, con.contribution_status_id, con.check_number, NULL as from_financial_account_id,
427 REPLACE(REPLACE(REPLACE(
428 CASE
429 WHEN con.receive_date IS NOT NULL THEN
430 con.receive_date
431 WHEN con.receipt_date IS NOT NULL THEN
432 con.receipt_date
433 ELSE
434 {$now}
435 END
436 , '-', ''), ':', ''), ' ', '') as trxn_date,
437 CASE
438 WHEN con.payment_instrument_id IS NULL THEN
439 {$financialAccountId}
440 WHEN con.payment_instrument_id IS NOT NULL THEN
441 tpi.financial_account_id
442 END as to_financial_account_id,
443 IF(eft.financial_trxn_id IS NULL, 'insert', eft.financial_trxn_id) as action
444 FROM civicrm_contribution con
445 LEFT JOIN civicrm_entity_financial_trxn eft
446 ON (eft.entity_table = 'civicrm_contribution' AND eft.entity_id = con.id)
447 LEFT JOIN {$tempTableName1} tpi
448 ON con.payment_instrument_id = tpi.instrument_id
449 WHERE con.contribution_status_id IN ({$completedStatus}, {$cancelledStatus})";
450 CRM_Core_DAO::executeQuery($sql);
451 // CRM-12141
452 $sql = "ALTER TABLE {$tempTableName2} ADD INDEX index_action (action);";
453 CRM_Core_DAO::executeQuery($sql);
454
455 //handling for completed contribution and cancelled contribution
456 //insertion of new records
457 $sql = "
458 INSERT INTO civicrm_financial_trxn
459 (contribution_id, payment_instrument_id, currency, total_amount, net_amount, fee_amount, trxn_id, status_id, check_number,
460 to_financial_account_id, from_financial_account_id, trxn_date)
461 SELECT tempI.contribution_id, tempI.payment_instrument_id, tempI.currency, tempI.total_amount, tempI.net_amount,
462 tempI.fee_amount, tempI.trxn_id, tempI.contribution_status_id, tempI.check_number,
463 tempI.to_financial_account_id, tempI.from_financial_account_id, tempI.trxn_date
464 FROM {$tempTableName2} tempI
465 WHERE tempI.action = 'insert';";
466 CRM_Core_DAO::executeQuery($sql);
467
468 //update of existing records
469 $sql = "
470 UPDATE civicrm_financial_trxn ft
471 INNER JOIN {$tempTableName2} tempU
472 ON (tempU.action != 'insert' AND ft.id = tempU.action)
473 SET ft.from_financial_account_id = NULL,
474 ft.to_financial_account_id = tempU.to_financial_account_id,
475 ft.status_id = tempU.contribution_status_id,
476 ft.payment_instrument_id = tempU.payment_instrument_id,
477 ft.check_number = tempU.check_number,
478 ft.contribution_id = tempU.contribution_id;";
479 CRM_Core_DAO::executeQuery($sql);
480
481 //insert the -ve transaction rows for cancelled contributions
482 $sql = "
483 INSERT INTO civicrm_financial_trxn
484 (contribution_id, payment_instrument_id, currency, total_amount, net_amount, fee_amount, trxn_id, status_id,
485 check_number, to_financial_account_id, from_financial_account_id, trxn_date)
486 SELECT ft.contribution_id, ft.payment_instrument_id, ft.currency, -ft.total_amount, ft.net_amount, ft.fee_amount, ft.trxn_id,
487 ft.status_id, ft.check_number, ft.to_financial_account_id, ft.from_financial_account_id, ft.trxn_date
488 FROM civicrm_financial_trxn ft
489 WHERE ft.status_id = {$cancelledStatus};";
490 CRM_Core_DAO::executeQuery($sql);
491
492 //inserting entity financial trxn entries if its not present in entity_financial_trxn for completed and pending contribution statuses
493 //this also handles +ve and -ve both transaction entries for a cancelled contribution
494 $sql = "
495 INSERT INTO civicrm_entity_financial_trxn (entity_table, entity_id, financial_trxn_id, amount)
496 SELECT 'civicrm_contribution', ft.contribution_id, ft.id, ft.total_amount as amount
497 FROM civicrm_financial_trxn ft
498 WHERE contribution_id IS NOT NULL AND
499 ft.id NOT IN (SELECT financial_trxn_id
500 FROM civicrm_entity_financial_trxn
501 WHERE entity_table = 'civicrm_contribution'
502 AND entity_id = ft.contribution_id)";
503 CRM_Core_DAO::executeQuery($sql);
504 //end of adding financial_trxn records and entity_financial_trxn records related to contribution
505
506 //update all linked line_item rows
507 // set line_item.financial_type_id = contribution.financial_type_id if contribution page id is null and not participant line item
508 // set line_item.financial_type_id = price_field_value.financial_type_id if contribution page id is set and not participant line item
509 // set line_item.financial_type_id = event.financial_type_id if its participant line item and line_item.price_field_value_id is null
510 // set line_item.financial_type_id = price_field_value.financial_type_id if its participant line item and line_item.price_field_value_id is set
511 $updateLineItemSql = "
512 UPDATE civicrm_line_item li
513 LEFT JOIN civicrm_contribution con
514 ON (li.entity_id = con.id AND li.entity_table = 'civicrm_contribution')
515 LEFT JOIN civicrm_price_field_value cpfv
516 ON li.price_field_value_id = cpfv.id
517 LEFT JOIN civicrm_participant cp
518 ON (li.entity_id = cp.id AND li.entity_table = 'civicrm_participant')
519 LEFT JOIN civicrm_event ce
520 ON ce.id = cp.event_id
521 SET li.financial_type_id = CASE
522 WHEN (con.contribution_page_id IS NULL || li.price_field_value_id IS NULL) AND cp.id IS NULL THEN
523 con.financial_type_id
524 WHEN (con.contribution_page_id IS NOT NULL AND cp.id IS NULL) || (cp.id IS NOT NULL AND li.price_field_value_id IS NOT NULL) THEN
525 cpfv.financial_type_id
526 WHEN cp.id IS NOT NULL AND li.price_field_value_id IS NULL THEN
527 ce.financial_type_id
528 END";
529 CRM_Core_DAO::executeQuery($updateLineItemSql, $queryParams);
530
531 //add the financial_item entries
532 //add a temp column so that inserting entity_financial_trxn entries gets easy
533 $sql = "ALTER TABLE civicrm_financial_item ADD COLUMN f_trxn_id INT DEFAULT NULL";
534 CRM_Core_DAO::executeQuery($sql);
535
536 //add financial_item entries for contribution completed / pending pay later / cancelled
537 $contributionlineItemSql = "
538 INSERT INTO civicrm_financial_item
539 (transaction_date, contact_id, amount, currency, entity_table, entity_id, description, status_id, financial_account_id, f_trxn_id)
540
541 SELECT REPLACE(REPLACE(REPLACE(ft.trxn_date, '-', ''), ':', ''), ' ', ''), con.contact_id,
542 IF(ft.total_amount < 0 AND con.contribution_status_id = %3, -li.line_total, li.line_total) as line_total, con.currency, 'civicrm_line_item',
543 li.id as line_item_id, li.label as line_item_label,
544 IF(con.contribution_status_id = {$pendingStatus}, {$unpaidStatus}, {$paidStatus}) as status_id, efa.financial_account_id as financial_account_id,
545 ft.id as f_trxn_id
546 FROM civicrm_line_item li
547 INNER JOIN civicrm_contribution con
548 ON (li.entity_id = con.id AND li.entity_table = 'civicrm_contribution')
549 INNER JOIN civicrm_financial_trxn ft
550 ON (con.id = ft.contribution_id)
551 LEFT JOIN civicrm_entity_financial_account efa
552 ON (li.financial_type_id = efa.entity_id AND efa.entity_table = 'civicrm_financial_type'
553 AND efa.account_relationship = {$incomeAccountIs})
554 WHERE con.contribution_status_id IN (%1, %3) OR (con.is_pay_later = 1 AND con.contribution_status_id = %2)";
555 CRM_Core_DAO::executeQuery($contributionlineItemSql, $queryParams);
556
557 //add financial_item entries for event
558 $participantLineItemSql = "
559 INSERT INTO civicrm_financial_item
560 (transaction_date, contact_id, amount, currency, entity_table, entity_id, description, status_id, financial_account_id, f_trxn_id)
561
562 SELECT REPLACE(REPLACE(REPLACE(ft.trxn_date, '-', ''), ':', ''), ' ', ''), con.contact_id,
563 IF(ft.total_amount < 0 AND con.contribution_status_id = %3, -li.line_total, li.line_total) as line_total,
564 con.currency, 'civicrm_line_item', li.id as line_item_id, li.label as line_item_label,
565 IF(con.contribution_status_id = {$pendingStatus}, {$unpaidStatus}, {$paidStatus}) as status_id,
566 efa.financial_account_id as financial_account_id, ft.id as f_trxn_id
567 FROM civicrm_line_item li
568 INNER JOIN civicrm_participant par
569 ON (li.entity_id = par.id AND li.entity_table = 'civicrm_participant')
570 INNER JOIN civicrm_participant_payment pp
571 ON (pp.participant_id = par.id)
572 INNER JOIN civicrm_contribution con
573 ON (pp.contribution_id = con.id)
574 INNER JOIN civicrm_financial_trxn ft
575 ON (con.id = ft.contribution_id)
576 LEFT JOIN civicrm_entity_financial_account efa
577 ON (li.financial_type_id = efa.entity_id AND
578 efa.entity_table = 'civicrm_financial_type' AND efa.account_relationship = {$incomeAccountIs})
579 WHERE con.contribution_status_id IN (%1, %3) OR (con.is_pay_later = 1 AND con.contribution_status_id = %2)";
580 CRM_Core_DAO::executeQuery($participantLineItemSql, $queryParams);
581
582 //fee handling for contributions
583 //insert fee entries in financial_trxn for contributions
584 $sql = "ALTER TABLE civicrm_financial_trxn ADD COLUMN is_fee TINYINT DEFAULT NULL";
585 CRM_Core_DAO::executeQuery($sql);
586
587 $sql = "
588 INSERT INTO civicrm_financial_trxn
589 (contribution_id, payment_instrument_id, currency, total_amount, net_amount, fee_amount, trxn_id, status_id, check_number,
590 to_financial_account_id, from_financial_account_id, trxn_date, payment_processor_id, is_fee)
591
592 SELECT con.id, ft.payment_instrument_id, ft.currency, ft.fee_amount, NULL, NULL, ft.trxn_id, %1 as status_id,
593 ft.check_number, efaFT.financial_account_id as to_financial_account_id, CASE
594 WHEN efaPP.financial_account_id IS NOT NULL THEN
595 efaPP.financial_account_id
596 WHEN tpi.financial_account_id IS NOT NULL THEN
597 tpi.financial_account_id
598 ELSE
599 {$financialAccountId}
600 END as from_financial_account_id, ft.trxn_date, ft.payment_processor_id, 1 as is_fee
601 FROM civicrm_contribution con
602 INNER JOIN civicrm_financial_trxn ft
603 ON (ft.contribution_id = con.id)
604 LEFT JOIN civicrm_entity_financial_account efaFT
605 ON (con.financial_type_id = efaFT.entity_id AND efaFT.entity_table = 'civicrm_financial_type'
606 AND efaFT.account_relationship = {$expenseAccountIs})
607 LEFT JOIN civicrm_entity_financial_account efaPP
608 ON (ft.payment_processor_id = efaPP.entity_id AND efaPP.entity_table = 'civicrm_payment_processor'
609 AND efaPP.account_relationship = {$assetAccountIs})
610 LEFT JOIN {$tempTableName1} tpi
611 ON ft.payment_instrument_id = tpi.instrument_id
612 WHERE ft.fee_amount IS NOT NULL AND ft.fee_amount != 0 AND (con.contribution_status_id IN (%1, %3) OR (con.contribution_status_id =%2 AND con.is_pay_later = 1))
613 GROUP BY con.id";
614 CRM_Core_DAO::executeQuery($sql, $queryParams);
615
616 //link financial_trxn to contribution
617 $sql = "
618 INSERT INTO civicrm_entity_financial_trxn
619 (entity_table, entity_id, financial_trxn_id, amount)
620 SELECT 'civicrm_contribution', ft.contribution_id, ft.id, ft.total_amount
621 FROM civicrm_financial_trxn ft
622 WHERE ft.is_fee = 1";
623 CRM_Core_DAO::executeQuery($sql);
624
625 //add fee related entries to financial item table
626 $domainId = CRM_Core_Config::domainID();
627 $domainContactId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain', $domainId, 'contact_id');
628 $sql = "
629 INSERT INTO civicrm_financial_item
630 (transaction_date, contact_id, amount, currency, entity_table, entity_id, description, status_id, financial_account_id, f_trxn_id)
631 SELECT ft.trxn_date, {$domainContactId} as contact_id, ft.total_amount, ft.currency, 'civicrm_financial_trxn', ft.id,
632 'Fee', {$paidStatus} as status_id, ft.to_financial_account_id as financial_account_id, ft.id as f_trxn_id
633 FROM civicrm_financial_trxn ft
634 WHERE ft.is_fee = 1;";
635 CRM_Core_DAO::executeQuery($sql);
636
637 //add entries to entity_financial_trxn table
638 $sql = "
639 INSERT INTO civicrm_entity_financial_trxn (entity_table, entity_id, financial_trxn_id, amount)
640 SELECT 'civicrm_financial_item' as entity_table, fi.id as entity_id, fi.f_trxn_id as financial_trxn_id, fi.amount
641 FROM civicrm_financial_item fi";
642 CRM_Core_DAO::executeQuery($sql);
643
644 //drop the temparory columns
645 $sql = "ALTER TABLE civicrm_financial_trxn
646 DROP COLUMN contribution_id,
647 DROP COLUMN is_fee;";
648 CRM_Core_DAO::executeQuery($sql);
649
650 $sql = "ALTER TABLE civicrm_financial_item DROP f_trxn_id";
651 CRM_Core_DAO::executeQuery($sql);
652
653 return TRUE;
654 }
655
656 function createDomainContacts() {
657 $domainParams = $context = array();
658 $query = "
659 ALTER TABLE `civicrm_domain` ADD `contact_id` INT( 10 ) UNSIGNED NULL DEFAULT NULL COMMENT 'FK to Contact ID. This is specifically not an FK to avoid circular constraints',
660 ADD CONSTRAINT `FK_civicrm_domain_contact_id` FOREIGN KEY (`contact_id`) REFERENCES `civicrm_contact` (`id`);";
661 CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray, TRUE, NULL, FALSE, FALSE);
662
663 $query = 'SELECT cd.id, cd.name, ce.email FROM `civicrm_domain` cd
664 LEFT JOIN civicrm_loc_block clb ON clb.id = cd. loc_block_id
665 LEFT JOIN civicrm_email ce ON ce.id = clb.email_id ;' ;
666 $dao = CRM_Core_DAO::executeQuery($query);
667 while($dao->fetch()) {
668 $params = array(
669 'sort_name' => $dao->name,
670 'display_name' => $dao->name,
671 'legal_name' => $dao->name,
672 'organization_name' => $dao->name,
673 'contact_type' => 'Organization'
674 );
675 $query = "SELECT cc.id FROM `civicrm_contact` cc
676 LEFT JOIN civicrm_email ce ON ce.contact_id = cc.id
677 WHERE cc.contact_type = 'Organization' AND cc.organization_name = '{$dao->name}' ";
678 if ($dao->email) {
679 $query .= " AND ce.email = '{$dao->email}' ";
680 }
681 $contactID = CRM_Core_DAO::singleValueQuery($query);
682 $context[1] = $dao->name;
683 if (empty($contactID)) {
684 $contact = CRM_Contact_BAO_Contact::add($params);
685 $contactID = $contact->id;
686 $context[0] = 'added';
687 }
688 else {
689 $context[0] = 'merged';
690 }
691 $domainParams['contact_id'] = $contactID;
692 CRM_Core_BAO_Domain::edit($domainParams, $dao->id);
693 }
694 return $context;
695 }
696
697 function task_4_3_alpha1_checkDBConstraints() {
698 //checking whether the foreign key exists before dropping it CRM-11260
699 $config = CRM_Core_Config::singleton();
700 $dbUf = DB::parseDSN($config->dsn);
701 $params = array();
702 $tables = array(
703 'autorenewal_msg_id' => array('tableName' => 'civicrm_membership_type', 'fkey' => 'FK_civicrm_membership_autorenewal_msg_id'),
704 'to_account_id' => array('tableName' => 'civicrm_financial_trxn', 'constraintName' => 'civicrm_financial_trxn_ibfk_2'),
705 'from_account_id' => array('tableName' => 'civicrm_financial_trxn', 'constraintName' => 'civicrm_financial_trxn_ibfk_1'),
706 'contribution_type_id' => array('tableName' => 'civicrm_contribution_recur', 'fkey' => 'FK_civicrm_contribution_recur_contribution_type_id'),
707 );
708 $query = "SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
709 WHERE table_name = 'civicrm_contribution_recur'
710 AND constraint_name = 'FK_civicrm_contribution_recur_contribution_type_id'
711 AND TABLE_SCHEMA = '{$dbUf['database']}'";
712
713 $dao = CRM_Core_DAO::executeQuery($query, $params, TRUE, NULL, FALSE, FALSE);
714 foreach($tables as $columnName => $value){
715 if ($value['tableName'] == 'civicrm_membership_type' || $value['tableName'] == 'civicrm_contribution_recur') {
716 $foreignKeyExists = CRM_Core_DAO::checkConstraintExists($value['tableName'], $value['fkey']);
717 $fKey = $value['fkey'];
718 } else {
719 $foreignKeyExists = CRM_Core_DAO::checkFKConstraintInFormat($value['tableName'], $columnName);
720 $fKey = "`FK_{$value['tableName']}_{$columnName}`";
721 }
722 if ($foreignKeyExists || $value['tableName'] == 'civicrm_financial_trxn') {
723 if ($value['tableName'] != 'civicrm_contribution_recur' || ($value['tableName'] == 'civicrm_contribution_recur' && $dao->N)) {
724 $constraintName = $foreignKeyExists ? $fKey : $value['constraintName'];
725 CRM_Core_DAO::executeQuery("ALTER TABLE {$value['tableName']} DROP FOREIGN KEY {$constraintName}", $params, TRUE, NULL, FALSE, FALSE);
726 }
727 CRM_Core_DAO::executeQuery("ALTER TABLE {$value['tableName']} DROP INDEX {$fKey}", $params, TRUE, NULL, FALSE, FALSE);
728 }
729 }
730 // check if column contact_id is present or not in civicrm_financial_account
731 $fieldExists = CRM_Core_DAO::checkFieldExists('civicrm_financial_account', 'contact_id', FALSE);
732 if (!$fieldExists) {
733 $query = "ALTER TABLE civicrm_financial_account ADD `contact_id` int(10) unsigned DEFAULT NULL COMMENT 'Version identifier of financial_type' AFTER `name`, ADD CONSTRAINT `FK_civicrm_financial_account_contact_id` FOREIGN KEY (`contact_id`) REFERENCES `civicrm_contact`(id);";
734 CRM_Core_DAO::executeQuery($query, $params, TRUE, NULL, FALSE, FALSE);
735 }
736 }
737
738 /**
739 * Read creation and modification times from civicrm_log; add
740 * them to civicrm_contact.
741 */
742 function convertTimestamps(CRM_Queue_TaskContext $ctx, $startId, $endId) {
743 $sql = "
744 SELECT entity_id, min(modified_date) AS created, max(modified_date) AS modified
745 FROM civicrm_log
746 WHERE entity_table = 'civicrm_contact'
747 AND entity_id BETWEEN %1 AND %2
748 GROUP BY entity_id
749 ";
750 $params = array(
751 1 => array($startId, 'Integer'),
752 2 => array($endId, 'Integer'),
753 );
754 $dao = CRM_Core_DAO::executeQuery($sql, $params);
755 while ($dao->fetch()) {
756 // FIXME civicrm_log.modified_date is DATETIME; civicrm_contact.modified_date is TIMESTAMP
757 CRM_Core_DAO::executeQuery(
758 'UPDATE civicrm_contact SET created_date = %1, modified_date = %2 WHERE id = %3',
759 array(
760 1 => array($dao->created, 'String'),
761 2 => array($dao->modified, 'String'),
762 3 => array($dao->entity_id, 'Integer'),
763 )
764 );
765 }
766
767 return TRUE;
768 }
769
770 /**
771 * replace contribution_type to financial_type in table
772 * civicrm_saved_search and Structure civicrm_report_instance
773 */
774 function replaceContributionTypeId(CRM_Queue_TaskContext $ctx, $query, $table) {
775 $dao = CRM_Core_DAO::executeQuery($query);
776 while ($dao->fetch()) {
777 $formValues = unserialize($dao->form_values);
778 foreach (array('contribution_type_id_op', 'contribution_type_id_value', 'contribution_type_id') as $value) {
779 if (array_key_exists($value, $formValues)) {
780 $key = preg_replace('/contribution/', 'financial', $value);
781 $formValues[$key] = $formValues[$value];
782 unset($formValues[$value]);
783 }
784 }
785 if ($table != 'savedSearch') {
786 foreach (array('fields', 'group_bys') as $value) {
787 if (array_key_exists($value, $formValues)) {
788 if (array_key_exists('contribution_type_id', $formValues[$value])) {
789 $formValues[$value]['financial_type_id'] = $formValues[$value]['contribution_type_id'];
790 unset($formValues[$value]['contribution_type_id']);
791 }
792 else if (array_key_exists('contribution_type', $formValues[$value])) {
793 $formValues[$value]['financial_type'] = $formValues[$value]['contribution_type'];
794 unset($formValues[$value]['contribution_type']);
795 }
796 }
797 }
798 if (array_key_exists('order_bys', $formValues)) {
799 foreach ($formValues['order_bys'] as $key => $values) {
800 if (preg_grep('/contribution_type/', $values)) {
801 $formValues['order_bys'][$key]['column'] = preg_replace('/contribution_type/', 'financial_type', $values['column']);
802 }
803 }
804 }
805 }
806
807 if ($table == 'savedSearch') {
808 $saveDao = new CRM_Contact_DAO_SavedSearch();
809 }
810 else {
811 $saveDao = new CRM_Report_DAO_Instance();
812 }
813 $saveDao->id = $dao->id;
814
815 if ($table == 'savedSearch') {
816 if (array_key_exists('mapper', $formValues)) {
817 foreach ($formValues['mapper'] as $key => $values) {
818 foreach ($values as $k => $v) {
819 if (preg_grep('/contribution_/', $v)) {
820 $formValues['mapper'][$key][$k] = preg_replace('/contribution_type/', 'financial_type', $v);
821 }
822 }
823 }
824 }
825 foreach (array('select_tables', 'where_tables') as $value) {
826 if (preg_match('/contribution_type/', $dao->$value)) {
827 $tempValue = unserialize($dao->$value);
828 if (array_key_exists('civicrm_contribution_type', $tempValue)) {
829 $tempValue['civicrm_financial_type'] = $tempValue['civicrm_contribution_type'];
830 unset($tempValue['civicrm_contribution_type']);
831 }
832 $saveDao->$value = serialize($tempValue);
833 }
834 }
835 if (preg_match('/contribution_type/', $dao->where_clause)) {
836 $saveDao->where_clause = preg_replace('/contribution_type/', 'financial_type', $dao->where_clause);
837 }
838 }
839 $saveDao->form_values = serialize($formValues);
840
841 $saveDao->save();
842 }
843 return TRUE;
844 }
845
846 /**
847 * Check/Add INDEX CRM-12141
848 *
849 * @return bool TRUE for success
850 */
851 function task_4_3_x_checkIndexes(CRM_Queue_TaskContext $ctx) {
852 $query = "SHOW KEYS FROM `civicrm_entity_financial_trxn`
853 WHERE key_name IN ('UI_entity_financial_trxn_entity_table', 'UI_entity_financial_trxn_entity_id');";
854 $dao = CRM_Core_DAO::executeQuery($query);
855 if (!$dao->N) {
856 CRM_Core_DAO::executeQuery("ALTER TABLE civicrm_entity_financial_trxn
857 ADD INDEX UI_entity_financial_trxn_entity_table (entity_table),
858 ADD INDEX UI_entity_financial_trxn_entity_id (entity_id);");
859 }
860 return TRUE;
861 }
862
863 /**
864 * Update phones CRM-11292
865 *
866 * @return bool TRUE for success
867 */
868 static function phoneNumeric(CRM_Queue_TaskContext $ctx) {
869 CRM_Core_DAO::executeQuery(CRM_Contact_BAO_Contact::DROP_STRIP_FUNCTION_43);
870 CRM_Core_DAO::executeQuery(CRM_Contact_BAO_Contact::CREATE_STRIP_FUNCTION_43);
871 CRM_Core_DAO::executeQuery("UPDATE civicrm_phone SET phone_numeric = civicrm_strip_non_numeric(phone)");
872 return TRUE;
873 }
874
875 /**
876 * (Queue Task Callback)
877 */
878 static function task_4_3_x_runSql(CRM_Queue_TaskContext $ctx, $rev) {
879 $upgrade = new CRM_Upgrade_Form();
880 $upgrade->processSQL($rev);
881
882 return TRUE;
883 }
884
885 /**
886 * Syntatic sugar for adding a task which (a) is in this class and (b) has
887 * a high priority.
888 *
889 * After passing the $funcName, you can also pass parameters that will go to
890 * the function. Note that all params must be serializable.
891 */
892 protected function addTask($title, $funcName) {
893 $queue = CRM_Queue_Service::singleton()->load(array(
894 'type' => 'Sql',
895 'name' => CRM_Upgrade_Form::QUEUE_NAME,
896 ));
897
898 $args = func_get_args();
899 $title = array_shift($args);
900 $funcName = array_shift($args);
901 $task = new CRM_Queue_Task(
902 array(get_class($this), $funcName),
903 $args,
904 $title
905 );
906 $queue->createItem($task, array('weight' => -1));
907 }
908 }