Wednesday, 20 December 2023

Set time zone with timezone

 1. active admin side:

ActiveAdmin.register BxBlockEvents::Event, as: 'Event' do

  Formtastic::FormBuilder.perform_browser_validations = true


  permit_params :title, :is_active, :time, :date, :latitude, :longitude, :assign_to, :email_account_id, :notify, :repeat, :notes, :visibility, :event_type, :address, :custom_repeat_in_number, :custom_repeat_every, :assignee_email, :visible_email, :format, :description, :start_date, :end_date, :timezone, :cvent_id, :status, :cvent_response_code, :event_creation_id, :is_feature_event, :venue_id, :is_booking_start, planner_attributes: [:id, :prefix, :first_name, :last_name, :company, :title, :email, :_destroy, addresses_attributes: [:id, :region_code, :address1, :address2, :address3, :city, :country_code, :postal_code, :latitude, :longitude, :address_type, :_destroy]]



  index do

    selectable_column

    id_column

    column :title

    toggle_bool_column :is_active

    column :cvent_id

    column :status

    column :event_creation_id

    toggle_bool_column :is_booking_start

    column "" do |resource|

      links = ''.html_safe

      links += link_to I18n.t('active_admin.view'), resource_path(resource), class: "member_link_event show_link"

      if resource.status == "Created"

        links += link_to I18n.t('active_admin.edit'), edit_resource_path(resource), class: "member_link_event edit_link"

      end

      links += link_to I18n.t('active_admin.delete'), resource_path(resource), method: :delete, 'data-confirm' => I18n.t('active_admin.delete_confirmation'), class: "member_link_event delete_link"

      links

    end

  end


  show do

    attributes_table do

      row:title

      row:time

      row:date

      row:latitude

      row:longitude

      row:assign_to

      row:email_account_id

      row:notify

      row:repeat

      row:notes

      row:visibility

      row:created_at

      row:updated_at

      row:event_type

      row:address

      row:custom_repeat_in_number

      row:custom_repeat_every

      row:assignee_email

      row:visible_email

      row:format

      row:description

      row:start_date

      row:end_date

      row:timezone

      row:cvent_id

      row:status

      row:cvent_response_code

      row:event_creation_id

      row:is_active

      row:is_feature_event

      row:is_booking_start

    end

    panel 'Sessions' do

      table_for event.event_sessions do

        column :id do |object|

            link_to object&.id, "/admin/event_sessions/#{object&.id}"

          end

        column :title

        column :status

      end

    end

  end

  controller do

    helper_method :fetch_speakers_for_select

    around_action :set_time_zone

    def set_time_zone

      if (params[:action] == "create") || (params[:action] == "update")

        Time.use_zone(params[:event][:timezone]) { yield }

      elsif params[:action] == "edit" || params[:action] == "show"

        Time.use_zone(resource.timezone) { yield }

      else

        yield

      end

    end

    def fetch_speakers_for_select(event)

      accepted_speaker_ids = event.user_events.where(status: 'accept').pluck(:account_id)

      AccountBlock::Account.speakers.map do |speaker|

        speaker_name = "#{speaker.first_name} #{speaker.last_name}"

        disabled = accepted_speaker_ids.include?(speaker.id)

        [speaker_name, speaker.id, { disabled: disabled }]

      end

    end

    def create

      super

      user_event = params[:event][:speaker_account_ids].reject { |e| e.nil? || e&.empty? }

      if user_event.present?

        user_event.each do |user|

          invitation = BxBlockEvents::UserEvent.create(account_id: user.to_i, event_id: resource.id)

          headings = resource&.title.to_s

          content = resource&.title.to_s

          message = "<p>We inform you that you have been invited to the event " + "<b>#{resource&.title.to_s}</b>" + " which we hope will be of interest to you.</p>"

          app_url = "event_invite"

          module_type = "Event"

          BxBlockNotifications::NotificationCreator.new(user.to_i, headings, content, app_url, resource.id, module_type, message, invitation.id).call if invitation&.id.present?

        end

      end

      if params[:event][:images].present?

        resource.event_images.attach(params[:event][:images])

      end

    end


    def update

      super do |success, failure|

        if success.present? && (!params[:event][:is_active].to_s.present? && !params[:event][:is_booking_start].to_s.present? )

          new_or_exit_ids = params[:event][:speaker_account_ids].reject { |e| e.nil? || e&.empty? } rescue []

          already_speaker_ids = resource.user_events.where.not(status: "accept").map(&:account_id).map{|sp| sp.to_s}

          remove_speaker_ids = already_speaker_ids - new_or_exit_ids

          new_speaker_ids = new_or_exit_ids - already_speaker_ids

          delete_invitation(resource, remove_speaker_ids)

          new_speaker_ids.each do |user|

            invitation = BxBlockEvents::UserEvent.create(account_id: user.to_i, event_id: resource.id)

            headings = resource&.title.to_s

            content = resource&.title.to_s

            app_url = "event_invite"

            message = "<p>We inform you that you have been invited to the event " + "<b>#{resource&.title.to_s}</b>" + " which we hope will be of interest to you.</p>"

            module_type = "Event"

            BxBlockNotifications::NotificationCreator.new(user.to_i, headings, content, app_url, resource.id, module_type, message, invitation.id).call if invitation&.id.present?

          end

          if params[:event][:images].present?

            resource.event_images.destroy_all

            resource.event_images.attach(params[:event][:images])

          end

        end

      end

    end

    private

    def delete_invitation(resource, remove_speaker_ids)

      resource.user_events.where(account_id: remove_speaker_ids).destroy_all

    end

  end


  form do |f|

    f.inputs do

      # f.semantic_errors *f.object.errors.keys

      f.semantic_errors :cvent_id

      f.input :title

      # f.input :email_account_id, label: 'Account', as: :select, collection: AccountBlock::Account.all.map{|e| ["#{e.first_name} #{e.last_name}",e.id]}, include_blank: false, :input_html => {:width => 'auto'}


      f.input :speaker_account_ids, label: 'Add speakers', as: :select, collection: fetch_speakers_for_select(f.object), selected: f.object.user_events.map(&:account_id), include_blank: false, :input_html => {:width => 'auto', multiple: true}


      f.input :format, include_blank: false

      if f.object.new_record?

        f.input :start_date, as: :date_time_picker, :input_html => {:width => 'auto'}

        f.input :end_date, as: :date_time_picker, :input_html => {:width => 'auto'}

      else

        f.input :start_date, as: :date_time_picker, :input_html => {:width => 'auto', :value => f.object.start_date.in_time_zone(f.object.timezone)}

        f.input :end_date, as: :date_time_picker, :input_html => {:width => 'auto', :value => f.object.end_date.in_time_zone(f.object.timezone)}

      end

      f.input :timezone, :as => :select, :collection => ([["Asia/Calcutta", "Asia/Calcutta"], ["Asia/Dubai", "Asia/Dubai"], ["Europe/London", "Europe/London"], ["Europe/Paris", "Europe/Paris"], ["Australia/Darwin", "Australia/Darwin"], ["Australia/Adelaide", "Australia/Adelaide"], ["America/Mexico_City", "America/Mexico_City"], ["America/New_York", "America/New_York"]]), include_blank: false

      f.input :images, label: 'Add image', as: :file

      f.object.event_images.each do |img|

        span do

          image_tag(img,height: '80', width: '100') rescue nil

        end

      end

      # f.input :time, :as => :time_picker

      # f.input :date

      # f.input :latitude

      # f.input :longitude

      # f.input :assign_to

      # f.input :notify

      # f.input :repeat

      # f.input :notes

      f.input :visibility

      f.input :event_type, :as => :select, :collection => ([["Celebration", "Celebration"], ["Conference", "Conference"], ["Dinner", "Dinner"], ["Forum", "Forum"], ["FundraiserBenefit", "FundraiserBenefit"], ["Holiday", "Holiday"], ["IncentiveTrip", "IncentiveTrip"], ["Meeting", "Meeting"], ["OtherGeneral", "OtherGeneral"], ["PoliticalEvent", "PoliticalEvent"], ["Reunion", "Reunion"], ["SaveTheDate", "SaveTheDate"], ["Seminar", "Seminar"], ["SportsEvent", "SportsEvent"], ["TradeShow", "TradeShow"], ["TrainingSession", "TrainingSession"], ["Webinar", "Webinar"]]), include_blank: false

      f.input :address

      f.input :custom_repeat_in_number

      f.input :custom_repeat_every

      f.input :assignee_email

      f.input :visible_email

      f.input :description, input_html: {required: true}

      f.input :is_feature_event

    end

    f.inputs 'Venues' do

      f.input :venue_id, as: :select, collection: BxBlockEvents::Venue.all.map{|v| ["#{v.name}", v.id]}, :input_html => {}, include_blank: false

    end


    f.inputs 'Planners' do

      f.inputs :for => [:planner, f.object.planner || BxBlockEvents::Planner.new] do |planner|

        planner.input :prefix

        planner.input :first_name

        planner.input :last_name

        planner.input :company

        planner.input :title

        planner.input :email

      end

    end


    f.inputs 'Planner addresses' do

      f.inputs :for => ['home_address', (resource&.planner&.addresses.where(address_type: "home_address").last rescue nil || BxBlockEvents::Address.new)] do |pha|

        f.inputs 'Home address' do

          pha_id = resource&.planner&.addresses.where(address_type: "home_address").last.present? ? resource&.planner&.addresses.where(address_type: "home_address").last&.id : nil rescue nil

          pha.input :id, input_html: {:value => pha_id, name: "event[planner_attributes][addresses_attributes][0][id]"}, as: :hidden

          pha.input :region_code, input_html: {required: true, name: "event[planner_attributes][addresses_attributes][0][region_code]", maxlength: 9}

          pha.input :address1, input_html: {required: true, name: "event[planner_attributes][addresses_attributes][0][address1]", maxlength: 35}

          pha.input :address2, input_html: {required: true, name: "event[planner_attributes][addresses_attributes][0][address2]", maxlength: 35}

          pha.input :address3, input_html: {required: true, name: "event[planner_attributes][addresses_attributes][0][address3]", maxlength: 35}

          pha.input :city, input_html: {required: true, name: "event[planner_attributes][addresses_attributes][0][city]", maxlength: 30}

          pha.input :country_code, :as => :select, :collection => (ISO3166::Country.codes.map{|code| code}), include_blank: false, input_html: {name: "event[planner_attributes][addresses_attributes][0][country_code]", maxlength: 3}

          pha.input :postal_code, input_html: {required: true, name: "event[planner_attributes][addresses_attributes][0][postal_code]", maxlength: 24}

          pha.input :address_type, :input_html => { :value => "home_address", name: "event[planner_attributes][addresses_attributes][0][address_type]" }, as: :hidden

        end

      end

      f.inputs :for => ['work_address', (resource&.planner&.addresses.where(address_type: "work_address").last rescue nil || BxBlockEvents::Address.new)] do |pwa|

        f.inputs 'Work address' do

          pwa_id = resource&.planner&.addresses.where(address_type: "work_address").last.present? ? resource&.planner&.addresses.where(address_type: "work_address").last&.id : nil rescue nil

          pwa.input :id, input_html: {:value => pwa_id, name: "event[planner_attributes][addresses_attributes][1][id]"}, as: :hidden

          pwa.input :region_code, input_html: {required: true, name: "event[planner_attributes][addresses_attributes][1][region_code]", maxlength: 9}

          pwa.input :address1, input_html: {required: true, name: "event[planner_attributes][addresses_attributes][1][address1]", maxlength: 35}

          pwa.input :address2, input_html: {required: true, name: "event[planner_attributes][addresses_attributes][1][address2]", maxlength: 35}

          pwa.input :address3, input_html: {required: true, name: "event[planner_attributes][addresses_attributes][1][address3]", maxlength: 35}

          pwa.input :city, input_html: {required: true, name: "event[planner_attributes][addresses_attributes][1][city]", maxlength: 30}

          pwa.input :country_code, :as => :select, :collection => (ISO3166::Country.codes.map{|code| code}), include_blank: false, input_html: {name: "event[planner_attributes][addresses_attributes][1][country_code]", maxlength: 3}

          pwa.input :postal_code, input_html: {required: true, name: "event[planner_attributes][addresses_attributes][1][postal_code]", maxlength: 24}

          pwa.input :address_type, :input_html => { :value => "work_address", name: "event[planner_attributes][addresses_attributes][1][address_type]" }, as: :hidden

        end

      end

    end

    

    f.actions

  end  

end

----------------------------------------------------------------------------

2. Normal controller OR view

Add this line application controller OR and where necessary :
around_action :set_time_zone
def set_time_zone
    if session[:timezone].present?
      Time.use_zone('New Delhi') { yield }
    else
      yield
    end    
  end

No comments:

Post a Comment