Chris@909: # Redmine - project management software Chris@909: # Copyright (C) 2006-2011 Jean-Philippe Lang Chris@909: # Chris@909: # This program is free software; you can redistribute it and/or Chris@909: # modify it under the terms of the GNU General Public License Chris@909: # as published by the Free Software Foundation; either version 2 Chris@909: # of the License, or (at your option) any later version. Chris@909: # Chris@909: # This program is distributed in the hope that it will be useful, Chris@909: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@909: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@909: # GNU General Public License for more details. Chris@909: # Chris@909: # You should have received a copy of the GNU General Public License Chris@909: # along with this program; if not, write to the Free Software Chris@909: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@909: Chris@909: class GroupsController < ApplicationController Chris@909: layout 'admin' Chris@909: Chris@909: before_filter :require_admin Chris@909: Chris@909: helper :custom_fields Chris@909: Chris@909: # GET /groups Chris@909: # GET /groups.xml Chris@909: def index Chris@909: @groups = Group.find(:all, :order => 'lastname') Chris@909: Chris@909: respond_to do |format| Chris@909: format.html # index.html.erb Chris@909: format.xml { render :xml => @groups } Chris@909: end Chris@909: end Chris@909: Chris@909: # GET /groups/1 Chris@909: # GET /groups/1.xml Chris@909: def show Chris@909: @group = Group.find(params[:id]) Chris@909: Chris@909: respond_to do |format| Chris@909: format.html # show.html.erb Chris@909: format.xml { render :xml => @group } Chris@909: end Chris@909: end Chris@909: Chris@909: # GET /groups/new Chris@909: # GET /groups/new.xml Chris@909: def new Chris@909: @group = Group.new Chris@909: Chris@909: respond_to do |format| Chris@909: format.html # new.html.erb Chris@909: format.xml { render :xml => @group } Chris@909: end Chris@909: end Chris@909: Chris@909: # GET /groups/1/edit Chris@909: def edit Chris@909: @group = Group.find(params[:id], :include => :projects) Chris@909: end Chris@909: Chris@909: # POST /groups Chris@909: # POST /groups.xml Chris@909: def create Chris@909: @group = Group.new(params[:group]) Chris@909: Chris@909: respond_to do |format| Chris@909: if @group.save Chris@909: format.html { Chris@909: flash[:notice] = l(:notice_successful_create) Chris@909: redirect_to(params[:continue] ? new_group_path : groups_path) Chris@909: } Chris@909: format.xml { render :xml => @group, :status => :created, :location => @group } Chris@909: else Chris@909: format.html { render :action => "new" } Chris@909: format.xml { render :xml => @group.errors, :status => :unprocessable_entity } Chris@909: end Chris@909: end Chris@909: end Chris@909: Chris@909: # PUT /groups/1 Chris@909: # PUT /groups/1.xml Chris@909: def update Chris@909: @group = Group.find(params[:id]) Chris@909: Chris@909: respond_to do |format| Chris@909: if @group.update_attributes(params[:group]) Chris@909: flash[:notice] = l(:notice_successful_update) Chris@909: format.html { redirect_to(groups_path) } Chris@909: format.xml { head :ok } Chris@909: else Chris@909: format.html { render :action => "edit" } Chris@909: format.xml { render :xml => @group.errors, :status => :unprocessable_entity } Chris@909: end Chris@909: end Chris@909: end Chris@909: Chris@909: # DELETE /groups/1 Chris@909: # DELETE /groups/1.xml Chris@909: def destroy Chris@909: @group = Group.find(params[:id]) Chris@909: @group.destroy Chris@909: Chris@909: respond_to do |format| Chris@909: format.html { redirect_to(groups_url) } Chris@909: format.xml { head :ok } Chris@909: end Chris@909: end Chris@909: Chris@909: def add_users Chris@909: @group = Group.find(params[:id]) Chris@909: users = User.find_all_by_id(params[:user_ids]) Chris@909: @group.users << users if request.post? Chris@909: respond_to do |format| Chris@909: format.html { redirect_to :controller => 'groups', :action => 'edit', :id => @group, :tab => 'users' } Chris@909: format.js { Chris@909: render(:update) {|page| Chris@909: page.replace_html "tab-content-users", :partial => 'groups/users' Chris@909: users.each {|user| page.visual_effect(:highlight, "user-#{user.id}") } Chris@909: } Chris@909: } Chris@909: end Chris@909: end Chris@909: Chris@909: def remove_user Chris@909: @group = Group.find(params[:id]) Chris@909: @group.users.delete(User.find(params[:user_id])) if request.delete? Chris@909: respond_to do |format| Chris@909: format.html { redirect_to :controller => 'groups', :action => 'edit', :id => @group, :tab => 'users' } Chris@909: format.js { render(:update) {|page| page.replace_html "tab-content-users", :partial => 'groups/users'} } Chris@909: end Chris@909: end Chris@909: Chris@909: def autocomplete_for_user Chris@909: @group = Group.find(params[:id]) Chris@909: @users = User.active.not_in_group(@group).like(params[:q]).all(:limit => 100) Chris@909: render :layout => false Chris@909: end Chris@909: Chris@909: def edit_membership Chris@909: @group = Group.find(params[:id]) Chris@909: @membership = Member.edit_membership(params[:membership_id], params[:membership], @group) Chris@909: @membership.save if request.post? Chris@909: respond_to do |format| Chris@909: if @membership.valid? Chris@909: format.html { redirect_to :controller => 'groups', :action => 'edit', :id => @group, :tab => 'memberships' } Chris@909: format.js { Chris@909: render(:update) {|page| Chris@909: page.replace_html "tab-content-memberships", :partial => 'groups/memberships' Chris@909: page.visual_effect(:highlight, "member-#{@membership.id}") Chris@909: } Chris@909: } Chris@909: else Chris@909: format.js { Chris@909: render(:update) {|page| Chris@909: page.alert(l(:notice_failed_to_save_members, :errors => @membership.errors.full_messages.join(', '))) Chris@909: } Chris@909: } Chris@909: end Chris@909: end Chris@909: end Chris@909: Chris@909: def destroy_membership Chris@909: @group = Group.find(params[:id]) Chris@909: Member.find(params[:membership_id]).destroy if request.post? Chris@909: respond_to do |format| Chris@909: format.html { redirect_to :controller => 'groups', :action => 'edit', :id => @group, :tab => 'memberships' } Chris@909: format.js { render(:update) {|page| page.replace_html "tab-content-memberships", :partial => 'groups/memberships'} } Chris@909: end Chris@909: end Chris@909: end