PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/civicrm/custom/php/CRM/Report/Form/Case/Summary.php

https://github.com/nysenate/Bluebird-CRM
PHP | 392 lines | 319 code | 32 blank | 41 comment | 26 complexity | df9bc4a053c99710b1d9dd2f697a17f2 MD5 | raw file
Possible License(s): JSON, BSD-3-Clause, MPL-2.0-no-copyleft-exception, AGPL-1.0, GPL-2.0, AGPL-3.0, Apache-2.0, MIT, GPL-3.0, CC-BY-4.0, LGPL-2.1, BSD-2-Clause, LGPL-3.0
  1. <?php
  2. /*
  3. +--------------------------------------------------------------------+
  4. | Copyright CiviCRM LLC. All rights reserved. |
  5. | |
  6. | This work is published under the GNU AGPLv3 license with some |
  7. | permitted exceptions and without any warranty. For full license |
  8. | and copyright information, see https://civicrm.org/licensing |
  9. +--------------------------------------------------------------------+
  10. */
  11. /**
  12. *
  13. * @package CRM
  14. * @copyright CiviCRM LLC https://civicrm.org/licensing
  15. */
  16. class CRM_Report_Form_Case_Summary extends CRM_Report_Form {
  17. protected $_summary = NULL;
  18. protected $_relField = FALSE;
  19. protected $_exposeContactID = FALSE;
  20. protected $_customGroupExtends = ['Case'];
  21. //NYSS 14297
  22. public $optimisedForOnlyFullGroupBy = FALSE;
  23. /**
  24. * Class constructor.
  25. */
  26. public function __construct() {
  27. $this->case_types = CRM_Case_PseudoConstant::caseType();
  28. $this->case_statuses = CRM_Core_OptionGroup::values('case_status');
  29. $rels = CRM_Core_PseudoConstant::relationshipType();
  30. foreach ($rels as $relid => $v) {
  31. $this->rel_types[$relid] = $v['label_b_a'];
  32. }
  33. $this->_columns = [
  34. 'civicrm_c2' => [
  35. 'dao' => 'CRM_Contact_DAO_Contact',
  36. 'fields' => [
  37. 'client_name' => [
  38. 'name' => 'sort_name',
  39. 'title' => ts('Contact Name'),
  40. 'required' => TRUE,
  41. ],
  42. 'id' => [
  43. 'no_display' => TRUE,
  44. 'required' => TRUE,
  45. ],
  46. ],
  47. 'order_bys' => [
  48. 'client_name' => [
  49. 'title' => ts('Contact Name'),
  50. 'name' => 'sort_name',
  51. ],
  52. ],
  53. 'grouping' => 'case-fields',
  54. ],
  55. 'civicrm_case' => [
  56. 'dao' => 'CRM_Case_DAO_Case',
  57. 'fields' => [
  58. 'id' => [
  59. 'title' => ts('Case ID'),
  60. 'required' => TRUE,
  61. ],
  62. 'subject' => [
  63. 'title' => ts('Case Subject'),
  64. 'default' => TRUE,
  65. ],
  66. 'status_id' => [
  67. 'title' => ts('Status'),
  68. 'default' => TRUE,
  69. ],
  70. 'case_type_id' => [
  71. 'title' => ts('Case Type'),
  72. 'default' => TRUE,
  73. ],
  74. 'start_date' => [
  75. 'title' => ts('Start Date'),
  76. 'default' => TRUE,
  77. 'type' => CRM_Utils_Type::T_DATE,
  78. ],
  79. 'end_date' => [
  80. 'title' => ts('End Date'),
  81. 'default' => TRUE,
  82. 'type' => CRM_Utils_Type::T_DATE,
  83. ],
  84. 'duration' => [
  85. 'title' => ts('Duration (Days)'),
  86. 'default' => FALSE,
  87. ],
  88. 'is_deleted' => [
  89. 'title' => ts('Deleted?'),
  90. 'default' => FALSE,
  91. 'type' => CRM_Utils_Type::T_BOOLEAN,
  92. ],
  93. ],
  94. 'filters' => [
  95. 'start_date' => [
  96. 'title' => ts('Start Date'),
  97. 'operatorType' => CRM_Report_Form::OP_DATE,
  98. 'type' => CRM_Utils_Type::T_DATE,
  99. ],
  100. 'end_date' => [
  101. 'title' => ts('End Date'),
  102. 'operatorType' => CRM_Report_Form::OP_DATE,
  103. 'type' => CRM_Utils_Type::T_DATE,
  104. ],
  105. 'case_type_id' => [
  106. 'title' => ts('Case Type'),
  107. 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
  108. 'options' => CRM_Case_BAO_Case::buildOptions('case_type_id', 'search'),
  109. 'type' => CRM_Utils_Type::T_INT,
  110. ],
  111. 'status_id' => [
  112. 'title' => ts('Status'),
  113. 'type' => CRM_Utils_Type::T_INT,
  114. 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
  115. 'options' => CRM_Case_BAO_Case::buildOptions('status_id', 'search'),
  116. ],
  117. 'is_deleted' => [
  118. 'title' => ts('Deleted?'),
  119. 'type' => CRM_Utils_Type::T_BOOLEAN,
  120. 'default' => 0,
  121. ],
  122. ],
  123. 'order_bys' => [
  124. 'start_date' => [
  125. 'title' => ts('Start Date'),
  126. ],
  127. 'end_date' => [
  128. 'title' => ts('End Date'),
  129. ],
  130. 'status_id' => [
  131. 'title' => ts('Status'),
  132. ],
  133. ],
  134. 'grouping' => 'case-fields',
  135. ],
  136. 'civicrm_contact' => [
  137. 'dao' => 'CRM_Contact_DAO_Contact',
  138. 'fields' => [
  139. 'sort_name' => [
  140. 'title' => ts('Staff Member'),
  141. 'default' => TRUE,
  142. ],
  143. ],
  144. 'filters' => [
  145. 'sort_name' => [
  146. 'title' => ts('Staff Member'),
  147. ],
  148. ],
  149. ],
  150. 'civicrm_relationship' => [
  151. 'dao' => 'CRM_Contact_DAO_Relationship',
  152. 'filters' => [
  153. 'relationship_type_id' => [
  154. 'title' => ts('Staff Relationship'),
  155. 'type' => CRM_Utils_Type::T_INT,
  156. 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
  157. 'options' => $this->rel_types,
  158. ],
  159. 'is_active' => [
  160. 'title' => ts('Active Relationship?'),
  161. 'type' => CRM_Utils_Type::T_BOOLEAN,
  162. ],
  163. ],
  164. ],
  165. 'civicrm_relationship_type' => [
  166. 'dao' => 'CRM_Contact_DAO_RelationshipType',
  167. 'fields' => [
  168. 'label_b_a' => [
  169. 'title' => ts('Relationship'),
  170. 'default' => TRUE,
  171. ],
  172. ],
  173. ],
  174. 'civicrm_case_contact' => [
  175. 'dao' => 'CRM_Case_DAO_CaseContact',
  176. ],
  177. ];
  178. parent::__construct();
  179. }
  180. public function preProcess() {
  181. parent::preProcess();
  182. }
  183. public function select() {
  184. $select = [];
  185. $this->_columnHeaders = [];
  186. foreach ($this->_columns as $tableName => $table) {
  187. if (array_key_exists('fields', $table)) {
  188. foreach ($table['fields'] as $fieldName => $field) {
  189. if (!empty($field['required']) ||
  190. !empty($this->_params['fields'][$fieldName])
  191. ) {
  192. if ($tableName == 'civicrm_relationship_type') {
  193. $this->_relField = TRUE;
  194. }
  195. if ($fieldName == 'duration') {
  196. $select[] = "IF({$table['fields']['end_date']['dbAlias']} Is Null, '', DATEDIFF({$table['fields']['end_date']['dbAlias']}, {$table['fields']['start_date']['dbAlias']})) as {$tableName}_{$fieldName}";
  197. }
  198. //NYSS 14297
  199. /*elseif ($tableName == 'civicrm_relationship_type') {
  200. $select[] = " IF(contact_civireport.id = relationship_civireport.contact_id_a, relationship_type_civireport.label_b_a, relationship_type_civireport.label_a_b) as civicrm_relationship_type_label_b_a";
  201. }*/
  202. else {
  203. $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
  204. }
  205. $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = $field['type'] ?? NULL;
  206. $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title'];
  207. }
  208. }
  209. }
  210. }
  211. $this->_selectClauses = $select;
  212. $this->_select = "SELECT " . implode(', ', $select) . " ";
  213. }
  214. /**
  215. * @param $fields
  216. * @param $files
  217. * @param $self
  218. *
  219. * @return array
  220. */
  221. public static function formRule($fields, $files, $self) {
  222. $errors = $grouping = [];
  223. if (empty($fields['relationship_type_id_value']) &&
  224. (array_key_exists('sort_name', $fields['fields']) ||
  225. array_key_exists('label_b_a', $fields['fields']))
  226. ) {
  227. $errors['fields'] = ts('Either filter on at least one relationship type, or de-select Staff Member and Relationship from the list of fields.');
  228. }
  229. if ((!empty($fields['relationship_type_id_value']) ||
  230. !empty($fields['sort_name_value'])) &&
  231. (!array_key_exists('sort_name', $fields['fields']) ||
  232. !array_key_exists('label_b_a', $fields['fields']))
  233. ) {
  234. $errors['fields'] = ts('To filter on Staff Member or Relationship, please also select Staff Member and Relationship from the list of fields.');
  235. }
  236. return $errors;
  237. }
  238. public function from() {
  239. $cc = $this->_aliases['civicrm_case'];
  240. $c = $this->_aliases['civicrm_contact'];
  241. $c2 = $this->_aliases['civicrm_c2'];
  242. $cr = $this->_aliases['civicrm_relationship'];
  243. $crt = $this->_aliases['civicrm_relationship_type'];
  244. $ccc = $this->_aliases['civicrm_case_contact'];
  245. foreach ($this->_columns['civicrm_relationship']['filters'] as $fieldName => $field) {
  246. if (!empty($this->_params[$fieldName . '_op'])
  247. && array_key_exists("{$fieldName}_value", $this->_params)
  248. && !CRM_Utils_System::isNull($this->_params["{$fieldName}_value"])
  249. ) {
  250. $this->_relField = TRUE;
  251. break;
  252. }
  253. }
  254. if ($this->_relField) {
  255. $this->_from = "
  256. FROM civicrm_contact $c
  257. inner join civicrm_relationship $cr on {$c}.id = ${cr}.contact_id_b OR {$c}.id = ${cr}.contact_id_a
  258. inner join civicrm_case $cc on ${cc}.id = ${cr}.case_id
  259. inner join civicrm_relationship_type $crt on ${crt}.id=${cr}.relationship_type_id
  260. inner join civicrm_case_contact $ccc on ${ccc}.case_id = ${cc}.id
  261. inner join civicrm_contact $c2 on ${c2}.id=${ccc}.contact_id
  262. ";
  263. }
  264. else {
  265. $this->_from = "
  266. FROM civicrm_case $cc
  267. inner join civicrm_case_contact $ccc on ${ccc}.case_id = ${cc}.id
  268. inner join civicrm_contact $c2 on ${c2}.id=${ccc}.contact_id
  269. ";
  270. }
  271. }
  272. public function storeWhereHavingClauseArray() {
  273. if (!empty($this->_params['fields']['label_b_a']) && $this->_params['fields']['label_b_a'] == 1) {
  274. $this->_whereClauses[] = '(contact_civireport.sort_name != c2_civireport.sort_name)';
  275. }
  276. //NYSS 14297
  277. if ($this->_relField) {
  278. $this->_whereClauses[] = '(contact_civireport.id NOT IN (SELECT contact_id FROM civicrm_case_contact WHERE case_id = case_civireport.id))';
  279. }
  280. parent::storeWhereHavingClauseArray();
  281. }
  282. public function groupBy() {
  283. $this->_groupBy = "";
  284. //NYSS 14297/14476
  285. if ($this->_relField) {
  286. $this->_groupBy = "GROUP BY c2_civireport.id, contact_civireport.id, case_civireport.id";
  287. }
  288. }
  289. public function postProcess() {
  290. $this->beginPostProcess();
  291. $sql = $this->buildQuery(TRUE);
  292. $rows = $graphRows = [];
  293. $this->buildRows($sql, $rows);
  294. $this->formatDisplay($rows);
  295. $this->doTemplateAssignment($rows);
  296. $this->endPostProcess($rows);
  297. }
  298. /**
  299. * Alter display of rows.
  300. *
  301. * Iterate through the rows retrieved via SQL and make changes for display purposes,
  302. * such as rendering contacts as links.
  303. *
  304. * @param array $rows
  305. * Rows generated by SQL, with an array for each row.
  306. */
  307. public function alterDisplay(&$rows) {
  308. $entryFound = FALSE;
  309. foreach ($rows as $rowNum => $row) {
  310. if (array_key_exists('civicrm_case_status_id', $row)) {
  311. if ($value = $row['civicrm_case_status_id']) {
  312. $rows[$rowNum]['civicrm_case_status_id'] = $this->case_statuses[$value];
  313. $entryFound = TRUE;
  314. }
  315. }
  316. if (array_key_exists('civicrm_case_case_type_id', $row) &&
  317. !empty($rows[$rowNum]['civicrm_case_case_type_id'])
  318. ) {
  319. $value = $row['civicrm_case_case_type_id'];
  320. $typeIds = explode(CRM_Core_DAO::VALUE_SEPARATOR, $value);
  321. $value = [];
  322. foreach ($typeIds as $typeId) {
  323. if ($typeId) {
  324. $value[$typeId] = $this->case_types[$typeId];
  325. }
  326. }
  327. $rows[$rowNum]['civicrm_case_case_type_id'] = implode(', ', $value);
  328. $entryFound = TRUE;
  329. }
  330. // convert Case ID and Subject to links to Manage Case
  331. if (array_key_exists('civicrm_case_id', $row) &&
  332. !empty($rows[$rowNum]['civicrm_c2_id'])
  333. ) {
  334. $url = CRM_Utils_System::url("civicrm/contact/view/case",
  335. 'reset=1&action=view&cid=' . $row['civicrm_c2_id'] . '&id=' .
  336. $row['civicrm_case_id'],
  337. $this->_absoluteUrl
  338. );
  339. $rows[$rowNum]['civicrm_case_id_link'] = $url;
  340. $rows[$rowNum]['civicrm_case_id_hover'] = ts("Manage Case");
  341. $entryFound = TRUE;
  342. }
  343. if (array_key_exists('civicrm_case_subject', $row) &&
  344. !empty($rows[$rowNum]['civicrm_c2_id'])
  345. ) {
  346. $url = CRM_Utils_System::url("civicrm/contact/view/case",
  347. 'reset=1&action=view&cid=' . $row['civicrm_c2_id'] . '&id=' .
  348. $row['civicrm_case_id'],
  349. $this->_absoluteUrl
  350. );
  351. $rows[$rowNum]['civicrm_case_subject_link'] = $url;
  352. $rows[$rowNum]['civicrm_case_subject_hover'] = ts("Manage Case");
  353. $entryFound = TRUE;
  354. }
  355. if (!$entryFound) {
  356. break;
  357. }
  358. }
  359. }
  360. }