Merge pull request #14673 from eileenmcnaughton/payment_create
[civicrm-core.git] / CRM / Case / Form / Activity / LinkCases.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2019
32 */
33
34 /**
35 * This class generates form components for LinkCase Activity.
36 */
37 class CRM_Case_Form_Activity_LinkCases {
38
39 /**
40 * @param CRM_Core_Form $form
41 *
42 * @throws Exception
43 */
44 public static function preProcess(&$form) {
45 if (empty($form->_caseId)) {
46 CRM_Core_Error::fatal(ts('Case Id not found.'));
47 }
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);
53
54 $form->assign('clientID', $form->_currentlyViewedContactId);
55 $form->assign('sortName', CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $form->_currentlyViewedContactId, 'sort_name'));
56 $form->assign('caseTypeLabel', CRM_Case_BAO_Case::getCaseType($caseId));
57
58 // get the related cases for given case.
59 $relatedCases = $form->get('relatedCases');
60 if (!isset($relatedCases)) {
61 $relatedCases = CRM_Case_BAO_Case::getRelatedCases($caseId);
62 $form->set('relatedCases', empty($relatedCases) ? FALSE : $relatedCases);
63 }
64 }
65
66 /**
67 * Set default values for the form.
68 *
69 * @param CRM_Core_Form $form
70 *
71 * @return array
72 */
73 public static function setDefaultValues(&$form) {
74 $defaults = [];
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;
79 }
80
81 /**
82 * @param CRM_Core_Form $form
83 */
84 public static function buildQuickForm(&$form) {
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 }
90 $form->addEntityRef('link_to_case_id', ts('Link To Case'), [
91 'entity' => 'Case',
92 'api' => [
93 'extra' => ['case_id.case_type_id.title', 'contact_id.sort_name'],
94 'params' => [
95 'case_id' => ['NOT IN' => $excludeCaseIds],
96 'case_id.is_deleted' => 0,
97 ],
98 ],
99 ], TRUE);
100 }
101
102 /**
103 * Global validation rules for the form.
104 *
105 * @param array $values
106 * Posted values of the form.
107 *
108 * @param $files
109 * @param CRM_Core_Form $form
110 *
111 * @return array
112 * list of errors to be posted back to the form
113 */
114 public static function formRule($values, $files, $form) {
115 $errors = [];
116
117 $linkCaseId = CRM_Utils_Array::value('link_to_case_id', $values);
118 assert('is_numeric($linkCaseId)');
119 if ($linkCaseId == CRM_Utils_Array::first($form->_caseId)) {
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)) {
126 $errors['link_to_case'] = ts('Selected case is already linked.');
127 }
128
129 return empty($errors) ? TRUE : $errors;
130 }
131
132 /**
133 * Process the form submission.
134 *
135 *
136 * @param CRM_Core_Form $form
137 * @param array $params
138 */
139 public static function beginPostProcess(&$form, &$params) {
140 }
141
142 /**
143 * Process the form submission.
144 *
145 *
146 * @param CRM_Core_Form $form
147 * @param array $params
148 * @param CRM_Activity_BAO_Activity $activity
149 */
150 public static function endPostProcess(&$form, &$params, &$activity) {
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) {
156 $caseParams = [
157 'case_id' => $linkCaseID,
158 'activity_id' => $activityId,
159 ];
160 CRM_Case_BAO_Case::processCaseActivity($caseParams);
161 }
162 }
163
164 }