diff options
Diffstat (limited to 'page.cgi')
-rwxr-xr-x | page.cgi | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/page.cgi b/page.cgi new file mode 100755 index 0000000..6743f91 --- /dev/null +++ b/page.cgi @@ -0,0 +1,120 @@ +#!/usr/bin/env lua + +local foods = require'foods' +local qs = require'qs' + +local days = { + "mon","tue","wed","thu","fri","sat","sun" +} + + +print"Content-type: text/html" +print"" + +local prev_foods = {} +local Q = {} +if os.getenv'REQUEST_METHOD' == 'POST' then + Q = qs.parse_qs(io.read()) + local log = assert(io.open("/home/rebecca/pr/food/LOG", "a+")) + log:write(os.date("%Y-%m-%dT%H:%M:%S")) + for i=0,13 do + local prev_food = assert(tonumber(Q[tostring(i)])) + prev_foods[i] = prev_food + log:write(" "..prev_food) + end + log:write("\n") + log:close() +else + for i=0,13 do + prev_foods[i] = 1 + end +end + +local counts = {} +for i = 0,13 do + counts[i] = (counts[i] or 0) + 1 +end +local start_day = tonumber(Q.start or 1) + +local function ok(fi, si) + return true +end + +local function sel(sel_ix) + local opts = {} + for food_ix,food in ipairs(foods) do + if food_ix == prev_foods[sel_ix] then + table.insert(opts, + '<option selected value='..food_ix..'>' + .. food_ix .. ' ' .. food.name .. '</option>' + ) + elseif ok(food_ix, sel_ix) then + table.insert(opts, + '<option '..'value='..food_ix..'>' + .. food_ix .. ' ' .. food.name .. '</option>' + ) + end + end + return '<select name='..sel_ix..'>' + .. table.concat(opts,'\n') .. '</select>' +end + +local function row(n) + local d = 1+((n+start_day-1)%7) + return '<tr>' + .. '<th>' .. days[d] .. '</th>' + .. '<td>' .. sel(2*n) .. '</td>' + .. '<td>' .. sel(2*n+1) .. '</td>' + +end + + +print[[ +<form method=post> +<label for=start>start on:</label> +<select id=start name=start> +]] +for i,d in ipairs(days) do + local s = i == start_day and 'selected ' or '' + print('<option '..s..'value='..i..'>'..d..'</option>') +end +print[[ +</select> + +<table> +<tr> + <th>\</th> + <th>lunch</th> + <th>dinner</th> +</tr> +]] +for n=0,6 do print(row(n)) end +print[[ +</table> +<input type=submit value=check name=submit> +<br> +<hr> +]] + +local ings = {} +for _,fx in pairs(prev_foods) do + local food = foods[fx] + for k,v in pairs(foods[fx].ing) do + ings[k] = (ings[k] or 0) + v + end +end +local i2 = {} +for k,v in pairs(ings) do + table.insert(i2,k) +end +table.sort(i2) +print'<dl>' + +for _,g in ipairs(i2) do + print('<dt>'..g..'</dt>') + print('<dd>'..ings[g]..'x</dd>') +end +print'<hr>' +for _,g in ipairs(i2) do + print(g..', ') +end |