cirandas.net

ref: master

plugins/fb_app/controllers/public/fb_app_plugin_page_tab_controller.rb


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
class FbAppPluginPageTabController < FbAppPluginController

  no_design_blocks

  before_filter :change_theme
  before_filter :disable_cache

  include ProductsPlugin::CatalogHelper

  helper ProductsPlugin::CatalogHelper
  helper ProductsPlugin::ProductsHelper
  helper FbAppPlugin::DisplayHelper

  def index
    return unless load_page_tabs

    if params[:tabs_added]
      @page_tabs = FbAppPlugin::PageTab.create_from_tabs_added params[:tabs_added]
      @page_tab  = @page_tabs.first
      redirect_to @page_tab.facebook_url
    elsif @signed_request or @page_id
      if @page_tab.present?
        if product_id = params[:product_id]
          @product = environment.products.find product_id
          @profile = @product.profile
          @inputs  = @product.inputs
          @allowed_user = false
          load_catalog

          render action: 'product'
        elsif @page_tab.config_type.in? [:profile, :own_profile]
          @profile = @page_tab.value

          load_catalog
          render action: 'catalog' unless performed?
        else
          # fake profile for catalog controller
          @profile = environment.enterprise_default_template
          @profile.shopping_cart_settings.enabled = true

          base_query = @page_tab.value
          params[:base_query] = base_query
          params[:scope] = 'all'

          load_catalog
          render action: 'catalog' unless performed?
        end
      else
        render action: 'first_load'
      end
    else
      # render template
      render action: 'index'
    end
  end

  def search_autocomplete
    load_page_tabs
    load_search_autocomplete
    respond_to do |format|
      format.json{ render 'catalog/search_autocomplete' }
    end
  end

  def admin
    return redirect_to '/plugin/fb_app/myprofile_config' if params[:page_id].blank? and params[:signed_request].blank?
    return unless load_page_tabs

    if request.patch? and @page_id.present?
      create_page_tabs if @page_tab.nil?

      @page_tab.update! params[:page_tab]

      respond_to do |format|
        format.js{ render action: 'admin' }
      end
    end
  end

  def destroy
    @page_tab = FbAppPlugin::PageTab.find params[:id]
    return render_access_denied unless user.present? and (user.is_admin?(environment) or user.is_admin? @page_tab.profile)
    @page_tab.destroy
    render nothing: true
  end

  def uninstall
    render text: params.to_yaml
  end

  def enterprise_search
    scope = environment.enterprises.enabled.is_public
    @query = params[:query]
    @profiles = scope.limit(10).order('name ASC').
      where(['nickname ILIKE ? OR nickname ILIKE ? OR name ILIKE ? OR name ILIKE ? OR identifier LIKE ?',
             "#{@query}%", "% #{@query}%", "#{@query}%", "% #{@query}%", "#{@query}%"])
    render partial: 'open_graph_plugin/myprofile/profile_search', locals: {profiles: @profiles}
  end

  # unfortunetely, this needs to be public
  def profile
    @profile
  end

  protected

  def default_url_options
    {profile: @profile.identifier} if @profile
  end

  def load_page_tabs
    @signed_requests = read_param params[:signed_request]
    if @signed_requests.present?
      @page_ids = @signed_requests.map do |signed_request|
        @data   = FbAppPlugin::Auth.parse_signed_request signed_request
        @data[:page][:id] rescue nil
      end
    else
      @page_ids = read_param params[:page_id]
    end

    @page_tabs      = FbAppPlugin::PageTab.where page_id: @page_ids
    @signed_request = @signed_requests.first
    @page_id        = @page_ids.first
    @page_tab       = @page_tabs.first
    @profile        = @page_tab.profile if @page_tab

    if @page_tab.blank?
      render_not_found
      return false
    end

    response.headers['X-Frame-Options'] = 'ALLOWALL'

    true
  end

  def create_page_tabs
    @page_tabs  = FbAppPlugin::PageTab.create_from_page_ids @page_ids
    @page_tab ||= @page_tabs.first
  end

  def change_theme
    # move to config
    unless theme_responsive?
      @current_theme = 'ees'
      @theme_responsive = true
    end
    @without_pure_chat = true
  end
  def get_layout
    return nil if request.format == :js or request.xhr?

    return 'application-responsive'
  end

  def disable_cache
    @disable_cache_theme_navigation = true
  end

  def load_catalog options = {}
    @use_show_more = true
    catalog_load_index options
  end

  def read_param param
    if param.is_a? Hash
      param.values
    else
      Array(param).select{ |p| p.present? }
    end
  end

  def protect_against_forgery?
    false
  end

end