Merge pull request #11702 from jmcclelland/pdf-receipt-filename
[civicrm-core.git] / CRM / Case / Form / Activity / LinkCases.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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-2018
32 */
33
34 /**
35 * This class generates form components for LinkCase Activity.
36 */
37 class CRM_Case_Form_Activity_LinkCases {
38 /**
39 * @param CRM_Core_Form $form
40 *
41 * @throws Exception
42 */
43 public static function preProcess(&$form) {
44 if (empty($form->_caseId)) {
45 CRM_Core_Error::fatal(ts('Case Id not found.'));
46 }
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);
52
53 $form->assign('clientID', $form->_currentlyViewedContactId);
54 $form->assign('sortName', CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $form->_currentlyViewedContactId, 'sort_name'));
55 $form->assign('caseTypeLabel', CRM_Case_BAO_Case::getCaseType($caseId));
56
57 // get the related cases for given case.
58 $relatedCases = $form->get('relatedCases');
59 if (!isset($relatedCases)) {
60 $relatedCases = CRM_Case_BAO_Case::getRelatedCases($caseId);
61 $form->set('relatedCases', empty($relatedCases) ? FALSE : $relatedCases);
62 }
63 }
64
65 /**
66 * Set default values for the form.
67 *
68 * @param CRM_Core_Form $form
69 *
70 * @return array
71 */
72 public static function setDefaultValues(&$form) {
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;
78 }
79
80 /**
81 * @param CRM_Core_Form $form
82 */
83 public static function buildQuickForm(&$form) {
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);
99 }
100
101 /**
102 * Global validation rules for the form.
103 *
104 * @param array $values
105 * Posted values of the form.
106 *
107 * @param $files
108 * @param CRM_Core_Form $form
109 *
110 * @return array
111 * list of errors to be posted back to the form
112 */
113 public static function formRule($values, $files, $form) {
114 $errors = array();
115
116 $linkCaseId = CRM_Utils_Array::value('link_to_case_id', $values);
117 assert('is_numeric($linkCaseId)');
118 if ($linkCaseId == CRM_Utils_Array::first($form->_caseId)) {
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)) {
125 $errors['link_to_case'] = ts('Selected case is already linked.');
126 }
127
128 return empty($errors) ? TRUE : $errors;
129 }
130
131 /**
132 * Process the form submission.
133 *
134 *
135 * @param CRM_Core_Form $form
136 * @param array $params
137 */
138 public static function beginPostProcess(&$form, &$params) {
139 }
140
141 /**
142 * Process the form submission.
143 *
144 *
145 * @param CRM_Core_Form $form
146 * @param array $params
147 * @param CRM_Activity_BAO_Activity $activity
148 */
149 public static function endPostProcess(&$form, &$params, &$activity) {
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 }
162
163 }