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

  • WmcHandler
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Todo
  • Download
  1: <?php
  2: 
  3: namespace Mapbender\WmcBundle\Element;
  4: 
  5: use Mapbender\CoreBundle\Component\Element;
  6: use Mapbender\CoreBundle\Component\StateHandler;
  7: use Mapbender\CoreBundle\Entity\State;
  8: use Mapbender\CoreBundle\Form\Type\StateType;
  9: use Mapbender\WmsBundle\Component\LegendUrl;
 10: use Mapbender\WmsBundle\Component\OnlineResource;
 11: use Mapbender\WmcBundle\Component\WmcParser;
 12: use Mapbender\WmcBundle\Entity\Wmc;
 13: use Mapbender\WmcBundle\Form\Type\WmcLoadType;
 14: use Mapbender\WmcBundle\Form\Type\WmcType;
 15: use Symfony\Component\HttpFoundation\Response;
 16: 
 17: class WmcHandler extends Element
 18: {
 19: 
 20:     /**
 21:      * @inheritdoc
 22:      */
 23:     static public function getClassTitle()
 24:     {
 25:         return "WmcHandler";
 26:     }
 27: 
 28:     /**
 29:      * @inheritdoc
 30:      */
 31:     static public function getClassDescription()
 32:     {
 33:         return "";
 34:     }
 35: 
 36:     /**
 37:      * @inheritdoc
 38:      */
 39:     static public function getClassTags()
 40:     {
 41:         return array("wmc", "handler");
 42:     }
 43: 
 44:     /**
 45:      * @inheritdoc
 46:      */
 47:     public static function getDefaultConfiguration()
 48:     {
 49:         return array(
 50:             "tooltip"         => null,
 51:             "target"          => null,
 52:             "accessRoles"           => array(),
 53:             "keepBaseSources" => false,
 54:             "useEditor"       => false,
 55:             "useSuggestMap"   => false,
 56:             'receiver'        => array("email"),
 57:             "useLoader"       => false,
 58:         );
 59:     }
 60: 
 61:     /**
 62:      * @inheritdoc
 63:      */
 64:     public static function getType()
 65:     {
 66:         return 'Mapbender\WmcBundle\Element\Type\WmcHandlerAdminType';
 67:     }
 68: 
 69:     /**
 70:      * @inheritdoc
 71:      */
 72:     public static function getFormTemplate()
 73:     {
 74:         return 'MapbenderWmcBundle:ElementAdmin:wmchandler.html.twig';
 75:     }
 76: 
 77:     /**
 78:      * @inheritdoc
 79:      */
 80:     public function getWidgetName()
 81:     {
 82:         return 'mapbender.mbWmcHandler';
 83:     }
 84: 
 85:     /**
 86:      * @inheritdoc
 87:      */
 88:     public function getAssets()
 89:     {
 90:         $configuration = $this->getConfiguration();
 91:         $js            = array('jquery.form.js', 'mapbender.element.wmchandler.js');
 92: //        if($configuration["useSuggestMap"]) {
 93: //            $js[] = 'mapbender.element.wmchandler_suggestmap.js';
 94: //        }
 95: //        if($configuration["useEditor"]) {
 96: //            $js[] = 'mapbender.element.wmchandler_editor.js';
 97: //        }
 98: //        if($configuration["useLoader"]) {
 99: //            $js[] = 'mapbender.element.wmchandler_loader.js';
100: //        }
101:         return array(
102:             'js'  => $js,
103:             'css' => array()
104:         );
105:     }
106: 
107:     /**
108:      * @inheritdoc
109:      */
110:     public function getConfiguration()
111:     {
112:         $configuration         = parent::getConfiguration();
113:         $toload                = array();
114:         $wmcid                 = $this->container->get('request')->get('wmc');
115:         if ($wmcid) $toload["wmc"]         = $wmcid;
116:         $stateid               = $this->container->get('request')->get('state');
117:         if ($stateid) $toload["state"]       = $stateid;
118:         if (count($toload) > 0) $configuration["load"] = $toload;
119:         return $configuration;
120:     }
121: 
122:     /**
123:      * @inheritdoc
124:      */
125:     public function render()
126:     {
127:         $wmc  = new Wmc();
128:         $form = $this->container->get("form.factory")->create(new WmcLoadType(),
129:                 $wmc);
130:         $html = $this->container->get('templating')
131:                 ->render('MapbenderWmcBundle:Element:wmchandler.html.twig',
132:                 array(
133:             'id'            => $this->getId(),
134:             'configuration' => $this->getConfiguration(),
135:             'title'         => $this->getTitle(),
136:             'form'          => $form->createView()));
137:         return $html;
138:     }
139: 
140:     public function httpAction($action)
141:     {
142:         $session = $this->container->get("session");
143: 
144:         if ($session->get("proxyAllowed", false) !== true)
145:         {
146:             throw new AccessDeniedHttpException('You are not allowed to use this proxy without a session.');
147:         }
148:         switch ($action)
149:         {
150:             case 'get':
151:                 return $this->getWmc();
152:                 break;
153:             case 'list':
154:                 return $this->getWmcList();
155:                 break;
156:             case 'remove':
157:                 return $this->removeWmc();
158:                 break;
159:             case 'save':
160:                 return $this->saveWmc();
161:                 break;
162:             case 'load':
163:                 $type = $this->container->get('request')->get("type", null);
164:                 $id   = $this->container->get('request')->get("_id", null);
165:                 if ($type === "wmc") return $this->loadWmc($id);
166:                 else if ($type === "state") return $this->loadState($id);
167:                 break;
168:             case 'state':
169:                 return $this->saveState();
170:                 break;
171:             case 'loadxml':
172:                 return $this->loadXml();
173:                 break;
174:             case 'wmcasxml':
175:                 return $this->getWmcAsXml();
176:                 break;
177: //            case 'wmcasjson':
178: //                return $this->getWmcAsJson();
179: //                break;
180:             default:
181:                 throw new NotFoundHttpException('No such action');
182:         }
183:     }
184: 
185:     private function getWmcAsXml()
186:     {
187:         $request = $this->container->get('request');
188:         $wmc     = Wmc::create();
189:         $form    = $this->container->get("form.factory")->create(new WmcLoadType(),
190:                 $wmc);
191:         $form->bindRequest($request);
192:         if ($form->isValid())
193:         { //TODO: Is file an image (jpg/png/gif?)
194:             $state = $wmc->getState();
195:             if ($state !== null && $state->getJson() !== null)
196:             {
197:                 $state->setServerurl($this->getBaseUrl());
198:                 $state->setSlug($this->application->getSlug());
199:                 $state->setTitle("Mapbender State");
200:                 $wmc->setWmcid(round((microtime(true) * 1000)));
201:                 $xml      = $this->container->get('templating')
202:                         ->render('MapbenderWmcBundle:Wmc:wmc110_simple.xml.twig',
203:                         array(
204:                     'wmc' => $wmc));
205:                 $response = new Response();
206:                 $response->setContent($xml);
207:                 $response->headers->set('Content-Type', 'application/xml');
208:                 $response->headers->set('Content-Disposition',
209:                         'attachment; filename=wmc.xml');
210:                 return $response;
211:             }
212:         }
213:         return new Response(json_encode(array(
214:                     "error" => 'WMC:  can not be loaded.')), 200,
215:                 array('Content-Type' => 'application/json'));
216:     }
217: 
218:     protected function loadXml()
219:     {
220:         $request = $this->container->get('request');
221:         $wmc     = Wmc::create();
222:         $form    = $this->container->get("form.factory")->create(new WmcLoadType(),
223:                 $wmc);
224:         $form->bindRequest($request);
225:         if ($form->isValid())
226:         {
227:             if ($wmc->getXml() !== null)
228:             {
229:                 $file   = $wmc->getXml();
230:                 $path   = $file->getPathname();
231:                 $doc    = WmcParser::loadDocument($path);
232:                 $parser = WmcParser::getParser($doc);
233:                 $wmc    = $parser->parse();
234:                 if (file_exists($file->getPathname()))
235:                         unlink($file->getPathname());
236:                 return new Response(json_encode(array("data" => array(round((microtime(true)
237:                                         * 1000)) => $wmc->getState()->getJson()))),
238:                         200, array('Content-Type' => 'application/json'));
239:             } else
240:             {
241:                 return new Response(json_encode(array(
242:                             "error" => 'WMC:  can not be loaded.')), 200,
243:                         array('Content-Type' => 'application/json'));
244:             }
245:         }
246:         else
247:         {
248:             return new Response(json_encode(array(
249:                         "error" => 'WMC:  can not be loaded.')), 200,
250:                     array('Content-Type' => 'application/json'));
251:         }
252:     }
253: 
254:     protected function saveState()
255:     {
256:         $json = $this->container->get('request')->get("state", null);
257:         if ($json !== null)
258:         {
259:             $state = new State();
260:             $state->setServerurl($this->getBaseUrl());
261:             $state->setSlug($this->application->getSlug());
262:             $state->setTitle("SuggestMap");
263:             $state->setJson($json);
264:             $em    = $this->container->get('doctrine')->getEntityManager();
265:             $em->persist($state);
266:             $em->flush();
267:             return new Response(json_encode(array(
268:                         "id" => $state->getId())), 200,
269:                     array('Content-Type' => 'application/json'));
270:         }
271:         else
272:         {
273:             return new Response(json_encode(array(
274:                         "error" => 'State can not be saved.')), 200,
275:                     array('Content-Type' => 'application/json'));
276:         }
277:     }
278: 
279:     /**
280:      * Returns a json encoded state
281:      * 
282:      * @return \Symfony\Component\HttpFoundation\Response a json encoded result.
283:      */
284:     protected function loadState($stateid)
285:     {
286:         if ($stateid)
287:         {
288:             $state = $this->container->get('doctrine')
289:                     ->getRepository('Mapbender\CoreBundle\Entity\State')
290:                     ->find($stateid);
291:             $id    = $state->getId();
292:             return new Response(json_encode(array("data" => array($id => $state->getJson()))),
293:                     200, array('Content-Type' => 'application/json'));
294:         }
295:         else
296:         {
297:             return new Response(json_encode(array("error" => 'State: ' . $stateid . ' is not found')),
298:                     200, array('Content-Type' => 'application/json'));
299:         }
300:     }
301: 
302:     /**
303:      * Returns a json encoded or html form wmc or error if wmc is not found.
304:      * 
305:      * @return \Symfony\Component\HttpFoundation\Response a json encoded result.
306:      */
307:     protected function getWmc()
308:     {
309:         //@TODO access for loader ???
310:         $wmcid = $this->container->get("request")->get("wmcid", null);
311:         if ($wmcid)
312:         {
313:             $wmc  = $this->container->get('doctrine')
314:                     ->getRepository('Mapbender\WmcBundle\Entity\Wmc')
315:                     ->find($wmcid);
316:             $form = $this->container->get("form.factory")->create(new WmcType(),
317:                     $wmc);
318:             $html = $this->container->get('templating')
319:                     ->render('MapbenderWmcBundle:Wmc:form.html.twig',
320:                     array(
321:                 'form' => $form->createView(),
322:                 'id'   => $this->getEntity()->getId()));
323:             return new Response($html, 200, array('Content-Type' => 'text/html'));
324:         }
325:         else
326:         {
327:             $wmc   = new Wmc();
328:             $wmc->setState(new State());
329:             $state = $wmc->getState();
330:             $state->setServerurl($this->getBaseUrl());
331:             $state->setSlug($this->application->getSlug());
332:             $form  = $this->container->get("form.factory")->create(new WmcType(),
333:                     $wmc);
334:             $html  = $this->container->get('templating')
335:                     ->render('MapbenderWmcBundle:Wmc:form.html.twig',
336:                     array(
337:                 'form' => $form->createView(),
338:                 'id'   => $this->getEntity()->getId()));
339:             return new Response($html, 200, array('Content-Type' => 'text/html'));
340:         }
341:     }
342: 
343:     /**
344:      * Returns a json encoded or html form wmc or error if wmc is not found.
345:      * 
346:      * @return \Symfony\Component\HttpFoundation\Response a json encoded result.
347:      */
348:     protected function loadWmc($wmcid)
349:     {
350:         //@TODO access for loader ???
351:         if ($wmcid)
352:         {
353:             $wmc = $this->container->get('doctrine')
354:                     ->getRepository('Mapbender\WmcBundle\Entity\Wmc')
355:                     ->find($wmcid);
356:             $id  = $wmc->getId();
357:             return new Response(json_encode(array("data" => array($id => $wmc->getState()->getJson()))),
358:                     200, array('Content-Type' => 'application/json'));
359:         }
360:         else
361:         {
362:             return new Response(json_encode(array("error" => 'WMC: ' . $wmcid . ' is not found')),
363:                     200, array('Content-Type' => 'application/json'));
364:         }
365:     }
366: 
367:     /**
368:      * Returns a json encoded wmc or error if wmc is not found.
369:      * 
370:      * @param integer|string $id wmc id
371:      * @return \Symfony\Component\HttpFoundation\Response a json encoded result.
372:      */
373:     protected function removeWmc()
374:     {
375:         //@TODO access for editor ???
376:         $wmcid = $this->container->get("request")->get("wmcid", null);
377:         $this->container->get("request")->attributes->remove("wmcid");
378:         if (!$wmcid)
379:         {
380:             return new Response(json_encode(array(
381:                         "error" => 'Error: wmc id is not found')), 200,
382:                     array('Content-Type' => 'application/json'));
383:         }
384:         $wmc = $this->container->get('doctrine')
385:                 ->getRepository('Mapbender\WmcBundle\Entity\Wmc')
386:                 ->find($wmcid);
387:         if ($wmc)
388:         {
389:             $em = $this->container->get('doctrine')->getEntityManager();
390:             $em->getConnection()->beginTransaction();
391:             if ($wmc->getScreenshotPath() !== null)
392:             {
393:                 $upload_directory = $this->createWmcDirs();
394:                 if ($upload_directory !== null)
395:                 {
396:                     $filepath = $upload_directory . "/". $wmc->getScreenshotPath();
397:                     if(file_exists($filepath))
398:                         unlink ($filepath);
399:                 }
400:             }
401:             $em->remove($wmc);
402:             $em->flush();
403:             $em->getConnection()->commit();
404:             return new Response(json_encode(array(
405:                         "success" => "WMC: " . $wmcid . " is removed.")), 200,
406:                     array('Content-Type' => 'application/json'));
407:         }
408:         else
409:         {
410:             return new Response(json_encode(array(
411:                         "error" => "WMC: " . $wmcid . " is not found")), 200,
412:                     array('Content-Type' => 'application/json'));
413:         }
414:     }
415: 
416:     /**
417:      * Returns a html encoded list of all wmc documents
418:      * 
419:      * @return \Symfony\Component\HttpFoundation\Response 
420:      */
421:     protected function getWmcList()
422:     {
423:         //@TODO access for editor ???
424:         $config   = $this->getConfiguration();
425:         $access = true;
426:         if ($access && $config["useEditor"] === true)
427:         {
428:             $response = new Response();
429:             $entities     = $this->container->get('doctrine')
430:                     ->getRepository('Mapbender\WmcBundle\Entity\Wmc')
431:                     ->findAll();
432:             $responseBody = $this->container->get('templating')
433:                     ->render('MapbenderWmcBundle:Wmc:list.html.twig',
434:                     array("entities" => $entities)
435:             );
436: 
437:             $response->setContent($responseBody);
438:             return $response;
439:         }
440:         else
441:         {
442:             throw new AccessDeniedHttpException('You are not allowed to use this proxy without a session.');
443:         }
444:     }
445: 
446:     protected function saveWmc()
447:     {
448:         //@TODO access for editor ???
449:         $request = $this->container->get('request');
450:         $wmc     = Wmc::create();
451:         $form    = $this->container->get("form.factory")->create(new WmcType(),
452:                 $wmc);
453:         if ($request->getMethod() === 'POST')
454:         {
455:             $form->bindRequest($request);
456:             if ($form->isValid())
457:             { //TODO: Is file an image (jpg/png/gif?)
458:                 if ($wmc->getId() !== null)
459:                 {
460:                     $wmc  = $this->container->get('doctrine')
461:                             ->getRepository('Mapbender\WmcBundle\Entity\Wmc')
462:                             ->find($wmc->getId());
463:                     $form = $this->container->get("form.factory")->create(new WmcType(),
464:                             $wmc);
465:                     $form->bindRequest($request);
466:                     if (!$form->isValid())
467:                     {
468:                         return new Response(json_encode(array(
469:                                     "error" => "WMC: " . $wmc->getId() . " can not be found.")),
470:                                 200, array('Content-Type' => 'application/json'));
471:                     }
472:                 }
473:                 $em = $this->container->get('doctrine')->getEntityManager();
474:                 $em->getConnection()->beginTransaction();
475:                 $em->persist($wmc);
476:                 $em->flush();
477:                 if ($wmc->getScreenshotPath() === null)
478:                 {
479:                     if ($wmc->getScreenshot() !== null)
480:                     {
481:                         $upload_directory = $this->createWmcDirs();
482:                         if ($upload_directory !== null)
483:                         {
484:                             $dirs      = $this->container->getParameter("directories");
485:                             $filename  = sprintf('screenshot-%d.%s',
486:                                     $wmc->getId(),
487:                                     $wmc->getScreenshot()->guessExtension());
488:                             $wmc->getScreenshot()->move($upload_directory,
489:                                     $filename);
490:                             $wmc->setScreenshotPath($filename);
491:                             $format    = $wmc->getScreenshot()->getClientMimeType();
492:                             $url_base  = $request->getScheme() . '://' . $request->getHttpHost() . $request->getBasePath();
493:                             $serverurl = $url_base . "/" . $dirs["wmc"];
494:                             $logourl   = $serverurl . "/" . $this->application->getSlug() . "/" . $filename;
495:                             $logoUrl   = LegendUrl::create(null, null,
496:                                             OnlineResource::create($format,
497:                                                     $logourl));
498:                             $state     = $wmc->getState();
499:                             $state->setServerurl($this->getBaseUrl());
500:                             $state->setSlug($this->application->getSlug());
501:                             $wmc->setLogourl($logoUrl);
502:                         }
503:                     }
504:                     else
505:                     {
506:                         $wmc->setScreenshotPath(null);
507:                     }
508:                     $em->persist($wmc);
509:                     $em->flush();
510:                 }
511:                 $em->getConnection()->commit();
512:                 return new Response(json_encode(array(
513:                             "success" => "WMC: " . $wmc->getId() . " is saved.")),
514:                         200, array('Content-Type' => 'application/json'));
515:             }
516:             else
517:             {
518:                 return new Response(json_encode(array(
519:                             "error" => 'WMC: ' . $wmc->getId() . ' can not be saved.')),
520:                         200, array('Content-Type' => 'application/json'));
521:             }
522:         }
523:     }
524: 
525:     protected function getBaseUrl()
526:     {
527:         $request  = $this->container->get('request');
528:         $dirs     = $this->container->getParameter("directories");
529:         $url_base = $request->getScheme() . '://' . $request->getHttpHost() . $request->getBasePath();
530:         return $url_base;
531:     }
532:     
533:     protected function createWmcDirs()
534:     {
535:         $basedir = $this->container->get('kernel')->getRootDir() . '/../web/';
536:         $dirs    = $this->container->getParameter("directories");
537:         $dir     = $basedir . $dirs["wmc"] . "/" . $this->application->getSlug();
538:         if (!is_dir($dir))
539:         {
540:             $a = mkdir($dir);
541:             if ($a)
542:             {
543:                 return $dir;
544:             }
545:             else
546:             {
547:                 return null;
548:             }
549:         }
550:         else
551:         {
552:             return $dir;
553:         }
554:     }
555: 
556: }
557: 
558: 
Mapbender3 API documenation API documentation generated by ApiGen 2.8.0