bsw@525: local voting_right_unit_id
bsw@529: local current_trustee_id
bsw@529: local current_trustee_name
bsw@525: 
bsw@1045: local head_text
bsw@1045: 
bsw@525: local unit = Unit:by_id(param.get("unit_id", atom.integer))
bsw@1045: local area = Area:by_id(param.get("area_id", atom.integer))
bsw@1045: local issue = Issue:by_id(param.get("issue_id", atom.integer))
bsw@1045: local initiative = Initiative:by_id(param.get("initiative_id", atom.integer))
bsw@1045: 
bsw@1045: if initiative then
bsw@1045:   issue = initiative.issue
bsw@1045: end
bsw@1045: 
bsw@525: if unit then
bsw@529:   unit:load_delegation_info_once_for_member_id(app.session.member_id)
bsw@525:   voting_right_unit_id = unit.id
bsw@529:   if unit.delegation_info.own_delegation_scope == 'unit' then
bsw@529:     current_trustee_id = unit.delegation_info.first_trustee_id
bsw@529:     current_trustee_name = unit.delegation_info.first_trustee_name
bsw@529:   end
bsw@1045:   execute.view { module = "unit", view = "_head", params = { unit = unit } }
bsw@1045:   head_text = _"Set unit delegation"
bsw@525: end
bsw@525: 
bsw@525: if area then
bsw@529:   area:load_delegation_info_once_for_member_id(app.session.member_id)
bsw@525:   voting_right_unit_id = area.unit_id
bsw@563:   if area.delegation_info.own_delegation_scope == 'area' then
bsw@563:     current_trustee_id = area.delegation_info.first_trustee_id
bsw@563:     current_trustee_name = area.delegation_info.first_trustee_name
bsw@563:   end
bsw@1045:   execute.view { module = "area", view = "_head", params = { area = area } }
bsw@1045:   head_text = _"Set area delegation"
bsw@525: end
bsw@525: 
bsw@525: if issue then
bsw@551:   issue:load("member_info", { member_id = app.session.member_id })
bsw@525:   voting_right_unit_id = issue.area.unit_id
bsw@563:   if issue.member_info.own_delegation_scope == 'issue' then
bsw@563:     current_trustee_id = issue.member_info.first_trustee_id
bsw@563:     current_trustee_name = issue.member_info.first_trustee_name
bsw@563:   end
bsw@1045:   execute.view { module = "issue", view = "_head", params = { issue = issue } }
bsw@1045:   head_text = _"Set issue delegation"
bsw@1045: end
bsw@1045: 
bsw@575: local delegation
bsw@575: local unit_id
bsw@575: local area_id
bsw@575: local issue_id
bsw@575: local initiative_id
bsw@575: 
bsw@575: local scope = "unit"
bsw@575: 
bsw@1045: local inline = param.get("inline", atom.boolean)
bsw@1045: 
bsw@1045: 
bsw@575: unit_id = param.get("unit_id", atom.integer)
bsw@575: 
bsw@575: if param.get("initiative_id", atom.integer) then
bsw@1045:   initiative_id = initiative.id
bsw@575:   issue_id = initiative.issue_id
bsw@575:   scope = "issue"
bsw@575: end
bsw@575: 
bsw@575: if param.get("issue_id", atom.integer) then
bsw@575:   issue_id = param.get("issue_id", atom.integer)
bsw@575:   scope = "issue"
bsw@575: end
bsw@575: 
bsw@575: if param.get("area_id", atom.integer) then
bsw@575:   area_id = param.get("area_id", atom.integer)
bsw@575:   scope = "area"
bsw@575: end
bsw@575: 
bsw@575: 
bsw@575: 
bsw@575: local delegation
bsw@575: local issue
bsw@575: 
bsw@575: if issue_id then
bsw@575:   issue = Issue:by_id(issue_id)
bsw@575:   delegation = Delegation:by_pk(app.session.member.id, nil, nil, issue_id)
bsw@575:   if not delegation then
bsw@575:     delegation = Delegation:by_pk(app.session.member.id, nil, issue.area_id)
bsw@575:   end
bsw@575:   if not delegation then
bsw@575:     delegation = Delegation:by_pk(app.session.member.id, issue.area.unit_id)
bsw@575:   end
bsw@575: elseif area_id then
bsw@575:   delegation = Delegation:by_pk(app.session.member.id, nil, area_id)
bsw@575:   if not delegation then
bsw@575:     local area = Area:by_id(area_id)
bsw@575:     delegation = Delegation:by_pk(app.session.member.id, area.unit_id)
bsw@575:   end
bsw@575: end
bsw@575: 
bsw@575: if not delegation then
bsw@575:   delegation = Delegation:by_pk(app.session.member.id, unit_id)
bsw@575: end
bsw@525: 
bsw@529: local contact_members = Member:build_selector{
bsw@529:   is_contact_of_member_id = app.session.member_id,
bsw@529:   voting_right_for_unit_id = voting_right_unit_id,
bsw@529:   active = true,
bsw@529:   order = "name"
bsw@529: }:exec()
bsw@529: 
bsw@529: local preview_trustee_id = param.get("preview_trustee_id", atom.integer)
bsw@529: 
bsw@529: ui.script{ static = "js/update_delegation_info.js" }
bsw@529: 
bsw@1045: 
bsw@1045: ui.section( function () 
bsw@1045:   
bsw@1045:   ui.sectionHead( function ()
bsw@1045:     ui.heading{ level = 1, content = head_text }
bsw@1045:   end )
bsw@1045:   
bsw@1045:   ui.sectionRow( function ()
bsw@525: 
bsw@1045:   ui.form{
bsw@1045:     attr = { class = "wide section", id = "delegationForm" },
bsw@1045:     module = "delegation",
bsw@1045:     action = "update",
bsw@1045:     params = {
bsw@1045:       unit_id = unit and unit.id or nil,
bsw@1045:       area_id = area and area.id or nil,
bsw@1045:       issue_id = issue and issue.id or nil,
bsw@1045:       initiative_id = initiative_id
bsw@1045:     },
bsw@1045:     routing = {
bsw@1045:       default = {
bsw@1045:         mode = "redirect",
bsw@1045:         module = area and "area" or initiative and "initiative" or issue and "issue" or "unit",
bsw@1045:         view = "show",
bsw@1045:         id = area and area.id or initiative and initiative.id or issue and issue.id or unit.id,
bsw@1045:       }
bsw@1045:     },
bsw@1045:     content = function()
bsw@1145:       local records
bsw@1045:       if issue then
bsw@1045:         local delegate_name = ""
bsw@1098:         local scope = _"no delegation set"
bsw@1045:         local area_delegation = Delegation:by_pk(app.session.member_id, nil, issue.area_id)
bsw@1045:         if area_delegation then
bsw@1045:           delegate_name = area_delegation.trustee and area_delegation.trustee.name or _"abandoned"
bsw@1045:           scope = _"area"
bsw@1045:         else
bsw@1045:           local unit_delegation = Delegation:by_pk(app.session.member_id, issue.area.unit_id)
bsw@1045:           if unit_delegation then
bsw@1045:             delegate_name = unit_delegation.trustee.name
bsw@1045:             scope = config.single_unit_id and _"global" or _"unit"
bsw@1045:           end
bsw@1045:         end
bsw@1045:         local text_apply
bsw@1045:         local text_abandon
bsw@1045:         if config.single_unit_id then
bsw@1045:           text_apply = _("Apply global or area delegation for this issue (Currently: #{delegate_name} [#{scope}])", { delegate_name = delegate_name, scope = scope })
bsw@1045:           text_abandon = _"Abandon unit and area delegations for this issue"
bsw@1045:         else
bsw@1045:           text_apply = _("Apply unit or area delegation for this issue (Currently: #{delegate_name} [#{scope}])", { delegate_name = delegate_name, scope = scope })
bsw@1045:           text_abandon = _"Abandon unit and area delegations for this issue"
bsw@1045:         end
bsw@1045:         
bsw@1045:         records = {
bsw@1045:           { id = -1, name = text_apply },
bsw@1045:           { id = 0,  name = text_abandon }
bsw@1045:         }
bsw@1045:       elseif area then
bsw@1045:         local delegate_name = ""
bsw@1098:         local scope = _"no delegation set"
bsw@1045:         local unit_delegation = Delegation:by_pk(app.session.member_id, area.unit_id)
bsw@525:         if unit_delegation then
bsw@525:           delegate_name = unit_delegation.trustee.name
bsw@525:           scope = config.single_unit_id and _"global" or _"unit"
bsw@525:         end
bsw@1045:         local text_apply
bsw@1045:         local text_abandon
bsw@1045:         if config.single_unit_id then
bsw@1045:           text_apply = _("Apply global delegation for this area (Currently: #{delegate_name} [#{scope}])", { delegate_name = delegate_name, scope = scope })
bsw@1045:           text_abandon = _"Abandon global delegation for this area"
bsw@1045:         else
bsw@1045:           text_apply = _("Apply unit delegation for this area (Currently: #{delegate_name} [#{scope}])", { delegate_name = delegate_name, scope = scope })
bsw@1045:           text_abandon = _"Abandon unit delegation for this area"
bsw@1045:         end
bsw@1045:         records = {
bsw@1045:           {
bsw@1045:             id = -1,
bsw@1045:             name = text_apply
bsw@1045:           },
bsw@1045:           {
bsw@1045:             id = 0,
bsw@1045:             name = text_abandon
bsw@1045:           }
bsw@1045:         }
bsw@1045: 
bsw@525:       else
bsw@1045:         records = {
bsw@1045:           {
bsw@1045:             id = -1,
bsw@1045:             name = _"No delegation"
bsw@1045:           }
bsw@1045:         }
bsw@1045: 
bsw@525:       end
bsw@1045:       -- add current trustee
bsw@1045:       if current_trustee_id then
bsw@1045:         records[#records+1] = {id="_", name= "--- " .. _"Current delegatee" .. " ---"}
bsw@1045:         records[#records+1] = { id = current_trustee_id, name = current_trustee_name }
bsw@1045:       end
bsw@1045:       -- add initiative authors
bsw@1045:       if initiative then
bsw@1045:         records[#records+1] = {id="_", name= "--- " .. _"Initiators" .. " ---"}
bsw@1045:         for i,record in ipairs(initiative.initiators) do
bsw@1045:           records[#records+1] = record.member
bsw@1045:         end
bsw@1045:       end
bsw@1045:       -- add saved members
bsw@1045:       if #contact_members > 0 then
bsw@1045:         records[#records+1] = {id="_", name= "--- " .. _"Saved contacts" .. " ---"}
bsw@1045:         for i, record in ipairs(contact_members) do
bsw@1045:           records[#records+1] = record
bsw@1045:         end
bsw@1045:       end
bsw@1045: 
bsw@1145:       local disabled_records = {}
bsw@1045:       disabled_records["_"] = true
bsw@1045:       disabled_records[app.session.member_id] = true
bsw@1045: 
bsw@1045:       local value = current_trustee_id
bsw@1045:       if preview_trustee_id then
bsw@1045:         value = preview_trustee_id
bsw@1045:       end
bsw@1045:       if preview_trustee_id == nil and delegation and not delegation.trustee_id then
bsw@1045:         value = 0
bsw@525:       end
bsw@1045: 
bsw@1045:       ui.heading{ level = 2, content = _"Choose your delegatee" }
bsw@1045:       
bsw@1045:       ui.field.select{
bsw@1045:         attr = { onchange = "updateDelegationInfo();" },
bsw@1045:         name = "trustee_id",
bsw@1045:         foreign_records = records,
bsw@1045:         foreign_id = "id",
bsw@1045:         foreign_name = "name",
bsw@1045:         disabled_records = disabled_records,
bsw@1045:         value = value
bsw@1045:       }
bsw@1045:       
bsw@1045:       ui.container{ content = _"You can choose only members which you have been saved as contact before." }
bsw@1045: 
bsw@1045:       ui.field.hidden{ name = "preview" }
bsw@1045:       
bsw@1045:       slot.put("
")
bsw@1045:       ui.tag { tag = "input", content = "", attr = { 
bsw@1045:         type = "submit",
bsw@1045:         value = _"Save",
bsw@1045:         class = "btn btn-default",
bsw@1045:       } }
bsw@1045:         
bsw@1045:       slot.put("
")
bsw@1045:       if initiative then
bsw@1045:         ui.link{
bsw@1045:           module = "initiative",
bsw@1045:           view = "show",
bsw@1045:           id = initiative.id,
bsw@1045:           content = function()
bsw@1045:               slot.put(_"Cancel")
bsw@1045:           end,
bsw@1045:         }
bsw@1045:       elseif issue then
bsw@1045:         ui.link{
bsw@1045:           module = "issue",
bsw@1045:           view = "show",
bsw@1045:           id = issue.id,
bsw@1045:           content = function()
bsw@1045:               slot.put(_"Cancel")
bsw@1045:           end,
bsw@1045:         }
bsw@1045:       elseif area then
bsw@1045:         ui.link{
bsw@1045:           module = "area",
bsw@1045:           view = "show",
bsw@1045:           id = area.id,
bsw@1045:           content = function()
bsw@1045:               slot.put(_"Cancel")
bsw@1045:           end,
bsw@1045:         }
bsw@525:       else
bsw@1045:         ui.link{
bsw@1045:           module = "index",
bsw@1045:           view = "index",
bsw@1045:           content = function()
bsw@1045:               slot.put(_"Cancel")
bsw@1045:           end,
bsw@1045:         }
bsw@525:       end
bsw@525: 
bsw@525:     end
bsw@1045:   }
bsw@525: 
bsw@1045: end ) end )
bsw@1045: -- ------------------------
bsw@525: 
bsw@1045: ui.sidebar( "tab-members", function () 
bsw@525: 
bsw@1045: ui.sidebarHead( function ()
bsw@1045:   ui.heading { level = 1, content = _"Preview of delegation" }
bsw@1045: end )
bsw@525: 
bsw@583: local preview_inherit = false
bsw@583: local tmp_preview_trustee_id = preview_trustee_id
bsw@583: if preview_trustee_id == -1 then
bsw@583:   preview_inherit = true
bsw@583:   tmp_preview_trustee_id = nil
bsw@583: end
bsw@575: local delegation_chain = Member:new_selector()
bsw@575:   :add_field("delegation_chain.*")
bsw@583:   :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")
bsw@575:   :add_order_by("index")
bsw@575:   :exec()
bsw@525: 
bsw@575: for i, record in ipairs(delegation_chain) do
bsw@575:   local style
bsw@575:   local overridden = (not issue or issue.state ~= 'voting') and record.overridden
bsw@1045:   ui.sidebarSection( function ()
bsw@1045:     if record.scope_in then
bsw@1045:       if not overridden then
bsw@1045:         local text = _"delegated to"
bsw@1045:         ui.image{
bsw@1045:           attr = { class = "delegation_arrow", alt = text, title = text },
bsw@1045:           static = "delegation_arrow_24_vertical.png"
bsw@1045:         }
bsw@1045:       else
bsw@1045:         local text = _"delegated to"
bsw@1045:         ui.image{
bsw@1045:           attr = { class = "delegation_arrow delegation_arrow_overridden", alt = text, title = text  },
bsw@1045:           static = "delegation_arrow_24_vertical.png"
bsw@1045:         }
bsw@1045:       end
bsw@1045:       ui.tag{
bsw@1045:         attr = { class = "delegation_scope" .. (overridden and " delegation_scope_overridden" or "") },
bsw@1045:         content = function()
bsw@1045:           if record.scope_in == "unit" then
bsw@1045:             slot.put(config.single_object_mode and _"Global delegation" or _"Unit delegation")
bsw@1045:           elseif record.scope_in == "area" then
bsw@1045:             slot.put(_"Area delegation")
bsw@1045:           elseif record.scope_in == "issue" then
bsw@1045:             slot.put(_"Issue delegation")
bsw@1045:           end
bsw@1045:         end
bsw@575:       }
bsw@575:     end
bsw@1045:     ui.container{
bsw@1045:       attr = { class = overridden and "delegation_overridden" or "" },
bsw@575:       content = function()
bsw@1045:         execute.view{
bsw@1045:           module = "member",
bsw@1045:           view = "_show_thumb",
bsw@1045:           params = { member = record }
bsw@1045:         }
bsw@575:       end
bsw@575:     }
bsw@1045:     if issue and issue.state ~= 'voting' and record.participation and not record.overridden then
bsw@1045:       ui.container{
bsw@1045:         attr = { class = "delegation_participation" },
bsw@1045:         content = function()
bsw@1045:           if i == #delegation_chain then
bsw@1045:           ui.tag{ content = _"This member is currently participating in this issue." }
bsw@1045:           else
bsw@1045:           ui.tag{ content = _"This member is participating, the remaining delegation chain is suspended during discussing." }
bsw@1045:           end
bsw@1045:         end
bsw@575:       }
bsw@575:     end
bsw@1045:     slot.put("
")
bsw@1045:   end )
bsw@525: end
bsw@525: 
bsw@577: if preview_trustee_id == 0 or not preview_trustee_id == null and delegation and not delegation.trustee_id then
bsw@575:   ui.image{
bsw@575:     static = "icons/16/table_go_crossed.png"
bsw@575:   }
bsw@577:   if issue_id then
bsw@575:     slot.put(_"Delegation turned off for issue")
bsw@577:   elseif area_id then
bsw@575:     slot.put(_"Delegation turned off for area")
bsw@525:   end
bsw@525: end
bsw@525: 
bsw@1045: 
bsw@1045: end )