2011-10-30 23:02:41 +00:00
|
|
|
module Locomotive::CustomFieldsHelper
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-10-07 00:45:41 +00:00
|
|
|
def options_for_field_kind
|
2011-05-19 15:44:04 +00:00
|
|
|
%w(string text category boolean date file has_one has_many).map do |kind|
|
2011-03-04 23:29:40 +00:00
|
|
|
[t("custom_fields.kind.#{kind}"), kind]
|
2010-07-23 20:09:54 +00:00
|
|
|
end
|
2010-05-19 16:17:45 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-25 00:32:12 +00:00
|
|
|
def options_for_order_by(content_type, collection_name)
|
2010-10-30 22:30:30 +00:00
|
|
|
options = %w{created_at updated_at _position_in_list}.map do |type|
|
2011-10-30 23:02:41 +00:00
|
|
|
[t("locomotive.content_types.form.order_by.#{type.gsub(/^_/, '')}"), type]
|
2010-05-25 00:32:12 +00:00
|
|
|
end
|
|
|
|
options + options_for_highlighted_field(content_type, collection_name)
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2011-03-09 23:54:38 +00:00
|
|
|
def options_for_order_direction
|
|
|
|
%w(asc desc).map do |direction|
|
2011-10-30 23:02:41 +00:00
|
|
|
[t("locomotive.content_types.form.order_direction.#{direction}"), direction]
|
2011-03-09 23:54:38 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-05-25 00:32:12 +00:00
|
|
|
def options_for_highlighted_field(content_type, collection_name)
|
|
|
|
custom_fields_collection_name = "ordered_#{collection_name.singularize}_custom_fields".to_sym
|
|
|
|
collection = content_type.send(custom_fields_collection_name)
|
2011-08-25 22:30:34 +00:00
|
|
|
collection.delete_if { |f| f.label == 'field name' || %w(file has_one has_many).include?(f.kind) }
|
2010-05-25 00:32:12 +00:00
|
|
|
collection.map { |field| [field.label, field._name] }
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-06-16 14:43:29 +00:00
|
|
|
def options_for_group_by_field(content_type, collection_name)
|
|
|
|
custom_fields_collection_name = "ordered_#{collection_name.singularize}_custom_fields".to_sym
|
|
|
|
collection = content_type.send(custom_fields_collection_name)
|
|
|
|
collection.delete_if { |f| not f.category? }
|
|
|
|
collection.map { |field| [field.label, field._name] }
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-06-22 13:04:40 +00:00
|
|
|
def options_for_text_formatting
|
2011-03-09 23:54:38 +00:00
|
|
|
options = %w(none html).map do |option|
|
2011-10-30 23:02:41 +00:00
|
|
|
[t("locomotive.custom_fields.text_formatting.#{option}"), option]
|
2010-06-22 13:04:40 +00:00
|
|
|
end
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2011-05-19 15:44:04 +00:00
|
|
|
def options_for_association_target
|
2011-08-25 22:30:34 +00:00
|
|
|
current_site.reload.content_types.collect { |c| [c.name, c.content_klass.to_s] }
|
2011-05-19 15:44:04 +00:00
|
|
|
end
|
|
|
|
|
2011-08-11 20:45:46 +00:00
|
|
|
def options_for_reverse_lookups(my_content_type)
|
|
|
|
klass_name = my_content_type.content_klass.to_s
|
|
|
|
|
|
|
|
[].tap do |options|
|
2011-12-05 14:29:32 +00:00
|
|
|
ContentType.where(:'contents_custom_fields.kind' => 'has_one', :'contents_custom_fields.target' => klass_name).each do |content_type|
|
|
|
|
content_type.contents_custom_fields.find_all { |f| f.has_one? && f.target == klass_name }.each do |field|
|
2011-08-11 20:45:46 +00:00
|
|
|
options << {
|
|
|
|
:klass => content_type.content_klass.to_s,
|
|
|
|
:label => field.label,
|
|
|
|
:name => field._name
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def filter_options_for_reverse_has_many(contents, reverse_lookup, object)
|
|
|
|
# Only display items which don't belong to a different object
|
|
|
|
contents.reject do |c|
|
|
|
|
owner = c.send(reverse_lookup.to_sym)
|
|
|
|
!(owner.nil? || owner == object._id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-06-23 23:17:08 +00:00
|
|
|
def options_for_has_one(field, value)
|
|
|
|
self.options_for_has_one_or_has_many(field) do |groups|
|
|
|
|
grouped_options_for_select(groups.collect do |g|
|
|
|
|
if g[:items].empty?
|
|
|
|
nil
|
|
|
|
else
|
|
|
|
[g[:name], g[:items].collect { |c| [c._label, c._id] }]
|
|
|
|
end
|
|
|
|
end.compact, value)
|
|
|
|
end
|
2011-05-19 15:44:04 +00:00
|
|
|
end
|
|
|
|
|
2011-08-11 20:45:46 +00:00
|
|
|
def options_for_has_many(field, content = nil)
|
|
|
|
self.options_for_has_one_or_has_many(field, content)
|
2011-06-23 23:17:08 +00:00
|
|
|
end
|
2011-05-19 15:44:04 +00:00
|
|
|
|
2011-08-11 20:45:46 +00:00
|
|
|
def options_for_has_one_or_has_many(field, content = nil, &block)
|
2011-08-30 21:13:34 +00:00
|
|
|
content_type = field.target.constantize._parent.reload
|
2011-06-23 23:17:08 +00:00
|
|
|
|
|
|
|
if content_type.groupable?
|
|
|
|
grouped_contents = content_type.list_or_group_contents
|
|
|
|
|
2011-08-11 20:45:46 +00:00
|
|
|
grouped_contents.each do |g|
|
|
|
|
g[:items] = filter_options_for_reverse_has_many(g[:items], field.reverse_lookup, content)
|
|
|
|
end if field.reverse_has_many?
|
|
|
|
|
2011-06-23 23:17:08 +00:00
|
|
|
if block_given?
|
|
|
|
block.call(grouped_contents)
|
|
|
|
else
|
|
|
|
grouped_contents.collect do |g|
|
|
|
|
if g[:items].empty?
|
|
|
|
nil
|
|
|
|
else
|
|
|
|
{ :name => g[:name], :items => g[:items].collect { |c| [c._label, c._id] } }
|
|
|
|
end
|
|
|
|
end.compact
|
|
|
|
end
|
|
|
|
else
|
|
|
|
contents = content_type.ordered_contents
|
2011-08-11 20:45:46 +00:00
|
|
|
|
|
|
|
if field.reverse_has_many?
|
|
|
|
contents = filter_options_for_reverse_has_many(contents, field.reverse_lookup, content)
|
|
|
|
end
|
|
|
|
|
2011-06-23 23:17:08 +00:00
|
|
|
contents.collect { |c| [c._label, c._id] }
|
|
|
|
end
|
2011-05-19 15:44:04 +00:00
|
|
|
end
|
|
|
|
|
2011-08-11 20:45:46 +00:00
|
|
|
def has_many_data_to_js(field, content)
|
|
|
|
options = {
|
|
|
|
:taken_ids => content.send(field._alias.to_sym).ids
|
|
|
|
}
|
|
|
|
|
|
|
|
if !content.new_record? && field.reverse_has_many?
|
|
|
|
url_options = {
|
|
|
|
:breadcrumb_alias => field.reverse_lookup_alias,
|
|
|
|
"content[#{field.reverse_lookup_alias}]" => content._id
|
|
|
|
}
|
|
|
|
|
|
|
|
options.merge!(
|
|
|
|
:new_item => {
|
2011-10-30 23:02:41 +00:00
|
|
|
:label => t('locomotive.contents.form.has_many.new_item'),
|
2011-10-31 23:44:23 +00:00
|
|
|
:url => new_content_url(field.target_klass._parent.slug, url_options)
|
2011-08-11 20:45:46 +00:00
|
|
|
},
|
2011-10-31 23:44:23 +00:00
|
|
|
:edit_item_url => edit_content_url(field.target_klass._parent.slug, 42, url_options)
|
2011-08-11 20:45:46 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
collection_to_js(options_for_has_many(field, content), options)
|
|
|
|
end
|
|
|
|
|
2010-07-23 20:09:54 +00:00
|
|
|
end
|