/
chart_content.php
114 lines (98 loc) · 4 KB
/
chart_content.php
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
function get_chart_content($chart, $user, $minified = false, $path = '') {
$theme_css = array();
$theme_js = array();
$next_theme_id = $chart->getTheme();
$locale = $user->getLanguage();
$themeLocale = null;
while (!empty($next_theme_id)) {
$theme = get_theme_meta($next_theme_id, $path);
$theme_js[] = '/static/themes/' . $next_theme_id . '/theme.js';
if ($theme['hasStyles']) {
$theme_css[] = '/static/themes/' . $next_theme_id . '/theme.css';
}
if (!empty($theme['hasLocaleJS'])) {
$theme_js[] = $theme['localeJS'];
if (empty($themeLocale)) $themeLocale = $theme['locale'];
}
$next_theme_id = $theme['extends'];
}
// theme locale overrides user locale
if (!empty($themeLocale)) $locale = $themeLocale;
// per-chart locale overrides theme locale
$chartLocale = $chart->getLanguage();
if (!empty($chartLocale)) $locale = $chartLocale;
$abs = 'http://' . $GLOBALS['dw_config']['domain'];
$base_js = array(
// use CDN instead
// $abs . '/static/vendor/globalize/globalize.min.js',
// $abs . '/static/vendor/underscore/underscore-min.js',
// $abs . '/static/vendor/jquery/jquery.min.js'
'//assets.datawrapper.s3.amazonaws.com/globalize.min.js',
'//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.2/underscore-min.js',
'//cdnjs.cloudflare.com/ajax/libs/jquery/1.8.2/jquery.min.js'
);
$vis_js = array();
$vis_css = array();
$next_vis_id = $chart->getType();
$vis_libs = array();
while (!empty($next_vis_id)) {
$vis = get_visualization_meta($next_vis_id, $path);
$vjs = array();
if (!empty($vis['libraries'])) {
foreach ($vis['libraries'] as $url) {
$vis_libs[] = '/static/vendor/' . $url;
}
}
$vjs[] = '/static/visualizations/' . $vis['id'] . '/' . $vis['id'] . '.js';
$vis_js = array_merge($vis_js, array_reverse($vjs));
if ($vis['hasCSS']) {
$vis_css[] = '/static/visualizations/' . $vis['id'] . '/style.css';
}
$next_vis_id = !empty($vis['extends']) ? $vis['extends'] : null;
}
$styles = array_merge($vis_css, array_reverse($theme_css));
$the_vis = get_visualization_meta($chart->getType(), $path);
$the_theme = get_theme_meta($chart->getTheme(), $path);
if ($minified) {
$scripts = array_merge(
$base_js,
array(
'/lib/vis/' . $the_vis['id'] . '-' . $the_vis['version'] . '.min.js',
'/lib/theme/' . $the_theme['id'] . '-' . $the_theme['version'] . '.min.js',
)
);
$styles = array($chart->getID().'.min.css');
} else {
$scripts = array_unique(
array_merge(
$base_js,
array('/static/js/dw.min.js'),
array_reverse($theme_js),
array_reverse($vis_js),
$vis_libs
)
);
}
$page = array(
'chartData' => $chart->loadData(),
'chart' => $chart,
'chartLocale' => str_replace('_', '-', $locale),
'metricPrefix' => get_metric_prefix($locale),
'theme' => $the_theme,
'visualization' => $the_vis,
'stylesheets' => $styles,
'scripts' => $scripts,
'themeJS' => array_reverse($theme_js),
'visJS' => array_merge(array_reverse($vis_js), $vis_libs),
'origin' => !empty($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '',
'DW_DOMAIN' => 'http://' . $GLOBALS['dw_config']['domain'] . '/',
'DW_CHART_DATA' => 'http://' . $GLOBALS['dw_config']['domain'] . '/chart/' . $chart->getID() . '/data',
'ASSET_PATH' => $minified ? '' : '/static/themes/'.$the_theme['id'].'/'
);
if (isset($GLOBALS['dw_config']['piwik'])) {
$page['PIWIK_URL'] = $GLOBALS['dw_config']['piwik']['url'];
$page['PIWIK_IDSITE'] = $GLOBALS['dw_config']['piwik']['idSite'];
}
return $page;
}