| rev | 
   line source | 
| 
bsw/jbe@0
 | 
     1 Draft = mondelefant.new_class()
 | 
| 
bsw/jbe@0
 | 
     2 Draft.table = 'draft'
 | 
| 
bsw/jbe@0
 | 
     3 
 | 
| 
bsw@81
 | 
     4 -- Many drafts belonging to an initiative
 | 
| 
bsw/jbe@0
 | 
     5 Draft:add_reference{
 | 
| 
bsw/jbe@0
 | 
     6   mode          = 'm1',
 | 
| 
bsw/jbe@0
 | 
     7   to            = "Initiative",
 | 
| 
bsw/jbe@0
 | 
     8   this_key      = 'initiative_id',
 | 
| 
bsw/jbe@0
 | 
     9   that_key      = 'id',
 | 
| 
bsw/jbe@0
 | 
    10   ref           = 'initiative',
 | 
| 
bsw/jbe@0
 | 
    11 }
 | 
| 
bsw/jbe@0
 | 
    12 
 | 
| 
bsw@81
 | 
    13 -- Many drafts are authored by a member
 | 
| 
bsw/jbe@0
 | 
    14 Draft:add_reference{
 | 
| 
bsw/jbe@0
 | 
    15   mode          = 'm1',
 | 
| 
bsw/jbe@0
 | 
    16   to            = "Member",
 | 
| 
bsw/jbe@0
 | 
    17   this_key      = 'author_id',
 | 
| 
bsw/jbe@0
 | 
    18   that_key      = 'id',
 | 
| 
bsw/jbe@0
 | 
    19   ref           = 'author',
 | 
| 
bsw/jbe@0
 | 
    20 }
 | 
| 
bsw/jbe@0
 | 
    21 
 | 
| 
bsw/jbe@0
 | 
    22 function Draft.object_get:author_name()
 | 
| 
bsw/jbe@0
 | 
    23   return self.author and self.author.name or _"Unknown author"
 | 
| 
bsw/jbe@0
 | 
    24 end
 | 
| 
bsw@81
 | 
    25 
 | 
| 
bsw/jbe@1309
 | 
    26 model.has_rendered_content(Draft, RenderedDraft, "content", "draft_id")
 | 
| 
bsw@1208
 | 
    27 
 | 
| 
bsw@1208
 | 
    28 function Draft:update_content(member_id, initiative_id, p_formatting_engine, content, external_reference, preview)
 | 
| 
bsw@1208
 | 
    29   local initiative = Initiative:by_id(initiative_id)
 | 
| 
bsw@1208
 | 
    30 
 | 
| 
bsw@1208
 | 
    31   -- TODO important m1 selectors returning result _SET_!
 | 
| 
bsw@1208
 | 
    32   local issue = initiative:get_reference_selector("issue"):for_share():single_object_mode():exec()
 | 
| 
bsw@1208
 | 
    33 
 | 
| 
bsw@1208
 | 
    34   if issue.closed then
 | 
| 
bsw@1208
 | 
    35     slot.put_into("error", _"This issue is already closed.")
 | 
| 
bsw@1208
 | 
    36     return false
 | 
| 
bsw@1208
 | 
    37   elseif issue.half_frozen then 
 | 
| 
bsw@1208
 | 
    38     slot.put_into("error", _"This issue is already frozen.")
 | 
| 
bsw@1208
 | 
    39     return false
 | 
| 
bsw@1208
 | 
    40   elseif issue.phase_finished then
 | 
| 
bsw@1208
 | 
    41     slot.put_into("error", _"Current phase is already closed.")
 | 
| 
bsw@1208
 | 
    42     return false
 | 
| 
bsw@1208
 | 
    43   end
 | 
| 
bsw@1208
 | 
    44 
 | 
| 
bsw@1208
 | 
    45   local initiator = Initiator:by_pk(initiative.id, member_id)
 | 
| 
bsw@1208
 | 
    46   if not initiator or not initiator.accepted then
 | 
| 
bsw/jbe@1309
 | 
    47     return false
 | 
| 
bsw@1208
 | 
    48   end
 | 
| 
bsw@1208
 | 
    49 
 | 
| 
bsw@1208
 | 
    50   local tmp = db:query({ "SELECT text_entries_left FROM member_contingent_left WHERE member_id = ? AND polling = ?", member_id, initiative.polling }, "opt_object")
 | 
| 
bsw@1208
 | 
    51   if not tmp or tmp.text_entries_left < 1 then
 | 
| 
bsw@1208
 | 
    52     slot.put_into("error", _"Sorry, you have reached your personal flood limit. Please be slower...")
 | 
| 
bsw@1208
 | 
    53     return false
 | 
| 
bsw@1208
 | 
    54   end
 | 
| 
bsw@1208
 | 
    55 
 | 
| 
bsw@1208
 | 
    56   local formatting_engine
 | 
| 
bsw@1208
 | 
    57   if config.enforce_formatting_engine then
 | 
| 
bsw@1208
 | 
    58     formatting_engine = config.enforce_formatting_engine 
 | 
| 
bsw@1208
 | 
    59   else
 | 
| 
bsw@1208
 | 
    60     formatting_engine = p_formatting_engine
 | 
| 
bsw@1208
 | 
    61 
 | 
| 
bsw@1208
 | 
    62     local formatting_engine_valid = false
 | 
| 
bsw@1208
 | 
    63     for i, fe in pairs(config.formatting_engines) do
 | 
| 
bsw@1208
 | 
    64       if formatting_engine == fe.id then
 | 
| 
bsw@1208
 | 
    65         formatting_engine_valid = true
 | 
| 
bsw@1208
 | 
    66       end
 | 
| 
bsw@1208
 | 
    67     end
 | 
| 
bsw@1208
 | 
    68     if not formatting_engine_valid then
 | 
| 
bsw@1208
 | 
    69       error("invalid formatting engine!")
 | 
| 
bsw@1208
 | 
    70     end
 | 
| 
bsw@1208
 | 
    71   end
 | 
| 
bsw@1208
 | 
    72 
 | 
| 
bsw@1208
 | 
    73   if preview then
 | 
| 
bsw@1208
 | 
    74     return false
 | 
| 
bsw@1208
 | 
    75   end
 | 
| 
bsw@1208
 | 
    76 
 | 
| 
bsw@1208
 | 
    77   local old_draft = initiative.current_draft
 | 
| 
bsw@1208
 | 
    78 
 | 
| 
bsw@1208
 | 
    79   local draft = Draft:new()
 | 
| 
bsw@1208
 | 
    80   draft.author_id = member_id
 | 
| 
bsw@1208
 | 
    81   draft.initiative_id = initiative.id
 | 
| 
bsw@1208
 | 
    82   draft.formatting_engine = formatting_engine or old_draft and old_draft.formatting_engine or nil
 | 
| 
bsw@1208
 | 
    83   draft.content = content or old_draft and old_draft.content or nil
 | 
| 
bsw@1208
 | 
    84   if external_reference == false then
 | 
| 
bsw@1208
 | 
    85     draft.external_reference = nil
 | 
| 
bsw@1208
 | 
    86   else
 | 
| 
bsw@1208
 | 
    87     draft.external_reference = external_reference or old_draft and old_draft.external_reference or nil
 | 
| 
bsw@1208
 | 
    88   end
 | 
| 
bsw@1208
 | 
    89   draft:save()
 | 
| 
bsw@1208
 | 
    90 
 | 
| 
bsw@1208
 | 
    91   local supporter = Supporter:by_pk(initiative.id, member_id)
 | 
| 
bsw@1208
 | 
    92 
 | 
| 
bsw@1208
 | 
    93   if supporter then
 | 
| 
bsw@1208
 | 
    94     supporter.draft_id = draft.id
 | 
| 
bsw@1208
 | 
    95     supporter:save()
 | 
| 
bsw@1208
 | 
    96   end
 | 
| 
bsw@1208
 | 
    97 
 | 
| 
bsw@1208
 | 
    98   draft:render_content()
 | 
| 
bsw@1208
 | 
    99 
 | 
| 
bsw@1495
 | 
   100   local draft_attachments = DraftAttachment:new_selector()
 | 
| 
bsw@1495
 | 
   101     :add_where{ "draft_id = ?", old_draft.id }
 | 
| 
bsw@1495
 | 
   102     :exec()
 | 
| 
bsw@1495
 | 
   103 
 | 
| 
bsw@1495
 | 
   104   for i, draft_attachment in ipairs(draft_attachments) do
 | 
| 
bsw@1495
 | 
   105     local new_draft_attachment = DraftAttachment:new()
 | 
| 
bsw@1495
 | 
   106     new_draft_attachment.draft_id = draft.id
 | 
| 
bsw@1495
 | 
   107     new_draft_attachment.file_id = draft_attachment.file_id
 | 
| 
bsw@1495
 | 
   108     new_draft_attachment.title = draft_attachment.title
 | 
| 
bsw@1495
 | 
   109     new_draft_attachment.description = draft_attachment.description
 | 
| 
bsw@1495
 | 
   110     new_draft_attachment:save()
 | 
| 
bsw@1495
 | 
   111   end
 | 
| 
bsw@1495
 | 
   112 
 | 
| 
bsw@1208
 | 
   113   slot.put_into("notice", _"The initiative text has been updated")
 | 
| 
bsw@1495
 | 
   114   return draft.id
 | 
| 
bsw@1208
 | 
   115 end
 |