mpog_software_plugin.rb 11.7 KB
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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
class MpogSoftwarePlugin < Noosfero::Plugin
  include ActionView::Helpers::TagHelper
  include ActionView::Helpers::FormTagHelper
  include ActionView::Helpers::FormOptionsHelper
  include ActionView::Helpers::JavaScriptHelper
  include ActionView::Helpers::AssetTagHelper
  include FormsHelper
  include ActionView::Helpers
  include ActionDispatch::Routing
  include Rails.application.routes.url_helpers

  def self.plugin_name
    'MpogSoftwarePlugin'
  end

  def self.plugin_description
    _('Add Public Software and MPOG features.')
  end

  def profile_editor_extras
    profile = context.profile

    if profile.person?
      expanded_template('person_editor_extras.html.erb')
    elsif profile.respond_to?(:software_info) &&
      !profile.software_info.nil?

      if profile.software_info.first_edit?
        profile.software_info.first_edit = false
        profile.software_info.save!
        expanded_template('first_edit_software_community_extras.html.erb')
      end
    end
  end

  def profile_editor_transaction_extras
    single_hash_transactions = { :user => 'user',
                                 :software_info => 'software_info',
                                 :version => 'license', :language => 'generic_model',
                                 :operating_system => 'generic_model',
                                 :software_categories => 'software_categories',
                                 :instituton => 'instituton',
                                 :library => 'generic_model',
                                 :database => 'generic_model' }

    single_hash_transactions.each do |model, transaction|
      call_model_transaction(model, transaction)
    end
  end

  def profile_editor_controller_filters
    block = proc do
      if request.post? && params[:institution]
        is_admin = environment.admins.include?(current_user.person)

        unless is_admin
          institution = profile.user.institutions

          if !params[:institution].blank? && !params[:institution][:sisp].nil?
            if params[:institution][:sisp] != institution.sisp
              params[:institution][:sisp] = institution.sisp
            end
          end
        end
      end
    end

    [{
      :type => 'before_filter',
      :method_name => 'validate_institution_sisp_field_access',
      :options => { :only => :edit },
      :block => block
    }]
  end

  def profile_tabs
    if context.profile.community?
      return profile_tabs_software if context.profile.software?
      profile_tabs_institution
    end
  end

  def control_panel_buttons
    if context.profile.software?
      return software_info_button
    elsif context.profile.person?
      return create_new_software_button
    elsif context.profile.institution?
      return institution_info_button
    end
  end

  def self.extra_blocks
    {
      SoftwaresBlock => { :type => [Environment, Person]  },
      SoftwareInformationBlock => {  :type => [Community]  },
      InstitutionsBlock => {  :type => [Environment, Person] },
      DownloadBlock => { :type => [Community] },
      RepositoryBlock => { :type => [Community] },
      CategoriesAndTagsBlock => { :type => [Community] },
      CategoriesSoftwareBlock => { :type => [Environment] },
      SearchCatalogBlock => { :type => [Environment] }
    }
  end

  def stylesheet?
    true
  end

  def js_files
    %w(
      jquery.maskedinput.min.js
      spb-utils.js
      mpog-software.js
      mpog-software-validations.js
      mpog-user-validations.js
      mpog-institution-validations.js
      mpog-incomplete-registration.js
      mpog-search.js
      software-catalog.js
      mpog-software-block.js
    )
  end

  def add_new_organization_buttons
    proc do
      button(
        :add,
        _('Create a new software'),
        :controller => 'mpog_software_plugin_myprofile',
        :action => 'new_software'
      )
    end
  end

  # FIXME - if in error log apears has_permission?, try to use this method
  def has_permission?(person, permission, target)
    person.has_permission_without_plugins?(permission, target)
  end

  def profile_blocks_extra_content
    return if context.session[:user].nil? ||
      !context.session[:hide_incomplete_percentage].blank?

    person = Person.where(:user_id => context.session[:user]).first
    puts "="*80,person,"="*80
    call_percentage_profile_template(person)
  end

  def custom_user_registration_attributes(user)
    return  if context.params[:user][:institution_ids].nil?
    context.params[:user][:institution_ids].delete('')

    update_user_institutions(user)

    user.institutions.each do |institution|
      community = institution.community
      community.add_member user.person
    end
  end

  def calc_percentage_registration(person)
    required_list = profile_required_list
    empty_fields = profile_required_empty_list person
    count = required_list[:person_fields].count +
            required_list[:user_fields].count
    percentege = 100 - ((empty_fields.count * 100) / count)
    person.percentage_incomplete = percentege
    person.save(validate: false)
    percentege
  end

  def admin_panel_links
    [
      {
        :title => _('Create Institution'),
        :url => {
          :controller => 'mpog_software_plugin',
          :action => 'create_institution_admin'
        }
      }
    ]
  end

  protected

  def create_url_to_edit_profile person
    new_url = person.public_profile_url
    new_url[:controller] = 'profile_editor'
    new_url[:action] = 'edit'
    new_url
  end

  def profile_required_list
    fields = {}
    fields[:person_fields] = %w(cell_phone
                                contact_phone
                                comercial_phone
                                country
                                city
                                state
                                organization_website
                                image
                                identifier
                                name)

    fields[:user_fields] = %w(secondary_email email)
    fields
  end


  def profile_required_empty_list(person)
    empty_fields = []
    required_list = profile_required_list

    required_list[:person_fields].each do |field|
      empty_fields << field.sub('_',' ') if person.send(field).blank?
    end
    required_list[:user_fields].each do |field|
      empty_fields << field.sub('_',' ') if person.user.send(field).blank?
    end
    empty_fields
  end

  def user_transaction
    user_editor_institution_actions

    User.transaction do
      context.profile.user.update_attributes!(context.params[:user])
    end
  end

  def institution_transaction
    institution.date_modification = DateTime.now
    institution.save
    institution_models = %w(governmental_power governmental_sphere
                            juridical_nature)

    institution_models.each do |model|
      call_institution_transaction(model)
    end

    if context.params.has_key?(:institution)
      Institution.transaction do
        context.profile.
          institution.
          update_attributes!(context.params[:institution])
      end
    end
  end

  def software_info_transaction
    SoftwareInfo.transaction do
      context.profile.
        software_info.
        update_attributes!(context.params[:software_info])
    end
  end

  def license_transaction
    license = LicenseInfo.find(context.params[:version])
    context.profile.software_info.license_info = license
    context.profile.software_info.save!
  end

  private

  # Add and remove the user from it's institutions communities
  def user_editor_institution_actions
    user = context.profile.user

    old_communities = []
    context.profile.user.institutions.each do |institution|
      old_communities << institution.community
    end

    new_communities = []
    unless context.params[:user][:institution_ids].nil?
      context.params[:user][:institution_ids].delete('')

      context.params[:user][:institution_ids].each do |id|
        new_communities << Institution.find(id).community
      end
    end

    manage_user_institutions(user, leave_communities, enter_communities)
  end

  def show_sisp_field
    current_person = User.find(context.session[:user]).person
    context.environment.admins.include?(current_person)
  end

  def call_model_transaction(model,name)
    send(name + '_transaction') if context.params.key?(model.to_sym)
  end

  def call_institution_transaction(model)
    context.profile.institution.send(model + '_id = ',
                                     context.params[model.to_sym])
    context.profile.institution.save!
  end

  def generic_model_transaction
    models_list = [
                    [SoftwareLanguage, SoftwareLanguageHelper, 'language'],
                    [SoftwareDatabase, DatabaseHelper, 'database'],
                    [OperatingSystem, OperatingSystemHelper, 'operating_system'],
                    [Library, LibraryHelper, 'library']
                  ]
    models_list.each do |model|
      list_of_model = 'list_'+model[2].to_s
      model[0].transaction do
        list = model[1].send(list_of_model, context.params[model[2].to_sym])

        if model[2].send('valid_'+list_of_model+'?', list_of_model)
          model[0].where(
            :software_info_id => context.profile.software_info.id
          ).destroy_all

          list.each do |model|
            model.software_info = context.profile.software_info
            model.save!
          end
        else
          raise 'Invalid Software #{model[2]} fields'
        end
      end
    end
  end

  def software_info_button
    {
      :title => _('Software Info'),
      :icon => 'edit-profile-group control-panel-software-link',
      :url => {
        :controller => 'mpog_software_plugin_myprofile',
        :action => 'edit_software'
      }
    }
  end

  def create_new_software_button
    {
      :title => _('Create a new software'),
      :icon => 'design-editor',
      :url => {
        :controller => 'mpog_software_plugin_myprofile',
        :action => 'new_software'
      }
    }
  end

  def institution_info_button
    {
      :title => _('Institution Info'),
      :icon => 'edit-profile-group control-panel-instituton-link',
      :url => {
        :controller => 'mpog_software_plugin_myprofile',
        :action => 'edit_institution'
      }
    }
  end

  def manage_user_institutions(leave_communities, enter_communities)
    leave_communities = (old_communities - new_communities)
    enter_communities = (new_communities - old_communities)

    leave_communities.each do |community|
      community.remove_member(user.person)
      user.institutions.delete(community.institution)
    end

    enter_communities.each do |community|
      community.add_member(user.person)
      user.institutions << community.institution
    end
  end

  def profile_tabs_software
    { :title => _('Software'),
      :id => 'mpog-fields',
      :content => proc do render :partial => 'software_tab' end,
      :start => true }
  end

  def profile_tabs_institution
    { :title => _('Institution'),
      :id => 'mpog-fields',
      :content => Proc::new do render :partial => 'institution_tab' end,
      :start => true
    }
  end

  def call_percentage_profile_template(person)
    if context.profile && context.profile.person? && !person.nil?
      @person = person
      @percentege = calc_percentage_registration(person)

      if @percentege >= 0 && @percentege <= 100
        expanded_template('incomplete_registration.html.erb')
      end
    end
  end

  def update_user_institutions(user)
    context.params[:user][:institution_ids].each do |institution_id|
      institution = Institution.find institution_id
      user.institutions << institution

      if institution.community.admins.blank?
        institution.community.add_admin(user.person)
      end
    end
    user.save unless user.institution_ids.empty?
  end
end