# -*- encoding : utf-8 -*-
class GloballinksController < ApplicationController

  before_filter :require_user

  access_control do
    actions :list do
      allow :admin
    end
    actions :index do
      allow anonymous, logged_in
    end
  end

  def index
    @countries = Country.where("counter > ?", 0)
    # @layers = Layer.find_all_by_public(true)
    @layers = Layer.find(:all)

    if current_user
      @placemarks = Placemark.find(:all,  :order => "updated_at DESC", :conditions => ["layer_id IN (?)", @layers])
    else
      @placemarks = Placemark.find(:all,  :order => "updated_at DESC", :conditions => ["layer_id IN (?) and public = ?", @layers, true])
    end
    respond_to do |format|
      format.json { render json: @countries }
      format.html
    end


  end

  def list

    # TODO: admins only!


    @countries = Country.all
    @maps = Map.find_all_by_public(true)
    @globallinks = GlobalLink.all

    gls = {maps: []}
    @globallinks.group_by(&:map).each do |map, globallinks|
      gls[:maps] << {
        id: map.id,
        title: map.title,
        globallinks: globallinks
      }
    end
    # results.to_json

    respond_to do |format|
      format.json { render json: @globallinks }
      format.html
    end


  end

  def show
  end
end
