Last active
May 24, 2022 19:38
-
-
Save aydun/d49197c81205b463ef5cdef38c97b317 to your computer and use it in GitHub Desktop.
case counts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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