3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
11 namespace Civi\Report
;
14 * OutputHandlers can either be the standard core ones: print/pdf/csv, or
15 * extensions can add their own.
17 * @package Civi\Report
19 class OutputHandlerFactory
{
21 protected static $singleton;
25 * Array of registered possible OutputHandlers.
27 protected static $registered = [];
32 * @return OutputHandlerFactory
34 public static function singleton() {
35 if (self
::$singleton === NULL) {
36 self
::$singleton = new OutputHandlerFactory();
37 self
::registerBuiltins();
39 return self
::$singleton;
43 * Return an OutputHandler based on the parameters.
45 * @param \CRM_Report_Form $form
46 * A CiviReport that extends CRM_Report_Form.
48 * @return \Civi\Report\OutputHandlerInterface|NULL
49 * An object that implements the OutputHandlerInterface, or NULL if
50 * nothing suitable for the given parameters.
52 public function create(\CRM_Report_Form
$form) {
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.
62 \CRM_Utils_Hook
::alterReportVar('outputhandlers', self
::$registered, $form);
63 foreach (self
::$registered as $candidate) {
65 $outputHandler = new $candidate();
66 if ($outputHandler->isOutputHandlerFor($form)) {
67 $outputHandler->setForm($form);
68 return $outputHandler;
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());
80 * Register an outputHandler to handle an output format.
82 * @param string $outputHandler
83 * The classname of a class that implements OutputHandlerInterface.
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;
92 * There are some handlers that were hard-coded in to the form before which
93 * have now been moved to outputhandlers.
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');