diff --git a/app/controllers/ical_tickets_controller.rb b/app/controllers/ical_tickets_controller.rb index 38f206428..bd6cdff10 100644 --- a/app/controllers/ical_tickets_controller.rb +++ b/app/controllers/ical_tickets_controller.rb @@ -12,13 +12,20 @@ class IcalTicketsController < ApplicationController # @response_message 200 [String] iCal file ready to import in calendar applications. # @response_message 401 Permission denied. def all - new_open_events_data = new_open_events_data_get - pending_events_data = pending_events_data_get - escalation_events_data = escalation_events_data_get + new_open_events_data = ICal::Ticket.new_open(current_user) + pending_events_data = ICal::Ticket.pending(current_user) + escalation_events_data = ICal::Ticket.escalation(current_user) events_data = new_open_events_data + pending_events_data + escalation_events_data - events_data_to_ical( events_data ) + ical = ICal.to_ical( events_data ) + + send_data( + ical, + filename: 'zammad_tickets.ical', + type: 'text/plain', + disposition: 'inline' + ) end # @path [GET] /ical/tickets_new_open @@ -28,9 +35,16 @@ class IcalTicketsController < ApplicationController # @response_message 200 [String] iCal file ready to import in calendar applications. # @response_message 401 Permission denied. def new_open - events_data = new_open_events_data_get + events_data = ICal::Ticket.new_open(current_user) - events_data_to_ical( events_data ) + ical = ICal.to_ical( events_data ) + + send_data( + ical, + filename: 'zammad_tickets_new_open.ical', + type: 'text/plain', + disposition: 'inline' + ) end # @path [GET] /ical/tickets_pending @@ -40,9 +54,16 @@ class IcalTicketsController < ApplicationController # @response_message 200 [String] iCal file ready to import in calendar applications. # @response_message 401 Permission denied. def pending - events_data = pending_events_data_get + events_data = ICal::Ticket.pending(current_user) - events_data_to_ical( events_data ) + ical = ICal.to_ical( events_data ) + + send_data( + ical, + filename: 'zammad_tickets_pending.ical', + type: 'text/plain', + disposition: 'inline' + ) end # @path [GET] /ical/ticket_escalation @@ -52,131 +73,13 @@ class IcalTicketsController < ApplicationController # @response_message 200 [String] iCal file ready to import in calendar applications. # @response_message 401 Permission denied. def escalation - events_data = escalation_events_data_get + events_data = ICal::Ticket.escalation(current_user) - events_data_to_ical( events_data ) - end - - private - - def new_open_events_data_get - - condition = { - 'tickets.owner_id' => current_user.id, - 'tickets.state_id' => Ticket::State.where( - state_type_id: Ticket::StateType.where( - name: %w(new open), - ), - ), - } - - tickets = Ticket.search( - current_user: current_user, - condition: condition, - ) - - events_data = [] - tickets.each do |ticket| - - event_data = {} - - event_data[:dtstart] = Icalendar::Values::Date.new( Time.zone.today ) - event_data[:dtend] = Icalendar::Values::Date.new( Time.zone.today ) - event_data[:summary] = "#{ ticket.state.name } ticket: '#{ ticket.title }'" - event_data[:description] = "T##{ ticket.number }" - - events_data.push event_data - end - - events_data - end - - def pending_events_data_get - - condition = { - 'tickets.owner_id' => current_user.id, - 'tickets.state_id' => Ticket::State.where( - state_type_id: Ticket::StateType.where( - name: [ - 'pending reminder', - 'pending action', - ], - ), - ), - } - - tickets = Ticket.search( - current_user: current_user, - condition: condition, - ) - - events_data = [] - tickets.each do |ticket| - - event_data = {} - - # rubocop:disable Rails/TimeZone - event_data[:dtstart] = Icalendar::Values::DateTime.new( ticket.pending_time ) - event_data[:dtend] = Icalendar::Values::DateTime.new( ticket.pending_time ) - # rubocop:enable Rails/TimeZone - event_data[:summary] = "#{ ticket.state.name } ticket: '#{ ticket.title }'" - event_data[:description] = "T##{ ticket.number }" - - events_data.push event_data - end - - events_data - end - - def escalation_events_data_get - - condition = [ - 'tickets.escalation_time IS NOT NULL', - 'tickets.owner_id = ?', current_user.id - ] - - tickets = Ticket.search( - current_user: current_user, - condition: condition, - ) - - events_data = [] - tickets.each do |ticket| - - event_data = {} - - # rubocop:disable Rails/TimeZone - event_data[:dtstart] = Icalendar::Values::DateTime.new( ticket.escalation_time ) - event_data[:dtend] = Icalendar::Values::DateTime.new( ticket.escalation_time ) - # rubocop:enable Rails/TimeZone - event_data[:summary] = "ticket escalation: '#{ ticket.title }'" - event_data[:description] = "T##{ ticket.number }" - - events_data.push event_data - end - - events_data - end - - def events_data_to_ical(events_data) - - cal = Icalendar::Calendar.new - - events_data.each do |event_data| - - cal.event do |e| - e.dtstart = event_data[:dtstart] - e.dtend = event_data[:dtend] - e.summary = event_data[:summary] - e.description = event_data[:description] - e.ip_class = 'PRIVATE' - end - - end + ical = ICal.to_ical( events_data ) send_data( - cal.to_ical, - filename: 'zammad.ical', + ical, + filename: 'zammad_tickets_escalation.ical', type: 'text/plain', disposition: 'inline' ) diff --git a/lib/i_cal.rb b/lib/i_cal.rb new file mode 100644 index 000000000..7f8be5683 --- /dev/null +++ b/lib/i_cal.rb @@ -0,0 +1,24 @@ +# Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/ + +module ICal + + def self.to_ical(events_data) + + cal = Icalendar::Calendar.new + + events_data.each do |event_data| + + cal.event do |e| + e.dtstart = event_data[:dtstart] + e.dtend = event_data[:dtend] + e.summary = event_data[:summary] + e.description = event_data[:description] + e.ip_class = 'PRIVATE' + end + + end + + cal.to_ical + end + +end diff --git a/lib/i_cal/ticket.rb b/lib/i_cal/ticket.rb new file mode 100644 index 000000000..f58537177 --- /dev/null +++ b/lib/i_cal/ticket.rb @@ -0,0 +1,104 @@ +# Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/ + +module ICal::Ticket + + def self.new_open(user) + + condition = { + 'tickets.owner_id' => user.id, + 'tickets.state_id' => Ticket::State.where( + state_type_id: Ticket::StateType.where( + name: %w(new open), + ), + ), + } + + tickets = Ticket.search( + current_user: user, + condition: condition, + ) + + events_data = [] + tickets.each do |ticket| + + event_data = {} + + event_data[:dtstart] = Icalendar::Values::Date.new( Time.zone.today ) + event_data[:dtend] = Icalendar::Values::Date.new( Time.zone.today ) + event_data[:summary] = "#{ticket.state.name} ticket: '#{ticket.title}'" + event_data[:description] = "T##{ticket.number}" + + events_data.push event_data + end + + events_data + end + + def self.pending(user) + + condition = { + 'tickets.owner_id' => user.id, + 'tickets.state_id' => Ticket::State.where( + state_type_id: Ticket::StateType.where( + name: [ + 'pending reminder', + 'pending action', + ], + ), + ), + } + + tickets = Ticket.search( + current_user: user, + condition: condition, + ) + + events_data = [] + tickets.each do |ticket| + + event_data = {} + + # rubocop:disable Rails/TimeZone + event_data[:dtstart] = Icalendar::Values::DateTime.new( ticket.pending_time ) + event_data[:dtend] = Icalendar::Values::DateTime.new( ticket.pending_time ) + # rubocop:enable Rails/TimeZone + event_data[:summary] = "#{ticket.state.name} ticket: '#{ticket.title}'" + event_data[:description] = "T##{ticket.number}" + + events_data.push event_data + end + + events_data + end + + def self.escalation(user) + + condition = [ + 'tickets.escalation_time IS NOT NULL', + 'tickets.owner_id = ?', user.id + ] + + tickets = Ticket.search( + current_user: user, + condition: condition, + ) + + events_data = [] + tickets.each do |ticket| + + event_data = {} + + # rubocop:disable Rails/TimeZone + event_data[:dtstart] = Icalendar::Values::DateTime.new( ticket.escalation_time ) + event_data[:dtend] = Icalendar::Values::DateTime.new( ticket.escalation_time ) + # rubocop:enable Rails/TimeZone + event_data[:summary] = "ticket escalation: '#{ticket.title}'" + event_data[:description] = "T##{ticket.number}" + + events_data.push event_data + end + + events_data + end + +end