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 = {}
|
|
|
|
|
2009-05-03 21:34:58 +02:00
|
|
|
all_tables['todos'] = current_user.todos.find(:all, :include => [:tags])
|
2007-07-30 05:29:18 +00:00
|
|
|
all_tables['contexts'] = current_user.contexts.find(:all)
|
|
|
|
all_tables['projects'] = current_user.projects.find(:all)
|
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])
|
|
|
|
tags = Tag.find(:all, :conditions => ["id IN (?) OR id IN (?)", todo_tag_ids, rec_todo_tag_ids])
|
|
|
|
taggings = Tagging.find(:all, :conditions => ["tag_id IN (?) OR tag_id IN(?)", todo_tag_ids, rec_todo_tag_ids])
|
2009-02-08 15:25:42 +01:00
|
|
|
|
2009-05-03 21:34:58 +02:00
|
|
|
all_tables['tags'] = tags
|
2009-02-08 15:25:42 +01:00
|
|
|
all_tables['taggings'] = taggings
|
2007-07-30 05:29:18 +00:00
|
|
|
all_tables['notes'] = current_user.notes.find(:all)
|
2008-09-05 17:41:30 +02:00
|
|
|
all_tables['recurring_todos'] = current_user.recurring_todos.find(:all)
|
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"]
|
2009-05-13 21:08:35 +02:00
|
|
|
current_user.todos.find(:all, :include => [:context, :project]).each do |todo|
|
|
|
|
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'
|
|
|
|
CSV::Writer.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
|
2007-07-30 05:29:18 +00:00
|
|
|
current_user.notes.find(:all,:order=>"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
|
2007-03-30 04:36:52 +00: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),
|
|
|
|
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])
|
|
|
|
tags = Tag.find(:all, :conditions => ["id IN (?) OR id IN (?)", todo_tag_ids, rec_todo_tag_ids])
|
|
|
|
taggings = Tagging.find(:all, :conditions => ["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.find(:all).to_xml(:skip_instruct => true)
|
2007-07-30 05:29:18 +00:00
|
|
|
result << current_user.contexts.find(:all).to_xml(:skip_instruct => true)
|
|
|
|
result << current_user.projects.find(:all).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)
|
2007-07-30 05:29:18 +00:00
|
|
|
result << current_user.notes.find(:all).to_xml(:skip_instruct => true)
|
2008-09-05 17:41:30 +02:00
|
|
|
result << current_user.recurring_todos.find(:all).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
|
|
|
|
else
|
|
|
|
return Time.parse(timestring + 'UTC')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-03-30 04:36:52 +00:00
|
|
|
def yaml_import
|
2008-09-05 03:25:56 +02:00
|
|
|
@errmessage = ''
|
|
|
|
@inarray = YAML::load(params['import']['yaml'])
|
|
|
|
# arrays to handle id translations
|
|
|
|
|
|
|
|
# contexts
|
|
|
|
translate_context = Hash.new
|
|
|
|
translate_context[nil] = nil
|
|
|
|
current_user.contexts.each { |context| context.destroy }
|
|
|
|
@inarray['contexts'].each { | item |
|
|
|
|
newitem = Context.new(item.ivars['attributes'])
|
|
|
|
newitem.user_id = current_user.id
|
|
|
|
newitem.created_at = adjust_time(item.ivars['attributes']['created_at'])
|
|
|
|
newitem.save(false)
|
|
|
|
translate_context[item.ivars['attributes']['id'].to_i] = newitem.id
|
|
|
|
}
|
|
|
|
|
|
|
|
# projects
|
|
|
|
translate_project = Hash.new
|
|
|
|
translate_project[nil] = nil
|
|
|
|
current_user.projects.each { |item| item.destroy }
|
|
|
|
@inarray['projects'].each { |item|
|
|
|
|
newitem = Project.new(item.ivars['attributes'])
|
|
|
|
# ids
|
|
|
|
newitem.user_id = current_user.id
|
|
|
|
newitem.default_context_id = translate_context[newitem.default_context_id]
|
|
|
|
newitem.save(false)
|
|
|
|
translate_project[item.ivars['attributes']['id'].to_i] = newitem.id
|
|
|
|
|
|
|
|
# state + dates
|
|
|
|
newitem.transition_to(item.ivars['attributes']['state'])
|
|
|
|
newitem.completed_at = adjust_time(item.ivars['attributes']['completed_at'])
|
|
|
|
newitem.created_at = adjust_time(item.ivars['attributes']['created_at'])
|
|
|
|
newitem.position = item.ivars['attributes']['position']
|
|
|
|
newitem.save(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
# todos
|
|
|
|
translate_todo = Hash.new
|
|
|
|
translate_todo[nil] = nil
|
|
|
|
current_user.todos.each { |item| item.destroy }
|
|
|
|
@inarray['todos'].each { |item|
|
|
|
|
newitem = Todo.new(item.ivars['attributes'])
|
|
|
|
# ids
|
|
|
|
newitem.user_id = current_user.id
|
|
|
|
newitem.context_id = translate_context[newitem.context_id]
|
|
|
|
newitem.project_id = translate_project[newitem.project_id]
|
|
|
|
# TODO: vyresit recurring_todo_id
|
|
|
|
newitem.save(false)
|
|
|
|
translate_todo[item.ivars['attributes']['id'].to_i] = newitem.id
|
|
|
|
|
|
|
|
# state + dates
|
|
|
|
case item.ivars['attributes']['state']
|
2008-09-05 17:53:18 +02:00
|
|
|
when 'active' then newitem.activate!
|
|
|
|
when 'project_hidden' then newitem.hide!
|
|
|
|
when 'completed'
|
|
|
|
newitem.complete!
|
|
|
|
newitem.completed_at = adjust_time(item.ivars['attributes']['completed_at'])
|
|
|
|
when 'deferred' then newitem.defer!
|
2008-09-05 03:25:56 +02:00
|
|
|
end
|
|
|
|
newitem.created_at = adjust_time(item.ivars['attributes']['created_at'])
|
|
|
|
newitem.save(false)
|
|
|
|
}
|
|
|
|
|
2008-09-05 17:53:18 +02:00
|
|
|
# tags
|
2008-09-05 03:25:56 +02:00
|
|
|
translate_tag = Hash.new
|
|
|
|
translate_tag[nil] = nil
|
|
|
|
current_user.tags.each { |item| item.destroy }
|
|
|
|
@inarray['tags'].each { |item|
|
|
|
|
newitem = Tag.new(item.ivars['attributes'])
|
|
|
|
newitem.created_at = adjust_time(item.ivars['attributes']['created_at'])
|
|
|
|
newitem.save
|
|
|
|
translate_tag[item.ivars['attributes']['id'].to_i] = newitem.id
|
|
|
|
}
|
|
|
|
|
|
|
|
# taggings
|
|
|
|
current_user.taggings.each { |item| item.destroy }
|
|
|
|
@inarray['taggings'].each { |item|
|
|
|
|
newitem = Tagging.new(item.ivars['attributes'])
|
|
|
|
newitem.user_id = current_user.id
|
|
|
|
newitem.tag_id = translate_tag[newitem.tag_id]
|
|
|
|
case newitem.taggable_type
|
2008-09-05 17:53:18 +02:00
|
|
|
when 'Todo' then newitem.taggable_id = translate_todo[newitem.taggable_id]
|
|
|
|
else newitem.taggable_id = 0
|
2008-09-05 03:25:56 +02:00
|
|
|
end
|
|
|
|
newitem.save
|
|
|
|
}
|
|
|
|
|
|
|
|
# notes
|
|
|
|
current_user.notes.each { |item| item.destroy }
|
|
|
|
@inarray['notes'].each { |item|
|
|
|
|
newitem = Note.new(item.ivars['attributes'])
|
|
|
|
newitem.id = item.ivars['attributes']['id']
|
|
|
|
newitem.user_id = current_user.id
|
|
|
|
newitem.project_id = translate_project[newitem.project_id]
|
|
|
|
newitem.created_at = adjust_time(item.ivars['attributes']['created_at'])
|
|
|
|
newitem.save
|
|
|
|
}
|
2007-03-30 04:36:52 +00:00
|
|
|
end
|
2008-09-05 17:53:18 +02:00
|
|
|
|
|
|
|
end
|