APIv4 - Enable getFields to find fields across implicit FK joins
[civicrm-core.git] / Civi / Report / OutputHandlerFactory.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 namespace Civi\Report;
12
13 /**
14 * OutputHandlers can either be the standard core ones: print/pdf/csv, or
15 * extensions can add their own.
16 *
17 * @package Civi\Report
18 */
19 class OutputHandlerFactory {
20
21 protected static $singleton;
22
23 /**
24 * @var array
25 * Array of registered possible OutputHandlers.
26 */
27 protected static $registered = [];
28
29 /**
30 * Singleton function.
31 *
32 * @return OutputHandlerFactory
33 */
34 public static function singleton() {
35 if (self::$singleton === NULL) {
36 self::$singleton = new OutputHandlerFactory();
37 self::registerBuiltins();
38 }
39 return self::$singleton;
40 }
41
42 /**
43 * Return an OutputHandler based on the parameters.
44 *
45 * @param \CRM_Report_Form $form
46 * A CiviReport that extends CRM_Report_Form.
47 *
48 * @return \Civi\Report\OutputHandlerInterface|NULL
49 * An object that implements the OutputHandlerInterface, or NULL if
50 * nothing suitable for the given parameters.
51 */
52 public function create(\CRM_Report_Form $form) {
53 /**
54 * The first draft of this had extensions register their classes,
55 * but it needed to be early on because there's also the dropdown on the
56 * report form that lists the output formats available which happens
57 * earlier than the output run and that worked better as a simple hook.
58 * So it just felt out of place then to have two different types of things,
59 * and people are used to hooks, and there's already alterReportVar which
60 * seemed a natural place.
61 */
62 \CRM_Utils_Hook::alterReportVar('outputhandlers', self::$registered, $form);
63 foreach (self::$registered as $candidate) {
64 try {
65 $outputHandler = new $candidate();
66 if ($outputHandler->isOutputHandlerFor($form)) {
67 $outputHandler->setForm($form);
68 return $outputHandler;
69 }
70 }
71 catch (\Exception $e) {
72 // no ts() since this is a sysadmin-y message
73 \Civi::log()->warn("Unable to use $candidate as an output handler. " . $e->getMessage());
74 }
75 }
76 return NULL;
77 }
78
79 /**
80 * Register an outputHandler to handle an output format.
81 *
82 * @param string $outputHandler
83 * The classname of a class that implements OutputHandlerInterface.
84 */
85 public function register(string $outputHandler) {
86 // Use classname as index to (a) avoid duplicates and (b) make it easier
87 // to unset/overwrite one via hook.
88 self::$registered[$outputHandler] = $outputHandler;
89 }
90
91 /**
92 * There are some handlers that were hard-coded in to the form before which
93 * have now been moved to outputhandlers.
94 */
95 private static function registerBuiltins() {
96 self::$singleton->register('\CRM_Report_OutputHandler_Print');
97 self::$singleton->register('\CRM_Report_OutputHandler_Csv');
98 self::$singleton->register('\CRM_Report_OutputHandler_Pdf');
99 }
100
101 }