Class: TemplatesController

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

Instance Method Summary (collapse)

Methods inherited from ApplicationController

#allow_cors, #auth!, #check_for_initial_install, #compute_pending_moderation, #current_ability, #delayed_job_running, #gh_latest_version, #latest_version, #precompile_error_catch, #process_notification, #restart_webserver, #set_locale, #set_version, #switch_to_main_app_ability, #switch_to_plugin_ability, #use_plugin_ability, #webserver_supports_restart?

Instance Method Details

- (Object) create

POST /templates POST /templates.xml



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

def create
  @template = Template.new(template_params)
  auth!
  @template.media.each do |media|
    media.key = "original"
  end
  

  respond_to do |format|
    if @template.save
      format.html { redirect_to(@template, :notice => t(:template_created)) }
      format.xml  { render :xml => @template, :status => :created, :location => @template }
    else
      format.html { redirect_to new_template_path(@template, :type => 'create'), :locals => {:template => @template} }
      format.xml  { render :xml => @template.errors, :status => :unprocessable_entity }
    end
  end
end

- (Object) destroy

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



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/controllers/templates_controller.rb', line 97

def destroy
  @template = Template.find(params[:id])
  auth!
  if @template.destroy
    respond_to do |format|
      format.html { redirect_to(templates_url) }
      format.xml  { head :ok }
    end
  else
    redirect_to(@template, :notice => t(:cannot_delete_template))
    return 
  end
end

- (Object) edit

GET /templates/1/edit



46
47
48
49
50
51
52
# File 'app/controllers/templates_controller.rb', line 46

def edit
  @template = Template.find(params[:id])
  auth!
  if(@template.media.empty?)
    @template.media.build
  end
end

- (Object) import

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

TODO - This should be cleaned up, we should throw smarter errors too.



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'app/controllers/templates_controller.rb', line 176

def import
  xml_file = params[:descriptor]
  image_file = params[:image]
  @template = Template.new(params[:template])
  auth!
  
  if xml_file.nil? || image_file.nil?
    respond_to do |format|
      format.html { render :action => "new" }
      format.xml  { render :xml => @template.errors, :status => :unprocessable_entity }
    end
  else
    begin
      xml_data = xml_file.read
      if !xml_data.blank? && @template.import_xml(xml_data)
        @template.media.build({:key=>"original", :file => image_file})
      end  
    rescue REXML::ParseException
      raise t(:template_import_error)
    end

    respond_to do |format|
      if @template.save
        format.html { redirect_to(@template, :notice => t(:template_created)) }
        format.xml  { render :xml => @template, :status => :created, :location => @template }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @template.errors, :status => :unprocessable_entity }
      end
    end
  end
end

- (Object) index

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



7
8
9
10
11
12
13
14
15
16
# File 'app/controllers/templates_controller.rb', line 7

def index
  @templates = Template.all
  auth!

  respond_to do |format|
    format.html # index.html.erb
    format.xml  { render :xml => @template }
    format.js { }
  end
end

- (Object) new

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



34
35
36
37
38
39
40
41
42
43
# File 'app/controllers/templates_controller.rb', line 34

def new
  @template = Template.new
  auth!
  @template.media.build

  respond_to do |format|
    format.html # new.html.erb
    format.xml  { render :xml => @template }
  end
end

- (Object) preview

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



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

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

  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

- (Object) show

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



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

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

  respond_to do |format|
    format.html # show.html.erb
    format.xml  { render :xml => @template.to_xml(:include => [:positions]) }
    format.js { }
  end
end

- (Object) update

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



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/controllers/templates_controller.rb', line 77

def update
  @template = Template.find(params[:id])
  auth!
  @template.media.each do |media|
    media.key = "original"
  end

  respond_to do |format|
    if @template.update_attributes(template_params)
      format.html { redirect_to(@template, :notice => t(:template_updated)) }
      format.xml  { head :ok }
    else
      format.html { render :action => "edit" }
      format.xml  { render :xml => @template.errors, :status => :unprocessable_entity }
    end
  end
end