/
base_helper.rb
214 lines (178 loc) · 7.69 KB
/
base_helper.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
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
169
170
171
172
173
174
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
205
206
207
208
209
210
211
212
213
214
module Admin::BaseHelper
include ActionView::Helpers::DateHelper
def subtabs_for(current_module)
output = []
AccessControl.project_module(current_user.profile_label, current_module).submenus.each_with_index do |m,i|
next if m.name.empty?
current =
output << subtab(_(m.name), (m.url[:controller] == params[:controller] && m.url[:action] == params[:action]) ? '' : m.url)
end
output.join("\n").html_safe
end
def subtab(label, options = {})
return content_tag :li, link_to(label, '#'), :class => 'active' if options.empty?
content_tag :li, link_to(label, options)
end
def show_page_heading
return if @page_heading.nil? or @page_heading.blank?
heading = "<div class='page-header'>"
heading << content_tag(:h2, @page_heading.html_safe)
heading << "</div>"
end
def cancel(url = {:action => 'index'})
link_to _("Cancel"), url, :class => 'btn'
end
def save(val = _("Store"))
submit_tag(val, :class => 'btn btn-primary')
end
def link_to_edit(label, record, controller = controller.controller_name)
link_to label, {:controller => controller, :action => 'edit', :id => record.id}, :class => 'edit'
end
def link_to_edit_with_profiles(label, record, controller = controller.controller_name)
if current_user.admin? || current_user.id == record.user_id
link_to label, {:controller => controller, :action => 'edit', :id => record.id}, :class => 'edit'
end
end
def link_to_destroy(record, controller = controller.controller_name)
link_to image_tag('admin/delete.png', :alt => _("delete"), :title => _("Delete content")),
:controller => controller, :action => 'destroy', :id => record.id
end
def link_to_destroy_with_profiles(record, controller = controller.controller_name)
if current_user.admin? || current_user.id == record.user_id
link_to(_("delete"),
{ controller: controller, action: 'destroy', id: record.id },
data: {confirm: _("Are you sure?")},
method: :post, class: 'btn danger', title: _("Delete content"))
end
end
def text_filter_options
TextFilter.all.collect do |filter|
[ filter.description, filter ]
end
end
def text_filter_options_with_id
TextFilter.all.collect do |filter|
[ filter.description, filter.id ]
end
end
def plugin_options(kind, blank = true)
r = TypoPlugins::Keeper.available_plugins(kind).collect do |plugin|
[ plugin.name, plugin.to_s ]
end
blank ? r << [_("none"),''] : r
end
def alternate_class
@class = @class != '' ? '' : 'class="shade"'
end
def task_overview
content_tag :li, link_to(_('Back to list'), :action => 'index')
end
def collection_select_with_current(object, method, collection, value_method, text_method, current_value, prompt=false)
result = "<select name='#{object}[#{method}]'>\n"
if prompt == true
result << "<option value=''>" << _("Please select") << "</option>"
end
for element in collection
if current_value and current_value == element.send(value_method)
result << "<option value='#{element.send(value_method)}' selected='selected'>#{element.send(text_method)}</option>\n"
else
result << "<option value='#{element.send(value_method)}'>#{element.send(text_method)}</option>\n"
end
end
result << "</select>\n"
return result
end
def render_void_table(size, cols)
if size == 0
"<tr>\n<td colspan=#{cols}>" + _("There are no %s yet. Why don't you start and create one?", _(controller.controller_name)) + "</td>\n</tr>\n"
end
end
def cancel_or_save(message=_("Save"))
result = cancel
result << " "
result << _("or")
result << " "
result << save(message)
return result
end
def get_short_url(item)
return "" if item.short_url.nil?
sprintf("<small>%s %s</small>", _("Short url:"), link_to(item.short_url, item.short_url))
end
def show_actions item
html = <<-HTML
<div class='action'>
<small>#{link_to_published item}</small> |
<small>#{link_to _("Edit"), :action => 'edit', :id => item.id}</small> |
<small>#{link_to _("Delete"), :action => 'destroy', :id => item.id}</small> |
#{get_short_url item}
</div>
HTML
end
def format_date(date)
date.strftime('%d/%m/%Y')
end
def format_date_time(date)
date.strftime('%d/%m/%Y %H:%M')
end
def link_to_published(item)
return link_to_permalink(item, _("Show"), nil, 'published') if item.published
type = controller.controller_name == 'content' ? "" : "_page"
link_to(_("Preview"), {:controller => '/articles', :action => "preview#{type}", :id => item.id}, {:class => 'unpublished', :target => '_new'})
end
def published_or_not(item)
return "<span class='label label-success'>#{_("Published")}</span>" if item.state.to_s.downcase == 'published'
return "<span class='label label-info'>#{_("Draft")}</span>" if item.state.to_s.downcase == 'draft'
return "<span class='label label-important'>#{_("Withdrawn")}</span>" if item.state.to_s.downcase == 'withdrawn'
return "<span class='label label-warning'>#{_("Publication pending")}</span>" if item.state.to_s.downcase == 'publicationpending'
end
def macro_help_popup(macro, text)
unless current_user.editor == 'visual'
"<a href=\"#{url_for :controller => 'textfilters', :action => 'macro_help', :id => macro.short_name}\" onclick=\"return popup(this, 'Typo Macro Help')\">#{text}</a>"
end
end
def render_macros(macros)
result = link_to_function _("Show help on Typo macros") + " (+/-)",update_page { |page| page.visual_effect(:toggle_blind, "macros", :duration => 0.2) }
result << "<table id='macros' style='display: none;'>"
result << "<tr>"
result << "<th>#{_('Name')}</th>"
result << "<th>#{_('Description')}</th>"
result << "<th>#{_('Tag')}</th>"
result << "</tr>"
for macro in macros.sort_by { |f| f.short_name }
result << "<tr #{alternate_class}>"
result << "<td>#{macro_help_popup macro, macro.display_name}</td>"
result << "<td>#{h macro.description}</td>"
result << "<td><code><typo:#{h macro.short_name}></code></td>"
result << "</tr>"
end
result << "</table>"
end
def build_editor_link(label, action, id, update, editor)
link = link_to_remote(label,
:url => { :action => action, 'editor' => editor},
:class => 'ui-button-text',
:loading => "new Element.show('update_spinner_#{id}')",
:success => "new Element.toggle('update_spinner_#{id}')",
:update => "#{update}")
link << image_tag("spinner-blue.gif", :id => "update_spinner_#{id}", :style => 'display:none;')
end
def display_pagination(collection, cols, first='', last='')
"<tr><td class='#{first} #{last}' colspan=#{cols} class='paginate'>#{paginate(collection)}</td></tr>"
end
def show_thumbnail_for_editor(image)
thumb = "#{::Rails.root.to_s}/public/files/thumb_#{image.filename}"
picture = "#{this_blog.base_url}/files/#{image.filename}"
image.create_thumbnail unless File.exists? thumb
# If something went wrong with thumbnail generation, we just display a place holder
thumbnail = (File.exists? thumb) ? "#{this_blog.base_url}/files/thumb_#{image.filename}" : "#{this_blog.base_url}/images/thumb_blank.jpg"
picture = "<a onclick=\"edInsertImageFromCarousel('article_body_and_extended', '#{this_blog.base_url}/files/#{image.filename}');\" />"
picture << "<img class='tumb' src='#{thumbnail}' "
picture << "alt='#{this_blog.base_url}/files/#{image.filename}' />"
picture << "</a>"
return picture
end
def save_settings
"<div class='form-actions'>#{cancel} #{_("or")} #{save(_("Update settings"))}</div>".html_safe
end
end