Exemplo n.º 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, "")
}
Exemplo n.º 2
0
func (t *DesignerController) SavePanel(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())
	}

	_id := payload["_id"].(string)
	title := payload["title"].(string)
	hide, _ := strconv.ParseBool(payload["hide"].(string))
	var width int = int(payload["width"].(float64))
	var offset int = int(payload["offset"].(float64))
	hideContainerPanel, _ := strconv.ParseBool(payload["hideContainerPanel"].(string))

	panelID := payload["panelID"].(string)

	config, err := t.getConfig(_id)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}
	contentOld := config["content"].([]interface{})

	if panelID == "" {
		panelID = helper.RandomIDWithPrefix("p")
		contentNew := map[string]interface{}{
			"panelID":            panelID,
			"title":              title,
			"width":              width,
			"offset":             offset,
			"hide":               hide,
			"hideContainerPanel": hideContainerPanel,
			"content":            []interface{}{},
		}
		config["content"] = append([]interface{}{contentNew}, contentOld...)
	} else {
		for i, eachRaw := range contentOld {
			each := eachRaw.(map[string]interface{})
			if each["panelID"] == payload["panelID"] {
				contentOld[i].(map[string]interface{})["title"] = title
				contentOld[i].(map[string]interface{})["width"] = width
				contentOld[i].(map[string]interface{})["offset"] = offset
				contentOld[i].(map[string]interface{})["hideContainerPanel"] = hideContainerPanel
			}
		}

		config["content"] = contentOld
	}

	err = t.setConfig(_id, config)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	return helper.Result(true, panelID, "")
	//~ return helper.Result(true, config, config)
	//~ return fmt.printf("%v",config)
}
Exemplo n.º 3
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, "")
}
Exemplo n.º 4
0
func (t *DesignerController) SaveWidget(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())
	}
	_id := payload["_id"].(string)
	panelWidgetID := payload["panelWidgetID"].(string)

	width := int(100)
	if _, ok := payload["width"]; ok {
		width = int(payload["width"].(float64))
	}

	height := int(400)
	if _, ok := payload["height"]; ok {
		height = int(payload["height"].(float64))
	}

	config, err := t.getConfig(_id)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}
	content := config["content"].([]interface{})
	contentNew := map[string]interface{}{
		"panelWidgetID": panelWidgetID,
		"dataSource":    payload["dataSource"].(string),
		"title":         payload["title"].(string),
		"type":          payload["type"].(string),
		"widgetID":      payload["widgetID"].(string),
		"width":         width,
		"height":        height,
	}

	if panelWidgetID == "" {
		panelWidgetID = helper.RandomIDWithPrefix("pw")
		contentNew["panelWidgetID"] = panelWidgetID

		for i, eachRaw := range content {
			each := eachRaw.(map[string]interface{})
			if each["panelID"] == payload["panelID"] {
				each["content"] = append([]interface{}{contentNew}, each["content"].([]interface{})...)
			}

			config["content"].([]interface{})[i] = each
		}

		err = t.setConfig(_id, config)
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}
	} else {
	outer:
		for i, eachRaw := range content {
			each := eachRaw.(map[string]interface{})
			if each["panelID"] == payload["panelID"] {
				for j, subRaw := range each["content"].([]interface{}) {
					sub := subRaw.(map[string]interface{})
					if sub["panelWidgetID"] == panelWidgetID {
						content[i].(map[string]interface{})["content"].([]interface{})[j] = contentNew
						break outer
					}
				}
			}
		}

		config["content"] = content
		err = t.setConfig(_id, config)
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}
	}

	return helper.Result(true, nil, "")
}
Exemplo n.º 5
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, "")
}
Exemplo n.º 6
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
}