webmcp
diff framework/env/ui_deprecated/multiselect.lua @ 0:9fdfb27f8e67
Version 1.0.0
author | jbe/bsw |
---|---|
date | Sun Oct 25 12:00:00 2009 +0100 (2009-10-25) |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/framework/env/ui_deprecated/multiselect.lua Sun Oct 25 12:00:00 2009 +0100 1.3 @@ -0,0 +1,50 @@ 1.4 +function ui_deprecated.multiselect(args) 1.5 + local record = assert(slot.get_state_table(), "ui_deprecated.multiselect was not called within a form.").form_record 1.6 + local name = args.name 1.7 + local relationship = args.relationship 1.8 + 1.9 + local foreign_records = relationship.foreign_records 1.10 + 1.11 + local selector = relationship.connected_by_model:new_selector() 1.12 + selector:add_where{ relationship.connected_by_my_id .. ' = ?', record[relationship.my_id] } 1.13 + local connected_by_records = selector:exec() 1.14 + 1.15 + local connections = {} 1.16 + for i, connected_by_record in ipairs(connected_by_records) do 1.17 + connections[connected_by_record[relationship.connected_by_foreign_id]] = connected_by_record 1.18 + end 1.19 + 1.20 + local html = {} 1.21 + 1.22 + if args.type == "checkboxes" then 1.23 + for i, foreign_record in ipairs(foreign_records) do 1.24 + local selected = '' 1.25 + if connections[foreign_record[relationship.foreign_id]] then 1.26 + selected = ' checked="1"' 1.27 + end 1.28 + html[#html + 1] = '<input type="checkbox" name="' .. name .. '" value="' .. foreign_record[relationship.foreign_id] .. '"' .. selected .. '> ' .. convert.to_human(foreign_record[relationship.foreign_name], "string") .. '<br />\n' 1.29 + end 1.30 + 1.31 + else 1.32 + html[#html + 1] = '<select name="' .. name .. '" multiple="1">' 1.33 + for i, foreign_record in ipairs(foreign_records) do 1.34 + local selected = '' 1.35 + if connections[foreign_record[relationship.foreign_id]] then 1.36 + selected = ' selected="1"' 1.37 + end 1.38 + html[#html + 1] = '<option value="' .. foreign_record[relationship.foreign_id] .. '"' .. selected .. '>' .. convert.to_human(foreign_record[relationship.foreign_name], "string") .. '</option>\n' 1.39 + end 1.40 + html[#html + 1] = '</select>' 1.41 + 1.42 + end 1.43 + 1.44 + slot.put('<div class="ui_field ui_multiselect">\n') 1.45 + if args.label then 1.46 + slot.put('<div class="label">', encode.html(args.label), '</div>\n') 1.47 + end 1.48 + slot.put('<div class="value">', 1.49 + table.concat(html), 1.50 + '</div>\n', 1.51 + '</div>\n' 1.52 + ) 1.53 +end 1.54 \ No newline at end of file