Exemple #1
0
func (db *publishController) Preview(context *admin.Context) {
	type resource struct {
		*admin.Resource
		Value interface{}
	}

	var drafts = []resource{}

	draftDB := context.GetDB().Set(publishDraftMode, true).Unscoped()
	for _, res := range context.Admin.GetResources() {
		if visibleInterface, ok := res.Value.(visiblePublishResourceInterface); ok {
			if !visibleInterface.VisiblePublishResource() {
				continue
			}
		} else if res.Config.Invisible {
			continue
		}

		results := res.NewSlice()
		if IsPublishableModel(res.Value) || IsPublishEvent(res.Value) {
			if draftDB.Unscoped().Where("publish_status = ?", DIRTY).Find(results).RowsAffected > 0 {
				drafts = append(drafts, resource{
					Resource: res,
					Value:    results,
				})
			}
		}
	}
	context.Execute("publish_drafts", drafts)
}
Exemple #2
0
func (db *publishController) PublishOrDiscard(context *admin.Context) {
	var request = context.Request
	var ids = request.Form["checked_ids[]"]
	var records = []interface{}{}
	var values = map[string][]string{}

	for _, id := range ids {
		if keys := strings.Split(id, "__"); len(keys) == 2 {
			name, id := keys[0], keys[1]
			values[name] = append(values[name], id)
		}
	}

	draftDB := context.GetDB().Set("publish:draft_mode", true).Unscoped()
	for name, value := range values {
		res := context.Admin.GetResource(name)
		results := res.NewSlice()
		if draftDB.Find(results, fmt.Sprintf("%v IN (?)", res.PrimaryDBName()), value).Error == nil {
			resultValues := reflect.Indirect(reflect.ValueOf(results))
			for i := 0; i < resultValues.Len(); i++ {
				records = append(records, resultValues.Index(i).Interface())
			}
		}
	}

	if request.Form.Get("publish_type") == "publish" {
		Publish{DB: draftDB}.Publish(records...)
	} else if request.Form.Get("publish_type") == "discard" {
		Publish{DB: draftDB}.Discard(records...)
	}
	http.Redirect(context.Writer, context.Request, context.Request.RequestURI, http.StatusFound)
}
Exemple #3
0
func (db *publishController) Preview(context *admin.Context) {
	drafts := make(map[*admin.Resource]interface{})
	draftDB := context.GetDB().Set("publish:draft_mode", true).Unscoped()
	for _, res := range context.Admin.GetResources() {
		results := res.NewSlice()
		if isPublishableModel(res.Value) {
			if draftDB.Where("publish_status = ?", DIRTY).Find(results).RowsAffected > 0 {
				drafts[res] = results
			}
		}
	}
	context.Execute("publish/drafts", drafts)
}
Exemple #4
0
func updatePosition(context *admin.Context) {
	if result, err := context.FindOne(); err == nil {
		if position, ok := result.(sortingInterface); ok {
			if pos, err := strconv.Atoi(context.Request.Form.Get("to")); err == nil {
				var count int
				if _, ok := result.(sortingDescInterface); ok {
					context.GetDB().New().Model(modelValue(result)).Count(&count)
					pos = count - pos + 1
				}

				if MoveTo(context.GetDB(), position, pos) == nil {
					var pos = position.GetPosition()
					if _, ok := result.(sortingDescInterface); ok {
						pos = count - pos + 1
					}

					context.Writer.Write([]byte(fmt.Sprintf("%d", pos)))
					return
				}
			}
		}
	}
	context.Writer.Write([]byte("Error"))
	context.Writer.WriteHeader(422)
}
Exemple #5
0
func updatePosition(context *admin.Context) {
	if result, err := context.FindOne(); err == nil {
		if position, ok := result.(sortingInterface); ok {
			if pos, err := strconv.Atoi(context.Request.Form.Get("to")); err == nil {
				var count int
				if _, ok := result.(sortingDescInterface); ok {
					var result = context.Resource.NewStruct()
					context.GetDB().Set("l10n:mode", "locale").Order("position DESC", true).First(result)
					count = result.(sortingInterface).GetPosition()
					pos = count - pos + 1
				}

				if MoveTo(context.GetDB(), position, pos) == nil {
					var pos = position.GetPosition()
					if _, ok := result.(sortingDescInterface); ok {
						pos = count - pos + 1
					}

					context.Writer.Write([]byte(fmt.Sprintf("%d", pos)))
					return
				}
			}
		}
	}
	context.Writer.WriteHeader(admin.HTTPUnprocessableEntity)
	context.Writer.Write([]byte("Error"))
}
Exemple #6
0
func (db *publishController) Diff(context *admin.Context) {
	resourceID := strings.Split(context.Request.URL.Path, "/")[4]
	params := strings.Split(resourceID, "__")
	name, id := params[0], params[1]
	res := context.Admin.GetResource(name)

	draft := res.NewStruct()
	context.GetDB().Set("publish:draft_mode", true).Unscoped().First(draft, id)

	production := res.NewStruct()
	context.GetDB().Set("publish:draft_mode", false).Unscoped().First(production, id)

	results := map[string]interface{}{"Production": production, "Draft": draft, "Resource": res}

	fmt.Fprintf(context.Writer, string(context.Render("publish/diff", results)))
}
Exemple #7
0
func (controller *i18nController) Index(context *admin.Context) {
	context.Execute("index", controller.I18n)
}