bsw@241: Unit = mondelefant.new_class() bsw@241: Unit.table = 'unit' bsw@241: bsw@241: Unit:add_reference{ bsw@241: mode = '1m', bsw@241: to = "Area", bsw@241: this_key = 'id', bsw@241: that_key = 'unit_id', bsw@241: ref = 'areas', bsw/jbe@1309: back_ref = 'unit', bsw/jbe@1309: default_order = "area.name, area.id" bsw@241: } bsw@241: bsw@281: Unit:add_reference{ bsw@281: mode = 'mm', bsw@281: to = "Member", bsw@281: this_key = 'id', bsw@281: that_key = 'id', bsw@281: connected_by_table = 'privilege', bsw@281: connected_by_this_key = 'unit_id', bsw@281: connected_by_that_key = 'member_id', bsw@281: ref = 'members' bsw@281: } bsw@281: bsw@525: Unit:add_reference{ bsw@525: mode = "11", bsw@525: to = mondelefant.class_prototype, bsw@525: this_key = "id", bsw@525: that_key = "unit_id", bsw@525: ref = "delegation_info", bsw@525: back_ref = "unit", bsw@525: selector_generator = function(list, options) bsw@525: assert(options.member_id, "member_id mandatory for delegation_info") bsw@525: local ids = { sep = ", " } bsw@525: for i, object in ipairs(list) do bsw@525: local id = object.id bsw@525: if id ~= nil then bsw@525: ids[#ids+1] = {"?", id} bsw@525: end bsw@525: end bsw@525: local sub_selector = Unit:get_db_conn():new_selector() bsw@525: if #ids == 0 then bsw@525: return sub_selector:empty_list_mode() bsw@525: end bsw@525: sub_selector:from("unit") bsw@525: sub_selector:add_field("unit.id", "unit_id") bsw@529: sub_selector:add_field{ '(delegation_info(?, unit.id, null, null, ?)).*', options.member_id, options.trustee_id } bsw@525: sub_selector:add_where{ 'unit.id IN ($)', ids } bsw@525: bsw@525: local selector = Unit:get_db_conn():new_selector() bsw@525: selector:add_from(sub_selector, "delegation_info") bsw@525: selector:left_join("member", "first_trustee", "first_trustee.id = delegation_info.first_trustee_id") bsw@525: selector:left_join("member", "other_trustee", "other_trustee.id = delegation_info.other_trustee_id") bsw@525: selector:add_field("delegation_info.*") bsw@525: selector:add_field("first_trustee.name", "first_trustee_name") bsw@525: selector:add_field("other_trustee.name", "other_trustee_name") bsw@525: return selector bsw@525: end bsw@525: } bsw@525: bsw@529: function Unit.list:load_delegation_info_once_for_member_id(member_id, trustee_id) bsw@525: if self._delegation_info_loaded_for_member_id ~= member_id then bsw@529: self:load("delegation_info", { member_id = member_id, trustee_id = trustee_id }) bsw@525: for i, unit in ipairs(self) do bsw@525: unit._delegation_info_loaded_for_member_id = member_id bsw@525: end bsw@525: self._delegation_info_loaded_for_member_id = member_id bsw@525: end bsw@525: end bsw@525: bsw@529: function Unit.object:load_delegation_info_once_for_member_id(member_id, trustee_id) bsw@525: if self._delegation_info_loaded_for_member_id ~= member_id then bsw@525: self:load("delegation_info", { member_id = member_id }) bsw@525: self._delegation_info_loaded_for_member_id = member_id bsw@525: end bsw@525: end bsw@525: bsw@525: bsw@525: bsw@1145: local function recursive_add_child_units(units, parent_unit) bsw@286: parent_unit.childs = {} bsw@286: for i, unit in ipairs(units) do bsw@286: if unit.parent_id == parent_unit.id then bsw@286: parent_unit.childs[#(parent_unit.childs)+1] = unit bsw@286: recursive_add_child_units(units, unit) bsw@286: end bsw@286: end bsw@286: end bsw@286: bsw@1145: local function recursive_get_child_units(units, parent_unit, depth) bsw@286: for i, unit in ipairs(parent_unit.childs) do bsw@286: unit.depth = depth bsw@286: units[#units+1] = unit bsw@286: recursive_get_child_units(units, unit, depth + 1) bsw@286: end bsw@286: end bsw@241: bsw@831: function Unit:get_flattened_tree(args) bsw@831: local units_selector = Unit:new_selector() bsw@831: :add_order_by("name") bsw@831: if not args or not args.include_inactive then bsw@831: units_selector:add_where("active") bsw@831: end bsw@831: local units = units_selector:exec() bsw@286: local unit_tree = {} bsw@286: for i, unit in ipairs(units) do bsw@286: if not unit.parent_id then bsw@286: unit_tree[#unit_tree+1] = unit bsw@286: recursive_add_child_units(units, unit) bsw@286: end bsw@286: end bsw@286: local depth = 1 bsw@286: local units = {} bsw@286: for i, unit in ipairs(unit_tree) do bsw@286: unit.depth = depth bsw@286: units[#units+1] = unit bsw@286: recursive_get_child_units(units, unit, depth + 1) bsw@286: end bsw@286: return units bsw@241: end