hydra-head 3.1.0.pre5 → 3.1.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. data/HISTORY.textile +14 -1
  2. data/app/helpers/application_helper.rb +1 -166
  3. data/app/helpers/article_metadata_helper.rb +2 -78
  4. data/app/helpers/blacklight_helper.rb +2 -195
  5. data/app/helpers/downloads_helper.rb +3 -18
  6. data/app/helpers/generic_content_objects_helper.rb +2 -14
  7. data/app/helpers/hydra/application_helper_behavior.rb +10 -0
  8. data/app/helpers/hydra/article_metadata_helper_behavior.rb +82 -0
  9. data/app/helpers/hydra/blacklight_helper_behavior.rb +203 -0
  10. data/app/helpers/hydra/downloads_helper_behavior.rb +21 -0
  11. data/app/helpers/hydra/generic_content_objects_helper_behavior.rb +16 -0
  12. data/app/helpers/hydra/hydra_assets_helper_behavior.rb +66 -0
  13. data/app/helpers/hydra/hydra_djatoka_helper_behavior.rb +23 -0
  14. data/app/helpers/hydra/hydra_fedora_metadata_helper_behavior.rb +349 -0
  15. data/app/helpers/hydra/hydra_helper_behavior.rb +184 -0
  16. data/app/helpers/hydra/hydra_uploader_helper_behavior.rb +18 -0
  17. data/app/helpers/hydra/inline_editable_metadata_helper_behavior.rb +15 -0
  18. data/app/helpers/hydra/javascript_includes_helper_behavior.rb +91 -0
  19. data/app/helpers/hydra/personalization_helper_behavior.rb +44 -0
  20. data/app/helpers/hydra/release_process_helper_behavior.rb +32 -0
  21. data/app/helpers/hydra_assets_helper.rb +2 -64
  22. data/app/helpers/hydra_djatoka_helper.rb +3 -22
  23. data/app/helpers/hydra_fedora_metadata_helper.rb +2 -347
  24. data/app/helpers/hydra_helper.rb +2 -182
  25. data/app/helpers/hydra_uploader_helper.rb +2 -16
  26. data/app/helpers/inline_editable_metadata_helper.rb +3 -14
  27. data/app/helpers/javascript_includes_helper.rb +2 -89
  28. data/app/helpers/personalization_helper.rb +2 -42
  29. data/app/helpers/release_process_helper.rb +2 -30
  30. data/lib/blacklight/blacklight_helper_behavior.rb +612 -0
  31. data/lib/hydra-head/version.rb +1 -1
  32. data/lib/railties/hydra-fixtures.rake +0 -1
  33. data/tasks/hydra-head.rake +1 -2
  34. data/test_support/features/step_definitions/edit_metadata_steps.rb +0 -1
  35. data/{spec → test_support/spec}/controllers/catalog_valid_html_spec.rb +1 -11
  36. data/test_support/spec/helpers/blacklight_helper_spec.rb +6 -2
  37. data/test_support/spec/spec_helper.rb +5 -3
  38. metadata +23 -14
  39. data/lib/hydra/fixture_loader.rb +0 -48
  40. data/lib/stanford/searchworks_helper.rb +0 -1338
  41. data/lib/stanford/solr_helper.rb +0 -108
  42. data/lib/stanford_blacklight_extensions.rb +0 -6
  43. data/spec-rails2/helpers/hydra_fedora_metadata_helper_spec.rb +0 -219
  44. data/spec-rails2/spec_helper.rb +0 -88
@@ -0,0 +1,18 @@
1
+ module Hydra::HydraUploaderHelperBehavior
2
+
3
+ # Generate the appropriate url for posting uploads to
4
+ # Uses the +container_id+ method to figure out what container uploads should go into
5
+ def upload_url
6
+ upload_url = asset_file_assets_path(:asset_id=>container_id)
7
+ end
8
+
9
+ # The id of the container that uploads should be posted into
10
+ # If params[:container_id] is not set, it uses params[:id] (assumes that you're uploading items into the current object)
11
+ def container_id
12
+ if !params[:asset_id].nil?
13
+ return params[:asset_id]
14
+ else
15
+ return params[:id]
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,15 @@
1
+ module Hydra::InlineEditableMetadataHelperBehavior
2
+
3
+ def inline_editable_text_field(object_name, method, options = {})
4
+ end
5
+
6
+ def inline_editable_text_area(object_name, method, options = {})
7
+ end
8
+
9
+ def inline_editable_select(object_name, method, options = {})
10
+ end
11
+
12
+ def inline_editable_checkbox(object_name, method, options = {})
13
+ end
14
+
15
+ end
@@ -0,0 +1,91 @@
1
+ # Use this helper to declare which javascript should be loaded in which views
2
+ # Internally, it relies on include_javascript_for_#{controller}_#{action} helper methods
3
+ # These helper methods will usually append their includes to the controller's javascript_includes array
4
+ #
5
+ # @example Within your views (or in your controllers), call the helper like this
6
+ # include_javascript_for "hydrangea_articles", "edit"
7
+ #
8
+ # To declare your own array of includes for a specific content type or action, define a helper method in your host application or plugin like this
9
+ # @example Declaring the javascript includes for hydrangea_datasets show view while reusing the includes from catalog_edit
10
+ # def include_javascript_for_hydrangea_datasets_show
11
+ # include_javascript_for_catalog_edit
12
+ # javascript_includes << ['hydrangeaArticleBehaviors.js', {:plugin=>:hydrangea_articles}]
13
+ # end
14
+ module Hydra::JavascriptIncludesHelperBehavior
15
+
16
+ # Add the appropriate javascripts for the specified content type & action into the Controller's javascript_includes array
17
+ # If you have defined custom javascript includes for that content_type & action, they will be used.
18
+ # @param [String or Symbol] content_type
19
+ # @param [String or Symbol] action
20
+ # @example This will rely on the include_javascript_for_hydranea_articles_edit helper method if it's defined. Defaults to calling include_default_javascript("edit")
21
+ # include_javascript_for "hydrangea_articles", "edit"
22
+ def include_javascript_for(content_type, action, opts={})
23
+ begin
24
+ method_name = "include_javascript_for_#{content_type.to_s}_#{action.to_s}"
25
+ logger.debug "attempting to include #{method_name}"
26
+ self.send(method_name.to_sym)
27
+ rescue
28
+ logger.debug "... no specific includes defined for #{content_type.to_s}. Using defaults for #{action.to_s} views"
29
+ include_default_javascript( action )
30
+ end
31
+ end
32
+
33
+ # Add the default javascript to the controller's javascript_includes
34
+ # Takes a method argument (ie. show or edit) to decide which defaults to use.
35
+ # Currently configured to use the javascript includes for catalog show / edit.
36
+ # @param [String or Symbol] method Currently only "show" and "edit" have defaults set
37
+ def include_default_javascript(method)
38
+ case method.to_s
39
+ when "show"
40
+ include_javascript_for_catalog_show
41
+ when "edit"
42
+ include_javascript_for_catalog_edit
43
+ else
44
+ logger.debug "No default javascript includes defined for #{method} views. Doing nothing."
45
+ end
46
+ end
47
+
48
+ #
49
+ # Helpers for Catalog Show & Edit Javascript Includes
50
+ #
51
+
52
+ # Adds the appropriate javascripts to javascript_includes for CatalogController show views
53
+ # Override this if you want to change the set of javascript_includes for CatalogController show views
54
+ def include_javascript_for_catalog_show
55
+ javascript_includes << ['custom', {:plugin=>"hydra-head"}]
56
+
57
+ # This file contains the page initialization scripts for catalog show views
58
+ javascript_includes << ["catalog/show", {:plugin=>"hydra-head"}]
59
+ end
60
+
61
+ # Adds the appropriate javascripts to javascript_includes for CatalogController edit views
62
+ # Override this if you want to change the set of javascript_includes for CatalogController edit views
63
+ def include_javascript_for_catalog_edit
64
+ # This _would_ include the fluid infusion javascripts, but we don't want them
65
+ # javascript_includes << infusion_javascripts(:default_no_jquery, :extras=>[:inline_editor_integrations], :debug=>true, :render_html=>false)
66
+
67
+ javascript_includes << ["jquery.jeditable.mini.js", {:plugin=>"hydra-head"}]
68
+ javascript_includes << ["jquery.form.js", {:plugin=>"hydra-head"}]
69
+ javascript_includes << ['custom', {:plugin=>"hydra-head"}]
70
+
71
+ javascript_includes << ["jquery.hydraMetadata.js", {:plugin=>"hydra-head"}]
72
+ javascript_includes << ["jquery.notice.js", {:plugin=>"hydra-head"}]
73
+
74
+ javascript_includes << ["jquery.jeditable.mini.js", "date-picker/js/datepicker", "jquery.form.js", 'custom', "catalog/edit", "jquery.hydraMetadata.js", "jquery.notice.js", {:plugin=>"hydra-head"}]
75
+ # For DatePicker
76
+ javascript_includes << ["jquery.ui.widget.js","jquery.ui.datepicker.js", "mediashelf.datepicker.js", {:plugin=>"hydra-head" }]
77
+
78
+ # For Fancybox
79
+ javascript_includes << ["fancybox/jquery.fancybox-1.3.1.pack.js", {:plugin=>"hydra-head"}]
80
+ stylesheet_links << ["/javascripts/fancybox/jquery.fancybox-1.3.1.css", {:plugin=>"hydra-head"}]
81
+
82
+ # For slider controls
83
+ javascript_includes << ["select_to_ui_slider/selectToUISlider.jQuery.js", {:plugin=>"hydra-head"}]
84
+ stylesheet_links << ["/javascripts/select_to_ui_slider/css/ui.slider.extras.css", {:plugin=>"hydra-head"}]
85
+ stylesheet_links << ["slider", {:plugin=>"hydra-head"}]
86
+
87
+ # This file contains the page initialization scripts for catalog edit views
88
+ javascript_includes << ["catalog/edit", {:plugin=>"hydra-head"}]
89
+ end
90
+
91
+ end
@@ -0,0 +1,44 @@
1
+ module Hydra::PersonalizationHelperBehavior
2
+
3
+ DEFAULT_USER_ATTRIBUTES = ['full_name', 'affiliation', 'photo']
4
+
5
+ # Helper methods to retrieve information from the user attributes
6
+ #
7
+ # == get_full_name_from_login
8
+ #
9
+ # Given a login, returns a string concatenating first_name and last_name
10
+ #
11
+ # == get_affiliation_from_login
12
+ #
13
+ # Given a login, returns a string
14
+ #
15
+ # == get_photo_from_login
16
+ #
17
+ # Given a login, returns a string representing either a path or a url pointing to an image file
18
+ DEFAULT_USER_ATTRIBUTES.each do |m|
19
+ class_eval <<-EOC
20
+ def #{m}_from_login login
21
+ get_user_attribute(login, '#{m}')
22
+ end
23
+ EOC
24
+ end
25
+
26
+ # Creates an image tag with the user#photo attribute as the source
27
+ # @param [string] login the login of the user
28
+ # @return an html image tag or an empty string
29
+ def user_photo_tag login
30
+ path = photo_from_login login
31
+ path == "" ? "" : image_tag(path)
32
+ end
33
+
34
+ private
35
+
36
+ # Retrieves an attribute from the user
37
+ # @param [string] login the login of the user
38
+ # @param [string] the name of the attribute: out of the box values are first_name, last_name, full_name, affiliation, and photo
39
+ def get_user_attribute login, attribute
40
+ user = User.find_by_login(login)
41
+ user.nil? ? "" : user.send(attribute.to_sym)
42
+ end
43
+
44
+ end
@@ -0,0 +1,32 @@
1
+ module Hydra::ReleaseProcessHelperBehavior
2
+
3
+ def display_release_status_notice(document)
4
+ readiness = document.test_release_readiness
5
+ if readiness == true
6
+ flash[:notice] ||= []
7
+ if document.submitted_for_release?
8
+ flash[:notice] << "This item has been released for library circulation."
9
+ else
10
+ flash[:notice] << "This item is ready to be released for library circulation."
11
+ end
12
+ else
13
+ flash[:error] ||= []
14
+ flash[:error] = flash[:error] | readiness[:failures]
15
+ end
16
+ end
17
+
18
+ def check_embargo_date_format
19
+ if params.keys.include? [:embargo, :embargo_release_date]
20
+ em_date = params[[:embargo, :embargo_release_date]]["0"]
21
+ unless em_date.blank?
22
+ begin
23
+ !Date.parse(em_date)
24
+ rescue
25
+ params[[:embargo,:embargo_release_date]]["0"] = ""
26
+ raise "Unacceptable date format"
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ end
@@ -1,66 +1,4 @@
1
- require 'mediashelf/active_fedora_helper'
2
- require 'sanitize'
3
1
  module HydraAssetsHelper
4
- include MediaShelf::ActiveFedoraHelper
5
-
6
- # Create a link for creating a new asset of the specified content_type
7
- # If user is not logged in, the link leads to the login page with appropriate redirect params for creating the asset after logging in
8
- # @param [String] link_label for the link
9
- # @param [String] content_type
10
- def link_to_create_asset(link_label, content_type)
11
- if current_user
12
- link_to link_label, {:action => 'new', :controller => 'assets', :content_type => content_type}, :class=>"create_asset"
13
- else
14
- link_to link_label, new_user_session_path(:redirect_params => {:action => "new", :controller=> "assets", :content_type => content_type}), :class=>"create_asset"
15
- end
16
- end
17
-
18
- # Render a link to delete the given asset from the repository.
19
- # Includes a confirmation message.
20
- def delete_asset_link(pid, asset_type_display="asset")
21
- "<a href=\"#{ url_for(:action=>:delete, :controller=>:catalog, :id=>pid)}\" class=\"delete_asset_link\" >Delete this #{asset_type_display}</a>".html_safe
22
- end
23
-
24
- def document_type(document)
25
- if (document[Blacklight.config[:show][:display_type]])
26
- document[Blacklight.config[:show][:display_type]].first.gsub("info:fedora/afmodel:","")
27
- else ""
28
- end
29
- end
30
-
31
- def get_person_from_role(doc,role,opts={})
32
- i = 0
33
- while i < 10
34
- persons_roles = doc["person_#{i}_role_t"].map{|w|w.strip.downcase} unless doc["person_#{i}_role_t"].nil?
35
- if persons_roles and persons_roles.include?(role.downcase)
36
- return {:first=>doc["person_#{i}_first_name_t"], :last=>doc["person_#{i}_last_name_t"]}
37
- end
38
- i += 1
39
- end
40
- end
41
-
42
- def get_file_asset_count(document)
43
- count = 0
44
- obj = load_af_instance_from_solr(document)
45
- count += obj.file_objects.length unless obj.nil?
46
- count
47
- end
48
-
49
- def get_file_asset_description(document)
50
- obj = load_af_instance_from_solr(document)
51
- if obj.nil? || obj.file_objects.empty?
52
- return ""
53
- else
54
- fobj = FileAsset.load_instance_from_solr(obj.file_objects.first.pid)
55
- fobj.nil? ? "" : short_description(fobj.datastreams["descMetadata"].get_values("description").first)
56
- end
57
- end
58
-
59
- def short_description(desc,max=150)
60
- if desc.length > max
61
- desc = desc[0..max].concat("...")
62
- end
63
- short_description = desc.capitalize
64
- end
65
-
2
+ include Hydra::HydraAssetsHelperBehavior
66
3
  end
4
+
@@ -1,23 +1,4 @@
1
1
  module HydraDjatokaHelper
2
-
3
- def hydra_djatoka_url_for(document, opts={})
4
-
5
- if document.kind_of?(SolrDocument)
6
- pid = document.id
7
- elsif document.kind_of?(Mash)
8
- pid = document[:id]
9
- elsif document.respond_to?(:pid)
10
- pid = document.pid
11
- end
12
-
13
- if !pid.nil?
14
- if opts[:scale]
15
- result = url_for(:controller=>:get,:action=>"show",:id=>pid, :format=>"jp2", "image_server[scale]"=>opts[:scale])
16
- else
17
- result = url_for(:controller=>:get,:action=>"show",:id=>pid, :format=>"jp2", "image_server"=>true)
18
- end
19
- end
20
-
21
- end
22
-
23
- end
2
+ include Hydra::HydraDjatokaHelperBehavior
3
+ end
4
+
@@ -1,349 +1,4 @@
1
- require "inline_editable_metadata_helper"
2
- require "block_helpers"
3
- require "active_support"
4
- require "redcloth" # Provides textile parsing support for textile_area method
5
-
6
1
  module HydraFedoraMetadataHelper
7
-
8
- def fedora_text_field(resource, datastream_name, field_key, opts={})
9
- field_name = field_name_for(field_key)
10
- field_values = get_values_from_datastream(resource, datastream_name, field_key, opts)
11
- field_values = [""] if field_values.empty?
12
- field_values = [field_values.first] unless opts.fetch(:multiple, true)
13
-
14
- required = opts.fetch(:required, true) ? "required" : ""
15
-
16
- body = ""
17
-
18
- field_values.each_with_index do |current_value, z|
19
- base_id = generate_base_id(field_name, current_value, field_values, opts)
20
- name = "asset[#{datastream_name}][#{field_name}][#{z}]"
21
- body << "<input class=\"editable-edit edit\" id=\"#{base_id}\" data-datastream-name=\"#{datastream_name}\" name=\"#{name}\" value=\"#{h(current_value.strip)}\" #{required} type=\"text\" />"
22
- body << "<a href=\"\" title=\"Delete '#{h(current_value)}'\" class=\"destructive field\">Delete</a>" if opts.fetch(:multiple, true) && !current_value.empty?
23
- end
24
-
25
- result = field_selectors_for(datastream_name, field_key)
26
- result << body
27
-
28
- return result.html_safe
29
- end
30
-
31
- def fedora_text_area(resource, datastream_name, field_key, opts={})
32
- fedora_textile_text_area(resource, datastream_name, field_key, opts)
33
- end
34
-
35
- # Textile textarea varies from the other methods in a few ways
36
- # Since we're using jeditable with this instead of fluid, we need to provide slightly different hooks for the javascript
37
- # * we are storing the datastream name in data-datastream-name so that we can construct a load url on the fly when initializing the textarea
38
- def fedora_textile_text_area(resource, datastream_name, field_key, opts={})
39
- field_name = field_name_for(field_key)
40
- field_values = get_values_from_datastream(resource, datastream_name, field_key, opts)
41
- field_values = [""] if field_values.empty?
42
- if opts.fetch(:multiple, true)
43
- container_tag_type = :li
44
- else
45
- field_values = [field_values.first]
46
- container_tag_type = :span
47
- end
48
- body = ""
49
-
50
- field_values.each_with_index do |current_value, z|
51
- base_id = generate_base_id(field_name, current_value, field_values, opts)
52
- name = "asset[#{datastream_name}][#{field_name}][#{z}]"
53
- processed_field_value = Sanitize.clean( RedCloth.new(current_value, [:sanitize_html]).to_html, Sanitize::Config::BASIC)
54
- body << "<textarea class=\"editable-edit edit\" id=\"#{base_id}\" data-datastream-name=\"#{datastream_name}\" name=\"#{name}\" rows=\"10\" cols=\"25\">#{h(current_value.strip)}</textarea>"
55
- body << "<a href=\"\" title=\"Delete '#{h(current_value)}'\" class=\"destructive field\">Delete</a>" unless z == 0
56
- end
57
-
58
- result = field_selectors_for(datastream_name, field_key)
59
- result << body
60
-
61
- return result.html_safe
62
-
63
- end
64
-
65
- # Expects :choices option. Option tags for the select are generated from the :choices option using Rails "options_for_select":http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/options_for_select helper
66
- # If no :choices option is provided, returns a regular fedora_text_field
67
- def fedora_select(resource, datastream_name, field_key, opts={})
68
- if opts[:choices].nil?
69
- result = fedora_text_field(resource, datastream_name, field_key, opts)
70
- else
71
- choices = opts[:choices]
72
- field_name = field_name_for(field_key)
73
- field_values = get_values_from_datastream(resource, datastream_name, field_key, opts)
74
-
75
- body = ""
76
- z = 0
77
- base_id = generate_base_id(field_name, field_values.first, field_values, opts.merge({:multiple=>false}))
78
- name = "asset[#{datastream_name}][#{field_name}][#{z}]"
79
-
80
- body << "<select name=\"#{name}\" class=\"metadata-dd select-edit\" id=\"#{field_name}\">"
81
- body << options_for_select(choices, field_values)
82
- body << "</select>"
83
-
84
- result = field_selectors_for(datastream_name, field_key)
85
- result << body
86
- end
87
- return result.html_safe
88
- end
89
-
90
- def fedora_date_select(resource, datastream_name, field_key, opts={})
91
- field_name = field_name_for(field_key)
92
- field_values = get_values_from_datastream(resource, datastream_name, field_key, opts)
93
- base_id = generate_base_id(field_name, field_values.first, field_values, opts.merge({:multiple=>false}))
94
- name = "asset[#{datastream_name}][#{base_id}]"
95
-
96
- value = field_values.first
97
- field_value = value.nil? ? "" : value
98
-
99
- field_value[/(\d+)-(\d+)-(\d+)/]
100
- year = ($1.nil? or $1.empty?) ? "" : $1.to_i
101
- month = ($2.nil? or $2.empty?) ? "-1" : $2
102
- day = ($3.nil? or $3.empty?) ? "-1" : $3
103
-
104
- # Make sure that month and day values are double-digit
105
- [month, day].each {|v| v.length == 1 ? v.insert(0, "0") : nil }
106
-
107
-
108
- year_options = Array.new(101) {|i| 1910+i}
109
- # year_options = Array.new(4) {|i| 1990+i}
110
-
111
- year_options.insert(0, ["Year", "-1"])
112
-
113
- body = ""
114
- body << "<div class=\"date-select\" name=\"#{name}\">"
115
- body << "<input class=\"controlled-date-part w4em\" style=\"width:4em;\" type=\"text\" id=\"#{base_id}-sel-y\" name=\"#{base_id}-sel-y\" maxlength=\"4\" value=\"#{year}\" />"
116
- body << "<select class=\"controlled-date-part\" id=\"#{base_id}-sel-mm\" name=\"#{base_id}-sel-mm\">"
117
- body << options_for_select([["Month","-1"],["January", "01"],["February", "02"],["March", "03"],
118
- ["April", "04"],["May", "05"],["June", "06"],["July", "07"],["August", "08"],
119
- ["September", "09"],["October", "10"],["November", "11"],["December", "12"]
120
- ], month)
121
- body << "</select> / "
122
- body << "<select class=\"controlled-date-part\" id=\"#{base_id}-sel-dd\" name=\"#{base_id}-sel-dd\">"
123
- body << options_for_select([["Day","-1"],"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"], day)
124
- body << "</select>"
125
- body << "</div>"
126
- body << <<-EOF
127
- <script type="text/javascript">
128
- // <![CDATA[
129
- // since the form element ids need to be generated on the server side for the options, the options are attached to the wrapping div via the jQuery data() method.
130
- $('div.date-select[name="#{name}"]').data("opts", {
131
- formElements:{"#{base_id}-sel-dd":"d","#{base_id}-sel-y":"Y","#{base_id}-sel-mm":"m"}
132
- });
133
- // ]]>
134
- </script>
135
- EOF
136
-
137
- result = field_selectors_for(datastream_name, field_key)
138
- result << body
139
- return result.html_safe
140
- end
141
-
142
- def fedora_submit(resource, datastream_name, field_key, opts={})
143
- result = ""
144
- h_name = OM::XML::Terminology.term_hierarchical_name(*field_key)
145
- field_key.each do |pointer|
146
- result << tag(:input, :type=>"submit", :name=>"field_selectors[#{datastream_name}][#{h_name}]", :value => field_key.to_s.capitalize)
147
- end
148
- return result.html_safe
149
- end
150
-
151
- def fedora_checkbox(resource, datastream_name, field_key, opts={})
152
- result = ""
153
- field_values = get_values_from_datastream(resource, datastream_name, field_key, opts)
154
- h_name = OM::XML::Terminology.term_hierarchical_name(*field_key)
155
-
156
- v_name = field_key.last.to_s
157
-
158
- checked = field_values.first.downcase == "yes" ? "checked" : ""
159
-
160
- result = field_selectors_for(datastream_name, field_key)
161
-
162
- # adding so that customized checked and unchecked values can be passed in
163
- checked_value = (opts[:default_values] && opts[:default_values][:checked]) ? opts[:default_values][:checked] : "yes"
164
- unchecked_value = (opts[:default_values] && opts[:default_values][:unchecked]) ? opts[:default_values][:unchecked] : "no"
165
-
166
- result << tag(:input, :type=>"hidden", :id=>"#{h_name}_checked_value", :value=>checked_value )
167
- result << tag(:input, :type=>"hidden", :id=>"#{h_name}_unchecked_value", :value=>unchecked_value )
168
-
169
- if field_values.first.downcase == "yes"
170
- result << tag(:input, :type=>"checkbox", :id=>h_name, :class=>"fedora-checkbox", :name=>"asset[#{datastream_name}][#{h_name}][0]", :value=>checked_value, :checked=>"checked")
171
- else
172
- result << tag(:input, :type=>"checkbox", :id=>h_name, :class=>"fedora-checkbox", :name=>"asset[#{datastream_name}][#{h_name}][0]", :value=>unchecked_value)
173
- end
174
- return result.html_safe
175
- end
176
-
177
- # Expects :choices option.
178
- # :choices should be a hash with value/label pairs
179
- # :choices => {"first_choice"=>"Apple", "second_choice"=>"Pear" }
180
- # If no :choices option is provided, returns a regular fedora_text_field
181
- def fedora_radio_button(resource, datastream_name, field_key, opts={})
182
- if opts[:choices].nil?
183
- result = fedora_text_field(resource, datastream_name, field_key, opts)
184
- else
185
- choices = opts[:choices]
186
-
187
- field_name = field_name_for(field_key)
188
- field_values = get_values_from_datastream(resource, datastream_name, field_key, opts)
189
- h_name = OM::XML::Terminology.term_hierarchical_name(*field_key)
190
- default_value = opts.keys.include?(:default_value) ? opts[:default_value] : ""
191
-
192
- selected_value = field_values.empty? ? "" : field_values.first
193
- selected_value = default_value if selected_value.blank?
194
-
195
- body = ""
196
- z = 0
197
- base_id = generate_base_id(field_name, field_values.first, field_values, opts.merge({:multiple=>false}))
198
- name = "asset[#{datastream_name}][#{field_name}][#{z}]"
199
-
200
- result = field_selectors_for(datastream_name, field_key)
201
- choices.sort.each do |choice,label|
202
- if choice == selected_value
203
- result << tag(:input, :type=>"radio", :id=>"availability_#{choice}", :class=>"fedora-radio-button", :name=>"asset[#{datastream_name}][#{h_name}][0]", :value=>choice.downcase, :checked=>true)
204
- else
205
- result << tag(:input, :type=>"radio", :id=>"availability_#{choice}", :class=>"fedora-radio-button", :name=>"asset[#{datastream_name}][#{h_name}][0]", :value=>choice.downcase)
206
- end
207
- result << " <label>#{label}</label> "
208
- end
209
- result
210
- end
211
- return result.html_safe
212
- end
213
-
214
-
215
- def fedora_text_field_insert_link(datastream_name, field_key, opts={})
216
- field_name = field_name_for(field_key) || field_key
217
- field_type = field_name == "person" ? "person" : "textfield"
218
- link_text = "Add #{(opts[:label] || field_key.last || field_key).to_s.camelize.titlecase}"
219
- result = "<a class='addval #{field_type}' href='#' data-datastream-name=\"#{datastream_name}\" title='#{link_text}'>#{link_text}</a>"
220
- return result.html_safe
221
- end
222
-
223
- def fedora_text_area_insert_link(datastream_name, field_key, opts={})
224
- field_name = field_name_for(field_key)
225
- link_text = "Add #{(opts[:label] || field_key.last || field_key).to_s.camelize.titlecase}"
226
- result = "<a class='addval textarea' href='#' data-datastream-name=\"#{datastream_name}\" title='#{link_text}'>#{link_text}</a>"
227
- return result.html_safe
228
- end
229
-
230
- def fedora_field_label(datastream_name, field_key, label=nil)
231
- field_name = field_name_for(field_key)
232
- if label.nil?
233
- label = field_name
234
- end
235
- if params and params[:action] == "show"
236
- return content_tag(:span, label).html_safe
237
- else
238
- return content_tag("label", label, :for=>field_name).html_safe
239
- end
240
- end
241
-
242
- # Generate hidden inputs to handle mapping field names to server-side metadata mappings
243
- # this allows us to round-trip OM metadata mappings
244
- # also (importantly) allows us to avoid executing xpath queries from http requests.
245
- # *Note*: It's important that you serialize these inputs in order from top to bottom (standard HTML form behavior)
246
- def field_selectors_for(datastream_name, field_key)
247
- result = ""
248
- if field_key.kind_of?(Array)
249
- h_name = OM::XML::Terminology.term_hierarchical_name(*field_key)
250
- field_key.each do |pointer|
251
- if pointer.kind_of?(Hash)
252
- k = pointer.keys.first
253
- v = pointer.values.first
254
-
255
- result << tag(:input, :type=>"hidden", :class=>"fieldselector", :name=>"field_selectors[#{datastream_name}][#{h_name}][][#{k}]", :value=>v)
256
- else
257
- result << tag(:input, :type=>"hidden", :class=>"fieldselector", :name=>"field_selectors[#{datastream_name}][#{h_name}][]", :value=>pointer.to_s)
258
- end
259
- end
260
- end
261
- return result
262
- end
263
-
264
- # hydra_form_for block helper
265
- # allows you to construct an entire hydra form by passing a block into this method
266
- class HydraFormFor < BlockHelpers::Base
267
-
268
- def initialize(resource, opts={})
269
- @resource = resource
270
- end
271
-
272
- def fedora_label(datastream_name, field_key, opts={})
273
- helper.fedora_label(@resource, datastream_name, field_key, opts)
274
- end
275
-
276
- def fedora_text_field(datastream_name, field_key, opts={})
277
- helper.fedora_label(@resource, datastream_name, field_key, opts)
278
- end
279
-
280
- def fedora_text_area(datastream_name, field_key, opts={})
281
- helper.fedora_text_area(@resource, datastream_name, field_key, opts)
282
- end
283
-
284
- def fedora_select(datastream_name, field_key, opts={})
285
- helper.fedora_select(@resource, datastream_name, field_key, opts)
286
- end
287
-
288
- def fedora_submit(datastream_name, field_key, opts={})
289
- helper.fedora_submit(@resource, datastream_name, field_key, opts)
290
- end
291
-
292
- def fedora_checkbox(datastream_name, field_key, opts={})
293
- helper.fedora_checkbox(@resource, datastream_name, field_key, opts)
294
- end
295
-
296
- def fedora_radio_button(datastream_name, field_key, opts={})
297
- helper.fedora_radio_button(@resource, datastream_name, field_key, opts)
298
- end
299
-
300
- def fedora_text_field_insert_link(datastream_name, field_key, opts={})
301
- helper.fedora_text_field_insert_link(@resource, datastream_name, field_key, opts={})
302
- end
303
-
304
- def fedora_field_label(datastream_name, field_key, opts={})
305
- helper.fedora_field_label(@resource, datastream_name, field_key, opts)
306
- end
307
-
308
- def display(body)
309
- inner_html = content_tag :input, :type=>"hidden", :name=>"content_type", :value=>@resource.class.to_s.underscore
310
- inner_html = inner_html << body
311
- content_tag :form, inner_html
312
- end
313
-
314
- end
315
-
316
- #
317
- # Internal helper methods
318
- #
319
-
320
- # retrieve field values from datastream.
321
- # If :values is provided, skips accessing the datastream and returns the contents of :values instead.
322
- def get_values_from_datastream(resource, datastream_name, field_key, opts={})
323
- if opts.has_key?(:values)
324
- values = opts[:values]
325
- if values.nil? then values = [opts.fetch(:default, "")] end
326
- else
327
- values = resource.get_values_from_datastream(datastream_name, field_key, opts.fetch(:default, ""))
328
- if values.empty? then values = [ opts.fetch(:default, "") ] end
329
- end
330
- return values
331
- end
332
-
333
- def field_name_for(field_key)
334
- if field_key.kind_of?(Array)
335
- return OM::XML::Terminology.term_hierarchical_name(*field_key)
336
- else
337
- field_key.to_s
338
- end
339
- end
340
-
341
- def generate_base_id(field_name, current_value, values, opts)
342
- if opts.fetch(:multiple, true)
343
- return field_name+"_"+values.index(current_value).to_s
344
- else
345
- return field_name
346
- end
347
- end
348
-
2
+ include Hydra::HydraFedoraMetadataHelperBehavior
349
3
  end
4
+