| rev | 
   line source | 
| 
bsw/jbe@1309
 | 
     1 if config.disable_delegations then
 | 
| 
bsw/jbe@1309
 | 
     2   return
 | 
| 
bsw/jbe@1309
 | 
     3 end
 | 
| 
bsw/jbe@1309
 | 
     4 
 | 
| 
bsw/jbe@0
 | 
     5 local truster_id = app.session.member.id
 | 
| 
bsw/jbe@0
 | 
     6 
 | 
| 
bsw/jbe@0
 | 
     7 local trustee_id = param.get("trustee_id", atom.integer)
 | 
| 
bsw/jbe@0
 | 
     8 
 | 
| 
bsw@248
 | 
     9 local unit_id = param.get("unit_id", atom.integer)
 | 
| 
bsw@248
 | 
    10 
 | 
| 
bsw/jbe@0
 | 
    11 local area_id = param.get("area_id", atom.integer)
 | 
| 
bsw/jbe@0
 | 
    12 
 | 
| 
bsw/jbe@0
 | 
    13 local issue_id = param.get("issue_id", atom.integer)
 | 
| 
bsw/jbe@0
 | 
    14 
 | 
| 
bsw@575
 | 
    15 local initiative_id = param.get("initiative_id", atom.integer)
 | 
| 
bsw@575
 | 
    16 
 | 
| 
bsw/jbe@0
 | 
    17 if issue_id then 
 | 
| 
bsw/jbe@0
 | 
    18   area_id = nil
 | 
| 
bsw/jbe@0
 | 
    19 end
 | 
| 
bsw/jbe@0
 | 
    20 
 | 
| 
bsw@529
 | 
    21 local preview = param.get("preview") 
 | 
| 
bsw@529
 | 
    22 
 | 
| 
bsw@529
 | 
    23 if preview == "1" then
 | 
| 
bsw@529
 | 
    24   request.redirect{ module = "delegation", view = "show", params = {
 | 
| 
bsw@575
 | 
    25     unit_id = unit_id, area_id = area_id, issue_id = issue_id, initiative_id = initiative_id, preview_trustee_id = trustee_id
 | 
| 
bsw@529
 | 
    26   } }
 | 
| 
bsw@529
 | 
    27   return
 | 
| 
bsw@529
 | 
    28 end
 | 
| 
bsw@529
 | 
    29 
 | 
| 
bsw@293
 | 
    30 local delegation = Delegation:by_pk(truster_id, unit_id, area_id, issue_id)
 | 
| 
bsw@293
 | 
    31 
 | 
| 
bsw@293
 | 
    32 
 | 
| 
bsw/jbe@4
 | 
    33 if param.get("delete") or trustee_id == -1 then
 | 
| 
bsw/jbe@0
 | 
    34 
 | 
| 
bsw/jbe@0
 | 
    35   if delegation then
 | 
| 
bsw/jbe@0
 | 
    36     delegation:destroy()
 | 
| 
bsw/jbe@0
 | 
    37   end
 | 
| 
bsw/jbe@5
 | 
    38 
 | 
| 
bsw/jbe@0
 | 
    39 else
 | 
| 
bsw@296
 | 
    40   
 | 
| 
bsw@300
 | 
    41   local trustee
 | 
| 
bsw@300
 | 
    42   
 | 
| 
bsw@300
 | 
    43   if trustee_id then
 | 
| 
bsw@300
 | 
    44     trustee = Member:by_id(trustee_id)
 | 
| 
bsw@300
 | 
    45   end
 | 
| 
bsw/jbe@0
 | 
    46 
 | 
| 
bsw@281
 | 
    47   local check_unit_id
 | 
| 
bsw@281
 | 
    48   if unit_id then
 | 
| 
bsw@281
 | 
    49     check_unit_id = unit_id
 | 
| 
bsw@281
 | 
    50   elseif area_id then
 | 
| 
bsw@281
 | 
    51     local area = Area:by_id(area_id)
 | 
| 
bsw@281
 | 
    52     check_unit_id = area.unit_id
 | 
| 
bsw@281
 | 
    53   else
 | 
| 
bsw@281
 | 
    54     local issue = Issue:by_id(issue_id)
 | 
| 
bsw@281
 | 
    55     local area = Area:by_id(issue.area_id)
 | 
| 
bsw@281
 | 
    56     check_unit_id = area.unit_id
 | 
| 
bsw@281
 | 
    57   end
 | 
| 
bsw@296
 | 
    58   
 | 
| 
bsw@1622
 | 
    59   if trustee and not (
 | 
| 
bsw@1622
 | 
    60     trustee:has_voting_right_for_unit_id(check_unit_id)
 | 
| 
bsw@1622
 | 
    61     or trustee:has_initiative_right_for_unit_id(check_unit_id)
 | 
| 
bsw@1622
 | 
    62   ) then
 | 
| 
bsw@296
 | 
    63     slot.put_into("error", _"Trustee has no voting right in this unit")
 | 
| 
bsw@296
 | 
    64     return false
 | 
| 
bsw@296
 | 
    65   end
 | 
| 
bsw@281
 | 
    66 
 | 
| 
bsw@1622
 | 
    67   if not (
 | 
| 
bsw@1622
 | 
    68     app.session.member:has_voting_right_for_unit_id(check_unit_id) 
 | 
| 
bsw@1622
 | 
    69     or app.session.member:has_initiative_right_for_unit_id(check_unit_id) 
 | 
| 
bsw@1622
 | 
    70   ) then
 | 
| 
bsw/jbe@1309
 | 
    71     return execute.view { module = "index", view = "403" }
 | 
| 
bsw@281
 | 
    72   end
 | 
| 
bsw@281
 | 
    73 
 | 
| 
bsw/jbe@0
 | 
    74   if not delegation then
 | 
| 
bsw/jbe@0
 | 
    75     delegation = Delegation:new()
 | 
| 
bsw/jbe@0
 | 
    76     delegation.truster_id = truster_id
 | 
| 
bsw@248
 | 
    77     delegation.unit_id    = unit_id
 | 
| 
bsw/jbe@0
 | 
    78     delegation.area_id    = area_id
 | 
| 
bsw/jbe@0
 | 
    79     delegation.issue_id   = issue_id
 | 
| 
bsw/jbe@5
 | 
    80     if issue_id then
 | 
| 
bsw/jbe@5
 | 
    81       delegation.scope = "issue"
 | 
| 
bsw/jbe@5
 | 
    82     elseif area_id then
 | 
| 
bsw/jbe@5
 | 
    83       delegation.scope = "area"
 | 
| 
bsw@248
 | 
    84     elseif unit_id then
 | 
| 
bsw@248
 | 
    85       delegation.scope = "unit"
 | 
| 
bsw/jbe@5
 | 
    86     end
 | 
| 
bsw/jbe@0
 | 
    87   end
 | 
| 
bsw@180
 | 
    88   if trustee_id == 0 then
 | 
| 
bsw@180
 | 
    89     delegation.trustee_id = nil
 | 
| 
bsw@180
 | 
    90   else
 | 
| 
bsw@180
 | 
    91     delegation.trustee_id = trustee_id
 | 
| 
bsw@180
 | 
    92   end
 | 
| 
bsw/jbe@0
 | 
    93 
 | 
| 
bsw/jbe@0
 | 
    94   delegation:save()
 | 
| 
bsw@529
 | 
    95 
 | 
| 
bsw/jbe@0
 | 
    96 end
 | 
| 
bsw/jbe@0
 | 
    97 
 |