liquid_feedback_frontend
view app/main/initiative/_action/create.lua @ 1200:8e36f08eaac6
Added Moonbridge to about page
| author | bsw | 
|---|---|
| date | Tue Jun 23 19:57:10 2015 +0200 (2015-06-23) | 
| parents | 9604ba51624a | 
| children | 32cc544d5a5b | 
 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 local timing
   102 if not issue and policy.free_timeable then
   103   local free_timing_string = util.trim(param.get("free_timing"))
   104   if not free_timing_string or #free_timing_string < 1 then
   105     slot.put_into("error", _"Choose timing")
   106     return false
   107   end
   108   local available_timings
   109   if config.free_timing and config.free_timing.available_func then
   110     available_timings = config.free_timing.available_func(policy)
   111     if available_timings == false then
   112       error("error in free timing config")
   113     end
   114   end
   115   if available_timings then
   116     local timing_available = false
   117     for i, available_timing in ipairs(available_timings) do
   118       if available_timing.id == free_timing_string then
   119 	timing_available = true
   120       end
   121     end
   122     if not timing_available then
   123       slot.put_into("error", _"Invalid timing")
   124       return false
   125     end
   126   end
   127   timing = config.free_timing.calculate_func(policy, free_timing_string)
   128   if not timing then
   129     error("error in free timing config")
   130   end
   131 end
   133 if param.get("preview") or param.get("edit") then
   134   return
   135 end
   137 local initiative = Initiative:new()
   139 if not issue then
   140   issue = Issue:new()
   141   issue.area_id = area.id
   142   issue.policy_id = policy_id
   144   if policy.polling then
   145     issue.accepted = 'now'
   146     issue.state = 'discussion'
   147     initiative.polling = true
   149     if policy.free_timeable then
   150       issue.discussion_time = timing.discussion
   151       issue.verification_time = timing.verification
   152       issue.voting_time = timing.voting
   153     end
   155   end
   157   issue:save()
   159   if config.etherpad then
   160     local result = net.curl(
   161       config.etherpad.api_base 
   162       .. "api/1/createGroupPad?apikey=" .. config.etherpad.api_key
   163       .. "&groupID=" .. config.etherpad.group_id
   164       .. "&padName=Issue" .. tostring(issue.id)
   165       .. "&text=" .. request.get_absolute_baseurl() .. "issue/show/" .. tostring(issue.id) .. ".html"
   166     )
   167   end
   168 end
   170 if param.get("polling", atom.boolean) and app.session.member:has_polling_right_for_unit_id(area.unit_id) then
   171   initiative.polling = true
   172 end
   173 initiative.issue_id = issue.id
   174 initiative.name = name
   175 initiative:save()
   177 local draft = Draft:new()
   178 draft.initiative_id = initiative.id
   179 draft.formatting_engine = formatting_engine
   180 draft.content = param.get("draft")
   181 draft.author_id = app.session.member.id
   182 draft:save()
   184 local initiator = Initiator:new()
   185 initiator.initiative_id = initiative.id
   186 initiator.member_id = app.session.member.id
   187 initiator.accepted = true
   188 initiator:save()
   190 if not is_polling then
   191   local supporter = Supporter:new()
   192   supporter.initiative_id = initiative.id
   193   supporter.member_id = app.session.member.id
   194   supporter.draft_id = draft.id
   195   supporter:save()
   196 end
   198 slot.put_into("notice", _"Initiative successfully created")
   200 request.redirect{
   201   module = "initiative",
   202   view = "show",
   203   id = initiative.id
   204 }
