Merge pull request #15890 from civicrm/5.20
[civicrm-core.git] / CRM / Case / Form / Activity / LinkCases.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
abd06efc 19 * This class generates form components for LinkCase Activity.
6a488035
TO
20 */
21class CRM_Case_Form_Activity_LinkCases {
f157740d 22
4c6ce474 23 /**
c490a46a 24 * @param CRM_Core_Form $form
4c6ce474
EM
25 *
26 * @throws Exception
27 */
00be9182 28 public static function preProcess(&$form) {
abd06efc 29 if (empty($form->_caseId)) {
6a488035
TO
30 CRM_Core_Error::fatal(ts('Case Id not found.'));
31 }
eb20fbf0
TO
32 if (count($form->_caseId) != 1) {
33 CRM_Core_Resources::fatal(ts('Expected one case-type'));
34 }
35
36 $caseId = CRM_Utils_Array::first($form->_caseId);
6a488035
TO
37
38 $form->assign('clientID', $form->_currentlyViewedContactId);
abd06efc 39 $form->assign('sortName', CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $form->_currentlyViewedContactId, 'sort_name'));
eb20fbf0 40 $form->assign('caseTypeLabel', CRM_Case_BAO_Case::getCaseType($caseId));
6a488035
TO
41
42 // get the related cases for given case.
43 $relatedCases = $form->get('relatedCases');
44 if (!isset($relatedCases)) {
6e19e2ea 45 $relatedCases = CRM_Case_BAO_Case::getRelatedCases($caseId);
6a488035
TO
46 $form->set('relatedCases', empty($relatedCases) ? FALSE : $relatedCases);
47 }
6a488035
TO
48 }
49
50 /**
df371444 51 * Set default values for the form.
6a488035 52 *
c490a46a 53 * @param CRM_Core_Form $form
77b97be7 54 *
df371444 55 * @return array
6a488035 56 */
00be9182 57 public static function setDefaultValues(&$form) {
be2fb01f 58 $defaults = [];
fe88acfa
CW
59 if (!empty($_GET['link_to_case_id']) && CRM_Utils_Rule::positiveInteger($_GET['link_to_case_id'])) {
60 $defaults['link_to_case_id'] = $_GET['link_to_case_id'];
61 }
62 return $defaults;
6a488035
TO
63 }
64
4c6ce474 65 /**
c490a46a 66 * @param CRM_Core_Form $form
4c6ce474 67 */
00be9182 68 public static function buildQuickForm(&$form) {
abd06efc
CW
69 $excludeCaseIds = (array) $form->_caseId;
70 $relatedCases = $form->get('relatedCases');
71 if (is_array($relatedCases) && !empty($relatedCases)) {
72 $excludeCaseIds = array_merge($excludeCaseIds, array_keys($relatedCases));
73 }
be2fb01f 74 $form->addEntityRef('link_to_case_id', ts('Link To Case'), [
abd06efc 75 'entity' => 'Case',
be2fb01f
CW
76 'api' => [
77 'extra' => ['case_id.case_type_id.title', 'contact_id.sort_name'],
78 'params' => [
79 'case_id' => ['NOT IN' => $excludeCaseIds],
abd06efc 80 'case_id.is_deleted' => 0,
be2fb01f
CW
81 ],
82 ],
83 ], TRUE);
6a488035
TO
84 }
85
86 /**
fe482240 87 * Global validation rules for the form.
6a488035 88 *
64bd5a0e
TO
89 * @param array $values
90 * Posted values of the form.
6a488035 91 *
77b97be7 92 * @param $files
c490a46a 93 * @param CRM_Core_Form $form
77b97be7 94 *
a6c01b45
CW
95 * @return array
96 * list of errors to be posted back to the form
6a488035 97 */
00be9182 98 public static function formRule($values, $files, $form) {
be2fb01f 99 $errors = [];
6a488035
TO
100
101 $linkCaseId = CRM_Utils_Array::value('link_to_case_id', $values);
251924af 102 assert('is_numeric($linkCaseId)');
eb20fbf0 103 if ($linkCaseId == CRM_Utils_Array::first($form->_caseId)) {
6a488035
TO
104 $errors['link_to_case'] = ts('Please select some other case to link.');
105 }
106
107 // do check for existing related cases.
108 $relatedCases = $form->get('relatedCases');
109 if (is_array($relatedCases) && array_key_exists($linkCaseId, $relatedCases)) {
f72119d9 110 $errors['link_to_case'] = ts('Selected case is already linked.');
6a488035
TO
111 }
112
113 return empty($errors) ? TRUE : $errors;
114 }
115
116 /**
fe482240 117 * Process the form submission.
6a488035 118 *
6a488035 119 *
c490a46a
CW
120 * @param CRM_Core_Form $form
121 * @param array $params
6a488035 122 */
e547f744
TO
123 public static function beginPostProcess(&$form, &$params) {
124 }
6a488035
TO
125
126 /**
fe482240 127 * Process the form submission.
6a488035 128 *
6a488035 129 *
c490a46a
CW
130 * @param CRM_Core_Form $form
131 * @param array $params
df371444 132 * @param CRM_Activity_BAO_Activity $activity
6a488035 133 */
00be9182 134 public static function endPostProcess(&$form, &$params, &$activity) {
6a488035
TO
135 $activityId = $activity->id;
136 $linkCaseID = CRM_Utils_Array::value('link_to_case_id', $params);
137
138 //create a link between two cases.
139 if ($activityId && $linkCaseID) {
be2fb01f 140 $caseParams = [
6a488035
TO
141 'case_id' => $linkCaseID,
142 'activity_id' => $activityId,
be2fb01f 143 ];
6a488035
TO
144 CRM_Case_BAO_Case::processCaseActivity($caseParams);
145 }
146 }
96025800 147
6a488035 148}