/
images_controller.rb
114 lines (92 loc) · 3.52 KB
/
images_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
module ::Refinery
module Admin
class ImagesController < ::Admin::BaseController
helper_method :change_list_mode_if_specified
crudify :'refinery/image',
:order => "created_at DESC",
:sortable => false,
:xhr_paging => true
before_filter :change_list_mode_if_specified, :init_dialog
def new
@image = Refinery::Image.new if @image.nil?
@url_override = refinery_admin_images_path(:dialog => from_dialog?)
end
# This renders the image insert dialog
def insert
self.new if @image.nil?
@url_override = refinery_admin_images_path(request.query_parameters.merge(:insert => true))
if params[:conditions].present?
extra_condition = params[:conditions].split(',')
extra_condition[1] = true if extra_condition[1] == "true"
extra_condition[1] = false if extra_condition[1] == "false"
extra_condition[1] = nil if extra_condition[1] == "nil"
end
find_all_images(({extra_condition[0].to_sym => extra_condition[1]} if extra_condition.present?))
search_all_images if searching?
paginate_images
render :action => "insert"
end
def create
@images = []
begin
unless params[:image].present? and params[:image][:image].is_a?(Array)
@images << (@image = Refinery::Image.create(params[:image]))
else
params[:image][:image].each do |image|
@images << (@image = Refinery::Image.create(:image => image))
end
end
rescue Dragonfly::FunctionManager::UnableToHandle
logger.warn($!.message)
@image = Refinery::Image.new
end
unless params[:insert]
if @images.all?{|i| i.valid?}
flash.notice = t('created', :scope => 'refinery.crudify', :what => "'#{@images.collect{|i| i.title}.join("', '")}'")
unless from_dialog?
redirect_to :action => 'index'
else
render :text => "<script>parent.window.location = '#{refinery_admin_images_path}';</script>"
end
else
self.new # important for dialogs
render :action => 'new'
end
else
# if all uploaded images are ok redirect page back to dialog, else show current page with error
if @images.all?{|i| i.valid?}
@image_id = @image.id if @image.persisted?
@image = nil
redirect_to request.query_parameters.merge(:action => 'insert')
else
self.insert
end
end
end
protected
def init_dialog
@app_dialog = params[:app_dialog].present?
@field = params[:field]
@update_image = params[:update_image]
@thumbnail = params[:thumbnail]
@callback = params[:callback]
@conditions = params[:conditions]
end
def change_list_mode_if_specified
if action_name == 'index' and params[:view].present? and image_views.include?(params[:view].to_sym)
::Refinery::Setting.set(:preferred_image_view, params[:view])
end
end
def paginate_images
@images = @images.page(@paginate_page_number ||= params[:page])
.per(Image.per_page(from_dialog?, !@app_dialog))
end
def restrict_controller
super unless action_name == 'insert'
end
def store_current_location!
super unless action_name == 'insert' or from_dialog?
end
end
end
end