Commit 2568a09e9886d2b5912b9412cd5cda1ef54a9048

Authored by Tallys Martins
1 parent b06e2399

Adds profile fields block

Signed-off-by: Tallys Martins <tallysmartins@gmail.com>
plugins/profile_fields_block/lib/profile_fields_block.rb 0 → 100644
... ... @@ -0,0 +1,90 @@
  1 +class ProfileFieldsBlockPlugin::ProfileFieldsBlock < Block
  2 +
  3 + settings_items :fields, type: Array, :default => []
  4 + settings_items :custom_fields, type: Hash, :default => {}
  5 + settings_items :link_fields, type: Hash, :default => {}
  6 +
  7 + attr_accessible :fields
  8 + attr_accessible :custom_fields
  9 +
  10 + def self.description
  11 + _('Display profile fields on a block')
  12 + end
  13 +
  14 + def self.short_description
  15 + _('Show one or more fields')
  16 + end
  17 +
  18 + def self.pretty_name
  19 + _('Profile Fields')
  20 + end
  21 +
  22 + def help
  23 + _('This blocks displays profile fields. You can display them as link or plain text')
  24 + end
  25 +
  26 + def update_link_fields(custom_fields)
  27 + self.link_fields = {}
  28 + CustomField.all.map{ |cf| self.link_fields[cf.name.to_sym] = (cf.format == "link") }
  29 + self.save!
  30 + end
  31 +
  32 + def available_fields
  33 + return [] if self.owner.nil?
  34 + regroupe_fields(self.owner.active_fields)
  35 + end
  36 +
  37 + def available_custom_fields
  38 + return [] if self.owner.nil?
  39 + custom_fields = CustomField.where(customized_type: self.owner.type, active: true).collect { |f| [f.name, f.id] }
  40 + update_link_fields(custom_fields)
  41 + regroupe_fields(custom_fields)
  42 + end
  43 +
  44 + def visible_fields_for(current_user)
  45 + fields = get_profile_fields
  46 + fields.each do |field, value|
  47 + fields.delete(field) unless self.owner.can_view_field?(current_user, field.to_s)
  48 + end
  49 + fields
  50 + end
  51 +
  52 + def has_field?(field)
  53 + (self.fields.include?(field) || (self.custom_fields.has_key?(field.to_sym) &&
  54 + checked_field?(self.custom_fields[field.to_sym])))
  55 + end
  56 +
  57 + def cacheable?
  58 + true
  59 + end
  60 +
  61 + private
  62 +
  63 + #when fields checkbox are unchecked, a hidden field with value =0 is given
  64 + def checked_field? value
  65 + return value != "0"
  66 + end
  67 +
  68 + def get_profile_fields
  69 + hash = {}
  70 + self.fields.each do |f|
  71 + hash[f] = self.owner.send(f.to_sym) if checked_field? f
  72 + end
  73 + hash.merge!(get_profile_custom_fields)
  74 + end
  75 +
  76 + def get_profile_custom_fields
  77 + fields_keys = self.custom_fields.reject{ |k,v| !checked_field? v}.keys
  78 + hash = self.owner.custom_values_hash.slice *fields_keys
  79 + end
  80 +
  81 + #group fields in 3 groups
  82 + def regroupe_fields(fields_list)
  83 + res = []
  84 + offset = (fields_list.size/3.0).ceil
  85 + 3.times do
  86 + res.append(fields_list.shift(offset))
  87 + end
  88 + res.sort
  89 + end
  90 +end
... ...
plugins/profile_fields_block/lib/profile_fields_block_plugin.rb 0 → 100644
... ... @@ -0,0 +1,27 @@
  1 +class ProfileFieldsBlockPlugin < Noosfero::Plugin
  2 +
  3 + def self.plugin_name
  4 + # FIXME
  5 + "ProfileFieldsBlockPlugin"
  6 + end
  7 +
  8 + def self.extra_blocks
  9 + {
  10 + ProfileFieldsBlock => { :type => [Community, Person, Enterprise] }
  11 + }
  12 + end
  13 +
  14 + def self.plugin_description
  15 + # FIXME
  16 + _("A plugin that adds a block to display profile fields information")
  17 + end
  18 +
  19 + def stylesheet?
  20 + true
  21 + end
  22 +
  23 + def self.api_mount_points
  24 + [ProfileFieldsBlockPlugin::API]
  25 + end
  26 +
  27 +end
... ...
plugins/profile_fields_block/lib/profile_fields_block_plugin/api.rb 0 → 100644
... ... @@ -0,0 +1,16 @@
  1 +require_relative 'api_entities'
  2 +
  3 +class ProfileFieldsBlockPlugin::API < Grape::API
  4 +
  5 + include Api::Helpers
  6 +
  7 + resource :profile_fields_block_plugin do
  8 +
  9 + get 'profile_fields_blocks/:id' do
  10 + block = ProfileFieldsBlockPlugin::ProfileFieldsBlock.find_by(id: params[:id])
  11 + not_found! unless block
  12 + present block.visibe_fields_for(current_user), with: Grape::Presenters::Presenter
  13 + end
  14 +
  15 + end
  16 +end
... ...
plugins/profile_fields_block/lib/profile_fields_block_plugin/api_entities.rb 0 → 100644
plugins/profile_fields_block/po/custom_fields_block.pot 0 → 100644
... ... @@ -0,0 +1,22 @@
  1 +# SOME DESCRIPTIVE TITLE.
  2 +# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
  3 +# This file is distributed under the same license as the PACKAGE package.
  4 +# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
  5 +#
  6 +#, fuzzy
  7 +msgid ""
  8 +msgstr ""
  9 +"Project-Id-Version: 1.3~rc2-1-ga15645d\n"
  10 +"POT-Creation-Date: 2015-10-30 16:35-0300\n"
  11 +"PO-Revision-Date: 2015-08-06 17:21-0300\n"
  12 +"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
  13 +"Language-Team: LANGUAGE <LL@li.org>\n"
  14 +"Language: \n"
  15 +"MIME-Version: 1.0\n"
  16 +"Content-Type: text/plain; charset=UTF-8\n"
  17 +"Content-Transfer-Encoding: 8bit\n"
  18 +"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n"
  19 +
  20 +#: plugins/template/lib/template_plugin.rb:10
  21 +msgid "A plugin that does this and that."
  22 +msgstr ""
... ...
plugins/profile_fields_block/public/style.css 0 → 100644
plugins/profile_fields_block/views/blocks/profile_fields.html.erb 0 → 100644
... ... @@ -0,0 +1,10 @@
  1 +<% extend CustomFieldsHelper %>
  2 +
  3 +<%= block_title(block.title, block.subtitle) %>
  4 +<% block.visible_fields_for(user).each do |key, value| %>
  5 + <% if block.link_fields[key.to_sym] %>
  6 + <%= value_for_format("link", value) %>
  7 + <% else %>
  8 + <%= value %>
  9 + <% end %>
  10 +<% end %>
... ...
plugins/profile_fields_block/views/box_organizer/_profile_fields_block.html.erb 0 → 100644
... ... @@ -0,0 +1,23 @@
  1 +<div class="profile-fields">
  2 + <div class="fields">
  3 + <span><p>Fields</p></span>
  4 + <% @block.available_fields.each_with_index do |fields, index| %>
  5 + <div class="fields-list-<%= index %>" >
  6 + <% fields.each do |field| %>
  7 + <%= labelled_check_box(_('%s') % field, 'block[fields][]', field, @block.has_field?(field)) %>
  8 + <% end %>
  9 + </div>
  10 + <% end %>
  11 + </div>
  12 +
  13 + <div class="custom-fields">
  14 + <span><p>Custom Fields</p></span>
  15 + <% @block.available_custom_fields.each_with_index do |fields, index| %>
  16 + <div class="fields-list-<%= index %>" >
  17 + <% fields.each do |field, key| %>
  18 + <%= labelled_check_box(_('%s') % field, "block[custom_fields][#{field}]", key, @block.has_field?(field)) %>
  19 + <% end %>
  20 + </div>
  21 + <% end %>
  22 + </div>
  23 +</div>
... ...