Class: TodosController

Inherits:
ApplicationController show all
Defined in:
app/controllers/todos_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#admin_login_required, #admin_or_self_login_required, #all_done_todos_for, #boolean_param, cas_enabled?, #cas_enabled?, #count_deferred_todos, #count_undone_todos, #count_undone_todos_phrase, #done_todos_for, #enable_mobile_content_negotiation, #for_autocomplete, #format_date, #format_dependencies_as_json_for_auto_complete, #handle_unverified_request, #init_data_for_sidebar, #init_hidden_todo_counts, #init_not_done_counts, #mobile?, #notify, #openid_enabled?, openid_enabled?, #parse_date_per_user_prefs, prefered_auth?, #prefered_auth?, #redirect_back_or_home, #render_failure, #sanitize, #set_locale, #set_session_expiration, #set_time_zone, #set_zindex_counter, #todo_xml_params

Methods included from Common

like_operator, #set_theme

Methods included from LoginSystem

#access_denied, #authorize?, #basic_auth_denied, #current_user, #get_basic_auth_data, #get_current_user, #logged_in?, #login_from_cookie, #login_optional, #login_or_feed_token_required, #login_required, #logout_user, #prefs, #protect?, #redirect_back_or_default, #redirect_to_login, #set_current_user, #store_location

Instance Method Details

#add_predecessorObject



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'app/controllers/todos_controller.rb', line 266

def add_predecessor
  @source_view = params['_source_view'] || 'todo'
  @predecessor = current_user.todos.find(params['predecessor'])
  @predecessors = @predecessor.predecessors
  @todo = current_user.todos.includes(Todo::DEFAULT_INCLUDES).find(params['successor'])
  @original_state = @todo.state
  unless @predecessor.completed?
    begin
      @todo.add_predecessor(@predecessor)
      @todo.block! unless @todo.pending?
      @saved = @todo.save
    rescue ActiveRecord::RecordInvalid
      @saved = false
    end

    @status_message = t('todos.added_dependency', :dependency => @predecessor.description)
    @status_message += ' ' + t('todos.set_to_pending', :task => @todo.description) unless @original_state == 'pending'
  else
    @saved = false
  end
  respond_to do |format|
    format.js
  end
end

#all_doneObject



568
569
570
571
572
573
574
# File 'app/controllers/todos_controller.rb', line 568

def all_done
  @source_view = 'done'
  @page_title = t('todos.completed_tasks_title')

  @done = current_user.todos.completed.includes(Todo::DEFAULT_INCLUDES).reorder('completed_at DESC').paginate :page => params[:page], :per_page => 20
  @count = @done.size
end

#all_done_tagObject



687
688
689
690
691
692
# File 'app/controllers/todos_controller.rb', line 687

def all_done_tag
  done_by_tag_setup
  @done = current_user.todos.completed.with_tag(@tag.id).reorder('completed_at DESC').includes(Todo::DEFAULT_INCLUDES).paginate :page => params[:page], :per_page => 20
  @count = @done.size
  render :template => 'todos/all_done'
end

#attachmentObject



804
805
806
807
808
809
810
811
812
813
814
815
816
# File 'app/controllers/todos_controller.rb', line 804

def attachment
  id = params[:id]
  filename = params[:filename]
  attachment = current_user.attachments.find(id)

  if attachment
    send_file(attachment.file.path,
      disposition: 'attachment',
      type: 'message/rfc822')
  else
    head :not_found
  end
end

#auto_complete_for_predecessorObject



767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
# File 'app/controllers/todos_controller.rb', line 767

def auto_complete_for_predecessor
  unless params['id'].nil?
    get_todo_from_params
    # Begin matching todos in current project, excluding @todo itself
    @items = get_not_completed_for_predecessor(@todo.project, @todo.id) unless @todo.project.nil?
    # Then look in the current context, excluding @todo itself
    @items = get_not_completed_for_predecessor(@todo.context, @todo.id) unless !@items.empty? || @todo.context.nil?
    # Match todos in other projects, excluding @todo itself
    @items = get_not_completed_for_predecessor(current_user, @todo.id) unless !@items.empty?
  else
    # New todo - TODO: Filter on current project in project view
    @items = get_not_completed_for_predecessor(current_user)
  end
  render :inline => format_dependencies_as_json_for_auto_complete(@items)
end

#change_contextObject



402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# File 'app/controllers/todos_controller.rb', line 402

def change_context
  # change context if you drag a todo to another context
  @todo = current_user.todos.find(params[:id])
  @original_item = current_user.todos.build(@todo.attributes)  # create a (unsaved) copy of the original todo
  @context = current_user.contexts.find(params[:todo][:context_id])
  @todo.context = @context
  @saved = @todo.save

  @context_changed = true
  @status_message = t('todos.context_changed', :name => @context.name)
  determine_down_count
  determine_remaining_in_container_count(@original_item)

  respond_to do |format|
    format.js  { render :action => :update }
    format.xml { render :xml => @todo.to_xml(*todo_xml_params) }
  end
end

#check_deferredObject

Check for any due tickler items, activate them Called by periodically_call_remote



602
603
604
605
606
607
608
# File 'app/controllers/todos_controller.rb', line 602

def check_deferred
  @due_tickles = current_user.deferred_todos.find_and_activate_ready
  respond_to do |format|
    format.html { redirect_to home_path }
    format.js
  end
end

#check_for_next_todo(todo) ⇒ Object (private)



1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
# File 'app/controllers/todos_controller.rb', line 1066

def check_for_next_todo(todo)
  # check if this todo has a related recurring_todo. If so, create next todo
  new_recurring_todo = nil
  recurring_todo = nil
  if todo.from_recurring_todo?
    recurring_todo = todo.recurring_todo

    # check if there are active todos belonging to this recurring todo. only
    # add new one if all active todos are completed
    if recurring_todo.todos.active.count == 0

      # check for next todo either from the due date or the show_from date
      date_to_check = todo.due || todo.show_from

      # if both due and show_from are nil, check for a next todo from now
      date_to_check ||= Time.zone.now

      if recurring_todo.active? && recurring_todo.continues_recurring?(date_to_check)
        # shift the reference date to yesterday if date_to_check is furher in
        # the past. This is to make sure we do not get older todos for overdue
        # todos. I.e. checking a daily todo that is overdue with 5 days will
        # create a new todo which is overdue by 4 days if we don't shift the
        # date. Discard the time part in the compare. We pick yesterday so
        # that new todos due for today will be created instead of new todos
        # for tomorrow.
        date = date_to_check.at_midnight >= Time.zone.now.at_midnight ? date_to_check : Time.zone.now - 1.day

        new_recurring_todo = TodoFromRecurringTodo.new(current_user, recurring_todo).create(date)
      end
    end
  end
  return new_recurring_todo
end

#convert_to_projectObject



783
784
785
786
787
788
789
790
791
792
793
# File 'app/controllers/todos_controller.rb', line 783

def convert_to_project
  todo = current_user.todos.find(params[:id])
  @project = ProjectFromTodo.new(todo).create

  if @project.valid?
    redirect_to project_url(@project)
  else
    flash[:error] = "Could not create project from todo: #{@project.errors.full_messages[0]}"
    onsite_redirect_to request.env["HTTP_REFERER"] || root_url
  end
end

#count_old_due_empty(id) ⇒ Object (private)



1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
# File 'app/controllers/todos_controller.rb', line 1124

def count_old_due_empty(id)
  due_today_date = Time.zone.now
  due_this_week_date = Time.zone.now.end_of_week
  due_next_week_date = due_this_week_date + 7.days
  due_this_month_date = Time.zone.now.end_of_month

  case id
  when "due_today"
    return current_user.todos.not_completed.where('todos.due <= ?', due_today_date).count
  when "due_this_week"
    return current_user.todos.not_completed.where('todos.due > ? AND todos.due <= ?', due_today_date, due_this_week_date).count
  when "due_next_week"
    return current_user.todos.not_completed.where('todos.due > ? AND todos.due <= ?', due_this_week_date, due_next_week_date).count
  when "due_this_month"
    return current_user.todos.not_completed.where('todos.due > ? AND todos.due <= ?', due_next_week_date, due_this_month_date).count
  when "due_after_this_month"
    return current_user.todos.not_completed.where('todos.due > ?', due_this_month_date).count
  else
    raise Exception.new, "unknown due id for calendar: '#{id}'"
  end
end

#createObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'app/controllers/todos_controller.rb', line 86

def create
  @source_view = params['_source_view'] || 'todo'
  @default_context = current_user.contexts.where(:name => params['default_context_name']).first
  @default_project = current_user.projects.where(:name => params['default_project_name']).first if params['default_project_name'].present?

  @tag_name = params['_tag_name']

  is_multiple = params[:todo] && params[:todo][:multiple_todos] && !params[:todo][:multiple_todos].nil?
  if is_multiple
    create_multiple
  else
    p = Todos::TodoCreateParamsHelper.new(params, current_user)
    p.parse_dates unless mobile?
    tag_list = p.tag_list

    @todo = current_user.todos.build
    @todo.assign_attributes(p.attributes)
    p.add_errors(@todo)

    if @todo.errors.empty?
      @todo.add_predecessor_list(p.predecessor_list)
      @saved = @todo.save
      @todo.tag_with(tag_list) if @saved && tag_list.present?
      @todo.block! if @todo.uncompleted_predecessors?
    else
      @saved = false
    end

    @todo_was_created_deferred = @todo.deferred?
    @todo_was_created_blocked = @todo.pending?
    @not_done_todos = [@todo] if p.new_project_created || p.new_context_created
    @new_project_created = p.new_project_created
    @new_context_created = p.new_context_created

    respond_to do |format|
      format.html do
        redirect_to :action => "index"
      end
      format.m do
        @return_path = cookies[:mobile_url] ? cookies[:mobile_url] : mobile_path
        if @saved
          onsite_redirect_to @return_path
        else
          @projects = current_user.projects
          @contexts = current_user.contexts
          render :action => "new"
        end
      end
      format.js do
        if @saved
          determine_down_count
          @contexts = current_user.contexts
          @projects = current_user.projects
          @context = @todo.context
          @project = @todo.project
          @initial_context_name = params['default_context_name']
          @initial_project_name = params['default_project_name']
          @initial_tags = params['initial_tag_list']
          @status_message = t('todos.added_new_next_action')
          @status_message += ' ' + t('todos.to_tickler') if @todo.deferred?
          @status_message += ' ' + t('todos.in_pending_state') if @todo.pending?
          @status_message += ' ' + t('todos.in_hidden_state') if @todo.hidden?
          @status_message = t('todos.added_new_project') + ' / ' + @status_message if @new_project_created
          @status_message = t('todos.added_new_context') + ' / ' + @status_message if @new_context_created
        end
        render :action => 'create'
      end
      format.xml do
        if @saved
          head :created, :location => todo_url(@todo)
        else
          render_failure @todo.errors.full_messages.to_xml(root: "errors", skip_types: true).html_safe, 409
        end
      end
    end
  end
end

#create_multipleObject



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'app/controllers/todos_controller.rb', line 164

def create_multiple
  p = Todos::TodoCreateParamsHelper.new(params, current_user)
  tag_list = p.tag_list

  @not_done_todos = []
  @build_todos = []
  @todos = []
  errors = []
  @predecessor = nil
  validates = true

  # first build all todos and check if they would validate on save
  params[:todo][:multiple_todos].split("\n").map do |line|
    # Ignore blank lines
    if line.present?
      @todo = current_user.todos.build({ :description => line, :context_id => p.context_id, :project_id => p.project_id })
      validates &&= @todo.valid?

      @build_todos << @todo
    end
  end

  # if all todos validate, then save them and add predecessors and tags
  if validates
    @build_todos.each do |todo|
      @saved = todo.save
      validates &&= @saved

      if @predecessor && @saved && p.sequential?
        todo.add_predecessor(@predecessor)
        todo.block!
      end

      todo.tag_with(tag_list) if @saved && tag_list.present?

      @todos << todo
      @not_done_todos << todo if p.new_context_created || p.new_project_created
      @predecessor = todo
    end
  else
    @todos = @build_todos
    @saved = false
  end

  respond_to do |format|
    format.html { redirect_to :action => "index" }
    format.js do
      determine_down_count if @saved
      @contexts = current_user.contexts if p.new_context_created
      @projects = current_user.projects if p.new_project_created
      @new_project_created = p.new_project_created
      @new_context_created = p.new_context_created
      @initial_context_name = params['default_context_name']
      @initial_project_name = params['default_project_name']
      @initial_tags = params['initial_tag_list']
      if @saved && @todos.size > 0
        @default_tags = @todos[0].project.default_tags unless @todos[0].project.nil?
      else
        @multiple_error = @todos.size > 0 ? "" : t('todos.next_action_needed')
        @saved = false
        @default_tags = current_user.projects.where(:name => @initial_project_name).default_tags if @initial_project_name.present?
      end

      @status_message = @todos.size > 1 ? t('todos.added_new_next_action_plural') : t('todos.added_new_next_action_singular')
      @status_message = t('todos.added_new_project') + ' / ' + @status_message if p.new_project_created
      @status_message = t('todos.added_new_context') + ' / ' + @status_message if p.new_context_created

      render :action => 'create_multiple'
    end
    format.xml do
      if @saved
        head :created, :location => context_url(@todos[0].context)
      else
        render :xml => @todos[0].errors.to_xml, :status => 422
      end
    end
  end
end

#deferObject



711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
# File 'app/controllers/todos_controller.rb', line 711

def defer
  @source_view = params['_source_view'] || 'todo'
  numdays = params['days'].to_i

  @todo = current_user.todos.find(params[:id])
  # Create a (unsaved) copy of the original todo
  @original_item = current_user.todos.build(@todo.attributes)

  @todo_deferred_state_changed = true
  @new_context_created = false
  @due_date_changed = false
  @tag_was_removed = false
  @todo_hidden_state_changed = false
  @todo_was_deferred_from_active_state = @todo.show_from.nil?

  @todo.show_from = (@todo.show_from || @todo.user.date) + numdays.days
  @saved = @todo.save
  @status_message = t('todos.action_saved_to_tickler')

  determine_down_count
  determine_remaining_in_container_count(@todo)
  source_view do |page|
    page.project do
      @remaining_undone_in_project = current_user.projects.find(@todo.project_id).todos.not_completed.count
    end
    page.tag { determine_deferred_tag_count(params['_tag_name']) }
  end

  respond_to do |format|
    format.html { redirect_to :back }
    format.js { render :action => 'update' }
    format.m do
      notify(:notice, t("todos.action_deferred", :description => @todo.description))
      do_mobile_todo_redirection
    end
  end
end

#destroyObject



491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
# File 'app/controllers/todos_controller.rb', line 491

def destroy
  @source_view = params['_source_view'] || 'todo'
  @todo = current_user.todos.find(params['id'])
  # Create a (unsaved) copy of the original todo
  @original_item = current_user.todos.build(@todo.attributes)
  @context_id = @todo.context_id
  @project_id = @todo.project_id
  @todo_was_destroyed = true
  @todo_was_destroyed_from_deferred_state = @todo.deferred?
  @todo_was_destroyed_from_pending_state = @todo.pending?
  @todo_was_destroyed_from_deferred_or_pending_state = @todo_was_destroyed_from_deferred_state || @todo_was_destroyed_from_pending_state

  @uncompleted_predecessors = []
  @todo.uncompleted_predecessors.each do |predecessor|
    @uncompleted_predecessors << predecessor
  end

  activated_successor_count = 0
  @pending_to_activate = []
  @todo.pending_successors.each do |successor|
    if successor.uncompleted_predecessors.size == 1
      @pending_to_activate << successor
      activated_successor_count += 1
    end
  end

  @saved = @todo.destroy

  # check if this todo has a related recurring_todo. If so, create next todo
  @new_recurring_todo = check_for_next_todo(@todo) if @saved

  respond_to do |format|
    format.html do
      if @saved
        message = t('todos.action_deleted_success')
        if activated_successor_count > 0
          message += " activated #{pluralize(activated_successor_count, 'pending action')}"
        end
        notify :notice, message, 2.0
        redirect_to :action => 'index'
      else
        notify :error, t('todos.action_deleted_error'), 2.0
        redirect_to :action => 'index'
      end
    end
    format.js do
      if @saved
        determine_down_count
        if source_view_is_one_of(:todo, :deferred, :project, :context, :tag)
          determine_remaining_in_container_count(@todo)
        elsif source_view_is :calendar
          @original_item_due_id = get_due_id_for_calendar(@original_item.due)
          @old_due_empty = is_old_due_empty(@original_item_due_id)
        end
      end
      render
    end
    format.xml { render :body => '200 OK. Action deleted.', :status => 200 }
  end
end

#determine_changes_by_this_updateObject (private)



1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
# File 'app/controllers/todos_controller.rb', line 1250

def determine_changes_by_this_update
  @todo_was_activated_from_deferred_state = @todo.active? && @original_item.deferred?
  @todo_was_activated_from_pending_state = @todo.active? && @original_item.pending?
  @todo_was_deferred_from_active_state = @todo.deferred? && !@original_item.deferred?
  @todo_was_blocked_from_active_state = @todo.pending? && !@original_item.pending?

  @todo_deferred_state_changed = @original_item.deferred? != @todo.deferred?
  @todo_pending_state_changed = @original_item.pending? != @todo.pending?
  @todo_hidden_state_changed = @original_item.hidden? != @todo.hidden?

  @due_date_changed = @original_item.due != @todo.due

  source_view do |page|
    page.calendar do
      @old_due_empty = is_old_due_empty(@original_item_due_id)
      @new_due_id = get_due_id_for_calendar(@todo.due)
    end
    page.tag do
      @tag_name = params['_tag_name']
      @tag_was_removed = !@todo.has_tag?(@tag_name)
    end
    page.context do
      @todo_should_be_hidden = @todo_hidden_state_changed && @todo.hidden?
    end
  end
end

#determine_completed_countObject (private)



1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
# File 'app/controllers/todos_controller.rb', line 1047

def determine_completed_count
  todos = nil

  source_view do |from|
    from.todo    { todos = current_user.todos.not_hidden.completed }
    from.context { todos = find_completed(current_user.contexts, @todo.context_id, @todo.context.hidden?) }
    from.project { todos = find_completed(current_user.projects, @todo.project_id, @todo.project.hidden?) unless @todo.project_id.nil? }
    from.tag     { todos = current_user.todos.with_tag(@tag.id).completed }
  end

  @completed_count = todos.nil? ? 0 : todos.count
end

#determine_deferred_tag_count(tag_name) ⇒ Object (private)



1060
1061
1062
1063
1064
# File 'app/controllers/todos_controller.rb', line 1060

def determine_deferred_tag_count(tag_name)
  tag = Tag.where(:name => tag_name).first
  # tag.nil? should normally not happen, but is a workaround for #929
  @remaining_deferred_or_pending_count = tag.nil? ? 0 : current_user.todos.deferred.with_tag(tag.id).count
end

#determine_down_countObject (private)



924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
# File 'app/controllers/todos_controller.rb', line 924

def determine_down_count
  source_view do |from|
    from.todo do
      @down_count = current_user.todos.active.not_hidden.count
    end
    from.context do
      context_id = @original_item ? @original_item.context_id || @todo.context_id : @todo.context_id
      todos = current_user.contexts.find(context_id).todos.not_completed

      if @todo.context.hidden?
        # include hidden todos
        @down_count = todos.count
      else
        # exclude hidden_todos
        @down_count = todos.not_hidden.count
      end
    end
    from.project do
      unless @todo.project_id == nil
        @down_count = current_user.projects.find(@todo.project_id).todos.active_or_hidden.count
      end
    end
    from.deferred do
      @down_count = current_user.todos.deferred_or_blocked.count
    end
    from.tag do
      @tag_name = params['_tag_name']
      @tag = Tag.where(:name => @tag_name).first
      if @tag.nil?
        @tag = Tag.new(:name => @tag_name)
      end
      @down_count = current_user.todos.with_tag(@tag.id).active.not_hidden.count
    end
  end
end

#determine_remaining_in_container_count(todo = @todo) ⇒ Object (private)



980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
# File 'app/controllers/todos_controller.rb', line 980

def determine_remaining_in_container_count(todo = @todo)
  source_view do |from|
    from.deferred do
      todos_in_container, todos_in_target_container = find_todos_in_container_and_target_container(todo, @todo)
      @remaining_in_context = todos_in_container.deferred_or_blocked.count
      @target_context_count = todos_in_target_container.deferred_or_blocked.count
    end
    from.todo do
      todos_in_container, todos_in_target_container = find_todos_in_container_and_target_container(todo, @todo)
      @remaining_in_context = todos_in_container.active.not_hidden.count
      @target_context_count = todos_in_target_container.active.not_hidden.count
    end
    from.tag do
      tag = Tag.where(:name => params['_tag_name']).first
      tag = Tag.new(:name => params['tag']) if tag.nil?

      todos_in_container, todos_in_target_container = find_todos_in_container_and_target_container(todo, @todo)

      @remaining_in_context = todos_in_container.active.not_hidden.with_tag(tag.id).count
      @target_context_count = todos_in_target_container.active.not_hidden.with_tag(tag.id).count
      @remaining_hidden_count = current_user.todos.hidden.with_tag(tag.id).count
      @remaining_deferred_or_pending_count = current_user.todos.with_tag(tag.id).deferred_or_blocked.count
    end
    from.project do
      project_id = @project_changed ? @original_item.project_id : @todo.project_id
      @remaining_deferred_or_pending_count = current_user.projects.find(project_id).todos.deferred_or_blocked.count

      if @todo_was_completed_from_deferred_or_blocked_state
        @remaining_in_context = @remaining_deferred_or_pending_count
      else
        @remaining_in_context = current_user.projects.find(project_id).todos.active_or_hidden.count
      end

      @target_context_count = current_user.projects.find(project_id).todos.active.count
    end
    from.calendar do
      @target_context_count = @new_due_id.blank? ? 0 : count_old_due_empty(@new_due_id)
    end
    from.context do
      context = current_user.contexts.find(todo.context_id)
      @remaining_deferred_or_pending_count = context.todos.deferred_or_blocked.count

      remaining_actions_in_context = context.todos.reload.active
      remaining_actions_in_context = remaining_actions_in_context.not_hidden if !context.hidden?
      @remaining_in_context = remaining_actions_in_context.count

      if @todo_was_deferred_or_blocked
        actions_in_target = current_user.contexts.find(@todo.context_id).todos.reload.active
        actions_in_target = actions_in_target.not_hidden if !context.hidden?
      else
        actions_in_target = @todo.context.todos.deferred_or_blocked
      end
      @target_context_count = actions_in_target.count
    end
    from.done do
      @remaining_in_context = DoneTodos.remaining_in_container(current_user.todos, @original_completed_period)
    end
    from.all_done { @remaining_in_context = current_user.todos.completed.count }
  end
end

#do_mobile_todo_redirectionObject (private)



824
825
826
827
828
829
830
831
832
# File 'app/controllers/todos_controller.rb', line 824

def do_mobile_todo_redirection
  if cookies[:mobile_url]
    old_path = cookies[:mobile_url]
    cookies[:mobile_url] = { :value => nil, :secure => SITE_CONFIG['secure_cookies'] }
    onsite_redirect_to old_path
  else
    onsite_redirect_to todos_path(:format => 'm')
  end
end

#doneObject



552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
# File 'app/controllers/todos_controller.rb', line 552

def done
  @source_view = 'done'
  @page_title = t('todos.completed_tasks_title')

  @done_today, @done_rest_of_week, @done_rest_of_month = DoneTodos.done_todos_for_container(current_user.todos)
  @count = @done_today.size + @done_rest_of_week.size + @done_rest_of_month.size

  respond_to do |format|
    format.html
    format.xml do
      completed_todos = current_user.todos.completed
      render :xml => completed_todos.to_xml(*[todo_xml_params[0].merge({ :root => :todos })])
    end
  end
end

#done_by_tag_setupObject



694
695
696
697
698
699
# File 'app/controllers/todos_controller.rb', line 694

def done_by_tag_setup
  @source_view = params['_source_view'] || 'done'
  @tag_name = sanitize(params[:name]) # sanitize to prevent XSS vunerability!
  @page_title = t('todos.all_completed_tagged_page_title', :tag_name => @tag_name)
  @tag = Tag.where(:name => @tag_name).first_or_create
end

#done_tagObject



674
675
676
677
678
679
680
681
682
683
684
685
# File 'app/controllers/todos_controller.rb', line 674

def done_tag
  done_by_tag_setup

  completed_todos = current_user.todos.completed.with_tag(@tag.id)

  @done_today = get_done_today(completed_todos)
  @done_rest_of_week = get_done_rest_of_week(completed_todos)
  @done_rest_of_month = get_done_rest_of_month(completed_todos)
  @count = @done_today.size + @done_rest_of_week.size + @done_rest_of_month.size

  render :template => 'todos/done'
end

#editObject



243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'app/controllers/todos_controller.rb', line 243

def edit
  @todo = current_user.todos.find(params['id'])
  @source_view = params['_source_view'] || 'todo'
  @tag_name = params['_tag_name']
  respond_to do |format|
    format.js
    format.m do
      @projects = current_user.projects.active
      @contexts = current_user.contexts
      @edit_mobile = true
      @return_path = cookies[:mobile_url] ? cookies[:mobile_url] : mobile_path
    end
  end
end

#filter_format_for_tag_viewObject (private)



872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
# File 'app/controllers/todos_controller.rb', line 872

def filter_format_for_tag_view
  # routes for tag view do not set :format
  if params[:name] =~ /.*\.m$/
    set_format_for_tag_view(:m)
  elsif params[:name] =~ /.*\.txt$/
    set_format_for_tag_view(:txt)
    # set content-type to text/plain or it remains text/html
    response.headers["Content-Type"] = 'text/plain'
  elsif params[:format].nil?
    # if no format is given, default to html
    # note that if url has ?format=m, we should not overwrite it here
    request.format = :html
    params[:format] = :html
  end
end

#filter_to_contextObject



610
611
612
613
# File 'app/controllers/todos_controller.rb', line 610

def filter_to_context
  context = current_user.contexts.find(params['context']['id'])
  redirect_to context_todos_path(context, :format => 'm')
end

#filter_to_projectObject



615
616
617
618
# File 'app/controllers/todos_controller.rb', line 615

def filter_to_project
  project = current_user.projects.find(params['project']['id'])
  redirect_to project_todos_path(project, :format => 'm')
end

#find_and_activate_readyObject (private)



840
841
842
# File 'app/controllers/todos_controller.rb', line 840

def find_and_activate_ready
  current_user.deferred_todos.find_and_activate_ready
end

#find_completed(relation, id, include_hidden) ⇒ Object (private)



1041
1042
1043
1044
1045
# File 'app/controllers/todos_controller.rb', line 1041

def find_completed(relation, id, include_hidden)
  todos = relation.find(id).todos.completed
  todos = todos.not_hidden if !include_hidden
  return todos
end

#find_todos_in_container_and_target_container(todo, target_todo) ⇒ Object (private)



969
970
971
972
973
974
975
976
977
978
# File 'app/controllers/todos_controller.rb', line 969

def find_todos_in_container_and_target_container(todo, target_todo)
  if @group_view_by == 'context'
    todos_in_container = current_user.contexts.find(todo.context_id).todos
    todos_in_target_container = current_user.contexts.find(@todo.context_id).todos
  else
    todos_in_container = find_todos_in_project_container(todo)
    todos_in_target_container = find_todos_in_project_container(@todo)
  end
  return todos_in_container, todos_in_target_container
end

#find_todos_in_project_container(todo) ⇒ Object (private)



960
961
962
963
964
965
966
967
# File 'app/controllers/todos_controller.rb', line 960

def find_todos_in_project_container(todo)
  if todo.project.nil?
    # container with todos without project
    todos_in_container = current_user.todos.where(:project_id => nil)
  else
    todos_in_container = current_user.projects.find(todo.project_id).todos
  end
end

#find_todos_with_tag_expr(tag_expr) ⇒ Object (private)



908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
# File 'app/controllers/todos_controller.rb', line 908

def find_todos_with_tag_expr(tag_expr)
  # Optimize for the common case of selecting only one tag
  if @single_tag
    tag = current_user.tags.where(:name => @tag_name).first
    tag_id = tag.nil? ? -1 : tag.id
    return current_user.todos.with_tag(tag_id)
  end

  tag_ids = get_ids_from_tag_expr(tag_expr)
  todos = current_user.todos
  tag_ids.each do |ids|
    todos = todos.with_tags(ids) unless ids.nil? || ids.empty?
  end
  return todos
end

#get_done_in_period(completed_todos, before, after, includes = { :include => Todo::DEFAULT_INCLUDES }) ⇒ Object (private)



1283
1284
1285
# File 'app/controllers/todos_controller.rb', line 1283

def get_done_in_period(completed_todos, before, after, includes = { :include => Todo::DEFAULT_INCLUDES })
  completed_todos.completed_before(before).completed_after(after).includes(includes[:include])
end

#get_done_rest_of_month(completed_todos, includes = { :include => Todo::DEFAULT_INCLUDES }) ⇒ Object (private)

all completed todos [begin_of_month, begin_of_week]



1293
1294
1295
# File 'app/controllers/todos_controller.rb', line 1293

def get_done_rest_of_month(completed_todos, includes = { :include => Todo::DEFAULT_INCLUDES })
  get_done_in_period(completed_todos, Time.zone.now.beginning_of_week, Time.zone.now.beginning_of_month)
end

#get_done_rest_of_week(completed_todos, includes = { :include => Todo::DEFAULT_INCLUDES }) ⇒ Object (private)

all completed todos [begin_of_week, start_of_today]



1288
1289
1290
# File 'app/controllers/todos_controller.rb', line 1288

def get_done_rest_of_week(completed_todos, includes = { :include => Todo::DEFAULT_INCLUDES })
  get_done_in_period(completed_todos, Time.zone.now.beginning_of_day, Time.zone.now.beginning_of_week)
end

#get_done_today(completed_todos, includes = { :include => Todo::DEFAULT_INCLUDES }) ⇒ Object (private)

all completed todos [today@00:00, today@now]



1278
1279
1280
1281
# File 'app/controllers/todos_controller.rb', line 1278

def get_done_today(completed_todos, includes = { :include => Todo::DEFAULT_INCLUDES })
  start_of_this_day = Time.zone.now.beginning_of_day
  completed_todos.completed_after(start_of_this_day).includes(includes[:include])
end

#get_due_id_for_calendar(due) ⇒ Object (private)



1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
# File 'app/controllers/todos_controller.rb', line 1100

def get_due_id_for_calendar(due)
  return "" if due.nil?
  due_today_date = Time.zone.now
  due_this_week_date = Time.zone.now.end_of_week
  due_next_week_date = due_this_week_date + 7.days
  due_this_month_date = Time.zone.now.end_of_month
  if due <= due_today_date
    new_due_id = "due_today"
  elsif due <= due_this_week_date
    new_due_id = "due_this_week"
  elsif due <= due_next_week_date
    new_due_id = "due_next_week"
  elsif due <= due_this_month_date
    new_due_id = "due_this_month"
  else
    new_due_id = "due_after_this_month"
  end
  return new_due_id
end

#get_ids_from_tag_expr(tag_expr) ⇒ Object (private)



895
896
897
898
899
900
901
902
903
904
905
906
# File 'app/controllers/todos_controller.rb', line 895

def get_ids_from_tag_expr(tag_expr)
  ids = []
  tag_expr.each do |tag_list|
    id_list = []
    tag_list.each do |tag|
      tag = Tag.where(:name => tag).first
      id_list << tag.id if tag
    end
    ids << id_list
  end
  return ids
end

#get_not_completed_for_predecessor(relation, todo_id = nil) ⇒ Object



756
757
758
759
760
761
762
763
764
765
# File 'app/controllers/todos_controller.rb', line 756

def get_not_completed_for_predecessor(relation, todo_id = nil)
  items = relation.todos.not_completed
    .where('(LOWER(todos.description) ' + Common.like_operator + '?)', "%#{params[:term].downcase}%")
  items = items.where("AND NOT(todos.id=?)", todo_id) unless todo_id.nil?

  items
    .includes(:context, :project)
    .reorder('description ASC')
    .limit(10)
end

#get_not_done_todosObject (private)



1297
1298
1299
# File 'app/controllers/todos_controller.rb', line 1297

def get_not_done_todos
  Todos::UndoneTodosQuery.new(current_user).query(params)
end

#get_params_for_tag_viewObject (private)



849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
# File 'app/controllers/todos_controller.rb', line 849

def get_params_for_tag_view
  filter_format_for_tag_view

  # Don't use sanitize here because these are only used for a DB query.
  @tag_expr = []
  # Tag conditions handled as OR.
  @tag_expr << params[:name].split(',')

  # Additional tag condition(s) handled as AND.
  @tag_expr << params[:and].split(',') if params[:and]
  i = 1
  while params['and' + i.to_s]
    @tag_expr << params['and' + i.to_s].split(',')
    i += 1
  end

  @single_tag = @tag_expr.size == 1 && @tag_expr[0].size == 1

  # These are used in the templates, sanitise to prevent XSS.
  @tag_name = sanitize(@tag_expr[0][0])
  @tag_title = sanitize(@single_tag ? @tag_name : tag_title(@tag_expr))
end

#get_todo_from_paramsObject (private)



834
835
836
837
838
# File 'app/controllers/todos_controller.rb', line 834

def get_todo_from_params
  # TODO: this was a :append_before but was removed to tune performance per
  # method. Reconsider re-enabling it
  @todo = current_user.todos.find(params['id'])
end

#indexObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/controllers/todos_controller.rb', line 8

def index
  @source_view = params['_source_view'] || 'todo'

  init_data_for_sidebar unless mobile?

  @todos = current_user.todos.includes(Todo::DEFAULT_INCLUDES)
  if params[:context_id]
    context = current_user.contexts.find(params[:context_id])
    @todos = @todos.where('context_id' => context.id)
  end
  if params[:project_id]
    project = current_user.projects.find(params[:project_id])
    @todos = @todos.where('project_id' => project.id)
  end
  @todos = @todos.limit(sanitize(params[:limit])) if params[:limit]

  @not_done_todos = get_not_done_todos

  @projects = current_user.projects.includes(:default_context)
  @contexts = current_user.contexts
  @contexts_to_show = current_user.contexts.active
  @projects_to_show = current_user.projects.active

  # If you've set no_completed to zero, the completed items box isn't shown
  # on the home page
  max_completed = current_user.prefs.show_number_completed
  @done = current_user.todos.completed.limit(max_completed).includes(Todo::DEFAULT_INCLUDES) unless max_completed == 0

  respond_to do |format|
    format.html do
      @page_title = t('todos.task_list_title')
      # Set count badge to number of not-done, not hidden context items
      @count = current_user.todos.active.not_hidden.count(:all)
      @todos_without_project = @not_done_todos.select { |t| t.project.nil? }
    end
    format.m do
      @page_title = t('todos.mobile_todos_page_title')
      @home = true

      cookies[:mobile_url] = { :value => request.fullpath, :secure => SITE_CONFIG['secure_cookies'] }
      determine_down_count

      render :action => 'index'.freeze
    end
    format.text do
      # somehow passing Mime[:text] using content_type to render does not work
      headers['Content-Type'.freeze] = Mime[:text].to_s
      render :content_type => Mime[:text]
    end
    format.xml do
      @xml_todos = params[:limit_to_active_todos] ? @not_done_todos : @todos
      render :xml => @xml_todos.to_xml(*[todo_xml_params[0].merge({ :root => :todos })])
    end
    format.any(:rss, :atom) do
      @feed_title = 'Tracks Actions'.freeze
      @feed_description = "Actions for #{current_user.display_name}"
    end
    format.ics
  end
end

#is_old_due_empty(id) ⇒ Object (private)



1120
1121
1122
# File 'app/controllers/todos_controller.rb', line 1120

def is_old_due_empty(id)
  return 0 == count_old_due_empty(id)
end

#list_deferredObject



576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
# File 'app/controllers/todos_controller.rb', line 576

def list_deferred
  @source_view = 'deferred'
  @page_title = t('todos.deferred_tasks_title')

  @contexts_to_show = @contexts = current_user.contexts
  @projects_to_show = @projects = current_user.projects

  includes = params[:format] == 'xml' ? [:context, :project] : Todo::DEFAULT_INCLUDES

  @not_done_todos = current_user.todos.deferred.includes(includes).reorder('show_from') + current_user.todos.pending.includes(includes)
  @todos_without_project = @not_done_todos.select { |t| t.project.nil? }
  @down_count = @count = @not_done_todos.size

  respond_to do |format|
    format.html do
      init_not_done_counts
      init_hidden_todo_counts
      init_data_for_sidebar unless mobile?
    end
    format.m
    format.xml { render :xml => @not_done_todos.to_xml(*[todo_xml_params[0].merge({ :root => :todos })]) }
  end
end

#list_hiddenObject



749
750
751
752
753
754
# File 'app/controllers/todos_controller.rb', line 749

def list_hidden
  @hidden = current_user.todos.hidden
  respond_to do |format|
    format.xml { render :xml => @hidden.to_xml(*[todo_xml_params[0].merge({ :root => :todos })]) }
  end
end

#newObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/controllers/todos_controller.rb', line 69

def new
  @projects = current_user.projects.active
  @contexts = current_user.contexts
  respond_to do |format|
    format.m do
      @new_mobile = true
      @return_path = cookies[:mobile_url] ? cookies[:mobile_url] : mobile_path
      @mobile_from_context = current_user.contexts.find(params[:from_context]) if params[:from_context]
      @mobile_from_project = current_user.projects.find(params[:from_project]) if params[:from_project]
      if params[:from_project] && !params[:from_context]
        # we have a project but not a context -> use the default context
        @mobile_from_context = @mobile_from_project.default_context
      end
    end
  end
end

#onsite_redirect_to(destination) ⇒ Object (private)



1301
1302
1303
1304
1305
1306
1307
1308
1309
# File 'app/controllers/todos_controller.rb', line 1301

def onsite_redirect_to(destination)
  uri = URI.parse(destination)

  if uri.query.present?
    redirect_to("#{uri.path}?#{uri.query}")
  else
    redirect_to(uri.path)
  end
end

#parse_date_for_update(date, error_msg) ⇒ Object (private)



1193
1194
1195
1196
1197
1198
1199
# File 'app/controllers/todos_controller.rb', line 1193

def parse_date_for_update(date, error_msg)
  begin
    parse_date_per_user_prefs(date)
  rescue
    @todo.errors.add(:base, error_msg)
  end
end

#provide_project_or_context_for_viewObject



482
483
484
485
486
487
488
489
# File 'app/controllers/todos_controller.rb', line 482

def provide_project_or_context_for_view
  # see application_helper:source_view_key, used in shown partials
  if source_view_is :project
    @project = @todo.project
  elsif source_view_is :context
    @context = @todo.context
  end
end

#remove_predecessorObject



291
292
293
294
295
296
297
298
299
300
301
302
# File 'app/controllers/todos_controller.rb', line 291

def remove_predecessor
  @source_view = params['_source_view'] || 'todo'
  @todo = current_user.todos.includes(Todo::DEFAULT_INCLUDES).find(params['id'])
  @predecessor = current_user.todos.find(params['predecessor'])
  @predecessors = @predecessor.predecessors
  @successor = @todo
  @removed = @successor.remove_predecessor(@predecessor)
  determine_remaining_in_container_count(@todo)
  respond_to do |format|
    format.js
  end
end

#set_format_for_tag_view(format) ⇒ Object (private)



888
889
890
891
892
893
# File 'app/controllers/todos_controller.rb', line 888

def set_format_for_tag_view(format)
    # tag name ends with .m, set format to :m en remove .m from name
    request.format = format
    params[:format] = format
    params[:name] = params[:name].chomp(".#{format.to_s}")
end

#set_group_view_byObject (private)



820
821
822
# File 'app/controllers/todos_controller.rb', line 820

def set_group_view_by
  @group_view_by = params['_group_view_by'] || cookies['group_view_by'] || 'context'
end

#showObject



258
259
260
261
262
263
264
# File 'app/controllers/todos_controller.rb', line 258

def show
  @todo = current_user.todos.find(params['id'])
  respond_to do |format|
    format.m { render :action => 'show' }
    format.xml { render :xml => @todo.to_xml(*[todo_xml_params[0].merge({ :root => :todo })]) }
  end
end

#show_notesObject



795
796
797
798
799
800
801
802
# File 'app/controllers/todos_controller.rb', line 795

def show_notes
  @todo = current_user.todos.find(params['id'])
  @return_path = cookies[:mobile_url] ? cookies[:mobile_url] : mobile_path
  respond_to do |format|
    format.html { redirect_to home_path, "Viewing note of todo is not implemented" }
    format.m { render :action => "show_notes" }
  end
end

#tagObject

/todos/tag/[tag_name] shows all the actions tagged with tag_name



621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
# File 'app/controllers/todos_controller.rb', line 621

def tag
  get_params_for_tag_view
  @page_title = t('todos.tagged_page_title', :tag_name => @tag_title)
  @source_view = params['_source_view'] || 'tag'

  init_data_for_sidebar unless mobile?

  todos_with_tag_ids = find_todos_with_tag_expr(@tag_expr)

  @not_done_todos = todos_with_tag_ids
    .active.not_hidden
    .reorder(Arel.sql('todos.due IS NULL, todos.due ASC, todos.created_at ASC'))
    .includes(Todo::DEFAULT_INCLUDES)
  @hidden_todos = todos_with_tag_ids
    .hidden
    .reorder(Arel.sql('todos.completed_at DESC, todos.created_at DESC'))
    .includes(Todo::DEFAULT_INCLUDES)
  @deferred_todos = todos_with_tag_ids
    .deferred
    .reorder(Arel.sql('todos.show_from ASC, todos.created_at DESC'))
    .includes(Todo::DEFAULT_INCLUDES)
  @pending_todos = todos_with_tag_ids
    .blocked
    .reorder(Arel.sql('todos.show_from ASC, todos.created_at DESC'))
    .includes(Todo::DEFAULT_INCLUDES)
  @todos_without_project = @not_done_todos.select { |t| t.project.nil? }

  # If you've set no_completed to zero, the completed items box isn't shown on
  # the tag page
  @done = todos_with_tag_ids.completed
    .limit(current_user.prefs.show_number_completed)
    .reorder('todos.completed_at DESC')
    .includes(Todo::DEFAULT_INCLUDES)

  @projects = current_user.projects
  @contexts = current_user.contexts
  @contexts_to_show = @contexts.active
  @projects_to_show = @projects.active

  # Set defaults for new_action
  @initial_tags = @tag_name

  # Set count badge to number of items with this tag
  @not_done_todos.empty? ? @count = 0 : @count = @not_done_todos.size
  @down_count = @count

  respond_to do |format|
    format.html
    format.m { cookies[:mobile_url] = { :value => request.fullpath, :secure => SITE_CONFIG['secure_cookies'] } }
    format.text { render :action => 'index', :layout => false, :content_type => Mime[:text] }
  end
end

#tag_title(tag_expr) ⇒ Object (private)



844
845
846
847
# File 'app/controllers/todos_controller.rb', line 844

def tag_title(tag_expr)
  and_list = tag_expr.inject([]) { |s, tag_list| s << tag_list.join(',') }
  return and_list.join(' AND ')
end

#tagsObject



701
702
703
704
705
706
707
708
709
# File 'app/controllers/todos_controller.rb', line 701

def tags
  tags_beginning = current_user.tags.where(Tag.arel_table[:name].matches("#{params[:term]}%"))
  tags_all = current_user.tags.where(Tag.arel_table[:name].matches("%#{params[:term]}%"))
  tags_all -= tags_beginning

  respond_to do |format|
    format.autocomplete { render :body => for_autocomplete(tags_beginning + tags_all, params[:term]) }
  end
end

#toggle_checkObject

Toggles the ‘done’ status of the action



306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
# File 'app/controllers/todos_controller.rb', line 306

def toggle_check
  @todo = current_user.todos.find(params['id'])

  @source_view = params['_source_view'] || 'todo'

  # Create a (unsaved) copy of the original todo, before it was toggled
  @original_item = current_user.todos.build(@todo.attributes)
  @original_completed_period = DoneTodos.completed_period(@todo.completed_at)

  @todo_was_completed_from_deferred_or_blocked_state =
    @original_item.deferred? || @original_item.pending?

  @saved = @todo.toggle_completion!

  # As a result of toggling the done status of the todo, have we shifted from
  # "Done" to "Blocked"?
  @todo_was_blocked_from_completed_state = @todo.pending?

  # Check if this todo has a related recurring_todo. If so, create next todo
  @new_recurring_todo = check_for_next_todo(@todo) if @saved

  @predecessors = @todo.uncompleted_predecessors
  if @saved
    if @todo.completed?
      @pending_to_activate = @todo.activate_pending_todos
    else
      @active_to_block = @todo.block_successors
    end
  end

  respond_to do |format|
    format.js do
      if @saved
        determine_remaining_in_container_count(@todo)
        determine_down_count
        determine_completed_count
        determine_deferred_tag_count(params['_tag_name']) if source_view_is(:tag)
        @wants_redirect_after_complete = @todo.completed? && !@todo.project_id.nil? && current_user.prefs.show_project_on_todo_done && !source_view_is(:project)
        if source_view_is :calendar
          @original_item_due_id = get_due_id_for_calendar(@original_item.due)
          @old_due_empty = is_old_due_empty(@original_item_due_id)
        end
      end
      render
    end
    format.xml { render :xml => @todo.to_xml(*todo_xml_params) }
    format.html do
      if @saved
        # TODO: I think this will work, but can't figure out how to test it
        notify(:notice, t("todos.action_marked_complete", :description => @todo.description, :completed => @todo.completed? ? 'complete' : 'incomplete'))
        redirect_to :action => "index"
      else
        notify(:notice, t("todos.action_marked_complete_error", :description => @todo.description, :completed => @todo.completed? ? 'complete' : 'incomplete'), "index")
        redirect_to :action => "index"
      end
    end
    format.m do
      if @saved
        if cookies[:mobile_url]
          old_path = cookies[:mobile_url]
          cookies[:mobile_url] = { :value => nil, :secure => SITE_CONFIG['secure_cookies'] }
          notify(:notice, t("todos.action_marked_complete", :description => @todo.description, :completed => @todo.completed? ? 'complete' : 'incomplete'))
          onsite_redirect_to old_path
        else
          notify(:notice, t("todos.action_marked_complete", :description => @todo.description, :completed => @todo.completed? ? 'complete' : 'incomplete'))
          onsite_redirect_to todos_path(:format => 'm')
        end
      else
        render :action => "edit", :format => :m
      end
    end
  end
end

#toggle_starObject



380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# File 'app/controllers/todos_controller.rb', line 380

def toggle_star
  @todo = current_user.todos.find(params['id'])
  @todo.toggle_star!
  @saved = true # cannot determine error
  respond_to do |format|
    format.js
    format.xml { render :xml => @todo.to_xml(*todo_xml_params) }
    format.html { redirect_to request.referrer }
    format.m do
      if cookies[:mobile_url]
        old_path = cookies[:mobile_url]
        cookies[:mobile_url] = { :value => nil, :secure => SITE_CONFIG['secure_cookies'] }
        notify(:notice, "Star toggled")
        onsite_redirect_to old_path
      else
        notify(:notice, "Star toggled")
        onsite_redirect_to todos_path(:format => 'm')
      end
    end
  end
end

#updateObject



421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
# File 'app/controllers/todos_controller.rb', line 421

def update
  @source_view = params['_source_view'] || 'todo'

  @todo = current_user.todos.find(params['id'])
  @original_item = current_user.todos.build(@todo.attributes)  # create a (unsaved) copy of the original todo
  @original_item_due_id = get_due_id_for_calendar(@original_item.due)

  update_tags
  update_project
  update_context
  update_due_and_show_from_dates
  update_completed_state
  update_dependencies
  update_attributes_of_todo

  begin
    @saved = @todo.save!
  rescue ActiveRecord::RecordInvalid => exception
    record = exception.record
    if record.is_a?(Dependency)
      record.errors.each { |key, value| @todo.errors.add(key, value) }
    end
    @saved = false
  end

  provide_project_or_context_for_view

  # this is set after save and cleared after reload, so save it here
  @removed_predecessors = @todo.removed_predecessors

  @todo.reload # refresh context and project object too (not only their id's)

  update_dependency_state
  if @project_changed
    @remaining_undone_in_project = current_user.projects.find(@original_item.project_id).todos.active.count if source_view_is :project
  end

  determine_changes_by_this_update
  determine_remaining_in_container_count((@context_changed || @project_changed) ? @original_item : @todo)
  determine_down_count
  determine_deferred_tag_count(sanitize(params['_tag_name'])) if source_view_is(:tag)

  @todo.touch_predecessors if @original_item.description != @todo.description

  respond_to do |format|
    format.js do
      @status_message = @todo.deferred? ? t('todos.action_saved_to_tickler') : t('todos.action_saved')
      @status_message = t('todos.added_new_project') + ' / ' + @status_message if @new_project_created
      @status_message = t('todos.added_new_context') + ' / ' + @status_message if @new_context_created
    end
    format.xml { render :xml => @todo.to_xml(*todo_xml_params) }
    format.m do
      if @saved
        do_mobile_todo_redirection
      else
        render :action => "edit", :format => :m
      end
    end
  end
end

#update_attributes_of_todoObject (private)



1243
1244
1245
1246
1247
1248
# File 'app/controllers/todos_controller.rb', line 1243

def update_attributes_of_todo
  # TODO: duplication with todo_create_params_helper
  @todo.attributes = params.require(:todo).permit(
      :context_id, :project_id, :description, :notes,
      :due, :show_from, :state)
end

#update_completed_stateObject (private)



1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
# File 'app/controllers/todos_controller.rb', line 1209

def update_completed_state
  if params['done'] == '1' && !@todo.completed?
    @todo.complete!
    @todo.activate_pending_todos
  end
  # strange. if checkbox is not checked, there is no 'done' in params.
  # Therefore I've used the negation
  if !(params['done'] == '1') && @todo.completed?
    @todo.activate!
    @todo.block_successors
  end
end

#update_contextObject (private)



1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
# File 'app/controllers/todos_controller.rb', line 1167

def update_context
  @context_changed = false
  if params['todo']['context_id'].blank? && params['context_name'].present?
    @context = current_user.contexts.where(:name => params['context_name'].strip).first
    if @context.nil?
      @new_context = current_user.contexts.build
      @new_context.name = params['context_name'].strip
      @new_context.save
      @new_context_created = true
      @new_container = @new_context
      @not_done_todos = [@todo]
      @context = @new_context
    end
    params["todo"]["context_id"] = @context.id
    @context_changed = @original_item.context_id != params["todo"]["context_id"] = @context.id
  end
end

#update_date_for_update(key) ⇒ Object (private)



1201
1202
1203
# File 'app/controllers/todos_controller.rb', line 1201

def update_date_for_update(key)
  params['todo'][key] = params["todo"].key?(key) ? parse_date_for_update(params["todo"][key], t("todos.error.invalid_#{key}_date")) : ""
end

#update_dependenciesObject (private)



1222
1223
1224
# File 'app/controllers/todos_controller.rb', line 1222

def update_dependencies
  @todo.add_predecessor_list(params[:predecessor_list])
end

#update_dependency_stateObject (private)



1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
# File 'app/controllers/todos_controller.rb', line 1226

def update_dependency_state
  # assumes @todo.save was called so that the predecessor_list is persistent
  original_item_predecessor_list =
    @original_item.predecessors
      .map { |t| t.specification }
      .join(', ')

  if original_item_predecessor_list != params[:predecessor_list]
    # Possible state change with new dependencies
    if @todo.uncompleted_predecessors.empty?
      @todo.activate! if @todo.state == 'pending' # Activate pending if no uncompleted predecessors
    else
      @todo.block! if @todo.state == 'active' # Block active if we got uncompleted predecessors
    end
  end
end

#update_due_and_show_from_datesObject (private)



1205
1206
1207
# File 'app/controllers/todos_controller.rb', line 1205

def update_due_and_show_from_dates
  %w{ due show_from }.each { |date| update_date_for_update(date) }
end

#update_projectObject (private)



1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
# File 'app/controllers/todos_controller.rb', line 1146

def update_project
  @project_changed = false
  if params['todo']['project_id'].blank? && !params['project_name'].nil?
    if params['project_name'].blank?
      project = Project.null_object
    else
      project = current_user.projects.where(:name => params['project_name'].strip).first
      unless project
        project = current_user.projects.build
        project.name = params['project_name'].strip
        project.save
        @new_project_created = true
        @new_container = project
        @not_done_todos = [@todo]
      end
    end
    params["todo"]["project_id"] = project.id
    @project_changed = @original_item.project_id != params["todo"]["project_id"] = project.id
  end
end

#update_tagsObject (private)



1185
1186
1187
1188
1189
1190
1191
# File 'app/controllers/todos_controller.rb', line 1185

def update_tags
  if params[:tag_list]
    @todo.tag_with(params[:tag_list])
    # Force a reload for proper rendering
    @todo.tags.reload
  end
end