1: <?php
2:
3: 4: 5: 6: 7:
8:
9: namespace Mapbender\ManagerBundle\Controller;
10:
11: use Symfony\Bundle\FrameworkBundle\Controller\Controller;
12: use FOM\ManagerBundle\Configuration\Route as ManagerRoute;
13: use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
14: use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
15: use Symfony\Component\HttpFoundation\Response;
16: use Mapbender\WmsBundle\Entity\WmsSource;
17: use Mapbender\CoreBundle\Entity\Source;
18: use Symfony\Component\Security\Acl\Domain\ObjectIdentity;
19: use Symfony\Component\Security\Core\Exception\AccessDeniedException;
20: use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
21:
22: 23: 24:
25: class RepositoryController extends Controller {
26: 27: 28: 29: 30: 31: 32:
33: public function indexAction($page) {
34: $securityContext = $this->get('security.context');
35: $oid = new ObjectIdentity('class', 'Mapbender\CoreBundle\Entity\Source');
36:
37: $em = $this->getDoctrine()->getEntityManager();
38: $query = $em->createQuery(
39: "SELECT s FROM MapbenderCoreBundle:Source s ORDER BY s.id ASC");
40: $sources = $query->getResult();
41:
42: $allowed_sources = array();
43: foreach($sources as $source) {
44: if(!$securityContext->isGranted('VIEW', $source)) {
45: continue;
46: }
47: $allowed_sources[] = $source;
48: }
49:
50: return array(
51: 'title' => 'Repository',
52: 'sources' => $allowed_sources,
53: 'create_permission' => $securityContext->isGranted('CREATE', $oid)
54: );
55: }
56:
57: 58: 59: 60: 61: 62: 63:
64: public function newAction()
65: {
66: $securityContext = $this->get('security.context');
67: $oid = new ObjectIdentity('class', 'Mapbender\CoreBundle\Entity\Source');
68:
69: if(false === $securityContext->isGranted('CREATE', $oid)) {
70: throw new AccessDeniedException();
71: }
72:
73: $managers = $this->get('mapbender')->getRepositoryManagers();
74: return array(
75: 'managers' => $managers
76: );
77: }
78:
79: 80: 81: 82: 83:
84: public function viewAction($sourceId){
85: $source = $this->getDoctrine()
86: ->getRepository("MapbenderCoreBundle:Source")->find($sourceId);
87: $managers = $this->get('mapbender')->getRepositoryManagers();
88: $manager = $managers[$source->getManagertype()];
89: return $this->forward(
90: $manager['bundle'] . ":" . "Repository:view",
91: array("id" => $source->getId())
92: );
93: }
94:
95: 96: 97: 98: 99: 100: 101: 102: 103: 104:
105:
106: 107: 108: 109: 110:
111: public function deleteAction($sourceId){
112: $source = $this->getDoctrine()
113: ->getRepository("MapbenderCoreBundle:Source")->find($sourceId);
114:
115: $securityContext = $this->get('security.context');
116: if(false === $securityContext->isGranted('DELETE', $source)) {
117: throw new AccessDeniedException();
118: }
119:
120: $managers = $this->get('mapbender')->getRepositoryManagers();
121: $manager = $managers[$source->getManagertype()];
122: return $this->forward(
123: $manager['bundle'] . ":" . "Repository:delete",
124: array("sourceId" => $source->getId())
125: );
126: }
127:
128: 129: 130: 131:
132: public function instanceAction($slug, $instanceId){
133: $sourceInst = $this->getDoctrine()
134: ->getRepository("MapbenderCoreBundle:SourceInstance")
135: ->find($instanceId);
136:
137: if(null === $sourceInst) {
138: throw $this->createNotFoundException('Instance does not exist');
139: }
140:
141: $securityContext = $this->get('security.context');
142: if(!$securityContext->isGranted('EDIT', $sourceInst->getSource())) {
143: throw new AccessDeniedHttpException();
144: }
145:
146: $managers = $this->get('mapbender')->getRepositoryManagers();
147: $manager = $managers[$sourceInst->getManagertype()];
148: return $this->forward(
149: $manager['bundle'] . ":" . "Repository:instance",
150: array("slug" => $slug, "instanceId" => $sourceInst->getId())
151: );
152: }
153:
154: 155: 156: 157:
158: public function instanceWeightAction($slug, $layersetId, $instanceId){
159: $number = $this->get("request")->get("number");
160: $layersetId_new = $this->get("request")->get("new_layersetId");
161:
162: $instance = $this->getDoctrine()
163: ->getRepository('MapbenderWmsBundle:WmsInstance')
164: ->findOneById($instanceId);
165:
166: if(!$instance)
167: {
168: throw $this->createNotFoundException('The wms instance with"
169: ." the id "' . $instanceId . '" does not exist.');
170: }
171: if(intval($number) === $instance->getWeight() && $layersetId === $layersetId_new)
172: {
173: return new Response(json_encode(array(
174: 'error' => '',
175: 'result' => 'ok')), 200,
176: array('Content-Type' => 'application/json'));
177: }
178:
179: if($layersetId === $layersetId_new)
180: {
181: $em = $this->getDoctrine()->getEntityManager();
182: $instance->setWeight($number);
183: $em->persist($instance);
184: $em->flush();
185: $query = $em->createQuery(
186: "SELECT i FROM MapbenderWmsBundle:WmsInstance i"
187: . " WHERE i.layerset=:lsid ORDER BY i.weight ASC");
188: $query->setParameters(array("lsid" => $layersetId));
189: $instList = $query->getResult();
190:
191: $num = 0;
192: foreach($instList as $inst)
193: {
194: if($num === intval($instance->getWeight()))
195: {
196: if($instance->getId() === $inst->getId())
197: {
198: $num++;
199: } else
200: {
201: $num++;
202: $inst->setWeight($num);
203: $num++;
204: }
205: } else
206: {
207: if($instance->getId() !== $inst->getId())
208: {
209: $inst->setWeight($num);
210: $num++;
211: }
212: }
213: }
214: foreach($instList as $inst)
215: {
216: $em->persist($inst);
217: }
218: $em->flush();
219: } else
220: {
221: $layerset_new = $this->getDoctrine()
222: ->getRepository("MapbenderCoreBundle:Layerset")
223: ->find($layersetId_new);
224: $em = $this->getDoctrine()->getEntityManager();
225: $instance->setLayerset($layerset_new);
226: $layerset_new->addInstance($instance);
227: $instance->setWeight($number);
228: $em->persist($layerset_new);
229: $em->persist($instance);
230: $em->flush();
231:
232:
233: $query = $em->createQuery(
234: "SELECT i FROM MapbenderWmsBundle:WmsInstance i"
235: . " WHERE i.layerset=:lsid ORDER BY i.weight ASC");
236: $query->setParameters(array("lsid" => $layersetId));
237: $instList = $query->getResult();
238:
239: $num = 0;
240: foreach($instList as $inst)
241: {
242: $inst->setWeight($num);
243: $em->persist($inst);
244: $num++;
245: }
246: $em->flush();
247:
248:
249: $query = $em->createQuery(
250: "SELECT i FROM MapbenderWmsBundle:WmsInstance i"
251: . " WHERE i.layerset=:lsid ORDER BY i.weight ASC");
252: $query->setParameters(array("lsid" => $layersetId_new));
253: $instList = $query->getResult();
254: $num = 0;
255: foreach($instList as $inst)
256: {
257: if($num === intval($instance->getWeight()))
258: {
259: if($instance->getId() === $inst->getId())
260: {
261: $num++;
262: } else
263: {
264: $num++;
265: $inst->setWeight($num);
266: $num++;
267: }
268: } else
269: {
270: if($instance->getId() !== $inst->getId())
271: {
272: $inst->setWeight($num);
273: $num++;
274: }
275: }
276: }
277: foreach($instList as $inst)
278: {
279: $em->persist($inst);
280: $em->flush();
281: }
282:
283: }
284:
285: return new Response(json_encode(array(
286: 'error' => '',
287: 'result' => 'ok')), 200, array(
288: 'Content-Type' => 'application/json'));
289: }
290:
291: 292: 293: 294:
295: public function instanceEnabledAction($slug, $layersetId, $instanceId){
296: $sourceInst = $this->getDoctrine()
297: ->getRepository("MapbenderCoreBundle:SourceInstance")
298: ->find($instanceId);
299: $managers = $this->get('mapbender')->getRepositoryManagers();
300: $manager = $managers[$sourceInst->getManagertype()];
301: return $this->forward(
302: $manager['bundle'] . ":" . "Repository:instanceenabled",
303: array("slug" => $slug,
304: "layersetId" => $layersetId,
305: "instanceId" => $sourceInst->getId())
306: );
307: }
308:
309: 310: 311: 312:
313: public function instanceLayerWeightAction($slug, $instanceId, $instLayerId){
314: $sourceInst = $this->getDoctrine()
315: ->getRepository("MapbenderCoreBundle:SourceInstance")
316: ->find($instanceId);
317: $managers = $this->get('mapbender')->getRepositoryManagers();
318: $manager = $managers[$sourceInst->getManagertype()];
319: return $this->forward(
320: $manager['bundle'] . ":" . "Repository:instancelayerpriority",
321: array("slug" => $slug,
322: "instanceId" => $sourceInst->getId(),
323: "instLayerId" => $instLayerId)
324: );
325: }
326:
327: }
328: