Class: TemplatesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/templates_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#after_sign_in_path_for, #allow_cors, #allow_screen_if_unsecured, #apply_relative_root, #auth!, #check_for_initial_install, #compute_pending_moderation, #current_ability, #current_accessor, #current_screen, #extract_pagination_from_relation, #http_basic_user_name_and_password, #precompile_error_catch, #process_notification, #reapply_pagination, #restart_webserver, #screen_api, #set_locale, #set_time_zone, #set_version, #sign_in_screen, #sign_out_screen, #switch_to_main_app_ability, #switch_to_plugin_ability, #use_plugin_ability, #webserver_supports_restart?

Instance Method Details

#createObject

POST /templates POST /templates.xml



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/controllers/templates_controller.rb', line 53

def create
  @template = Template.new(template_params)
  auth!
  # set key based on file extension
  @template.media.each do |media|
    extension = (media.file_name.blank? ? nil : media.file_name.split('.')[-1].downcase)
    media.key = (extension == "css" ? "css" : "original") unless extension.nil?
  end

  # reject any empty media (key wont be set)
  @template.media.to_a.reject! {|i| i.key.blank?}

  respond_to do |format|
    if @template.save
      process_notification(@template, {}, process_notification_options({:params => {:template_name => @template.name}}))
      format.html { redirect_to(edit_template_path(@template), :notice => t(:template_created)) }
      format.xml  { render :xml => @template, :status => :created, :location => @template }
    else
      @type = "create"
      format.html { render :new }
      format.xml  { render :xml => @template.errors, :status => :unprocessable_entity }
    end
  end
end

#destroyObject

DELETE /templates/1 DELETE /templates/1.xml



112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'app/controllers/templates_controller.rb', line 112

def destroy
  @template = Template.find(params[:id])
  auth!

  unless @template.is_deletable?
    redirect_to(@template, :notice => t(:cannot_delete_template, :screens => @template.screens.collect { |s| s.name if can? :read, s}.join(", ")))
    return
  end

  process_notification(@template, {}, process_notification_options({:params => {:template_name => @template.name}}))
  @template.destroy
  respond_with(@template)
end

#editObject

GET /templates/1/edit



41
42
43
44
45
46
47
48
49
# File 'app/controllers/templates_controller.rb', line 41

def edit
  @template = Template.find(params[:id])
  auth!
  # make sure that we only have two html file inputs, one for the graphic ("original") and one for the stylesheet ("css")
  @template.media.reject! {|m| true}
  while @template.media.length < 2 do
    @template.media.build
  end
end

#importObject

PUT /templates/import PUT /templates/import.xml Import a template from an XML description and convert it to an actual template model.



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'app/controllers/templates_controller.rb', line 189

def import
  @template = Template.new
  archive = params[:package]

  if @template.import_archive(archive)
    # is_hidden checkbox supercedes xml
    @template.is_hidden = template_params[:is_hidden]
    if @template.save
      process_notification(@template, {}, process_notification_options({:params => {:template_name => @template.name}}))
      flash[:notice] = t(:template_created)
    end
  end

  respond_with(@template)
end

#indexObject

GET /templates GET /templates.xml GET /templates.js



11
12
13
14
15
# File 'app/controllers/templates_controller.rb', line 11

def index
  @templates = Template.all
  auth!
  respond_with(@templates)
end

#newObject

GET /templates/new GET /templates/new.xml



31
32
33
34
35
36
37
38
# File 'app/controllers/templates_controller.rb', line 31

def new
  @template = Template.new
  auth!
  # one for the graphic background and one for the css file
  @template.media.build()
  @template.media.build()
  respond_with(@template)
end

#previewObject

GET /template/1/preview Generate a preview of the template based on the request format.



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
179
180
181
182
183
# File 'app/controllers/templates_controller.rb', line 128

def preview
  @template = Template.find(params[:id])
  auth!(:action => :preview)

  if stale?(:last_modified => @template.last_modified.utc, :etag => @template, :public => true)
    # Hide the fields if the hide_fields param is set,
    # show them by default though.
    @hide_fields = false
    if !params[:hide_fields].nil?
      @hide_fields = [true, "true", 1, "1"].include?(params[:hide_fields])
    end

    # Hide the field names if the hide_text param is set,
    # show them by default though.
    @hide_text = false
    if !params[:hide_text].nil?
      @hide_text = [true, "true", 1, "1"].include?(params[:hide_text])
    end

    @only_fields = []
    if !params[:fields].nil?
      @only_fields = params[:fields].split(',').map{|i| i.to_i}
    end

    jpg =  Mime::Type.lookup_by_extension(:jpg)  #JPG is getting defined elsewhere.
    if([jpg, Mime::PNG, Mime::HTML].include?(request.format))
      @image = nil
      @image = @template.preview_image(@hide_fields, @hide_text, @only_fields)

      # Resize the image if needed.
      # We do this post-field drawing because RMagick seems to struggle with small font sizes.
      if  !params[:height].nil? || !params[:width].nil?
        require 'concerto_image_magick'
        @image = ConcertoImageMagick.resize(@image, params[:width].to_i, params[:height].to_i)
      end

      case request.format
        when jpg
          @image.format = "JPG"
        when Mime::PNG
          @image.format = "PNG"
      end

      data = nil
      data = @image.to_blob

      send_data data,
                :filename => "#{@template.name.underscore}.#{@image.format.downcase}_preview",
                :type => @image.mime_type, :disposition => 'inline'
    else
      respond_to do |format|
        format.svg
      end
    end
  end
end

#showObject

GET /templates/1 GET /templates/1.xml GET /templates/1.js



20
21
22
23
24
25
26
27
# File 'app/controllers/templates_controller.rb', line 20

def show
  @template = Template.find(params[:id])
  run_callbacks :show # Run plugin hooks
  auth!
  respond_with(@template) do |format|
    format.xml { render :xml => @template.to_xml(:include => [:positions])  }
  end
end

#updateObject

PUT /templates/1 PUT /templates/1.xml



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
# File 'app/controllers/templates_controller.rb', line 80

def update
  @template = Template.find(params[:id])
  auth!

  # get a copy of the params and remove the files as we process them
  template_parameters = template_params
  template_parameters[:media_attributes].each do |k, media|
    if !media.empty?
      # for the files that were uploaded, determine their media key based on their extension
      new_media = @template.media.build(media)
      extension = (new_media.file_name.blank? ? nil : new_media.file_name.split('.')[-1].downcase)
      new_media.key = (extension == "css" ? "css" : "original") unless extension.nil?

      # mark any existing @template.media with this same key as replaced (obsolete)
      @template.media.each do |m|
        m.key = 'replaced_' + m.key if m.key == new_media.key and m != new_media #!m.new_record?
      end
    end
    # remove the html file from the attributes so it is not processed in the in the update_attributes below
    template_parameters[:media_attributes].delete(k)
  end unless template_parameters[:media_attributes].nil?

  if @template.update_attributes(template_parameters)
    process_notification(@template, {}, process_notification_options({:params => {:template_name => @template.name}}))
    flash[:notice] = t(:template_updated)
  end

  respond_with(@template)
end