Merge pull request #17976 from civicrm/5.28
[civicrm-core.git] / CRM / Case / Form / ActivityToCase.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
df371444 19 * This class generates form components for building activity to a case.
6a488035
TO
20 */
21class CRM_Case_Form_ActivityToCase extends CRM_Core_Form {
22
23 /**
100fef9d 24 * Build all the data structures needed to build the form.
269ba3e5 25 *
26 * @throws \CRM_Core_Exception
6a488035 27 */
00be9182 28 public function preProcess() {
a3d827a7 29 $this->_activityId = CRM_Utils_Request::retrieve('activityId', 'Positive');
6a488035 30 if (!$this->_activityId) {
269ba3e5 31 throw new CRM_Core_Exception('required activity id is missing.');
6a488035
TO
32 }
33
a3d827a7 34 $this->_currentCaseId = CRM_Utils_Request::retrieve('caseId', 'Positive');
6a488035
TO
35 $this->assign('currentCaseId', $this->_currentCaseId);
36 $this->assign('buildCaseActivityForm', TRUE);
6a1e5601
MWMC
37
38 switch (CRM_Utils_Request::retrieve('fileOnCaseAction', 'String')) {
39 case 'move':
40 CRM_Utils_System::setTitle(ts('Move to Case'));
41 break;
42
43 case 'copy':
44 CRM_Utils_System::setTitle(ts('Copy to Case'));
45 break;
46
47 }
6a488035
TO
48 }
49
50 /**
269ba3e5 51 * Set default values for the form. For edit/view mode.
6a488035 52 *
f72119d9 53 * @return array
269ba3e5 54 *
55 * @throws \CRM_Core_Exception
56 * @throws \CiviCRM_API3_Exception
6a488035 57 */
00be9182 58 public function setDefaultValues() {
be2fb01f
CW
59 $defaults = [];
60 $params = ['id' => $this->_activityId];
6a488035
TO
61
62 CRM_Activity_BAO_Activity::retrieve($params, $defaults);
3b8550cf 63 $defaults['file_on_case_activity_subject'] = $defaults['subject'] ?? '';
050fc124 64 $defaults['file_on_case_target_contact_id'] = $defaults['target_contact'];
6a488035 65
7359cdbc 66 // If this contact has an open case, supply it as a default
bd343dcc 67 $cid = CRM_Utils_Request::retrieve('cid', 'Integer');
5ff596c0 68 if (!$cid) {
be2fb01f 69 $act = civicrm_api3('Activity', 'getsingle', ['id' => $this->_activityId, 'return' => 'target_contact_id']);
5ff596c0
CW
70 if (!empty($act['target_contact_id'])) {
71 $cid = $act['target_contact_id'][0];
72 }
73 }
bd343dcc 74 if ($cid) {
be2fb01f 75 $cases = civicrm_api3('CaseContact', 'get', [
abd06efc 76 'contact_id' => $cid,
be2fb01f
CW
77 'case_id' => ['!=' => $this->_currentCaseId],
78 'case_id.status_id' => ['!=' => "Closed"],
abd06efc 79 'case_id.is_deleted' => 0,
be2fb01f
CW
80 'case_id.end_date' => ['IS NULL' => 1],
81 'options' => ['limit' => 1],
abd06efc 82 'return' => 'case_id',
be2fb01f 83 ]);
abd06efc
CW
84 foreach ($cases['values'] as $record) {
85 $defaults['file_on_case_unclosed_case_id'] = $record['case_id'];
bd343dcc
CW
86 break;
87 }
88 }
6a488035
TO
89 return $defaults;
90 }
91
92 /**
fe482240 93 * Build the form object.
6a488035
TO
94 */
95 public function buildQuickForm() {
be2fb01f 96 $this->addEntityRef('file_on_case_unclosed_case_id', ts('Select Case'), [
abd06efc 97 'entity' => 'Case',
be2fb01f
CW
98 'api' => [
99 'extra' => ['contact_id'],
100 'params' => [
101 'case_id' => ['!=' => $this->_currentCaseId],
abd06efc 102 'case_id.is_deleted' => 0,
be2fb01f
CW
103 'case_id.status_id' => ['!=' => "Closed"],
104 'case_id.end_date' => ['IS NULL' => 1],
105 ],
106 ],
107 ], TRUE);
108 $this->addEntityRef('file_on_case_target_contact_id', ts('With Contact(s)'), ['multiple' => TRUE]);
109 $this->add('text', 'file_on_case_activity_subject', ts('Subject'), ['size' => 50]);
6a488035 110 }
96025800 111
6a488035 112}