1: <?php
2: namespace Mapbender\MonitoringBundle\Controller;
3: use Symfony\Bundle\FrameworkBundle\Controller\Controller;
4: use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
5: use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
6: use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
7: use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
8: use Mapbender\MonitoringBundle\Entity\MonitoringDefinition;
9: use Mapbender\MonitoringBundle\Form\MonitoringDefinitionType;
10: use Mapbender\MonitoringBundle\Component\MonitoringRunner;
11: use Mapbender\Component\HTTP\HTTPClient;
12: use Mapbender\WmsBundle\Entity\WMSService;
13:
14: use Symfony\Component\HttpFoundation\Response;
15:
16: use Symfony\Component\HttpFoundation\Request;
17:
18: 19: 20: 21: 22:
23: class MonitoringDefinitionController extends Controller {
24:
25: 26: 27: 28: 29: 30:
31: public function indexAction(array $monitoringDefinitionList) {
32: $total = $this->getDoctrine()
33: ->getEntityManager()
34: ->createQuery("SELECT count(mb.id) as total From MapbenderMonitoringBundle:MonitoringDefinition mb")
35: ->getScalarResult();
36:
37: $total = $total[0]['total'];
38: $request = $this->get('request');
39: $offset = $request->get('usedOffset');
40: $limit = $request->get('usedLimit');
41: $nextOffset = count($monitoringDefinitionList) < $limit ? $offset : $offset + $limit;
42: $prevOffset = ($offset - $limit) > 0 ? $offset - $limit : 0;
43: $lastOffset = ($total - $limit) > 0 ? $total - $limit : 0;
44: return array(
45: "offset" => $offset,
46: "nextOffset" => $nextOffset,
47: "prevOffset" => $prevOffset,
48: "lastOffset" => $lastOffset,
49: "limit" => $limit,
50: "total" => $total,
51: "mdList" => $monitoringDefinitionList,
52:
53: );
54: }
55:
56: 57: 58: 59: 60:
61: public function createAction() {
62: $form = $this->get("form.factory")->create(
63: new MonitoringDefinitionType(),
64: new MonitoringDefinition()
65: );
66:
67:
68: return array(
69: "form" => $form->createView()
70: );
71: }
72:
73: 74: 75: 76: 77:
78: public function importAction(WMSService $wms) {
79: $md = new MonitoringDefinition();
80: $md->setType(get_class($wms));
81: $md->setTypeId($wms->getId());
82: $md->setName($wms->getName());
83: $md->setTitle($wms->getTitle());
84: $md->setRequestUrl($wms->getOnlineResource());
85:
86: $em = $this->getDoctrine()
87: ->getEntityManager();
88: $em->persist($md);
89: $em->flush();
90: return $this->redirect($this->generateUrl(
91: "mapbender_monitoring_monitoringdefinition_edit",
92: array("mdId" => $md->getId())
93: ));
94: }
95:
96: 97: 98: 99:
100: public function addAction() {
101: $md = new MonitoringDefinition();
102:
103: $form = $this->get("form.factory")->create(
104: new MonitoringDefinitionType(),
105: $md
106: );
107:
108: $request = $this->get("request");
109:
110: $form->bindRequest($request);
111:
112: if($form->isValid()) {
113: $em = $this->getDoctrine()->getEntityManager();
114: $em->persist($md);
115: $em->flush();
116: return $this->redirect($this->generateUrl("mapbender_monitoring_monitoringdefinition_index"));
117: } else {
118: return $this->render(
119: "MapbenderMonitoringBundle:MonitoringDefinition:create.html.twig",
120: array("form" => $form->createView())
121: );
122: }
123: }
124:
125: 126: 127: 128: 129:
130: public function editAction(MonitoringDefinition $md) {
131: $form = $this->get("form.factory")->create(
132: new MonitoringDefinitionType(),
133: $md
134: );
135: $query = $this->getDoctrine()->getEntityManager()->createQuery(
136: "SELECT j From MapbenderMonitoringBundle:MonitoringJob j"
137: ." WHERE j.monitoringDefinition= :md"
138: ." ORDER BY j.timestamp DESC")
139: ->setMaxResults(5)
140: ->setParameter("md", $md->getId());
141: $lastjobs = $query->getResult();
142:
143: return array(
144: "form" => $form->createView(),
145: "md" => $md,
146: "lastjobs" => $lastjobs
147: );
148: }
149:
150: 151: 152: 153: 154:
155: public function fromwmsdeleteAction(MonitoringDefinition $md){
156:
157: try{
158: $em = $this->getDoctrine()->getEntityManager();
159: $em->remove($md);
160: $em->flush();
161:
162: }catch(\Exception $e){
163:
164: }
165: return $this->redirect($this->generateUrl(
166: "mapbender_monitoring_monitoringdefinition_index"));
167: }
168:
169:
170: 171: 172: 173:
174: public function deleteAction(MonitoringDefinition $md) {
175: $em = $this->getDoctrine()->getEntityManager();
176: try {
177: $em->remove($md);
178: $em->flush();
179: } catch(\Exception $E) {
180: $this->get("logger")->info("Could not delete monitoring definition. ".$E->getMessage());
181: $this->get("session")->setFlash("error","Could not delete monitoring definition.");
182: return $this->redirect($this->generateUrl("mapbender_monitoring_monitoringdefinition_index"));
183: }
184:
185: $this->get("session")->setFlash("success","Your monitoring definition has been deleted.");
186: return $this->redirect($this->generateUrl("mapbender_monitoring_monitoringdefinition_index"));
187: }
188:
189: 190: 191: 192: 193:
194: public function confirmDeleteAction(MonitoringDefinition $md) {
195:
196: return array(
197: "md" => $md
198: );
199: }
200:
201: 202: 203: 204:
205: public function saveAction(MonitoringDefinition $md) {
206: $form = $this->get("form.factory")->create(
207: new MonitoringDefinitionType(),
208: $md
209: );
210:
211: $request = $this->get("request");
212:
213: $form->bindRequest($request);
214:
215: if($form->isValid()) {
216: try {
217: $em = $this->getDoctrine()->getEntityManager();
218: $em->persist($md);
219: $em->flush();
220: } catch(\Exception $E) {
221: $this->get("logger")->err("Could not save monitoring definition. ".$E->getMessage());
222: $this->get("session")->setFlash("error","Could not save monitoring definition");
223: return $this->redirect($this->generateUrl("mapbender_monitoring_monitoringdefinition_edit",array("mdId" => $md->getId())));
224: }
225: return $this->redirect($this->generateUrl("mapbender_monitoring_monitoringdefinition_index"));
226: } else {
227: return $this->render(
228: "MapbenderMonitoringBundle:MonitoringDefinition:edit.html.twig",
229: array("form" => $form->createView(),
230: "md" => $md)
231: );
232: }
233: }
234:
235: 236: 237: 238:
239: public function runAction(MonitoringDefinition $md) {
240: $client = new HTTPClient($this->container);
241: $mr = new MonitoringRunner($md,$client);
242: $job = $mr->run();
243: if($md->getLastMonitoringJob()){
244: if(strcmp($job->getResult(), $md->getLastMonitoringJob()->getResult()) != 0){
245: $job->setChanged(true);
246: } else {
247: $job->setChanged(false);
248: }
249: }else {
250: $job->setChanged(true);
251: }
252: $md->addMonitoringJob($job);
253: $em = $this->getDoctrine()->getEntityManager();
254: $em->persist($md);
255: $em->flush();
256: return $this->redirect(
257: $this->generateUrl(
258: "mapbender_monitoring_monitoringdefinition_edit",
259: array("mdId" => $md->getId())
260: )
261: );
262: }
263:
264: 265: 266: 267:
268: public function statresetAction(MonitoringDefinition $md) {
269: $em = $this->getDoctrine()->getEntityManager();
270: foreach($md->getMonitoringJobs() as $job){
271: $em->remove($job);
272: }
273: $em->flush();
274: return $this->redirect(
275: $this->generateUrl(
276: "mapbender_monitoring_monitoringdefinition_edit",
277: array("mdId" => $md->getId())
278: )
279: );
280: }
281:
282: 283: 284: 285: 286:
287: public function showAction($jId) {
288: $tr = $this->get('translator');
289: $job = $this->getDoctrine()->getRepository("MapbenderMonitoringBundle:MonitoringJob")
290: ->findOneById($jId);
291: $result = array("html" => "<pre>".htmlentities($job->getResult())."</pre>",
292: "error" => "", "title" => $tr->trans('Job_result'));
293: $response = new Response();
294: $response->setContent(json_encode($result));
295: $response->headers->set('Content-Type', 'application/json');
296: return $response;
297: }
298: }
299:
300: ?>