1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CMEN\GoogleChartsBundle\Twig; |
4
|
|
|
|
5
|
|
|
use CMEN\GoogleChartsBundle\ChartOutput\ChartOutputInterface; |
6
|
|
|
use CMEN\GoogleChartsBundle\Exception\GoogleChartsException; |
7
|
|
|
use CMEN\GoogleChartsBundle\GoogleCharts\Chart; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @author Christophe Meneses |
11
|
|
|
*/ |
12
|
|
|
class GoogleChartsExtension extends \Twig_Extension |
13
|
|
|
{ |
14
|
|
|
/** @var ChartOutputInterface */ |
15
|
|
|
private $chartOutput; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* GoogleChartsExtension constructor. |
19
|
|
|
* |
20
|
|
|
* @param ChartOutputInterface $chartOutput |
21
|
|
|
*/ |
22
|
|
|
public function __construct(ChartOutputInterface $chartOutput) |
23
|
|
|
{ |
24
|
|
|
$this->chartOutput = $chartOutput; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
|
|
public function getFunctions() |
31
|
|
|
{ |
32
|
|
|
return [ |
33
|
|
|
new \Twig_SimpleFunction('gc_draw', [$this, 'gcDraw'], ['is_safe' => ['html']]), |
34
|
|
|
new \Twig_SimpleFunction('gc_start', [$this, 'gcStart'], ['is_safe' => ['html']]), |
35
|
|
|
new \Twig_SimpleFunction('gc_end', [$this, 'gcEnd'], ['is_safe' => ['html']]), |
36
|
|
|
new \Twig_SimpleFunction('gc_event', [$this, 'gcEvent'], ['is_safe' => ['html']]), |
37
|
|
|
new \Twig_SimpleFunction('gc_language', [$this, 'gcLanguage']), |
38
|
|
|
]; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Returns the Javascript for the beginning of one or more charts. |
43
|
|
|
* |
44
|
|
|
* @param Chart|Chart[] $charts Chart instance or array of Chart instance |
45
|
|
|
* @param string|string[]|null $elementsID HTML element ID or array of HTML elements IDs. Can be null |
46
|
|
|
* |
47
|
|
|
* @return string |
48
|
|
|
* |
49
|
|
|
* @throws GoogleChartsException |
50
|
|
|
*/ |
51
|
|
|
public function gcStart($charts, $elementsID = null) |
52
|
|
|
{ |
53
|
|
|
return $this->chartOutput->startCharts($charts, $elementsID); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Returns the Javascript for the end of one or more charts. |
58
|
|
|
* |
59
|
|
|
* @param Chart|Chart[] $charts Chart instance or array of Chart instance |
60
|
|
|
* |
61
|
|
|
* @return string |
62
|
|
|
* |
63
|
|
|
* @throws GoogleChartsException |
64
|
|
|
*/ |
65
|
|
|
public function gcEnd($charts) |
66
|
|
|
{ |
67
|
|
|
return $this->chartOutput->endCharts($charts); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Returns the full Javascript to draw one or more charts. |
72
|
|
|
* |
73
|
|
|
* @param Chart|Chart[] $charts Chart instance or array of Chart instance |
74
|
|
|
* @param string|string[]|null $elementsID HTML element ID or array of HTML elements IDs. Can be null |
75
|
|
|
* |
76
|
|
|
* @return string |
77
|
|
|
* |
78
|
|
|
* @throws GoogleChartsException |
79
|
|
|
*/ |
80
|
|
|
public function gcDraw($charts, $elementsID = null) |
81
|
|
|
{ |
82
|
|
|
return $this->chartOutput->fullCharts($charts, $elementsID); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Add an event to a chart. |
87
|
|
|
* |
88
|
|
|
* @param Chart $chart A Chart instance |
89
|
|
|
* @param string $type Type of event |
90
|
|
|
* @param string $functionName Name of Javascript function |
91
|
|
|
*/ |
92
|
|
|
public function gcEvent(Chart $chart, $type, $functionName) |
93
|
|
|
{ |
94
|
|
|
$chart->getEvents()->addListener($type, $functionName); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Set the locale. Must be called before drawing charts. |
99
|
|
|
* |
100
|
|
|
* @see https://developers.google.com/chart/interactive/docs/basic_load_libs#loadwithlocale |
101
|
|
|
* |
102
|
|
|
* @param string $language Locale, for example : "fr" |
103
|
|
|
*/ |
104
|
|
|
public function gcLanguage($language) |
105
|
|
|
{ |
106
|
|
|
$this->chartOutput->setLanguage($language); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritdoc} |
111
|
|
|
*/ |
112
|
|
|
public function getName() |
113
|
|
|
{ |
114
|
|
|
return 'cmen_google_charts_extension'; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|