$select = $from = $where = '';
- $select = 'SELECT DISTINCT civicrm_group.id, civicrm_group.title ';
+ $select = 'SELECT civicrm_group.id, civicrm_group.title ';
$from = ' FROM civicrm_group ';
$where = " WHERE civicrm_group.is_active = 1 ";
if ($contactId) {
if ($visibility) {
$where .= " AND civicrm_group.visibility != 'User and User Admin Only'";
}
+ $groupBy = " GROUP BY civicrm_group.id, civicrm_group.title";
$orderby = " ORDER BY civicrm_group.name";
- $sql = $select . $from . $where . $orderby;
+ $sql = $select . $from . $where . $groupBy . $orderby;
$group->query($sql);
* @return string
*/
public static function getGroupByFromSelectColumns($selectClauses, $groupBy = NULL) {
+ $mysqlVersion = CRM_Core_DAO::singleValueQuery('SELECT VERSION()');
+ $sqlMode = CRM_Core_DAO::singleValueQuery('SELECT @@sql_mode');
+ if (version_compare($mysqlVersion, '5.7', '<') && (!empty($sqlMode) && !in_array('ONLY_FULL_GROUP_BY', explode(',', $sqlMode)))) {
+ return '';
+ }
$groupBy = (array) $groupBy;
- $regexToExclude = '/(COALESCE|ROUND|AVG|COUNT|GROUP_CONCAT|SUM|MAX|MIN)\(/i';
+ $regexToExclude = '/(ROUND|AVG|COUNT|GROUP_CONCAT|SUM|MAX|MIN)\(/i';
foreach ($selectClauses as $key => $val) {
$aliasArray = preg_split('/ as /i', $val);
// if more than 1 alias we need to split by ','.
else {
list($selectColumn, $alias) = array_pad($aliasArray, 2, NULL);
$dateRegex = '/^(DATE_FORMAT|DATE_ADD)/i';
- // exclude columns which are alreagy included in groupBy and aggregate functions from select
+ // exclude columns which are already included in groupBy and aggregate functions from select
if (!in_array($selectColumn, $groupBy) && preg_match($regexToExclude, trim($selectColumn)) !== 1) {
if (!empty($alias) && preg_match($dateRegex, trim($selectColumn))) {
$selectColAlias[] = $alias;
list($select, $from, $where) = $this->query(FALSE, FALSE, FALSE, $onlyDeleted);
$from = " FROM civicrm_prevnext_cache pnc INNER JOIN civicrm_contact contact_a ON contact_a.id = pnc.entity_id1 AND pnc.cacheKey = '$cacheKey' " . substr($from, 31);
$order = " ORDER BY pnc.id";
- $groupBy = " GROUP BY contact_a.id";
+ $groupBy = " GROUP BY contact_a.id, pnc.id";
$groupBy .= self::getGroupByFromSelectColumns($this->_select, 'contact_a.id');
$limit = " LIMIT $offset, $rowCount";
$query = "$select $from $where $groupBy $order $limit";
SELECT DISTINCT UPPER(LEFT(title, 1)) as sort_name
FROM civicrm_contribution_page
WHERE $whereClause
-ORDER BY LEFT(title, 1)
+ORDER BY UPPER(LEFT(title, 1))
";
$dao = CRM_Core_DAO::executeQuery($query, $whereParams);
";
CRM_Core_DAO::executeQuery($updateLineItem);
+ $fiColumns = "fi.contact_id, fi.description, fi.amount, fi.currency,
+ fi.financial_account_id, fi.status_id, fi.entity_table, fi.entity_id";
// gathering necessary info to record negative (deselected) financial_item
$updateFinancialItem = "
- SELECT fi.*, SUM(fi.amount) as differenceAmt, price_field_value_id, financial_type_id, tax_amount
+ SELECT {$fiColumns}, SUM(fi.amount) as differenceAmt, price_field_value_id, financial_type_id, tax_amount
FROM civicrm_financial_item fi LEFT JOIN civicrm_line_item li ON (li.id = fi.entity_id AND fi.entity_table = 'civicrm_line_item')
WHERE (li.entity_table = 'civicrm_participant' AND li.entity_id = {$participantId})
-GROUP BY li.entity_table, li.entity_id, price_field_value_id
+GROUP BY li.entity_table, li.entity_id, price_field_value_id, fi.id, {$fiColumns}
";
$updateFinancialItemInfoDAO = CRM_Core_DAO::executeQuery($updateFinancialItem);
$trxn = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contributionId, 'DESC', TRUE);
while ($updateFinancialItemInfoDAO->fetch()) {
$updateFinancialItemInfoValues = (array) $updateFinancialItemInfoDAO;
$updateFinancialItemInfoValues['transaction_date'] = date('YmdHis');
- // the below params are not needed
- unset($updateFinancialItemInfoValues['id']);
- unset($updateFinancialItemInfoValues['created_date']);
+
// if not submitted and difference is not 0 make it negative
if (!in_array($updateFinancialItemInfoValues['price_field_value_id'], $submittedFieldValueIds) && $updateFinancialItemInfoValues['differenceAmt'] != 0) {
// INSERT negative financial_items
SELECT DISTINCT UPPER(LEFT(title, 1)) as sort_name
FROM civicrm_event
WHERE $whereClause
- ORDER BY LEFT(title, 1)
+ ORDER BY UPPER(LEFT(title, 1))
";
$dao = CRM_Core_DAO::executeQuery($query, $whereParams);
$query = "REPLACE INTO I_$job_id (email_id, contact_id)
- SELECT DISTINCT $email.id as email_id,
+ SELECT $email.id as email_id,
$contact.id as contact_id
FROM $email
INNER JOIN $contact
AND $email.on_hold = 0
AND $mg.mailing_id = {$mailing_id}
AND X_$job_id.contact_id IS null
+ GROUP BY $email.id, $contact.id
$order_by";
if ($mode == 'sms') {
// Query prior mailings.
$query = "REPLACE INTO I_$job_id (email_id, contact_id)
- SELECT DISTINCT $email.id as email_id,
+ SELECT $email.id as email_id,
$contact.id as contact_id
FROM $email
INNER JOIN $contact
AND $email.on_hold = 0
AND $mg.mailing_id = {$mailing_id}
AND X_$job_id.contact_id IS null
+ GROUP BY $email.id, $contact.id
$order_by";
if ($mode == 'sms') {
// Get the emails with only location override.
$query = "REPLACE INTO I_$job_id (email_id, contact_id)
- SELECT DISTINCT $email.id as local_email_id,
+ SELECT $email.id as local_email_id,
$contact.id as contact_id
FROM $email
INNER JOIN $contact
AND $email.on_hold = 0
AND $mg.mailing_id = {$mailing_id}
AND X_$job_id.contact_id IS null
+ GROUP BY $email.id, $contact.id
$order_by";
if ($mode == "sms") {
$query = "REPLACE INTO I_$job_id (phone_id, contact_id)
LEFT JOIN $job ON ( $job.mailing_id = $mailing.id AND $job.is_test = 0 AND $job.parent_id IS NULL )
LEFT JOIN civicrm_contact createdContact ON ( civicrm_mailing.created_id = createdContact.id )
LEFT JOIN civicrm_contact scheduledContact ON ( civicrm_mailing.scheduled_id = scheduledContact.id )
- WHERE $mailingACL $additionalClause
- GROUP BY {$groupFromSelect}";
+ WHERE $mailingACL $additionalClause";
+
+ if (!empty($groupFromSelect)) {
+ $query .= " GROUP BY {$groupFromSelect}";
+ }
if ($sort) {
$orderBy = trim($sort->orderBy());
LEFT JOIN civicrm_contact createdContact ON ( civicrm_mailing.created_id = createdContact.id )
LEFT JOIN civicrm_contact scheduledContact ON ( civicrm_mailing.scheduled_id = scheduledContact.id )
WHERE $whereClause
-ORDER BY LEFT(name, 1)
+ORDER BY UPPER(LEFT(name, 1))
";
$dao = CRM_Core_DAO::executeQuery($query, $params);
return $sql;
}
+ /**
+ * append select with ANY_VALUE() keyword.
+ *
+ * @param array $selectArray
+ * @param array $groupBy - Columns already included in GROUP By clause.
+ */
+ public function appendSelect($selectClauses, $groupBy) {
+ $mysqlVersion = CRM_Core_DAO::singleValueQuery('SELECT VERSION()');
+ $sqlMode = explode(',', CRM_Core_DAO::singleValueQuery('SELECT @@sql_mode'));
+
+ // Disable only_full_group_by mode for lower sql versions.
+ if (version_compare($mysqlVersion, '5.7', '<') || (!empty($sqlMode) && !in_array('ONLY_FULL_GROUP_BY', $sqlMode))) {
+ $key = array_search('ONLY_FULL_GROUP_BY', $sqlMode);
+ unset($sqlMode[$key]);
+ CRM_Core_DAO::executeQuery("SET SESSION sql_mode = '" . implode(',', $sqlMode) . "'");
+ return;
+ }
+ $groupBy = array_map('trim', (array) $groupBy);
+ $aggregateFunctions = '/(ROUND|AVG|COUNT|GROUP_CONCAT|SUM|MAX|MIN)\(/i';
+ foreach ($selectClauses as $key => &$val) {
+ list($selectColumn, $alias) = array_pad(preg_split('/ as /i', $val), 2, NULL);
+ // append ANY_VALUE() keyword
+ if (!in_array($selectColumn, $groupBy) && preg_match($aggregateFunctions, trim($selectColumn)) !== 1) {
+ $val = str_replace($selectColumn, "ANY_VALUE({$selectColumn})", $val);
+ }
+ }
+ $this->_select = "SELECT " . implode(', ', $selectClauses) . " ";
+ }
+
/**
* Build group by clause.
*/
public function groupBy() {
$this->_groupBy = "GROUP BY {$this->_aliases['civicrm_contribution']}.contact_id ";
- $this->_groupBy .= CRM_Contact_BAO_Query::getGroupByFromSelectColumns($this->_selectClauses, "{$this->_aliases['civicrm_contribution']}.contact_id");
+ $this->appendSelect($this->_selectClauses, "{$this->_aliases['civicrm_contribution']}.contact_id");
$this->assign('chartSupported', TRUE);
}
$this->limit();
}
- $sql = "{$this->_select} {$this->_from} {$this->_where} $limitFilter {$this->_groupBy} {$this->_having} {$this->_rollup}";
+ $sql = "{$this->_select} {$this->_from} {$this->_where} {$limitFilter} {$this->_groupBy} {$this->_having} {$this->_rollup}";
if (!empty($this->_orderByArray)) {
$this->_orderBy = str_replace('contact_civireport.', 'civicrm_contact_', "ORDER BY ISNULL(civicrm_contribution_contact_id), " . implode(', ', $this->_orderByArray));
public function groupBy() {
$this->_rollup = 'WITH ROLLUP';
- $groupFromSelect = CRM_Contact_BAO_Query::getGroupByFromSelectColumns($this->selectClause, array("{$this->_aliases['civicrm_contribution_soft']}.contact_id", "constituentname.id"));
+ $this->appendSelect($this->selectClause, array("{$this->_aliases['civicrm_contribution_soft']}.contact_id", "constituentname.id"));
$this->_groupBy = "
-GROUP BY {$this->_aliases['civicrm_contribution_soft']}.contact_id, constituentname.id {$groupFromSelect} {$this->_rollup}";
+GROUP BY {$this->_aliases['civicrm_contribution_soft']}.contact_id, constituentname.id {$this->_rollup}";
}
public function where() {
!empty($this->_params['group_bys_freq'][$fieldName])
) {
- $append = "YEAR({$field['dbAlias']}),";
+ $append = "YEAR({$field['dbAlias']});;";
if (in_array(strtolower($this->_params['group_bys_freq'][$fieldName]),
array('year')
)) {
) {
$this->_rollup = " WITH ROLLUP";
}
- $groupBy = $this->_groupBy;
- $this->_groupBy = "GROUP BY " . implode(', ', $this->_groupBy);
+ $groupBy = array();
+ foreach ($this->_groupBy as $key => $val) {
+ if (strpos($val, ';;') !== false) {
+ $groupBy = array_merge($groupBy, explode(';;', $val));
+ }
+ else {
+ $groupBy[] = $this->_groupBy[$key];
+ }
+ }
+ $this->_groupBy = "GROUP BY " . implode(', ', $groupBy);
}
else {
$groupBy = "{$this->_aliases['civicrm_contact']}.id";
$this->_groupBy = "GROUP BY {$this->_aliases['civicrm_contact']}.id";
}
- $this->_groupBy .= CRM_Contact_BAO_Query::getGroupByFromSelectColumns($this->_selectClauses, $groupBy);
$this->_groupBy .= $this->_rollup;
+ // append select with ANY_VALUE() keyword
+ $this->appendSelect($this->_selectClauses, $groupBy);
}
/**
$this->assign('chartSupported', TRUE);
$fiscalYearOffset = self::fiscalYearOffset("{$this->_aliases['civicrm_contribution']}.receive_date");
$this->_groupBy = "GROUP BY {$this->_aliases['civicrm_contribution']}.contact_id, {$fiscalYearOffset}";
- $this->_groupBy .= CRM_Contact_BAO_Query::getGroupByFromSelectColumns($this->_selectClauses, array("{$this->_aliases['civicrm_contribution']}.contact_id", $fiscalYearOffset));
+ $this->appendSelect($this->_selectClauses, array("{$this->_aliases['civicrm_contribution']}.contact_id", $fiscalYearOffset));
$this->_groupBy .= " {$this->_rollup}";
}
public function groupBy() {
$this->assign('chartSupported', TRUE);
$this->_rollup = " WITH ROLLUP";
- $groupFromSelect = CRM_Contact_BAO_Query::getGroupByFromSelectColumns($this->_selectClauses, "{$this->_aliases['civicrm_event']}.id");
- $this->_groupBy = " GROUP BY {$this->_aliases['civicrm_event']}.id {$groupFromSelect} {$this->_rollup}";
+ $this->appendSelect($this->_selectClauses, "{$this->_aliases['civicrm_event']}.id");
+ $this->_groupBy = " GROUP BY {$this->_aliases['civicrm_event']}.id {$this->_rollup}";
}
public function postProcess() {
}
public function groupBy() {
- $this->_groupBy = "GROUP BY {$this->_aliases['civicrm_mailing']}.id";
- $this->_groupBy .= CRM_Contact_BAO_Query::getGroupByFromSelectColumns($this->_selectClauses, "{$this->_aliases['civicrm_mailing']}.id");
+ $groupBy = array(
+ "{$this->_aliases['civicrm_mailing']}.id",
+ "{$this->_aliases['civicrm_mailing_job']}.end_date",
+ );
+ $this->_groupBy = "GROUP BY " . implode(', ', $groupBy);
+ $this->_groupBy .= CRM_Contact_BAO_Query::getGroupByFromSelectColumns($this->_selectClauses, $groupBy);
}
public function orderBy() {
}
$this->_rollup = ' WITH ROLLUP';
- $groupFromSelect = CRM_Contact_BAO_Query::getGroupByFromSelectColumns($this->_selectClauses, $this->_groupBy);
- $this->_groupBy = 'GROUP BY ' . implode(', ', $this->_groupBy) . $groupFromSelect .
+ $this->appendSelect($this->_selectClauses, $this->_groupBy);
+ $this->_groupBy = 'GROUP BY ' . implode(', ', $this->_groupBy) .
" {$this->_rollup} ";
}
else {
$groupBy = "{$this->_aliases['civicrm_contact']}.id";
$this->_groupBy = "GROUP BY {$groupBy}";
}
- $this->_groupBy .= CRM_Contact_BAO_Query::getGroupByFromSelectColumns($this->_selectClauses, $groupBy);
+ $this->appendSelect($this->_selectClauses, $groupBy);
$this->_groupBy .= " {$this->_rollup}";
}