Class: DataController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

#admin_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_not_done_counts, #init_project_hidden_todo_counts, #mobile?, #notify, openid_enabled?, #openid_enabled?, #parse_date_per_user_prefs, prefered_auth?, #prefered_auth?, #redirect_back_or_home, #render_failure, #sanitize, #set_charset, #set_group_view_by, #set_locale, #set_session_expiration, #set_time_zone, #set_zindex_counter, #todo_xml_params

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



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

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

#csv_actionsObject

export all actions as csv



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

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



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

def csv_import
  begin
    filename = Tracks::Utils.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] = "#{count} Projects imported"
    when 'todos'
      count = Todo.import path_and_file, params, current_user
      flash[:notice] = "#{count} Todos imported"
    else
      flash[:error] = t('data.invalid_import_destination')
    end
  rescue Exception => e
    flash[:error] = t('data.invalid_import_destination') + ": #{e}"
  end
  File.delete(path_and_file)
  redirect_to import_data_path
end

#csv_mapObject



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

def csv_map
  if params[:file].blank?
    flash[:notice] = "File can't be blank"
    redirect_to :back
  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] = "Invalid CVS: could not read headers: #{e}"
      redirect_to :back
      return
    end

    #save file for later
    begin
      uploaded_file = params[:file]
      @filename = Tracks::Utils.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] = "Could not save uploaded CSV (#{path_and_file}). Can Tracks write to the upload directory? #{e}"
      redirect_to :back
      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] = "Invalid import destination"
      redirect_to :back
    end
    respond_to do |format|
      format.html
    end
  end
end

#csv_notesObject

export all notes as csv



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

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



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

def export
  # Show list of formats for export
end

#importObject



9
10
11
# File 'app/controllers/data_controller.rb', line 9

def import

end

#import_headers(file) ⇒ Object



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

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

#indexObject



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

def index
  @page_title = "TRACKS::Export"
end

#xml_exportObject



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

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>



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

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
  result.gsub!(/\n/, "\r\n")   # TODO: general functionality for line endings
  send_data(result, :filename => "tracks_backup.yml", :type => 'text/plain')
end

#yaml_formObject



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

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

#yaml_importObject



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

def yaml_import
  raise "YAML loading is disabled"
end