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 Symfony\Component\HttpFoundation\Request;
17: use Mapbender\CoreBundle\Entity\Application;
18: use Mapbender\ManagerBundle\Form\Type\ApplicationType;
19: use Mapbender\CoreBundle\Entity\Layerset;
20: use Mapbender\CoreBundle\Form\Type\LayersetType;
21:
22:
23: use Mapbender\CoreBundle\Entity\SourceInstance;
24: use Symfony\Component\Security\Acl\Domain\ObjectIdentity;
25: use Symfony\Component\Security\Core\Exception\AccessDeniedException;
26:
27: class ApplicationController extends Controller
28: {
29: 30: 31: 32: 33: 34: 35: 36:
37: public function indexAction()
38: {
39: $securityContext = $this->get('security.context');
40: $oid = new ObjectIdentity('class', 'Mapbender\CoreBundle\Entity\Application');
41:
42: $applications = $this->get('mapbender')->getApplicationEntities();
43: $allowed_applications = array();
44: foreach($applications as $application)
45: {
46: if($securityContext->isGranted('VIEW', $application))
47: {
48: if(!$application->isPublished() && !$securityContext->isGranted('OWNER', $application)) {
49: continue;
50: }
51: $allowed_applications[] = $application;
52: }
53: }
54:
55: return array(
56: 'applications' => $allowed_applications,
57: 'create_permission' => $securityContext->isGranted('CREATE', $oid)
58: );
59: }
60:
61: 62: 63: 64: 65: 66: 67:
68: public function newAction()
69: {
70: $application = new Application();
71:
72:
73: $this->checkGranted('CREATE', $application);
74:
75: $form = $this->createApplicationForm($application);
76:
77: return array(
78: 'application' => $application,
79: 'form' => $form->createView(),
80: 'form_name' => $form->getName());
81: }
82:
83: 84: 85: 86: 87: 88: 89:
90: public function createAction()
91: {
92: $application = new Application();
93:
94:
95: $this->checkGranted('CREATE', $application);
96:
97: $form = $this->createApplicationForm($application);
98: $request = $this->getRequest();
99:
100: $form->bindRequest($request);
101: if($form->isValid())
102: {
103: $application->setUpdated(new \DateTime('now'));
104: $application->setTemplate("Mapbender\CoreBundle\Template\Fullscreen");
105: $em = $this->getDoctrine()->getEntityManager();
106:
107: $em->getConnection()->beginTransaction();
108: $em->persist($application);
109: $em->flush();
110:
111: $aclManager = $this->get('fom.acl.manager');
112: $aclManager->setObjectACLFromForm($application, $form->get('acl'),
113: 'object');
114:
115: $em->getConnection()->commit();
116:
117: $this->get('session')->setFlash('success',
118: 'Your application has been saved.');
119:
120: return $this->redirect(
121: $this->generateUrl('mapbender_manager_application_index'));
122: }
123:
124: return array(
125: 'application' => $application,
126: 'form' => $form->createView(),
127: 'form_name' => $form->getName());
128: }
129:
130: 131: 132: 133: 134: 135: 136:
137: public function editAction($slug)
138: {
139: $application = $this->get('mapbender')->getApplicationEntity($slug);
140:
141:
142: $this->checkGranted('EDIT', $application);
143:
144: $form = $this->createApplicationForm($application);
145:
146: $templateClass = $application->getTemplate();
147: $em = $this->getDoctrine()->getEntityManager();
148: $query = $em->createQuery(
149: "SELECT s FROM MapbenderCoreBundle:Source s ORDER BY s.id ASC");
150: $sources = $query->getResult();
151:
152: return array(
153: 'application' => $application,
154: 'regions' => $templateClass::getRegions(),
155: 'slug' => $slug,
156: 'available_elements' => $this->getElementList(),
157: 'sources' => $sources,
158: 'form' => $form->createView(),
159: 'form_name' => $form->getName());
160: }
161:
162: 163: 164: 165: 166: 167:
168: public function updateAction($slug)
169: {
170: $application = $this->get('mapbender')->getApplicationEntity($slug);
171:
172:
173: $this->checkGranted('EDIT', $application);
174: $templateClassOld = $application->getTemplate();
175: $form = $this->createApplicationForm($application);
176: $request = $this->getRequest();
177:
178: $form->bindRequest($request);
179: if($form->isValid())
180: {
181: $em = $this->getDoctrine()->getEntityManager();
182:
183: $em->getConnection()->beginTransaction();
184: $templateClassNew = $application->getTemplate();
185: $regions = $templateClassNew::getRegions();
186: if($templateClassOld !== $templateClassNew && count($regions) > 0)
187: {
188: foreach($application->getElements() as $element)
189: {
190: if(!in_array($element->getRegion(), $regions))
191: {
192: $element->setRegion($regions[0]);
193: }
194: }
195: }
196: $application->setUpdated(new \DateTime('now'));
197:
198: try
199: {
200: $em->flush();
201:
202: $aclManager = $this->get('fom.acl.manager');
203: $aclManager->setObjectACLFromForm($application,
204: $form->get('acl'), 'object');
205:
206: $em->getConnection()->commit();
207:
208: $this->get('session')->setFlash('success',
209: 'Your application has been updated.');
210: } catch(\Exception $e)
211: {
212: $this->get('session')->setFlash('error',
213: 'There was an error trying to save your application.');
214: $em->getConnection()->rollback();
215: $em->close();
216:
217: if($this->container->getParameter('kernel.debug'))
218: {
219: throw($e);
220: }
221: }
222: return $this->redirect(
223: $this->generateUrl('mapbender_manager_application_edit',
224: array(
225: 'slug' => $application->getSlug())));
226: } else {
227: $application->setSlug($slug);
228: }
229:
230: $error = "error";
231:
232: if(count($form->getErrors()) > 0)
233: {
234: $error = $form->getErrors();
235: $error = $error[0]->getMessageTemplate();
236: } else
237: {
238: foreach($form->getChildren() as $child)
239: {
240: if(count($child->getErrors()) > 0)
241: {
242: $error = $child->getErrors();
243: $error = $error[0]->getMessageTemplate();
244: break;
245: }
246: }
247: }
248:
249: $templateClass = $application->getTemplate();
250: $em = $this->getDoctrine()->getEntityManager();
251: $query = $em->createQuery(
252: "SELECT s FROM MapbenderCoreBundle:Source s ORDER BY s.id ASC");
253: $sources = $query->getResult();
254: return new Response($this->container->get('templating')
255: ->render('MapbenderManagerBundle:Application:edit.html.twig',
256: array(
257: 'application' => $application,
258: 'regions' => $templateClass::getRegions(),
259: 'slug' => $slug,
260: 'available_elements' => $this->getElementList(),
261: 'sources' => $sources,
262: 'form' => $form->createView(),
263: 'form_name' => $form->getName())));
264: }
265:
266: 267: 268: 269: 270: 271:
272: public function toggleStateAction($slug)
273: {
274: $application = $this->get('mapbender')->getApplicationEntity($slug);
275:
276:
277: $this->checkGranted('EDIT', $application);
278:
279: $em = $this->getDoctrine()->getEntityManager();
280:
281: $requestedState = $this->get('request')->get('state');
282: $currentState = $application->isPublished();
283: $newState = $currentState;
284:
285: switch($requestedState)
286: {
287: case 'enabled':
288: case 'disabled':
289: $newState = $requestedState === 'enabled' ? true : false;
290: $application->setPublished($newState);
291: $em->flush();
292: $message = 'State switched';
293: break;
294: case null:
295: $message = 'No state given';
296: break;
297: default:
298: $message = 'Unknown state requested';
299: break;
300: }
301:
302: return new Response(json_encode(array(
303: 'oldState' => $currentState ? 'enabled' : 'disabled',
304: 'newState' => $newState ? 'enabled' : 'disabled',
305: 'message' => $message)), 200, array(
306: 'Content-Type' => 'application/json'
307: ));
308: }
309:
310: 311: 312: 313: 314: 315:
316: public function confirmDeleteAction($slug)
317: {
318: $application = $this->get('mapbender')->getApplicationEntity($slug);
319: if($application === null)
320: {
321: $this->get('session')->setFlash('error',
322: 'Your application has been already deleted.');
323: return $this->redirect(
324: $this->generateUrl('mapbender_manager_application_index'));
325: }
326:
327:
328: $this->checkGranted('EDIT', $application);
329:
330: $id = $application->getId();
331: return array(
332: 'application' => $application,
333: 'form' => $this->createDeleteForm($id)->createView());
334: }
335:
336: 337: 338: 339: 340: 341:
342: public function deleteAction($slug)
343: {
344: $application = $this->get('mapbender')->getApplicationEntity($slug);
345:
346:
347: $this->checkGranted('DELETE', $application);
348:
349: try {
350: $em = $this->getDoctrine()->getEntityManager();
351: $aclProvider = $this->get('security.acl.provider');
352: $em->getConnection()->beginTransaction();
353: $oid = ObjectIdentity::fromDomainObject($application);
354: $aclProvider->deleteAcl($oid);
355: $em->remove($application);
356: $em->flush();
357: $em->commit();
358: $this->get('session')->setFlash('success','Your application has been deleted.');
359: } catch(Exception $e) {
360: $this->get('session')->setFlash('error','Your application couldn\'t be deleted.');
361: }
362:
363: return new Response();
364: }
365:
366:
367:
368: 369: 370: 371: 372: 373: 374:
375: public function newLayersetAction($slug)
376: {
377: $application = $this->get('mapbender')->getApplicationEntity($slug);
378:
379: $this->checkGranted('EDIT', $application);
380: $layerset = new Layerset();
381: $layerset->setApplication($application);
382:
383: $form = $this->createForm(new LayersetType(), $layerset);
384:
385: return array(
386: "isnew" => true,
387: "application" => $application,
388: 'form' => $form->createView());
389: }
390:
391: 392: 393: 394: 395: 396: 397:
398: public function editLayersetAction($slug, $layersetId)
399: {
400: $application = $this->get('mapbender')->getApplicationEntity($slug);
401:
402: $this->checkGranted('EDIT', $application);
403: $layerset = $this->getDoctrine()
404: ->getRepository("MapbenderCoreBundle:Layerset")
405: ->find($layersetId);
406:
407: $form = $this->createForm(new LayersetType(), $layerset);
408:
409: return array(
410: "isnew" => false,
411: "application" => $application,
412: 'form' => $form->createView());
413: }
414:
415: 416: 417: 418: 419: 420: 421: 422:
423: public function saveLayersetAction($slug, $layersetId = null)
424: {
425: $application = $this->get('mapbender')->getApplicationEntity($slug);
426: $this->checkGranted('EDIT', $application);
427: if($layersetId === null)
428: {
429: $layerset = new Layerset();
430: $form = $this->createForm(new LayersetType(), $layerset);
431: $layerset->setApplication($application);
432: } else
433: {
434: $layerset = $this->getDoctrine()
435: ->getRepository("MapbenderCoreBundle:Layerset")
436: ->find($layersetId);
437: $form = $this->createForm(new LayersetType(), $layerset);
438: }
439: $form->bindRequest($this->get('request'));
440: if($form->isValid())
441: {
442: $this->getDoctrine()->getEntityManager()->persist($layerset);
443: $this->getDoctrine()->getEntityManager()->flush();
444: $this->get("logger")->debug("Layerset saved");
445: $this->get('session')->setFlash('success', "Your layerset has been saved");
446: return $this->redirect($this->generateUrl(
447: 'mapbender_manager_application_edit',
448: array('slug' => $slug)));
449: }
450: $this->get('session')->setFlash('error',
451: 'Your form has errors, please review them below.');
452: return array(
453: "isnew" => $layerset->getId() === null ? true : false,
454: "application" => $application,
455: 'form' => $form->createView());
456: }
457:
458: 459: 460: 461: 462: 463: 464:
465: public function confirmDeleteLayersetAction($slug, $layersetId)
466: {
467: $application = $this->get('mapbender')->getApplicationEntity($slug);
468: $this->checkGranted('EDIT', $application);
469: $layerset = $this->getDoctrine()
470: ->getRepository("MapbenderCoreBundle:Layerset")
471: ->find($layersetId);
472: return array(
473: 'application' => $application,
474: 'layerset' => $layerset,
475: 'form' => $this->createDeleteForm($layerset->getId())->createView()
476: );
477: }
478:
479: 480: 481: 482: 483: 484:
485: public function deleteLayersetAction($slug, $layersetId)
486: {
487: $application = $this->get('mapbender')->getApplicationEntity($slug);
488: $this->checkGranted('EDIT', $application);
489: $layerset = $this->getDoctrine()
490: ->getRepository("MapbenderCoreBundle:Layerset")
491: ->find($layersetId);
492: if($layerset !== null)
493: {
494: $em = $this->getDoctrine()->getEntityManager();
495:
496: $em->getConnection()->beginTransaction();
497:
498: $em->remove($layerset);
499: $em->flush();
500: $em->getConnection()->commit();
501:
502: $this->get("logger")->debug('The layerset "'
503: . $layerset->getId() . '"has been deleted.');
504: $this->get('session')->setFlash('success',
505: 'Your layerset has been deleted.');
506: return $this->redirect($this->generateUrl(
507: 'mapbender_manager_application_edit',
508: array('slug' => $slug)) . "#layersets");
509: }
510: $this->get('session')->setFlash('error',
511: 'Your layerset con not be delete.');
512: return $this->redirect($this->generateUrl(
513: 'mapbender_manager_application_edit',
514: array('slug' => $slug)) . "#layersets");
515: }
516:
517:
518:
519:
520:
521: 522: 523: 524: 525: 526:
527: public function listInstanceAction($slug, $layersetId, Request $request)
528: {
529: $application = $this->get('mapbender')->getApplicationEntity($slug);
530:
531: $this->checkGranted('EDIT', $application);
532:
533: $layerset = $this->getDoctrine()
534: ->getRepository("MapbenderCoreBundle:Layerset")
535: ->find($layersetId);
536:
537: $securityContext = $this->get('security.context');
538: $em = $this->getDoctrine()->getEntityManager();
539: $query = $em->createQuery(
540: "SELECT s FROM MapbenderCoreBundle:Source s ORDER BY s.id ASC");
541: $sources = $query->getResult();
542:
543: $allowed_sources = array();
544: foreach($sources as $source) {
545: if($securityContext->isGranted('EDIT', $source)) {
546: $allowed_sources[] = $source;
547: }
548: }
549:
550: return array(
551: 'application' => $application,
552: 'layerset' => $layerset,
553: 'sources' => $allowed_sources);
554: }
555:
556: 557: 558: 559: 560:
561: public function addInstanceAction($slug, $layersetId, $sourceId,
562: Request $request)
563: {
564: $application = $this->get('mapbender')->getApplicationEntity($slug);
565:
566: $this->checkGranted('EDIT', $application);
567:
568: $layerset = $this->getDoctrine()
569: ->getRepository("MapbenderCoreBundle:Layerset")
570: ->find($layersetId);
571:
572: $source = $this->getDoctrine()
573: ->getRepository("MapbenderCoreBundle:Source")
574: ->find($sourceId);
575:
576: $sourceInstance = $source->createInstance();
577: $sourceInstance->setLayerset($layerset);
578: $sourceInstance->setWeight(-1);
579:
580: $layerset->addInstance($sourceInstance);
581: $em = $this->getDoctrine()->getEntityManager();
582: $em->persist($sourceInstance);
583: $em->persist($application);
584: $em->persist($layerset);
585: $em->flush();
586:
587: $num = 0;
588: foreach($layerset->getInstances() as $instance)
589: {
590: $instance->setWeight($num);
591: $instance->generateConfiguration();
592: $em->persist($instance);
593: $em->flush();
594: $num++;
595: }
596:
597: $this->get("logger")->debug('A new instance "'
598: . $sourceInstance->getId() . '"has been created. Please edit it!');
599: $this->get('session')->setFlash('success',
600: 'A new instance has been created. Please edit it!');
601: return $this->redirect(
602: $this->generateUrl(
603: "mapbender_manager_repository_instance",
604: array("slug" => $slug, "instanceId" => $sourceInstance->getId()))
605: );
606: }
607:
608: 609: 610: 611: 612: 613:
614: public function deleteInstanceAction($slug, $layersetId, $instanceId)
615: {
616: $application = $this->get('mapbender')->getApplicationEntity($slug);
617:
618: $this->checkGranted('EDIT', $application);
619: $sourceInst = $this->getDoctrine()
620: ->getRepository("MapbenderCoreBundle:SourceInstance")
621: ->find($instanceId);
622:
623: $managers = $this->get('mapbender')->getRepositoryManagers();
624: $manager = $managers[$sourceInst->getSource()->getManagertype()];
625:
626: return $this->forward(
627: $manager['bundle'] . ":" . "Repository:deleteInstance",
628: array("slug" => $slug, "instanceId" => $instanceId));
629: }
630:
631:
632:
633: 634: 635:
636: private function createApplicationForm($application)
637: {
638: $available_templates = array();
639: foreach($this->get('mapbender')->getTemplates() as $templateClassName)
640: {
641: $available_templates[$templateClassName] =
642: $templateClassName::getTitle();
643: }
644: asort($available_templates);
645:
646: return $this->createForm(new ApplicationType(), $application,
647: array(
648: 'available_templates' => $available_templates));
649: }
650:
651: 652: 653:
654: private function getElementList()
655: {
656: $available_elements = array();
657: foreach($this->get('mapbender')->getElements() as $elementClassName)
658: {
659: $available_elements[$elementClassName] = array(
660: 'title' => $elementClassName::getClassTitle(),
661: 'description' => $elementClassName::getClassDescription(),
662: 'tags' => $elementClassName::getClassTags());
663: }
664: asort($available_elements);
665:
666: return $available_elements;
667: }
668:
669: 670: 671:
672: private function createDeleteForm($id)
673: {
674: return $this->createFormBuilder(array('id' => $id))
675: ->add('id', 'hidden')
676: ->getForm();
677: }
678:
679: 680: 681: 682: 683: 684: 685:
686: private function checkGranted($action, $object)
687: {
688: $securityContext = $this->get('security.context');
689: if($action === "CREATE")
690: {
691: $oid = new ObjectIdentity('class', get_class($object));
692: if(false === $securityContext->isGranted($action, $oid))
693: {
694: throw new AccessDeniedException();
695: }
696: } else if($action === "VIEW" && !$securityContext->isGranted($action,
697: $object))
698: {
699: throw new AccessDeniedException();
700: } else if($action === "EDIT" && !$securityContext->isGranted($action,
701: $object))
702: {
703: throw new AccessDeniedException();
704: } else if($action === "DELETE" && !$securityContext->isGranted($action,
705: $object))
706: {
707: throw new AccessDeniedException();
708: }
709: }
710:
711: }
712:
713: