Class: Backend::ListingsController

Inherits:
BaseController show all
Defined in:
app/controllers/backend/listings_controller.rb

Instance Attribute Summary

Attributes inherited from ApplicationController

#current_theme

Instance Method Summary collapse

Methods inherited from BaseController

accounts_range_crit, crit_params, journal_entries_states_crit, journal_period_crit, journals_crit, search_conditions

Methods inherited from ApplicationController

#after_sign_in_path_for, #authorized?, human_action_name, #human_action_name

Instance Method Details

#createObject


77
78
79
80
81
# File 'app/controllers/backend/listings_controller.rb', line 77

def create
  @listing = Listing.new listing_params
  return if save_and_redirect(@listing, url: { action: :edit, id: 'id'.c })
  # render_restfully_form
end

#destroyObject


97
98
99
100
101
# File 'app/controllers/backend/listings_controller.rb', line 97

def destroy
  return unless @listing = find_and_check
  Listing.destroy(@listing.id) if @listing
  redirect_to action: :index
end

#duplicateObject


103
104
105
106
107
# File 'app/controllers/backend/listings_controller.rb', line 103

def duplicate
  return unless @listing = find_and_check
  @listing.duplicate
  redirect_to action: :index
end

#editObject


83
84
85
86
87
# File 'app/controllers/backend/listings_controller.rb', line 83

def edit
  return unless @listing = find_and_check
  t3e @listing.attributes
  # render_restfully_form
end

#extractObject


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
# File 'app/controllers/backend/listings_controller.rb', line 41

def extract
  return unless @listing = find_and_check
  begin
    @listing.save unless @listing.query?
    query = @listing.query.to_s
    # FIXME: This is dirty code to solve quickly no_mail mode
    query.gsub!(' ORDER BY ', ' AND (' + @listing.coordinate_columns.collect { |c| "#{c.name} NOT LIKE '%@%.%'" }.join(' AND ') + ') ORDER BY ') if params[:mode] == 'no_mail'
    # FIXME: Manage suppression of CURRENT_COMPANY...
    first_item = []
    @listing.exportable_fields.each { |item| first_item << item.label }
    result = ActiveRecord::Base.connection.select_rows(query)
    result.insert(0, first_item)

    respond_to do |format|
      format.xml { render xml: result.to_xml, filename: @listing.name.simpleize + '.xml' }
      format.csv do
        csv_string = Ekylibre::CSV.generate do |csv|
          for item in result
            csv << item
          end
        end
        send_data(csv_string, filename: @listing.name.simpleize + '.csv', type: Mime::CSV)
      end
    end

  rescue Exception => e
    notify_error(:fails_to_extract_listing, message: e.message)
    redirect_to_back
  end
end

#indexObject

Displays the main page with the list of listings


37
38
39
# File 'app/controllers/backend/listings_controller.rb', line 37

def index
  session[:listing_coordinate_column] = nil
end

#mailObject


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
# File 'app/controllers/backend/listings_controller.rb', line 109

def mail
  return unless @listing = find_and_check
  if (query = @listing.query).blank?
    @listing.save
    query = @listing.query
  end
  query = query.to_s
  if !@listing.can_mail? || query.blank?
    notify_warning(:you_must_have_an_email_column)
    redirect_to_back
    return
  end
  if session[:listing_coordinate_column] || @listing.coordinate_columns.count == 1
    full_results = ActiveRecord::Base.connection.select_all(query)
    listing_coordinate_column = @listing.coordinate_columns.count == 1 ? @listing.coordinate_columns[0] : find_and_check(:listing_node, session[:listing_coordinate_column])
    # raise StandardError.new listing_coordinate_column.inspect
    results = full_results.select { |c| !c[listing_coordinate_column.label].blank? }
    @mails = results.collect { |c| c[listing_coordinate_column.label] }
    # @mails.uniq! ### CHECK ????????
    @columns = (!full_results.empty? ? full_results[0].keys.sort : [])
    session[:mail] ||= {}
  end
  if request.post?
    if params[:node]
      session[:listing_coordinate_column] = ListingNode.find_by_key(params[:node][:mail]).id
      redirect_to_back
    else
      session[:mail] = params.dup
      session[:mail].delete(:attachment)
      texts = [params[:mail_subject], params[:mail_body]]
      if attachment = (params[:attachment].blank? ? nil : params[:attachment])
        # file = "#{Rails.root.to_s}/tmp/uploads/attachment_#{attachment.original_filename.gsub(/\W/,'_')}"
        # File.open(file, "wb") { |f| f.write(attachment.read)}
        attachment = { filename: attachment.original_filename, content_type: attachment.content_type, body: attachment.read.dup }
      end
      if params[:send_test]
        results = [results[0]]
        results[0][listing_coordinate_column.label] = params[:from]
      end
      for result in results
        ts = texts.collect do |t|
          r = t.to_s.dup
          @columns.each { |c| r.gsub!(/\{\{#{c}\}\}/, result[c].to_s) }
          r
        end
        Mailman.mailing(params[:from], result[listing_coordinate_column.label], ts[0], ts[1], attachment).deliver
      end
      notify_success_now(:mails_are_sent)
      session[:listing_coordinate_column] = nil
    end
  end
  t3e @listing.attributes
end

#newObject


72
73
74
75
# File 'app/controllers/backend/listings_controller.rb', line 72

def new
  @listing = Listing.new
  # render_restfully_form
end

#updateObject


89
90
91
92
93
94
95
# File 'app/controllers/backend/listings_controller.rb', line 89

def update
  return unless @listing = find_and_check
  @listing.attributes = listing_params
  return if save_and_redirect(@listing, url: { action: :edit, id: 'id'.c })
  t3e @listing.attributes
  # render_restfully_form
end