Merge pull request #15208 from civicrm/5.17
[civicrm-core.git] / CRM / Case / Form / Activity / LinkCases.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
6a488035
TO
32 */
33
34/**
abd06efc 35 * This class generates form components for LinkCase Activity.
6a488035
TO
36 */
37class CRM_Case_Form_Activity_LinkCases {
f157740d 38
4c6ce474 39 /**
c490a46a 40 * @param CRM_Core_Form $form
4c6ce474
EM
41 *
42 * @throws Exception
43 */
00be9182 44 public static function preProcess(&$form) {
abd06efc 45 if (empty($form->_caseId)) {
6a488035
TO
46 CRM_Core_Error::fatal(ts('Case Id not found.'));
47 }
eb20fbf0
TO
48 if (count($form->_caseId) != 1) {
49 CRM_Core_Resources::fatal(ts('Expected one case-type'));
50 }
51
52 $caseId = CRM_Utils_Array::first($form->_caseId);
6a488035
TO
53
54 $form->assign('clientID', $form->_currentlyViewedContactId);
abd06efc 55 $form->assign('sortName', CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $form->_currentlyViewedContactId, 'sort_name'));
eb20fbf0 56 $form->assign('caseTypeLabel', CRM_Case_BAO_Case::getCaseType($caseId));
6a488035
TO
57
58 // get the related cases for given case.
59 $relatedCases = $form->get('relatedCases');
60 if (!isset($relatedCases)) {
6e19e2ea 61 $relatedCases = CRM_Case_BAO_Case::getRelatedCases($caseId);
6a488035
TO
62 $form->set('relatedCases', empty($relatedCases) ? FALSE : $relatedCases);
63 }
6a488035
TO
64 }
65
66 /**
df371444 67 * Set default values for the form.
6a488035 68 *
c490a46a 69 * @param CRM_Core_Form $form
77b97be7 70 *
df371444 71 * @return array
6a488035 72 */
00be9182 73 public static function setDefaultValues(&$form) {
be2fb01f 74 $defaults = [];
fe88acfa
CW
75 if (!empty($_GET['link_to_case_id']) && CRM_Utils_Rule::positiveInteger($_GET['link_to_case_id'])) {
76 $defaults['link_to_case_id'] = $_GET['link_to_case_id'];
77 }
78 return $defaults;
6a488035
TO
79 }
80
4c6ce474 81 /**
c490a46a 82 * @param CRM_Core_Form $form
4c6ce474 83 */
00be9182 84 public static function buildQuickForm(&$form) {
abd06efc
CW
85 $excludeCaseIds = (array) $form->_caseId;
86 $relatedCases = $form->get('relatedCases');
87 if (is_array($relatedCases) && !empty($relatedCases)) {
88 $excludeCaseIds = array_merge($excludeCaseIds, array_keys($relatedCases));
89 }
be2fb01f 90 $form->addEntityRef('link_to_case_id', ts('Link To Case'), [
abd06efc 91 'entity' => 'Case',
be2fb01f
CW
92 'api' => [
93 'extra' => ['case_id.case_type_id.title', 'contact_id.sort_name'],
94 'params' => [
95 'case_id' => ['NOT IN' => $excludeCaseIds],
abd06efc 96 'case_id.is_deleted' => 0,
be2fb01f
CW
97 ],
98 ],
99 ], TRUE);
6a488035
TO
100 }
101
102 /**
fe482240 103 * Global validation rules for the form.
6a488035 104 *
64bd5a0e
TO
105 * @param array $values
106 * Posted values of the form.
6a488035 107 *
77b97be7 108 * @param $files
c490a46a 109 * @param CRM_Core_Form $form
77b97be7 110 *
a6c01b45
CW
111 * @return array
112 * list of errors to be posted back to the form
6a488035 113 */
00be9182 114 public static function formRule($values, $files, $form) {
be2fb01f 115 $errors = [];
6a488035
TO
116
117 $linkCaseId = CRM_Utils_Array::value('link_to_case_id', $values);
251924af 118 assert('is_numeric($linkCaseId)');
eb20fbf0 119 if ($linkCaseId == CRM_Utils_Array::first($form->_caseId)) {
6a488035
TO
120 $errors['link_to_case'] = ts('Please select some other case to link.');
121 }
122
123 // do check for existing related cases.
124 $relatedCases = $form->get('relatedCases');
125 if (is_array($relatedCases) && array_key_exists($linkCaseId, $relatedCases)) {
f72119d9 126 $errors['link_to_case'] = ts('Selected case is already linked.');
6a488035
TO
127 }
128
129 return empty($errors) ? TRUE : $errors;
130 }
131
132 /**
fe482240 133 * Process the form submission.
6a488035 134 *
6a488035 135 *
c490a46a
CW
136 * @param CRM_Core_Form $form
137 * @param array $params
6a488035 138 */
e547f744
TO
139 public static function beginPostProcess(&$form, &$params) {
140 }
6a488035
TO
141
142 /**
fe482240 143 * Process the form submission.
6a488035 144 *
6a488035 145 *
c490a46a
CW
146 * @param CRM_Core_Form $form
147 * @param array $params
df371444 148 * @param CRM_Activity_BAO_Activity $activity
6a488035 149 */
00be9182 150 public static function endPostProcess(&$form, &$params, &$activity) {
6a488035
TO
151 $activityId = $activity->id;
152 $linkCaseID = CRM_Utils_Array::value('link_to_case_id', $params);
153
154 //create a link between two cases.
155 if ($activityId && $linkCaseID) {
be2fb01f 156 $caseParams = [
6a488035
TO
157 'case_id' => $linkCaseID,
158 'activity_id' => $activityId,
be2fb01f 159 ];
6a488035
TO
160 CRM_Case_BAO_Case::processCaseActivity($caseParams);
161 }
162 }
96025800 163
6a488035 164}