| rev | 
   line source | 
| 
bsw/jbe@5
 | 
     1 local tmp = db:query({ "SELECT text_entries_left, initiatives_left FROM member_contingent_left WHERE member_id = ?", app.session.member.id }, "opt_object")
 | 
| 
bsw/jbe@5
 | 
     2 if tmp then
 | 
| 
bsw/jbe@5
 | 
     3   if tmp.initiatives_left and tmp.initiatives_left < 1 then
 | 
| 
bsw/jbe@5
 | 
     4     slot.put_into("error", _"Sorry, your contingent for creating initiatives has been used up. Please try again later.")
 | 
| 
bsw/jbe@5
 | 
     5     return false
 | 
| 
bsw/jbe@5
 | 
     6   end
 | 
| 
bsw/jbe@5
 | 
     7   if tmp.text_entries_left and tmp.text_entries_left < 1 then
 | 
| 
bsw/jbe@5
 | 
     8     slot.put_into("error", _"Sorry, you have reached your personal flood limit. Please be slower...")
 | 
| 
bsw/jbe@5
 | 
     9     return false
 | 
| 
bsw/jbe@5
 | 
    10   end
 | 
| 
bsw/jbe@5
 | 
    11 end
 | 
| 
bsw/jbe@5
 | 
    12 
 | 
| 
bsw/jbe@0
 | 
    13 local issue
 | 
| 
bsw/jbe@0
 | 
    14 local area
 | 
| 
bsw/jbe@0
 | 
    15 
 | 
| 
bsw/jbe@0
 | 
    16 local issue_id = param.get("issue_id", atom.integer)
 | 
| 
bsw/jbe@0
 | 
    17 if issue_id then
 | 
| 
bsw/jbe@0
 | 
    18   issue = Issue:new_selector():add_where{"id=?",issue_id}:single_object_mode():exec()
 | 
| 
bsw/jbe@5
 | 
    19   if issue.closed then
 | 
| 
bsw/jbe@5
 | 
    20     slot.put_into("error", _"This issue is already closed.")
 | 
| 
bsw/jbe@5
 | 
    21     return false
 | 
| 
bsw/jbe@5
 | 
    22   elseif issue.fully_frozen then 
 | 
| 
bsw/jbe@5
 | 
    23     slot.put_into("error", _"Voting for this issue has already begun.")
 | 
| 
bsw/jbe@5
 | 
    24     return false
 | 
| 
bsw/jbe@5
 | 
    25   end
 | 
| 
bsw/jbe@0
 | 
    26   area = issue.area
 | 
| 
bsw/jbe@0
 | 
    27 else
 | 
| 
bsw/jbe@0
 | 
    28   local area_id = param.get("area_id", atom.integer)
 | 
| 
bsw/jbe@0
 | 
    29   area = Area:new_selector():add_where{"id=?",area_id}:single_object_mode():exec()
 | 
| 
bsw/jbe@52
 | 
    30   if not area.active then
 | 
| 
bsw/jbe@52
 | 
    31     slot.put_into("error", "Invalid area.")
 | 
| 
bsw/jbe@52
 | 
    32     return false
 | 
| 
bsw/jbe@52
 | 
    33   end
 | 
| 
bsw/jbe@0
 | 
    34 end
 | 
| 
bsw/jbe@0
 | 
    35 
 | 
| 
bsw@281
 | 
    36 if not app.session.member:has_voting_right_for_unit_id(area.unit_id) then
 | 
| 
bsw@281
 | 
    37   error("access denied")
 | 
| 
bsw@281
 | 
    38 end
 | 
| 
bsw@281
 | 
    39 
 | 
| 
bsw/jbe@6
 | 
    40 local name = param.get("name")
 | 
| 
bsw/jbe@6
 | 
    41 
 | 
| 
bsw/jbe@6
 | 
    42 local name = util.trim(name)
 | 
| 
bsw/jbe@6
 | 
    43 
 | 
| 
bsw/jbe@6
 | 
    44 if #name < 3 then
 | 
| 
bsw/jbe@6
 | 
    45   slot.put_into("error", _"This name is really too short!")
 | 
| 
bsw/jbe@6
 | 
    46   return false
 | 
| 
bsw/jbe@6
 | 
    47 end
 | 
| 
bsw/jbe@6
 | 
    48 
 | 
| 
bsw@81
 | 
    49 local formatting_engine = param.get("formatting_engine")
 | 
| 
bsw@81
 | 
    50 
 | 
| 
bsw@81
 | 
    51 local formatting_engine_valid = false
 | 
| 
bsw@81
 | 
    52 for fe, dummy in pairs(config.formatting_engine_executeables) do
 | 
| 
bsw@81
 | 
    53   if formatting_engine == fe then
 | 
| 
bsw@81
 | 
    54     formatting_engine_valid = true
 | 
| 
bsw@81
 | 
    55   end
 | 
| 
bsw@81
 | 
    56 end
 | 
| 
bsw@81
 | 
    57 if not formatting_engine_valid then
 | 
| 
bsw@81
 | 
    58   error("invalid formatting engine!")
 | 
| 
bsw@81
 | 
    59 end
 | 
| 
bsw@81
 | 
    60 
 | 
| 
bsw@95
 | 
    61 if param.get("preview") then
 | 
| 
bsw@95
 | 
    62   return
 | 
| 
bsw@95
 | 
    63 end
 | 
| 
bsw@81
 | 
    64 
 | 
| 
bsw@81
 | 
    65 
 | 
| 
bsw/jbe@0
 | 
    66 local initiative = Initiative:new()
 | 
| 
bsw/jbe@0
 | 
    67 
 | 
| 
bsw/jbe@0
 | 
    68 if not issue then
 | 
| 
bsw@64
 | 
    69   local policy_id = param.get("policy_id", atom.integer)
 | 
| 
bsw@64
 | 
    70   if policy_id == -1 then
 | 
| 
bsw@64
 | 
    71     slot.put_into("error", _"Please choose a policy")
 | 
| 
bsw@64
 | 
    72     return false
 | 
| 
bsw@64
 | 
    73   end
 | 
| 
bsw@64
 | 
    74   local policy = Policy:by_id(policy_id)
 | 
| 
bsw@64
 | 
    75   if not policy.active then
 | 
| 
bsw@64
 | 
    76     slot.put_into("error", "Invalid policy.")
 | 
| 
bsw@64
 | 
    77     return false
 | 
| 
bsw@64
 | 
    78   end
 | 
| 
bsw@898
 | 
    79   if policy.polling and not app.session.member:has_polling_right_for_unit_id(area.unit_id) then
 | 
| 
bsw@898
 | 
    80     error("no polling right for this unit")
 | 
| 
bsw@898
 | 
    81   end
 | 
| 
bsw@898
 | 
    82   
 | 
| 
bsw@7
 | 
    83   if not area:get_reference_selector("allowed_policies")
 | 
| 
bsw@7
 | 
    84     :add_where{ "policy.id = ?", policy_id }
 | 
| 
bsw@7
 | 
    85     :optional_object_mode()
 | 
| 
bsw@7
 | 
    86     :exec()
 | 
| 
bsw@7
 | 
    87   then
 | 
| 
bsw@7
 | 
    88     error("policy not allowed")
 | 
| 
bsw@7
 | 
    89   end
 | 
| 
bsw/jbe@0
 | 
    90   issue = Issue:new()
 | 
| 
bsw/jbe@0
 | 
    91   issue.area_id = area.id
 | 
| 
bsw@7
 | 
    92   issue.policy_id = policy_id
 | 
| 
bsw@898
 | 
    93   
 | 
| 
bsw@898
 | 
    94   if policy.polling then
 | 
| 
bsw@898
 | 
    95     issue.accepted = 'now'
 | 
| 
bsw@898
 | 
    96     issue.state = 'discussion'
 | 
| 
bsw@898
 | 
    97     initiative.polling = true
 | 
| 
bsw@901
 | 
    98     
 | 
| 
bsw@901
 | 
    99     if policy.free_timeable then
 | 
| 
bsw@901
 | 
   100       local free_timing_string = util.trim(param.get("free_timing"))
 | 
| 
bsw@901
 | 
   101       local available_timings
 | 
| 
bsw@901
 | 
   102       if config.free_timing and config.free_timing.available_func then
 | 
| 
bsw@901
 | 
   103         available_timings = config.free_timing.available_func(policy)
 | 
| 
bsw@901
 | 
   104         if available_timings == false then
 | 
| 
bsw@901
 | 
   105           error("error in free timing config")
 | 
| 
bsw@901
 | 
   106         end
 | 
| 
bsw@901
 | 
   107       end
 | 
| 
bsw@901
 | 
   108       if available_timings then
 | 
| 
bsw@901
 | 
   109         local timing_available = false
 | 
| 
bsw@901
 | 
   110         for i, available_timing in ipairs(available_timings) do
 | 
| 
bsw@901
 | 
   111           if available_timing.id == free_timing_string then
 | 
| 
bsw@901
 | 
   112             timing_available = true
 | 
| 
bsw@901
 | 
   113           end
 | 
| 
bsw@901
 | 
   114         end
 | 
| 
bsw@901
 | 
   115         if not timing_available then
 | 
| 
bsw@901
 | 
   116           error('Invalid timing')
 | 
| 
bsw@901
 | 
   117         end
 | 
| 
bsw@901
 | 
   118       end
 | 
| 
bsw@901
 | 
   119       local timing = config.free_timing.calculate_func(policy, free_timing_string)
 | 
| 
bsw@901
 | 
   120       if not timing then
 | 
| 
bsw@901
 | 
   121         error("error in free timing config")
 | 
| 
bsw@901
 | 
   122       end
 | 
| 
bsw@901
 | 
   123       issue.discussion_time = timing.discussion
 | 
| 
bsw@901
 | 
   124       issue.verification_time = timing.verification
 | 
| 
bsw@901
 | 
   125       issue.voting_time = timing.voting
 | 
| 
bsw@901
 | 
   126     end
 | 
| 
bsw@901
 | 
   127     
 | 
| 
bsw@898
 | 
   128   end
 | 
| 
bsw@898
 | 
   129   
 | 
| 
bsw/jbe@0
 | 
   130   issue:save()
 | 
| 
bsw@286
 | 
   131 
 | 
| 
bsw@286
 | 
   132   if config.etherpad then
 | 
| 
bsw@286
 | 
   133     local result = net.curl(
 | 
| 
bsw@286
 | 
   134       config.etherpad.api_base 
 | 
| 
bsw@286
 | 
   135       .. "api/1/createGroupPad?apikey=" .. config.etherpad.api_key
 | 
| 
bsw@286
 | 
   136       .. "&groupID=" .. config.etherpad.group_id
 | 
| 
bsw@286
 | 
   137       .. "&padName=Issue" .. tostring(issue.id)
 | 
| 
jbe@326
 | 
   138       .. "&text=" .. request.get_absolute_baseurl() .. "issue/show/" .. tostring(issue.id) .. ".html"
 | 
| 
bsw@286
 | 
   139     )
 | 
| 
bsw@286
 | 
   140   end
 | 
| 
bsw/jbe@0
 | 
   141 end
 | 
| 
bsw/jbe@0
 | 
   142 
 | 
| 
bsw@898
 | 
   143 if param.get("polling", atom.boolean) and app.session.member:has_polling_right_for_unit_id(area.unit_id) then
 | 
| 
bsw@898
 | 
   144   initiative.polling = true
 | 
| 
bsw@898
 | 
   145 end
 | 
| 
bsw/jbe@0
 | 
   146 initiative.issue_id = issue.id
 | 
| 
bsw/jbe@6
 | 
   147 initiative.name = name
 | 
| 
bsw/jbe@6
 | 
   148 param.update(initiative, "discussion_url")
 | 
| 
bsw/jbe@0
 | 
   149 initiative:save()
 | 
| 
bsw/jbe@0
 | 
   150 
 | 
| 
bsw/jbe@0
 | 
   151 local draft = Draft:new()
 | 
| 
bsw/jbe@0
 | 
   152 draft.initiative_id = initiative.id
 | 
| 
bsw/jbe@4
 | 
   153 draft.formatting_engine = formatting_engine
 | 
| 
bsw/jbe@0
 | 
   154 draft.content = param.get("draft")
 | 
| 
bsw/jbe@0
 | 
   155 draft.author_id = app.session.member.id
 | 
| 
bsw/jbe@0
 | 
   156 draft:save()
 | 
| 
bsw/jbe@0
 | 
   157 
 | 
| 
bsw/jbe@0
 | 
   158 local initiator = Initiator:new()
 | 
| 
bsw/jbe@0
 | 
   159 initiator.initiative_id = initiative.id
 | 
| 
bsw/jbe@0
 | 
   160 initiator.member_id = app.session.member.id
 | 
| 
bsw@10
 | 
   161 initiator.accepted = true
 | 
| 
bsw/jbe@0
 | 
   162 initiator:save()
 | 
| 
bsw/jbe@0
 | 
   163 
 | 
| 
bsw/jbe@0
 | 
   164 local supporter = Supporter:new()
 | 
| 
bsw/jbe@0
 | 
   165 supporter.initiative_id = initiative.id
 | 
| 
bsw/jbe@0
 | 
   166 supporter.member_id = app.session.member.id
 | 
| 
bsw/jbe@0
 | 
   167 supporter.draft_id = draft.id
 | 
| 
bsw/jbe@0
 | 
   168 supporter:save()
 | 
| 
bsw/jbe@0
 | 
   169 
 | 
| 
bsw/jbe@0
 | 
   170 slot.put_into("notice", _"Initiative successfully created")
 | 
| 
bsw/jbe@0
 | 
   171 
 | 
| 
bsw/jbe@0
 | 
   172 request.redirect{
 | 
| 
bsw/jbe@0
 | 
   173   module = "initiative",
 | 
| 
bsw/jbe@0
 | 
   174   view = "show",
 | 
| 
bsw/jbe@0
 | 
   175   id = initiative.id
 | 
| 
jbe@326
 | 
   176 }
 |