bsw@217: Unit = mondelefant.new_class() bsw@217: Unit.table = 'unit' bsw@217: bsw@217: function Unit:get_flattened_tree() bsw@217: local units = Unit:new_selector():exec() bsw@217: local tmp_list = {} bsw@217: for i, unit in ipairs(units) do bsw@217: tmp_list[unit.id] = unit bsw@217: end bsw@217: bsw@217: local function get_depth(unit) bsw@217: local depth bsw@217: if unit.parent_id then bsw@217: local parent = tmp_list[unit.parent_id] bsw@217: depth = get_depth(parent) + 1 bsw@217: else bsw@217: depth = 0 bsw@217: end bsw@217: return depth bsw@217: end bsw@217: bsw@217: bsw@217: for i, unit in ipairs(units) do bsw@217: local depth = get_depth(unit) bsw@217: local name = "" bsw@217: for i = 1, depth do bsw@217: if i < depth then bsw@217: name = name .. "     " bsw@217: else bsw@217: name = name .. "   ‣ " bsw@217: end bsw@217: end bsw@217: unit.flattened_tree_name = name .. unit.name bsw@217: end bsw@217: bsw@217: local function recurse(units) bsw@217: local list = {} bsw@217: for i, unit in ipairs(units) do bsw@217: list[#list+1] = tmp_list[unit.id] bsw@217: for i, item in ipairs(recurse( bsw@217: Unit:new_selector() bsw@217: :add_where{ "parent_id = ?", unit.id }:add_order_by("name"):exec() bsw@217: )) do bsw@217: list[#list+1] = tmp_list[item.id] bsw@217: end bsw@217: end bsw@217: return list bsw@217: end bsw@217: bsw@217: local list = recurse(Unit:new_selector():add_where("parent_id ISNULL"):add_order_by("name"):exec()) bsw@217: bsw@217: return list bsw@217: end bsw@218: bsw@218: function Unit.object_get:name_with_path() bsw@218: local name = "" bsw@218: if self.parent then bsw@218: name = self.parent.name_with_path .. " / " bsw@218: end bsw@218: name = name .. self.name bsw@218: return name bsw@218: end