2007-03-30 04:36:52 +00:00
|
|
|
class ContextsController < ApplicationController
|
|
|
|
|
|
|
|
|
|
helper :todos
|
|
|
|
|
|
2007-04-11 06:04:17 +00:00
|
|
|
before_filter :init, :except => [:index, :create, :destroy, :order]
|
2007-03-30 04:36:52 +00:00
|
|
|
before_filter :init_todos, :only => :show
|
|
|
|
|
before_filter :set_context_from_params, :only => [:update, :destroy]
|
|
|
|
|
skip_before_filter :login_required, :only => [:index]
|
|
|
|
|
prepend_before_filter :login_or_feed_token_required, :only => [:index]
|
|
|
|
|
|
|
|
|
|
def index
|
2012-04-30 13:51:42 +02:00
|
|
|
@active_contexts = current_user.contexts.active
|
|
|
|
|
@hidden_contexts = current_user.contexts.hidden
|
2010-10-22 20:46:06 +02:00
|
|
|
@new_context = current_user.contexts.build
|
2012-05-01 09:39:53 +02:00
|
|
|
init_not_done_counts(['context'])
|
2011-02-11 16:18:59 +01:00
|
|
|
|
|
|
|
|
# save all contexts here as @new_context will add an empty one to current_user.contexts
|
|
|
|
|
@all_contexts = @active_contexts + @hidden_contexts
|
|
|
|
|
@count = @all_contexts.size
|
|
|
|
|
|
2007-03-30 04:36:52 +00:00
|
|
|
respond_to do |format|
|
|
|
|
|
format.html &render_contexts_html
|
2008-04-19 19:15:07 +00:00
|
|
|
format.m &render_contexts_mobile
|
2011-02-11 16:18:59 +01:00
|
|
|
format.xml { render :xml => @all_contexts.to_xml( :except => :user_id ) }
|
2012-04-19 15:31:18 +02:00
|
|
|
format.rss do
|
|
|
|
|
@feed_title = 'Tracks Contexts'
|
|
|
|
|
@feed_description = "Lists all the contexts for #{current_user.display_name}"
|
|
|
|
|
end
|
|
|
|
|
format.atom do
|
|
|
|
|
@feed_title = 'Tracks Contexts'
|
|
|
|
|
@feed_description = "Lists all the contexts for #{current_user.display_name}"
|
|
|
|
|
end
|
2010-11-09 19:57:53 +01:00
|
|
|
format.text do
|
|
|
|
|
render :action => 'index', :layout => false, :content_type => Mime::TEXT
|
|
|
|
|
end
|
2012-04-09 12:48:40 +02:00
|
|
|
format.autocomplete &render_autocomplete
|
2007-03-30 04:36:52 +00:00
|
|
|
end
|
|
|
|
|
end
|
2012-04-19 15:31:18 +02:00
|
|
|
|
2007-03-30 04:36:52 +00:00
|
|
|
def show
|
2008-12-17 17:15:04 +01:00
|
|
|
@contexts = current_user.contexts(true)
|
2011-06-17 13:33:54 +02:00
|
|
|
if @context.nil?
|
2007-05-21 06:12:55 +00:00
|
|
|
respond_to do |format|
|
|
|
|
|
format.html { render :text => 'Context not found', :status => 404 }
|
|
|
|
|
format.xml { render :xml => '<error>Context not found</error>', :status => 404 }
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
@page_title = "TRACKS::Context: #{@context.name}"
|
|
|
|
|
respond_to do |format|
|
|
|
|
|
format.html
|
2008-04-19 19:15:07 +00:00
|
|
|
format.m &render_context_mobile
|
2007-05-21 06:12:55 +00:00
|
|
|
format.xml { render :xml => @context.to_xml( :except => :user_id ) }
|
|
|
|
|
end
|
|
|
|
|
end
|
2007-03-30 04:36:52 +00:00
|
|
|
end
|
2012-04-27 14:22:16 +02:00
|
|
|
|
2007-03-30 04:36:52 +00:00
|
|
|
def create
|
|
|
|
|
if params[:format] == 'application/xml' && params['exception']
|
2012-04-19 15:31:18 +02:00
|
|
|
render_failure "Expected post format is valid xml like so: <context><name>context name</name></context>.", 400
|
2007-03-30 04:36:52 +00:00
|
|
|
return
|
|
|
|
|
end
|
2012-04-19 15:31:18 +02:00
|
|
|
@context = current_user.contexts.build(params['context'])
|
2007-03-30 04:36:52 +00:00
|
|
|
@saved = @context.save
|
|
|
|
|
@context_not_done_counts = { @context.id => 0 }
|
2007-04-02 04:18:19 +00:00
|
|
|
respond_to do |format|
|
2007-11-16 14:28:52 +00:00
|
|
|
format.js do
|
|
|
|
|
@down_count = current_user.contexts.size
|
|
|
|
|
end
|
2007-04-02 04:18:19 +00:00
|
|
|
format.xml do
|
2012-04-19 15:31:18 +02:00
|
|
|
if @context.new_record?
|
2012-04-27 14:22:16 +02:00
|
|
|
render_failure @context.errors.to_xml.html_safe, 409
|
2007-03-30 04:36:52 +00:00
|
|
|
else
|
2007-12-04 06:24:23 +00:00
|
|
|
head :created, :location => context_url(@context)
|
2007-03-30 04:36:52 +00:00
|
|
|
end
|
2008-01-18 21:02:54 +00:00
|
|
|
end
|
2007-03-30 04:36:52 +00:00
|
|
|
end
|
|
|
|
|
end
|
2011-06-26 23:05:33 +02:00
|
|
|
|
2007-03-30 04:36:52 +00:00
|
|
|
# Edit the details of the context
|
2009-04-07 21:18:23 +02:00
|
|
|
#
|
2007-03-30 04:36:52 +00:00
|
|
|
def update
|
|
|
|
|
params['context'] ||= {}
|
|
|
|
|
success_text = if params['field'] == 'name' && params['value']
|
2011-06-26 23:05:33 +02:00
|
|
|
params['context']['id'] = params['id']
|
|
|
|
|
params['context']['name'] = params['value']
|
2007-03-30 04:36:52 +00:00
|
|
|
end
|
2009-04-07 21:18:23 +02:00
|
|
|
|
|
|
|
|
@original_context_hidden = @context.hidden?
|
2007-03-30 04:36:52 +00:00
|
|
|
@context.attributes = params["context"]
|
2009-04-07 21:18:23 +02:00
|
|
|
|
2010-10-24 22:31:57 +02:00
|
|
|
@saved = @context.save
|
|
|
|
|
|
|
|
|
|
if @saved
|
2008-08-18 16:02:13 +02:00
|
|
|
if boolean_param('wants_render')
|
2010-10-24 22:31:57 +02:00
|
|
|
@state_changed = (@original_context_hidden != @context.hidden?)
|
|
|
|
|
@new_state = (@context.hidden? ? "hidden" : "active") if @state_changed
|
2008-04-28 05:53:24 +00:00
|
|
|
respond_to do |format|
|
|
|
|
|
format.js
|
|
|
|
|
end
|
2010-10-24 22:31:57 +02:00
|
|
|
|
|
|
|
|
# TODO is this param ever used? is this dead code?
|
2011-06-26 23:05:33 +02:00
|
|
|
|
2008-08-18 16:02:13 +02:00
|
|
|
elsif boolean_param('update_context_name')
|
|
|
|
|
@contexts = current_user.projects
|
|
|
|
|
render :template => 'contexts/update_context_name.js.rjs'
|
|
|
|
|
return
|
2007-03-30 04:36:52 +00:00
|
|
|
else
|
|
|
|
|
render :text => success_text || 'Success'
|
|
|
|
|
end
|
|
|
|
|
else
|
2010-10-24 22:31:57 +02:00
|
|
|
respond_to do |format|
|
2011-06-26 23:05:33 +02:00
|
|
|
format.js
|
2011-06-10 23:29:42 +02:00
|
|
|
format.xml {
|
|
|
|
|
if @saved
|
2011-06-26 23:05:33 +02:00
|
|
|
render :xml => @context.to_xml( :except => :user_id )
|
2011-06-10 23:29:42 +02:00
|
|
|
else
|
|
|
|
|
render :text => "Error on update: #{@context.errors.full_messages.inject("") {|v, e| v + e + " " }}", :status => 409
|
|
|
|
|
end
|
|
|
|
|
}
|
2010-10-24 22:31:57 +02:00
|
|
|
end
|
2007-03-30 04:36:52 +00:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2009-10-02 19:45:49 -04:00
|
|
|
def edit
|
|
|
|
|
@context = Context.find(params[:id])
|
|
|
|
|
respond_to do |format|
|
|
|
|
|
format.js
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2008-01-18 21:02:54 +00:00
|
|
|
# Fairly self-explanatory; deletes the context If the context contains
|
|
|
|
|
# actions, you'll get a warning dialogue. If you choose to go ahead, any
|
|
|
|
|
# actions in the context will also be deleted.
|
2007-03-30 04:36:52 +00:00
|
|
|
def destroy
|
2010-08-19 11:25:04 +02:00
|
|
|
# make sure the deleted recurring patterns are removed from associated todos
|
|
|
|
|
@context.recurring_todos.each { |rt| rt.clear_todos_association } unless @context.recurring_todos.nil?
|
2011-06-26 23:05:33 +02:00
|
|
|
|
2007-03-30 04:36:52 +00:00
|
|
|
@context.destroy
|
|
|
|
|
respond_to do |format|
|
2010-10-23 17:52:50 +02:00
|
|
|
format.js do
|
|
|
|
|
@down_count = current_user.contexts.size
|
|
|
|
|
update_state_counts
|
|
|
|
|
end
|
2007-03-30 04:36:52 +00:00
|
|
|
format.xml { render :text => "Deleted context #{@context.name}" }
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Methods for changing the sort order of the contexts in the list
|
2009-04-07 21:18:23 +02:00
|
|
|
#
|
2007-03-30 04:36:52 +00:00
|
|
|
def order
|
2009-10-02 19:45:49 -04:00
|
|
|
context_ids = params["container_context"]
|
|
|
|
|
@projects = current_user.contexts.update_positions( context_ids )
|
2007-03-30 04:36:52 +00:00
|
|
|
render :nothing => true
|
2009-10-02 19:45:49 -04:00
|
|
|
rescue
|
|
|
|
|
notify :error, $!
|
|
|
|
|
redirect_to :action => 'index'
|
2007-03-30 04:36:52 +00:00
|
|
|
end
|
2011-06-26 23:05:33 +02:00
|
|
|
|
2011-06-17 14:30:32 +02:00
|
|
|
def done_todos
|
|
|
|
|
@source_view = 'context'
|
2011-06-26 23:05:33 +02:00
|
|
|
@context = current_user.contexts.find(params[:id])
|
|
|
|
|
@page_title = t('contexts.completed_tasks_title', :context_name => @context.name)
|
|
|
|
|
|
|
|
|
|
completed_todos = @context.todos.completed
|
2011-06-17 14:30:32 +02:00
|
|
|
|
|
|
|
|
@done_today = get_done_today(completed_todos)
|
|
|
|
|
@done_this_week = get_done_this_week(completed_todos)
|
|
|
|
|
@done_this_month = get_done_this_month(completed_todos)
|
|
|
|
|
@count = @done_today.size + @done_this_week.size + @done_this_month.size
|
2011-06-26 23:05:33 +02:00
|
|
|
|
2011-06-17 14:30:32 +02:00
|
|
|
render :template => 'todos/done'
|
|
|
|
|
end
|
2011-06-26 23:05:33 +02:00
|
|
|
|
2011-06-17 14:30:32 +02:00
|
|
|
def all_done_todos
|
|
|
|
|
@source_view = 'context'
|
2011-06-26 23:05:33 +02:00
|
|
|
@context = current_user.contexts.find(params[:id])
|
|
|
|
|
@page_title = t('contexts.all_completed_tasks_title', :context_name => @context.name)
|
2011-06-17 14:30:32 +02:00
|
|
|
|
2011-06-26 23:05:33 +02:00
|
|
|
@done = @context.todos.completed.paginate :page => params[:page], :per_page => 20, :order => 'completed_at DESC', :include => Todo::DEFAULT_INCLUDES
|
2011-06-17 14:30:32 +02:00
|
|
|
@count = @done.size
|
|
|
|
|
render :template => 'todos/all_done'
|
|
|
|
|
end
|
|
|
|
|
|
2007-03-30 04:36:52 +00:00
|
|
|
protected
|
|
|
|
|
|
2010-10-23 17:52:50 +02:00
|
|
|
def update_state_counts
|
|
|
|
|
@active_contexts_count = current_user.contexts.active.count
|
|
|
|
|
@hidden_contexts_count = current_user.contexts.hidden.count
|
|
|
|
|
@show_active_contexts = @active_contexts_count > 0
|
|
|
|
|
@show_hidden_contexts = @hidden_contexts_count > 0
|
|
|
|
|
end
|
|
|
|
|
|
2008-01-18 21:02:54 +00:00
|
|
|
def render_contexts_html
|
|
|
|
|
lambda do
|
|
|
|
|
@page_title = "TRACKS::List Contexts"
|
2009-04-07 21:18:23 +02:00
|
|
|
@no_active_contexts = @active_contexts.empty?
|
|
|
|
|
@no_hidden_contexts = @hidden_contexts.empty?
|
|
|
|
|
@active_count = @active_contexts.size
|
|
|
|
|
@hidden_count = @hidden_contexts.size
|
2008-01-18 21:02:54 +00:00
|
|
|
render
|
2007-03-30 04:36:52 +00:00
|
|
|
end
|
2008-01-18 21:02:54 +00:00
|
|
|
end
|
2011-06-26 23:05:33 +02:00
|
|
|
|
2008-04-19 19:15:07 +00:00
|
|
|
def render_contexts_mobile
|
|
|
|
|
lambda do
|
2008-04-27 19:23:19 +00:00
|
|
|
@page_title = "TRACKS::List Contexts"
|
2009-04-14 21:49:43 +02:00
|
|
|
@active_contexts = current_user.contexts.active
|
|
|
|
|
@hidden_contexts = current_user.contexts.hidden
|
2011-06-26 23:05:33 +02:00
|
|
|
@down_count = @active_contexts.size + @hidden_contexts.size
|
2012-05-12 13:37:36 +02:00
|
|
|
cookies[:mobile_url]= {:value => request.fullpath, :secure => SITE_CONFIG['secure_cookies']}
|
|
|
|
|
render
|
2008-04-19 19:15:07 +00:00
|
|
|
end
|
|
|
|
|
end
|
2011-06-26 23:05:33 +02:00
|
|
|
|
2008-04-19 19:15:07 +00:00
|
|
|
def render_context_mobile
|
|
|
|
|
lambda do
|
2008-04-27 19:23:19 +00:00
|
|
|
@page_title = "TRACKS::List actions in "+@context.name
|
2011-06-26 23:05:33 +02:00
|
|
|
@not_done = @not_done_todos.select {|t| t.context_id == @context.id }
|
|
|
|
|
@down_count = @not_done.size
|
2012-05-12 13:37:36 +02:00
|
|
|
cookies[:mobile_url]= {:value => request.fullpath, :secure => SITE_CONFIG['secure_cookies']}
|
2008-08-04 16:13:51 +02:00
|
|
|
@mobile_from_context = @context.id
|
2012-05-12 13:37:36 +02:00
|
|
|
render
|
2008-04-19 19:15:07 +00:00
|
|
|
end
|
|
|
|
|
end
|
2012-04-09 12:48:40 +02:00
|
|
|
|
|
|
|
|
def render_autocomplete
|
|
|
|
|
lambda do
|
|
|
|
|
# first get active contexts with todos then those without
|
|
|
|
|
filled_contexts = @active_contexts.reject { |ctx| ctx.todos.count == 0 } + @hidden_contexts.reject { |ctx| ctx.todos.count == 0 }
|
|
|
|
|
empty_contexts = @active_contexts.find_all { |ctx| ctx.todos.count == 0 } + @hidden_contexts.find_all { |ctx| ctx.todos.count == 0 }
|
|
|
|
|
render :text => for_autocomplete(filled_contexts + empty_contexts, params[:term])
|
|
|
|
|
end
|
|
|
|
|
end
|
2011-06-26 23:05:33 +02:00
|
|
|
|
2008-01-18 21:02:54 +00:00
|
|
|
def feed_options
|
|
|
|
|
Context.feed_options(current_user)
|
|
|
|
|
end
|
2007-03-30 04:36:52 +00:00
|
|
|
|
2008-01-18 21:02:54 +00:00
|
|
|
def set_context_from_params
|
2012-04-12 17:30:05 +02:00
|
|
|
@context = current_user.contexts.find(params[:id])
|
2008-01-18 21:02:54 +00:00
|
|
|
rescue
|
|
|
|
|
@context = nil
|
|
|
|
|
end
|
2011-06-26 23:05:33 +02:00
|
|
|
|
2008-01-18 21:02:54 +00:00
|
|
|
def init
|
|
|
|
|
@source_view = params['_source_view'] || 'context'
|
|
|
|
|
init_data_for_sidebar
|
|
|
|
|
end
|
2007-03-30 04:36:52 +00:00
|
|
|
|
2008-01-18 21:02:54 +00:00
|
|
|
def init_todos
|
|
|
|
|
set_context_from_params
|
|
|
|
|
unless @context.nil?
|
|
|
|
|
@max_completed = current_user.prefs.show_number_completed
|
2012-04-12 17:30:05 +02:00
|
|
|
@done = @context.todos.completed.all(:limit => @max_completed)
|
2011-06-26 23:05:33 +02:00
|
|
|
|
2008-01-18 21:02:54 +00:00
|
|
|
# @not_done_todos = @context.not_done_todos TODO: Temporarily doing this
|
|
|
|
|
# search manually until I can work out a way to do the same thing using
|
|
|
|
|
# not_done_todos acts_as_todo_container method Hides actions in hidden
|
|
|
|
|
# projects from context.
|
2012-03-23 14:04:55 +01:00
|
|
|
@not_done_todos = @context.todos.active(
|
2011-06-26 23:05:33 +02:00
|
|
|
:order => "todos.due IS NULL, todos.due ASC, todos.created_at ASC",
|
2011-06-17 14:58:32 +02:00
|
|
|
:include => Todo::DEFAULT_INCLUDES)
|
2008-12-03 14:03:59 +01:00
|
|
|
|
2012-03-23 14:04:55 +01:00
|
|
|
@deferred = @context.todos.deferred(:include => Todo::DEFAULT_INCLUDES)
|
|
|
|
|
@pending = @context.todos.pending(:include => Todo::DEFAULT_INCLUDES)
|
|
|
|
|
|
2008-12-03 14:03:59 +01:00
|
|
|
@projects = current_user.projects
|
|
|
|
|
|
2012-03-23 20:39:04 +01:00
|
|
|
@count = @not_done_todos.count + @deferred.count + @pending.count
|
2007-03-30 04:36:52 +00:00
|
|
|
end
|
2010-12-17 16:01:22 +01:00
|
|
|
|
2008-01-18 21:02:54 +00:00
|
|
|
end
|
2007-03-30 04:36:52 +00:00
|
|
|
|
|
|
|
|
end
|