1: <?php
2: namespace Mapbender\KmlBundle\Element;
3:
4: use Mapbender\CoreBundle\Component\Element;
5: use Symfony\Component\DependencyInjection\ContainerInterface;
6: use Symfony\Component\HttpFoundation\Response;
7:
8: class KmlExport extends Element {
9: public function getTitle() {
10: return "Please give me a title";
11: }
12:
13: public function getDescription() {
14: return "Please give me a description";
15: }
16:
17: public function getTags() {
18: return array();
19: }
20:
21:
22: public function getWidgetName() {
23: return 'mapbender.mbKmlExport';
24: }
25:
26: public function getAssets() {
27: return array(
28: 'js' => array(
29: 'mapbender.element.kmlexport.js'
30: ),
31: 'css' => array()
32: );
33: }
34:
35: public function getConfiguration() {
36: $opts = $this->configuration;
37: $opts['text'] = $this->name;
38:
39: if(array_key_exists('target', $this->configuration)) {
40: $elementId = $this->configuration['target'];
41: $finalId = $this->application->getFinalId($elementId);
42: $opts = array_merge($opts, array('target' => $finalId));
43: }
44: return array(
45: 'options' => $opts,
46: 'init' => 'mbKmlExport',
47: );
48: }
49:
50: public function httpAction($action) {
51: switch($action) {
52: case 'mapexport':
53: return $this->map2Kml();
54: }
55: }
56:
57: private function map2Kml() {
58: $response = new Response();
59:
60: $layers = $this->get('request')->get('layers');
61: foreach($layers as $title => &$layer) {
62: parse_str($layer, $layer);
63:
64: $layer['params']['LAYERS'] = implode(',',
65: $layer['options']['layers']);
66:
67: $layer['params']['WIDTH'] = 512;
68: $layer['params']['HEIGHT'] = 512;
69: $layer['params']['SRS'] = 'EPSG:4326';
70:
71: $delimiter = strpos($layer['options']['url'], '?') === False ?
72: '?' : '&';
73: $layer['getMap'] = $layer['options']['url'] . $delimiter
74: . http_build_query($layer['params']);
75: }
76:
77:
78: $extent = new \rectObj();
79: $extentIn = explode(',', $this->get('request')->get('extent'));
80: $extent->setExtent($extentIn[0], $extentIn[1], $extentIn[2], $extentIn[3]);
81:
82: $srs = $this->get('request')->get('srs');
83: $srsFrom = new \projectionObj($srs);
84: $srsTo = new \projectionObj('EPSG:4326');
85: $extent->project($srsFrom, $srsTo);
86:
87: $xml = $this->get('templating')
88: ->render('MapbenderKmlBundle:Element:kmlexport_map.kml.twig',
89: array('layers' => $layers, 'extent' => array(
90: 'minx' => $extent->minx,
91: 'miny' => $extent->miny,
92: 'maxx' => $extent->maxx,
93: 'maxy' => $extent->maxy,
94: )));
95:
96: $response->setContent($xml);
97: $response->headers->set('Content-Type',
98: 'application/vnd.google-earth.kml+xml');
99: $response->headers->set('Content-Disposition',
100: 'attachment; filename="bkg.kml"');
101: return $response;
102: }
103:
104: public function render() {
105: return $this->get('templating')
106: ->render('MapbenderKmlBundle:Element:kmlexport.html.twig', array(
107: 'id' => $this->id,
108: 'application' => $this->application->getSlug(),
109: 'configuration' => $this->configuration,
110: 'label' => $this->name));
111: }
112: }
113: