summaryrefslogtreecommitdiffstats
path: root/library/Icinga/Web/Widget/Tabextension/OutputFormat.php
blob: d5d83aff5de44ad9d576648ae84f3ed8771043da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */

namespace Icinga\Web\Widget\Tabextension;

use Icinga\Application\Platform;
use Icinga\Application\Hook;
use Icinga\Web\Url;
use Icinga\Web\Widget\Tab;
use Icinga\Web\Widget\Tabs;

/**
 * Tabextension that offers different output formats for the user in the dropdown area
 */
class OutputFormat implements Tabextension
{
    /**
     * PDF output type
     */
    const TYPE_PDF = 'pdf';

    /**
     * JSON output type
     */
    const TYPE_JSON = 'json';

    /**
     * CSV output type
     */
    const TYPE_CSV = 'csv';

    /**
     * An array of tabs to be added to the dropdown area
     *
     * @var array
     */
    private $tabs = array();

    /**
     * Create a new OutputFormat extender
     *
     * In general, it's assumed that all types are supported when an outputFormat extension
     * is added, so this class offers to remove specific types instead of adding ones
     *
     * @param array $disabled An array of output types to <b>not</b> show.
     */
    public function __construct(array $disabled = array())
    {
        foreach ($this->getSupportedTypes() as $type => $tabConfig) {
            if (!in_array($type, $disabled)) {
                $tabConfig['url'] = Url::fromRequest();
                $tab = new Tab($tabConfig);
                $tab->setTargetBlank();
                $this->tabs[] = $tab;
            }
        }
    }

    /**
     * Applies the format selectio to the provided tabset
     *
     * @param   Tabs $tabs The tabs object to extend with
     *
     * @see     Tabextension::apply()
     */
    public function apply(Tabs $tabs)
    {
        foreach ($this->tabs as $tab) {
            $tabs->addAsDropdown($tab->getName(), $tab);
        }
    }

    /**
     * Return an array containing the tab definitions for all supported types
     *
     * Using array_keys on this array or isset allows to check whether a
     * requested type is supported
     *
     * @return  array
     */
    public function getSupportedTypes()
    {
        $supportedTypes = array();

        $pdfexport = Hook::has('Pdfexport');

        if ($pdfexport || Platform::extensionLoaded('gd')) {
            $supportedTypes[self::TYPE_PDF] = array(
                'name'      => 'pdf',
                'label'     => 'PDF',
                'icon'      => 'file-pdf',
                'urlParams' => array('format' => 'pdf'),
            );
        }

        $supportedTypes[self::TYPE_CSV] = array(
            'name'      => 'csv',
            'label'     => 'CSV',
            'icon'      => 'file-excel',
            'urlParams' => array('format' => 'csv')
        );

        if (Platform::extensionLoaded('json')) {
            $supportedTypes[self::TYPE_JSON] = array(
                'name'      => 'json',
                'label'     => 'JSON',
                'icon'      => 'doc-text',
                'urlParams' => array('format' => 'json')
            );
        }

        return $supportedTypes;
    }
}