tracks/tracks/app/controllers/context_controller.rb
bsag d0a542f625 Main changes are to login and session management:
* Added Luke Melia's patch to warn the user when the session has timed out when the user has added or checked off a next action without refreshing the page first. If they check off an item, they are redirected to the login page, then when they return, they are informed that the action has been checked off. If they add an item, they are informed after returning from the login page that the next action hasn't been added. Fixes #163.
  * Made some stylistic changes to login and signup pages to make them tidier, and to fit with the main theme better
  * Fixed bug with deleting items: the sheet which appeared was an alert (with only an 'OK' box, rather than a confirmation (with both an 'OK' and 'Cancel' box). Fixes #189.
  * Added a new feed icon to comply with the new de-facto standard: from [http://www.feedicons.com/ Feed Icons].



git-svn-id: http://www.rousette.org.uk/svn/tracks-repos/trunk@172 a4c988fc-2ded-0310-b66e-134b36920a42
2006-01-08 13:21:24 +00:00

207 lines
5.8 KiB
Ruby

class ContextController < ApplicationController
helper :todo
before_filter :login_required
layout "standard"
def index
list
render_action "list"
end
# Main method for listing contexts
# Set page title, and collect existing contexts in @contexts
#
def list
self.init
@page_title = "TRACKS::List Contexts"
end
# Filter the projects to show just the one passed in the URL
# e.g. <home>/project/show/<project_name> shows just <project_name>.
#
def show
init
init_todos
@on_page = "context"
@page_title = "TRACKS::Context: #{@context.name}"
end
# Creates a new context via Ajax helpers
#
def new_context
context = @session['user'].contexts.build
context.attributes = @params['context']
context.name = deurlize(context.name)
if context.save
render :partial => 'context_listing', :locals => { :context_listing => context }
else
flash["warning"] = "Couldn't add new context"
render :text => "#{flash["warning"]}"
end
end
# Called by a form button
# Parameters from form fields are passed to create new action
# in the selected context.
def add_item
self.init
@item = @user.todos.build
@item.attributes = @params["todo"]
if @item.due?
@item.due = Date.strptime(@params["todo"]["due"], DATE_FORMAT)
else
@item.due = ""
end
@saved = @item.save
@on_page = "context"
@up_count = Todo.find(:all, :conditions => ["todos.user_id = ? and todos.done = 0 and todos.context_id IN (?)", @user.id, @item.context_id]).size.to_s
return if request.xhr?
# fallback for standard requests
if @saved
flash["notice"] = 'Added new next action.'
redirect_to :controller => 'todo', :action => 'list'
else
flash["warning"] = 'The next action was not added. Please try again.'
redirect_to :controller => 'todo', :action => 'list'
end
rescue
if request.xhr? # be sure to include an error.rjs
render :action => 'error'
else
flash["warning"] = 'An error occurred on the server.'
redirect_to :controller => 'todo', :action => 'list'
end
end
# Delete a next action
#
def destroy_action
self.init
@item = check_user_return_item
@saved = @item.destroy
@down_count = Todo.find(:all, :conditions => ["todos.user_id = ? and todos.done = 0 and todos.context_id IN (?)", @user.id, @item.context_id]).size.to_s
return if request.xhr?
# fallback for standard requests
if @saved
flash["notice"] = 'Successfully deleted next action'
redirect_to :controller => 'todo', :action => 'list'
else
render :controller => 'todo', :action => 'list'
end
rescue
if request.xhr? # be sure to include an error.rjs
render :action => 'error'
else
flash["warning"] = 'An error occurred on the server.'
render :controller => 'todo', :action => 'list'
end
end
# Edit the details of the context
#
def update
check_user_set_context
@context.attributes = @params["context"]
@context.name = deurlize(@context.name)
if @context.save
render_partial 'context_listing', @context
else
flash["warning"] = "Couldn't update new context"
render :text => ""
end
end
# 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.
def destroy
check_user_set_context
if @context.destroy
render_text ""
else
flash["warning"] = "Couldn't delete context \"#{@context.name}\""
redirect_to( :controller => "context", :action => "list" )
end
end
# Methods for changing the sort order of the contexts in the list
#
def order
@params["list-contexts"].each_with_index do |id, position|
if check_user_matches_context_user(id)
Context.update(id, :position => position + 1)
end
end
render :nothing => true
end
protected
def check_user_set_context
@user = @session['user']
if @params["name"]
@context = Context.find_by_name_and_user_id(deurlize(@params["name"]), @user.id)
elsif @params['id']
@context = Context.find_by_id_and_user_id(@params["id"], @user.id)
else
redirect_to(:controller => "context", :action => "list" )
end
if @user == @context.user
return @context
else
@context = nil # Should be nil anyway.
flash["warning"] = "Item and session user mis-match: #{@context.user_id} and #{@session['user'].id}!"
render_text ""
end
end
def check_user_matches_context_user(id)
@user = @session['user']
@context = Context.find_by_id_and_user_id(id, @user.id)
if @user == @context.user
return @context
else
@context = nil
flash["warning"] = "Project and session user mis-match: #{@context.user_id} and #{@session['user'].id}!"
render_text ""
end
end
def check_user_return_item
item = Todo.find( @params['id'] )
if @session['user'] == item.user
return item
else
flash["warning"] = "Item and session user mis-match: #{item.user.name} and #{@session['user'].name}!"
render_text ""
end
end
def init
@user = @session['user']
@projects = @user.projects.collect { |x| x.done? ? nil:x }.compact
@contexts = @user.contexts
@todos = @user.todos
@done = Todo.find(:all, :conditions => ["todos.user_id = ? and todos.done = 1", @user.id], :include => [:project], :order => "completed DESC")
end
def init_todos
check_user_set_context
@done = @context.find_done_todos
@not_done = @context.find_not_done_todos
@count = @not_done.size
end
end