liquid_feedback_frontend
view app/main/delegation/show.lua @ 1587:d5c5bf6bfda5
Show action element in issues only if necessary
| author | bsw | 
|---|---|
| date | Wed Jan 27 09:57:10 2021 +0100 (2021-01-27) | 
| parents | 8c9f7f9152ce | 
| children | dcbe505ddf24 | 
 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 = issue and "issue" or initiative and "initiative" or "index",
   140               view = issue and "show" or initiative and "show" or "index",
   141               id = initiative and initiative.id or issue and issue.id,
   142               params = { area = area and area.id, unit = unit and unit.id or area and area.unit_id }
   144             }
   145           },
   146           content = function()
   147             local records
   148             if issue then
   149               local delegate_name = ""
   150               local scope = _"no delegation set"
   151               local area_delegation = Delegation:by_pk(app.session.member_id, nil, issue.area_id)
   152               if area_delegation then
   153                 delegate_name = area_delegation.trustee and area_delegation.trustee.name or _"abandoned"
   154                 scope = _"area"
   155               else
   156                 local unit_delegation = Delegation:by_pk(app.session.member_id, issue.area.unit_id)
   157                 if unit_delegation then
   158                   delegate_name = unit_delegation.trustee.name
   159                   scope = config.single_unit_id and _"global" or _"unit"
   160                 end
   161               end
   162               local text_apply
   163               local text_abandon
   164               if config.single_unit_id then
   165                 text_apply = _("Apply global or area delegation for this issue (Currently: #{delegate_name} [#{scope}])", { delegate_name = delegate_name, scope = scope })
   166                 text_abandon = _"Abandon unit and area delegations for this issue"
   167               else
   168                 text_apply = _("Apply unit or area delegation for this issue (Currently: #{delegate_name} [#{scope}])", { delegate_name = delegate_name, scope = scope })
   169                 text_abandon = _"Abandon unit and area delegations for this issue"
   170               end
   172               records = {
   173                 { id = -1, name = text_apply },
   174                 { id = 0,  name = text_abandon }
   175               }
   176             elseif area then
   177               local delegate_name = ""
   178               local scope = _"no delegation set"
   179               local unit_delegation = Delegation:by_pk(app.session.member_id, area.unit_id)
   180               if unit_delegation then
   181                 delegate_name = unit_delegation.trustee.name
   182                 scope = config.single_unit_id and _"global" or _"unit"
   183               end
   184               local text_apply
   185               local text_abandon
   186               if config.single_unit_id then
   187                 text_apply = _("Apply global delegation for this area (Currently: #{delegate_name} [#{scope}])", { delegate_name = delegate_name, scope = scope })
   188                 text_abandon = _"Abandon global delegation for this area"
   189               else
   190                 text_apply = _("Apply unit delegation for this area (Currently: #{delegate_name} [#{scope}])", { delegate_name = delegate_name, scope = scope })
   191                 text_abandon = _"Abandon unit delegation for this area"
   192               end
   193               records = {
   194                 {
   195                   id = -1,
   196                   name = text_apply
   197                 },
   198                 {
   199                   id = 0,
   200                   name = text_abandon
   201                 }
   202               }
   204             else
   205               records = {
   206                 {
   207                   id = -1,
   208                   name = _"No delegation"
   209                 }
   210               }
   212             end
   213             -- add current trustee
   214             if current_trustee_id then
   215               records[#records+1] = {id="_", name= "--- " .. _"Current delegatee" .. " ---"}
   216               records[#records+1] = { id = current_trustee_id, name = current_trustee_name }
   217             end
   218             -- add initiative authors
   219             if initiative then
   220               records[#records+1] = {id="_", name= "--- " .. _"Initiators" .. " ---"}
   221               for i,record in ipairs(initiative.initiators) do
   222                 records[#records+1] = record.member
   223               end
   224             end
   225             -- add saved members
   226             if #contact_members > 0 then
   227               records[#records+1] = {id="_", name= "--- " .. _"Saved contacts" .. " ---"}
   228               for i, record in ipairs(contact_members) do
   229                 records[#records+1] = record
   230               end
   231             end
   233             local disabled_records = {}
   234             disabled_records["_"] = true
   235             disabled_records[app.session.member_id] = true
   237             local value = current_trustee_id
   238             if preview_trustee_id then
   239               value = preview_trustee_id
   240             end
   241             if preview_trustee_id == nil and delegation and not delegation.trustee_id then
   242               value = 0
   243             end
   245             ui.tag{ content = _"Choose your delegatee" }
   247             ui.field.select{
   248               attr = { onchange = "updateDelegationInfo();" },
   249               name = "trustee_id",
   250               foreign_records = records,
   251               foreign_id = "id",
   252               foreign_name = "name",
   253               disabled_records = disabled_records,
   254               value = value
   255             }
   256             slot.put("<br />")
   258             ui.container{ content = _"You can choose only members which you have been saved as contact before." }
   260             ui.field.hidden{ name = "preview" }
   262             slot.put("<br />")
   263             ui.tag { tag = "input", content = "", attr = { 
   264               type = "submit",
   265               value = _"Save",
   266               class = "mdl-button mdl-js-button mdl-button--raised mdl-button--colored",
   267             } }
   269             slot.put("   ")
   270             if initiative then
   271               ui.link{
   272                 module = "initiative",
   273                 view = "show",
   274                 id = initiative.id,
   275                 attr = { class = "mdl-button mdl-js-button mdl-button--underlined" },
   276                 content = function()
   277                     slot.put(_"Cancel")
   278                 end,
   279               }
   280             elseif issue then
   281               ui.link{
   282                 module = "issue",
   283                 view = "show",
   284                 id = issue.id,
   285                 attr = { class = "mdl-button mdl-js-button mdl-button--underlined" },
   286                 content = function()
   287                     slot.put(_"Cancel")
   288                 end,
   289               }
   290             elseif area then
   291               ui.link{
   292                 module = "index",
   293                 view = "index",
   294                 params = { unit = area.unit_id, area = area.id },
   295                 attr = { class = "mdl-button mdl-js-button mdl-button--underlined" },
   296                 content = function()
   297                     slot.put(_"Cancel")
   298                 end,
   299               }
   300             else
   301               ui.link{
   302                 module = "index",
   303                 view = "index",
   304                 attr = { class = "mdl-button mdl-js-button mdl-button--underlined" },
   305                 content = function()
   306                     slot.put(_"Cancel")
   307                 end,
   308               }
   309             end
   311           end
   312         }
   314       end }
   315     end }
   316   end }
   317   -- ------------------------
   319   ui.container{ attr = { class = "mdl-cell mdl-cell--12-col mdl-cell--4-col-desktop" }, content = function() 
   320     ui.container{ attr = { class = "mdl-card mdl-shadow--2dp mdl-card__fullwidth" }, content = function()
   322       ui.container{ attr = { class = "mdl-card__title mdl-card--border" }, content = function()
   323         ui.container{
   324           attr = { class = "mdl-card__title-text" },
   325           content = _"Preview of delegation"
   326         }
   327       end }
   329       ui.container{ attr = { class = "mdl-card__content mdl-card--border" }, content = function()
   330         local preview_inherit = false
   331         local tmp_preview_trustee_id = preview_trustee_id
   332         if preview_trustee_id == -1 then
   333           preview_inherit = true
   334           tmp_preview_trustee_id = nil
   335         end
   336         local delegation_chain = Member:new_selector()
   337           :add_field("delegation_chain.*")
   338           :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")
   339           :add_order_by("index")
   340           :exec()
   342         for i, record in ipairs(delegation_chain) do
   343           local style
   344           local overridden = (not issue or issue.state ~= 'voting') and record.overridden
   345           ui.sidebarSection( function ()
   346             if record.scope_in then
   347               if not overridden then
   348                 local text = _"delegated to"
   349                 ui.image{
   350                   attr = { class = "delegation_arrow", alt = text, title = text },
   351                   static = "delegation_arrow_24_vertical.png"
   352                 }
   353               else
   354                 local text = _"delegated to"
   355                 ui.image{
   356                   attr = { class = "delegation_arrow delegation_arrow_overridden", alt = text, title = text  },
   357                   static = "delegation_arrow_24_vertical.png"
   358                 }
   359               end
   360               ui.tag{
   361                 attr = { class = "delegation_scope" .. (overridden and " delegation_scope_overridden" or "") },
   362                 content = function()
   363                   if record.scope_in == "unit" then
   364                     slot.put(config.single_object_mode and _"Global delegation" or _"Unit delegation")
   365                   elseif record.scope_in == "area" then
   366                     slot.put(_"Area delegation")
   367                   elseif record.scope_in == "issue" then
   368                     slot.put(_"Issue delegation")
   369                   end
   370                 end
   371               }
   372             end
   373             ui.container{
   374               attr = { class = overridden and "delegation_overridden" or "" },
   375               content = function()
   376                 execute.view{
   377                   module = "member",
   378                   view = "_show_thumb",
   379                   params = { member = record }
   380                 }
   381               end
   382             }
   383             if issue and issue.state ~= 'voting' and record.participation and not record.overridden then
   384               ui.container{
   385                 attr = { class = "delegation_participation" },
   386                 content = function()
   387                   if i == #delegation_chain then
   388                   ui.tag{ content = _"This member is currently participating in this issue." }
   389                   else
   390                   ui.tag{ content = _"This member is participating, the remaining delegation chain is suspended during discussing." }
   391                   end
   392                 end
   393               }
   394             end
   395             slot.put("<br style='clear: left'/>")
   396           end )
   397         end
   399         if preview_trustee_id == 0 or not preview_trustee_id == null and delegation and not delegation.trustee_id then
   400           ui.image{
   401             static = "icons/16/table_go_crossed.png"
   402           }
   403           if issue_id then
   404             slot.put(_"Delegation turned off for issue")
   405           elseif area_id then
   406             slot.put(_"Delegation turned off for area")
   407           end
   408         end
   410       end }
   412     end }
   414   end }
   417 end }
