annotate model/unit.lua @ 310:b520779a23c4
First part of editing privileges for members
author |
bsw |
date |
Mon Feb 27 16:18:45 2012 +0100 (2012-02-27) |
parents |
c587d8762e62 |
children |
63d6549cc00b |
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@241
|
10 back_ref = 'unit'
|
bsw@241
|
11 }
|
bsw@241
|
12
|
bsw@281
|
13 Unit:add_reference{
|
bsw@281
|
14 mode = 'mm',
|
bsw@281
|
15 to = "Member",
|
bsw@281
|
16 this_key = 'id',
|
bsw@281
|
17 that_key = 'id',
|
bsw@281
|
18 connected_by_table = 'privilege',
|
bsw@281
|
19 connected_by_this_key = 'unit_id',
|
bsw@281
|
20 connected_by_that_key = 'member_id',
|
bsw@281
|
21 ref = 'members'
|
bsw@281
|
22 }
|
bsw@281
|
23
|
bsw@286
|
24 function recursive_add_child_units(units, parent_unit)
|
bsw@286
|
25 parent_unit.childs = {}
|
bsw@286
|
26 for i, unit in ipairs(units) do
|
bsw@286
|
27 if unit.parent_id == parent_unit.id then
|
bsw@286
|
28 parent_unit.childs[#(parent_unit.childs)+1] = unit
|
bsw@286
|
29 recursive_add_child_units(units, unit)
|
bsw@286
|
30 end
|
bsw@286
|
31 end
|
bsw@286
|
32 end
|
bsw@286
|
33
|
bsw@286
|
34 function recursive_get_child_units(units, parent_unit, depth)
|
bsw@286
|
35 for i, unit in ipairs(parent_unit.childs) do
|
bsw@286
|
36 unit.depth = depth
|
bsw@286
|
37 units[#units+1] = unit
|
bsw@286
|
38 recursive_get_child_units(units, unit, depth + 1)
|
bsw@286
|
39 end
|
bsw@286
|
40 end
|
bsw@241
|
41
|
bsw@286
|
42 function Unit:get_flattened_tree()
|
bsw@286
|
43 local units = Unit:new_selector():add_order_by("name"):exec()
|
bsw@286
|
44 local unit_tree = {}
|
bsw@286
|
45 for i, unit in ipairs(units) do
|
bsw@286
|
46 if not unit.parent_id then
|
bsw@286
|
47 unit_tree[#unit_tree+1] = unit
|
bsw@286
|
48 recursive_add_child_units(units, unit)
|
bsw@286
|
49 end
|
bsw@286
|
50 end
|
bsw@286
|
51 local depth = 1
|
bsw@286
|
52 local units = {}
|
bsw@286
|
53 for i, unit in ipairs(unit_tree) do
|
bsw@286
|
54 unit.depth = depth
|
bsw@286
|
55 units[#units+1] = unit
|
bsw@286
|
56 recursive_get_child_units(units, unit, depth + 1)
|
bsw@286
|
57 end
|
bsw@286
|
58 return units
|
bsw@241
|
59 end
|