Class: DataController

Inherits:
ApplicationController show all
Defined in:
app/controllers/data_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_group_view_by, #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

#adjust_time(timestring) ⇒ Object

adjusts time to utc



201
202
203
204
205
206
207
# File 'app/controllers/data_controller.rb', line 201

def adjust_time(timestring)
  if (timestring == '') || (timestring == nil)
    return nil
  else
    return Time.parse(timestring + 'UTC')
  end
end

#csv_actionsObject

export all actions as csv



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'app/controllers/data_controller.rb', line 126

def csv_actions
  content_type = 'text/csv'
  CSV.generate(result = "") do |csv|
    csv << ["id", "Context", "Project", "Description", "Notes", "Tags",
      "Created at", "Due", "Completed at", "User ID", "Show from",
      "state"]
    current_user.todos.includes(:context, :project, :taggings, :tags).each do |todo|
      csv << [todo.id, todo.context.name,
        todo.project_id.nil? ? "" : todo.project.name,
        todo.description,
        todo.notes, todo.tags.collect { |t| t.name }.join(', '),
        todo.created_at.to_formatted_s(:db),
        todo.due? ? todo.due.to_formatted_s(:db) : "",
        todo.completed_at? ? todo.completed_at.to_formatted_s(:db) : "",
        todo.user_id,
        todo.show_from? ? todo.show_from.to_formatted_s(:db) : "",
        todo.state]
    end
  end
  send_data(result, :filename => "todos.csv", :type => content_type)
end

#csv_importObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/controllers/data_controller.rb', line 55

def csv_import
  begin
    filename = sanitize_filename(params[:file])
    path_and_file = Rails.root.join('public', 'uploads', 'csv', filename)
    case params[:import_to]
    when 'projects'
      count = Project.import path_and_file, params, current_user
      flash[:notice] = t 'data.import.projects_count', count: count
    when 'todos'
      count = Todo.import path_and_file, params, current_user
      if !count
        flash[:error] = t('data.import.errors.no_context')
      else
        flash[:notice] = t 'data.import.todos_count', count: count
      end
    else
      flash[:error] = t('data.import.errors.invalid_destination')
    end
  rescue Exception => e
    flash[:error] = t 'data.import.errors.invalid_destination', e: e
  end
  File.delete(path_and_file)
  redirect_to import_data_path
end

#csv_mapObject



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
# File 'app/controllers/data_controller.rb', line 10

def csv_map
  if params[:file].blank?
    flash[:notice] = t "data.import.errors.file_blank"
    redirect_back fallback_location: root_path
  else
    @import_to = params[:import_to]

    begin
      # Get column headers and format as [['name', column_number]...]
      i = -1
      @headers = import_headers(params[:file].path).collect { |v| [v, i += 1] }
      @headers.unshift ['', i]
    rescue Exception => e
      flash[:error] = t "data.import.errors.invalid_csv", e: e
      redirect_back fallback_location: root_path
      return
    end

    # Save file for later
    begin
      uploaded_file = params[:file]
      @filename = sanitize_filename(uploaded_file.original_filename)
      path_and_file = Rails.root.join('public', 'uploads', 'csv', @filename)
      File.open(path_and_file, "wb") { |f| f.write(uploaded_file.read) }
    rescue Exception => e
      flash[:error] = t "data.import.errors.save_error", path_and_file: path_and_file, e: e
      redirect_back fallback_location: root_path
      return
    end

    case @import_to
    when 'projects'
      @labels = [:name, :description]
    when 'todos'
      @labels = [:description, :context, :project, :notes, :created_at, :due, :completed_at]
    else
      flash[:error] = t "data.import.errors.invalid_destination"
      redirect_back fallback_location: root_path
    end
    respond_to do |format|
      format.html
    end
  end
end

#csv_notesObject

export all notes as csv



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'app/controllers/data_controller.rb', line 149

def csv_notes
  content_type = 'text/csv'
  CSV.generate(result = "") do |csv|
    csv << ["id", "User ID", "Project", "Note", "Created at", "Updated at"]
    # had to remove project include because it's association order is leaking
    # through and causing an ambiguous column ref even with_exclusive_scope
    # didn't seem to help -JamesKebinger
    current_user.notes.reorder("notes.created_at").each do |note|
      # Format dates in ISO format for easy sorting in spreadsheet Print
      # context and project names for easy viewing
      csv << [note.id, note.user_id,
        note.project_id = note.project_id.nil? ? "" : note.project.name,
        note.body, note.created_at.to_formatted_s(:db),
        note.updated_at.to_formatted_s(:db)]
    end
  end
  send_data(result, :filename => "notes.csv", :type => content_type)
end

#exportObject



86
87
88
# File 'app/controllers/data_controller.rb', line 86

def export
  # Show list of formats for export
end

#importObject



8
# File 'app/controllers/data_controller.rb', line 8

def import; end

#import_headers(file) ⇒ Object



80
81
82
83
84
# File 'app/controllers/data_controller.rb', line 80

def import_headers(file)
  CSV.foreach(file, headers: false) do |row|
    return row
  end
end

#indexObject



4
5
6
# File 'app/controllers/data_controller.rb', line 4

def index
  @page_title = t('data.export.page_title')
end

#sanitize_filename(filename) ⇒ Object (private)



215
216
217
# File 'app/controllers/data_controller.rb', line 215

def sanitize_filename(filename)
  filename.gsub(/[^0-9A-z.\-]/, '_')
end

#xml_exportObject



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
# File 'app/controllers/data_controller.rb', line 168

def xml_export
  todo_tag_ids = Tag.find_by_sql([
      "SELECT DISTINCT tags.id
       FROM tags, taggings, todos
       WHERE todos.user_id = ?
       AND tags.id = taggings.tag_id
       AND taggings.taggable_id = todos.id", current_user.id])
  rec_todo_tag_ids = Tag.find_by_sql([
      "SELECT DISTINCT tags.id
       FROM tags, taggings, recurring_todos
       WHERE recurring_todos.user_id = ?
       AND tags.id = taggings.tag_id
       AND taggings.taggable_id = recurring_todos.id", current_user.id])
  tags = Tag.where("id IN (?) OR id IN (?)", todo_tag_ids, rec_todo_tag_ids)
  taggings = Tagging.where("tag_id IN (?) OR tag_id IN(?)", todo_tag_ids, rec_todo_tag_ids)

  result = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><tracks_data>"
  result << current_user.todos.to_xml(:skip_instruct => true)
  result << current_user.contexts.to_xml(:skip_instruct => true)
  result << current_user.projects.to_xml(:skip_instruct => true)
  result << tags.to_xml(:skip_instruct => true)
  result << taggings.to_xml(:skip_instruct => true)
  result << current_user.notes.to_xml(:skip_instruct => true)
  result << current_user.recurring_todos.to_xml(:skip_instruct => true)
  result << "</tracks_data>"
  send_data(result, :filename => "tracks_data.xml", :type => 'text/xml')
end

#yaml_exportObject

Thanks to a tip by Gleb Arshinov <http://lists.rubyonrails.org/pipermail/rails/2004-November/000199.html>



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
# File 'app/controllers/data_controller.rb', line 92

def yaml_export
  all_tables = {}

  all_tables['todos'] = current_user.todos.includes(:tags).load
  all_tables['contexts'] = current_user.contexts.load
  all_tables['projects'] = current_user.projects.load

  todo_tag_ids = Tag.find_by_sql([
      "SELECT DISTINCT tags.id
       FROM tags, taggings, todos
       WHERE todos.user_id = ?
       AND tags.id = taggings.tag_id
       AND taggings.taggable_id = todos.id", current_user.id])
  rec_todo_tag_ids = Tag.find_by_sql([
      "SELECT DISTINCT tags.id
       FROM tags, taggings, recurring_todos
       WHERE recurring_todos.user_id = ?
       AND tags.id = taggings.tag_id
       AND taggings.taggable_id = recurring_todos.id", current_user.id])
  tags = Tag.where("id IN (?) OR id IN (?)", todo_tag_ids, rec_todo_tag_ids)
  taggings = Tagging.where("tag_id IN (?) OR tag_id IN(?)", todo_tag_ids, rec_todo_tag_ids)

  all_tables['tags'] = tags.load
  all_tables['taggings'] = taggings.load
  all_tables['notes'] = current_user.notes.load
  all_tables['recurring_todos'] = current_user.recurring_todos.load

  result = all_tables.to_yaml
  # TODO: general functionality for line endings
  result.gsub!(/\n/, "\r\n")
  send_data(result, :filename => "tracks_backup.yml", :type => 'text/plain')
end

#yaml_formObject



196
197
198
# File 'app/controllers/data_controller.rb', line 196

def yaml_form
  # Draw the form to input the YAML text data
end

#yaml_importObject



209
210
211
# File 'app/controllers/data_controller.rb', line 209

def yaml_import
  raise t "data.import.yaml_disabled"
end