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