// Generate a BaseHRef-relative link to this page func (d *DataObjectBase) Link(args ...string) string { hier := orm.IsHierarchical(d.ClassName) if !hier { return "" } obj := interface{}(d) res := d.URLSegment // @todo data.Eval.(int) may fail for a map where the ParentID may have type of string for parentID := data.Eval(obj, "ParentID").(int); parentID > 0; { // @todo don't hardcode "SiteTree", derive the base class using metadata. q := orm.NewQuery("SiteTree").Where("\"SiteTree_Live\".\"ID\"=" + strconv.Itoa(parentID)) ds, e := q.Run() if e != nil { return "" } items, _ := ds.(orm.DataList).Items() obj = items[0] res = data.Eval(obj, "URLSegment").(string) + "/" + res } for _, a := range args { res += "/" + a } if res == "home" { res = "/" } return res }
// Handle a request for a general page out of site tree: // - pull apart the path, and use it to guide the location of a site tree record // from the SS DB, matching URL segments exactly // - if there is no matching page, find an error page instead // - with the page in sitetree located, use ClassName to determine the controller that should be invoked. // - grab the data object and render the template with it. func SiteTreeHandler(w http.ResponseWriter, r *http.Request) { pageID, e := findPageToRender(r) if e != nil { ErrorHandler(w, e) return } if pageID == 0 { pageID, e = findNotFoundPage(r) } if e != nil { ErrorHandler(w, e) return } if pageID == 0 { // uh oh, couldn't find anything we could render off in site tree e = errors.New("Could not find anything to render at all") ErrorHandler(w, e) return } // fmt.Printf("SiteTreeHandler has found a page: %d\n", pageID) siteCache := getSiteCache() page := siteCache.GetCacheByID(pageID) if page == nil { q := orm.NewQuery("SiteTree").Where("\"SiteTree_Live\".\"ID\"=" + strconv.Itoa(pageID)) v, _ := q.Run() if e != nil { ErrorHandler(w, e) return } res := v.(orm.DataList) items, e := res.Items() if e != nil { ErrorHandler(w, e) } if len(items) == 0 { e = errors.New("Could not locate object with ID " + strconv.Itoa(pageID)) ErrorHandler(w, e) return } page = items[0] siteCache.CacheDataObject(pageID, page) } renderWithMatchedController(w, r, page) }
// Return the SiteConfig DataObject. func (ctl *BaseController) SiteConfig() (obj interface{}, e error) { v := cache.Get("goss.SiteConfig") if v != nil { return v, nil } q := orm.NewQuery("SiteConfig").Limit(0, 1) res, e := q.Run() if e != nil { return nil, e } items, _ := res.(orm.DataList).Items() if len(items) < 1 { return nil, errors.New("There is no SiteConfig record") } if configuration.cacheSiteConfigTTL > 0 { cache.Store("goss.SiteConfig", items[0], time.Duration(configuration.cacheSiteConfigTTL)*time.Second) } return items[0], nil }
func (ctl *BaseController) Menu(level int) (orm.DataList, error) { key := "goss.Menu." + strconv.Itoa(level) result := cache.Get(key) if result != nil { return result.(orm.DataList), nil } if level == 1 { q := orm.NewQuery("SiteTree").Where("\"SiteTree_Live\".\"ParentID\"=0").Where("\"ShowInMenus\"=1").Sort("\"Sort\" ASC") v, e := q.Run() if e != nil { return nil, e } if configuration.cacheMenuTTL > 0 { cache.Store(key, v, time.Duration(configuration.cacheMenuTTL)*time.Second) } return v.(orm.DataList), nil } return nil, nil }