liquid_feedback_frontend
view app/main/initiative/_action/create.lua @ 1077:78b6dc244ef2
Set version number to 3.0.2
| author | bsw | 
|---|---|
| date | Tue Jul 22 23:01:41 2014 +0200 (2014-07-22) | 
| parents | 701a5cf6b067 | 
| children | 5b65ea5c24f5 | 
 line source
     1 local issue
     2 local area
     4 local issue_id = param.get("issue_id", atom.integer)
     5 if issue_id then
     6   issue = Issue:new_selector():add_where{"id=?",issue_id}:for_share():single_object_mode():exec()
     7   if issue.closed then
     8     slot.put_into("error", _"This issue is already closed.")
     9     return false
    10   elseif issue.fully_frozen then 
    11     slot.put_into("error", _"Voting for this issue has already begun.")
    12     return false
    13   elseif issue.phase_finished then
    14     slot.put_into("error", _"Current phase is already closed.")
    15     return false
    16   end
    17   area = issue.area
    18 else
    19   local area_id = param.get("area_id", atom.integer)
    20   area = Area:new_selector():add_where{"id=?",area_id}:single_object_mode():exec()
    21   if not area.active then
    22     slot.put_into("error", "Invalid area.")
    23     return false
    24   end
    25 end
    27 if not app.session.member:has_voting_right_for_unit_id(area.unit_id) then
    28   error("access denied")
    29 end
    31 local policy_id = param.get("policy_id", atom.integer)
    32 local policy
    33 if policy_id then
    34   policy = Policy:by_id(policy_id)
    35 end
    37 if not issue then
    38   if policy_id == -1 then
    39     slot.put_into("error", _"Please choose a policy")
    40     return false
    41   end
    42   if not policy.active then
    43     slot.put_into("error", "Invalid policy.")
    44     return false
    45   end
    46   if policy.polling and not app.session.member:has_polling_right_for_unit_id(area.unit_id) then
    47     error("no polling right for this unit")
    48   end
    50   if not area:get_reference_selector("allowed_policies")
    51     :add_where{ "policy.id = ?", policy_id }
    52     :optional_object_mode()
    53     :exec()
    54   then
    55     error("policy not allowed")
    56   end
    57 end
    59 local is_polling = (issue and param.get("polling", atom.boolean)) or (policy and policy.polling) or false
    61 local tmp = db:query({ "SELECT text_entries_left, initiatives_left FROM member_contingent_left WHERE member_id = ? AND polling = ?", app.session.member.id, is_polling }, "opt_object")
    62 if not tmp or tmp.initiatives_left < 1 then
    63   slot.put_into("error", _"Sorry, your contingent for creating initiatives has been used up. Please try again later.")
    64   return false
    65 end
    66 if tmp and tmp.text_entries_left < 1 then
    67   slot.put_into("error", _"Sorry, you have reached your personal flood limit. Please be slower...")
    68   return false
    69 end
    71 local name = param.get("name")
    73 local name = util.trim(name)
    75 if #name < 3 then
    76   slot.put_into("error", _"Please enter a meaningful title for your initiative!")
    77   return false
    78 end
    80 if #name > 140 then
    81   slot.put_into("error", _"This title is too long!")
    82   return false
    83 end
    85 local formatting_engine
    86 if config.enforce_formatting_engine then
    87   formatting_engine = config.enforce_formatting_engine
    88 else
    89   formatting_engine = param.get("formatting_engine")
    90   local formatting_engine_valid = false
    91   for i, fe in ipairs(config.formatting_engines) do
    92     if formatting_engine == fe.id then
    93       formatting_engine_valid = true
    94     end
    95   end
    96   if not formatting_engine_valid then
    97     error("invalid formatting engine!")
    98   end
    99 end
   101 if param.get("preview") or param.get("edit") then
   102   return
   103 end
   106 local initiative = Initiative:new()
   108 if not issue then
   109   issue = Issue:new()
   110   issue.area_id = area.id
   111   issue.policy_id = policy_id
   113   if policy.polling then
   114     issue.accepted = 'now'
   115     issue.state = 'discussion'
   116     initiative.polling = true
   118     if policy.free_timeable then
   119       local free_timing_string = util.trim(param.get("free_timing"))
   120       local available_timings
   121       if config.free_timing and config.free_timing.available_func then
   122         available_timings = config.free_timing.available_func(policy)
   123         if available_timings == false then
   124           error("error in free timing config")
   125         end
   126       end
   127       if available_timings then
   128         local timing_available = false
   129         for i, available_timing in ipairs(available_timings) do
   130           if available_timing.id == free_timing_string then
   131             timing_available = true
   132           end
   133         end
   134         if not timing_available then
   135           error('Invalid timing')
   136         end
   137       end
   138       local timing = config.free_timing.calculate_func(policy, free_timing_string)
   139       if not timing then
   140         error("error in free timing config")
   141       end
   142       issue.discussion_time = timing.discussion
   143       issue.verification_time = timing.verification
   144       issue.voting_time = timing.voting
   145     end
   147   end
   149   issue:save()
   151   if config.etherpad then
   152     local result = net.curl(
   153       config.etherpad.api_base 
   154       .. "api/1/createGroupPad?apikey=" .. config.etherpad.api_key
   155       .. "&groupID=" .. config.etherpad.group_id
   156       .. "&padName=Issue" .. tostring(issue.id)
   157       .. "&text=" .. request.get_absolute_baseurl() .. "issue/show/" .. tostring(issue.id) .. ".html"
   158     )
   159   end
   160 end
   162 if param.get("polling", atom.boolean) and app.session.member:has_polling_right_for_unit_id(area.unit_id) then
   163   initiative.polling = true
   164 end
   165 initiative.issue_id = issue.id
   166 initiative.name = name
   167 initiative:save()
   169 local draft = Draft:new()
   170 draft.initiative_id = initiative.id
   171 draft.formatting_engine = formatting_engine
   172 draft.content = param.get("draft")
   173 draft.author_id = app.session.member.id
   174 draft:save()
   176 local initiator = Initiator:new()
   177 initiator.initiative_id = initiative.id
   178 initiator.member_id = app.session.member.id
   179 initiator.accepted = true
   180 initiator:save()
   182 if not is_polling then
   183   local supporter = Supporter:new()
   184   supporter.initiative_id = initiative.id
   185   supporter.member_id = app.session.member.id
   186   supporter.draft_id = draft.id
   187   supporter:save()
   188 end
   190 slot.put_into("notice", _"Initiative successfully created")
   192 request.redirect{
   193   module = "initiative",
   194   view = "show",
   195   id = initiative.id
   196 }
