-
Notifications
You must be signed in to change notification settings - Fork 2
/
crm_google_calendar_model_hooks.rb
111 lines (93 loc) · 3.51 KB
/
crm_google_calendar_model_hooks.rb
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
class CrmGoogleCalendarModelHooks < FatFreeCRM::Callback::Base
Task.class_eval do
after_create :create_gcalendar
after_update :update_gcalendar
after_destroy :destroy_gcalendar
require 'gcal4ruby'
#----------------------------------------------------------------------------
def create_gcalendar
if cal = get_calendar
if bucket == "specific_time"
event = GCal4Ruby::Event.new(cal)
if assigned_to
attendee = User.find(assigned_to)
event.attendees = [{ :name => attendee.full_name, :email => attendee.email }]
end
event.title = get_title
event.start = get_event_start
event.end = get_event_end
# TODO: Put the uri of the task: event.where = request.request_uri
event.reminder = { :minutes => "15", :method => 'email' }
event.save
end
end
end
#----------------------------------------------------------------------------
def update_gcalendar
if cal = get_calendar
if bucket == "specific_time"
title = get_title
# Search for the event
event = GCal4Ruby::Event.find(cal, title, {:scope => :first})
unless event.blank?
if assigned_to
attendee = User.find(assigned_to)
event.attendees = [{ :name => attendee.full_name, :email => attendee.email }]
end
event.title = title
event.start = get_event_start
event.end = get_event_end
# TODO: Put the uri of the task: event.where = request.request_uri
event.reminder = { :minutes => "15", :method => 'email' }
event.save
else
create_gcalendar
end
end
end
end
#----------------------------------------------------------------------------
def destroy_gcalendar
if cal = get_calendar
if bucket == "specific_time"
# Search for the event
event = GCal4Ruby::Event.find(cal, get_title, {:scope => :first})
unless event.blank?
event.delete
end
end
end
end
#----------------------------------------------------------------------------
def get_calendar
current_user = User.find(user_id)
service = GCal4Ruby::Service.new
begin
service.authenticate(current_user.pref[:google_account], current_user.pref[:google_password])
rescue GCal4Ruby::HTTPPostFailed => ex
return false
end
service.calendars.first
end
#----------------------------------------------------------------------------
def get_title
if asset_id.blank?
"crm - #{get_category} - #{name}"
else
"crm - #{get_category} - #{name} (#{asset_type}: #{asset_type == "Contact" ? asset.full_name : asset.name})"
end
end
#----------------------------------------------------------------------------
def get_category
category == "" ? "other" : category
end
#----------------------------------------------------------------------------
def get_event_start
Setting.task_calendar_with_time == true ? due_at : due_at + 28800
end
#----------------------------------------------------------------------------
def get_event_end
Setting.task_calendar_with_time == true ? due_at : due_at + 32400
end
end
end