Overview

Namespaces

  • Mapbender
    • Component
      • HTTP
    • CoreBundle
      • Command
      • Component
        • Exception
      • Controller
      • DataFixtures
        • ORM
      • DependencyInjection
      • Element
        • Type
      • Entity
      • EventListener
      • Extension
      • Form
        • DataTransformer
        • EventListener
        • Type
      • Security
      • Template
    • DrupalIntegrationBundle
      • DependencyInjection
      • Security
        • Authentication
          • Provider
          • Token
        • Authorization
          • Voter
        • Factory
        • Firewall
        • User
      • Session
    • KmlBundle
      • Element
    • ManagerBundle
      • Controller
      • Form
        • DataTransformer
        • Type
    • MonitoringBundle
      • Command
      • Component
      • Controller
      • DependencyInjection
      • Entity
      • EventListener
      • Form
    • PrintBundle
      • Component
      • Controller
    • WmcBundle
      • Component
        • Exception
      • Element
        • Type
      • Entity
      • Form
        • EventListener
        • Type
    • WmsBundle
      • Component
        • Exception
      • Controller
      • DependencyInjection
      • Element
        • Type
      • Entity
      • Event
      • Form
        • EventListener
        • Type
    • WmtsBundle
      • Component
        • Exception
      • Controller
      • Entity
      • Form
        • Type
  • None
  • PHP

Classes

  • ApplicationController
  • ElementController
  • RepositoryController
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Todo
  • Download
  1: <?php
  2: 
  3: /**
  4:  * Mapbender application management
  5:  *
  6:  * @author Christian Wygoda <christian.wygoda@wheregroup.com>
  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\HttpKernel\Exception\NotFoundHttpException;
 17: use Mapbender\CoreBundle\Component\Element As ComponentElement;
 18: use Mapbender\CoreBundle\Entity\Element;
 19: use Mapbender\CoreBundle\Form\Type\BaseElementType;
 20: use Doctrine\Common\Collections\ArrayCollection;
 21: use Mapbender\ManagerBundle\Form\Type\YAMLConfigurationType;
 22: use Mapbender\CoreBundle\Validator\Constraints\ContainsElementTarget;
 23: use Mapbender\CoreBundle\Validator\Constraints\ContainsElementTargetValidator;
 24: 
 25: class ElementController extends Controller
 26: {
 27: 
 28:     /**
 29:      * Show element class selection
 30:      *
 31:      * @ManagerRoute("/application/{slug}/element/select")
 32:      * @Method({"GET","POST"})
 33:      * @Template
 34:      */
 35:     public function selectAction($slug)
 36:     {
 37:         $elements = array();
 38:         foreach($this->get('mapbender')->getElements() as $elementClassName)
 39:         {
 40:             $elements[$elementClassName] = array(
 41:                 'title' => $elementClassName::getClassTitle(),
 42:                 'description' => $elementClassName::getClassDescription(),
 43:                 'tags' => $elementClassName::getClassTags());
 44:         }
 45: 
 46:         return array(
 47:             'elements' => $elements,
 48:             'region' => $this->get('request')->get('region'));
 49:     }
 50: 
 51:     /**
 52:      * Shows form for creating new element
 53:      *
 54:      * @ManagerRoute("/application/{slug}/element/new")
 55:      * @Method("GET")
 56:      * @Template("MapbenderManagerBundle:Element:edit.html.twig")
 57:      */
 58:     public function newAction($slug)
 59:     {
 60:         $application = $this->get('mapbender')->getApplicationEntity($slug);
 61: 
 62:         // Get class for element
 63:         $class = $this->getRequest()->get('class');
 64: 
 65:         if(!class_exists($class))
 66:         {
 67:             throw new \RuntimeException('An Element class "' . $class
 68:                     . '" does not exist.');
 69:         }
 70: 
 71:         // Get first region (by default)
 72:         $template = $application->getTemplate();
 73:         $regions = $template::getRegions();
 74:         $region = $this->get('request')->get('region');
 75: 
 76:         $element = $this->getDefaultElement($class, $region);
 77:         $form = $this->getElementForm($application, $element);
 78: 
 79:         return array(
 80:             'form' => $form['form']->createView(),
 81:             'theme' => $form['theme'],
 82:             'assets' => $form['assets']);
 83:     }
 84: 
 85:     /**
 86:      * Create a new element from POSTed data
 87:      *
 88:      * @ManagerRoute("/application/{slug}/element/new")
 89:      * @Method("POST")
 90:      * @Template("MapbenderManagerBundle:Element:edit.html.twig")
 91:      */
 92:     public function createAction($slug)
 93:     {
 94:         $application = $this->get('mapbender')->getApplicationEntity($slug);
 95: 
 96:         $data = $this->get('request')->get('form');
 97:         $element = $this->getDefaultElement($data['class'], $data['region']);
 98:         $element->setApplication($application);
 99:         $form = $this->getElementForm($application, $element);
100:         $form['form']->bindRequest($this->get('request'));
101: 
102:         if($form['form']->isValid())
103:         {
104:             $em = $this->getDoctrine()->getEntityManager();
105:             $query = $em->createQuery(
106:                     "SELECT e FROM MapbenderCoreBundle:Element e"
107:                     . " WHERE e.region=:reg AND e.application=:app");
108:             $query->setParameters(array(
109:                 "reg" => $element->getRegion(),
110:                 "app" => $element->getApplication()->getId()));
111:             $elements = $query->getResult();
112:             $element->setWeight(count($elements) + 1);
113:             $application = $element->getApplication();
114:             $application->setUpdated(new \DateTime());
115:             $em->persist($element);
116:             $em->flush();
117:             $entity_class = $element->getClass();
118:             $appl = new \Mapbender\CoreBundle\Component\Application($this->container, $application, array());
119:             $elComp = new $entity_class($appl, $this->container, $element);
120:             $elComp->postSave();
121:             $this->get('session')->setFlash('success',
122:                                             'Your element has been saved.');
123: 
124:             return new Response('', 201);
125:         } else
126:         {
127:             return array(
128:                 'form' => $form['form']->createView(),
129:                 'theme' => $form['theme'],
130:                 'assets' => $form['assets']);
131:         }
132:     }
133: 
134:     /**
135:      * @ManagerRoute("/application/{slug}/element/{id}", requirements={"id" = "\d+"})
136:      * @Method("GET")
137:      * @Template
138:      */
139:     public function editAction($slug, $id)
140:     {
141:         $application = $this->get('mapbender')->getApplicationEntity($slug);
142: 
143:         $element = $this->getDoctrine()
144:                 ->getRepository('MapbenderCoreBundle:Element')
145:                 ->findOneById($id);
146: 
147:         if(!$element)
148:         {
149:             throw $this->createNotFoundException('The element with the id "'
150:                     . $id . '" does not exist.');
151:         }
152: 
153:         $form = $this->getElementForm($application, $element);
154: 
155:         return array(
156:             'form' => $form['form']->createView(),
157:             'theme' => $form['theme'],
158:             'assets' => $form['assets']);
159:     }
160: 
161:     /**
162:      * Updates element by POSTed data
163:      *
164:      * @ManagerRoute("/application/{slug}/element/{id}", requirements = {"id" = "\d+" })
165:      * @Method("POST")
166:      * @Template("MapbenderManagerBundle:Element:edit.html.twig")
167:      */
168:     public function updateAction($slug, $id)
169:     {
170:         $application = $this->get('mapbender')->getApplicationEntity($slug);
171: 
172:         $element = $this->getDoctrine()
173:                 ->getRepository('MapbenderCoreBundle:Element')
174:                 ->findOneById($id);
175: 
176:         if(!$element)
177:         {
178:             throw $this->createNotFoundException('The element with the id "'
179:                     . $id . '" does not exist.');
180:         }
181: 
182:         $form = $this->getElementForm($application, $element);
183:         $form['form']->bindRequest($this->get('request'));
184: 
185:         if($form['form']->isValid())
186:         {
187:             $em = $this->getDoctrine()->getEntityManager();
188:             $application = $element->getApplication();
189:             $application->setUpdated(new \DateTime());
190:             $em->persist($element);
191:             $em->flush();
192: 
193:             $entity_class = $element->getClass();
194:             $appl = new \Mapbender\CoreBundle\Component\Application($this->container, $application, array());
195:             $elComp = new $entity_class($appl, $this->container, $element);
196:             $elComp->postSave();
197:             $this->get('session')->setFlash('success',
198:                                             'Your element has been saved.');
199: 
200:             return new Response('', 205);
201:         } else
202:         {
203:             return array(
204:                 'form' => $form['type']->getForm()->createView(),
205:                 'theme' => $form['theme'],
206:                 'assets' => $form['assets']);
207:         }
208:     }
209: 
210:     /**
211:      * Shows delete confirmation page
212:      *
213:      * @ManagerRoute("application/{slug}/element/{id}/delete", requirements = {
214:      *     "id" = "\d+" })
215:      * @Method("GET")
216:      * @Template("MapbenderManagerBundle:Element:delete.html.twig")
217:      */
218:     public function confirmDeleteAction($slug, $id)
219:     {
220:         $application = $this->get('mapbender')->getApplicationEntity($slug);
221: 
222:         $element = $this->getDoctrine()
223:                 ->getRepository('MapbenderCoreBundle:Element')
224:                 ->findOneById($id);
225: 
226:         if(!$element)
227:         {
228:             throw $this->createNotFoundException('The element with the id "'
229:                     . $id . '" does not exist.');
230:         }
231: 
232:         return array(
233:             'element' => $element,
234:             'form' => $this->createDeleteForm($id)->createView());
235:     }
236: 
237:     /**
238:      * Delete element
239:      *
240:      * @ManagerRoute("application/{slug}/element/{id}/delete")
241:      * @Method("POST")
242:      */
243:     public function deleteAction($slug, $id)
244:     {
245:         $application = $this->get('mapbender')->getApplicationEntity($slug);
246: 
247:         $element = $this->getDoctrine()
248:                 ->getRepository('MapbenderCoreBundle:Element')
249:                 ->findOneById($id);
250: 
251:         if(!$element)
252:         {
253:             throw $this->createNotFoundException('The element with the id "'
254:                     . $id . '" does not exist.');
255:         }
256: 
257:         $em = $this->getDoctrine()->getEntityManager();
258:         $query = $em->createQuery(
259:                 "SELECT e FROM MapbenderCoreBundle:Element e"
260:                 . " WHERE e.region=:reg AND e.application=:app"
261:                 . " AND e.weight>=:min ORDER BY e.weight ASC");
262:         $query->setParameters(array(
263:             "reg" => $element->getRegion(),
264:             "app" => $element->getApplication()->getId(),
265:             "min" => $element->getWeight()));
266:         $elements = $query->getResult();
267:         foreach($elements as $elm)
268:         {
269:             if($elm->getId() !== $element->getId())
270:             {
271:                 $elm->setWeight($elm->getWeight() - 1);
272:             }
273:         }
274:         foreach($elements as $elm)
275:         {
276:             $em->persist($elm);
277:         }
278:         $em->remove($element);
279:         $em->flush();
280: 
281:         $this->get('session')->setFlash('success',
282:                                         'Your element has been removed.');
283: 
284:         return new Response();
285:     }
286: 
287:     /**
288:      * Delete element
289:      *
290:      * @ManagerRoute("application/element/{id}/weight")
291:      * @Method("POST")
292:      */
293:     public function weightAction($id)
294:     {
295:         $element = $this->getDoctrine()
296:                 ->getRepository('MapbenderCoreBundle:Element')
297:                 ->findOneById($id);
298: 
299:         if(!$element)
300:         {
301:             throw $this->createNotFoundException('The element with the id "'
302:                     . $id . '" does not exist.');
303:         }
304:         $number = $this->get("request")->get("number");
305:         $newregion = $this->get("request")->get("region");
306:         if(intval($number) === $element->getWeight()
307:                 && $element->getRegion() === $newregion)
308:         {
309:             return new Response(json_encode(array(
310:                                 'error' => '',
311:                                 'result' => 'ok')),
312:                             200,
313:                             array('Content-Type' => 'application/json'));
314:         }
315:         if($element->getRegion() === $newregion)
316:         {
317:             $em = $this->getDoctrine()->getEntityManager();
318:             $element->setWeight($number);
319:             $em->persist($element);
320:             $em->flush();
321:             $query = $em->createQuery(
322:                     "SELECT e FROM MapbenderCoreBundle:Element e"
323:                     . " WHERE e.region=:reg AND e.application=:app"
324:                     . " ORDER BY e.weight ASC");
325:             $query->setParameters(array(
326:                 "reg" => $newregion,
327:                 "app" => $element->getApplication()->getId()));
328:             $elements = $query->getResult();
329: 
330:             $num = 0;
331:             foreach($elements as $elm)
332:             {
333:                 if($num === intval($element->getWeight()))
334:                 {
335:                     if($element->getId() === $elm->getId())
336:                     {
337:                         $num++;
338:                     } else
339:                     {
340:                         $num++;
341:                         $elm->setWeight($num);
342:                         $num++;
343:                     }
344:                 } else
345:                 {
346:                     if($element->getId() !== $elm->getId())
347:                     {
348:                         $elm->setWeight($num);
349:                         $num++;
350:                     }
351:                 }
352:             }
353:             foreach($elements as $elm)
354:             {
355:                 $em->persist($elm);
356:             }
357:             $application = $element->getApplication();
358:             $application->setUpdated(new \DateTime());
359:             $em->persist($application);
360:             $em->flush();
361:         } else
362:         {
363:             // handle old region
364:             $em = $this->getDoctrine()->getEntityManager();
365:             $query = $em->createQuery(
366:                     "SELECT e FROM MapbenderCoreBundle:Element e"
367:                     . " WHERE e.region=:reg AND e.application=:app"
368:                     . " AND e.weight>=:min ORDER BY e.weight ASC");
369:             $query->setParameters(array(
370:                 "reg" => $element->getRegion(),
371:                 "app" => $element->getApplication()->getId(),
372:                 "min" => $element->getWeight()));
373:             $elements = $query->getResult();
374:             foreach($elements as $elm)
375:             {
376:                 if($elm->getId() !== $element->getId())
377:                 {
378:                     $elm->setWeight($elm->getWeight() - 1);
379:                 }
380:             }
381:             foreach($elements as $elm)
382:             {
383:                 $em->persist($elm);
384:             }
385:             $em->flush();
386:             // handle new region
387:             $query = $em->createQuery(
388:                     "SELECT e FROM MapbenderCoreBundle:Element e"
389:                     . " WHERE e.region=:reg AND e.application=:app"
390:                     . " AND e.weight>=:min ORDER BY e.weight ASC");
391:             $query->setParameters(array(
392:                 "reg" => $newregion,
393:                 "app" => $element->getApplication()->getId(),
394:                 "min" => $number));
395:             $elements = $query->getResult();
396:             foreach($elements as $elm)
397:             {
398:                 if($elm->getId() !== $element->getId())
399:                 {
400:                     $elm->setWeight($elm->getWeight() + 1);
401:                 }
402:             }
403:             foreach($elements as $elm)
404:             {
405:                 $em->persist($elm);
406:             }
407:             $em->flush();
408:             $element->setWeight($number);
409:             $element->setRegion($newregion);
410:             $em->persist($element);
411:             $application = $element->getApplication();
412:             $application->setUpdated(new \DateTime());
413:             $em->persist($application);
414:             $em->flush();
415:         }
416:         return new Response(json_encode(array(
417:                             'error' => '',
418:                             'result' => 'ok')), 200, array(
419:                     'Content-Type' => 'application/json'));
420:     }
421: 
422:     /**
423:      * Delete element
424:      *
425:      * @ManagerRoute("application/element/{id}/enable")
426:      * @Method("POST")
427:      */
428:     public function enableAction($id)
429:     {
430:         $element = $this->getDoctrine()
431:                 ->getRepository('MapbenderCoreBundle:Element')
432:                 ->findOneById($id);
433: 
434:         $enabled = $this->get("request")->get("enabled");
435:         if(!$element)
436:         {
437:             return new Response(
438:                             json_encode(array(
439:                                 'error' => 'An element with the id "' . $id . '" does not exist.',
440:                                 'result' => 'ok')),
441:                             200,
442:                             array('Content-Type' => 'application/json'));
443:         } else
444:         {
445:             $enabled = $enabled === "true"  ? true : false;
446:             $element->setEnabled($enabled);
447:             $em = $this->getDoctrine()->getEntityManager();
448:             $em->persist($element);
449:             $em->flush();
450:             return new Response(
451:                             json_encode(array(
452:                                 'error' => '',
453:                                 'result' => 'ok')),
454:                             200,
455:                             array('Content-Type' => 'application/json'));
456:         }
457:     }
458: 
459:     /**
460:      * Creates the form for the delete confirmation page
461:      */
462:     private function createDeleteForm($id)
463:     {
464:         return $this->createFormBuilder(array('id' => $id))
465:                         ->add('id', 'hidden')
466:                         ->getForm();
467:     }
468: 
469:     /**
470:      * Create form for given element
471:      *
472:      * @param string $class
473:      * @return dsd
474:      */
475:     private function getElementForm($application, $element)
476:     {
477:         $class = $element->getClass();
478: 
479:         // Create base form shared by all elements
480:         $formType = $this->createFormBuilder($element)
481:                 ->add('title', 'text')
482:                 ->add('class', 'hidden')
483:                 ->add('region', 'hidden');
484:         // Get configuration form, either basic YAML one or special form
485:         $configurationFormType = $class::getType();
486:         if($configurationFormType === null)
487:         {
488:             $formType->add('configuration', new YAMLConfigurationType(),
489:                            array(
490:                 'required' => false,
491:                 'attr' => array(
492:                     'class' => 'code-yaml')));
493:             $formTheme = 'MapbenderManagerBundle:Element:yaml-form.html.twig';
494:             $formAssets = array(
495:                 'js' => array(
496:                     'bundles/mapbendermanager/codemirror2/lib/codemirror.js',
497:                     'bundles/mapbendermanager/codemirror2/mode/yaml/yaml.js',
498:                     'bundles/mapbendermanager/js/form-yaml.js'),
499:                 'css' => array(
500:                     'bundles/mapbendermanager/codemirror2/lib/codemirror.css'));
501:         } else
502:         {
503:             $type = $class::getType();
504: 
505:             $formType->add('configuration', new $type(),
506:                            array(
507:                 'application' => $application
508:             ));
509:             $formTheme = $class::getFormTemplate();
510:             $formAssets = $class::getFormAssets();
511:         }
512: 
513:         return array(
514:             'form' => $formType->getForm(),
515:             'theme' => $formTheme,
516:             'assets' => $formAssets);
517:     }
518: 
519:     /**
520:      * Create default element
521:      *
522:      * @param string $class
523:      * @param string $region
524:      * @return Element
525:      */
526:     public function getDefaultElement($class, $region)
527:     {
528:         $element = new Element();
529:         $configuration = $class::getDefaultConfiguration();
530:         $element
531:                 ->setClass($class)
532:                 ->setRegion($region)
533:                 ->setWeight(0)
534:                 ->setTitle($class::getClassTitle())
535:                 ->setConfiguration($configuration);
536: 
537:         return $element;
538:     }
539: 
540: }
541: 
542: 
Mapbender3 API documenation API documentation generated by ApiGen 2.8.0