Add nickname (with character restrictions) and comment fields when closing bug
[potlatch2.git] / net / systemeD / potlatch2 / BugLayer.as
1 package net.systemeD.potlatch2 {
2
3     import net.systemeD.halcyon.VectorLayer;
4     import net.systemeD.halcyon.Map;
5     import net.systemeD.halcyon.connection.*;
6     import net.systemeD.halcyon.connection.actions.*;
7     import flash.net.*;
8     import flash.events.*;
9     import com.adobe.serialization.json.JSON;
10
11     public class BugLayer extends VectorLayer {
12
13         private var baseUrl:String;
14         private var apiKey:String;
15
16         // as strings, since that's how they are in tags and http calls
17         private var BUG_STATUS_OPEN:String = "1";
18         private var BUG_STATUS_FIXED:String = "2";
19         private var BUG_STATUS_INVALID:String = "3"; // or 'non-reproduceable'
20         private static const status:Array = ["", "open", "fixed", "invalid"];
21
22         public function BugLayer(n:String, map:Map, s:String, baseUrl:String, apiKey:String) {
23             this.baseUrl = baseUrl;
24             this.apiKey = apiKey;
25             super(n,map,s);
26         }
27
28         public function closeBug(m:Marker, nickname:String = "NoName", comment:String = "No Comment"):void {
29             var id:String = m.getTag('bug_id');
30             var status:String = BUG_STATUS_FIXED;
31
32             //TODO urlencode stuff
33             var urlReq:URLRequest = new URLRequest(baseUrl+"changeBugStatus?id="+id+"&status="+status+"&comment="+encodeURIComponent(comment)+"&nickname="+encodeURIComponent(nickname)+"&key="+apiKey);
34             urlReq.method = "POST";
35             urlReq.data = '    '; // dear Adobe, this is nuts, kthxbye (you can't POST with an empty payload)
36             var loader:URLLoader = new URLLoader();
37             loader.load(urlReq);
38             loader.addEventListener(Event.COMPLETE, function(e:Event):void { bugClosed(e, m); } );
39         }
40
41         private function bugClosed(event:Event, marker:Marker):void {
42             var action:UndoableEntityAction = new SetTagAction(marker, "status", status[int(BUG_STATUS_FIXED)]);
43             action.doAction(); // just do it, don't add to undo stack
44         }
45
46         public override function loadBbox(left:Number, right:Number,
47                                 top:Number, bottom:Number):void {
48             var loader:URLLoader = new URLLoader();
49             loader.load(new URLRequest(baseUrl+"getBugs?bbox="+map.edge_l+","+map.edge_b+","+map.edge_r+","+map.edge_t+"&key="+apiKey));
50             loader.addEventListener(Event.COMPLETE, parseJSON);
51         }
52
53         private function parseJSON(event:Event):void {
54             var result:String = String(event.target.data);
55             if (result) { // api returns 204 no content for no bugs, and the JSON parser treats '' as an error
56               var featureCollection:Object = JSON.decode(result);
57
58               for each (var feature:Object in featureCollection.features) {
59                 // geoJSON spec is x,y,z i.e. lon, lat, ele
60                 var lon:Number = feature.geometry.coordinates[0];
61                 var lat:Number = feature.geometry.coordinates[1];
62                 var tags:Object = {};
63                 tags["name"] = String(feature.properties.description).substr(0,10)+'...';
64                 tags["description"] = feature.properties.description;
65                 tags["bug_id"] = feature.id;
66                 tags["nickname"] = feature.properties.nickname;
67                 tags["type"] = feature.properties.type;
68                 tags["date_created"] = feature.properties.date_created;
69                 tags["date_updated"] = feature.properties.date_updated;
70                 tags["source"] = feature.properties.source;
71                 tags["status"] = status[int(feature.properties.status)];
72                 var marker:Marker = createMarker(tags, lat, lon);
73               }
74               paint.updateEntityUIs(getObjectsByBbox(map.edge_l,map.edge_r,map.edge_t,map.edge_b), true, false);
75             }
76         }
77
78     }
79 }