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