Пример #1
0
func (t *DataSourceController) SaveDataSource(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson
	r.Request.ParseMultipartForm(32 << 20)

	payload := map[string]string{}
	err := r.GetForms(&payload)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	_id := payload["_id"]
	if _id == "" {
		_id = helper.RandomIDWithPrefix("ds")
	}

	// upload file
	if payload["type"] == "file" {
		filename := fmt.Sprintf("datasource-%s.json", _id)
		filepath := t.AppViewsPath + "data/datasource/" + filename

		_, _, err = helper.FetchThenSaveFile(r.Request, "file", filepath)
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}
		payload["path"] = filename
	}

	delete(payload, "file")

	if payload["_id"] == "" {
		payload["_id"] = _id

		// insert
		connection, err := helper.LoadConfig(t.AppViewsPath + "/data/datasource.json")
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}
		defer connection.Close()

		err = connection.NewQuery().Insert().Exec(toolkit.M{"data": payload})
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}
	} else {
		// update
		connection, err := helper.LoadConfig(t.AppViewsPath + "/data/datasource.json")
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}
		defer connection.Close()

		err = connection.NewQuery().Update().Where(dbox.Eq("_id", _id)).Exec(toolkit.M{"data": payload})
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}
	}

	return helper.Result(true, nil, "")
}
Пример #2
0
func (t *GridController) GetDetailGrid(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson
	payload := map[string]string{}
	err := r.GetForms(&payload)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	connection, err := helper.LoadConfig(t.AppViewsPath + "data/grid/" + payload["recordid"])
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}
	defer connection.Close()

	cursor, err := connection.NewQuery().Select("*").Cursor(nil)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}
	defer cursor.Close()

	res := []toolkit.M{}
	err = cursor.Fetch(&res, 0, false)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}
	if len(res) == 0 {
		return helper.Result(false, nil, "No data found")
	}

	return helper.Result(true, res, "")
}
Пример #3
0
func (t *SelectorController) GetSelectorConfigs(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson
	configFilepath := t.AppViewsPath + "data/selector.json"

	if _, err := os.Stat(configFilepath); err != nil {
		if os.IsNotExist(err) {
			os.Create(configFilepath)
		} else {
			return helper.Result(false, nil, err.Error())
		}
	}

	connection, err := helper.LoadConfig(configFilepath)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}
	defer connection.Close()

	cursor, err := connection.NewQuery().Select("*").Cursor(nil)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}
	defer cursor.Close()

	dataSource, err := cursor.Fetch(nil, 0, false)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	if len(dataSource.Data) > 0 {
		return helper.Result(true, dataSource.Data, "")
	}

	return helper.Result(true, []interface{}{}, "")
}
Пример #4
0
func (t *DesignerController) GetConfig(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	payload := map[string]string{}
	err := r.GetForms(&payload)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}
	_id := payload["_id"]

	data, err := t.getConfig(_id)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	connection, err := helper.LoadConfig(t.AppViewsPath + "/data/routes.json")
	if !helper.HandleError(err) {
		helper.Result(false, nil, err.Error())
	}
	defer connection.Close()

	cursor, err := connection.NewQuery().Where(dbox.Eq("_id", _id)).Cursor(nil)
	if !helper.HandleError(err) {
		helper.Result(false, nil, err.Error())
	}
	defer cursor.Close()

	dataSource, err := cursor.Fetch(nil, 0, false)
	if !helper.HandleError(err) {
		helper.Result(false, nil, err.Error())
	}
	data["href"] = dataSource.Data[0].(map[string]interface{})["href"]

	return helper.Result(true, data, "")
}
Пример #5
0
func (t *ChartController) RemoveChartConfig(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	payload := map[string]string{}
	err := r.GetForms(&payload)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	// remove chart
	filename := fmt.Sprintf("chart-%s.json", payload["_id"])
	err = os.Remove(t.AppViewsPath + "data/chart/" + filename)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	// remove chart meta data
	connection, err := helper.LoadConfig(t.AppViewsPath + "data/chart.json")
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}
	defer connection.Close()

	err = connection.NewQuery().Delete().Where(dbox.Eq("_id", payload["_id"])).Exec(nil)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	return helper.Result(true, nil, "")
}
Пример #6
0
func (t *DataSourceController) RemoveDataSource(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	payload := map[string]string{}
	err := r.GetForms(&payload)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	connection, err := helper.LoadConfig(t.AppViewsPath + "data/datasource.json")
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}
	defer connection.Close()

	err = connection.NewQuery().Delete().Where(dbox.Eq("_id", payload["_id"])).Exec(nil)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	if payload["type"] == "file" {
		err = os.Remove(t.AppViewsPath + "data/datasource/" + payload["path"])
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}
	}

	return helper.Result(true, nil, "")
}
Пример #7
0
func (t *GridController) GetGridData(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	connection, err := helper.LoadConfig(t.AppViewsPath + "data/mapgrid.json")
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}
	defer connection.Close()

	cursor, err := connection.NewQuery().Select("seq", "data").Cursor(nil)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}
	defer cursor.Close()

	res := []toolkit.M{}
	err = cursor.Fetch(&res, 0, false)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}
	if len(res) == 0 {
		return helper.Result(false, nil, "No data found")
	}

	return helper.Result(true, res, "")
}
Пример #8
0
func (t *ChartController) GetChartConfig(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	payload := map[string]string{}
	err := r.GetForms(&payload)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	filename := fmt.Sprintf("chart-%s.json", payload["_id"])
	isWithDataSource, err := strconv.ParseBool(payload["isWithDataSource"])
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	fileContent, err := ioutil.ReadFile(t.AppViewsPath + "data/chart/" + filename)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	data := map[string]interface{}{}
	err = json.Unmarshal(fileContent, &data)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	if isWithDataSource {
		dataSourceID := data["outsider"].(map[string]interface{})["dataSourceKey"].(string)

		connection, err := helper.LoadConfig(t.AppViewsPath + "data/datasource.json")
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}
		defer connection.Close()

		cursor, err := connection.NewQuery().Where(dbox.Eq("_id", dataSourceID)).Cursor(nil)
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}
		defer cursor.Close()

		res := []toolkit.M{}
		err = cursor.Fetch(&res, 0, false)
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}
		if len(res) == 0 {
			return helper.Result(false, nil, "No data found")
		}

		dsID := res[0].GetString("_id")
		dsType := res[0].GetString("type")
		dsPath := res[0].GetString("path")
		dataSource, _ := helper.FetchDataSource(dsID, dsType, dsPath)
		data["dataSource"] = map[string]interface{}{"data": dataSource}
	}

	return helper.Result(true, data, "")
}
Пример #9
0
func (t *SelectorController) SaveSelector(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson
	payload := map[string]string{}
	err := r.GetForms(&payload)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	_id := payload["ID"]
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	if _id == "" {
		_id = helper.RandomIDWithPrefix("c")
		connection, err := helper.LoadConfig(t.AppViewsPath + "/data/selector.json")
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}
		defer connection.Close()

		newData := toolkit.M{"ID": _id, "title": payload["title"], "fields": payload["fields"], "masterDataSource": payload["masterDataSource"]}
		err = connection.NewQuery().Insert().Exec(toolkit.M{"data": newData})
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}
	} else {
		connection, err := helper.LoadConfig(t.AppViewsPath + "/data/selector.json")
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}
		defer connection.Close()

		newData := toolkit.M{"ID": _id, "title": payload["title"], "fields": payload["fields"], "masterDataSource": payload["masterDataSource"]}
		err = connection.NewQuery().Update().Where(dbox.Eq("ID", _id)).Exec(toolkit.M{"data": newData})
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}
	}

	return helper.Result(true, _id, "")
}
Пример #10
0
func (t *PageController) GetRoutes(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	connection, err := helper.LoadConfig(t.AppViewsPath + "data/routes.json")
	helper.HandleError(err)
	defer connection.Close()

	cursor, err := connection.NewQuery().Select("*").Cursor(nil)
	helper.HandleError(err)
	defer cursor.Close()

	res := []toolkit.M{}
	err = cursor.Fetch(&res, 0, false)
	helper.HandleError(err)

	routes := []map[string]interface{}{}

	for _, each := range res {
		submenu := each.Get("submenu").([]interface{})

		routes = append(routes, map[string]interface{}{
			"_id":       each["_id"],
			"title":     each["title"],
			"href":      each["href"],
			"has_child": len(submenu) > 0,
		})

		for _, subRaw := range submenu {
			sub := subRaw.(map[string]interface{})
			submenu2 := sub["submenu"].([]interface{})

			routes = append(routes, map[string]interface{}{
				"_id":       fmt.Sprintf("%v|%v", each["_id"], sub["_id"]),
				"title":     fmt.Sprintf("%v|%v", each["title"], sub["title"]),
				"href":      sub["href"],
				"has_child": len(submenu2) > 0,
			})

			for _, subRaw2 := range submenu2 {
				sub2 := subRaw2.(map[string]interface{})
				submenu2 := sub2["submenu"].([]interface{})

				routes = append(routes, map[string]interface{}{
					"_id":       fmt.Sprintf("%v|%v|%v", each["_id"], sub["_id"], sub2["_id"]),
					"title":     fmt.Sprintf("%v|%v|%v", each["title"], sub["title"], sub2["title"]),
					"href":      sub["href"],
					"has_child": len(submenu2) > 0,
				})
			}
		}
	}

	return routes
}
Пример #11
0
func (t *TemplateController) GetHeader(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	connection, err := helper.LoadConfig(t.AppViewsPath + "data/header-app.json")
	helper.HandleError(err)
	defer connection.Close()
	cursor, err := connection.NewQuery().Select("*").Cursor(nil)
	helper.HandleError(err)
	if cursor == nil {
		fmt.Printf("Cursor not initialized")
	}
	defer cursor.Close()
	dataSource, err := cursor.Fetch(nil, 0, false)
	helper.HandleError(err)

	return dataSource.Data
}
Пример #12
0
func (t *TemplateController) GetMenuLeft(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	connection, err := helper.LoadConfig(t.AppViewsPath + "data/left-menu.json")
	helper.HandleError(err)
	defer connection.Close()
	cursor, err := connection.NewQuery().Select("*").Cursor(nil)
	helper.HandleError(err)
	if cursor == nil {
		fmt.Printf("Cursor not initialized")
	}
	defer cursor.Close()
	res := []toolkit.M{}
	err = cursor.Fetch(&res, 0, false)
	helper.HandleError(err)

	return res
}
Пример #13
0
func (t *DataSourceController) getDataSourceMetaData(_id string) (map[string]interface{}, error) {
	connection, err := helper.LoadConfig(t.AppViewsPath + "/data/datasource.json")
	if !helper.HandleError(err) {
		return nil, err
	}
	defer connection.Close()

	cursor, err := connection.NewQuery().Where(dbox.Eq("_id", _id)).Cursor(nil)
	if !helper.HandleError(err) {
		return nil, err
	}
	defer cursor.Close()

	dataSource, err := cursor.Fetch(nil, 0, false)
	if !helper.HandleError(err) {
		return nil, err
	}

	return dataSource.Data[0].(map[string]interface{}), nil
}
Пример #14
0
func (t *DataSourceController) GetDataSources(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	connection, err := helper.LoadConfig(t.AppViewsPath + "data/datasource.json")
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}
	defer connection.Close()

	cursor, err := connection.NewQuery().Select("*").Cursor(nil)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}
	defer cursor.Close()

	dataSource, err := cursor.Fetch(nil, 0, false)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	return helper.Result(true, dataSource.Data, "")
}
Пример #15
0
func (t *SelectorController) RemoveSelectorConfig(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	payload := map[string]string{}
	err := r.GetForms(&payload)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	connection, err := helper.LoadConfig(t.AppViewsPath + "data/selector.json")
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}
	defer connection.Close()

	err = connection.NewQuery().Delete().Where(dbox.Eq("ID", payload["ID"])).Exec(nil)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	return helper.Result(true, nil, "")
}
Пример #16
0
func (t *DesignerController) GetConfig(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	payload := map[string]string{}
	err := r.GetForms(&payload)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}
	_id := payload["_id"]

	data, err := t.getConfig(_id)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	connection, err := helper.LoadConfig(t.AppViewsPath + "/data/routes.json")
	if !helper.HandleError(err) {
		helper.Result(false, nil, err.Error())
	}
	defer connection.Close()

	cursor, err := connection.NewQuery().Where(dbox.Eq("_id", _id)).Cursor(nil)
	if !helper.HandleError(err) {
		helper.Result(false, nil, err.Error())
	}
	defer cursor.Close()

	res := []toolkit.M{}
	err = cursor.Fetch(&res, 0, false)
	if !helper.HandleError(err) {
		helper.Result(false, nil, err.Error())
	}
	if len(res) == 0 {
		return helper.Result(false, nil, "No data found")
	}
	data["href"] = res[0].GetString("href")

	return helper.Result(true, data, "")
}
Пример #17
0
func (t *ChartController) GetChartConfigs(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	configFilepath := t.AppViewsPath + "data/chart.json"

	if _, err := os.Stat(configFilepath); err != nil {
		if os.IsNotExist(err) {
			os.Create(configFilepath)
		} else {
			return helper.Result(false, nil, err.Error())
		}
	}

	connection, err := helper.LoadConfig(configFilepath)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}
	defer connection.Close()

	cursor, err := connection.NewQuery().Select("*").Cursor(nil)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}
	defer cursor.Close()

	res := []toolkit.M{}
	err = cursor.Fetch(&res, 0, false)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	if len(res) > 0 {
		return helper.Result(true, res, "")
	}

	return helper.Result(true, res, "")
}
Пример #18
0
func (t *DataSourceController) getDataSourceMetaData(_id string) (toolkit.M, error) {
	connection, err := helper.LoadConfig(t.AppViewsPath + "/data/datasource.json")
	if !helper.HandleError(err) {
		return nil, err
	}
	defer connection.Close()

	cursor, err := connection.NewQuery().Where(dbox.Eq("_id", _id)).Cursor(nil)
	if !helper.HandleError(err) {
		return nil, err
	}
	defer cursor.Close()

	res := []toolkit.M{}
	err = cursor.Fetch(&res, 0, false)
	if !helper.HandleError(err) {
		return nil, err
	}
	if len(res) == 0 {
		return nil, errors.New("No data found")
	}

	return res[0], nil
}
Пример #19
0
func (t *PageController) DeleteRoute(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	payload := map[string]string{}
	err := r.GetForms(&payload)
	helper.HandleError(err)
	_ids := strings.Split(payload["_id"], "|")

	connection, err := helper.LoadConfig(t.AppViewsPath + "data/routes.json")
	helper.HandleError(err)
	defer connection.Close()

	cursor, err := connection.NewQuery().Select("*").Cursor(nil)
	helper.HandleError(err)
	defer cursor.Close()

	res := []toolkit.M{}
	err = cursor.Fetch(&res, 0, false)
	helper.HandleError(err)

	// someday, someone would fix these codes, I hope
	// --------- suffering start here
outer:
	for i, each := range res {
		submenu := each["submenu"].([]interface{})

		if len(_ids) == 1 {
			if _ids[0] == each["_id"].(string) {
				res = append(res[:i], res[i+1:]...)
				break outer
			}
		}

		for j, subRaw := range submenu {
			sub := subRaw.(map[string]interface{})
			submenu2 := sub["submenu"].([]interface{})

			if len(_ids) == 2 {
				if _ids[0] == each["_id"].(string) && _ids[1] == sub["_id"].(string) {
					res[i]["submenu"] = append(submenu[:j], submenu[j+1:]...)
					break outer
				}
			}

			for k, subRaw2 := range submenu2 {
				sub2 := subRaw2.(map[string]interface{})

				if len(_ids) == 3 {
					if _ids[0] == each["_id"].(string) && _ids[1] == sub["_id"].(string) && _ids[2] == sub2["_id"].(string) {
						res[i]["submenu"].([]interface{})[j].(map[string]interface{})["submenu"] = append(submenu2[:k], submenu2[k+1:]...)
						break outer
					}
				}
			}
		}
	}
	// --------- suffering end here

	bytes, err := json.Marshal(res)
	helper.HandleError(err)
	ioutil.WriteFile(t.AppViewsPath+"data/routes.json", bytes, 0644)

	return true
}
Пример #20
0
func (t *PageController) SaveRoute(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	payload := map[string]string{}
	err := r.GetForms(&payload)
	helper.HandleError(err)
	_ids := strings.Split(payload["_id"], "|")
	parentIDs := strings.Split(payload["parentID"], "|")

	newData := toolkit.M{
		"_id":     helper.RandomIDWithPrefix("r"),
		"title":   payload["title"],
		"href":    payload["href"],
		"submenu": []interface{}{},
	}

	if payload["_id"] == "" {
		if payload["parentID"] == "" {
			// fresh row

			connection, err := helper.LoadConfig(t.AppViewsPath + "data/routes.json")
			helper.HandleError(err)
			defer connection.Close()
			err = connection.NewQuery().Insert().Exec(toolkit.M{"data": newData})
			helper.HandleError(err)
		} else {
			// update previous row

			connection, err := helper.LoadConfig(t.AppViewsPath + "data/routes.json")
			helper.HandleError(err)
			defer connection.Close()

			cursor, err := connection.NewQuery().Select("*").Cursor(nil)
			helper.HandleError(err)
			defer cursor.Close()

			res := []toolkit.M{}
			err = cursor.Fetch(&res, 0, false)
			helper.HandleError(err)

		outer:
			for h, top := range res {
				if top.Get("_id") == parentIDs[0] {
					if len(parentIDs) == 1 {
						res[h]["submenu"] = append(top.Get("submenu").([]interface{}), newData)
						break outer
					} else {
						for i, each := range top.Get("submenu").([]interface{}) {
							if each.(toolkit.M)["_id"].(string) == parentIDs[1] {
								top.Get("submenu").([]interface{})[i].(toolkit.M)["submenu"] = append(top.Get("submenu").([]interface{})[i].(toolkit.M)["submenu"].([]interface{}), newData)
								break outer
							}
						}
					}
				}
			}

			bytes, err := json.Marshal(res)
			helper.HandleError(err)
			ioutil.WriteFile(t.AppViewsPath+"data/routes.json", bytes, 0644)
		}
	} else {
		// update existing row

		connection, err := helper.LoadConfig(t.AppViewsPath + "data/routes.json")
		helper.HandleError(err)
		defer connection.Close()

		cursor, err := connection.NewQuery().Select("*").Cursor(nil)
		helper.HandleError(err)
		defer cursor.Close()

		res := []toolkit.M{}
		err = cursor.Fetch(&res, 0, false)
		helper.HandleError(err)

	outer2:
		for h, top := range res {
			if len(_ids) == 1 {
				if top.GetString("_id") == _ids[0] {
					top["title"] = payload["title"]
					top["href"] = payload["href"]
					break outer2
				}
			} else if len(_ids) == 2 {
				submenu := res[h]["submenu"].([]interface{})

				for i, each := range submenu {
					if each.(map[string]interface{})["_id"].(string) == _ids[1] {
						res[h]["submenu"].([]interface{})[i].(map[string]interface{})["title"] = payload["title"]
						res[h]["submenu"].([]interface{})[i].(map[string]interface{})["href"] = payload["href"]
						break outer2
					}
				}
			} else {
				submenu := res[h]["submenu"].([]interface{})

				for i, each := range submenu {
					if each.(map[string]interface{})["_id"].(string) == _ids[1] {
						submenu2 := each.(map[string]interface{})["submenu"].([]interface{})

						for j, each2 := range submenu2 {
							if each2.(map[string]interface{})["_id"].(string) == _ids[2] {
								res[h]["submenu"].([]interface{})[i].(map[string]interface{})["submenu"].([]interface{})[j].(map[string]interface{})["title"] = payload["title"]
								res[h]["submenu"].([]interface{})[i].(map[string]interface{})["submenu"].([]interface{})[j].(map[string]interface{})["href"] = payload["href"]
								break outer2
							}
						}
					}
				}
			}
		}

		bytes, err := json.Marshal(res)
		helper.HandleError(err)
		ioutil.WriteFile(t.AppViewsPath+"data/routes.json", bytes, 0644)
	}

	return true
}
Пример #21
0
func (t *DesignerController) GetWidget(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	payload := map[string]interface{}{}
	err := r.GetForms(&payload)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	if payload["type"].(string) == "chart" {
		bytes, err := ioutil.ReadFile(t.AppViewsPath + "data/chart/chart-" + payload["widgetID"].(string) + ".json")
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}

		data := map[string]interface{}{}
		err = json.Unmarshal(bytes, &data)
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}

		return helper.Result(true, data, "")
	} else if payload["type"].(string) == "grid" {
		connection, err := helper.LoadConfig(t.AppViewsPath + "data/grid/" + payload["widgetID"].(string) + ".json")
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}
		defer connection.Close()

		cursor, err := connection.NewQuery().Select("*").Cursor(nil)
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}
		defer cursor.Close()

		dataSource, err := cursor.Fetch(nil, 0, false)
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}

		return helper.Result(true, dataSource.Data, "")
	} else if payload["type"] == "selector" {
		connection, err := helper.LoadConfig(t.AppViewsPath + "data/selector.json")
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}
		defer connection.Close()

		cursor, err := connection.NewQuery().Where(dbox.Eq("_id", payload["widgetID"].(string))).Cursor(nil)
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}
		defer cursor.Close()

		dataSource, err := cursor.Fetch(nil, 0, false)
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}

		return helper.Result(true, dataSource.Data, "")
	}

	return helper.Result(true, map[string]interface{}{}, "")
}
Пример #22
0
func (t *PageController) GetRoute(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	payload := map[string]string{}
	err := r.GetForms(&payload)
	helper.HandleError(err)
	_ids := strings.Split(payload["_id"], "|")

	connection, err := helper.LoadConfig(t.AppViewsPath + "data/routes.json")
	helper.HandleError(err)
	defer connection.Close()

	cursor, err := connection.NewQuery().Select("*").Cursor(nil)
	helper.HandleError(err)
	defer cursor.Close()

	res := []toolkit.M{}
	err = cursor.Fetch(&res, 0, false)
	helper.HandleError(err)

	for _, each := range res {
		submenu := each["submenu"].([]interface{})

		if len(_ids) == 1 {
			if _ids[0] == each["_id"].(string) {
				return toolkit.M{
					"_id":      each["_id"],
					"title":    each["title"],
					"href":     each["href"],
					"parentID": "",
				}
			}
		}

		for _, subRaw := range submenu {
			sub := subRaw.(map[string]interface{})
			submenu2 := sub["submenu"].([]interface{})

			if len(_ids) == 2 {
				if _ids[0] == each["_id"].(string) && _ids[1] == sub["_id"].(string) {
					return toolkit.M{
						"_id":      fmt.Sprintf("%s|%s", each["_id"], sub["_id"]),
						"title":    sub["title"],
						"href":     sub["href"],
						"parentID": each["_id"],
					}
				}
			}

			for _, subRaw2 := range submenu2 {
				sub2 := subRaw2.(map[string]interface{})

				if len(_ids) == 3 {
					if _ids[0] == each["_id"].(string) && _ids[1] == sub["_id"].(string) && _ids[2] == sub2["_id"].(string) {
						return toolkit.M{
							"_id":      fmt.Sprintf("%s|%s|%s", each["_id"], sub["_id"], sub2["_id"]),
							"title":    sub2["title"],
							"href":     sub2["href"],
							"parentID": sub["_id"],
						}
					}
				}
			}
		}
	}

	return toolkit.M{}
}
Пример #23
0
func (t *ChartController) SaveChartConfig(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	payload := map[string]string{}
	err := r.GetForms(&payload)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	_id := payload["_id"]
	v, err := os.Getwd()
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	if _id == "" {
		_id = helper.RandomIDWithPrefix("c")
		filename := fmt.Sprintf("chart-%s.json", _id)

		// save chart configuration
		path := fmt.Sprintf("%s/data/chart/%s", v, filename)
		err = os.Remove(path)
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}
		ioutil.WriteFile(path, []byte(payload["config"]), 0644)

		// save chart meta data
		connection, err := helper.LoadConfig(t.AppViewsPath + "/data/chart.json")
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}
		defer connection.Close()

		newData := toolkit.M{"_id": _id, "title": payload["title"], "file": filename}
		err = connection.NewQuery().Insert().Exec(toolkit.M{"data": newData})
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}
	} else {
		filename := fmt.Sprintf("chart-%s.json", _id)

		// update chart configuration
		path := fmt.Sprintf("%s/data/chart/%s", v, filename)
		err = os.Remove(path)
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}
		ioutil.WriteFile(path, []byte(payload["config"]), 0644)

		// update chart meta data
		connection, err := helper.LoadConfig(t.AppViewsPath + "/data/chart.json")
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}
		defer connection.Close()

		newData := toolkit.M{"_id": _id, "title": payload["title"], "file": filename}
		err = connection.NewQuery().Update().Where(dbox.Eq("_id", _id)).Exec(toolkit.M{"data": newData})
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}
	}

	return helper.Result(true, _id, "")
}