1: <?php
2:
3: namespace Mapbender\WmcBundle\Element;
4:
5: use Mapbender\CoreBundle\Component\Element;
6: use Mapbender\WmcBundle\Entity\Wmc;
7: use Symfony\Component\HttpFoundation\Response;
8: use Symfony\Component\Security\Core\User\UserInterface;
9: use Doctrine\ORM\Query\Expr\Andx;
10: use Doctrine\ORM\Query\Expr\Comparison;
11:
12: class WmcStorage extends Element {
13: static public function getClassTitle() {
14: return "WMC Storage";
15: }
16:
17: static public function getClassDescription() {
18: return "Stores and loads WMC documents. Can provide a dialog for "
19: . "selecting and saving.";
20: }
21:
22: static public function getClassTags() {
23: return array('WMC');
24: }
25:
26: public function getAssets() {
27: return array(
28: 'js' => array('mapbender.element.wmcstorage.js'),
29: 'css' => array());
30: }
31:
32: public static function getDefaultConfiguration() {
33: return array(
34: 'target' => null);
35: }
36:
37: public function getWidgetName() {
38: return 'mapbender.mbWmcStorage';
39: }
40:
41: public function httpAction($action) {
42: $response = new Response();
43: $request = $this->get('request');
44: $em = $this->get('doctrine')->getEntityManager();
45: $repository = $this->get('doctrine')->getRepository('MapbenderWmcBundle:Wmc');
46:
47: switch($action) {
48: case 'downloadDirect':
49: $response->setContent(file_get_contents($request->files->get('wmcContent')));
50: $response->headers->set('Content-Type', 'application/xml');
51: return $response;
52: break;
53: case 'download':
54: $response->setContent($request->get('wmcContent'));
55: $response->headers->set('Content-Type', 'application/xml');
56: $response->headers->set('Content-Disposition', 'attachment; filename=wmc.xml');
57: return $response;
58: break;
59:
60: case 'save':
61:
62: $owner = $this->get('security.context')->getToken()->getUser()->getUsername();
63: $title = $request->get('title');
64: $public = strtolower($request->get('public'));
65: $public = $public === 'true' ? true : false;
66: if(!$title) {
67: throw new \Exception('You did not send a title for the WMC document.');
68: }
69: $crs = $request->get('crs');
70:
71: $wmcDocument = file_get_contents('php://input');
72:
73: $wmc = $repository->findOneBy(array(
74: 'title' => $title,
75: 'owner' => $owner
76: ));
77:
78: $status = array(
79: 'code' => 'ok'
80: );
81:
82: if($wmc) {
83: } else {
84:
85: $wmc = new Wmc();
86: $wmc->setTitle($title);
87: $wmc->setOwner($owner);
88: $wmc->setPublic($public);
89: $wmc->setDocument($wmcDocument);
90: $wmc->setCrs($crs);
91:
92: try {
93: $em->persist($wmc);
94: $em->flush();
95: $status['message'] = sprintf('Your WMC document was stored with id %d.', $wmc->getId());
96: } catch (\Exception $e) {
97: $status['code'] = 'error';
98: $status['message'] = $e->getMessage();
99: }
100: }
101: $response->setContent(json_encode($status));
102: $response->headers->set('Content-Type', 'application/json');
103: return $response;
104: break;
105:
106: case 'list':
107:
108: $owner = $this->get('security.context')->getToken()->getUser()->getUsername();
109: $params = $request->get('params', array());
110: if(!is_array($params)) {
111: throw new \Exception('The params parameter must be an array.');
112: }
113: $params = array_merge($params, array('owner' => $owner));
114: $qb = $repository->createQueryBuilder('w');
115:
116: $where = $qb->expr()->andx(
117: $qb->expr()->eq('w.crs', ':crs'));
118:
119: if($params['private'] === 'true') {
120: $where->add($qb->expr()->eq('w.owner', ':owner'));
121: } else {
122: $where->add(
123: $qb->expr()->orx(
124: $qb->expr()->eq('w.owner', ':owner'),
125: $qb->expr()->eq('w.public', 'true')
126: )
127: );
128: }
129:
130: $qb->add('where', $where);
131:
132: $qb->setParameter('crs', $params['crs']);
133: $qb->setParameter('owner', $owner);
134:
135: $list = $qb->getQuery()->getResult();
136:
137: foreach($list as &$item) {
138: $item = array(
139: 'id' => $item->getId(),
140: 'title' => $item->getTitle()
141: );
142: }
143: $response->setContent(json_encode($list));
144: $response->headers->set('Content-Type', 'application/json');
145: return $response;
146: break;
147:
148: case 'load':
149:
150: $owner = $this->get('security.context')->getToken()->getUser()->getUsername();
151: $params = $request->get('params');
152: if(!is_array($params)) {
153: throw new \Exception('The params parameter must be an array.');
154: }
155:
156: $qb = $repository->createQueryBuilder('w');
157:
158: $where = $qb->expr()->andx(
159: $qb->expr()->orx(
160: $qb->expr()->eq('w.owner', ':owner'),
161: $qb->expr()->eq('w.public', 'true')
162: )
163: );
164: foreach($params as $key => $value) {
165: $where->add(new Comparison("w.$key", '=', ":$key"));
166: $qb->setParameter($key, $value);
167: }
168:
169: $qb->add('where', $where);
170:
171: $qb->setParameter('owner', $owner);
172:
173: $wmc = $qb->getQuery()->getSingleResult();
174: if(!$wmc) {
175: throw new \Exception('No WMC found for your paramters.');
176: }
177: $response->setContent($wmc->getDocument());
178: $response->headers->set('Content-Type', 'application/xml');
179: return $response;
180: break;
181: case 'delete':
182:
183: $owner = $this->get('security.context')->getToken()->getUser()->getUsername();
184: $id = intval($request->get('id'));
185: $wmc = $repository->findOneBy(array(
186: 'id' => $id,
187: 'owner' => $owner
188: ));
189:
190: $em->remove($wmc);
191: $em->flush();
192: $response->setContent($id);
193: return $response;
194: break;
195: }
196: return parent::httpAction($action);
197: }
198:
199: public function render() {
200: return $this->container->get('templating')
201: ->render('MapbenderWmcBundle:Element:wmcstorage.html.twig', array(
202: 'id' => $this->getId(),
203: 'configuration' => $this->getConfiguration()));
204: }
205: }
206:
207: