Check-in by ben on 2024-10-06 21:49:20

Add option to change user list sort order

 INSERTED    DELETED
        1          0 make.sh
pharos/listsort/index.dcgi
       34          3 src/list/index.dcgi.m4
       54          0 src/listsort/index.dcgi.m4
       89          3 TOTAL over 3 changed files

Index: make.sh
==================================================================
--- make.sh
+++ make.sh
@@ -18,10 +18,11 @@
       images/index.gph        \
       details/index.dcgi      \
       download/index.dcgi     \
       list/index.dcgi         \
       lists/index.dcgi        \
+       listsort/index.dcgi     \
        raw/index.cgi           \
       search/index.dcgi       \
       software/index.gph      \
       sort/index.dcgi         \
       video/index.gph         \

ADDED   pharos/listsort/index.dcgi
Index: pharos/listsort/index.dcgi
==================================================================
--- /dev/null
+++ pharos/listsort/index.dcgi

Index: src/list/index.dcgi.m4
==================================================================
--- src/list/index.dcgi.m4
+++ src/list/index.dcgi.m4
@@ -10,21 +10,32 @@
incl(src/cgi.awk)
incl(src/util.awk)

function main(     acct, client_url, cmd, count, creator, iaout, id,
    is_private, items, label, list_id, name, name_slug, numfound,
-    page, pages, rows, query, title, type, url)
+    order, order_name, order_names, order_param, page, pages, rows,
+    query, title, type, url)
{
+    order_names["creatorSorter"] = creator
+    order_names["date"] = "date"
+    order_names["titleSorter"] = "title"
+    order_names["week"] = "week"
+
    rows = 15
    page = 1
+    order = ""

-    # parse out page number
+    # parse out page number and sort order
    for (i in parts) {
        if (parts[i] ~ /^rows[0-9][0-9]*$/) {
            rows = substr(parts[i], 5)
        } else if (parts[i] ~ /^page[0-9][0-9]*$/) {
            page = substr(parts[i], 5)
+        } else if (parts[i] ~ /^sort/) {
+            if (length(order) == 0) {
+                order = substr(parts[i], 5)
+            }
        }
    }

    split(search, parts, "/")
    acct = parts[1]
@@ -75,14 +86,28 @@

    name_slug = uri_encode(name)
    gsub(/%20/, "-", name_slug)
    client_url = api_ssl_endpoint "/details/" acct "/lists/" list_id \
        "/" name_slug
+    order_param = ""
+    if (length(order) > 0) {
+        split(order, parts, ":")
+        order_name = order_names[parts[1]]
+        if (length(order_name) > 0) {
+            if (parts[2] == "desc") {
+                client_url = client_url "?-" order_name
+            } else {
+                client_url = client_url "?" order_name
+            }
+            order_param = "&sort=" uri_encode(order)
+        }
+    }
    url = api_ssl_endpoint "/services/search/beta/page_production/" \
        "?user_query=identifier:(" query ")"                        \
        "&hits_per_page=" rows                                      \
        "&page=" page                                               \
+        order_param                                                 \
        "&aggregations=false"                                       \
        "&client_url=" client_url
    api_request(url, "GET", iaout)

    pages = int(numfound / rows)
@@ -117,11 +142,11 @@
            type = $3
        } else if ($1 == ".response.body.hits.hits[].fields.title" &&
            $2 == "s")
        {
            title = $3
-        } else if ($1 == ".response.body.hits.hits[]._score" && $2 == "a") {
+        } else if ($1 == ".response.body.hits.hits[]._score") {
            # the _score field happens to be toward the end of each item
            if (length(title) > 0) {
                if (length(creator) > 0) {
                    label = sprintf("[%s] %s by %s", mediatype[type], \
                        gph_encode(shorten(title, 40)), shorten(creator, 18))
@@ -154,10 +179,16 @@
    if (count == rows) {
        printf "[1|[>>] Page %d|%s/list/page%d/rows%d/%%09%s/%d|%s|%s]\n",
            page + 1, cgipath, page + 1, rows,
            acct, list_id, server, port
    }
+
+    # only show "sort" if there's more than one item to sort
+    if (numfound > 1) {
+        printf "[1|[^v] Sort|%s/listsort/%%09%s/%d|%s|%s]\n", cgipath,
+            acct, list_id, server, port
+    }

    printf "[1|Account %s|%s/account/%s|%s|%s]\n", acct, cgipath,
        acct, server, port

    print ""

ADDED   src/listsort/index.dcgi.m4
Index: src/listsort/index.dcgi.m4
==================================================================
--- /dev/null
+++ src/listsort/index.dcgi.m4
@@ -0,0 +1,54 @@
+include(config.m4)dnl
+#!__CMD_AWK__ -f
+
+# listsort/index.dcgi
+#
+# Change list sort order
+
+include(src/config.awk)
+incl(src/cgi.awk)
+
+function main(     acct, i, lbl, list_id, opt) {
+    lbl[1] = "Relevance"
+    opt[1] = ""
+    lbl[2] = "Weekly views [^]"
+    opt[2] = "week:asc"
+    lbl[3] = "Weekly views [v]"
+    opt[3] = "week:desc"
+    lbl[4] = "Title [^]"
+    opt[4] = "titleSorter:asc"
+    lbl[5] = "Title [v]"
+    opt[5] = "titleSorter:desc"
+    lbl[6] = "Date published [^]"
+    opt[6] = "date:asc"
+    lbl[7] = "Date published [v]"
+    opt[7] = "date:desc"
+    lbl[8] = "Creator [^]"
+    opt[8] = "creatorSorter:asc"
+    lbl[9] = "Creator [v]"
+    opt[9] = "creatorSorter:desc"
+
+    split(search, parts, "/")
+    acct = parts[1]
+    list_id = parts[2]
+
+    print "# Sort by"
+    print ""
+    for (i = 1; i < 10; i++) {
+        if (length(opt[i]) == 0) {
+            printf "[1|%s|%s/list/%%09%s/%d|%s|%s]\n",
+                lbl[i], cgipath, acct, list_id, server, port
+        } else {
+            printf "[1|%s|%s/list/sort%s%%09%s/%d|%s|%s]\n",
+                lbl[i], cgipath, opt[i], acct, list_id, server, port
+        }
+    }
+    exit 0
+}
+
+BEGIN {
+    config_init()
+
+    cgi_init()
+    main()
+}