Merge pull request #4693 from jaapjansma/CRM-15702
[civicrm-core.git] / CRM / Upgrade / Incremental / php / FourThree.php
index e8091fe593ab9743f54770d86daa7f3f110aeb85..30daff27d2011d51fec20fee885f3dbe724d39f5 100644 (file)
 class CRM_Upgrade_Incremental_php_FourThree {
   const BATCH_SIZE = 5000;
 
+  /**
+   * @param $errors
+   *
+   * @return bool
+   */
   function verifyPreDBstate(&$errors) {
     return TRUE;
   }
@@ -45,8 +50,10 @@ class CRM_Upgrade_Incremental_php_FourThree {
    * Note: This function is called iteratively for each upcoming
    * revision to the database.
    *
-   * @param $postUpgradeMessage string, alterable
+   * @param $preUpgradeMessage
    * @param $rev string, a version number, e.g. '4.3.alpha1', '4.3.beta3', '4.3.0'
+   * @param null $currentVer
+   *
    * @return void|bool
    */
   function setPreUpgradeMessage(&$preUpgradeMessage, $rev, $currentVer = NULL) {
@@ -73,21 +80,21 @@ class CRM_Upgrade_Incremental_php_FourThree {
         }
       }
     }
-    
+
     if ($rev == '4.3.6') {
       $constraintArray = array(
         'civicrm_contact' => 'contact_id',
-        'civicrm_payment_processor' => 'payment_processor_id', 
+        'civicrm_payment_processor' => 'payment_processor_id',
       );
-      
-      if (version_compare('4.1alpha1', $currentVer) <= 0) { 
+
+      if (version_compare('4.1alpha1', $currentVer) <= 0) {
         $constraintArray['civicrm_campaign'] = 'campaign_id';
       }
-      
-      if (version_compare('4.3alpha1', $currentVer) <= 0) { 
+
+      if (version_compare('4.3alpha1', $currentVer) <= 0) {
         $constraintArray['civicrm_financial_type'] = 'financial_type_id';
       }
-      
+
       foreach ($constraintArray as $key => $value) {
         $query = "SELECT contri_recur.id FROM civicrm_contribution_recur contri_recur LEFT JOIN {$key} ON contri_recur.{$value} = {$key}.id
 WHERE {$key}.id IS NULL";
@@ -233,6 +240,11 @@ WHERE ccp.financial_type_id IS NULL and cp.cost > 0');
     }
   }
 
+  /**
+   * @param $rev
+   *
+   * @return bool
+   */
   function upgrade_4_3_alpha1($rev) {
     self::task_4_3_alpha1_checkDBConstraints();
 
@@ -268,6 +280,9 @@ WHERE ccp.financial_type_id IS NULL and cp.cost > 0');
     return TRUE;
   }
 
+  /**
+   * @param $rev
+   */
   function upgrade_4_3_alpha2($rev) {
     //CRM-11847
     $isColumnPresent = CRM_Core_DAO::checkFieldExists('civicrm_dedupe_rule_group', 'is_default');
@@ -277,10 +292,16 @@ WHERE ccp.financial_type_id IS NULL and cp.cost > 0');
     $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => '4.3.alpha2')), 'task_4_3_x_runSql', $rev);
   }
 
+  /**
+   * @param $rev
+   */
   function upgrade_4_3_alpha3($rev) {
     $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => '4.3.alpha3')), 'task_4_3_x_runSql', $rev);
   }
 
+  /**
+   * @param $rev
+   */
   function upgrade_4_3_beta2($rev) {
     $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => '4.3.beta2')), 'task_4_3_x_runSql', $rev);
 
@@ -293,6 +314,9 @@ WHERE ccp.financial_type_id IS NULL and cp.cost > 0');
     }
   }
 
+  /**
+   * @param $rev
+   */
   function upgrade_4_3_beta3($rev) {
     $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => '4.3.beta3')), 'task_4_3_x_runSql', $rev);
     // CRM-12065
@@ -302,6 +326,9 @@ WHERE ccp.financial_type_id IS NULL and cp.cost > 0');
     $this->addTask('Replace contribution_type to financial_type in table civicrm_saved_search', 'replaceContributionTypeId', $query, 'savedSearch');
   }
 
+  /**
+   * @param $rev
+   */
   function upgrade_4_3_beta4($rev) {
     $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => '4.3.beta4')), 'task_4_3_x_runSql', $rev);
     // add indexes for civicrm_entity_financial_trxn
@@ -309,6 +336,9 @@ WHERE ccp.financial_type_id IS NULL and cp.cost > 0');
     $this->addTask('Check/Add indexes for civicrm_entity_financial_trxn', 'task_4_3_x_checkIndexes', $rev);
   }
 
+  /**
+   * @param $rev
+   */
   function upgrade_4_3_beta5($rev) {
     // CRM-12205
     if (
@@ -333,10 +363,16 @@ ADD COLUMN   premiums_nothankyou_label varchar(255) COLLATE utf8_unicode_ci DEFA
     $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => '4.3.beta5')), 'task_4_3_x_runSql', $rev);
   }
 
+  /**
+   * @param $rev
+   */
   function upgrade_4_3_4($rev) {
     $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => '4.3.4')), 'task_4_3_x_runSql', $rev);
   }
 
+  /**
+   * @param $rev
+   */
   function upgrade_4_3_5($rev) {
     // CRM-12156
     $config = CRM_Core_Config::singleton();
@@ -362,9 +398,12 @@ ADD CONSTRAINT `FK_civicrm_financial_item_contact_id` FOREIGN KEY (`contact_id`)
     $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => '4.3.5')), 'task_4_3_x_runSql', $rev);
   }
 
+  /**
+   * @param $rev
+   */
   function upgrade_4_3_6($rev) {
     //CRM-13094
-    $this->addTask(ts('Add missing contraints'), 'addMissingConstraints', $rev);
+    $this->addTask(ts('Add missing constraints'), 'addMissingConstraints', $rev);
     //CRM-13088
     $this->addTask('Add ON DELETE Options for constraints', 'task_4_3_x_checkConstraints', $rev);
     $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => '4.3.6')), 'task_4_3_x_runSql', $rev);
@@ -373,8 +412,11 @@ ADD CONSTRAINT `FK_civicrm_financial_item_contact_id` FOREIGN KEY (`contact_id`)
     $this->addTask('Update financial_account_id in financial_trxn table', 'updateFinancialTrxnData', $rev);
     $this->addTask('Update Line Item Data', 'updateLineItemData', $rev);
   }
-  
+
   //CRM-11636
+  /**
+   * @return bool
+   */
   function assignFinancialTypeToPriceRecords() {
     $upgrade = new CRM_Upgrade_Form();
     //here we update price set entries
@@ -416,6 +458,9 @@ INNER JOIN civicrm_price_set ps ON (pf.price_set_id = ps.id)
     return TRUE;
   }
 
+  /**
+   * @return bool
+   */
   static function _checkAndMigrateDefaultFinancialTypes() {
     $modifiedDefaults = FALSE;
     //insert types if not exists
@@ -445,6 +490,9 @@ ON DUPLICATE KEY UPDATE  is_active = 1
     return $modifiedDefaults;
   }
 
+  /**
+   * @return bool
+   */
   function createFinancialRecords() {
     $upgrade = new CRM_Upgrade_Form();
 
@@ -786,6 +834,9 @@ FROM   civicrm_financial_item fi";
     return TRUE;
   }
 
+  /**
+   * @return array
+   */
   function createDomainContacts() {
     $domainParams = $context = array();
     $query = "
@@ -912,9 +963,9 @@ ALTER TABLE civicrm_financial_account
 
     return TRUE;
   }
-  
+
   /**
-   * change index and add missing constraints for civicrm_contribution_recur
+   * Change index and add missing constraints for civicrm_contribution_recur
    */
   function addMissingConstraints(CRM_Queue_TaskContext $ctx) {
     $query = "SHOW KEYS FROM `civicrm_contribution_recur` WHERE key_name = 'UI_contrib_payment_instrument_id'";
@@ -924,9 +975,9 @@ ALTER TABLE civicrm_financial_account
       CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_contribution_recur ADD INDEX UI_contribution_recur_payment_instrument_id (payment_instrument_id)');
     }
     $constraintArray = array(
-      'contact_id' => " ADD CONSTRAINT `FK_civicrm_contribution_recur_contact_id` FOREIGN KEY (`contact_id`) REFERENCES `civicrm_contact` (`id`) ON DELETE CASCADE ", 
-      'payment_processor_id' => " ADD CONSTRAINT `FK_civicrm_contribution_recur_payment_processor_id` FOREIGN KEY (`payment_processor_id`) REFERENCES `civicrm_payment_processor` (`id`) ON DELETE SET NULL ", 
-      'financial_type_id' => " ADD CONSTRAINT `FK_civicrm_contribution_recur_financial_type_id` FOREIGN KEY (`financial_type_id`) REFERENCES `civicrm_financial_type` (`id`) ON DELETE SET NULL ", 
+      'contact_id' => " ADD CONSTRAINT `FK_civicrm_contribution_recur_contact_id` FOREIGN KEY (`contact_id`) REFERENCES `civicrm_contact` (`id`) ON DELETE CASCADE ",
+      'payment_processor_id' => " ADD CONSTRAINT `FK_civicrm_contribution_recur_payment_processor_id` FOREIGN KEY (`payment_processor_id`) REFERENCES `civicrm_payment_processor` (`id`) ON DELETE SET NULL ",
+      'financial_type_id' => " ADD CONSTRAINT `FK_civicrm_contribution_recur_financial_type_id` FOREIGN KEY (`financial_type_id`) REFERENCES `civicrm_financial_type` (`id`) ON DELETE SET NULL ",
       'campaign_id' => " ADD CONSTRAINT `FK_civicrm_contribution_recur_campaign_id` FOREIGN KEY (`campaign_id`) REFERENCES `civicrm_campaign` (`id`) ON DELETE SET NULL ",
     );
     $constraint = array();
@@ -940,23 +991,23 @@ ALTER TABLE civicrm_financial_account
       $query = "ALTER TABLE civicrm_contribution_recur " . implode(' , ', $constraint);
       CRM_Core_DAO::executeQuery($query);
     }
-    return TRUE;    
+    return TRUE;
   }
 
   /**
    * Update financial_account_id for bad data in financial_trxn table
    * CRM-12844
-   * 
+   *
    */
   function updateFinancialTrxnData(CRM_Queue_TaskContext $ctx) {
     $upgrade = new CRM_Upgrade_Form();
-    $sql = "SELECT cc.id contribution_id, cc.contribution_recur_id, cft.payment_processor_id, 
+    $sql = "SELECT cc.id contribution_id, cc.contribution_recur_id, cft.payment_processor_id,
 cft.id financial_trxn_id, cfi.entity_table, cft.from_financial_account_id, cft.to_financial_account_id
 
 FROM `civicrm_contribution` cc
 LEFT JOIN civicrm_entity_financial_trxn ceft ON ceft.entity_id = cc.id
 LEFT JOIN civicrm_financial_trxn cft ON cft.id = ceft.financial_trxn_id
-LEFT JOIN civicrm_entity_financial_trxn ceft1 ON ceft1.financial_trxn_id = ceft.financial_trxn_id 
+LEFT JOIN civicrm_entity_financial_trxn ceft1 ON ceft1.financial_trxn_id = ceft.financial_trxn_id
 LEFT JOIN civicrm_financial_item cfi ON cfi.id = ceft1.entity_id
 WHERE ceft.entity_table = 'civicrm_contribution'  AND cc.contribution_recur_id IS NOT NULL
 AND ceft1.entity_table = 'civicrm_financial_item' AND cft.id IS NOT NULL AND cft.payment_instrument_id = %1
@@ -981,7 +1032,7 @@ ORDER BY cft.id ";
         }
       }
       elseif (!array_key_exists($dao->contribution_id, $financialTrxn[$dao->contribution_recur_id])) {
-        if (($dao->entity_table == 'civicrm_line_item' && $dao->to_financial_account_id == $financialTrxn[$dao->contribution_recur_id]['from_financial_account_id']) 
+        if (($dao->entity_table == 'civicrm_line_item' && $dao->to_financial_account_id == $financialTrxn[$dao->contribution_recur_id]['from_financial_account_id'])
           || ($dao->entity_table == 'civicrm_financial_trxn' && $dao->from_financial_account_id == $financialTrxn[$dao->contribution_recur_id]['from_financial_account_id'])) {
           continue;
         }
@@ -996,8 +1047,8 @@ ORDER BY cft.id ";
         else {
           $field = 'to_financial_account_id';
         }
-        $sql = "UPDATE civicrm_financial_trxn SET $field = " . $financialTrxn[$dao->contribution_recur_id]['from_financial_account_id'] . ', 
-payment_processor_id = ' . $financialTrxn[$dao->contribution_recur_id]['payment_processor_id'] . ' WHERE  
+        $sql = "UPDATE civicrm_financial_trxn SET $field = " . $financialTrxn[$dao->contribution_recur_id]['from_financial_account_id'] . ',
+payment_processor_id = ' . $financialTrxn[$dao->contribution_recur_id]['payment_processor_id'] . ' WHERE
 id IN (' . implode(',', $val) . ')';
         CRM_Core_DAO::executeQuery($sql);
       }
@@ -1008,11 +1059,11 @@ id IN (' . implode(',', $val) . ')';
   /**
    * Update financial_account_id for bad data in financial_trxn table
    * CRM-12844
-   * 
+   *
    */
   function updateLineItemData(CRM_Queue_TaskContext $ctx) {
     $sql = "SELECT cc.id contribution_id, cc.contribution_recur_id,
-cc.financial_type_id contribution_financial_type, 
+cc.financial_type_id contribution_financial_type,
 cli.financial_type_id line_financial_type_id,
 cli.price_field_id, cli.price_field_value_id, cli.label, cli.id line_item_id,
 cfi.financial_account_id
@@ -1022,9 +1073,9 @@ LEFT JOIN civicrm_financial_item cfi ON cfi.entity_id = cli.id
 LEFT JOIN civicrm_price_field cpf ON cpf.id = cli.price_field_id
 LEFT JOIN civicrm_price_set cps ON cps.id = cpf.price_set_id
 LEFT JOIN civicrm_price_field_value cpfv ON cpfv.id = cli.price_field_value_id
-WHERE cfi.entity_table = 'civicrm_line_item' 
+WHERE cfi.entity_table = 'civicrm_line_item'
 AND cli.entity_table = 'civicrm_contribution'
-AND cps.is_quick_config = 1 AND cc.contribution_recur_id IS NOT NULL 
+AND cps.is_quick_config = 1 AND cc.contribution_recur_id IS NOT NULL
 ORDER BY cli.id";
     $dao = CRM_Core_DAO::executeQuery($sql);
     $financialTrxn = $subsequentPayments = array();
@@ -1048,14 +1099,14 @@ ORDER BY cli.id";
     foreach ($subsequentPayments as $key => $value) {
       $sql = "UPDATE civicrm_line_item cli
 LEFT JOIN civicrm_financial_item cfi ON cli.id = cfi.entity_id
-SET 
+SET
 cli.label = %1,
 cli.price_field_id = %2,
 cli.price_field_value_id = %3,
 cfi.financial_account_id = %4,
 cfi.description = %5,
 cli.financial_type_id = %6
-WHERE cfi.entity_table = 'civicrm_line_item' 
+WHERE cfi.entity_table = 'civicrm_line_item'
 AND cli.entity_table = 'civicrm_contribution' AND cli.id IN (" . implode(',', $value). ');';
       $params =  array(
         1 => array($financialTrxn[$key]['label'], 'String'),
@@ -1071,7 +1122,7 @@ AND cli.entity_table = 'civicrm_contribution' AND cli.id IN (" . implode(',', $v
   }
 
   /**
-   * replace contribution_type to financial_type in table
+   * Replace contribution_type to financial_type in table
    * civicrm_saved_search and Structure civicrm_report_instance
    */
   function replaceContributionTypeId(CRM_Queue_TaskContext $ctx, $query, $table) {
@@ -1150,6 +1201,8 @@ AND cli.entity_table = 'civicrm_contribution' AND cli.id IN (" . implode(',', $v
    * Add ON DELETE options for constraint if not present
    * CRM-13088 && CRM-12156
    *
+   * @param CRM_Queue_TaskContext $ctx
+   *
    * @return bool TRUE for success
    */
   function task_4_3_x_checkConstraints(CRM_Queue_TaskContext $ctx) {
@@ -1169,14 +1222,14 @@ AND cli.entity_table = 'civicrm_contribution' AND cli.id IN (" . implode(',', $v
       "'FK_civicrm_grant_financial_type_id'",
     );
 
-    $sql = "SELECT DELETE_RULE, TABLE_NAME, CONSTRAINT_NAME 
+    $sql = "SELECT DELETE_RULE, TABLE_NAME, CONSTRAINT_NAME
 FROM information_schema.REFERENTIAL_CONSTRAINTS
 WHERE CONSTRAINT_NAME IN (" . implode(',', $constraintArray) . ")
 AND CONSTRAINT_SCHEMA = %1";
     $params = array(1 => array($dbname['database'], 'String'));
     $onDelete = CRM_Core_DAO::executeQuery($sql, $params, TRUE, FALSE);
     while ($onDelete->fetch()) {
-      if (($onDelete->TABLE_NAME != 'civicrm_financial_item' && $onDelete->DELETE_RULE != 'SET NULL') || 
+      if (($onDelete->TABLE_NAME != 'civicrm_financial_item' && $onDelete->DELETE_RULE != 'SET NULL') ||
         ($onDelete->TABLE_NAME == 'civicrm_financial_item' && $onDelete->DELETE_RULE != 'CASCADE')) {
         $tableName = 'civicrm_financial_type';
         $onDeleteOption = ' SET NULL ';
@@ -1207,6 +1260,8 @@ AND CONSTRAINT_SCHEMA = %1";
   /**
    * Check/Add INDEX CRM-12141
    *
+   * @param CRM_Queue_TaskContext $ctx
+   *
    * @return bool TRUE for success
    */
   function task_4_3_x_checkIndexes(CRM_Queue_TaskContext $ctx) {
@@ -1230,6 +1285,8 @@ ADD INDEX UI_entity_financial_trxn_entity_id (entity_id);
   /**
    * Update phones CRM-11292
    *
+   * @param CRM_Queue_TaskContext $ctx
+   *
    * @return bool TRUE for success
    */
   static function phoneNumeric(CRM_Queue_TaskContext $ctx) {