box_organizer_controller.rb
3.44 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
class BoxOrganizerController < ApplicationController
before_filter :login_required
def index
end
def move_block
@block = boxes_holder.blocks.find(params[:id].gsub(/^block-/, ''))
@source_box = @block.box
target_position = nil
if (params[:target] =~ /before-block-([0-9]+)/)
block_before = boxes_holder.blocks.find($1)
target_position = block_before.position
@target_box = block_before.box
else
(params[:target] =~ /end-of-box-([0-9]+)/)
@target_box = boxes_holder.boxes.find($1)
end
if (@source_box != @target_box)
@block.remove_from_list
@block.box = @target_box
end
if target_position.nil?
# insert in the end of the box
@block.insert_at(@target_box.blocks.size + 1)
@block.move_to_bottom
else
# insert the block in the given position
@block.insert_at(@block.position && @block.position < target_position ? target_position - 1 : target_position)
end
@block.save!
@target_box.reload
unless request.xhr?
redirect_to :action => 'index'
end
end
def move_block_down
@block = boxes_holder.blocks.find(params[:id])
@block.move_lower
redirect_to :action => 'index'
end
def move_block_up
@block = boxes_holder.blocks.find(params[:id])
@block.move_higher
redirect_to :action => 'index'
end
def add_block
type = params[:type]
if ! type.blank?
if available_blocks.map(&:name).include?(type)
boxes_holder.boxes.find(params[:box_id]).blocks << type.constantize.new
redirect_to :action => 'index'
else
raise ArgumentError.new("Type %s is not allowed. Go away." % type)
end
else
@center_block_types = (Box.acceptable_center_blocks & available_blocks) + plugins.dispatch(:extra_blocks, :type => boxes_holder.class, :position => 1)
@side_block_types = (Box.acceptable_side_blocks & available_blocks) + plugins.dispatch(:extra_blocks, :type => boxes_holder.class, :position => [2,3])
@boxes = boxes_holder.boxes.with_position
render :action => 'add_block', :layout => false
end
end
def edit
@block = boxes_holder.blocks.find(params[:id])
render :action => 'edit', :layout => false
end
def search_autocomplete
if request.xhr? and params[:query]
search = params[:query]
path_list = if boxes_holder.is_a?(Environment) && boxes_holder.enabled?('use_portal_community') && boxes_holder.portal_community
boxes_holder.portal_community.articles.find(:all, :conditions=>"name ILIKE '%#{search}%' or path ILIKE '%#{search}%'", :limit=>20).map { |content| "/{portal}/"+content.path }
elsif boxes_holder.is_a?(Profile)
boxes_holder.articles.find(:all, :conditions=>"name ILIKE '%#{search}%' or path ILIKE '%#{search}%'", :limit=>20).map { |content| "/{profile}/"+content.path }
else
[]
end
render :json => path_list.to_json
else
redirect_to "/"
end
end
def save
@block = boxes_holder.blocks.find(params[:id])
@block.update_attributes(params[:block])
redirect_to :action => 'index'
end
def boxes_editor?
true
end
def remove
@block = Block.find(params[:id])
if @block.destroy
redirect_to :action => 'index'
else
session[:notice] = _('Failed to remove block')
end
end
def clone_block
block = Block.find(params[:id])
block.duplicate
redirect_to :action => 'index'
end
protected :boxes_editor?
end