From 02082d03de31af25beed17cf3d61ca513ea6f885 Mon Sep 17 00:00:00 2001 From: Ted Trask Date: Sat, 12 Jul 2008 15:57:36 +0000 Subject: Fixed redirect_to_referrer with GET data. Moved handling of multi select from acf_www-controller to controllerfunctions. Fixed form ids containing '.'. Added new handle_clientdata function to controllerfunctions. Modified password-controller to use handle_clientdata for multi select to work.k git-svn-id: svn://svn.alpinelinux.org/acf/core/trunk@1299 ab2d0c66-481e-0410-8bed-d214d4d58bed --- app/acf-util/password-controller.lua | 39 +++++++++++++---------- app/acf_www-controller.lua | 16 +--------- lib/controllerfunctions.lua | 61 ++++++++++++++++++++++++++---------- 3 files changed, 69 insertions(+), 47 deletions(-) diff --git a/app/acf-util/password-controller.lua b/app/acf-util/password-controller.lua index b248808..20f04ef 100755 --- a/app/acf-util/password-controller.lua +++ b/app/acf-util/password-controller.lua @@ -1,4 +1,5 @@ module(..., package.seeall) +require("controllerfunctions") default_action = "editme" @@ -8,20 +9,26 @@ end function editme(self) local output = self.model.read_user(self, self.sessiondata.userinfo.userid) + if clientdata.Save then -- just to make sure can't modify any other user from this action self.clientdata.userid = self.sessiondata.userinfo.userid -- As a special case for update_user, settings that don't change are nil self.clientdata.roles = nil + output.value.roles.value = nil -- if password is blank, don't update it or require it - if self.clientdata.password == "" then self.clientdata.password = nil end - if self.clientdata.password_confirm == "" then self.clientdata.password_confirm = nil end - - for name,value in pairs(output.value) do - value.value = self.clientdata[name] + if not self.clientdata.password or self.clientdata.password == "" then + self.clientdata.password = nil + output.value.password.value = nil + end + if not self.clientdata.password_confirm or self.clientdata.password_confirm == "" then + self.clientdata.password_confirm = nil + output.value.password_confirm.value = nil end + controllerfunctions.handle_clientdata(output, clientdata) + -- Update userinfo output = self.model.update_user(self, output) if not output.errtxt then @@ -34,7 +41,7 @@ function editme(self) -- Don't allow changing of roles for yourself output.value.roles = nil - + output.type = "form" output.label = "Edit My Settings" output.option = "Save" @@ -46,13 +53,17 @@ function edituser(self) if self.clientdata.Save then -- As a special case for update_user, settings that don't change are nil -- if password is blank, don't update it or require it - if self.clientdata.password == "" then self.clientdata.password = nil end - if self.clientdata.password_confirm == "" then self.clientdata.password_confirm = nil end - - for name,value in pairs(output.value) do - value.value = self.clientdata[name] + if not self.clientdata.password or self.clientdata.password == "" then + self.clientdata.password = nil + output.value.password.value = nil + end + if not self.clientdata.password_confirm or self.clientdata.password_confirm == "" then + self.clientdata.password_confirm = nil + output.value.password_confirm.value = nil end + controllerfunctions.handle_clientdata(output, clientdata) + -- Update userinfo output = self.model.update_user(self, output) if not output.errtxt then @@ -72,11 +83,7 @@ end function newuser(self) local output = self.model.read_user(self) if self.clientdata.Save then - for name,value in pairs(output.value) do - if self.clientdata[name] then - value.value = self.clientdata[name] - end - end + controllerfunctions.handle_clientdata(output, clientdata) -- Update userinfo output = self.model.create_user(self, output) diff --git a/app/acf_www-controller.lua b/app/acf_www-controller.lua index ce58893..cd8a49f 100644 --- a/app/acf_www-controller.lua +++ b/app/acf_www-controller.lua @@ -221,20 +221,6 @@ mvc.on_load = function (self, parent) self.clientdata = FORM self.conf.clientip = ENV.REMOTE_ADDR - -- FIXME this is because multi selects don't work in haserl - for name,oldtable in pairs(self.clientdata) do - if type(oldtable) == "table" then - -- Assume it's a sparse array, and remove blanks - local newtable={} - for x=1,table.maxn(oldtable) do - if oldtable[x] then - newtable[#newtable + 1] = oldtable[x] - end - end - self.clientdata[name] = newtable - end - end - parent_exception_handler = parent.exception_handler -- this sets the package path for us and our children @@ -488,7 +474,7 @@ redirect_to_referrer = function(self, result) -- might not have view. So redirect to default action for this controller. self:redirect() else - local prefix, controller, action = self.parse_path_info(ENV.HTTP_REFERER) + local prefix, controller, action = self.parse_path_info(ENV.HTTP_REFERER:gsub("%?.*", "")) if controller ~= self.conf.controller or action ~= self.conf.action then self.sessiondata[self.conf.action.."result"] = result error({type="redir_to_referrer"}) diff --git a/lib/controllerfunctions.lua b/lib/controllerfunctions.lua index ec62fee..5c3e559 100644 --- a/lib/controllerfunctions.lua +++ b/lib/controllerfunctions.lua @@ -1,25 +1,54 @@ module(..., package.seeall) +function handle_clientdata(form, clientdata) + form.errtxt = nil + for name,value in pairs(form.value) do + value.errtxt = nil + if name:find("%.") then + -- If the name has a '.' in it, haserl will interpret it as a table + local actualval = clientdata + for entry in name:gmatch("[^%.]+") do + if tonumber(entry) then + actualval = actualval[tonumber(entry)] + else + actualval = actualval[entry] + end + if not actualval then break end + end + clientdata[name] = actualval + end + if value.type == "boolean" then + value.value = (clientdata[name] ~= nil) + elseif value.type == "multi" then + -- FIXME this is because multi selects don't work in haserl + local oldtable = clientdata[name] or {} + -- Assume it's a sparse array, and remove blanks + local newtable={} + for x=1,table.maxn(oldtable) do + if oldtable[x] then + newtable[#newtable + 1] = oldtable[x] + end + end + value.value = newtable + elseif value.type == "list" then + value.value = {} + if clientdata[name] and clientdata[name] ~= "" then + for ip in string.gmatch(clientdata[name].."\n", "%s*(%S[^\n]*%S)%s*\n") do + table.insert(value.value, ip) + end + end + else + value.value = clientdata[name] or value.value + end + end +end + function handle_form(self, getFunction, setFunction, clientdata, option, label, descr, redirectOnSuccess) local form = getFunction() if clientdata[option] then - form.errtxt = nil - for name,value in pairs(form.value) do - value.errtxt = nil - if value.type == "boolean" then - value.value = (clientdata[name] ~= nil) - elseif value.type == "list" then - value.value = {} - if clientdata[name] and clientdata[name] ~= "" then - for ip in string.gmatch(clientdata[name].."\n", "%s*(%S[^\n]*%S)%s*\n") do - table.insert(value.value, ip) - end - end - else - value.value = clientdata[name] or value.value - end - end + handle_clientdata(form, clientdata) + form = setFunction(form) if not form.errtxt and descr then form.descr = descr -- cgit v1.2.3