<commit_before>class MapsController < ApplicationController

  layout 'mapdetail', :only => [:show, :edit, :preview, :warp, :clip, :align, :activity, :warped, :export, :metadata, :comments]
  
  before_filter :store_location, :only => [:warp, :align, :clip, :export, :edit, :comments ]
  
  before_filter :authenticate_user!, :only => [:new, :create, :edit, :update, :destroy, :delete, :warp, :rectify, :clip, :align, :warp_align, :mask_map, :delete_mask, :save_mask, :save_mask_and_warp, :set_rough_state, :set_rough_centroid, :publish, :trace, :id, :map_type, :create_inset]
 
  before_filter :check_administrator_role, :only => [:publish, :map_type, :create_inset]
 
  before_filter :find_map_if_available,
    :except => [:show, :index, :wms, :tile, :mapserver_wms, :warp_aligned, :status, :new, :create, :update, :edit, :tag, :geosearch, :map_type, :create_inset]

  before_filter :check_link_back, :only => [:show, :warp, :clip, :align, :warped, :export, :activity]

  skip_before_filter :check_site_read_only, :only => [:show, :index, :geosearch, :wms, :tile, :status, :warped, :comments, :export, :metadata, :activity, :preview, :thumb, :inset_maps]
  
  rescue_from ActiveRecord::RecordNotFound, :with => :bad_record

  helper :sort
  include SortHelper
  
  
 
  
  ###############
  #
  # Collection actions 
  #
  ###############
  def index
    sort_init('updated_at', {:default_order => "desc"})
    
    sort_update
    @show_warped = params[:show_warped]
    request.query_string.length > 0 ?  qstring = "?" + request.query_string : qstring = ""
    
    set_session_link_back url_for(:controller=> 'maps', :action => 'index',:skip_relative_url_root => false, :only_path => false )+ qstring
    
    @query = params[:query]
    
    @field = %w(text title description status catnyp nypl_digital_id uuid).detect{|f| f == (params[:field])}
    
    
    @field = "text" if @field.nil?
    where_col = @field
    
    if  @field == "text"
      where_col  = "(title || ' ' || description)"
    end
    
    #we'll use POSIX regular expression for searches    ~*'( |^)robinson([^A-z]|$)' and to strip out brakets etc  ~*'(:punct:|^|)plate 6([^A-z]|$)';
    if @query && @query.strip.length > 0 && @field
      conditions = ["#{where_col}  ~* ?", '(:punct:|^|)'+@query+'([^A-z]|$)']
    else
      conditions = nil
    end
    
    @year_min = Map.minimum(:issue_year).to_i - 1
    @year_max = Map.maximum(:issue_year).to_i + 1
    @year_min = 1600 if @year_min == 0
    @year_max = 2015 if @year_max == 0
    
    year_conditions = nil
    if params[:from] && params[:to] && !(@year_min == params[:from].to_i && @year_max == params[:to].to_i)
      year_conditions = {:issue_year => params[:from].to_i..params[:to].to_i}
    end
    
    @from = params[:from]
    @to = params[:to]
        
    if params[:sort_order] && params[:sort_order] == "desc"
      sort_nulls = " NULLS LAST"
    else
      sort_nulls = " NULLS FIRST"
    end
    @per_page = params[:per_page] || 50
    paginate_params = {
      :page => params[:page],
      :per_page => @per_page
    }
    order_options = sort_clause + sort_nulls
    where_options = conditions
    #order('name').where('name LIKE ?', "%#{search}%").paginate(page: page, per_page: 10)
    
    if @show_warped == "1"
      @maps = Map.warped.where(where_options).where(year_conditions).order(order_options).paginate(paginate_params)
    else
      @maps = Map.where(where_options).where(year_conditions).order(order_options).paginate(paginate_params)
    end
    
    @html_title = "Browse Maps"
    if request.xhr?
      render :action => 'index.rjs'
    else
      respond_to do |format|
        format.html{ render :layout =>'application' }  # index.html.erb
        format.xml  { render :xml => @maps.to_xml(:root => "maps", :except => [:content_type, :size, :bbox_geom, :uuid, :parent_uuid, :filename, :parent_id,  :map, :thumbnail, :rough_centroid]) {|xml|
            xml.tag!'stat', "ok"
            xml.tag!'total-entries', @maps.total_entries
            xml.tag!'per-page', @maps.per_page
            xml.tag!'current-page',@maps.current_page} }
        
        format.json { render :json => {:stat => "ok",
            :current_page => @maps.current_page,
            :per_page => @maps.per_page,
            :total_entries => @maps.total_entries,
            :total_pages => @maps.total_pages,
            :items => @maps.to_a}.to_json(:except => [:content_type, :size, :bbox_geom, :uuid, :parent_uuid, :filename, :parent_id,  :map, :thumbnail, :rough_centroid], :methods => :depicts_year) , :callback => params[:callback]
        }
      end
    end
    
  end
  
  
    
  def geosearch
    require 'geoplanet'
    sort_init 'updated_at'
    sort_update

    extents = [-74.1710,40.5883,-73.4809,40.8485] #NYC

    #TODO change to straight javascript call.
    if params[:place] && !params[:place].blank?
      place_query = params[:place]
      GeoPlanet.appid = APP_CONFIG['yahoo_app_id']
      
      geoplanet_result = GeoPlanet::Place.search(place_query, :count => 2)
      
      if geoplanet_result[0]<commit_msg>exposing bbox_geom and uuid in json/xml endopints<commit_after>class MapsController < ApplicationController

  layout 'mapdetail', :only => [:show, :edit, :preview, :warp, :clip, :align, :activity, :warped, :export, :metadata, :comments]
  
  before_filter :store_location, :only => [:warp, :align, :clip, :export, :edit, :comments ]
  
  before_filter :authenticate_user!, :only => [:new, :create, :edit, :update, :destroy, :delete, :warp, :rectify, :clip, :align, :warp_align, :mask_map, :delete_mask, :save_mask, :save_mask_and_warp, :set_rough_state, :set_rough_centroid, :publish, :trace, :id, :map_type, :create_inset]
 
  before_filter :check_administrator_role, :only => [:publish, :map_type, :create_inset]
 
  before_filter :find_map_if_available,
    :except => [:show, :index, :wms, :tile, :mapserver_wms, :warp_aligned, :status, :new, :create, :update, :edit, :tag, :geosearch, :map_type, :create_inset]

  before_filter :check_link_back, :only => [:show, :warp, :clip, :align, :warped, :export, :activity]

  skip_before_filter :check_site_read_only, :only => [:show, :index, :geosearch, :wms, :tile, :status, :warped, :comments, :export, :metadata, :activity, :preview, :thumb, :inset_maps]
  
  rescue_from ActiveRecord::RecordNotFound, :with => :bad_record

  helper :sort
  include SortHelper
  
  
 
  
  ###############
  #
  # Collection actions 
  #
  ###############
  def index
    sort_init('updated_at', {:default_order => "desc"})
    
    sort_update
    @show_warped = params[:show_warped]
    request.query_string.length > 0 ?  qstring = "?" + request.query_string : qstring = ""
    
    set_session_link_back url_for(:controller=> 'maps', :action => 'index',:skip_relative_url_root => false, :only_path => false )+ qstring
    
    @query = params[:query]
    
    @field = %w(text title description status catnyp nypl_digital_id uuid).detect{|f| f == (params[:field])}
    
    
    @field = "text" if @field.nil?
    where_col = @field
    
    if  @field == "text"
      where_col  = "(title || ' ' || description)"
    end
    
    #we'll use POSIX regular expression for searches    ~*'( |^)robinson([^A-z]|$)' and to strip out brakets etc  ~*'(:punct:|^|)plate 6([^A-z]|$)';
    if @query && @query.strip.length > 0 && @field
      conditions = ["#{where_col}  ~* ?", '(:punct:|^|)'+@query+'([^A-z]|$)']
    else
      conditions = nil
    end
    
    @year_min = Map.minimum(:issue_year).to_i - 1
    @year_max = Map.maximum(:issue_year).to_i + 1
    @year_min = 1600 if @year_min == 0
    @year_max = 2015 if @year_max == 0
    
    year_conditions = nil
    if params[:from] && params[:to] && !(@year_min == params[:from].to_i && @year_max == params[:to].to_i)
      year_conditions = {:issue_year => params[:from].to_i..params[:to].to_i}
    end
    
    @from = params[:from]
    @to = params[:to]
        
    if params[:sort_order] && params[:sort_order] == "desc"
      sort_nulls = " NULLS LAST"
    else
      sort_nulls = " NULLS FIRST"
    end
    @per_page = params[:per_page] || 50
    paginate_params = {
      :page => params[:page],
      :per_page => @per_page
    }
    order_options = sort_clause + sort_nulls
    where_options = conditions
    #order('name').where('name LIKE ?', "%#{search}%").paginate(page: page, per_page: 10)
    
    if @show_warped == "1"
      @maps = Map.warped.where(where_options).where(year_conditions).order(order_options).paginate(paginate_params)
    else
      @maps = Map.where(where_options).where(year_conditions).order(order_options).paginate(paginate_params)
    end
    
    @html_title = "Browse Maps"
    if request.xhr?
      render :action => 'index.rjs'
    else
      respond_to do |format|
        format.html{ render :layout =>'application' }  # index.html.erb
        format.xml  { render :xml => @maps.to_xml(:root => "maps", :except => [:content_type, :size, :parent_uuid, :filename, :parent_id,  :map, :thumbnail, :rough_centroid]) {|xml|
            xml.tag!'stat', "ok"
            xml.tag!'total-entries', @maps.total_entries
            xml.tag!'per-page', @maps.per_page
            xml.tag!'current-page',@maps.current_page} }
        
        format.json { render :json => {:stat => "ok",
            :current_page => @maps.current_page,
            :per_page => @maps.per_page,
            :total_entries => @maps.total_entries,
            :total_pages => @maps.total_pages,
            :items => @maps.to_a}.to_json(:except => [:content_type, :size, :parent_uuid, :filename, :parent_id,  :map, :thumbnail, :rough_centroid], :methods => :depicts_year) , :callback => params[:callback]
        }
      end
    end
    
  end
  
  
    
  def geosearch
    require 'geoplanet'
    sort_init 'updated_at'
    sort_update

    extents = [-74.1710,40.5883,-73.4809,40.8485] #NYC

    #TODO change to straight javascript call.
    if params[:place] && !params[:place].blank?
      place_query = params[:place]
      GeoPlanet.appid = APP_CONFIG['yahoo_app_id']
      
      geoplanet_result = GeoPlanet::Place.search(place_query, :count => 2)
      
      if geoplanet_result[0]
{'fraction_non_alphanumeric': 0.16525923016496466, 'fraction_numerical': 0.009033778476040848, 'mean_word_length': 3.371244635193133, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 71, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 17}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2471483', 'n_tokens_mistral': 3708, 'n_tokens_neox': 3567, 'n_words': 725}