| 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@525
 | 
    24 Unit:add_reference{
 | 
| 
bsw@525
 | 
    25   mode               = "11",
 | 
| 
bsw@525
 | 
    26   to                 = mondelefant.class_prototype,
 | 
| 
bsw@525
 | 
    27   this_key           = "id",
 | 
| 
bsw@525
 | 
    28   that_key           = "unit_id",
 | 
| 
bsw@525
 | 
    29   ref                = "delegation_info",
 | 
| 
bsw@525
 | 
    30   back_ref           = "unit",
 | 
| 
bsw@525
 | 
    31   selector_generator = function(list, options)
 | 
| 
bsw@525
 | 
    32     assert(options.member_id, "member_id mandatory for delegation_info")
 | 
| 
bsw@525
 | 
    33     local ids = { sep = ", " }
 | 
| 
bsw@525
 | 
    34     for i, object in ipairs(list) do
 | 
| 
bsw@525
 | 
    35       local id = object.id
 | 
| 
bsw@525
 | 
    36       if id ~= nil then
 | 
| 
bsw@525
 | 
    37         ids[#ids+1] = {"?", id}
 | 
| 
bsw@525
 | 
    38       end
 | 
| 
bsw@525
 | 
    39     end
 | 
| 
bsw@525
 | 
    40     local sub_selector = Unit:get_db_conn():new_selector()
 | 
| 
bsw@525
 | 
    41     if #ids == 0 then
 | 
| 
bsw@525
 | 
    42       return sub_selector:empty_list_mode()
 | 
| 
bsw@525
 | 
    43     end
 | 
| 
bsw@525
 | 
    44     sub_selector:from("unit")
 | 
| 
bsw@525
 | 
    45     sub_selector:add_field("unit.id", "unit_id")
 | 
| 
bsw@529
 | 
    46     sub_selector:add_field{ '(delegation_info(?, unit.id, null, null, ?)).*', options.member_id, options.trustee_id }
 | 
| 
bsw@525
 | 
    47     sub_selector:add_where{ 'unit.id IN ($)', ids }
 | 
| 
bsw@525
 | 
    48 
 | 
| 
bsw@525
 | 
    49     local selector = Unit:get_db_conn():new_selector()
 | 
| 
bsw@525
 | 
    50     selector:add_from(sub_selector, "delegation_info")
 | 
| 
bsw@525
 | 
    51     selector:left_join("member", "first_trustee", "first_trustee.id = delegation_info.first_trustee_id")
 | 
| 
bsw@525
 | 
    52     selector:left_join("member", "other_trustee", "other_trustee.id = delegation_info.other_trustee_id")
 | 
| 
bsw@525
 | 
    53     selector:add_field("delegation_info.*")
 | 
| 
bsw@525
 | 
    54     selector:add_field("first_trustee.name", "first_trustee_name")
 | 
| 
bsw@525
 | 
    55     selector:add_field("other_trustee.name", "other_trustee_name")
 | 
| 
bsw@525
 | 
    56     return selector
 | 
| 
bsw@525
 | 
    57   end
 | 
| 
bsw@525
 | 
    58 }
 | 
| 
bsw@525
 | 
    59 
 | 
| 
bsw@529
 | 
    60 function Unit.list:load_delegation_info_once_for_member_id(member_id, trustee_id)
 | 
| 
bsw@525
 | 
    61   if self._delegation_info_loaded_for_member_id ~= member_id then
 | 
| 
bsw@529
 | 
    62     self:load("delegation_info", { member_id = member_id, trustee_id = trustee_id })
 | 
| 
bsw@525
 | 
    63     for i, unit in ipairs(self) do
 | 
| 
bsw@525
 | 
    64       unit._delegation_info_loaded_for_member_id = member_id
 | 
| 
bsw@525
 | 
    65     end
 | 
| 
bsw@525
 | 
    66     self._delegation_info_loaded_for_member_id = member_id
 | 
| 
bsw@525
 | 
    67   end
 | 
| 
bsw@525
 | 
    68 end
 | 
| 
bsw@525
 | 
    69 
 | 
| 
bsw@529
 | 
    70 function Unit.object:load_delegation_info_once_for_member_id(member_id, trustee_id)
 | 
| 
bsw@525
 | 
    71   if self._delegation_info_loaded_for_member_id ~= member_id then
 | 
| 
bsw@525
 | 
    72     self:load("delegation_info", { member_id = member_id })
 | 
| 
bsw@525
 | 
    73     self._delegation_info_loaded_for_member_id = member_id
 | 
| 
bsw@525
 | 
    74   end
 | 
| 
bsw@525
 | 
    75 end
 | 
| 
bsw@525
 | 
    76 
 | 
| 
bsw@525
 | 
    77 
 | 
| 
bsw@525
 | 
    78 
 | 
| 
bsw@286
 | 
    79 function recursive_add_child_units(units, parent_unit)
 | 
| 
bsw@286
 | 
    80   parent_unit.childs = {}
 | 
| 
bsw@286
 | 
    81   for i, unit in ipairs(units) do
 | 
| 
bsw@286
 | 
    82     if unit.parent_id == parent_unit.id then
 | 
| 
bsw@286
 | 
    83       parent_unit.childs[#(parent_unit.childs)+1] = unit
 | 
| 
bsw@286
 | 
    84       recursive_add_child_units(units, unit)
 | 
| 
bsw@286
 | 
    85     end
 | 
| 
bsw@286
 | 
    86   end
 | 
| 
bsw@286
 | 
    87 end  
 | 
| 
bsw@286
 | 
    88 
 | 
| 
bsw@286
 | 
    89 function recursive_get_child_units(units, parent_unit, depth)
 | 
| 
bsw@286
 | 
    90   for i, unit in ipairs(parent_unit.childs) do
 | 
| 
bsw@286
 | 
    91     unit.depth = depth
 | 
| 
bsw@286
 | 
    92     units[#units+1] = unit
 | 
| 
bsw@286
 | 
    93     recursive_get_child_units(units, unit, depth + 1)
 | 
| 
bsw@286
 | 
    94   end
 | 
| 
bsw@286
 | 
    95 end
 | 
| 
bsw@241
 | 
    96 
 | 
| 
bsw@831
 | 
    97 function Unit:get_flattened_tree(args)
 | 
| 
bsw@831
 | 
    98   local units_selector = Unit:new_selector()
 | 
| 
bsw@831
 | 
    99     :add_order_by("name")
 | 
| 
bsw@831
 | 
   100   if not args or not args.include_inactive then
 | 
| 
bsw@831
 | 
   101     units_selector:add_where("active")
 | 
| 
bsw@831
 | 
   102   end
 | 
| 
bsw@831
 | 
   103   local units = units_selector:exec()
 | 
| 
bsw@286
 | 
   104   local unit_tree = {}
 | 
| 
bsw@286
 | 
   105   for i, unit in ipairs(units) do
 | 
| 
bsw@286
 | 
   106     if not unit.parent_id then
 | 
| 
bsw@286
 | 
   107       unit_tree[#unit_tree+1] = unit
 | 
| 
bsw@286
 | 
   108       recursive_add_child_units(units, unit)
 | 
| 
bsw@286
 | 
   109     end
 | 
| 
bsw@286
 | 
   110   end
 | 
| 
bsw@286
 | 
   111   local depth = 1
 | 
| 
bsw@286
 | 
   112   local units = {}
 | 
| 
bsw@286
 | 
   113   for i, unit in ipairs(unit_tree) do
 | 
| 
bsw@286
 | 
   114     unit.depth = depth
 | 
| 
bsw@286
 | 
   115     units[#units+1] = unit
 | 
| 
bsw@286
 | 
   116     recursive_get_child_units(units, unit, depth + 1)
 | 
| 
bsw@286
 | 
   117   end
 | 
| 
bsw@286
 | 
   118   return units
 | 
| 
bsw@241
 | 
   119 end
 |