Merge pull request #17641 from MegaphoneJon/core-1590
[civicrm-core.git] / CRM / Contact / Form / Task / PDF.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * This class provides the functionality to create PDF letter for a group of contacts or a single contact.
20 */
21 class CRM_Contact_Form_Task_PDF extends CRM_Contact_Form_Task {
22
23 /**
24 * All the existing templates in the system.
25 *
26 * @var array
27 */
28 public $_templates = NULL;
29
30 public $_single = NULL;
31
32 public $_cid = NULL;
33
34 public $_activityId = NULL;
35
36 /**
37 * Build all the data structures needed to build the form.
38 */
39 public function preProcess() {
40
41 $this->skipOnHold = $this->skipDeceased = FALSE;
42 CRM_Contact_Form_Task_PDFLetterCommon::preProcess($this);
43
44 // store case id if present
45 $this->_caseId = CRM_Utils_Request::retrieve('caseid', 'CommaSeparatedIntegers', $this, FALSE);
46 if (!empty($this->_caseId) && strpos($this->_caseId, ',')) {
47 $this->_caseIds = explode(',', $this->_caseId);
48 unset($this->_caseId);
49 }
50
51 // retrieve contact ID if this is 'single' mode
52 $cid = CRM_Utils_Request::retrieve('cid', 'CommaSeparatedIntegers', $this, FALSE);
53
54 if ($cid) {
55 // this is true in non-search context / single mode
56 // in search context 'id' is the default profile id for search display
57 // CRM-11227
58 $this->_activityId = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE);
59 }
60
61 if ($cid) {
62 CRM_Contact_Form_Task_PDFLetterCommon::preProcessSingle($this, $cid);
63 $this->_single = TRUE;
64 }
65 else {
66 parent::preProcess();
67 }
68 $this->assign('single', $this->_single);
69 }
70
71 /**
72 * Set default values for the form.
73 */
74 public function setDefaultValues() {
75 $defaults = [];
76 if (isset($this->_activityId)) {
77 $params = ['id' => $this->_activityId];
78 CRM_Activity_BAO_Activity::retrieve($params, $defaults);
79 $defaults['html_message'] = $defaults['details'] ?? NULL;
80 }
81 $defaults = $defaults + CRM_Contact_Form_Task_PDFLetterCommon::setDefaultValues();
82 return $defaults;
83 }
84
85 /**
86 * Build the form object.
87 */
88 public function buildQuickForm() {
89 //enable form element
90 $this->assign('suppressForm', FALSE);
91 CRM_Contact_Form_Task_PDFLetterCommon::buildQuickForm($this);
92 }
93
94 /**
95 * Process the form after the input has been submitted and validated.
96 */
97 public function postProcess() {
98 CRM_Contact_Form_Task_PDFLetterCommon::postProcess($this);
99 }
100
101 /**
102 * List available tokens for this form.
103 *
104 * @return array
105 */
106 public function listTokens() {
107 $tokens = CRM_Core_SelectValues::contactTokens();
108 if (isset($this->_caseId) || isset($this->_caseIds)) {
109 // For a single case, list tokens relevant for only that case type
110 $caseTypeId = isset($this->_caseId) ? CRM_Core_DAO::getFieldValue('CRM_Case_DAO_Case', $this->_caseId, 'case_type_id') : NULL;
111 $tokens += CRM_Core_SelectValues::caseTokens($caseTypeId);
112 }
113 return $tokens;
114 }
115
116 }