2007-03-30 04:36:52 +00:00
|
|
|
class DataController < ApplicationController
|
|
|
|
|
|
|
|
require 'csv'
|
|
|
|
|
|
|
|
def index
|
2008-09-05 17:53:18 +02:00
|
|
|
@page_title = "TRACKS::Export"
|
2007-03-30 04:36:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def import
|
|
|
|
end
|
|
|
|
|
|
|
|
def export
|
|
|
|
# Show list of formats for export
|
|
|
|
end
|
|
|
|
|
|
|
|
# Thanks to a tip by Gleb Arshinov
|
|
|
|
# <http://lists.rubyonrails.org/pipermail/rails/2004-November/000199.html>
|
|
|
|
def yaml_export
|
|
|
|
all_tables = {}
|
|
|
|
|
2013-05-13 15:35:21 +02:00
|
|
|
all_tables['todos'] = current_user.todos.includes(:tags).load
|
|
|
|
all_tables['contexts'] = current_user.contexts.load
|
|
|
|
all_tables['projects'] = current_user.projects.load
|
2009-02-08 15:25:42 +01:00
|
|
|
|
2009-05-03 21:34:58 +02:00
|
|
|
todo_tag_ids = Tag.find_by_sql([
|
|
|
|
"SELECT DISTINCT tags.id "+
|
2009-02-08 15:25:42 +01:00
|
|
|
"FROM tags, taggings, todos "+
|
|
|
|
"WHERE todos.user_id=? "+
|
|
|
|
"AND tags.id = taggings.tag_id " +
|
|
|
|
"AND taggings.taggable_id = todos.id ", current_user.id])
|
2009-05-03 21:34:58 +02:00
|
|
|
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])
|
2012-04-18 14:22:58 +02:00
|
|
|
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)
|
2009-02-08 15:25:42 +01:00
|
|
|
|
2013-05-13 15:35:21 +02:00
|
|
|
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
|
2007-03-30 04:36:52 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
def csv_actions
|
|
|
|
content_type = 'text/csv'
|
|
|
|
CSV::Writer.generate(result = "") do |csv|
|
2007-11-06 05:04:00 +00:00
|
|
|
csv << ["id", "Context", "Project", "Description", "Notes", "Tags",
|
2008-09-05 17:53:18 +02:00
|
|
|
"Created at", "Due", "Completed at", "User ID", "Show from",
|
|
|
|
"state"]
|
2012-04-18 14:22:58 +02:00
|
|
|
current_user.todos.include(:context, :project).all.each do |todo|
|
2009-05-13 21:08:35 +02:00
|
|
|
csv << [todo.id, todo.context.name,
|
|
|
|
todo.project_id.nil? ? "" : todo.project.name,
|
|
|
|
todo.description,
|
2008-09-05 17:53:18 +02:00
|
|
|
todo.notes, todo.tags.collect{|t| t.name}.join(', '),
|
|
|
|
todo.created_at.to_formatted_s(:db),
|
2009-05-13 21:08:35 +02:00
|
|
|
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]
|
2007-03-30 04:36:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
send_data(result, :filename => "todos.csv", :type => content_type)
|
|
|
|
end
|
2009-05-13 21:08:35 +02:00
|
|
|
|
2007-03-30 04:36:52 +00:00
|
|
|
|
|
|
|
def csv_notes
|
|
|
|
content_type = 'text/csv'
|
2012-04-24 20:47:07 +02:00
|
|
|
CSV.generate(result = "") do |csv|
|
2007-10-16 07:16:22 +00:00
|
|
|
csv << ["id", "User ID", "Project", "Note",
|
2008-09-05 17:53:18 +02:00
|
|
|
"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
|
2012-04-24 20:47:07 +02:00
|
|
|
current_user.notes.reorder("notes.created_at").each do |note|
|
2008-09-05 17:53:18 +02:00
|
|
|
# Format dates in ISO format for easy sorting in spreadsheet Print
|
|
|
|
# context and project names for easy viewing
|
2012-04-18 14:22:58 +02:00
|
|
|
csv << [note.id, note.user_id,
|
2008-09-05 17:53:18 +02:00
|
|
|
note.project_id = note.project_id.nil? ? "" : note.project.name,
|
|
|
|
note.body, note.created_at.to_formatted_s(:db),
|
2012-04-18 14:22:58 +02:00
|
|
|
note.updated_at.to_formatted_s(:db)]
|
2007-03-30 04:36:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
send_data(result, :filename => "notes.csv", :type => content_type)
|
|
|
|
end
|
|
|
|
|
|
|
|
def xml_export
|
2009-05-03 21:34:58 +02:00
|
|
|
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])
|
2012-04-18 14:22:58 +02:00
|
|
|
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)
|
2009-05-03 21:34:58 +02:00
|
|
|
|
|
|
|
result = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><tracks_data>"
|
2012-04-18 14:22:58 +02:00
|
|
|
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)
|
2009-05-03 21:34:58 +02:00
|
|
|
result << tags.to_xml(:skip_instruct => true)
|
|
|
|
result << taggings.to_xml(:skip_instruct => true)
|
2012-04-18 14:22:58 +02:00
|
|
|
result << current_user.notes.to_xml(:skip_instruct => true)
|
|
|
|
result << current_user.recurring_todos.to_xml(:skip_instruct => true)
|
2009-05-03 21:34:58 +02:00
|
|
|
result << "</tracks_data>"
|
|
|
|
send_data(result, :filename => "tracks_data.xml", :type => 'text/xml')
|
2007-03-30 04:36:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def yaml_form
|
|
|
|
# Draw the form to input the YAML text data
|
|
|
|
end
|
|
|
|
|
2008-09-05 03:25:56 +02:00
|
|
|
# adjusts time to utc
|
|
|
|
def adjust_time(timestring)
|
|
|
|
if (timestring=='') or ( timestring == nil)
|
|
|
|
return nil
|
2012-04-18 14:22:58 +02:00
|
|
|
else
|
2008-09-05 03:25:56 +02:00
|
|
|
return Time.parse(timestring + 'UTC')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-03-30 04:36:52 +00:00
|
|
|
def yaml_import
|
2013-05-07 09:07:57 +02:00
|
|
|
raise "YAML loading is disabled"
|
2007-03-30 04:36:52 +00:00
|
|
|
end
|
2008-09-05 17:53:18 +02:00
|
|
|
|
|
|
|
end
|