commit e6cf23a08bda265c1499b989ed6288e4c0e24ef9
parent 5f45b62a48f73746bcfb1c8b5870e42e9391fa65
Author: Jacob R. Edwards <jacob@jacobedwards.org>
Date: Sun, 17 Mar 2024 17:08:37 -0700
Add break time button
This button sets the period on the current times.
Diffstat:
5 files changed, 42 insertions(+), 1 deletion(-)
diff --git a/key.c b/key.c
@@ -40,5 +40,6 @@ struct kvalid keys[] = {
[KeyCreate] = { kvalid_stringne, "create" },
[KeyDelete] = { kvalid_stringne, "delete" },
[KeyTime] = { kvalid_stringne, "time" },
- [KeyFormat] = { kvalid_stringne, "format" }
+ [KeyFormat] = { kvalid_stringne, "format" },
+ [KeyBreak] = { kvalid_stringne, "break" }
};
diff --git a/key.h b/key.h
@@ -6,6 +6,7 @@ enum http_key {
KeyDelete, /* Delete account in account page */
KeyTime, /* one of start, startbreak, endbreak, end */
KeyFormat, /* currently either tsv;epoch or ;epoch for /export */
+ KeyBreak, /* Break of timesheet period */
KeyMax
};
diff --git a/pages/main.c b/pages/main.c
@@ -320,6 +320,26 @@ exportform(struct pagedata *pd)
}
enum kcgi_err
+breakform(struct pagedata *pd)
+{
+ enum kcgi_err status;
+
+ if ((status = khtml_elem(&pd->html, KELEM_FORM)) != KCGI_OK ||
+ (status = khtml_putc(&pd->html, ' ')) != KCGI_OK ||
+ (status = khtml_elem(&pd->html, KELEM_LABEL)) != KCGI_OK ||
+ (status = khtml_attr(&pd->html, KELEM_INPUT,
+ KATTR_TYPE, "submit",
+ KATTR_VALUE, "End period", KATTR__MAX)) != KCGI_OK ||
+ (status = khtml_attr(&pd->html, KELEM_INPUT,
+ KATTR_TYPE, "hidden",
+ KATTR_NAME, keys[KeyBreak].name,
+ KATTR_VALUE, "true", KATTR__MAX)) != KCGI_OK ||
+ (status = khtml_closeelem(&pd->html, 2)) != KCGI_OK)
+ return status;
+ return KCGI_OK;
+}
+
+enum kcgi_err
pagemain(struct pagedata *pd)
{
static char *css[] = { "css/main.css", "css/timesheet.css", NULL };
@@ -341,6 +361,11 @@ pagemain(struct pagedata *pd)
if (!pd->user)
return errorpage(pd, KHTTP_401);
+ if (pd->req.fieldmap[KeyBreak]) {
+ if (breaktime(pd, pd->user->hash))
+ err(1, "Unable to break time");
+ }
+
if (pd->req.fieldmap[KeyTime]) {
tf = gettf(pd->req.fieldmap[KeyTime]->parsed.s);
if (tf < 0)
@@ -367,6 +392,7 @@ pagemain(struct pagedata *pd)
if ((status = htmlwithin(pd, KELEM_H1, "Timekeeper")) != KCGI_OK ||
(status = exportform(pd)) != KCGI_OK ||
+ (status = breakform(pd)) != KCGI_OK ||
(status = printtimes(pd, times)) != KCGI_OK) {
freetimesheet(times);
return status;
diff --git a/times.c b/times.c
@@ -62,6 +62,18 @@ settime(struct pagedata *pd, char *hash, enum time_field f, time_t time)
Len(ps) - 1, ps + 1, 0) != SQLBOX_CODE_OK;
}
+int
+breaktime(struct pagedata *pd, char *hash)
+{
+ struct sqlbox_parm ps[] = {
+ { .sparm = hash, .type = SQLBOX_PARM_STRING },
+ { .sparm = hash, .type = SQLBOX_PARM_STRING }
+ };
+
+ return sqlbox_exec(pd->db, pd->dbid, StmtBreakTime,
+ Len(ps), ps, 0) != SQLBOX_CODE_OK;
+}
+
void
freetimesheet(struct timesheet *ts)
{
diff --git a/times.h b/times.h
@@ -24,6 +24,7 @@ extern char *timefields[];
extern enum time_flag timeflagmap[];
enum sqlbox_code settime(struct pagedata *pd, char *hash, enum time_field f, time_t time);
+int breaktime(struct pagedata *pd, char *hash);
void freetimesheet(struct timesheet *ts);
struct timesheet *newtimesheet(void);
struct timesheet *inserttimesheet(struct timesheet *ts, struct timesheet *list);