1: <?php
2:
3: namespace Mapbender\CoreBundle\Element;
4:
5: use Mapbender\CoreBundle\Component\Element;
6: use Symfony\Component\HttpFoundation\Response;
7: use Mapbender\CoreBundle\Element\Type\SearchRouterSelectType;
8: use Mapbender\CoreBundle\Element\Type\SearchRouterFormType;
9: use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
10:
11: 12: 13: 14: 15:
16: class SearchRouter extends Element
17: {
18:
19: 20: 21: 22:
23: protected $forms;
24:
25: 26: 27:
28: static public function getClassTitle()
29: {
30: return "Search Router";
31: }
32:
33: 34: 35:
36: static public function getClassDescription()
37: {
38: return "Configurable search routing element";
39: }
40:
41: 42: 43:
44: static public function getClassTags()
45: {
46: return array('Search', 'Router');
47: }
48:
49: 50: 51:
52: public function getWidgetName()
53: {
54: return 'mapbender.mbSearchRouter';
55: }
56:
57: 58: 59:
60: public function getAssets()
61: {
62: return array(
63: 'js' => array(
64: 'vendor/underscore.js',
65: 'vendor/json2.js',
66: 'vendor/backbone.js',
67: 'mapbender.element.searchRouter.Feature.js',
68: 'mapbender.element.searchRouter.Search.js',
69: 'mapbender.element.searchRouter.js'),
70: 'css' => array('mapbender.element.searchRouter.css'));
71: }
72:
73: 74: 75:
76: public function httpAction($action)
77: {
78: $response = new Response();
79: $response->headers->set('Content-Type', 'application/json');
80: $request = $this->container->get('request');
81:
82: list($target, $action) = explode('/', $action);
83: $conf = $this->getConfiguration();
84: if(!array_key_exists($target, $conf['routes']))
85: {
86: throw new NotFoundHttpException();
87: }
88:
89: if('autocomplete' === $action)
90: {
91: $data = json_decode($request->getContent());
92:
93:
94: $conf = $this->getConfiguration();
95: if(!array_key_exists($target, $conf['routes']))
96: {
97: throw new NotFoundHttpException();
98: }
99: $conf = $conf['routes'][$target];
100: $engine = new $conf['class']($this->container);
101:
102: $results = $engine->autocomplete(
103: $conf, $data->key, $data->value, $data->properties,
104: $data->srs, $data->extent);
105:
106: $response->setContent(json_encode(array(
107: 'key' => $data->key,
108: 'value' => $data->value,
109: 'properties' => $data->properties,
110: 'results' => $results
111: )));
112: return $response;
113: }
114:
115: if('search' === $action)
116: {
117:
118: $this->setupForms();
119: $form = $this->forms[$target];
120: $data = json_decode($request->getContent());
121: $form->bind(get_object_vars($data->properties));
122:
123: $conf = $conf['routes'][$target];
124: $engine = new $conf['class']($this->container);
125: $query = array(
126: 'form' => $form->getData(),
127: 'autocomplete_keys' => get_object_vars($data->autocomplete_keys)
128: );
129: $features = $engine->search(
130: $conf, $query, $request->get('srs'), $request->get('extent'));
131:
132:
133: $response->setContent(json_encode(array(
134: 'type' => 'FeatureCollection',
135: 'features' => $features,
136: 'query' => $query['form'])));
137: return $response;
138: }
139:
140: throw new NotFoundHttpException();
141: }
142:
143: 144: 145:
146: public function render()
147: {
148: return $this->container->get('templating')
149: ->render('MapbenderCoreBundle:Element:search_router.html.twig',
150: array('element' => $this));
151: }
152:
153: 154: 155: 156: 157:
158: public function getRouteSelectForm()
159: {
160: $configuration = $this->getConfiguration();
161:
162: $form = $this->container->get('form.factory')->createNamed(
163: 'search_routes', new SearchRouterSelectType(), null,
164: array('routes' => $configuration['routes']));
165:
166: return $form->createView();
167: }
168:
169: 170: 171:
172: protected function setupForms()
173: {
174: if(null === $this->forms)
175: {
176: $configuration = $this->getConfiguration();
177: foreach($configuration['routes'] as $name => $conf)
178: {
179: $this->forms[$name] = $this->setupForm($name, $conf);
180: }
181: }
182: }
183:
184: 185: 186: 187:
188: public function getForms()
189: {
190: if(null === $this->forms)
191: {
192: $this->setupForms();
193: }
194:
195: return $this->forms;
196: }
197:
198: 199: 200: 201: 202: 203: 204:
205: protected function setupForm($name, array $conf)
206: {
207: $form = $this->container->get('form.factory')->createNamed(
208: $name, new SearchRouterFormType(), null,
209: array('fields' => $conf));
210:
211: return $form;
212: }
213:
214: }
215: