INFRA-132 - CRM/Case - phpcbf
[civicrm-core.git] / CRM / Case / Form / Activity / LinkCases.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
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 */
00be9182 46 public static function preProcess(&$form) {
6a488035
TO
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 *
6a488035 76 *
c490a46a 77 * @param CRM_Core_Form $form
77b97be7 78 *
355ba699 79 * @return void
6a488035 80 */
00be9182 81 public static function setDefaultValues(&$form) {
6a488035
TO
82 return $defaults = array();
83 }
84
4c6ce474 85 /**
c490a46a 86 * @param CRM_Core_Form $form
4c6ce474 87 */
00be9182 88 public static function buildQuickForm(&$form) {
f72119d9 89 $form->add('text', 'link_to_case_id', ts('Link To Case'), array('class' => 'huge'), TRUE);
6a488035
TO
90 }
91
92 /**
100fef9d 93 * Global validation rules for the form
6a488035 94 *
64bd5a0e
TO
95 * @param array $values
96 * Posted values of the form.
6a488035 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
6a488035 103 */
00be9182 104 public static function formRule($values, $files, $form) {
6a488035
TO
105 $errors = array();
106
107 $linkCaseId = CRM_Utils_Array::value('link_to_case_id', $values);
251924af 108 assert('is_numeric($linkCaseId)');
eb20fbf0 109 if ($linkCaseId == CRM_Utils_Array::first($form->_caseId)) {
6a488035
TO
110 $errors['link_to_case'] = ts('Please select some other case to link.');
111 }
112
113 // do check for existing related cases.
114 $relatedCases = $form->get('relatedCases');
115 if (is_array($relatedCases) && array_key_exists($linkCaseId, $relatedCases)) {
f72119d9 116 $errors['link_to_case'] = ts('Selected case is already linked.');
6a488035
TO
117 }
118
119 return empty($errors) ? TRUE : $errors;
120 }
121
122 /**
c490a46a 123 * Process the form submission
6a488035 124 *
6a488035 125 *
c490a46a
CW
126 * @param CRM_Core_Form $form
127 * @param array $params
dd244018 128 *
355ba699 129 * @return void
6a488035 130 */
e547f744
TO
131 public static function beginPostProcess(&$form, &$params) {
132 }
6a488035
TO
133
134 /**
c490a46a 135 * Process the form submission
6a488035 136 *
6a488035 137 *
c490a46a
CW
138 * @param CRM_Core_Form $form
139 * @param array $params
6c8f6e67
EM
140 * @param $activity
141 *
355ba699 142 * @return void
6a488035 143 */
00be9182 144 public static function endPostProcess(&$form, &$params, &$activity) {
6a488035
TO
145 $activityId = $activity->id;
146 $linkCaseID = CRM_Utils_Array::value('link_to_case_id', $params);
147
148 //create a link between two cases.
149 if ($activityId && $linkCaseID) {
150 $caseParams = array(
151 'case_id' => $linkCaseID,
152 'activity_id' => $activityId,
153 );
154 CRM_Case_BAO_Case::processCaseActivity($caseParams);
155 }
156 }
157}