More phpdoc fixes, this time related to report form methods
[civicrm-core.git] / CRM / Report / OutputHandler / Csv.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 use Civi\Report\OutputHandlerInterface;
13 use Civi\Report\OutputHandlerBase;
14
15 /**
16 * CSV Report Output Handler
17 */
18 class CRM_Report_OutputHandler_Csv extends OutputHandlerBase implements OutputHandlerInterface {
19
20 /**
21 * Are we a suitable output handler based on the given form?
22 *
23 * The class member $form isn't set yet at this point since we don't
24 * even know if we're in play yet, so the form is a parameter.
25 *
26 * @param CRM_Report_Form $form
27 *
28 * @return bool
29 */
30 public function isOutputHandlerFor(CRM_Report_Form $form):bool {
31 return ($form->getOutputMode() === 'csv');
32 }
33
34 /**
35 * Return the download filename. This should be the "clean" name, not
36 * a munged temporary filename.
37 *
38 * @return string
39 */
40 public function getFileName():string {
41 return 'CiviReport.csv';
42 }
43
44 /**
45 * Return the html body of the email.
46 *
47 * @return string
48 */
49 public function getMailBody():string {
50 // @todo It would be nice if this was more end-user configurable, but
51 // keeping it the same as it was before for now.
52 $url = CRM_Utils_System::url('civicrm/report/instance/' . $this->getForm()->getID(), 'reset=1', TRUE);
53 return $this->getForm()->getReportHeader() . '<p>' . ts('Report URL') .
54 ": {$url}</p>" . '<p>' .
55 ts('The report is attached as a CSV file.') . '</p>' .
56 $this->getForm()->getReportFooter();
57 }
58
59 /**
60 * Return the report contents as a string, in this case the csv output.
61 *
62 * @return string
63 */
64 public function getOutputString():string {
65 //@todo Hmm. See note in CRM_Report_Form::endPostProcess about $rows.
66 $rows = $this->getForm()->getTemplate()->get_template_vars('rows');
67
68 // avoid pass-by-ref warning
69 $form = $this->getForm();
70
71 return CRM_Report_Utils_Report::makeCsv($form, $rows);
72 }
73
74 /**
75 * Set headers as appropriate and send the output to the browser.
76 */
77 public function download() {
78 //@todo Hmm. See note in CRM_Report_Form::endPostProcess about $rows.
79 $rows = $this->getForm()->getTemplate()->get_template_vars('rows');
80
81 // avoid pass-by-ref warning
82 $form = $this->getForm();
83
84 CRM_Report_Utils_Report::export2csv($form, $rows);
85 }
86
87 /**
88 * Mime type of the attachment.
89 *
90 * @return string
91 */
92 public function getMimeType():string {
93 return 'text/csv';
94 }
95
96 /**
97 * Charset of the attachment.
98 *
99 * @return string
100 */
101 public function getCharset():string {
102 return 'utf-8';
103 }
104
105 }