liquid_feedback_frontend
view app/main/delegation/show.lua @ 1352:07e320676bf3
Update unit privileges during registration update
| author | bsw | 
|---|---|
| date | Mon Aug 06 13:27:22 2018 +0200 (2018-08-06) | 
| parents | 32cc544d5a5b | 
| children | 8c9f7f9152ce | 
 line source
     1 local voting_right_unit_id
     2 local current_trustee_id
     3 local current_trustee_name
     5 local head_text
     7 local unit = Unit:by_id(param.get("unit_id", atom.integer))
     8 local area = Area:by_id(param.get("area_id", atom.integer))
     9 local issue = Issue:by_id(param.get("issue_id", atom.integer))
    10 local initiative = Initiative:by_id(param.get("initiative_id", atom.integer))
    12 if initiative then
    13   issue = initiative.issue
    14 end
    16 if unit then
    17   unit:load_delegation_info_once_for_member_id(app.session.member_id)
    18   voting_right_unit_id = unit.id
    19   if unit.delegation_info.own_delegation_scope == 'unit' then
    20     current_trustee_id = unit.delegation_info.first_trustee_id
    21     current_trustee_name = unit.delegation_info.first_trustee_name
    22   end
    23   execute.view { module = "unit", view = "_head", params = { unit = unit } }
    24   head_text = _"Set unit delegation"
    25 end
    27 if area then
    28   area:load_delegation_info_once_for_member_id(app.session.member_id)
    29   voting_right_unit_id = area.unit_id
    30   if area.delegation_info.own_delegation_scope == 'area' then
    31     current_trustee_id = area.delegation_info.first_trustee_id
    32     current_trustee_name = area.delegation_info.first_trustee_name
    33   end
    34   execute.view { module = "area", view = "_head", params = { area = area } }
    35   head_text = _"Set area delegation"
    36 end
    38 if issue then
    39   issue:load("member_info", { member_id = app.session.member_id })
    40   voting_right_unit_id = issue.area.unit_id
    41   if issue.member_info.own_delegation_scope == 'issue' then
    42     current_trustee_id = issue.member_info.first_trustee_id
    43     current_trustee_name = issue.member_info.first_trustee_name
    44   end
    45   execute.view { module = "issue", view = "_head", params = { issue = issue } }
    46   head_text = _"Set issue delegation"
    47 end
    49 local delegation
    50 local unit_id
    51 local area_id
    52 local issue_id
    53 local initiative_id
    55 local scope = "unit"
    57 local inline = param.get("inline", atom.boolean)
    60 unit_id = param.get("unit_id", atom.integer)
    62 if param.get("initiative_id", atom.integer) then
    63   initiative_id = initiative.id
    64   issue_id = initiative.issue_id
    65   scope = "issue"
    66 end
    68 if param.get("issue_id", atom.integer) then
    69   issue_id = param.get("issue_id", atom.integer)
    70   scope = "issue"
    71 end
    73 if param.get("area_id", atom.integer) then
    74   area_id = param.get("area_id", atom.integer)
    75   scope = "area"
    76 end
    80 local delegation
    81 local issue
    83 if issue_id then
    84   issue = Issue:by_id(issue_id)
    85   delegation = Delegation:by_pk(app.session.member.id, nil, nil, issue_id)
    86   if not delegation then
    87     delegation = Delegation:by_pk(app.session.member.id, nil, issue.area_id)
    88   end
    89   if not delegation then
    90     delegation = Delegation:by_pk(app.session.member.id, issue.area.unit_id)
    91   end
    92 elseif area_id then
    93   delegation = Delegation:by_pk(app.session.member.id, nil, area_id)
    94   if not delegation then
    95     local area = Area:by_id(area_id)
    96     delegation = Delegation:by_pk(app.session.member.id, area.unit_id)
    97   end
    98 end
   100 if not delegation then
   101   delegation = Delegation:by_pk(app.session.member.id, unit_id)
   102 end
   104 local contact_members = Member:build_selector{
   105   is_contact_of_member_id = app.session.member_id,
   106   voting_right_for_unit_id = voting_right_unit_id,
   107   active = true,
   108   order = "name"
   109 }:exec()
   111 local preview_trustee_id = param.get("preview_trustee_id", atom.integer)
   113 ui.script{ static = "js/update_delegation_info.js" }
   116 ui.container{ attr = { class = "mdl-grid" }, content = function()
   117   ui.container{ attr = { class = "mdl-cell mdl-cell--8-col" }, content = function()
   118     ui.container{ attr = { class = "mdl-card mdl-card__fullwidth mdl-shadow--2dp" }, content = function()
   119       ui.container{ attr = { class = "mdl-card__title mdl-card--border" }, content = function()
   120         ui.heading { attr = { class = "mdl-card__title-text" }, level = 2, content = head_text }
   121       end }
   123       ui.container{ attr = { class = "mdl-card__content mdl-card--border" }, content = function()
   126         ui.form{
   127           attr = { class = "wide section", id = "delegationForm" },
   128           module = "delegation",
   129           action = "update",
   130           params = {
   131             unit_id = unit and unit.id or nil,
   132             area_id = area and area.id or nil,
   133             issue_id = issue and issue.id or nil,
   134             initiative_id = initiative_id
   135           },
   136           routing = {
   137             default = {
   138               mode = "redirect",
   139               module = area and "area" or initiative and "initiative" or issue and "issue" or "unit",
   140               view = "show",
   141               id = area and area.id or initiative and initiative.id or issue and issue.id or unit.id,
   142             }
   143           },
   144           content = function()
   145             local records
   146             if issue then
   147               local delegate_name = ""
   148               local scope = _"no delegation set"
   149               local area_delegation = Delegation:by_pk(app.session.member_id, nil, issue.area_id)
   150               if area_delegation then
   151                 delegate_name = area_delegation.trustee and area_delegation.trustee.name or _"abandoned"
   152                 scope = _"area"
   153               else
   154                 local unit_delegation = Delegation:by_pk(app.session.member_id, issue.area.unit_id)
   155                 if unit_delegation then
   156                   delegate_name = unit_delegation.trustee.name
   157                   scope = config.single_unit_id and _"global" or _"unit"
   158                 end
   159               end
   160               local text_apply
   161               local text_abandon
   162               if config.single_unit_id then
   163                 text_apply = _("Apply global or area delegation for this issue (Currently: #{delegate_name} [#{scope}])", { delegate_name = delegate_name, scope = scope })
   164                 text_abandon = _"Abandon unit and area delegations for this issue"
   165               else
   166                 text_apply = _("Apply unit or area delegation for this issue (Currently: #{delegate_name} [#{scope}])", { delegate_name = delegate_name, scope = scope })
   167                 text_abandon = _"Abandon unit and area delegations for this issue"
   168               end
   170               records = {
   171                 { id = -1, name = text_apply },
   172                 { id = 0,  name = text_abandon }
   173               }
   174             elseif area then
   175               local delegate_name = ""
   176               local scope = _"no delegation set"
   177               local unit_delegation = Delegation:by_pk(app.session.member_id, area.unit_id)
   178               if unit_delegation then
   179                 delegate_name = unit_delegation.trustee.name
   180                 scope = config.single_unit_id and _"global" or _"unit"
   181               end
   182               local text_apply
   183               local text_abandon
   184               if config.single_unit_id then
   185                 text_apply = _("Apply global delegation for this area (Currently: #{delegate_name} [#{scope}])", { delegate_name = delegate_name, scope = scope })
   186                 text_abandon = _"Abandon global delegation for this area"
   187               else
   188                 text_apply = _("Apply unit delegation for this area (Currently: #{delegate_name} [#{scope}])", { delegate_name = delegate_name, scope = scope })
   189                 text_abandon = _"Abandon unit delegation for this area"
   190               end
   191               records = {
   192                 {
   193                   id = -1,
   194                   name = text_apply
   195                 },
   196                 {
   197                   id = 0,
   198                   name = text_abandon
   199                 }
   200               }
   202             else
   203               records = {
   204                 {
   205                   id = -1,
   206                   name = _"No delegation"
   207                 }
   208               }
   210             end
   211             -- add current trustee
   212             if current_trustee_id then
   213               records[#records+1] = {id="_", name= "--- " .. _"Current delegatee" .. " ---"}
   214               records[#records+1] = { id = current_trustee_id, name = current_trustee_name }
   215             end
   216             -- add initiative authors
   217             if initiative then
   218               records[#records+1] = {id="_", name= "--- " .. _"Initiators" .. " ---"}
   219               for i,record in ipairs(initiative.initiators) do
   220                 records[#records+1] = record.member
   221               end
   222             end
   223             -- add saved members
   224             if #contact_members > 0 then
   225               records[#records+1] = {id="_", name= "--- " .. _"Saved contacts" .. " ---"}
   226               for i, record in ipairs(contact_members) do
   227                 records[#records+1] = record
   228               end
   229             end
   231             local disabled_records = {}
   232             disabled_records["_"] = true
   233             disabled_records[app.session.member_id] = true
   235             local value = current_trustee_id
   236             if preview_trustee_id then
   237               value = preview_trustee_id
   238             end
   239             if preview_trustee_id == nil and delegation and not delegation.trustee_id then
   240               value = 0
   241             end
   243             ui.tag{ content = _"Choose your delegatee" }
   245             ui.field.select{
   246               attr = { onchange = "updateDelegationInfo();" },
   247               name = "trustee_id",
   248               foreign_records = records,
   249               foreign_id = "id",
   250               foreign_name = "name",
   251               disabled_records = disabled_records,
   252               value = value
   253             }
   254             slot.put("<br />")
   256             ui.container{ content = _"You can choose only members which you have been saved as contact before." }
   258             ui.field.hidden{ name = "preview" }
   260             slot.put("<br />")
   261             ui.tag { tag = "input", content = "", attr = { 
   262               type = "submit",
   263               value = _"Save",
   264               class = "mdl-button mdl-js-button mdl-button--raised mdl-button--colored",
   265             } }
   267             slot.put("   ")
   268             if initiative then
   269               ui.link{
   270                 module = "initiative",
   271                 view = "show",
   272                 id = initiative.id,
   273                 attr = { class = "mdl-button mdl-js-button mdl-button--underlined" },
   274                 content = function()
   275                     slot.put(_"Cancel")
   276                 end,
   277               }
   278             elseif issue then
   279               ui.link{
   280                 module = "issue",
   281                 view = "show",
   282                 id = issue.id,
   283                 attr = { class = "mdl-button mdl-js-button mdl-button--underlined" },
   284                 content = function()
   285                     slot.put(_"Cancel")
   286                 end,
   287               }
   288             elseif area then
   289               ui.link{
   290                 module = "index",
   291                 view = "index",
   292                 params = { unit = area.unit_id, area = area.id },
   293                 attr = { class = "mdl-button mdl-js-button mdl-button--underlined" },
   294                 content = function()
   295                     slot.put(_"Cancel")
   296                 end,
   297               }
   298             else
   299               ui.link{
   300                 module = "index",
   301                 view = "index",
   302                 attr = { class = "mdl-button mdl-js-button mdl-button--underlined" },
   303                 content = function()
   304                     slot.put(_"Cancel")
   305                 end,
   306               }
   307             end
   309           end
   310         }
   312       end }
   313     end }
   314   end }
   315   -- ------------------------
   317   ui.container{ attr = { class = "mdl-cell mdl-cell--12-col mdl-cell--4-col-desktop" }, content = function() 
   318     ui.container{ attr = { class = "mdl-card mdl-shadow--2dp mdl-card__fullwidth" }, content = function()
   320       ui.container{ attr = { class = "mdl-card__title mdl-card--border" }, content = function()
   321         ui.container{
   322           attr = { class = "mdl-card__title-text" },
   323           content = _"Preview of delegation"
   324         }
   325       end }
   327       ui.container{ attr = { class = "mdl-card__content mdl-card--border" }, content = function()
   328         local preview_inherit = false
   329         local tmp_preview_trustee_id = preview_trustee_id
   330         if preview_trustee_id == -1 then
   331           preview_inherit = true
   332           tmp_preview_trustee_id = nil
   333         end
   334         local delegation_chain = Member:new_selector()
   335           :add_field("delegation_chain.*")
   336           :join({ "delegation_chain(?,?,?,?,?,?)", app.session.member.id, unit_id, area_id, issue_id, tmp_preview_trustee_id, preview_inherit }, "delegation_chain", "member.id = delegation_chain.member_id")
   337           :add_order_by("index")
   338           :exec()
   340         for i, record in ipairs(delegation_chain) do
   341           local style
   342           local overridden = (not issue or issue.state ~= 'voting') and record.overridden
   343           ui.sidebarSection( function ()
   344             if record.scope_in then
   345               if not overridden then
   346                 local text = _"delegated to"
   347                 ui.image{
   348                   attr = { class = "delegation_arrow", alt = text, title = text },
   349                   static = "delegation_arrow_24_vertical.png"
   350                 }
   351               else
   352                 local text = _"delegated to"
   353                 ui.image{
   354                   attr = { class = "delegation_arrow delegation_arrow_overridden", alt = text, title = text  },
   355                   static = "delegation_arrow_24_vertical.png"
   356                 }
   357               end
   358               ui.tag{
   359                 attr = { class = "delegation_scope" .. (overridden and " delegation_scope_overridden" or "") },
   360                 content = function()
   361                   if record.scope_in == "unit" then
   362                     slot.put(config.single_object_mode and _"Global delegation" or _"Unit delegation")
   363                   elseif record.scope_in == "area" then
   364                     slot.put(_"Area delegation")
   365                   elseif record.scope_in == "issue" then
   366                     slot.put(_"Issue delegation")
   367                   end
   368                 end
   369               }
   370             end
   371             ui.container{
   372               attr = { class = overridden and "delegation_overridden" or "" },
   373               content = function()
   374                 execute.view{
   375                   module = "member",
   376                   view = "_show_thumb",
   377                   params = { member = record }
   378                 }
   379               end
   380             }
   381             if issue and issue.state ~= 'voting' and record.participation and not record.overridden then
   382               ui.container{
   383                 attr = { class = "delegation_participation" },
   384                 content = function()
   385                   if i == #delegation_chain then
   386                   ui.tag{ content = _"This member is currently participating in this issue." }
   387                   else
   388                   ui.tag{ content = _"This member is participating, the remaining delegation chain is suspended during discussing." }
   389                   end
   390                 end
   391               }
   392             end
   393             slot.put("<br style='clear: left'/>")
   394           end )
   395         end
   397         if preview_trustee_id == 0 or not preview_trustee_id == null and delegation and not delegation.trustee_id then
   398           ui.image{
   399             static = "icons/16/table_go_crossed.png"
   400           }
   401           if issue_id then
   402             slot.put(_"Delegation turned off for issue")
   403           elseif area_id then
   404             slot.put(_"Delegation turned off for area")
   405           end
   406         end
   408       end }
   410     end }
   412   end }
   415 end }
