Files
amsterdam/find.go
T

140 lines
3.6 KiB
Go

/*
* Amsterdam Web Communities System
* Copyright (c) 2025 Erbosoft Metaverse Design Solutions, All Rights Reserved
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
// Package main contains the high-level Amsterdam logic.
package main
import (
"fmt"
"strconv"
"git.erbosoft.com/amy/amsterdam/database"
"git.erbosoft.com/amy/amsterdam/ui"
)
// loadCategoryInformation loads the current category information to the context.
func loadCategoryInformation(ctxt ui.AmContext, offset int) error {
u := ctxt.CurrentUser()
catid := int32(-1)
p := ctxt.Parameter("catid")
if p != "" {
v, err := strconv.Atoi(p)
if err != nil {
return err
}
catid = int32(v)
} else if ctxt.IsSession("find.catid") {
x := ctxt.GetSession("find.catid")
if xx, ok := x.(int32); ok {
catid = xx
}
}
if catid > -1 {
cat, err := database.AmGetCategory(catid) // this step also resolves symlinks
if err != nil {
return err
}
catid = cat.CatId
}
ctxt.SetSession("find.catid", catid)
ctxt.VarMap().Set("catid", catid)
showHidden := database.AmTestPermission("Global.ShowHiddenCategories", u.BaseLevel)
ctxt.VarMap().Set("showHiddenCat", showHidden)
hier, err := database.AmGetCategoryHierarchy(catid)
if err != nil {
return err
}
ctxt.VarMap().Set("catHierarchy", hier)
subs, err := database.AmGetSubCategories(catid)
if err != nil {
return err
}
ctxt.VarMap().Set("catSubs", subs)
ctxt.VarMap().Set("displayCats", true)
if catid > -1 {
// search for communities in this category
listMax := int(ctxt.Globals().MaxSearchPage)
commList, numComm, err := database.AmGetCommunitiesForCategory(catid, offset*listMax, listMax, showHidden)
if err != nil {
return err
}
if len(commList) == 0 {
ctxt.VarMap().Set("resultHeader", "Communities in Category (None)")
} else {
ctxt.VarMap().Set("resultHeader", fmt.Sprintf("Communities in Category (Displaying %d-%d of %d)",
offset*listMax+1, offset*listMax+len(commList), numComm))
ctxt.VarMap().Set("resultList", commList)
if offset > 0 {
ctxt.VarMap().Set("resultShowPrev", true)
}
if offset*listMax+len(commList) < numComm {
ctxt.VarMap().Set("resultShowNext", true)
}
}
}
return nil
}
/* FindPage renders the Amsterdam "Find" page.
* Parameters:
* ctxt - The AmContext for the request.
* Returns:
* Command string dictating what to be rendered.
* Data as a parameter for the command string.
* Standard Go error status.
*/
func FindPage(ctxt ui.AmContext) (string, any, error) {
mode := ""
p := ctxt.Parameter("mode")
if p != "" {
mode = p
} else if ctxt.IsSession("find.mode") {
x := ctxt.GetSession("find.mode")
if xx, ok := x.(string); ok {
mode = xx
}
}
if mode == "" {
mode = "COM"
}
ofs := 0
p = ctxt.Parameter("ofs")
if p != "" {
v, err := strconv.Atoi(p)
if err == nil {
ofs = v
}
}
ctxt.SetSession("find.mode", mode)
ctxt.VarMap().Set("mode", mode)
switch mode {
case "COM":
ctxt.VarMap().Set("field", "name")
ctxt.VarMap().Set("oper", "st")
ctxt.VarMap().Set("term", "")
err := loadCategoryInformation(ctxt, ofs)
if err != nil {
return ui.ErrorPage(ctxt, err)
}
case "USR":
ctxt.VarMap().Set("field", "name")
ctxt.VarMap().Set("oper", "st")
ctxt.VarMap().Set("term", "")
case "CAT":
ctxt.VarMap().Set("oper", "st")
ctxt.VarMap().Set("term", "")
case "PST":
ctxt.VarMap().Set("term", "")
}
ctxt.VarMap().Set("amsterdam_pageTitle", "Find")
ctxt.SetLeftMenu("top")
return "framed_template", "find.jet", nil
}