rev |
line source |
bsw@241
|
1 Unit = mondelefant.new_class()
|
bsw@241
|
2 Unit.table = 'unit'
|
bsw@241
|
3
|
bsw@241
|
4 Unit:add_reference{
|
bsw@241
|
5 mode = '1m',
|
bsw@241
|
6 to = "Area",
|
bsw@241
|
7 this_key = 'id',
|
bsw@241
|
8 that_key = 'unit_id',
|
bsw@241
|
9 ref = 'areas',
|
bsw/jbe@1309
|
10 back_ref = 'unit',
|
bsw/jbe@1309
|
11 default_order = "area.name, area.id"
|
bsw@241
|
12 }
|
bsw@241
|
13
|
bsw@281
|
14 Unit:add_reference{
|
bsw@281
|
15 mode = 'mm',
|
bsw@281
|
16 to = "Member",
|
bsw@281
|
17 this_key = 'id',
|
bsw@281
|
18 that_key = 'id',
|
bsw@281
|
19 connected_by_table = 'privilege',
|
bsw@281
|
20 connected_by_this_key = 'unit_id',
|
bsw@281
|
21 connected_by_that_key = 'member_id',
|
bsw@281
|
22 ref = 'members'
|
bsw@281
|
23 }
|
bsw@281
|
24
|
bsw@525
|
25 Unit:add_reference{
|
bsw@525
|
26 mode = "11",
|
bsw@525
|
27 to = mondelefant.class_prototype,
|
bsw@525
|
28 this_key = "id",
|
bsw@525
|
29 that_key = "unit_id",
|
bsw@525
|
30 ref = "delegation_info",
|
bsw@525
|
31 back_ref = "unit",
|
bsw@525
|
32 selector_generator = function(list, options)
|
bsw@525
|
33 assert(options.member_id, "member_id mandatory for delegation_info")
|
bsw@525
|
34 local ids = { sep = ", " }
|
bsw@525
|
35 for i, object in ipairs(list) do
|
bsw@525
|
36 local id = object.id
|
bsw@525
|
37 if id ~= nil then
|
bsw@525
|
38 ids[#ids+1] = {"?", id}
|
bsw@525
|
39 end
|
bsw@525
|
40 end
|
bsw@525
|
41 local sub_selector = Unit:get_db_conn():new_selector()
|
bsw@525
|
42 if #ids == 0 then
|
bsw@525
|
43 return sub_selector:empty_list_mode()
|
bsw@525
|
44 end
|
bsw@525
|
45 sub_selector:from("unit")
|
bsw@525
|
46 sub_selector:add_field("unit.id", "unit_id")
|
bsw@529
|
47 sub_selector:add_field{ '(delegation_info(?, unit.id, null, null, ?)).*', options.member_id, options.trustee_id }
|
bsw@525
|
48 sub_selector:add_where{ 'unit.id IN ($)', ids }
|
bsw@525
|
49
|
bsw@525
|
50 local selector = Unit:get_db_conn():new_selector()
|
bsw@525
|
51 selector:add_from(sub_selector, "delegation_info")
|
bsw@525
|
52 selector:left_join("member", "first_trustee", "first_trustee.id = delegation_info.first_trustee_id")
|
bsw@525
|
53 selector:left_join("member", "other_trustee", "other_trustee.id = delegation_info.other_trustee_id")
|
bsw@525
|
54 selector:add_field("delegation_info.*")
|
bsw@525
|
55 selector:add_field("first_trustee.name", "first_trustee_name")
|
bsw@525
|
56 selector:add_field("other_trustee.name", "other_trustee_name")
|
bsw@525
|
57 return selector
|
bsw@525
|
58 end
|
bsw@525
|
59 }
|
bsw@525
|
60
|
bsw@529
|
61 function Unit.list:load_delegation_info_once_for_member_id(member_id, trustee_id)
|
bsw@525
|
62 if self._delegation_info_loaded_for_member_id ~= member_id then
|
bsw@529
|
63 self:load("delegation_info", { member_id = member_id, trustee_id = trustee_id })
|
bsw@525
|
64 for i, unit in ipairs(self) do
|
bsw@525
|
65 unit._delegation_info_loaded_for_member_id = member_id
|
bsw@525
|
66 end
|
bsw@525
|
67 self._delegation_info_loaded_for_member_id = member_id
|
bsw@525
|
68 end
|
bsw@525
|
69 end
|
bsw@525
|
70
|
bsw@529
|
71 function Unit.object:load_delegation_info_once_for_member_id(member_id, trustee_id)
|
bsw@525
|
72 if self._delegation_info_loaded_for_member_id ~= member_id then
|
bsw@525
|
73 self:load("delegation_info", { member_id = member_id })
|
bsw@525
|
74 self._delegation_info_loaded_for_member_id = member_id
|
bsw@525
|
75 end
|
bsw@525
|
76 end
|
bsw@525
|
77
|
bsw@525
|
78
|
bsw@525
|
79
|
bsw@1145
|
80 local function recursive_add_child_units(units, parent_unit)
|
bsw@286
|
81 parent_unit.childs = {}
|
bsw@286
|
82 for i, unit in ipairs(units) do
|
bsw@286
|
83 if unit.parent_id == parent_unit.id then
|
bsw@286
|
84 parent_unit.childs[#(parent_unit.childs)+1] = unit
|
bsw@286
|
85 recursive_add_child_units(units, unit)
|
bsw@286
|
86 end
|
bsw@286
|
87 end
|
bsw@286
|
88 end
|
bsw@286
|
89
|
bsw@1145
|
90 local function recursive_get_child_units(units, parent_unit, depth)
|
bsw@286
|
91 for i, unit in ipairs(parent_unit.childs) do
|
bsw@286
|
92 unit.depth = depth
|
bsw@286
|
93 units[#units+1] = unit
|
bsw@286
|
94 recursive_get_child_units(units, unit, depth + 1)
|
bsw@286
|
95 end
|
bsw@286
|
96 end
|
bsw@241
|
97
|
bsw@831
|
98 function Unit:get_flattened_tree(args)
|
bsw@831
|
99 local units_selector = Unit:new_selector()
|
bsw@831
|
100 :add_order_by("name")
|
bsw@831
|
101 if not args or not args.include_inactive then
|
bsw@831
|
102 units_selector:add_where("active")
|
bsw@831
|
103 end
|
bsw@1504
|
104 if not args or not args.include_hidden then
|
bsw@1504
|
105 units_selector:add_where("attr->'hidden' ISNULL OR NOT (attr->'hidden' = 'true')")
|
bsw@1504
|
106 end
|
bsw@1831
|
107 if args and args.member_id then
|
bsw@1828
|
108 units_selector
|
bsw@1828
|
109 :left_join("privilege", nil, { "privilege.member_id = ? AND privilege.unit_id = unit.id", args.member_id })
|
bsw@1828
|
110 :add_field("privilege.voting_right", "voting_right")
|
bsw@1828
|
111 end
|
bsw@831
|
112 local units = units_selector:exec()
|
bsw@286
|
113 local unit_tree = {}
|
bsw@286
|
114 for i, unit in ipairs(units) do
|
bsw@286
|
115 if not unit.parent_id then
|
bsw@286
|
116 unit_tree[#unit_tree+1] = unit
|
bsw@286
|
117 recursive_add_child_units(units, unit)
|
bsw@286
|
118 end
|
bsw@286
|
119 end
|
bsw@286
|
120 local depth = 1
|
bsw@286
|
121 local units = {}
|
bsw@286
|
122 for i, unit in ipairs(unit_tree) do
|
bsw@286
|
123 unit.depth = depth
|
bsw@286
|
124 units[#units+1] = unit
|
bsw@286
|
125 recursive_get_child_units(units, unit, depth + 1)
|
bsw@286
|
126 end
|
bsw@286
|
127 return units
|
bsw@241
|
128 end
|