liquid_feedback_frontend
view model/unit.lua @ 400:08a6b6e9de84
Translations
| author | bsw | 
|---|---|
| date | Wed Mar 07 18:42:38 2012 +0100 (2012-03-07) | 
| parents | c587d8762e62 | 
| children | 63d6549cc00b | 
 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 function recursive_add_child_units(units, parent_unit)
    25   parent_unit.childs = {}
    26   for i, unit in ipairs(units) do
    27     if unit.parent_id == parent_unit.id then
    28       parent_unit.childs[#(parent_unit.childs)+1] = unit
    29       recursive_add_child_units(units, unit)
    30     end
    31   end
    32 end  
    34 function recursive_get_child_units(units, parent_unit, depth)
    35   for i, unit in ipairs(parent_unit.childs) do
    36     unit.depth = depth
    37     units[#units+1] = unit
    38     recursive_get_child_units(units, unit, depth + 1)
    39   end
    40 end
    42 function Unit:get_flattened_tree()
    43   local units = Unit:new_selector():add_order_by("name"):exec()
    44   local unit_tree = {}
    45   for i, unit in ipairs(units) do
    46     if not unit.parent_id then
    47       unit_tree[#unit_tree+1] = unit
    48       recursive_add_child_units(units, unit)
    49     end
    50   end
    51   local depth = 1
    52   local units = {}
    53   for i, unit in ipairs(unit_tree) do
    54     unit.depth = depth
    55     units[#units+1] = unit
    56     recursive_get_child_units(units, unit, depth + 1)
    57   end
    58   return units
    59 end
