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