@@ -91,8 +91,13 @@ def create
9191 @tag_name = params [ '_tag_name' ]
9292
9393 is_multiple = params [ :todo ] && params [ :todo ] [ :multiple_todos ] && !params [ :todo ] [ :multiple_todos ] . nil?
94+ is_multiple_dates = params [ :todo ] &&
95+ params [ :todo ] [ :due ] . is_a? ( Array ) &&
96+ params [ :todo ] [ :due ] . count ( &:present? ) > 1
9497 if is_multiple
9598 create_multiple
99+ elsif is_multiple_dates
100+ create_multiple_dates
96101 else
97102 p = Todos ::TodoCreateParamsHelper . new ( params , current_user )
98103 p . parse_dates unless mobile?
@@ -240,6 +245,67 @@ def create_multiple
240245 end
241246 end
242247
248+ def create_multiple_dates
249+ p = Todos ::TodoCreateParamsHelper . new ( params , current_user )
250+ # parse_dates is skipped because due/show_from are arrays (guarded in helper)
251+ tag_list = p . tag_list
252+
253+ due_dates = Array ( params [ :todo ] [ :due ] ) . map { |d | current_user . prefs . parse_date ( d ) }
254+ show_from_dates = Array ( params [ :todo ] [ :show_from ] ) . map { |d | current_user . prefs . parse_date ( d ) }
255+
256+ @todos = [ ]
257+ @build_todos = [ ]
258+ validates = true
259+
260+ due_dates . each_with_index do |due , i |
261+ next if due . blank? && show_from_dates [ i ] . blank?
262+
263+ todo_attrs = p . attributes . merge ( 'due' => due , 'show_from' => show_from_dates [ i ] )
264+ todo = current_user . todos . build
265+ todo . assign_attributes ( todo_attrs )
266+ validates &&= todo . valid?
267+ @build_todos << todo
268+ end
269+
270+ if validates && @build_todos . any?
271+ @build_todos . each do |todo |
272+ @saved = todo . save
273+ todo . tag_with ( tag_list ) if @saved && tag_list . present?
274+ @todos << todo if @saved
275+ end
276+ @saved = @todos . size == @build_todos . size
277+ else
278+ @todos = @build_todos
279+ @saved = false
280+ end
281+
282+ @not_done_todos = @todos if p . new_project_created || p . new_context_created
283+
284+ respond_to do |format |
285+ format . html { redirect_to action : 'index' }
286+ format . js do
287+ determine_down_count if @saved
288+ @contexts = current_user . contexts if p . new_context_created
289+ @projects = current_user . projects if p . new_project_created
290+ @new_project_created = p . new_project_created
291+ @new_context_created = p . new_context_created
292+ @initial_context_name = params [ 'default_context_name' ]
293+ @initial_project_name = params [ 'default_project_name' ]
294+ @initial_tags = params [ 'initial_tag_list' ]
295+ if @saved && @todos . size > 0
296+ @default_tags = @todos [ 0 ] . project . default_tags unless @todos [ 0 ] . project . nil?
297+ else
298+ @multiple_error = @todos . size > 0 ? '' : t ( 'todos.next_action_needed' )
299+ @saved = false
300+ end
301+ @status_message = @todos . size > 1 ? t ( 'todos.added_new_next_action_plural' ) : t ( 'todos.added_new_next_action_singular' )
302+ @status_message = t ( 'todos.added_new_project' ) + ' / ' + @status_message if p . new_project_created
303+ @status_message = t ( 'todos.added_new_context' ) + ' / ' + @status_message if p . new_context_created
304+ render action : 'create_multiple'
305+ end
306+ end
307+ end
308+
243309 def edit
244310 @todo = current_user . todos . find ( params [ 'id' ] )
245311 @source_view = params [ '_source_view' ] || 'todo'
0 commit comments