From 29f9c09439e017356f717255c104895a646bcf54 Mon Sep 17 00:00:00 2001 From: Marcelo JĂșnior Date: Fri, 26 Sep 2014 16:44:44 -0300 Subject: [PATCH] dspace_plugin : refactoring add action for communities & collections --- plugins/dspace/controllers/dspace_plugin_controller.rb | 20 ++++++++++++++++++-- plugins/dspace/controllers/dspace_plugin_myprofile_controller.rb | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ plugins/dspace/lib/dspace/item.rb | 2 -- plugins/dspace/public/javascripts/dspace_plugin.js | 22 ++++++++++++++++++++++ plugins/dspace/public/style.css | 3 ++- plugins/dspace/views/cms/dspace_plugin/_collection.html.erb | 22 +++++++++++++++++++--- plugins/dspace/views/cms/dspace_plugin/_item.html.erb | 1 - plugins/dspace/views/content_viewer/_item.html.erb | 1 - plugins/dspace/views/content_viewer/community.html.erb | 3 +-- plugins/dspace/views/content_viewer/library.html.erb | 2 +- plugins/dspace/views/dspace_plugin_myprofile/dspace_plugin/_collection.html.erb | 38 ++++++++++++++++++++++++++++++++++++++ plugins/dspace/views/dspace_plugin_myprofile/dspace_plugin/_communityy.html.erb | 34 ++++++++++++++++++++++++++++++++++ plugins/dspace/views/dspace_plugin_myprofile/edit.html.erb | 42 ++++++++++++++++++++++++++++++++++++++++++ 13 files changed, 256 insertions(+), 13 deletions(-) create mode 100644 plugins/dspace/controllers/dspace_plugin_myprofile_controller.rb create mode 100644 plugins/dspace/public/javascripts/dspace_plugin.js create mode 100644 plugins/dspace/views/dspace_plugin_myprofile/dspace_plugin/_collection.html.erb create mode 100644 plugins/dspace/views/dspace_plugin_myprofile/dspace_plugin/_communityy.html.erb create mode 100644 plugins/dspace/views/dspace_plugin_myprofile/edit.html.erb diff --git a/plugins/dspace/controllers/dspace_plugin_controller.rb b/plugins/dspace/controllers/dspace_plugin_controller.rb index 3701424..f7b753b 100644 --- a/plugins/dspace/controllers/dspace_plugin_controller.rb +++ b/plugins/dspace/controllers/dspace_plugin_controller.rb @@ -2,8 +2,24 @@ class DspacePluginController < PublicController append_view_path File.join(File.dirname(__FILE__) + '/../views') def view_item - @collection = DspacePlugin::Collection.find(params[:collection_id]) - @item = Dspace::Item.get_item_by_id 6 + + collection_id = params[:collection_id] + item_id = params[:id] + + begin + @item = Dspace::Item.get_item_by_id item_id + rescue ActiveResource::UnauthorizedAccess + render_not_found + return + end + + begin + @collection = DspacePlugin::Collection.find(collection_id) + rescue ActiveRecord::RecordNotFound + render_not_found + return + end + end end diff --git a/plugins/dspace/controllers/dspace_plugin_myprofile_controller.rb b/plugins/dspace/controllers/dspace_plugin_myprofile_controller.rb new file mode 100644 index 0000000..fe52c6e --- /dev/null +++ b/plugins/dspace/controllers/dspace_plugin_myprofile_controller.rb @@ -0,0 +1,79 @@ +class DspacePluginMyprofileController < CmsController + append_view_path File.join(File.dirname(__FILE__) + '/../views') + + def new_dspace_content + + @success_back_to = params[:success_back_to] + + @parent = profile.articles.find(params[:parent_id]) if params && params[:parent_id] + record_coming + @type = params[:type] + if @type.blank? + @article_types = [] + available_article_types.each do |type| + @article_types.push({ + :class => type, + :short_description => type.short_description, + :description => type.description + }) + end + @parent_id = params[:parent_id] + render :action => 'select_article_type', :layout => false, :back_to => @back_to + return + else + refuse_blocks + end + + raise "Invalid article type #{@type}" unless valid_article_type?(@type) + + klass = @type.constantize + article_data = environment.enabled?('articles_dont_accept_comments_by_default') ? { :accept_comments => false } : {} + article_data.merge!(params[:article]) if params[:article] + + if @type == 'DspacePlugin::Collection' + ids_list = 'dspace_collections_ids' + names_list = 'dspace_collections_names' + elsif @type == 'DspacePlugin::Communityy' + ids_list = 'dspace_communities_ids' + names_list = 'dspace_communities_names' + end + + index = -1 + + article_data[ids_list].each do |id| + + index += 1 + + name = article_data[names_list][index] + + entity = klass.new + + parent = check_parent(params[:parent_id]) + + if parent + entity.parent = parent + parent_id = parent.id + end + + entity.dspace_community_id = id if @type == 'DspacePlugin::Communityy' + + if @type == 'DspacePlugin::Collection' + entity.dspace_collection_id = id + entity.dspace_community_id = article_data['dspace_community_id'] + end + + entity.profile = profile + entity.author = user + entity.last_changed_by = user + entity.created_by = user + entity.name = name + + entity.save! + + end + + redirect_to @parent.view_url + + end + +end diff --git a/plugins/dspace/lib/dspace/item.rb b/plugins/dspace/lib/dspace/item.rb index 58d99c5..98d5109 100644 --- a/plugins/dspace/lib/dspace/item.rb +++ b/plugins/dspace/lib/dspace/item.rb @@ -74,8 +74,6 @@ class Dspace::Item < Dspace::Resource bitstream.format = bs.attributes[:format] bitstream.link = bs.attributes[:link] - #raise bitstream.to_yaml - item.files << bitstream end diff --git a/plugins/dspace/public/javascripts/dspace_plugin.js b/plugins/dspace/public/javascripts/dspace_plugin.js new file mode 100644 index 0000000..7f5f8b0 --- /dev/null +++ b/plugins/dspace/public/javascripts/dspace_plugin.js @@ -0,0 +1,22 @@ +function selectCommunity(element, community_slug) { + var hidden_field = jQuery('').attr({ + id: 'article_dspace_community_name_', + name: 'article[dspace_communities_names][]', + type: 'hidden', + name: 'article[dspace_communities_names][]', + value: community_slug + }); + jQuery(hidden_field).insertAfter(element); +} + +function selectCollection(element, collection_slug) { + var hidden_field = jQuery('').attr({ + id: 'article_dspace_collection_name_', + name: 'article[dspace_collections_names][]', + type: 'hidden', + name: 'article[dspace_collections_names][]', + value: collection_slug + }); + jQuery(hidden_field).insertAfter(element); +} + diff --git a/plugins/dspace/public/style.css b/plugins/dspace/public/style.css index 95712cb..b5a477e 100644 --- a/plugins/dspace/public/style.css +++ b/plugins/dspace/public/style.css @@ -8,7 +8,8 @@ } #dspace_library li.item, -#dspace_library li.collection { +#dspace_library li.collection, +#dspace_library li.community { margin: 10px 0; } diff --git a/plugins/dspace/views/cms/dspace_plugin/_collection.html.erb b/plugins/dspace/views/cms/dspace_plugin/_collection.html.erb index 53f34f1..730cb27 100644 --- a/plugins/dspace/views/cms/dspace_plugin/_collection.html.erb +++ b/plugins/dspace/views/cms/dspace_plugin/_collection.html.erb @@ -1,8 +1,8 @@

<%= _('DSpace Collection') %>

-<%= required_fields_message %> +<%#= required_fields_message %> -<%= required f.text_field('name', :size => '64', :maxlength => 150) %> +<%#= required f.text_field('name', :size => '64', :maxlength => 150) %> <%= hidden_field_tag 'article[parent_id]', @article.parent_id %> @@ -16,7 +16,23 @@ <% collections = Dspace::Community.get_all_collections_from( dspace_server_url, community_id ).map { |collection| item = [_(collection.name), collection.id] } %> -<%= labelled_form_field(_('Collections:'), + + + + + + <% collections.each do |collection| %> + + + + + <% end %> +
<%= check_box_tag "article[dspace_collection_id]", true %><%= _('DSpace Collection') %>
<%= check_box_tag "article[dspace_collection_id][]", collection[1] %> + <%= hidden_field_tag "article[dspace_collection_name][]", collection[0].to_slug %> + <%= collection[0] %> +
+ +<%#= labelled_form_field(_('Collections:'), select(:article, :dspace_collection_id, options_for_select_with_title(collections))) %> diff --git a/plugins/dspace/views/cms/dspace_plugin/_item.html.erb b/plugins/dspace/views/cms/dspace_plugin/_item.html.erb index 21c7b03..9a34721 100644 --- a/plugins/dspace/views/cms/dspace_plugin/_item.html.erb +++ b/plugins/dspace/views/cms/dspace_plugin/_item.html.erb @@ -21,4 +21,3 @@ collections_2 = collections.map { |collection| :dspace_collection_id, options_for_select_with_title(collections_2))) %> -<% #raise collections_2.inspect %> diff --git a/plugins/dspace/views/content_viewer/_item.html.erb b/plugins/dspace/views/content_viewer/_item.html.erb index 8757ac6..e7aeec8 100644 --- a/plugins/dspace/views/content_viewer/_item.html.erb +++ b/plugins/dspace/views/content_viewer/_item.html.erb @@ -1,5 +1,4 @@
  • - <%= link_to item.name, :controller => 'dspace_plugin', :action => 'view_item', :id => item.id, :collection_id => @page.id %>
    <%= item.author %> (<%= item.issue_date %>)
  • diff --git a/plugins/dspace/views/content_viewer/community.html.erb b/plugins/dspace/views/content_viewer/community.html.erb index 5527c6d..e85faa5 100644 --- a/plugins/dspace/views/content_viewer/community.html.erb +++ b/plugins/dspace/views/content_viewer/community.html.erb @@ -6,7 +6,7 @@ <% if dspace_community.allow_create?(user) %>
    - <%= content_tag('a', :href => url_for({:controller => 'cms', :action => 'new', :type => "DspacePlugin::Collection", :parent_id => dspace_community.id}), :class => 'button with-text icon-add') do %> + <%= content_tag('a', :href => url_for({:controller => 'dspace_plugin_myprofile', :action => 'new', :type => "DspacePlugin::Collection", :parent_id => dspace_community.id}), :class => 'button with-text icon-add') do %> <%= _("Add a %s") % DspacePlugin::Collection.short_description %> <% end %>
    @@ -22,4 +22,3 @@ - diff --git a/plugins/dspace/views/content_viewer/library.html.erb b/plugins/dspace/views/content_viewer/library.html.erb index 0800839..3305f88 100644 --- a/plugins/dspace/views/content_viewer/library.html.erb +++ b/plugins/dspace/views/content_viewer/library.html.erb @@ -3,7 +3,7 @@ <% if dspace_library.allow_create?(user) %>
    - <%= content_tag('a', :href => url_for({:controller => 'cms', :action => 'new', :type => "DspacePlugin::Communityy", :parent_id => dspace_library.id}), :class => 'button with-text icon-add') do %> + <%= content_tag('a', :href => url_for({:controller => 'dspace_plugin_myprofile', :action => 'new', :type => "DspacePlugin::Communityy", :parent_id => dspace_library.id}), :class => 'button with-text icon-add') do %> <%= _("Add a %s") % DspacePlugin::Communityy.short_description %> <% end %>
    diff --git a/plugins/dspace/views/dspace_plugin_myprofile/dspace_plugin/_collection.html.erb b/plugins/dspace/views/dspace_plugin_myprofile/dspace_plugin/_collection.html.erb new file mode 100644 index 0000000..d4b1d20 --- /dev/null +++ b/plugins/dspace/views/dspace_plugin_myprofile/dspace_plugin/_collection.html.erb @@ -0,0 +1,38 @@ +

    <%= _('DSpace Collections') %>

    + +<%= hidden_field_tag 'article[parent_id]', @article.parent_id %> + +<%= hidden_field_tag 'article[dspace_community_id]', @article.parent.dspace_community_id %> + +<% dspace_server_url = @article.parent.parent.dspace_server_url %> + +<% community_id = @article.parent.dspace_community_id %> + +<% collections = Dspace::Community.get_all_collections_from( dspace_server_url, community_id ).map { |collection| item = [_(collection.name), collection.id] } %> + + + + + + + + <% dspace_collections_ids = DspacePlugin::Collection.find(:all, :conditions => { :parent_id => @article.parent_id}).map { |collection| ids = collection.dspace_collection_id.to_i } %> + + <% collections.each do |collection| %> + + + <% if dspace_collections_ids.include? collection[1] %> + + <% else %> + + <% end %> + + + + <% end %> + +
     <%= _('Collection name') %>
    <%= check_box_tag "article[dspace_collections_ids][]", collection[1], true, :disabled => 'disabled', :onclick => "selectCollection(this,'#{collection[0]}')" %> <%= check_box_tag "article[dspace_collections_ids][]", collection[1], false, :onclick => "selectCollection(this,'#{collection[0]}')" %> + <%= collection[0] %> +
    + +<%= javascript_include_tag 'plugins/dspace/javascripts/dspace_plugin' %> diff --git a/plugins/dspace/views/dspace_plugin_myprofile/dspace_plugin/_communityy.html.erb b/plugins/dspace/views/dspace_plugin_myprofile/dspace_plugin/_communityy.html.erb new file mode 100644 index 0000000..ef3ecd8 --- /dev/null +++ b/plugins/dspace/views/dspace_plugin_myprofile/dspace_plugin/_communityy.html.erb @@ -0,0 +1,34 @@ +

    <%= _('DSpace Communities') %>

    + +<%= hidden_field_tag 'article[parent_id]', @article.parent_id %> + +<% dspace_server_url = @article.parent.dspace_server_url %> + +<% communities = Dspace::Community.get_all_communities_from(dspace_server_url).map { |community| item = [_(community.name), community.id] } %> + + + + + + + + <% dspace_communities_ids = DspacePlugin::Communityy.find(:all, :conditions => { :parent_id => @article.parent_id }).map { |community| ids = community.dspace_community_id.to_i } %> + + <% communities.each do |community| %> + + + <% if dspace_communities_ids.include? community[1] %> + + <% else %> + + <% end %> + + + + <% end %> + +
     <%= _('Community name') %>
    <%= check_box_tag "article[dspace_communities_ids][]", community[1], true, :disabled => 'disabled', :onclick => "selectCommunity(this,'#{community[0]}')" %> <%= check_box_tag "article[dspace_communities_ids][]", community[1], false, :onclick => "selectCommunity(this,'#{community[0]}')" %> + <%= community[0] %> +
    + +<%= javascript_include_tag 'plugins/dspace/javascripts/dspace_plugin' %> diff --git a/plugins/dspace/views/dspace_plugin_myprofile/edit.html.erb b/plugins/dspace/views/dspace_plugin_myprofile/edit.html.erb new file mode 100644 index 0000000..757a94f --- /dev/null +++ b/plugins/dspace/views/dspace_plugin_myprofile/edit.html.erb @@ -0,0 +1,42 @@ +<%= error_messages_for 'article' %> + +
    '> + +<%= labelled_form_for 'article', :url => {action: "new_dspace_content"}, :html => { :multipart => true, :class => @type } do |f| %> + + <%= hidden_field_tag("type", @type) if @type %> + + <%= hidden_field_tag('parent_id', @parent_id) if @parent_id %> + + <%= hidden_field_tag('back_to', @back_to) %> + + <%= hidden_field_tag('success_back_to', @success_back_to) %> + + <%= render :partial => partial_for_class(@article.class), :locals => { :f => f } %> + + <% button_bar do %> + <%= submit_button :save, _('Save') %> + + <% if @back_to %> + <%= button :cancel, _('Cancel'), @back_to %> + <% elsif @parent_id || @article.parent %> + <%= button :cancel, _('Cancel'), :action => 'view', :id => @parent_id || @article.parent %> + <% else %> + <%= button :cancel, _('Cancel'), :action => 'index' %> + <% end %> + + <% unless @article.new_record? %> + <%= button :delete, _('Delete'), {:controller => :cms, :action => :destroy, :id => @article}, + :method => :post, :confirm => delete_article_message(@article) %> + <% end %> + <% end %> +<% end %> +
    + +<% if environment.enabled?('media_panel') && [TinyMceArticle, TextileArticle, Event, EnterpriseHomepage].any?{|klass| @article.kind_of?(klass)} %> + <%= render :partial => 'text_editor_sidebar' %> +<% end %> + +
    + +<%= javascript_include_tag "article.js" %> -- libgit2 0.21.2