liquid_feedback_frontend
diff model/unit.lua @ 286:c587d8762e62
Registration process updated for Core 2.0, lockable member fields, notification settings
author | bsw |
---|---|
date | Sat Feb 25 11:51:37 2012 +0100 (2012-02-25) |
parents | b77e6a17ca77 |
children | 63d6549cc00b |
line diff
1.1 --- a/model/unit.lua Fri Feb 17 15:16:02 2012 +0100 1.2 +++ b/model/unit.lua Sat Feb 25 11:51:37 2012 +0100 1.3 @@ -21,8 +21,39 @@ 1.4 ref = 'members' 1.5 } 1.6 1.7 -function Unit:get_flattened_tree() 1.8 - -- TODO implement 1.9 +function recursive_add_child_units(units, parent_unit) 1.10 + parent_unit.childs = {} 1.11 + for i, unit in ipairs(units) do 1.12 + if unit.parent_id == parent_unit.id then 1.13 + parent_unit.childs[#(parent_unit.childs)+1] = unit 1.14 + recursive_add_child_units(units, unit) 1.15 + end 1.16 + end 1.17 +end 1.18 + 1.19 +function recursive_get_child_units(units, parent_unit, depth) 1.20 + for i, unit in ipairs(parent_unit.childs) do 1.21 + unit.depth = depth 1.22 + units[#units+1] = unit 1.23 + recursive_get_child_units(units, unit, depth + 1) 1.24 + end 1.25 +end 1.26 1.27 - return Unit:new_selector():exec() 1.28 +function Unit:get_flattened_tree() 1.29 + local units = Unit:new_selector():add_order_by("name"):exec() 1.30 + local unit_tree = {} 1.31 + for i, unit in ipairs(units) do 1.32 + if not unit.parent_id then 1.33 + unit_tree[#unit_tree+1] = unit 1.34 + recursive_add_child_units(units, unit) 1.35 + end 1.36 + end 1.37 + local depth = 1 1.38 + local units = {} 1.39 + for i, unit in ipairs(unit_tree) do 1.40 + unit.depth = depth 1.41 + units[#units+1] = unit 1.42 + recursive_get_child_units(units, unit, depth + 1) 1.43 + end 1.44 + return units 1.45 end