public List <NovicellMapBuilderMapsModel> GetAllMaps()
 {
     using (_db)
     {
         var sql = string.Format("SELECT * FROM {0}", StaticHelper.GetMapsTableName());
         return(_db.Query <NovicellMapBuilderMapsModel>(sql).ToList());
     }
 }
 public void RemoveMap(int id)
 {
     using (_db)
     {
         var sql = string.Format("DELETE FROM {0} WHERE id = {1}", StaticHelper.GetMapsTableName(), id);
         _db.Execute(sql);
     }
 }
        public void RemoveDataSource([FromBody] NovicellMapBuilderDataModel data)
        {
            using (_db)
            {
                var sql = string.Format("SELECT * FROM {0} WHERE DataId = {1}", StaticHelper.GetMapsTableName(), data.Id);
                var mapsWithDateSource =
                    _db.Query <NovicellMapBuilderMapsModel>(sql).ToList();

                foreach (var map in mapsWithDateSource)
                {
                    map.DataId = -1;
                    _db.Save(map);
                }

                _db.Delete(data);
            }
        }
        public ApiResult GetMap(int id)
        {
            var result = new ApiResult();

            try
            {
                result.Success = true;
                using (_db)
                {
                    var sql = string.Format("SELECT * FROM {0} WHERE id = {1}", StaticHelper.GetMapsTableName(), id);
                    result.Data = _db.Query <NovicellMapBuilderMapsModel>(sql).FirstOrDefault();
                }
            }
            catch (Exception e)
            {
                result.Success      = false;
                result.ErrorMessage = e.Message;
            }

            return(result);
        }
        protected override TreeNodeCollection GetTreeNodes(string id, FormDataCollection queryStrings)
        {
            var nodes = new TreeNodeCollection();

            string[] rootNodes = { "Maps", "Data" };
            var      ts        = Services.TextService;

            if (id == Constants.System.Root.ToInvariantString())
            {
                foreach (var rootNode in rootNodes)
                {
                    var icon  = "icon-map";
                    var title = ts.Localize("ncmb/maps");
                    if (rootNode == "Data")
                    {
                        icon  = "icon-settings";
                        title = ts.Localize("ncmb/data");
                    }

                    var hasChildren = false;
                    if (rootNode == "Maps")
                    {
                        var mapsItems = Db.ExecuteScalar <int>("SELECT COUNT(Id) FROM " + StaticHelper.GetMapsTableName());
                        hasChildren = mapsItems > 0;
                    }
                    else if (rootNode == "Data")
                    {
                        var dataItems = Db.ExecuteScalar <int>("SELECT COUNT(Id) FROM " + StaticHelper.GetDataTableName());
                        hasChildren = dataItems > 0;
                    }


                    var node = CreateTreeNode(rootNode, "-1", queryStrings, title, icon, hasChildren);

                    nodes.Add(node);
                }
            }
            else if (id == rootNodes[0])
            {
                var mapsItems = Db.Query <NovicellMapBuilderMapsModel>("SELECT * FROM " + StaticHelper.GetMapsTableName()).ToList();
                nodes.AddRange(mapsItems.Select(item => CreateTreeNode("map-" + item.Id.ToString(), rootNodes[0], queryStrings, item.Name, "icon-map-marker")));
            }
            else if (id == rootNodes[1])
            {
                var dataItems = Db.Query <NovicellMapBuilderDataModel>("SELECT * FROM " + StaticHelper.GetDataTableName()).ToList();
                nodes.AddRange(dataItems.Select(item => CreateTreeNode("data-" + item.Id.ToString(), rootNodes[1], queryStrings, item.Name, "icon-server-alt")));
            }

            return(nodes);
        }
        public ApiResult CreateNewMap(string name)
        {
            var result = new ApiResult();

            try
            {
                result.Success = true;
                using (_db)
                {
                    name = name.Trim();
                    var alias    = StaticHelper.UppercaseWordsAndRemoveWhiteSpace(name.Replace("(", string.Empty).Replace(")", string.Empty));
                    var sql      = string.Format("SELECT * FROM {0}", StaticHelper.GetMapsTableName());
                    var allMaps  = _db.Query <NovicellMapBuilderMapsModel>(sql).ToList();
                    var mapName  = allMaps.FirstOrDefault(x => x.Name == name);
                    var mapAlias = allMaps.FirstOrDefault(x => x.Alias == alias);
                    var tmpName  = name;

                    var match = true;
                    var i     = 1;
                    while (match)
                    {
                        if (mapName != null)
                        {
                            tmpName  = name + " (" + i + ")";
                            alias    = StaticHelper.UppercaseWordsAndRemoveWhiteSpace(tmpName.Replace("(", string.Empty).Replace(")", string.Empty));
                            mapName  = allMaps.FirstOrDefault(x => x.Name == tmpName);
                            mapAlias = allMaps.FirstOrDefault(x => x.Alias == alias);

                            i++;
                        }
                        else if (mapAlias != null)
                        {
                            var tmpNameTwo = name + " (" + i + ")";
                            alias    = StaticHelper.UppercaseWordsAndRemoveWhiteSpace(tmpNameTwo.Replace("(", string.Empty).Replace(")", string.Empty));
                            mapAlias = allMaps.FirstOrDefault(x => x.Alias == alias);
                            i++;
                        }
                        else
                        {
                            name  = tmpName;
                            match = false;
                        }
                    }

                    var model = new NovicellMapBuilderMapsModel
                    {
                        Name             = name,
                        Alias            = alias,
                        InitialZoom      = 7,
                        MinZoom          = 7,
                        MaxZoom          = 18,
                        Style            = "[]",
                        DefaultIconStyle = "{\"url\":\"/App_Plugins/NcMapBuilder/Images/map-pin.png\",\"Width\":30,\"Height\":40}",
                        ClusterStyle     = "[{\"anchor\": [0, 0], \"textColor\":\"white\",\"url\":\"/App_Plugins/NcMapBuilder/Images/m1.png\",\"width\":53,\"height\":52}," +
                                           "{\"anchor\": [0, 0], \"textColor\":\"white\",\"url\":\"/App_Plugins/NcMapBuilder/Images/m2.png\",\"width\":56,\"height\":55}," +
                                           "{\"anchor\": [0, 0], \"textColor\":\"white\",\"url\":\"/App_Plugins/NcMapBuilder/Images/m3.png\",\"width\":66,\"height\":65}," +
                                           "{\"anchor\": [0, 0], \"textColor\":\"white\",\"url\":\"/App_Plugins/NcMapBuilder/Images/m4.png\",\"width\":78,\"height\":77}," +
                                           "{\"anchor\": [0, 0], \"textColor\":\"white\",\"url\":\"/App_Plugins/NcMapBuilder/Images/m5.png\",\"width\":90,\"height\":89}]",
                        Center               = "55.89736311199348,10.856373806359898",
                        UseGeoLocation       = true,
                        UseInfoWindows       = true,
                        InfoWindowWidth      = 300,
                        InfoWindowName       = "Default",
                        Draggable            = true,
                        UseZoomControl       = true,
                        UseMapTypeControl    = true,
                        UseStreetViewControl = true,
                        ScrollWheelEnabled   = true,
                        UseHybridMap         = false
                    };

                    result.Data = _db.Insert(model);
                }
            }
            catch (Exception e)
            {
                result.Success      = false;
                result.ErrorMessage = e.Message;
            }

            return(result);
        }