liquid_feedback_frontend
view model/unit.lua @ 1810:0a243b9aac6b
Added missing local statement
| author | bsw | 
|---|---|
| date | Thu Dec 02 13:22:15 2021 +0100 (2021-12-02) | 
| parents | 2a0d86117d54 | 
| children | caa6c71dd22a | 
 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   default_order = "area.name, area.id"
    12 }
    14 Unit:add_reference{
    15   mode                  = 'mm',
    16   to                    = "Member",
    17   this_key              = 'id',
    18   that_key              = 'id',
    19   connected_by_table    = 'privilege',
    20   connected_by_this_key = 'unit_id',
    21   connected_by_that_key = 'member_id',
    22   ref                   = 'members'
    23 }
    25 Unit:add_reference{
    26   mode               = "11",
    27   to                 = mondelefant.class_prototype,
    28   this_key           = "id",
    29   that_key           = "unit_id",
    30   ref                = "delegation_info",
    31   back_ref           = "unit",
    32   selector_generator = function(list, options)
    33     assert(options.member_id, "member_id mandatory for delegation_info")
    34     local ids = { sep = ", " }
    35     for i, object in ipairs(list) do
    36       local id = object.id
    37       if id ~= nil then
    38         ids[#ids+1] = {"?", id}
    39       end
    40     end
    41     local sub_selector = Unit:get_db_conn():new_selector()
    42     if #ids == 0 then
    43       return sub_selector:empty_list_mode()
    44     end
    45     sub_selector:from("unit")
    46     sub_selector:add_field("unit.id", "unit_id")
    47     sub_selector:add_field{ '(delegation_info(?, unit.id, null, null, ?)).*', options.member_id, options.trustee_id }
    48     sub_selector:add_where{ 'unit.id IN ($)', ids }
    50     local selector = Unit:get_db_conn():new_selector()
    51     selector:add_from(sub_selector, "delegation_info")
    52     selector:left_join("member", "first_trustee", "first_trustee.id = delegation_info.first_trustee_id")
    53     selector:left_join("member", "other_trustee", "other_trustee.id = delegation_info.other_trustee_id")
    54     selector:add_field("delegation_info.*")
    55     selector:add_field("first_trustee.name", "first_trustee_name")
    56     selector:add_field("other_trustee.name", "other_trustee_name")
    57     return selector
    58   end
    59 }
    61 function Unit.list:load_delegation_info_once_for_member_id(member_id, trustee_id)
    62   if self._delegation_info_loaded_for_member_id ~= member_id then
    63     self:load("delegation_info", { member_id = member_id, trustee_id = trustee_id })
    64     for i, unit in ipairs(self) do
    65       unit._delegation_info_loaded_for_member_id = member_id
    66     end
    67     self._delegation_info_loaded_for_member_id = member_id
    68   end
    69 end
    71 function Unit.object:load_delegation_info_once_for_member_id(member_id, trustee_id)
    72   if self._delegation_info_loaded_for_member_id ~= member_id then
    73     self:load("delegation_info", { member_id = member_id })
    74     self._delegation_info_loaded_for_member_id = member_id
    75   end
    76 end
    80 local function recursive_add_child_units(units, parent_unit)
    81   parent_unit.childs = {}
    82   for i, unit in ipairs(units) do
    83     if unit.parent_id == parent_unit.id then
    84       parent_unit.childs[#(parent_unit.childs)+1] = unit
    85       recursive_add_child_units(units, unit)
    86     end
    87   end
    88 end  
    90 local function recursive_get_child_units(units, parent_unit, depth)
    91   for i, unit in ipairs(parent_unit.childs) do
    92     unit.depth = depth
    93     units[#units+1] = unit
    94     recursive_get_child_units(units, unit, depth + 1)
    95   end
    96 end
    98 function Unit:get_flattened_tree(args)
    99   local units_selector = Unit:new_selector()
   100     :add_order_by("name")
   101   if not args or not args.include_inactive then
   102     units_selector:add_where("active")
   103   end
   104   if not args or not args.include_hidden then
   105     units_selector:add_where("attr->'hidden' ISNULL OR NOT (attr->'hidden' = 'true')")
   106   end
   107   local units = units_selector:exec()
   108   local unit_tree = {}
   109   for i, unit in ipairs(units) do
   110     if not unit.parent_id then
   111       unit_tree[#unit_tree+1] = unit
   112       recursive_add_child_units(units, unit)
   113     end
   114   end
   115   local depth = 1
   116   local units = {}
   117   for i, unit in ipairs(unit_tree) do
   118     unit.depth = depth
   119     units[#units+1] = unit
   120     recursive_get_child_units(units, unit, depth + 1)
   121   end
   122   return units
   123 end
