3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
35 * This class generates form components for LinkCase Activity.
37 class CRM_Case_Form_Activity_LinkCases
{
40 * @param CRM_Core_Form $form
44 public static function preProcess(&$form) {
45 if (empty($form->_caseId
)) {
46 CRM_Core_Error
::fatal(ts('Case Id not found.'));
48 if (count($form->_caseId
) != 1) {
49 CRM_Core_Resources
::fatal(ts('Expected one case-type'));
52 $caseId = CRM_Utils_Array
::first($form->_caseId
);
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));
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);
67 * Set default values for the form.
69 * @param CRM_Core_Form $form
73 public static function setDefaultValues(&$form) {
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'];
82 * @param CRM_Core_Form $form
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));
90 $form->addEntityRef('link_to_case_id', ts('Link To Case'), [
93 'extra' => ['case_id.case_type_id.title', 'contact_id.sort_name'],
95 'case_id' => ['NOT IN' => $excludeCaseIds],
96 'case_id.is_deleted' => 0,
103 * Global validation rules for the form.
105 * @param array $values
106 * Posted values of the form.
109 * @param CRM_Core_Form $form
112 * list of errors to be posted back to the form
114 public static function formRule($values, $files, $form) {
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.');
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.');
129 return empty($errors) ?
TRUE : $errors;
133 * Process the form submission.
136 * @param CRM_Core_Form $form
137 * @param array $params
139 public static function beginPostProcess(&$form, &$params) {
143 * Process the form submission.
146 * @param CRM_Core_Form $form
147 * @param array $params
148 * @param CRM_Activity_BAO_Activity $activity
150 public static function endPostProcess(&$form, &$params, &$activity) {
151 $activityId = $activity->id
;
152 $linkCaseID = CRM_Utils_Array
::value('link_to_case_id', $params);
154 //create a link between two cases.
155 if ($activityId && $linkCaseID) {
157 'case_id' => $linkCaseID,
158 'activity_id' => $activityId,
160 CRM_Case_BAO_Case
::processCaseActivity($caseParams);