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