Skip to content

Instantly share code, notes, and snippets.

@aydun
Last active May 24, 2022 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aydun/d49197c81205b463ef5cdef38c97b317 to your computer and use it in GitHub Desktop.
Save aydun/d49197c81205b463ef5cdef38c97b317 to your computer and use it in GitHub Desktop.
case counts
<?php
$caseStatuses = (array) \Civi\Api4\CiviCase::getFields()
->setLoadOptions(['id','name','label'])
->setAction('')
->addWhere('name', '=', 'status_id')
->addSelect('options')
->execute()
->first();
foreach ($caseStatuses['options'] as $status) {
$query[] = "SUM(cc.status_id=${status['id']}) AS \"${status['label']}\"";
}
$query = "SELECT ct.id, ct.title, " . implode(", ", $query) .
" FROM civicrm_case_type ct " .
" LEFT JOIN civicrm_case cc ON cc.case_type_id=ct.id AND cc.is_deleted=0 " .
" GROUP BY ct.id ORDER BY ct.title";
print_r($query);
/*
Sample output:
SELECT ct.id, ct.title, SUM(cc.status_id=1) AS "Ongoing", SUM(cc.status_id=2) AS "Resolved",
SUM(cc.status_id=3) AS "Urgent"
FROM civicrm_case_type ct
LEFT JOIN civicrm_case cc ON cc.case_type_id=ct.id AND cc.is_deleted=0
GROUP BY ct.id
ORDER BY ct.title
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment