From 177521aa341b7e5480564266403f6096445446b1 Mon Sep 17 00:00:00 2001 From: Craig Oates Date: Thu, 15 Sep 2022 15:16:24 +0100 Subject: [PATCH] add bool to checkbox convertion funcitons to utils pacakge. The functions are helper functions for when dealing with SQLite3. Because SQLite3 doesn't have a Boolean data-type, I have to store the 'true' and 'false' values as integetes. 0 == false 1 == true. --- src/utils.lisp | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/utils.lisp b/src/utils.lisp index 0e9cd9a..b5361a1 100644 --- a/src/utils.lisp +++ b/src/utils.lisp @@ -2,7 +2,8 @@ (defpackage #:utils (:use #:cl #:caveman2 - #:log4cl) + #:log4cl + #:app-constants) (:export #:i18n-load #:_ #:parse-iso-date @@ -11,7 +12,9 @@ #:separate-files-in-web-request #:set-alert #:get-alert - #:get-and-reset-alert) + #:get-and-reset-alert + #:bool-to-checkbox + #:checkbox-to-bool) (:documentation "Utilities that do not depend on models.")) (in-package #:utils) @@ -62,3 +65,22 @@ POST request." (let ((message (get-alert))) (set-alert nil) message)) + +(defun bool-to-checkbox (value) + "Converts `VALUE' so it can populate an HTML checkbox. +It is assumed you are converting a SQLite version of a Boolean so +either 1 (true) or 0 (false). If you need a traditional Boolean value, +DO NOT USE THIS FUNCTION." + (cond ((= value 0) "off") + ((null value) "off") + (t "on"))) + +(defun checkbox-to-bool (value) + "Converts a HTML Checkbox `VALUE' to a Boolean. +The `VALUE' will either be 'on' or 'off'. 'Boolean' in this instance +is assuming you are using SQLite and need to convert `VALUE' to an +integer/number. If you are needing a traditional Boolean value, DO NOT USE +THIS FUNCTION." + (cond ((string= "checked" value) +true+) + ((string= "off" value) +false+) + ((null value) +false+)))