/**
  * Create default settings for existing batches, for which settings don't already exist.
  */
 private static function importBatches()
 {
     $sql = "\n      SELECT id\n      FROM civicrm_batch\n      WHERE name LIKE 'GiftAid%'\n    ";
     $dao = CRM_Core_DAO::executeQuery($sql);
     while ($dao->fetch()) {
         // Only add settings for batches for which settings don't exist already
         if (CRM_Civigiftaid_BAO_BatchSettings::findByBatchId($dao->id) === FALSE) {
             // Set globally enabled to TRUE by default, for existing batches
             CRM_Civigiftaid_BAO_BatchSettings::create(array('batch_id' => (int) $dao->id, 'financial_types_enabled' => array(), 'globally_enabled' => TRUE));
         }
     }
 }
 /**
  * Return whether a row has financial type eligible for Gift Aid (i.e. has financial type which was enabled as
  * eligible for Gift Aid, at the time the contribution was added to the batch).
  *
  * @param $row
  *
  * @return bool
  */
 private function hasEligibleFinancialType($row)
 {
     // Lazy cache for batches
     static $batches = array();
     $batchId = $row['civicrm_entity_batch_batch_id'];
     if (!isset($batches[$batchId])) {
         if (($batch = CRM_Civigiftaid_BAO_BatchSettings::findByBatchId($batchId)) instanceof CRM_Core_DAO) {
             $batchArr = $batch->toArray();
             $batchArr['financial_types_enabled'] = unserialize($batchArr['financial_types_enabled']);
             $batches[$batchId] = $batchArr;
         } else {
             $batches[$batchId] = NULL;
         }
     }
     if ($batches[$batchId] && !$batches[$batchId]['globally_enabled']) {
         if (!in_array($row['civicrm_financial_type_financial_type_id'], $batches[$batchId]['financial_types_enabled'])) {
             return FALSE;
         }
     }
     return TRUE;
 }