Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2014-01-13-11-57-38
[civicrm-core.git] / CRM / Case / Form / Activity / LinkCases.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
232624b1 4 | CiviCRM version 4.4 |
6a488035
TO
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
32 * $Id$
33 *
34 */
35
36/**
37 * This class generates form components for OpenCase Activity
38 *
39 */
40class CRM_Case_Form_Activity_LinkCases {
41 static function preProcess(&$form) {
42 if (!isset($form->_caseId)) {
43 CRM_Core_Error::fatal(ts('Case Id not found.'));
44 }
45
46 $form->assign('clientID', $form->_currentlyViewedContactId);
47 $form->assign('caseTypeLabel', CRM_Case_BAO_Case::getCaseType($form->_caseId));
48
49 // get the related cases for given case.
50 $relatedCases = $form->get('relatedCases');
51 if (!isset($relatedCases)) {
52 $relatedCases = CRM_Case_BAO_Case::getRelatedCases($form->_caseId, $form->_currentlyViewedContactId);
53 $form->set('relatedCases', empty($relatedCases) ? FALSE : $relatedCases);
54 }
55 $excludeCaseIds = array($form->_caseId);
56 if (is_array($relatedCases) && !empty($relatedCases)) {
57 $excludeCaseIds = array_merge($excludeCaseIds, array_keys($relatedCases));
58 }
59 $form->assign('excludeCaseIds', implode(',', $excludeCaseIds));
60 }
61
62 /**
63 * This function sets the default values for the form. For edit/view mode
64 * the default values are retrieved from the database
65 *
66 * @access public
67 *
355ba699 68 * @return void
6a488035
TO
69 */
70 static function setDefaultValues(&$form) {
71 return $defaults = array();
72 }
73
74 static function buildQuickForm(&$form) {
75 $form->add('text', 'link_to_case', ts('Link To Case'));
76 $form->add('hidden', 'link_to_case_id', '', array('id' => 'link_to_case_id'));
77 }
78
79 /**
80 * global validation rules for the form
81 *
82 * @param array $values posted values of the form
83 *
84 * @return array list of errors to be posted back to the form
85 * @static
86 * @access public
87 */
88 static function formRule($values, $files, $form) {
89 $errors = array();
90
91 $linkCaseId = CRM_Utils_Array::value('link_to_case_id', $values);
92 if (!$linkCaseId) {
93 $errors['link_to_case'] = ts('Please select a case to link.');
94 }
95 elseif ($linkCaseId == $form->_caseId) {
96 $errors['link_to_case'] = ts('Please select some other case to link.');
97 }
98
99 // do check for existing related cases.
100 $relatedCases = $form->get('relatedCases');
101 if (is_array($relatedCases) && array_key_exists($linkCaseId, $relatedCases)) {
102 $errors['link_to_case'] = ts('It looks like selected case is already linked.');
103 }
104
105 return empty($errors) ? TRUE : $errors;
106 }
107
108 /**
109 * Function to process the form
110 *
111 * @access public
112 *
355ba699 113 * @return void
6a488035
TO
114 */
115 static function beginPostProcess(&$form, &$params) {
116 $params['id'] = $params['case_id'];
117 }
118
119 /**
120 * Function to process the form
121 *
122 * @access public
123 *
355ba699 124 * @return void
6a488035
TO
125 */
126 static function endPostProcess(&$form, &$params, &$activity) {
127 $activityId = $activity->id;
128 $linkCaseID = CRM_Utils_Array::value('link_to_case_id', $params);
129
130 //create a link between two cases.
131 if ($activityId && $linkCaseID) {
132 $caseParams = array(
133 'case_id' => $linkCaseID,
134 'activity_id' => $activityId,
135 );
136 CRM_Case_BAO_Case::processCaseActivity($caseParams);
137 }
138 }
139}
140