Add seasons’ relation, functions, and admin section
Seasons have a color to show on the calendar. I need them in HTML format
(e.g., #123abc) in order to set as value to `<input type="color">`, but
i did not want to save them as text in the database, as integers are
better representations of colors—in fact, that’s what the HTML syntax
also is: an integer.
I think the best would be to create an extension that adds an HTML color
type, with functions to convert from many representations (e.g., CSS’
rgb or even color names) to integer and back. However, that’s a lot of
work and i can satisfy Camper’s needs with just a couple of functions
and a domain.
To show the color on the index, at first tried to use a read-only
`<input type="color">`, but seems that this type of input can not be
read-only and must be disabled instead. However, i do not know whether
it makes sense to have a disabled input outside a form “just” to show
a color; i suspect it does not. Thus, at the end i use SVG with a
single circle, which is better that a 50%-rounded div with a background
color, even if the result is the same—SVG **is** intended for showing
pictures, which is this case.
2023-08-16 18:15:57 +00:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2023 jordi fita mas <jfita@peritasoft.com>
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
|
|
|
package season
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-09-27 12:21:27 +00:00
|
|
|
"fmt"
|
Add seasons’ relation, functions, and admin section
Seasons have a color to show on the calendar. I need them in HTML format
(e.g., #123abc) in order to set as value to `<input type="color">`, but
i did not want to save them as text in the database, as integers are
better representations of colors—in fact, that’s what the HTML syntax
also is: an integer.
I think the best would be to create an extension that adds an HTML color
type, with functions to convert from many representations (e.g., CSS’
rgb or even color names) to integer and back. However, that’s a lot of
work and i can satisfy Camper’s needs with just a couple of functions
and a domain.
To show the color on the index, at first tried to use a read-only
`<input type="color">`, but seems that this type of input can not be
read-only and must be disabled instead. However, i do not know whether
it makes sense to have a disabled input outside a form “just” to show
a color; i suspect it does not. Thus, at the end i use SVG with a
single circle, which is better that a 50%-rounded div with a background
color, even if the result is the same—SVG **is** intended for showing
pictures, which is this case.
2023-08-16 18:15:57 +00:00
|
|
|
"net/http"
|
2023-09-29 16:20:16 +00:00
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2023-09-27 00:23:09 +00:00
|
|
|
"time"
|
Add seasons’ relation, functions, and admin section
Seasons have a color to show on the calendar. I need them in HTML format
(e.g., #123abc) in order to set as value to `<input type="color">`, but
i did not want to save them as text in the database, as integers are
better representations of colors—in fact, that’s what the HTML syntax
also is: an integer.
I think the best would be to create an extension that adds an HTML color
type, with functions to convert from many representations (e.g., CSS’
rgb or even color names) to integer and back. However, that’s a lot of
work and i can satisfy Camper’s needs with just a couple of functions
and a domain.
To show the color on the index, at first tried to use a read-only
`<input type="color">`, but seems that this type of input can not be
read-only and must be disabled instead. However, i do not know whether
it makes sense to have a disabled input outside a form “just” to show
a color; i suspect it does not. Thus, at the end i use SVG with a
single circle, which is better that a 50%-rounded div with a background
color, even if the result is the same—SVG **is** intended for showing
pictures, which is this case.
2023-08-16 18:15:57 +00:00
|
|
|
|
2023-09-27 12:21:27 +00:00
|
|
|
"github.com/jackc/pgtype"
|
2023-10-03 19:14:37 +00:00
|
|
|
"github.com/jackc/pgx/v4"
|
2023-09-27 12:21:27 +00:00
|
|
|
|
Add seasons’ relation, functions, and admin section
Seasons have a color to show on the calendar. I need them in HTML format
(e.g., #123abc) in order to set as value to `<input type="color">`, but
i did not want to save them as text in the database, as integers are
better representations of colors—in fact, that’s what the HTML syntax
also is: an integer.
I think the best would be to create an extension that adds an HTML color
type, with functions to convert from many representations (e.g., CSS’
rgb or even color names) to integer and back. However, that’s a lot of
work and i can satisfy Camper’s needs with just a couple of functions
and a domain.
To show the color on the index, at first tried to use a read-only
`<input type="color">`, but seems that this type of input can not be
read-only and must be disabled instead. However, i do not know whether
it makes sense to have a disabled input outside a form “just” to show
a color; i suspect it does not. Thus, at the end i use SVG with a
single circle, which is better that a 50%-rounded div with a background
color, even if the result is the same—SVG **is** intended for showing
pictures, which is this case.
2023-08-16 18:15:57 +00:00
|
|
|
"dev.tandem.ws/tandem/camper/pkg/auth"
|
|
|
|
"dev.tandem.ws/tandem/camper/pkg/database"
|
|
|
|
"dev.tandem.ws/tandem/camper/pkg/form"
|
|
|
|
httplib "dev.tandem.ws/tandem/camper/pkg/http"
|
|
|
|
"dev.tandem.ws/tandem/camper/pkg/locale"
|
|
|
|
"dev.tandem.ws/tandem/camper/pkg/template"
|
|
|
|
"dev.tandem.ws/tandem/camper/pkg/uuid"
|
|
|
|
)
|
|
|
|
|
2023-10-14 21:14:23 +00:00
|
|
|
const UnsetColor = 0xd1d0df
|
2023-09-27 12:21:27 +00:00
|
|
|
|
Add seasons’ relation, functions, and admin section
Seasons have a color to show on the calendar. I need them in HTML format
(e.g., #123abc) in order to set as value to `<input type="color">`, but
i did not want to save them as text in the database, as integers are
better representations of colors—in fact, that’s what the HTML syntax
also is: an integer.
I think the best would be to create an extension that adds an HTML color
type, with functions to convert from many representations (e.g., CSS’
rgb or even color names) to integer and back. However, that’s a lot of
work and i can satisfy Camper’s needs with just a couple of functions
and a domain.
To show the color on the index, at first tried to use a read-only
`<input type="color">`, but seems that this type of input can not be
read-only and must be disabled instead. However, i do not know whether
it makes sense to have a disabled input outside a form “just” to show
a color; i suspect it does not. Thus, at the end i use SVG with a
single circle, which is better that a 50%-rounded div with a background
color, even if the result is the same—SVG **is** intended for showing
pictures, which is this case.
2023-08-16 18:15:57 +00:00
|
|
|
type AdminHandler struct {
|
2023-10-03 19:14:37 +00:00
|
|
|
locales locale.Locales
|
Add seasons’ relation, functions, and admin section
Seasons have a color to show on the calendar. I need them in HTML format
(e.g., #123abc) in order to set as value to `<input type="color">`, but
i did not want to save them as text in the database, as integers are
better representations of colors—in fact, that’s what the HTML syntax
also is: an integer.
I think the best would be to create an extension that adds an HTML color
type, with functions to convert from many representations (e.g., CSS’
rgb or even color names) to integer and back. However, that’s a lot of
work and i can satisfy Camper’s needs with just a couple of functions
and a domain.
To show the color on the index, at first tried to use a read-only
`<input type="color">`, but seems that this type of input can not be
read-only and must be disabled instead. However, i do not know whether
it makes sense to have a disabled input outside a form “just” to show
a color; i suspect it does not. Thus, at the end i use SVG with a
single circle, which is better that a 50%-rounded div with a background
color, even if the result is the same—SVG **is** intended for showing
pictures, which is this case.
2023-08-16 18:15:57 +00:00
|
|
|
}
|
|
|
|
|
2023-10-03 19:14:37 +00:00
|
|
|
func NewAdminHandler(locales locale.Locales) *AdminHandler {
|
|
|
|
return &AdminHandler{
|
|
|
|
locales: locales,
|
|
|
|
}
|
Add seasons’ relation, functions, and admin section
Seasons have a color to show on the calendar. I need them in HTML format
(e.g., #123abc) in order to set as value to `<input type="color">`, but
i did not want to save them as text in the database, as integers are
better representations of colors—in fact, that’s what the HTML syntax
also is: an integer.
I think the best would be to create an extension that adds an HTML color
type, with functions to convert from many representations (e.g., CSS’
rgb or even color names) to integer and back. However, that’s a lot of
work and i can satisfy Camper’s needs with just a couple of functions
and a domain.
To show the color on the index, at first tried to use a read-only
`<input type="color">`, but seems that this type of input can not be
read-only and must be disabled instead. However, i do not know whether
it makes sense to have a disabled input outside a form “just” to show
a color; i suspect it does not. Thus, at the end i use SVG with a
single circle, which is better that a 50%-rounded div with a background
color, even if the result is the same—SVG **is** intended for showing
pictures, which is this case.
2023-08-16 18:15:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *AdminHandler) Handler(user *auth.User, company *auth.Company, conn *database.Conn) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var head string
|
|
|
|
head, r.URL.Path = httplib.ShiftPath(r.URL.Path)
|
|
|
|
|
|
|
|
switch head {
|
2023-09-27 12:21:27 +00:00
|
|
|
case "":
|
|
|
|
switch r.Method {
|
|
|
|
case http.MethodGet:
|
|
|
|
serveSeasonIndex(w, r, user, company, conn)
|
|
|
|
case http.MethodPost:
|
|
|
|
addSeason(w, r, user, company, conn)
|
|
|
|
default:
|
|
|
|
httplib.MethodNotAllowed(w, r, http.MethodGet, http.MethodPost)
|
|
|
|
}
|
Add seasons’ relation, functions, and admin section
Seasons have a color to show on the calendar. I need them in HTML format
(e.g., #123abc) in order to set as value to `<input type="color">`, but
i did not want to save them as text in the database, as integers are
better representations of colors—in fact, that’s what the HTML syntax
also is: an integer.
I think the best would be to create an extension that adds an HTML color
type, with functions to convert from many representations (e.g., CSS’
rgb or even color names) to integer and back. However, that’s a lot of
work and i can satisfy Camper’s needs with just a couple of functions
and a domain.
To show the color on the index, at first tried to use a read-only
`<input type="color">`, but seems that this type of input can not be
read-only and must be disabled instead. However, i do not know whether
it makes sense to have a disabled input outside a form “just” to show
a color; i suspect it does not. Thus, at the end i use SVG with a
single circle, which is better that a 50%-rounded div with a background
color, even if the result is the same—SVG **is** intended for showing
pictures, which is this case.
2023-08-16 18:15:57 +00:00
|
|
|
case "new":
|
|
|
|
switch r.Method {
|
|
|
|
case http.MethodGet:
|
2024-01-12 19:26:45 +00:00
|
|
|
f := newSeasonForm(company)
|
Add seasons’ relation, functions, and admin section
Seasons have a color to show on the calendar. I need them in HTML format
(e.g., #123abc) in order to set as value to `<input type="color">`, but
i did not want to save them as text in the database, as integers are
better representations of colors—in fact, that’s what the HTML syntax
also is: an integer.
I think the best would be to create an extension that adds an HTML color
type, with functions to convert from many representations (e.g., CSS’
rgb or even color names) to integer and back. However, that’s a lot of
work and i can satisfy Camper’s needs with just a couple of functions
and a domain.
To show the color on the index, at first tried to use a read-only
`<input type="color">`, but seems that this type of input can not be
read-only and must be disabled instead. However, i do not know whether
it makes sense to have a disabled input outside a form “just” to show
a color; i suspect it does not. Thus, at the end i use SVG with a
single circle, which is better that a 50%-rounded div with a background
color, even if the result is the same—SVG **is** intended for showing
pictures, which is this case.
2023-08-16 18:15:57 +00:00
|
|
|
f.MustRender(w, r, user, company)
|
|
|
|
default:
|
|
|
|
httplib.MethodNotAllowed(w, r, http.MethodGet)
|
|
|
|
}
|
2023-12-20 18:52:14 +00:00
|
|
|
case "order":
|
|
|
|
switch r.Method {
|
|
|
|
case http.MethodPost:
|
|
|
|
orderSeasons(w, r, user, company, conn)
|
|
|
|
default:
|
|
|
|
httplib.MethodNotAllowed(w, r, http.MethodPost)
|
|
|
|
}
|
2023-09-27 12:21:27 +00:00
|
|
|
case "range":
|
Add seasons’ relation, functions, and admin section
Seasons have a color to show on the calendar. I need them in HTML format
(e.g., #123abc) in order to set as value to `<input type="color">`, but
i did not want to save them as text in the database, as integers are
better representations of colors—in fact, that’s what the HTML syntax
also is: an integer.
I think the best would be to create an extension that adds an HTML color
type, with functions to convert from many representations (e.g., CSS’
rgb or even color names) to integer and back. However, that’s a lot of
work and i can satisfy Camper’s needs with just a couple of functions
and a domain.
To show the color on the index, at first tried to use a read-only
`<input type="color">`, but seems that this type of input can not be
read-only and must be disabled instead. However, i do not know whether
it makes sense to have a disabled input outside a form “just” to show
a color; i suspect it does not. Thus, at the end i use SVG with a
single circle, which is better that a 50%-rounded div with a background
color, even if the result is the same—SVG **is** intended for showing
pictures, which is this case.
2023-08-16 18:15:57 +00:00
|
|
|
switch r.Method {
|
2023-09-29 16:20:16 +00:00
|
|
|
case http.MethodGet:
|
|
|
|
serveSeasonCalendar(w, r, user, company, conn)
|
2023-09-27 12:21:27 +00:00
|
|
|
case http.MethodPut:
|
|
|
|
updateSeasonCalendar(w, r, user, company, conn)
|
Add seasons’ relation, functions, and admin section
Seasons have a color to show on the calendar. I need them in HTML format
(e.g., #123abc) in order to set as value to `<input type="color">`, but
i did not want to save them as text in the database, as integers are
better representations of colors—in fact, that’s what the HTML syntax
also is: an integer.
I think the best would be to create an extension that adds an HTML color
type, with functions to convert from many representations (e.g., CSS’
rgb or even color names) to integer and back. However, that’s a lot of
work and i can satisfy Camper’s needs with just a couple of functions
and a domain.
To show the color on the index, at first tried to use a read-only
`<input type="color">`, but seems that this type of input can not be
read-only and must be disabled instead. However, i do not know whether
it makes sense to have a disabled input outside a form “just” to show
a color; i suspect it does not. Thus, at the end i use SVG with a
single circle, which is better that a 50%-rounded div with a background
color, even if the result is the same—SVG **is** intended for showing
pictures, which is this case.
2023-08-16 18:15:57 +00:00
|
|
|
default:
|
2023-09-29 16:20:16 +00:00
|
|
|
httplib.MethodNotAllowed(w, r, http.MethodGet, http.MethodPut)
|
Add seasons’ relation, functions, and admin section
Seasons have a color to show on the calendar. I need them in HTML format
(e.g., #123abc) in order to set as value to `<input type="color">`, but
i did not want to save them as text in the database, as integers are
better representations of colors—in fact, that’s what the HTML syntax
also is: an integer.
I think the best would be to create an extension that adds an HTML color
type, with functions to convert from many representations (e.g., CSS’
rgb or even color names) to integer and back. However, that’s a lot of
work and i can satisfy Camper’s needs with just a couple of functions
and a domain.
To show the color on the index, at first tried to use a read-only
`<input type="color">`, but seems that this type of input can not be
read-only and must be disabled instead. However, i do not know whether
it makes sense to have a disabled input outside a form “just” to show
a color; i suspect it does not. Thus, at the end i use SVG with a
single circle, which is better that a 50%-rounded div with a background
color, even if the result is the same—SVG **is** intended for showing
pictures, which is this case.
2023-08-16 18:15:57 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
if !uuid.Valid(head) {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
2024-01-12 19:26:45 +00:00
|
|
|
f := newSeasonForm(company)
|
Add seasons’ relation, functions, and admin section
Seasons have a color to show on the calendar. I need them in HTML format
(e.g., #123abc) in order to set as value to `<input type="color">`, but
i did not want to save them as text in the database, as integers are
better representations of colors—in fact, that’s what the HTML syntax
also is: an integer.
I think the best would be to create an extension that adds an HTML color
type, with functions to convert from many representations (e.g., CSS’
rgb or even color names) to integer and back. However, that’s a lot of
work and i can satisfy Camper’s needs with just a couple of functions
and a domain.
To show the color on the index, at first tried to use a read-only
`<input type="color">`, but seems that this type of input can not be
read-only and must be disabled instead. However, i do not know whether
it makes sense to have a disabled input outside a form “just” to show
a color; i suspect it does not. Thus, at the end i use SVG with a
single circle, which is better that a 50%-rounded div with a background
color, even if the result is the same—SVG **is** intended for showing
pictures, which is this case.
2023-08-16 18:15:57 +00:00
|
|
|
if err := f.FillFromDatabase(r.Context(), conn, head); err != nil {
|
|
|
|
if database.ErrorIsNotFound(err) {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
panic(err)
|
|
|
|
}
|
2023-10-03 19:14:37 +00:00
|
|
|
|
2024-01-12 19:26:45 +00:00
|
|
|
head, r.URL.Path = httplib.ShiftPath(r.URL.Path)
|
|
|
|
switch head {
|
2023-10-03 19:14:37 +00:00
|
|
|
case "":
|
|
|
|
switch r.Method {
|
|
|
|
case http.MethodGet:
|
|
|
|
f.MustRender(w, r, user, company)
|
|
|
|
case http.MethodPut:
|
|
|
|
editSeason(w, r, user, company, conn, f)
|
|
|
|
default:
|
|
|
|
httplib.MethodNotAllowed(w, r, http.MethodGet, http.MethodPut)
|
|
|
|
}
|
Add seasons’ relation, functions, and admin section
Seasons have a color to show on the calendar. I need them in HTML format
(e.g., #123abc) in order to set as value to `<input type="color">`, but
i did not want to save them as text in the database, as integers are
better representations of colors—in fact, that’s what the HTML syntax
also is: an integer.
I think the best would be to create an extension that adds an HTML color
type, with functions to convert from many representations (e.g., CSS’
rgb or even color names) to integer and back. However, that’s a lot of
work and i can satisfy Camper’s needs with just a couple of functions
and a domain.
To show the color on the index, at first tried to use a read-only
`<input type="color">`, but seems that this type of input can not be
read-only and must be disabled instead. However, i do not know whether
it makes sense to have a disabled input outside a form “just” to show
a color; i suspect it does not. Thus, at the end i use SVG with a
single circle, which is better that a 50%-rounded div with a background
color, even if the result is the same—SVG **is** intended for showing
pictures, which is this case.
2023-08-16 18:15:57 +00:00
|
|
|
default:
|
2024-01-12 19:26:45 +00:00
|
|
|
http.NotFound(w, r)
|
Add seasons’ relation, functions, and admin section
Seasons have a color to show on the calendar. I need them in HTML format
(e.g., #123abc) in order to set as value to `<input type="color">`, but
i did not want to save them as text in the database, as integers are
better representations of colors—in fact, that’s what the HTML syntax
also is: an integer.
I think the best would be to create an extension that adds an HTML color
type, with functions to convert from many representations (e.g., CSS’
rgb or even color names) to integer and back. However, that’s a lot of
work and i can satisfy Camper’s needs with just a couple of functions
and a domain.
To show the color on the index, at first tried to use a read-only
`<input type="color">`, but seems that this type of input can not be
read-only and must be disabled instead. However, i do not know whether
it makes sense to have a disabled input outside a form “just” to show
a color; i suspect it does not. Thus, at the end i use SVG with a
single circle, which is better that a 50%-rounded div with a background
color, even if the result is the same—SVG **is** intended for showing
pictures, which is this case.
2023-08-16 18:15:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func serveSeasonIndex(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company, conn *database.Conn) {
|
2023-09-27 00:23:09 +00:00
|
|
|
seasons, err := collectSeasonEntries(r.Context(), company, conn)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2023-10-18 19:06:41 +00:00
|
|
|
calendar, err := CollectSeasonCalendar(r.Context(), company, conn, GetCalendarYear(r.URL.Query()), time.January, 12)
|
Add seasons’ relation, functions, and admin section
Seasons have a color to show on the calendar. I need them in HTML format
(e.g., #123abc) in order to set as value to `<input type="color">`, but
i did not want to save them as text in the database, as integers are
better representations of colors—in fact, that’s what the HTML syntax
also is: an integer.
I think the best would be to create an extension that adds an HTML color
type, with functions to convert from many representations (e.g., CSS’
rgb or even color names) to integer and back. However, that’s a lot of
work and i can satisfy Camper’s needs with just a couple of functions
and a domain.
To show the color on the index, at first tried to use a read-only
`<input type="color">`, but seems that this type of input can not be
read-only and must be disabled instead. However, i do not know whether
it makes sense to have a disabled input outside a form “just” to show
a color; i suspect it does not. Thus, at the end i use SVG with a
single circle, which is better that a 50%-rounded div with a background
color, even if the result is the same—SVG **is** intended for showing
pictures, which is this case.
2023-08-16 18:15:57 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2023-09-27 12:21:27 +00:00
|
|
|
calendar.Form = newCalendarForm(r.Context(), company, conn)
|
Add seasons’ relation, functions, and admin section
Seasons have a color to show on the calendar. I need them in HTML format
(e.g., #123abc) in order to set as value to `<input type="color">`, but
i did not want to save them as text in the database, as integers are
better representations of colors—in fact, that’s what the HTML syntax
also is: an integer.
I think the best would be to create an extension that adds an HTML color
type, with functions to convert from many representations (e.g., CSS’
rgb or even color names) to integer and back. However, that’s a lot of
work and i can satisfy Camper’s needs with just a couple of functions
and a domain.
To show the color on the index, at first tried to use a read-only
`<input type="color">`, but seems that this type of input can not be
read-only and must be disabled instead. However, i do not know whether
it makes sense to have a disabled input outside a form “just” to show
a color; i suspect it does not. Thus, at the end i use SVG with a
single circle, which is better that a 50%-rounded div with a background
color, even if the result is the same—SVG **is** intended for showing
pictures, which is this case.
2023-08-16 18:15:57 +00:00
|
|
|
page := &seasonIndex{
|
2023-09-27 00:23:09 +00:00
|
|
|
Seasons: seasons,
|
|
|
|
Calendar: calendar,
|
Add seasons’ relation, functions, and admin section
Seasons have a color to show on the calendar. I need them in HTML format
(e.g., #123abc) in order to set as value to `<input type="color">`, but
i did not want to save them as text in the database, as integers are
better representations of colors—in fact, that’s what the HTML syntax
also is: an integer.
I think the best would be to create an extension that adds an HTML color
type, with functions to convert from many representations (e.g., CSS’
rgb or even color names) to integer and back. However, that’s a lot of
work and i can satisfy Camper’s needs with just a couple of functions
and a domain.
To show the color on the index, at first tried to use a read-only
`<input type="color">`, but seems that this type of input can not be
read-only and must be disabled instead. However, i do not know whether
it makes sense to have a disabled input outside a form “just” to show
a color; i suspect it does not. Thus, at the end i use SVG with a
single circle, which is better that a 50%-rounded div with a background
color, even if the result is the same—SVG **is** intended for showing
pictures, which is this case.
2023-08-16 18:15:57 +00:00
|
|
|
}
|
|
|
|
page.MustRender(w, r, user, company)
|
|
|
|
}
|
|
|
|
|
2023-10-18 19:06:41 +00:00
|
|
|
func GetCalendarYear(query url.Values) int {
|
2023-09-29 16:20:16 +00:00
|
|
|
yearStr := strings.TrimSpace(query.Get("year"))
|
|
|
|
if yearStr != "" {
|
|
|
|
if year, err := strconv.Atoi(yearStr); err == nil {
|
|
|
|
return year
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return time.Now().Year()
|
|
|
|
}
|
|
|
|
|
2023-10-18 19:06:41 +00:00
|
|
|
func GetCalendarMonth(query url.Values) time.Month {
|
|
|
|
monthStr := strings.TrimSpace(query.Get("month"))
|
|
|
|
if monthStr != "" {
|
|
|
|
if month, err := strconv.Atoi(monthStr); err == nil {
|
|
|
|
return time.Month(month)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return time.Now().Month()
|
|
|
|
}
|
|
|
|
|
Add seasons’ relation, functions, and admin section
Seasons have a color to show on the calendar. I need them in HTML format
(e.g., #123abc) in order to set as value to `<input type="color">`, but
i did not want to save them as text in the database, as integers are
better representations of colors—in fact, that’s what the HTML syntax
also is: an integer.
I think the best would be to create an extension that adds an HTML color
type, with functions to convert from many representations (e.g., CSS’
rgb or even color names) to integer and back. However, that’s a lot of
work and i can satisfy Camper’s needs with just a couple of functions
and a domain.
To show the color on the index, at first tried to use a read-only
`<input type="color">`, but seems that this type of input can not be
read-only and must be disabled instead. However, i do not know whether
it makes sense to have a disabled input outside a form “just” to show
a color; i suspect it does not. Thus, at the end i use SVG with a
single circle, which is better that a 50%-rounded div with a background
color, even if the result is the same—SVG **is** intended for showing
pictures, which is this case.
2023-08-16 18:15:57 +00:00
|
|
|
func collectSeasonEntries(ctx context.Context, company *auth.Company, conn *database.Conn) ([]*seasonEntry, error) {
|
|
|
|
rows, err := conn.Query(ctx, `
|
2023-12-20 18:52:14 +00:00
|
|
|
select season.slug
|
|
|
|
, '/admin/seasons/' || season.slug
|
2023-10-03 19:14:37 +00:00
|
|
|
, season.name
|
Add seasons’ relation, functions, and admin section
Seasons have a color to show on the calendar. I need them in HTML format
(e.g., #123abc) in order to set as value to `<input type="color">`, but
i did not want to save them as text in the database, as integers are
better representations of colors—in fact, that’s what the HTML syntax
also is: an integer.
I think the best would be to create an extension that adds an HTML color
type, with functions to convert from many representations (e.g., CSS’
rgb or even color names) to integer and back. However, that’s a lot of
work and i can satisfy Camper’s needs with just a couple of functions
and a domain.
To show the color on the index, at first tried to use a read-only
`<input type="color">`, but seems that this type of input can not be
read-only and must be disabled instead. However, i do not know whether
it makes sense to have a disabled input outside a form “just” to show
a color; i suspect it does not. Thus, at the end i use SVG with a
single circle, which is better that a 50%-rounded div with a background
color, even if the result is the same—SVG **is** intended for showing
pictures, which is this case.
2023-08-16 18:15:57 +00:00
|
|
|
, to_color(color)::text
|
|
|
|
, active
|
|
|
|
from season
|
2024-01-12 19:26:45 +00:00
|
|
|
where season.company_id = $1
|
|
|
|
order by position, name`, company.ID)
|
Add seasons’ relation, functions, and admin section
Seasons have a color to show on the calendar. I need them in HTML format
(e.g., #123abc) in order to set as value to `<input type="color">`, but
i did not want to save them as text in the database, as integers are
better representations of colors—in fact, that’s what the HTML syntax
also is: an integer.
I think the best would be to create an extension that adds an HTML color
type, with functions to convert from many representations (e.g., CSS’
rgb or even color names) to integer and back. However, that’s a lot of
work and i can satisfy Camper’s needs with just a couple of functions
and a domain.
To show the color on the index, at first tried to use a read-only
`<input type="color">`, but seems that this type of input can not be
read-only and must be disabled instead. However, i do not know whether
it makes sense to have a disabled input outside a form “just” to show
a color; i suspect it does not. Thus, at the end i use SVG with a
single circle, which is better that a 50%-rounded div with a background
color, even if the result is the same—SVG **is** intended for showing
pictures, which is this case.
2023-08-16 18:15:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
var seasons []*seasonEntry
|
|
|
|
for rows.Next() {
|
|
|
|
entry := &seasonEntry{}
|
2024-01-12 19:26:45 +00:00
|
|
|
if err = rows.Scan(&entry.Slug, &entry.URL, &entry.Name, &entry.Color, &entry.Active); err != nil {
|
Add seasons’ relation, functions, and admin section
Seasons have a color to show on the calendar. I need them in HTML format
(e.g., #123abc) in order to set as value to `<input type="color">`, but
i did not want to save them as text in the database, as integers are
better representations of colors—in fact, that’s what the HTML syntax
also is: an integer.
I think the best would be to create an extension that adds an HTML color
type, with functions to convert from many representations (e.g., CSS’
rgb or even color names) to integer and back. However, that’s a lot of
work and i can satisfy Camper’s needs with just a couple of functions
and a domain.
To show the color on the index, at first tried to use a read-only
`<input type="color">`, but seems that this type of input can not be
read-only and must be disabled instead. However, i do not know whether
it makes sense to have a disabled input outside a form “just” to show
a color; i suspect it does not. Thus, at the end i use SVG with a
single circle, which is better that a 50%-rounded div with a background
color, even if the result is the same—SVG **is** intended for showing
pictures, which is this case.
2023-08-16 18:15:57 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
seasons = append(seasons, entry)
|
|
|
|
}
|
|
|
|
|
|
|
|
return seasons, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type seasonEntry struct {
|
2024-01-12 19:26:45 +00:00
|
|
|
ID int
|
|
|
|
Slug string
|
|
|
|
URL string
|
|
|
|
Name string
|
|
|
|
Color string
|
|
|
|
Active bool
|
Add seasons’ relation, functions, and admin section
Seasons have a color to show on the calendar. I need them in HTML format
(e.g., #123abc) in order to set as value to `<input type="color">`, but
i did not want to save them as text in the database, as integers are
better representations of colors—in fact, that’s what the HTML syntax
also is: an integer.
I think the best would be to create an extension that adds an HTML color
type, with functions to convert from many representations (e.g., CSS’
rgb or even color names) to integer and back. However, that’s a lot of
work and i can satisfy Camper’s needs with just a couple of functions
and a domain.
To show the color on the index, at first tried to use a read-only
`<input type="color">`, but seems that this type of input can not be
read-only and must be disabled instead. However, i do not know whether
it makes sense to have a disabled input outside a form “just” to show
a color; i suspect it does not. Thus, at the end i use SVG with a
single circle, which is better that a 50%-rounded div with a background
color, even if the result is the same—SVG **is** intended for showing
pictures, which is this case.
2023-08-16 18:15:57 +00:00
|
|
|
}
|
|
|
|
|
2023-09-27 00:23:09 +00:00
|
|
|
var longMonthNames = []string{
|
|
|
|
locale.PgettextNoop("January", "month"),
|
|
|
|
locale.PgettextNoop("February", "month"),
|
|
|
|
locale.PgettextNoop("March", "month"),
|
|
|
|
locale.PgettextNoop("April", "month"),
|
|
|
|
locale.PgettextNoop("May", "month"),
|
|
|
|
locale.PgettextNoop("June", "month"),
|
|
|
|
locale.PgettextNoop("July", "month"),
|
|
|
|
locale.PgettextNoop("August", "month"),
|
|
|
|
locale.PgettextNoop("September", "month"),
|
|
|
|
locale.PgettextNoop("October", "month"),
|
|
|
|
locale.PgettextNoop("November", "month"),
|
|
|
|
locale.PgettextNoop("December", "month"),
|
|
|
|
}
|
|
|
|
|
2023-10-18 19:06:41 +00:00
|
|
|
func CollectSeasonCalendar(ctx context.Context, company *auth.Company, conn *database.Conn, year int, firstMonth time.Month, monthCount int) (*Calendar, error) {
|
|
|
|
firstDay := time.Date(year, firstMonth, 1, 0, 0, 0, 0, time.UTC)
|
|
|
|
lastDay := firstDay.AddDate(0, monthCount, 0).Add(-1 * time.Second)
|
2023-12-21 21:51:12 +00:00
|
|
|
fmt.Println(lastDay)
|
2023-09-27 00:23:09 +00:00
|
|
|
rows, err := conn.Query(ctx, `
|
|
|
|
select t.day::date
|
2023-09-27 12:21:27 +00:00
|
|
|
, to_color(coalesce(color, $1)) as color
|
|
|
|
from generate_series($2, $3, interval '1 day') as t(day)
|
2023-09-27 00:23:09 +00:00
|
|
|
left join season_calendar on season_range @> t.day::date
|
2023-09-27 12:21:27 +00:00
|
|
|
left join season on season.season_id = season_calendar.season_id and company_id = $4
|
2023-12-21 21:51:12 +00:00
|
|
|
`, UnsetColor, firstDay.Format("2006-01-02 15:04:05"), lastDay.Format("2006-01-02 15:04:05"), company.ID)
|
2023-09-27 00:23:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-10-14 21:14:23 +00:00
|
|
|
var month *Month
|
|
|
|
var week Week
|
|
|
|
calendar := &Calendar{
|
2023-09-29 16:20:16 +00:00
|
|
|
Year: year,
|
|
|
|
}
|
2023-09-27 00:23:09 +00:00
|
|
|
weekday := int(time.Monday)
|
|
|
|
for rows.Next() {
|
2023-10-14 21:14:23 +00:00
|
|
|
day := &Day{}
|
2023-09-27 00:23:09 +00:00
|
|
|
if err = rows.Scan(&day.Date, &day.Color); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
dayMonth := day.Date.Month()
|
|
|
|
if month == nil || month.Month != dayMonth {
|
|
|
|
if month != nil {
|
|
|
|
for ; weekday != int(time.Sunday); weekday = (weekday + 1) % 7 {
|
2023-10-14 21:14:23 +00:00
|
|
|
week = append(week, &Day{})
|
2023-09-27 00:23:09 +00:00
|
|
|
}
|
|
|
|
month.Weeks = append(month.Weeks, week)
|
2023-09-27 12:21:27 +00:00
|
|
|
calendar.Months = append(calendar.Months, month)
|
2023-09-27 00:23:09 +00:00
|
|
|
}
|
2023-10-14 21:14:23 +00:00
|
|
|
month = &Month{
|
2023-10-18 19:06:41 +00:00
|
|
|
Year: day.Date.Year(),
|
2023-09-27 00:23:09 +00:00
|
|
|
Month: dayMonth,
|
|
|
|
Name: longMonthNames[dayMonth-1],
|
|
|
|
}
|
2023-10-14 21:14:23 +00:00
|
|
|
week = Week{}
|
2023-09-27 00:23:09 +00:00
|
|
|
weekday = int(time.Monday)
|
|
|
|
dayWeekday := int(day.Date.Weekday())
|
|
|
|
for ; weekday != dayWeekday; weekday = (weekday + 1) % 7 {
|
2023-10-14 21:14:23 +00:00
|
|
|
week = append(week, &Day{})
|
2023-09-27 00:23:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
week = append(week, day)
|
|
|
|
weekday = (weekday + 1) % 7
|
|
|
|
if weekday == int(time.Monday) {
|
|
|
|
month.Weeks = append(month.Weeks, week)
|
2023-10-14 21:14:23 +00:00
|
|
|
week = Week{}
|
2023-09-27 00:23:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if month != nil {
|
|
|
|
for ; weekday != int(time.Sunday); weekday = (weekday + 1) % 7 {
|
2023-10-14 21:14:23 +00:00
|
|
|
week = append(week, &Day{})
|
2023-09-27 00:23:09 +00:00
|
|
|
}
|
|
|
|
month.Weeks = append(month.Weeks, week)
|
2023-09-27 12:21:27 +00:00
|
|
|
calendar.Months = append(calendar.Months, month)
|
2023-09-27 00:23:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return calendar, nil
|
|
|
|
}
|
|
|
|
|
2023-10-14 21:14:23 +00:00
|
|
|
type Calendar struct {
|
2023-09-29 16:20:16 +00:00
|
|
|
Year int
|
2023-10-14 21:14:23 +00:00
|
|
|
Months []*Month
|
2023-09-27 12:21:27 +00:00
|
|
|
Form *calendarForm
|
|
|
|
}
|
2023-09-27 00:23:09 +00:00
|
|
|
|
2023-10-14 21:14:23 +00:00
|
|
|
type Month struct {
|
2023-10-18 19:06:41 +00:00
|
|
|
Year int
|
2023-09-27 00:23:09 +00:00
|
|
|
Month time.Month
|
|
|
|
Name string
|
2023-10-14 21:14:23 +00:00
|
|
|
Weeks []Week
|
2023-09-27 00:23:09 +00:00
|
|
|
}
|
|
|
|
|
2023-10-14 21:14:23 +00:00
|
|
|
type Week []*Day
|
2023-09-27 00:23:09 +00:00
|
|
|
|
2023-10-14 21:14:23 +00:00
|
|
|
type Day struct {
|
2023-09-27 00:23:09 +00:00
|
|
|
Date time.Time
|
|
|
|
Color string
|
|
|
|
}
|
|
|
|
|
Add seasons’ relation, functions, and admin section
Seasons have a color to show on the calendar. I need them in HTML format
(e.g., #123abc) in order to set as value to `<input type="color">`, but
i did not want to save them as text in the database, as integers are
better representations of colors—in fact, that’s what the HTML syntax
also is: an integer.
I think the best would be to create an extension that adds an HTML color
type, with functions to convert from many representations (e.g., CSS’
rgb or even color names) to integer and back. However, that’s a lot of
work and i can satisfy Camper’s needs with just a couple of functions
and a domain.
To show the color on the index, at first tried to use a read-only
`<input type="color">`, but seems that this type of input can not be
read-only and must be disabled instead. However, i do not know whether
it makes sense to have a disabled input outside a form “just” to show
a color; i suspect it does not. Thus, at the end i use SVG with a
single circle, which is better that a 50%-rounded div with a background
color, even if the result is the same—SVG **is** intended for showing
pictures, which is this case.
2023-08-16 18:15:57 +00:00
|
|
|
type seasonIndex struct {
|
2023-09-27 00:23:09 +00:00
|
|
|
Seasons []*seasonEntry
|
2023-10-14 21:14:23 +00:00
|
|
|
Calendar *Calendar
|
Add seasons’ relation, functions, and admin section
Seasons have a color to show on the calendar. I need them in HTML format
(e.g., #123abc) in order to set as value to `<input type="color">`, but
i did not want to save them as text in the database, as integers are
better representations of colors—in fact, that’s what the HTML syntax
also is: an integer.
I think the best would be to create an extension that adds an HTML color
type, with functions to convert from many representations (e.g., CSS’
rgb or even color names) to integer and back. However, that’s a lot of
work and i can satisfy Camper’s needs with just a couple of functions
and a domain.
To show the color on the index, at first tried to use a read-only
`<input type="color">`, but seems that this type of input can not be
read-only and must be disabled instead. However, i do not know whether
it makes sense to have a disabled input outside a form “just” to show
a color; i suspect it does not. Thus, at the end i use SVG with a
single circle, which is better that a 50%-rounded div with a background
color, even if the result is the same—SVG **is** intended for showing
pictures, which is this case.
2023-08-16 18:15:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (page *seasonIndex) MustRender(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company) {
|
2023-09-27 12:21:27 +00:00
|
|
|
template.MustRenderAdminFiles(w, r, user, company, page, "season/index.gohtml", "season/calendar.gohtml")
|
Add seasons’ relation, functions, and admin section
Seasons have a color to show on the calendar. I need them in HTML format
(e.g., #123abc) in order to set as value to `<input type="color">`, but
i did not want to save them as text in the database, as integers are
better representations of colors—in fact, that’s what the HTML syntax
also is: an integer.
I think the best would be to create an extension that adds an HTML color
type, with functions to convert from many representations (e.g., CSS’
rgb or even color names) to integer and back. However, that’s a lot of
work and i can satisfy Camper’s needs with just a couple of functions
and a domain.
To show the color on the index, at first tried to use a read-only
`<input type="color">`, but seems that this type of input can not be
read-only and must be disabled instead. However, i do not know whether
it makes sense to have a disabled input outside a form “just” to show
a color; i suspect it does not. Thus, at the end i use SVG with a
single circle, which is better that a 50%-rounded div with a background
color, even if the result is the same—SVG **is** intended for showing
pictures, which is this case.
2023-08-16 18:15:57 +00:00
|
|
|
}
|
|
|
|
|
2024-01-12 19:26:45 +00:00
|
|
|
func processSeasonForm(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company, conn *database.Conn, f *seasonForm, act func(context.Context, *database.Tx) error) {
|
Add seasons’ relation, functions, and admin section
Seasons have a color to show on the calendar. I need them in HTML format
(e.g., #123abc) in order to set as value to `<input type="color">`, but
i did not want to save them as text in the database, as integers are
better representations of colors—in fact, that’s what the HTML syntax
also is: an integer.
I think the best would be to create an extension that adds an HTML color
type, with functions to convert from many representations (e.g., CSS’
rgb or even color names) to integer and back. However, that’s a lot of
work and i can satisfy Camper’s needs with just a couple of functions
and a domain.
To show the color on the index, at first tried to use a read-only
`<input type="color">`, but seems that this type of input can not be
read-only and must be disabled instead. However, i do not know whether
it makes sense to have a disabled input outside a form “just” to show
a color; i suspect it does not. Thus, at the end i use SVG with a
single circle, which is better that a 50%-rounded div with a background
color, even if the result is the same—SVG **is** intended for showing
pictures, which is this case.
2023-08-16 18:15:57 +00:00
|
|
|
if err := f.Parse(r); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := user.VerifyCSRFToken(r); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if ok, err := f.Valid(r.Context(), conn, user.Locale); err != nil {
|
|
|
|
panic(err)
|
|
|
|
} else if !ok {
|
|
|
|
if !httplib.IsHTMxRequest(r) {
|
|
|
|
w.WriteHeader(http.StatusUnprocessableEntity)
|
|
|
|
}
|
|
|
|
f.MustRender(w, r, user, company)
|
|
|
|
return
|
|
|
|
}
|
2024-01-12 19:26:45 +00:00
|
|
|
tx := conn.MustBegin(r.Context())
|
|
|
|
if err := act(r.Context(), tx); err == nil {
|
|
|
|
tx.MustCommit(r.Context())
|
|
|
|
} else {
|
|
|
|
if rErr := tx.Rollback(r.Context()); rErr != nil {
|
|
|
|
err = rErr
|
|
|
|
}
|
|
|
|
panic(err)
|
|
|
|
}
|
Add seasons’ relation, functions, and admin section
Seasons have a color to show on the calendar. I need them in HTML format
(e.g., #123abc) in order to set as value to `<input type="color">`, but
i did not want to save them as text in the database, as integers are
better representations of colors—in fact, that’s what the HTML syntax
also is: an integer.
I think the best would be to create an extension that adds an HTML color
type, with functions to convert from many representations (e.g., CSS’
rgb or even color names) to integer and back. However, that’s a lot of
work and i can satisfy Camper’s needs with just a couple of functions
and a domain.
To show the color on the index, at first tried to use a read-only
`<input type="color">`, but seems that this type of input can not be
read-only and must be disabled instead. However, i do not know whether
it makes sense to have a disabled input outside a form “just” to show
a color; i suspect it does not. Thus, at the end i use SVG with a
single circle, which is better that a 50%-rounded div with a background
color, even if the result is the same—SVG **is** intended for showing
pictures, which is this case.
2023-08-16 18:15:57 +00:00
|
|
|
httplib.Redirect(w, r, "/admin/seasons", http.StatusSeeOther)
|
|
|
|
}
|
|
|
|
|
|
|
|
func addSeason(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company, conn *database.Conn) {
|
2024-01-12 19:26:45 +00:00
|
|
|
f := newSeasonForm(company)
|
|
|
|
processSeasonForm(w, r, user, company, conn, f, func(ctx context.Context, tx *database.Tx) error {
|
|
|
|
var err error
|
|
|
|
if f.Slug, err = tx.AddSeason(ctx, company.ID, f.Name[f.DefaultLang].Val, f.Color.Val); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return translateSeason(ctx, tx, company, f)
|
Add seasons’ relation, functions, and admin section
Seasons have a color to show on the calendar. I need them in HTML format
(e.g., #123abc) in order to set as value to `<input type="color">`, but
i did not want to save them as text in the database, as integers are
better representations of colors—in fact, that’s what the HTML syntax
also is: an integer.
I think the best would be to create an extension that adds an HTML color
type, with functions to convert from many representations (e.g., CSS’
rgb or even color names) to integer and back. However, that’s a lot of
work and i can satisfy Camper’s needs with just a couple of functions
and a domain.
To show the color on the index, at first tried to use a read-only
`<input type="color">`, but seems that this type of input can not be
read-only and must be disabled instead. However, i do not know whether
it makes sense to have a disabled input outside a form “just” to show
a color; i suspect it does not. Thus, at the end i use SVG with a
single circle, which is better that a 50%-rounded div with a background
color, even if the result is the same—SVG **is** intended for showing
pictures, which is this case.
2023-08-16 18:15:57 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-01-12 19:26:45 +00:00
|
|
|
func translateSeason(ctx context.Context, tx *database.Tx, company *auth.Company, f *seasonForm) error {
|
|
|
|
for lang := range company.Locales {
|
|
|
|
l := lang.String()
|
|
|
|
if l == f.DefaultLang {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := tx.TranslateSeason(ctx, f.Slug, lang, f.Name[l].Val); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
Add seasons’ relation, functions, and admin section
Seasons have a color to show on the calendar. I need them in HTML format
(e.g., #123abc) in order to set as value to `<input type="color">`, but
i did not want to save them as text in the database, as integers are
better representations of colors—in fact, that’s what the HTML syntax
also is: an integer.
I think the best would be to create an extension that adds an HTML color
type, with functions to convert from many representations (e.g., CSS’
rgb or even color names) to integer and back. However, that’s a lot of
work and i can satisfy Camper’s needs with just a couple of functions
and a domain.
To show the color on the index, at first tried to use a read-only
`<input type="color">`, but seems that this type of input can not be
read-only and must be disabled instead. However, i do not know whether
it makes sense to have a disabled input outside a form “just” to show
a color; i suspect it does not. Thus, at the end i use SVG with a
single circle, which is better that a 50%-rounded div with a background
color, even if the result is the same—SVG **is** intended for showing
pictures, which is this case.
2023-08-16 18:15:57 +00:00
|
|
|
func editSeason(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company, conn *database.Conn, f *seasonForm) {
|
2024-01-12 19:26:45 +00:00
|
|
|
processSeasonForm(w, r, user, company, conn, f, func(ctx context.Context, tx *database.Tx) error {
|
|
|
|
if err := tx.EditSeason(ctx, f.Slug, f.Name[f.DefaultLang].Val, f.Color.Val, f.Active.Checked); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return translateSeason(ctx, tx, company, f)
|
Add seasons’ relation, functions, and admin section
Seasons have a color to show on the calendar. I need them in HTML format
(e.g., #123abc) in order to set as value to `<input type="color">`, but
i did not want to save them as text in the database, as integers are
better representations of colors—in fact, that’s what the HTML syntax
also is: an integer.
I think the best would be to create an extension that adds an HTML color
type, with functions to convert from many representations (e.g., CSS’
rgb or even color names) to integer and back. However, that’s a lot of
work and i can satisfy Camper’s needs with just a couple of functions
and a domain.
To show the color on the index, at first tried to use a read-only
`<input type="color">`, but seems that this type of input can not be
read-only and must be disabled instead. However, i do not know whether
it makes sense to have a disabled input outside a form “just” to show
a color; i suspect it does not. Thus, at the end i use SVG with a
single circle, which is better that a 50%-rounded div with a background
color, even if the result is the same—SVG **is** intended for showing
pictures, which is this case.
2023-08-16 18:15:57 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type seasonForm struct {
|
2024-01-12 19:26:45 +00:00
|
|
|
DefaultLang string
|
|
|
|
Slug string
|
|
|
|
Active *form.Checkbox
|
|
|
|
Name form.I18nInput
|
|
|
|
Color *form.Input
|
Add seasons’ relation, functions, and admin section
Seasons have a color to show on the calendar. I need them in HTML format
(e.g., #123abc) in order to set as value to `<input type="color">`, but
i did not want to save them as text in the database, as integers are
better representations of colors—in fact, that’s what the HTML syntax
also is: an integer.
I think the best would be to create an extension that adds an HTML color
type, with functions to convert from many representations (e.g., CSS’
rgb or even color names) to integer and back. However, that’s a lot of
work and i can satisfy Camper’s needs with just a couple of functions
and a domain.
To show the color on the index, at first tried to use a read-only
`<input type="color">`, but seems that this type of input can not be
read-only and must be disabled instead. However, i do not know whether
it makes sense to have a disabled input outside a form “just” to show
a color; i suspect it does not. Thus, at the end i use SVG with a
single circle, which is better that a 50%-rounded div with a background
color, even if the result is the same—SVG **is** intended for showing
pictures, which is this case.
2023-08-16 18:15:57 +00:00
|
|
|
}
|
|
|
|
|
2024-01-12 19:26:45 +00:00
|
|
|
func newSeasonForm(company *auth.Company) *seasonForm {
|
Add seasons’ relation, functions, and admin section
Seasons have a color to show on the calendar. I need them in HTML format
(e.g., #123abc) in order to set as value to `<input type="color">`, but
i did not want to save them as text in the database, as integers are
better representations of colors—in fact, that’s what the HTML syntax
also is: an integer.
I think the best would be to create an extension that adds an HTML color
type, with functions to convert from many representations (e.g., CSS’
rgb or even color names) to integer and back. However, that’s a lot of
work and i can satisfy Camper’s needs with just a couple of functions
and a domain.
To show the color on the index, at first tried to use a read-only
`<input type="color">`, but seems that this type of input can not be
read-only and must be disabled instead. However, i do not know whether
it makes sense to have a disabled input outside a form “just” to show
a color; i suspect it does not. Thus, at the end i use SVG with a
single circle, which is better that a 50%-rounded div with a background
color, even if the result is the same—SVG **is** intended for showing
pictures, which is this case.
2023-08-16 18:15:57 +00:00
|
|
|
return &seasonForm{
|
2024-01-12 19:26:45 +00:00
|
|
|
DefaultLang: company.DefaultLanguage.String(),
|
Add seasons’ relation, functions, and admin section
Seasons have a color to show on the calendar. I need them in HTML format
(e.g., #123abc) in order to set as value to `<input type="color">`, but
i did not want to save them as text in the database, as integers are
better representations of colors—in fact, that’s what the HTML syntax
also is: an integer.
I think the best would be to create an extension that adds an HTML color
type, with functions to convert from many representations (e.g., CSS’
rgb or even color names) to integer and back. However, that’s a lot of
work and i can satisfy Camper’s needs with just a couple of functions
and a domain.
To show the color on the index, at first tried to use a read-only
`<input type="color">`, but seems that this type of input can not be
read-only and must be disabled instead. However, i do not know whether
it makes sense to have a disabled input outside a form “just” to show
a color; i suspect it does not. Thus, at the end i use SVG with a
single circle, which is better that a 50%-rounded div with a background
color, even if the result is the same—SVG **is** intended for showing
pictures, which is this case.
2023-08-16 18:15:57 +00:00
|
|
|
Active: &form.Checkbox{
|
|
|
|
Name: "active",
|
|
|
|
Checked: true,
|
|
|
|
},
|
2024-01-12 19:26:45 +00:00
|
|
|
Name: form.NewI18nInput(company.Locales, "label"),
|
Add seasons’ relation, functions, and admin section
Seasons have a color to show on the calendar. I need them in HTML format
(e.g., #123abc) in order to set as value to `<input type="color">`, but
i did not want to save them as text in the database, as integers are
better representations of colors—in fact, that’s what the HTML syntax
also is: an integer.
I think the best would be to create an extension that adds an HTML color
type, with functions to convert from many representations (e.g., CSS’
rgb or even color names) to integer and back. However, that’s a lot of
work and i can satisfy Camper’s needs with just a couple of functions
and a domain.
To show the color on the index, at first tried to use a read-only
`<input type="color">`, but seems that this type of input can not be
read-only and must be disabled instead. However, i do not know whether
it makes sense to have a disabled input outside a form “just” to show
a color; i suspect it does not. Thus, at the end i use SVG with a
single circle, which is better that a 50%-rounded div with a background
color, even if the result is the same—SVG **is** intended for showing
pictures, which is this case.
2023-08-16 18:15:57 +00:00
|
|
|
Color: &form.Input{
|
|
|
|
Name: "season",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *seasonForm) FillFromDatabase(ctx context.Context, conn *database.Conn, slug string) error {
|
|
|
|
f.Slug = slug
|
2024-01-12 19:26:45 +00:00
|
|
|
var name database.RecordArray
|
|
|
|
row := conn.QueryRow(ctx, `
|
|
|
|
select season.name
|
|
|
|
, to_color(color)::text
|
|
|
|
, active
|
|
|
|
, array_agg((lang_tag, i18n.name))
|
|
|
|
from season
|
|
|
|
left join season_i18n as i18n using (season_id)
|
|
|
|
where slug = $1
|
|
|
|
group by season.name
|
|
|
|
, to_color(color)::text
|
|
|
|
, active
|
|
|
|
`, pgx.QueryResultFormats{pgx.BinaryFormatCode}, slug)
|
|
|
|
if err := row.Scan(&f.Name[f.DefaultLang].Val, &f.Color.Val, &f.Active.Checked, &name); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := f.Name.FillArray(name); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
Add seasons’ relation, functions, and admin section
Seasons have a color to show on the calendar. I need them in HTML format
(e.g., #123abc) in order to set as value to `<input type="color">`, but
i did not want to save them as text in the database, as integers are
better representations of colors—in fact, that’s what the HTML syntax
also is: an integer.
I think the best would be to create an extension that adds an HTML color
type, with functions to convert from many representations (e.g., CSS’
rgb or even color names) to integer and back. However, that’s a lot of
work and i can satisfy Camper’s needs with just a couple of functions
and a domain.
To show the color on the index, at first tried to use a read-only
`<input type="color">`, but seems that this type of input can not be
read-only and must be disabled instead. However, i do not know whether
it makes sense to have a disabled input outside a form “just” to show
a color; i suspect it does not. Thus, at the end i use SVG with a
single circle, which is better that a 50%-rounded div with a background
color, even if the result is the same—SVG **is** intended for showing
pictures, which is this case.
2023-08-16 18:15:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *seasonForm) Parse(r *http.Request) error {
|
|
|
|
if err := r.ParseForm(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f.Active.FillValue(r)
|
|
|
|
f.Name.FillValue(r)
|
|
|
|
f.Color.FillValue(r)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *seasonForm) Valid(ctx context.Context, conn *database.Conn, l *locale.Locale) (bool, error) {
|
|
|
|
v := form.NewValidator(l)
|
2024-01-12 19:26:45 +00:00
|
|
|
v.CheckRequired(f.Name[f.DefaultLang], l.GettextNoop("Name can not be empty."))
|
Add seasons’ relation, functions, and admin section
Seasons have a color to show on the calendar. I need them in HTML format
(e.g., #123abc) in order to set as value to `<input type="color">`, but
i did not want to save them as text in the database, as integers are
better representations of colors—in fact, that’s what the HTML syntax
also is: an integer.
I think the best would be to create an extension that adds an HTML color
type, with functions to convert from many representations (e.g., CSS’
rgb or even color names) to integer and back. However, that’s a lot of
work and i can satisfy Camper’s needs with just a couple of functions
and a domain.
To show the color on the index, at first tried to use a read-only
`<input type="color">`, but seems that this type of input can not be
read-only and must be disabled instead. However, i do not know whether
it makes sense to have a disabled input outside a form “just” to show
a color; i suspect it does not. Thus, at the end i use SVG with a
single circle, which is better that a 50%-rounded div with a background
color, even if the result is the same—SVG **is** intended for showing
pictures, which is this case.
2023-08-16 18:15:57 +00:00
|
|
|
if v.CheckRequired(f.Color, l.GettextNoop("Color can not be empty.")) {
|
|
|
|
if _, err := v.CheckValidColor(ctx, conn, f.Color, l.Gettext("This color is not valid. It must be like #123abc.")); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return v.AllOK, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *seasonForm) MustRender(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company) {
|
|
|
|
template.MustRenderAdmin(w, r, user, company, "season/form.gohtml", f)
|
|
|
|
}
|
2023-09-27 12:21:27 +00:00
|
|
|
|
2023-12-20 18:52:14 +00:00
|
|
|
func orderSeasons(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company, conn *database.Conn) {
|
|
|
|
if err := r.ParseForm(); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := user.VerifyCSRFToken(r); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
slugs := r.PostForm["slug"]
|
|
|
|
if len(slugs) > 0 {
|
|
|
|
for _, slug := range slugs {
|
|
|
|
if !uuid.Valid(slug) {
|
|
|
|
w.WriteHeader(http.StatusUnprocessableEntity)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := conn.OrderSeasons(r.Context(), slugs); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
serveSeasonIndex(w, r, user, company, conn)
|
|
|
|
}
|
|
|
|
|
2023-09-29 16:20:16 +00:00
|
|
|
func serveSeasonCalendar(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company, conn *database.Conn) {
|
|
|
|
f := newCalendarForm(r.Context(), company, conn)
|
2023-10-18 19:06:41 +00:00
|
|
|
f.MustRender(w, r, user, company, conn, GetCalendarYear(r.URL.Query()))
|
2023-09-29 16:20:16 +00:00
|
|
|
}
|
|
|
|
|
2023-09-27 12:21:27 +00:00
|
|
|
func updateSeasonCalendar(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company, conn *database.Conn) {
|
|
|
|
f := newCalendarForm(r.Context(), company, conn)
|
|
|
|
if ok, err := form.Handle(f, w, r, user); err != nil {
|
|
|
|
return
|
|
|
|
} else if ok {
|
|
|
|
var seasonRange pgtype.Daterange
|
|
|
|
if err = seasonRange.Set(fmt.Sprintf("[%s,%s]", f.StartDate.Val, f.EndDate.Val)); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if f.SeasonID.Val == "" {
|
2023-09-28 23:35:05 +00:00
|
|
|
// Nothing to do
|
|
|
|
} else if f.SeasonID.Val == "0" {
|
2023-09-27 12:21:27 +00:00
|
|
|
conn.MustExec(r.Context(), "select unset_season_range($1)", seasonRange)
|
|
|
|
} else {
|
|
|
|
conn.MustExec(r.Context(), "select set_season_range($1, $2)", f.SeasonID, seasonRange)
|
|
|
|
}
|
|
|
|
f.StartDate.Val = ""
|
|
|
|
f.EndDate.Val = ""
|
|
|
|
}
|
2023-10-18 19:06:41 +00:00
|
|
|
f.MustRender(w, r, user, company, conn, GetCalendarYear(r.Form))
|
2023-09-27 12:21:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type calendarForm struct {
|
|
|
|
Seasons []*seasonEntry
|
|
|
|
StartDate *form.Input
|
|
|
|
EndDate *form.Input
|
|
|
|
SeasonID *form.Input
|
|
|
|
}
|
|
|
|
|
|
|
|
func newCalendarForm(ctx context.Context, company *auth.Company, conn *database.Conn) *calendarForm {
|
|
|
|
return &calendarForm{
|
|
|
|
Seasons: mustCollectCalendarSeasons(ctx, company, conn),
|
|
|
|
StartDate: &form.Input{
|
|
|
|
Name: "start_date",
|
|
|
|
},
|
|
|
|
EndDate: &form.Input{
|
|
|
|
Name: "end_date",
|
|
|
|
},
|
|
|
|
SeasonID: &form.Input{
|
|
|
|
Name: "season_id",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func mustCollectCalendarSeasons(ctx context.Context, company *auth.Company, conn *database.Conn) []*seasonEntry {
|
|
|
|
rows, err := conn.Query(ctx, `
|
2023-10-03 19:14:37 +00:00
|
|
|
select 0 as season_id
|
2023-09-27 12:21:27 +00:00
|
|
|
, $1 as name
|
|
|
|
, to_color($2)::text
|
|
|
|
, true
|
2023-12-20 18:52:14 +00:00
|
|
|
, 0 as position
|
2023-09-27 12:21:27 +00:00
|
|
|
union all
|
2023-10-03 19:14:37 +00:00
|
|
|
select season_id
|
2023-09-27 12:21:27 +00:00
|
|
|
, name
|
|
|
|
, to_color(color)::text
|
|
|
|
, active
|
2023-12-20 18:52:14 +00:00
|
|
|
, position
|
2023-09-27 12:21:27 +00:00
|
|
|
from season
|
|
|
|
where company_id = $3
|
|
|
|
and active
|
2023-12-20 18:52:14 +00:00
|
|
|
order by position, name`, locale.PgettextNoop("Unset", "action"), UnsetColor, company.ID)
|
2023-09-27 12:21:27 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
var seasons []*seasonEntry
|
|
|
|
for rows.Next() {
|
|
|
|
entry := &seasonEntry{}
|
|
|
|
var sort int
|
2023-10-03 19:14:37 +00:00
|
|
|
if err = rows.Scan(&entry.ID, &entry.Name, &entry.Color, &entry.Active, &sort); err != nil {
|
2023-09-27 12:21:27 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
seasons = append(seasons, entry)
|
|
|
|
}
|
|
|
|
|
|
|
|
return seasons
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *calendarForm) Parse(r *http.Request) error {
|
|
|
|
if err := r.ParseForm(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f.StartDate.FillValue(r)
|
|
|
|
f.EndDate.FillValue(r)
|
|
|
|
f.SeasonID.FillValue(r)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *calendarForm) Valid(l *locale.Locale) bool {
|
|
|
|
v := form.NewValidator(l)
|
|
|
|
if v.CheckRequired(f.StartDate, l.GettextNoop("Start date can not be empty.")) {
|
|
|
|
v.CheckValidDate(f.StartDate, l.GettextNoop("Start date must be a valid date."))
|
|
|
|
}
|
|
|
|
if v.CheckRequired(f.EndDate, l.GettextNoop("End date can not be empty.")) {
|
|
|
|
v.CheckValidDate(f.EndDate, l.GettextNoop("End date must be a valid date."))
|
|
|
|
}
|
|
|
|
return v.AllOK
|
|
|
|
}
|
|
|
|
|
2023-09-29 16:20:16 +00:00
|
|
|
func (f *calendarForm) MustRender(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company, conn *database.Conn, year int) {
|
2023-10-18 19:06:41 +00:00
|
|
|
calendar, err := CollectSeasonCalendar(r.Context(), company, conn, year, time.January, 12)
|
2023-09-27 12:21:27 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
calendar.Form = f
|
2023-10-18 19:06:41 +00:00
|
|
|
template.MustRenderAdminNoLayout(w, r, user, company, "season/calendar.gohtml", calendar)
|
2023-09-27 12:21:27 +00:00
|
|
|
}
|