Module: PrintControl

Extended by:
ActiveSupport::Concern
Included in:
AbstractResourcesController
Defined in:
app/controllers/concerns/print_control.rb

Instance Method Summary collapse

Instance Method Details

#display_print_resultObject

send HTML or PDF down the wire



119
120
121
122
123
124
125
126
# File 'app/controllers/concerns/print_control.rb', line 119

def display_print_result
  params[:print][:output_type] ||= 'html'
  case params[:print][:output_type].downcase
    # when 'html'; render params[:print][:view_template_path], layout: 'print'
  when 'html';  render "stock_items/print/_stock_items_list", layout: 'print'
  when 'pdf';   download_print_result
  end
end

#download_print_resultObject

send PDF down the wire



131
132
133
134
135
# File 'app/controllers/concerns/print_control.rb', line 131

def download_print_result
  params[:print][:output_type] ||= 'pdf'
  params[:print][:medium]="download"
  print_resources
end

#email_print_resultObject

send PDF via email



140
141
142
143
144
145
146
147
148
149
150
# File 'app/controllers/concerns/print_control.rb', line 140

def email_print_result
  params[:print][:output_type] ||= 'pdf'
  if params[:print][:email_to].blank?
    flash[:error] = t('output.email.email_address_missing')
    return 301
  else
    print_resources
    flash[:info] = t('output.email.email_sent')
    return 200
  end
end

print this view - let the Class handle everything returning either the ID to a print_job or false (in which case something went terribly wrong)

always an Ajax call - hence will always update the print_jobs link with ‘yellow’-blink POST /printers/print.js params holds records to be printed

from the print-dialog:

from a link tag



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
# File 'app/controllers/concerns/print_control.rb', line 86

def print
  authorize resource, :print?
  if resources.any?
    params[:context]                    = self
    params[:printed_by]                 = current_user || User.first
    params[:print]                      ||= {}
    params[:print][:collation]          ||= 'list'
    params[:print_job]                  ||= {}
    # params[:print_job][:view_template]  ||= current_user.account.find_print_template(params[:print][:template]) || Template.new( template_path: 'list.html.haml')
    # params[:print_job][:print_driver]   = params[:print][:print_driver] || params[:print_job][:view_template].template_print_driver
    # params[:print_job][:paper]          = params[:print_job][:view_template].template_paper || params[:print][:paper] || "A4"
    params[:print_job][:paper]          = params[:print][:paper] || "A4"
    #
    # ok so we have items to print!
    status = case params[:print][:medium]
    when "display"  ; display_print_result and return
    when "email"    ; email_print_result
    when "printer"  ; print_print_result
    when "download" ; download_print_result and return
    else
      flash[:error] = t('output.medium.no_medium_found');
      301
    end
  end
  render :print, layout: false, status: status and return

# rescue Exception => e
#   scoop_from_error e
end


152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'app/controllers/concerns/print_control.rb', line 152

def print_print_result
  params[:print][:output_type] ||= 'pdf'
  if (result = print_resources) > 0
    flash[:info] = t(:resources_printed_correct)
    status = 200
  else
    case result
    when NOQUEUE; flash[:error] = t(:noqueue_created)
    when NOUSER; flash[:error] = t(:no_user_present)
    when PRINTRECERROR; flash[:error] = t(:printing_record_error)
    when PRINTCMDERROR; flash[:error] = t(:print_command_error)
    when PRINTLISTERROR; flash[:error] = t(:printing_list_error)
    when PRINTEXCEPTION ; flash[:error] = t(:exception_in_print_engine)
    end
    status = 301
  end
  status
end

print_resources will try to add a print_job return error code



175
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
# File 'app/controllers/concerns/print_control.rb', line 175

def print_resources
  if resource_class == PrintJob
    resources.each do |res|
      return NOQUEUE unless Delayed::Job.enqueue res, :queue => 'printing'
    end
    return PRINTSUCCESS
  else
    return NOUSER if params[:printed_by].nil?
    case params[:print][:collation]
      when 'record'
        if params[:print][:cmd].blank?
          resources.each do |res|
            return PRINTRECERROR unless res.print_record params
          end
        else
          resources.each do |res|
            return PRINTCMDERROR unless res.send(params[:print][:cmd],params)
          end
        end

      when 'list'
        params[:resources] = resources
        return PRINTLISTERROR unless resource_class.print_list( params )

    end
  end

  return PRINTSUCCESS

end