示例#1
0
 /**
  * Execute a query and get the single result.
  *
  * @param string $query
  *   Query to be executed.
  * @param array $params
  * @param bool $abort
  * @param bool $i18nRewrite
  * @return string|null
  *   the result of the query if any
  *
  */
 public static function &singleValueQuery($query, $params = array(), $abort = TRUE, $i18nRewrite = TRUE)
 {
     $queryStr = self::composeQuery($query, $params, $abort);
     static $_dao = NULL;
     if (!$_dao) {
         $_dao = new CRM_Core_DAO();
     }
     $_dao->query($queryStr, $i18nRewrite);
     $result = $_dao->getDatabaseResult();
     $ret = NULL;
     if ($result) {
         $row = $result->fetchRow();
         if ($row) {
             $ret = $row[0];
         }
     }
     $_dao->free();
     return $ret;
 }
/**
 * Find incorrect total lifetime contributions.
 *
 * Diangostic tool for collecting records with an incorrect
 * total lifetime contribution value in the summary field.
 *
 */
function sumfields_find_incorrect_total_lifetime_contribution_records()
{
    $ret = array();
    // We're only interested in one field for this test.
    $base_column_name = 'contribution_total_lifetime';
    // We need to ensure this field is enabled on this site.
    $active_fields = sumfields_get_setting('active_fields', array());
    if (!in_array($base_column_name, $active_fields)) {
        drush_log(dt("The total lifetime contribution is not active, this test will not work."), 'error');
        return FALSE;
    }
    // Get the name of the actual summary fields table.
    $table_name = _sumfields_get_custom_table_name();
    // Get the actual names of the field in question
    $custom_fields = _sumfields_get_custom_field_parameters();
    $column_name = $custom_fields[$base_column_name]['column_name'];
    // Load the field and group definitions because we need the trigger
    // clause that is stored here.
    $custom = sumfields_get_custom_field_definitions();
    // Get the base sql
    $config_trigger_sql = $custom['fields'][$base_column_name]['trigger_sql'];
    if (empty($table_name) || empty($column_name) || empty($config_trigger_sql)) {
        // Perhaps we are not properly enabled?
        drush_log(dt("Can't get table name or column name or trigger sql. Something is wrong."), 'error');
        return FALSE;
    }
    if ($db_trigger_sql != $config_trigger_sql) {
        drush_log(dt("Mis-match between db_trigger_sql (@db) and config_trigger_sql (@config). Using config.", array('@db' => $db_trigger_sql, '@config' => $config_trigger_sql)));
    }
    // Rewrite the sql with the appropriate variables filled in.
    if (FALSE === ($trigger_sql = sumfields_sql_rewrite($config_trigger_sql))) {
        $msg = sprintf(ts("Failed to rewrite sql for %s field."), $base_column_name);
        drush_log($msg, 'error');
        return FALSE;
    }
    // Iterate over all contacts with a contribution
    $contact_sql = "SELECT DISTINCT(contact_id) FROM civicrm_contribution WHERE " . "is_test = 0";
    $dao = CRM_Core_DAO::executeQuery($contact_sql);
    $trigger_dao = new CRM_Core_DAO();
    while ($dao->fetch()) {
        $sql = str_replace("NEW.contact_id", $dao->contact_id, $trigger_sql);
        $trigger_dao->query($sql);
        $trigger_result = $trigger_dao->getDatabaseResult();
        $row = $trigger_result->fetchRow();
        $trigger_total = empty($row[0]) ? '0.00' : $row[0];
        $table_sql = "SELECT `{$column_name}` AS table_total FROM `{$table_name}` WHERE entity_id = %0";
        $table_dao = CRM_Core_DAO::executeQuery($table_sql, array(0 => array($dao->contact_id, 'Integer')));
        $table_dao->fetch();
        $table_total = empty($table_dao->table_total) ? '0.00' : $table_dao->table_total;
        if ($table_total != $trigger_total) {
            $sql = "SELECT MAX(receive_date) AS last FROM civicrm_contribution WHERE contact_id = %0";
            $last_dao = CRM_Core_DAO::executeQuery($sql, array(0 => array($dao->contact_id, 'Integer')));
            $last_dao->fetch();
            $last_contribution = $last_dao->last;
            $ret[$dao->contact_id] = "Contact id: {$dao->contact_id}, Summary Table total: " . $table_total . ", Current trigger total: {$trigger_total}, Last Contribution: {$last_contribution}";
        }
    }
    return $ret;
}