ChartViewHelper.php 4.27 KB
Newer Older
1
<?php
2
3
namespace T3o\TerFe2\ViewHelpers;

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
/*******************************************************************
 *  Copyright notice
 *
 *  (c) 2011 Kai Vogel <kai.vogel@speedprogs.de>, Speedprogs.de
 *
 *  All rights reserved
 *
 *  This script is part of the TYPO3 project. The TYPO3 project is
 *  free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as
 *  published by the Free Software Foundation; either version 2 of
 *  the License, or (at your option) any later version.
 *
 *  The GNU General Public License can be found at
 *  http://www.gnu.org/copyleft/gpl.html.
 *
 *  This script is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  This copyright notice MUST APPEAR in all copies of the script!
 ******************************************************************/

/**
 * Chart view helper
 *
 * For documentation and examples visit http://www.jqplot.com
 */
33
class ChartViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper
34
35
36
37
38
39
40
41
42
43
44
{

    /**
     * Disable the escaping interceptor
     */
    protected $escapingInterceptorEnabled = FALSE;

    /**
     * @var string
     */
    protected $chart = '
45
			<div id="%1$s" style="height:%2$s;width:%3$s;" class="chart-container"></div>
46
			<script type="text/javascript">
47
48
49
50
51
52
53
54
55
				if (typeof(charts) == \'undefined\') {
					var charts = [];
				}
				charts[\'%1$s\'] = {
					lines: %4$s,
					options: {%5$s},
					isShy: %6$s,
					isRendered: false
				};
56
			</script>
57
58
		';

speedprogs_de's avatar
speedprogs_de committed
59

60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
    /**
     * Renders a jqPlot chart
     *
     * @param object $object The object to get chart from
     * @param string $method The render method
     * @param string $title Title of the chart
     * @param integer $height Height of the chart
     * @param integer $width Width of the chart
     * @param string $color Color of the line
     * @param integer $pointCount Count of points to render in one line
     * @param boolean $renderOnLoad Render chart when DOM is ready
     * @return string Chart
     */
    public function render($object = NULL, $method = 'downloadsByVersion', $title = '', $height = 300, $width = 400, $color = '#4D4D4D', $pointCount = 10, $renderOnLoad = TRUE)
    {
        if ($object === NULL) {
            $object = $this->renderChildren();
        }

        // Check object type
80
        if (!$object instanceof \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface) {
81
            throw new \Exception('Charts can only be rendered for domain objects yet');
82
83
84
85
        }

        // Check given method name
        if (empty($method)) {
86
            throw new \Exception('Can not render a chart without render method');
87
88
89
90
91
        }

        // Get method name
        $method = 'get' . ucfirst(trim($method));
        if (!method_exists($this, $method)) {
92
            throw new \Exception('No method with name "' . $method . '" defined in chart view helper');
93
94
95
96
97
98
99
100
        }

        // Get chart options
        $id = uniqid('chart-');
        $height = (int)$height . 'px';
        $width = (int)$width . 'px';
        $lines = json_encode($this->$method($object, (int)$pointCount));
        $options = '
101
102
				title: \'' . $title . '\',
				series: [{color:\'' . $color . '\'}]
103
			';
104
105
106
107
108
109
110
111
112
        $isShy = (empty($renderOnLoad) ? 'true' : 'false');

        return sprintf($this->chart, $id, $height, $width, $lines, $options, $isShy);
    }


    /**
     * Returns downloads by version
     *
113
     * @param \Tx_TerFe2_Domain_Model_Extension Extension object
114
115
116
     * @param integer $pointCount Count of points to render in one line
     * @return array Lines to render in chart
     */
117
    protected function getDownloadsByVersion(\Tx_TerFe2_Domain_Model_Extension $extension, $pointCount = 10)
118
119
120
121
122
123
124
125
126
127
128
129
    {
        $points = array();
        $versions = $extension->getReverseVersionsByVersionNumber();
        $counter = 0;

        foreach ($versions as $version) {
            if ($counter === $pointCount) {
                break;
            }
            $counter++;
            $points[] = array((string)$version->getVersionString(), (int)$version->getAllDownloads());
        }
130

131
        krsort($points);
132

133
134
        return array(array_values($points));
    }
135

speedprogs_de's avatar
speedprogs_de committed
136

137
}