liquid_feedback_frontend

view model/unit.lua @ 1145:904f6807f7fa

Added support for upcoming moonbridge bases WebMCP
author bsw
date Sat Mar 21 15:26:39 2015 +0100 (2015-03-21)
parents e3c2007828a1
children 32cc544d5a5b
line source
1 Unit = mondelefant.new_class()
2 Unit.table = 'unit'
4 Unit:add_reference{
5 mode = '1m',
6 to = "Area",
7 this_key = 'id',
8 that_key = 'unit_id',
9 ref = 'areas',
10 back_ref = 'unit'
11 }
13 Unit:add_reference{
14 mode = 'mm',
15 to = "Member",
16 this_key = 'id',
17 that_key = 'id',
18 connected_by_table = 'privilege',
19 connected_by_this_key = 'unit_id',
20 connected_by_that_key = 'member_id',
21 ref = 'members'
22 }
24 Unit:add_reference{
25 mode = "11",
26 to = mondelefant.class_prototype,
27 this_key = "id",
28 that_key = "unit_id",
29 ref = "delegation_info",
30 back_ref = "unit",
31 selector_generator = function(list, options)
32 assert(options.member_id, "member_id mandatory for delegation_info")
33 local ids = { sep = ", " }
34 for i, object in ipairs(list) do
35 local id = object.id
36 if id ~= nil then
37 ids[#ids+1] = {"?", id}
38 end
39 end
40 local sub_selector = Unit:get_db_conn():new_selector()
41 if #ids == 0 then
42 return sub_selector:empty_list_mode()
43 end
44 sub_selector:from("unit")
45 sub_selector:add_field("unit.id", "unit_id")
46 sub_selector:add_field{ '(delegation_info(?, unit.id, null, null, ?)).*', options.member_id, options.trustee_id }
47 sub_selector:add_where{ 'unit.id IN ($)', ids }
49 local selector = Unit:get_db_conn():new_selector()
50 selector:add_from(sub_selector, "delegation_info")
51 selector:left_join("member", "first_trustee", "first_trustee.id = delegation_info.first_trustee_id")
52 selector:left_join("member", "other_trustee", "other_trustee.id = delegation_info.other_trustee_id")
53 selector:add_field("delegation_info.*")
54 selector:add_field("first_trustee.name", "first_trustee_name")
55 selector:add_field("other_trustee.name", "other_trustee_name")
56 return selector
57 end
58 }
60 function Unit.list:load_delegation_info_once_for_member_id(member_id, trustee_id)
61 if self._delegation_info_loaded_for_member_id ~= member_id then
62 self:load("delegation_info", { member_id = member_id, trustee_id = trustee_id })
63 for i, unit in ipairs(self) do
64 unit._delegation_info_loaded_for_member_id = member_id
65 end
66 self._delegation_info_loaded_for_member_id = member_id
67 end
68 end
70 function Unit.object:load_delegation_info_once_for_member_id(member_id, trustee_id)
71 if self._delegation_info_loaded_for_member_id ~= member_id then
72 self:load("delegation_info", { member_id = member_id })
73 self._delegation_info_loaded_for_member_id = member_id
74 end
75 end
79 local function recursive_add_child_units(units, parent_unit)
80 parent_unit.childs = {}
81 for i, unit in ipairs(units) do
82 if unit.parent_id == parent_unit.id then
83 parent_unit.childs[#(parent_unit.childs)+1] = unit
84 recursive_add_child_units(units, unit)
85 end
86 end
87 end
89 local function recursive_get_child_units(units, parent_unit, depth)
90 for i, unit in ipairs(parent_unit.childs) do
91 unit.depth = depth
92 units[#units+1] = unit
93 recursive_get_child_units(units, unit, depth + 1)
94 end
95 end
97 function Unit:get_flattened_tree(args)
98 local units_selector = Unit:new_selector()
99 :add_order_by("name")
100 if not args or not args.include_inactive then
101 units_selector:add_where("active")
102 end
103 local units = units_selector:exec()
104 local unit_tree = {}
105 for i, unit in ipairs(units) do
106 if not unit.parent_id then
107 unit_tree[#unit_tree+1] = unit
108 recursive_add_child_units(units, unit)
109 end
110 end
111 local depth = 1
112 local units = {}
113 for i, unit in ipairs(unit_tree) do
114 unit.depth = depth
115 units[#units+1] = unit
116 recursive_get_child_units(units, unit, depth + 1)
117 end
118 return units
119 end

Impressum / About Us