liquid_feedback_frontend
annotate model/unit.lua @ 217:73dbc9e2bfd4
Cummulative patch for enhancements at next generation frontend
| author | bsw |
|---|---|
| date | Sat Mar 12 19:22:50 2011 +0100 (2011-03-12) |
| parents | |
| children | 7ea52c710503 |
| rev | line source |
|---|---|
| bsw@217 | 1 Unit = mondelefant.new_class() |
| bsw@217 | 2 Unit.table = 'unit' |
| bsw@217 | 3 |
| bsw@217 | 4 function Unit:get_flattened_tree() |
| bsw@217 | 5 local units = Unit:new_selector():exec() |
| bsw@217 | 6 local tmp_list = {} |
| bsw@217 | 7 for i, unit in ipairs(units) do |
| bsw@217 | 8 tmp_list[unit.id] = unit |
| bsw@217 | 9 end |
| bsw@217 | 10 |
| bsw@217 | 11 local function get_depth(unit) |
| bsw@217 | 12 local depth |
| bsw@217 | 13 if unit.parent_id then |
| bsw@217 | 14 local parent = tmp_list[unit.parent_id] |
| bsw@217 | 15 depth = get_depth(parent) + 1 |
| bsw@217 | 16 else |
| bsw@217 | 17 depth = 0 |
| bsw@217 | 18 end |
| bsw@217 | 19 return depth |
| bsw@217 | 20 end |
| bsw@217 | 21 |
| bsw@217 | 22 |
| bsw@217 | 23 for i, unit in ipairs(units) do |
| bsw@217 | 24 local depth = get_depth(unit) |
| bsw@217 | 25 local name = "" |
| bsw@217 | 26 for i = 1, depth do |
| bsw@217 | 27 if i < depth then |
| bsw@217 | 28 name = name .. " " |
| bsw@217 | 29 else |
| bsw@217 | 30 name = name .. " ‣ " |
| bsw@217 | 31 end |
| bsw@217 | 32 end |
| bsw@217 | 33 unit.flattened_tree_name = name .. unit.name |
| bsw@217 | 34 end |
| bsw@217 | 35 |
| bsw@217 | 36 local function recurse(units) |
| bsw@217 | 37 local list = {} |
| bsw@217 | 38 for i, unit in ipairs(units) do |
| bsw@217 | 39 list[#list+1] = tmp_list[unit.id] |
| bsw@217 | 40 for i, item in ipairs(recurse( |
| bsw@217 | 41 Unit:new_selector() |
| bsw@217 | 42 :add_where{ "parent_id = ?", unit.id }:add_order_by("name"):exec() |
| bsw@217 | 43 )) do |
| bsw@217 | 44 list[#list+1] = tmp_list[item.id] |
| bsw@217 | 45 end |
| bsw@217 | 46 end |
| bsw@217 | 47 return list |
| bsw@217 | 48 end |
| bsw@217 | 49 |
| bsw@217 | 50 local list = recurse(Unit:new_selector():add_where("parent_id ISNULL"):add_order_by("name"):exec()) |
| bsw@217 | 51 |
| bsw@217 | 52 return list |
| bsw@217 | 53 end |