diff --git a/management/editor/editor.go b/management/editor/editor.go index 3d4f27d8..6e54c8e7 100644 --- a/management/editor/editor.go +++ b/management/editor/editor.go @@ -150,12 +150,14 @@ func Form(post Editable, fields ...Field) ([]byte, error) { external = form.find('.post-controls.external'), id = form.find('input[name=id]'), timestamp = $('.__ponzu.content-only'), - slug = $('input[name=slug]'); + slug = $('input[name=slug]'), + allowEmptySlug = false; // hide if this is a new post, or a non-post editor page if (id.val() === '-1' || form.attr('action') !== '/admin/edit') { del.hide(); external.hide(); + allowEmptySlug = true; } // hide approval if not on a pending content item @@ -167,11 +169,17 @@ func Form(post Editable, fields ...Field) ([]byte, error) { if (form.attr('action') === '/admin/addon') { timestamp.hide(); slug.parent().hide(); + allowEmptySlug = true; } save.on('click', function(e) { e.preventDefault(); + if (!allowEmptySlug) { + alert("Slug cannot be empty"); + return; + } + if (getParam('status') === 'pending') { var action = form.attr('action'); form.attr('action', action + '?status=pending') @@ -238,7 +246,7 @@ func addPostDefaultFieldsToEditorView(p Editable, e *Editor) error { View: Input("Slug", p, map[string]string{ "label": "URL Slug", "type": "text", - "placeholder": "Can be left empty for new item", + "placeholder": "Can be left empty, only for new item", }), }, { diff --git a/system/db/content.go b/system/db/content.go index bcba4295..e57e8734 100644 --- a/system/db/content.go +++ b/system/db/content.go @@ -108,33 +108,38 @@ func update(ns, id string, data url.Values, existingContent *[]byte) (int, error return err } - // update the slug,type:id in contentIndex if public content + // get slugs data + var ( + existingSlug, newSlug string + ) + existingSlug = data.Get("slug") + if len(data["slug"]) > 1 { + newSlug = data["slug"][1] + } + + // update the slug (type:id) in contentIndex if public content if specifier == "" { - if len(data["slug"]) > 1 { - slug := data["slug"][0] - customSlug := data["slug"][1] - - target := fmt.Sprintf("%s:%d", ns, cid) - // if slug changed - if slug != customSlug { - ci := tx.Bucket([]byte("__contentIndex")) - if ci == nil { - return bolt.ErrBucketNotFound - } - // remove existing slug from __contentIndex - err = ci.Delete([]byte(fmt.Sprintf("%s", slug))) - if err != nil { - return err - } + target := fmt.Sprintf("%s:%d", ns, cid) + // if slug changed and valid + if existingSlug != newSlug && newSlug != "" { + ci := tx.Bucket([]byte("__contentIndex")) + if ci == nil { + return bolt.ErrBucketNotFound + } - // insert new slug to __contentIndex - k := []byte(customSlug) - v := []byte(target) - err := ci.Put(k, v) - if err != nil { - return err - } + // remove existing slug from __contentIndex + err = ci.Delete([]byte(fmt.Sprintf("%s", existingSlug))) + if err != nil { + return err + } + + // insert new slug to __contentIndex + k := []byte(newSlug) + v := []byte(target) + err := ci.Put(k, v) + if err != nil { + return err } } } @@ -801,20 +806,22 @@ func postToJSON(ns string, data url.Values) ([]byte, error) { return nil, err } + // get slugs data + var ( + existingSlug, newSlug string + ) + existingSlug = data.Get("slug") + if len(data["slug"]) > 1 { + newSlug = data["slug"][1] + } + + // for new item // if the content has no slug, and has no specifier, create a slug, check it // for duplicates, and add it to our values - if data.Get("slug") == "" && data.Get("__specifier") == "" { - // check custom slug - slugsData := data["slug"] - if len(slugsData) > 1 && data["slug"][1] != "" { - customSlug := data["slug"][1] - - if customSlug != "" { - post.(item.Sluggable).SetSlug(customSlug) - data.Set("slug", customSlug) - } - } else { - // generate default slug + if existingSlug == "" && data.Get("__specifier") == "" { + + // if no new slug specified, generate slug + if newSlug == "" { slug, err := item.Slug(post.(item.Identifiable)) if err != nil { return nil, err @@ -827,9 +834,19 @@ func postToJSON(ns string, data url.Values) ([]byte, error) { post.(item.Sluggable).SetSlug(slug) data.Set("slug", slug) + } else { + post.(item.Sluggable).SetSlug(newSlug) + data.Set("slug", newSlug) } } + // for editing item + // prevent edit to empty slug + if existingSlug != "" && newSlug == "" { + post.(item.Sluggable).SetSlug(existingSlug) + data.Set("slug", existingSlug) + } + // marshall content struct to json for db storage j, err := json.Marshal(post) if err != nil {