class SiteAdminController < ApplicationController

  permit 'site_admin'


  def index
    store_location
    @webpage_title = 'Site Administration'
    @flags_list = Flag.find(:all)
  end
  
  def make_viewable
    return unless request.post?
    @r = Resource.find(params[:resource][:id])
    @r.update_attributes(:viewable => true)
    redirect_back_or_default(:controller => '/site_admin', :action => 'index')
  end 

  def edit_entry
    return unless request.post?
    redirect_to(:controller => '/resource', :action => 'edit', :id => params[:resource][:id])
  end 

# DRY... FIX
  def delete_entry
    if params[:commit] == "Yes, Really Delete Entry"
      Resource.delete(params[:id])
      flash[:notice] = "Entry Deleted"
      redirect_back_or_default(:controller => '/site_admin', :action => 'index')
      return
    else
      @e = Resource.find(params[:resource][:id])
    end    
  end

  def show_users
    @u = User.find(:all)    
  end

  def show_roles
    @u = User.find(:all)        
  end

  def delete_flag
    Flag.delete(params[:id])
    flash[:notice] = "Flag Deleted"
    redirect_back_or_default(:controller => '/site_admin', :action => 'index')
  end

  def promote_user
    @u = User.find(params[:id])     
    @u.has_role 'site_admin'
    flash[:notice] = "#{@u.fullname} promoted to Site Admin Authority"
    redirect_to(:controller => '/site_admin', :action => 'index')
  rescue
    flash[:notice] = 'Promote User Failed'
    redirect_to :action => 'index'
  end

  def demote_user
    @u = User.find(params[:id])     
      if @u.id == current_user.id
        flash[:notice] = "You cannot demote yourself"
        redirect_to(:controller => '/site_admin', :action => 'index')
        return
      end      
    @u.has_no_role 'site_admin'
    flash[:notice] = "#{@u.fullname} is not Site Admin"
    redirect_back_or_default(:controller => '/site_admin', :action => 'index')
  rescue
    flash[:notice] = 'Demote User Failed'
    redirect_to :action => 'index'
  end



  def forward_flag
      flash[:notice] = "Forward Flag to Owner"
      redirect_back_or_default(:controller => '/site_admin', :action => 'index')
  end

  def show_user
    @u = User.find(params[:id])      
  end

  
  def delete_user
    if params[:commit] == "Yes, Really Delete User"
      User.delete(params[:id])
      flash[:notice] = "User Deleted"
      redirect_back_or_default(:controller => '/site_admin', :action => 'index')
      return
    else
      @u = User.find(params[:id])      
      if @u.id == current_user.id
        flash[:notice] = "You cannot delete yourself"
        redirect_back_or_default(:controller => '/site_admin', :action => 'index')
      end      
    end    
  end

end
