FP_eventsController   F
last analyzed

Complexity

Total Complexity 75

Size/Duplication

Total Lines 636
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 0%
Metric Value
dl 0
loc 636
ccs 0
cts 425
cp 0
rs 2.1415
wmc 75
lcom 0
cbo 5

9 Methods

Rating   Name   Duplication   Size   Complexity  
B action_markasinvited() 0 37 4
B action_markasattended() 0 37 4
B action_markasnotattended() 0 37 4
B action_markasnotinvited() 0 37 4
B action_markasaccepted() 0 38 4
B action_markasdeclined() 0 37 4
D action_add_to_list() 0 115 19
F action_sendinvitemails() 0 244 28
B sendEmail() 0 42 4

How to fix   Complexity   

Complex Class

Complex classes like FP_eventsController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use FP_eventsController, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * Date: 11/06/13
4
 * Written by: Andrew Mclaughlan
5
 * Company: SalesAgility
6
 */
7
8
class FP_eventsController extends SugarController
9
{
10
11
    public function action_markasinvited()
12
    {
13
        global $db; 
14
15
        $ids = $_POST['id'];
16
        $entire_list = $_POST['entire_list'];
17
        $event_id = $_POST['event_id'];
18
19
        if($entire_list != '1'){
20
21
            $contacts = explode(',', $ids);
22
23
            foreach($contacts as $contact){
24
                //update contacts query
25
                $query = 'UPDATE fp_events_contacts_c SET invite_status="Invited" WHERE fp_events_contactsfp_events_ida="'.$event_id.'" AND fp_events_contactscontacts_idb="'.$contact.'"';
26
                $res = $db->query($query);
27
                //update Leads query
28
                $query2 = 'UPDATE fp_events_leads_1_c SET invite_status="Invited" WHERE fp_events_leads_1fp_events_ida="'.$event_id.'" AND fp_events_leads_1leads_idb="'.$contact.'"';
29
                $res = $db->query($query2);
30
                //update targets query
31
                $query3 = 'UPDATE fp_events_prospects_1_c SET invite_status="Invited" WHERE fp_events_prospects_1fp_events_ida="'.$event_id.'" AND fp_events_prospects_1prospects_idb="'.$contact.'"';
32
                $res = $db->query($query3);      
33
            }
34
        }
35
        else if($entire_list == '1'){ //updates all records
36
37
                //update contacts query
38
                $query = 'UPDATE fp_events_contacts_c SET invite_status="Invited" WHERE fp_events_contactsfp_events_ida="'.$event_id.'"';
39
                $res = $db->query($query);
40
                //update Leads query
41
                $query2 = 'UPDATE fp_events_leads_1_c SET invite_status="Invited" WHERE fp_events_leads_1fp_events_ida="'.$event_id.'"';
42
                $res = $db->query($query2);
43
                //update targets query
44
                $query3 = 'UPDATE fp_events_prospects_1_c SET invite_status="Invited" WHERE fp_events_prospects_1fp_events_ida="'.$event_id.'"';
45
                $res = $db->query($query3);
46
        }
47
    }
48
49
    public function action_markasattended()
50
    {
51
        global $db;
52
53
        $ids = $_POST['id'];
54
        $entire_list = $_POST['entire_list'];
55
        $event_id = $_POST['event_id'];
56
57
         if($entire_list != '1'){
58
59
            $contacts = explode(',', $ids);
60
61
            foreach($contacts as $contact){
62
63
                $query = 'UPDATE fp_events_contacts_c SET invite_status="Attended" WHERE fp_events_contactsfp_events_ida="'.$event_id.'" AND fp_events_contactscontacts_idb="'.$contact.'"';
64
                $res = $db->query($query);
65
                //update Leads query
66
                $query2 = 'UPDATE fp_events_leads_1_c SET invite_status="Attended" WHERE fp_events_leads_1fp_events_ida="'.$event_id.'" AND fp_events_leads_1leads_idb="'.$contact.'"';
67
                $res = $db->query($query2);
68
                //update targets query
69
                $query3 = 'UPDATE fp_events_prospects_1_c SET invite_status="Attended" WHERE fp_events_prospects_1fp_events_ida="'.$event_id.'" AND fp_events_prospects_1prospects_idb="'.$contact.'"';
70
                $res = $db->query($query3);
71
            }
72
        }
73
        else if($entire_list == '1'){ //updates all records
74
75
                //update contacts query
76
                $query = 'UPDATE fp_events_contacts_c SET invite_status="Attended" WHERE fp_events_contactsfp_events_ida="'.$event_id.'"';
77
                $res = $db->query($query);
78
                //update Leads query
79
                $query2 = 'UPDATE fp_events_leads_1_c SET invite_status="Attended" WHERE fp_events_leads_1fp_events_ida="'.$event_id.'"';
80
                $res = $db->query($query2);
81
                //update targets query
82
                $query3 = 'UPDATE fp_events_prospects_1_c SET invite_status="Attended" WHERE fp_events_prospects_1fp_events_ida="'.$event_id.'"';
83
                $res = $db->query($query3);
84
        }
85
    }
86
87
    public function action_markasnotattended()
88
    {
89
        global $db;
90
91
        $ids = $_POST['id'];
92
        $entire_list = $_POST['entire_list'];
93
        $event_id = $_POST['event_id'];
94
95
        if($entire_list != '1'){
96
97
            $contacts = explode(',', $ids);
98
99
            foreach($contacts as $contact){
100
101
                $query = 'UPDATE fp_events_contacts_c SET invite_status="Not Attended" WHERE fp_events_contactsfp_events_ida="'.$event_id.'" AND fp_events_contactscontacts_idb="'.$contact.'"';
102
                $res = $db->query($query);
103
                //update Leads query
104
                $query2 = 'UPDATE fp_events_leads_1_c SET invite_status="Not Attended" WHERE fp_events_leads_1fp_events_ida="'.$event_id.'" AND fp_events_leads_1leads_idb="'.$contact.'"';
105
                $res = $db->query($query2);
106
                //update targets query
107
                $query3 = 'UPDATE fp_events_prospects_1_c SET invite_status="Not Attended" WHERE fp_events_prospects_1fp_events_ida="'.$event_id.'" AND fp_events_prospects_1prospects_idb="'.$contact.'"';
108
                $res = $db->query($query3);
109
            }
110
        }
111
        else if($entire_list == '1'){ //updates all records
112
113
                //update contacts query
114
                $query = 'UPDATE fp_events_contacts_c SET invite_status="Not Attended" WHERE fp_events_contactsfp_events_ida="'.$event_id.'"';
115
                $res = $db->query($query);
116
                //update Leads query
117
                $query2 = 'UPDATE fp_events_leads_1_c SET invite_status="Not Attended" WHERE fp_events_leads_1fp_events_ida="'.$event_id.'"';
118
                $res = $db->query($query2);
119
                //update targets query
120
                $query3 = 'UPDATE fp_events_prospects_1_c SET invite_status="Not Attended" WHERE fp_events_prospects_1fp_events_ida="'.$event_id.'"';
121
                $res = $db->query($query3);
122
        }
123
    }
124
125
    public function action_markasnotinvited()
126
    {
127
        global $db;
128
129
        $ids = $_POST['id'];
130
        $entire_list = $_POST['entire_list'];
131
        $event_id = $_POST['event_id'];
132
133
        if($entire_list != '1'){
134
135
            $contacts = explode(',', $ids);
136
137
            foreach($contacts as $contact){
138
139
                $query = 'UPDATE fp_events_contacts_c SET invite_status="Not Invited", email_responded="0" WHERE fp_events_contactsfp_events_ida="'.$event_id.'" AND fp_events_contactscontacts_idb="'.$contact.'"';
140
                $res = $db->query($query);
141
                //update Leads query
142
                $query2 = 'UPDATE fp_events_leads_1_c SET invite_status="Not Invited", email_responded="0" WHERE fp_events_leads_1fp_events_ida="'.$event_id.'" AND fp_events_leads_1leads_idb="'.$contact.'"';
143
                $res = $db->query($query2);
144
                //update targets query
145
                $query3 = 'UPDATE fp_events_prospects_1_c SET invite_status="Not Invited", email_responded="0" WHERE fp_events_prospects_1fp_events_ida="'.$event_id.'" AND fp_events_prospects_1prospects_idb="'.$contact.'"';
146
                $res = $db->query($query3);
147
            }
148
        }
149
        else if($entire_list == '1'){ //updates all records
150
151
                //update contacts query
152
                $query = 'UPDATE fp_events_contacts_c SET invite_status="Not Invited", email_responded="0" WHERE fp_events_contactsfp_events_ida="'.$event_id.'"';
153
                $res = $db->query($query);
154
                //update Leads query
155
                $query2 = 'UPDATE fp_events_leads_1_c SET invite_status="Not Invited", email_responded="0" WHERE fp_events_leads_1fp_events_ida="'.$event_id.'"';
156
                $res = $db->query($query2);
157
                //update targets query
158
                $query3 = 'UPDATE fp_events_prospects_1_c SET invite_status="Not Invited", email_responded="0" WHERE fp_events_prospects_1fp_events_ida="'.$event_id.'"';
159
                $res = $db->query($query3);
160
        }
161
    }
162
163
    public function action_markasaccepted()
164
    {
165
        global $db;
166
167
        $ids = $_POST['id'];
168
        $entire_list = $_POST['entire_list'];
169
        $event_id = $_POST['event_id'];
170
171
        if($entire_list != '1'){
172
173
            $contacts = explode(',', $ids);
174
175
            foreach($contacts as $contact){
176
177
                $query = 'UPDATE fp_events_contacts_c SET accept_status="Accepted" WHERE fp_events_contactsfp_events_ida="'.$event_id.'" AND fp_events_contactscontacts_idb="'.$contact.'"';
178
                $res = $db->query($query);
179
                //update Leads query
180
                $query2 = 'UPDATE fp_events_leads_1_c SET accept_status="Accepted" WHERE fp_events_leads_1fp_events_ida="'.$event_id.'" AND fp_events_leads_1leads_idb="'.$contact.'"';
181
                $res = $db->query($query2);
182
                //update targets query
183
                $query3 = 'UPDATE fp_events_prospects_1_c SET accept_status="Accepted" WHERE fp_events_prospects_1fp_events_ida="'.$event_id.'" AND fp_events_prospects_1prospects_idb="'.$contact.'"';
184
                $res = $db->query($query3);
185
186
            }
187
        }
188
        else if($entire_list == '1'){ //updates all records
189
190
                //update contacts query
191
                $query = 'UPDATE fp_events_contacts_c SET accept_status="Accepted" WHERE fp_events_contactsfp_events_ida="'.$event_id.'"';
192
                $res = $db->query($query);
193
                //update Leads query
194
                $query2 = 'UPDATE fp_events_leads_1_c SET accept_status="Accepted" WHERE fp_events_leads_1fp_events_ida="'.$event_id.'"';
195
                $res = $db->query($query2);
196
                //update targets query
197
                $query3 = 'UPDATE fp_events_prospects_1_c SET accept_status="Accepted" WHERE fp_events_prospects_1fp_events_ida="'.$event_id.'"';
198
                $res = $db->query($query3);
199
        }
200
    }
201
    public function action_markasdeclined()
202
    {
203
        global $db;
204
205
        $ids = $_POST['id'];
206
        $entire_list = $_POST['entire_list'];
207
        $event_id = $_POST['event_id'];
208
209
        if($entire_list != '1'){
210
211
            $contacts = explode(',', $ids);
212
213
            foreach($contacts as $contact){
214
215
                $query = 'UPDATE fp_events_contacts_c SET accept_status="Declined" WHERE fp_events_contactsfp_events_ida="'.$event_id.'" AND fp_events_contactscontacts_idb="'.$contact.'"';
216
                $res = $db->query($query);
217
                //update Leads query
218
                $query2 = 'UPDATE fp_events_leads_1_c SET accept_status="Declined" WHERE fp_events_leads_1fp_events_ida="'.$event_id.'" AND fp_events_leads_1leads_idb="'.$contact.'"';            $res = $db->query($query2);
219
                $res = $db->query($query2);
220
                //update targets query
221
                $query3 = 'UPDATE fp_events_prospects_1_c SET accept_status="Declined" WHERE fp_events_prospects_1fp_events_ida="'.$event_id.'" AND fp_events_prospects_1prospects_idb="'.$contact.'"';
222
                $res = $db->query($query3);
223
            }
224
        }
225
        else if($entire_list == '1'){ //updates all records
226
227
                //update contacts query
228
                $query = 'UPDATE fp_events_contacts_c SET accept_status="Declined" WHERE fp_events_contactsfp_events_ida="'.$event_id.'"';
229
                $res = $db->query($query);
230
                //update Leads query
231
                $query2 = 'UPDATE fp_events_leads_1_c SET accept_status="Declined" WHERE fp_events_leads_1fp_events_ida="'.$event_id.'"';
232
                $res = $db->query($query2);
233
                //update targets query
234
                $query3 = 'UPDATE fp_events_prospects_1_c SET accept_status="Declined" WHERE fp_events_prospects_1fp_events_ida="'.$event_id.'"';
235
                $res = $db->query($query3);
236
        }
237
    }
238
239
    public function action_add_to_list(){
240
241
        $ids = $_POST['subpanel_id'];
242
        $event_id = $_POST['return_id'];
243
        $type = $_POST['pop_up_type'];
244
      
245
246
        if(!is_array($ids)){
247
248
            $ids = array($ids); 
249
250
        }
251
        //Target lists. Can incliude contacts, leads and targets as part of the target list
252
        if($type == 'target_list'){
253
254
            foreach($ids as $list){
255
256
                $event = new FP_events();
257
                $event->retrieve($event_id);
258
                $event->load_relationship('fp_events_prospects_1');
259
                $event->load_relationship('fp_events_contacts');
260
                $event->load_relationship('fp_events_leads_1');
261
262
                $target_list = new ProspectList();
263
                $target_list->retrieve($list);
264
                $target_list->load_relationship('prospects');
265
                $target_list->load_relationship('contacts');
266
                $target_list->load_relationship('leads');
267
268
                //add prospects/targets
269
                foreach ($target_list->prospects->getBeans() as $contact) {
270
271
                    $contact_id_list = $event->fp_events_prospects_1->get();
272
                    
273
                    if(!in_array($contact->id, $contact_id_list)) { //check if its already related 
274
275
                        $event->fp_events_prospects_1->add($contact->id);
276
                   }
277
                }
278
                //add contacts
279
                foreach ($target_list->contacts->getBeans() as $contact) {
280
                    
281
                   $contact_id_list = $event->fp_events_contacts->get(); 
282
283
                   if(!in_array($contact->id, $contact_id_list)) { 
284
285
                        $event->fp_events_contacts->add($contact->id);
286
                   }
287
                }
288
                //add leads
289
                foreach($target_list->leads->getBeans() as $contact) {
290
291
                    $contact_id_list = $event->fp_events_leads_1->get();
292
293
                    if(!in_array($contact->id, $contact_id_list)) {     
294
295
                        $event->fp_events_leads_1->add($contact->id);
296
                    }
297
                }
298
            }   
299
        }
300
        //Targets
301
        elseif($type == 'targets'){
302
303
            foreach($ids as $target){
304
                
305
                $event = new FP_events();
306
                $event->retrieve($event_id);
307
                $event->load_relationship('fp_events_prospects_1');
308
309
                $contact_id_list = $event->fp_events_prospects_1->get();//get array of currently linked targets
310
311
                if(!in_array($target, $contact_id_list)) { //check if its already in the array
312
313
                    $event->fp_events_prospects_1->add($target);//if not add relationship
314
                }
315
            }
316
        }
317
        //leads
318
        elseif($type == 'leads'){
319
            
320
            foreach($ids as $lead){
321
322
                $event = new FP_events();
323
                $event->retrieve($event_id);
324
                $event->load_relationship('fp_events_leads_1');
325
326
                $contact_id_list = $event->fp_events_leads_1->get();//get array of currently linked leads
327
328
                if(!in_array($lead, $contact_id_list)) { //check if its already in the array
329
330
                    $event->fp_events_leads_1->add($lead);//if not add relationship
331
                }
332
            }
333
        }
334
        //contacts
335
        elseif($type == 'contacts'){
336
337
            foreach($ids as $contact){
338
                
339
                $event = new FP_events();
340
                $event->retrieve($event_id);
341
                $event->load_relationship('fp_events_contacts');
342
343
                $contact_id_list = $event->fp_events_contacts->get(); //get array of currently linked contacts
344
345
                if(!in_array($contact, $contact_id_list)) { 
346
347
                    $event->fp_events_contacts->add($contact);
348
               }
349
            }
350
        }
351
352
        die();
353
    }
354
355
    public function action_sendinvitemails(){
356
        global $db;
357
        global $sugar_config;
358
        global $mod_strings;
359
360
        $id = $_GET['record'];
361
        //get event
362
        $event = new FP_events();
363
        $event->retrieve($id);
364
365
        $event->load_relationship('fp_events_contacts'); // get related contacts
366
        $event->load_relationship('fp_events_prospects_1'); //get related targets
367
        $event->load_relationship('fp_events_leads_1'); //get related leads
368
369
        //Count the number of delegates linked to the event that have not yet been invited
370
        $query = "SELECT * FROM fp_events_contacts_c WHERE fp_events_contactsfp_events_ida='".$event->id."' AND (invite_status='Not Invited' OR invite_status='' OR invite_status IS NULL) AND deleted='0'";
371
        $result = $db->query($query);
372
        $contact_count = $db->getRowCount($result);//count contacts
373
374
        $query = "SELECT * FROM fp_events_prospects_1_c WHERE fp_events_prospects_1fp_events_ida='".$event->id."' AND (invite_status='Not Invited' OR invite_status='' OR invite_status IS NULL) AND deleted='0'";
375
        $result = $db->query($query);
376
        $prospect_count = $db->getRowCount($result);//count targets
377
378
        $query = "SELECT * FROM fp_events_leads_1_c WHERE fp_events_leads_1fp_events_ida='".$event->id."' AND (invite_status='Not Invited' OR invite_status='' OR invite_status IS NULL) AND deleted='0'";
379
        $result = $db->query($query);
380
        $lead_count = $db->getRowCount($result);//count leads
381
382
        $delegate_count = $contact_count + $prospect_count + $lead_count;//Total up delegates 
383
        $invite_count = 0; //used to count the number of emails sent
384
        $error_count = 0; //used to count the number of failed email attempts
385
386
        
387
        //loop through related contacts
388
        foreach ($event->fp_events_contacts->getBeans() as $contact) {
389
390
            //Get accept status of contact
391
            $query = 'SELECT invite_status FROM fp_events_contacts_c WHERE fp_events_contactsfp_events_ida="'.$event->id.'" AND fp_events_contactscontacts_idb="'.$contact->id.'"';
392
            $status = $db->getOne($query);
393
394
            if($status == null || $status == '' || $status == 'Not Invited'){
395
396
                $invite_count ++;
397
                //set email links
398
                $event->link = "<a href='".$sugar_config['site_url']."/index.php?entryPoint=responseEntryPoint&event=".$event->id."&delegate=".$contact->id."&type=c&response=accept'>Accept</a>";
399
                $event->link_declined = "<a href='".$sugar_config['site_url']."/index.php?entryPoint=responseEntryPoint&event=".$event->id."&delegate=".$contact->id."&type=c&response=decline'>Decline</a>";
400
401
                //Get the TO name and e-mail address for the message
402
                $rcpt_name = $contact->first_name . ' ' . $contact->last_name;
403
                $rcpt_email = $contact->email1;
404
405
                $emailTemp = new EmailTemplate();
406
                $emailTemp->disable_row_level_security = true;
407
                $emailTemp->retrieve($event->invite_templates);  //Use the ID value of the email template record
408
409
                //check email template is set, if not return error
410
                if($emailTemp->id == '')
411
                {
412
                    SugarApplication::appendErrorMessage($mod_strings['LBL_ERROR_MSG_5']);
413
                    SugarApplication::redirect("index.php?module=FP_events&return_module=FP_events&action=DetailView&record=".$event->id);
414
                    die();
415
                }
416
417
                //parse the lead varibales first
418
                $firstpass = $emailTemp->parse_template_bean($emailTemp->body_html, 'Contacts', $contact);
419
420
                $email_subject = $emailTemp->parse_template_bean($emailTemp->subject, 'FP_events', $event);
421
                $email_body = from_html($emailTemp->parse_template_bean($firstpass, 'FP_events', $event));
422
                $alt_emailbody = wordwrap($emailTemp->parse_template_bean($firstpass, 'FP_events', $event), 900);
423
424
                //get attachments
425
                $attachmentBean = new Note();
426
                $attachment_list = $attachmentBean->get_full_list('',"parent_type = 'Emails' AND parent_id = '".$event->invite_templates."'");
427
428
                $attachments = array();
429
430
                if($attachment_list != null){
431
432
                    foreach ($attachment_list as $attachment) {
433
                        $attachments[] = $attachment;
434
                    }
435
                }
436
437
                //send the email
438
                $send_invite = $this->sendEmail($rcpt_email, $email_subject, $rcpt_name, $email_body, $alt_emailbody, $contact, $attachments);
439
440
441
                //Send the message, log if error occurs
442
                if (!$send_invite){
443
                    $GLOBALS['log']->fatal('ERROR: Invite email failed to send to: '.$rcpt_name.' at '.$rcpt_email);
444
                    $error_count ++;
445
                }
446
                else {
447
                    //update contact to invites
448
                    $query = 'UPDATE fp_events_contacts_c SET invite_status="Invited" WHERE fp_events_contactsfp_events_ida="'.$event->id.'" AND fp_events_contactscontacts_idb="'.$contact->id.'"';
449
                    $res = $db->query($query);
450
                }
451
            }
452
        }
453
454
        //loop through related targets
455
        foreach ($event->fp_events_prospects_1->getBeans() as $target) {
456
457
            //Get accept status of contact
458
            $query = 'SELECT invite_status FROM fp_events_prospects_1_c WHERE fp_events_prospects_1fp_events_ida="'.$event->id.'" AND fp_events_prospects_1prospects_idb="'.$target->id.'"';
459
            $status = $db->getOne($query);
460
461
            if($status == null || $status == '' || $status == 'Not Invited'){
462
                $invite_count ++;
463
464
                //set email links
465
                $event->link = "<a href='".$sugar_config['site_url']."/index.php?entryPoint=responseEntryPoint&event=".$event->id."&delegate=".$target->id."&type=t&response=accept'>Accept</a>";
466
                $event->link_declined = "<a href='".$sugar_config['site_url']."/index.php?entryPoint=responseEntryPoint&event=".$event->id."&delegate=".$target->id."&type=t&response=decline'>Decline</a>";
467
468
                //Get the TO name and e-mail address for the message
469
                $rcpt_name = $target->first_name . ' ' . $target->last_name;
470
                $rcpt_email = $target->email1;
471
472
                $emailTemp = new EmailTemplate();
473
                $emailTemp->disable_row_level_security = true;
474
                $emailTemp->retrieve($event->invite_templates);  //Use the ID value of the email template record
475
476
                //parse the lead varibales first
477
                $firstpass = $emailTemp->parse_template_bean($emailTemp->body_html, 'Contacts', $target);
478
479
                $email_subject = $emailTemp->parse_template_bean($emailTemp->subject, 'FP_events', $event);
480
                $email_body = from_html($emailTemp->parse_template_bean($firstpass, 'FP_events', $event));
481
                $alt_emailbody = wordwrap($emailTemp->parse_template_bean($firstpass, 'FP_events', $event), 900);
482
483
                //get attachments
484
                $attachmentBean = new Note();
485
                $attachment_list = $attachmentBean->get_full_list('',"parent_type = 'Emails' AND parent_id = '".$event->invite_templates."'");
486
487
                $attachments = array();
488
489
                if($attachment_list != null){
490
491
                    foreach ($attachment_list as $attachment) {
492
                        $attachments[] = $attachment;
493
                    }
494
                }
495
496
                //send the email
497
                $send_invite = $this->sendEmail($rcpt_email, $email_subject, $rcpt_name, $email_body, $alt_emailbody, $target, $attachments);
498
499
500
                //Send the message, log if error occurs
501
                if (!$send_invite){
502
                    $GLOBALS['log']->fatal('ERROR: Invite email failed to send to: '.$rcpt_name.' at '.$rcpt_email);
503
                    $error_count ++;
504
                }
505
                else {
506
                    //update contact to invites
507
                    $query = 'UPDATE fp_events_prospects_1_c SET invite_status="Invited" WHERE fp_events_prospects_1fp_events_ida="'.$event->id.'" AND fp_events_prospects_1prospects_idb="'.$target->id.'"';
508
                    $res = $db->query($query);
509
                }
510
            }
511
        }
512
513
        //loop through related leads
514
        foreach ($event->fp_events_leads_1->getBeans() as $lead) {
515
516
            //Get accept status of contact
517
            $query = 'SELECT invite_status FROM fp_events_leads_1_c WHERE fp_events_leads_1fp_events_ida="'.$event->id.'" AND fp_events_leads_1leads_idb="'.$lead->id.'"';
518
            $status = $db->getOne($query);
519
520
            if($status == null || $status == '' || $status == 'Not Invited'){
521
522
                $invite_count ++;
523
                //set email links
524
                $event->link = "<a href='".$sugar_config['site_url']."/index.php?entryPoint=responseEntryPoint&event=".$event->id."&delegate=".$lead->id."&type=l&response=accept'>Accept</a>";
525
                $event->link_declined = "<a href='".$sugar_config['site_url']."/index.php?entryPoint=responseEntryPoint&event=".$event->id."&delegate=".$lead->id."&type=l&response=decline'>Decline</a>";
526
527
                //Get the TO name and e-mail address for the message
528
                $rcpt_name = $lead->first_name . ' ' . $lead->last_name;
529
                $rcpt_email = $lead->email1;
530
531
                $emailTemp = new EmailTemplate();
532
                $emailTemp->disable_row_level_security = true;
533
                $emailTemp->retrieve($event->invite_templates);  //Use the ID value of the email template record
534
535
                //parse the lead varibales first
536
                $firstpass = $emailTemp->parse_template_bean($emailTemp->body_html, 'Contacts', $lead);
537
538
                $email_subject = $emailTemp->parse_template_bean($emailTemp->subject, 'FP_events', $event);
539
                $email_body = from_html($emailTemp->parse_template_bean($firstpass, 'FP_events', $event));
540
                $alt_emailbody = wordwrap($emailTemp->parse_template_bean($firstpass, 'FP_events', $event), 900);
541
542
                //get attachments
543
                $attachmentBean = new Note();
544
                $attachment_list = $attachmentBean->get_full_list('',"parent_type = 'Emails' AND parent_id = '".$event->invite_templates."'");
545
546
                $attachments = array();
547
548
                if($attachment_list != null){
549
550
                    foreach ($attachment_list as $attachment) {
551
                        $attachments[] = $attachment;
552
                    }
553
                }
554
555
                //send the email
556
                $send_invite = $this->sendEmail($rcpt_email, $email_subject, $rcpt_name, $email_body, $alt_emailbody, $lead, $attachments);
557
558
559
                //Send the message, log if error occurs
560
                if (!$send_invite){
561
                    $GLOBALS['log']->fatal('ERROR: Invite email failed to send to: '.$rcpt_name.' at '.$rcpt_email);
562
                    $error_count ++;
563
                }
564
                else {
565
                    //update contact to invites
566
                    $query = 'UPDATE fp_events_leads_1_c SET invite_status="Invited" WHERE fp_events_leads_1fp_events_ida="'.$event->id.'" AND fp_events_leads_1leads_idb="'.$lead->id.'"';
567
                    $res = $db->query($query);
568
                }
569
            }
570
        }
571
        //Redirect with error message if all linked contacts have already been invited
572
        if($invite_count == 0) {
573
            SugarApplication::appendErrorMessage($mod_strings['LBL_ERROR_MSG_1']);
574
            SugarApplication::redirect("index.php?module=FP_events&return_module=FP_events&action=DetailView&record=".$event->id);
575
        }
576
        //Redirect if all emails fail to send
577
        if($error_count == $delegate_count){
578
            $_SESSION['user_error_message'] = array();//clear the error message array
579
            SugarApplication::appendErrorMessage($mod_strings['LBL_ERROR_MSG_2'].$delegate_count);
580
            SugarApplication::redirect("index.php?module=FP_events&return_module=FP_events&action=DetailView&record=".$event->id);        
581
582
        }
583
        else if($error_count > 0 && $error_count <= 10) {//redirect with failed email count.
584
            $_SESSION['user_error_message'] = array();
585
            SugarApplication::appendErrorMessage($error_count.$mod_strings['LBL_ERROR_MSG_4']);
586
            SugarApplication::redirect("index.php?module=FP_events&return_module=FP_events&action=DetailView&record=".$event->id); 
587
        }
588
         // Redirect with error count if failed email attempts are greater than 10 
589
        else if($error_count > 10) {   
590
            $_SESSION['user_error_message'] = array();
591
            SugarApplication::appendErrorMessage($mod_strings['LBL_ERROR_MSG_3']);
592
            SugarApplication::redirect("index.php?module=FP_events&return_module=FP_events&action=DetailView&record=".$event->id);    
593
        }
594
        else {
595
            SugarApplication::appendErrorMessage($mod_strings['LBL_SUCCESS_MSG']);
596
            SugarApplication::redirect("index.php?module=FP_events&return_module=FP_events&action=DetailView&record=".$event->id);     
597
        } 
598
    }
599
600
    //handles sending the emails
601
    public function sendEmail($emailTo, $emailSubject, $emailToname, $emailBody, $altemailBody, SugarBean $relatedBean = null, $attachments = array()){
602
       
603
        $emailObj = new Email();
604
        $defaults = $emailObj->getSystemDefaultEmail();
605
        $mail = new SugarPHPMailer();
606
        $mail->setMailerForSystem();
607
        $mail->From = $defaults['email'];
608
        $mail->FromName = $defaults['name'];
609
        $mail->ClearAllRecipients();
610
        $mail->ClearReplyTos();
611
        $mail->Subject=from_html($emailSubject);
612
        $mail->Body=$emailBody;
613
        $mail->AltBody = $altemailBody;
614
        $mail->handleAttachments($attachments);
615
        $mail->prepForOutbound();
616
        $mail->AddAddress($emailTo);
617
618
        //now create email
619
        if (@$mail->Send()) {
620
            $emailObj->to_addrs= '';
621
            $emailObj->type= 'out';
622
            $emailObj->deleted = '0';
623
            $emailObj->name = $mail->Subject;
624
            $emailObj->description = $mail->AltBody;
625
            $emailObj->description_html = $mail->Body;
626
            $emailObj->from_addr = $mail->From;
627
            if ( $relatedBean instanceOf SugarBean && !empty($relatedBean->id) ) {
628
                $emailObj->parent_type = $relatedBean->module_dir;
629
                $emailObj->parent_id = $relatedBean->id;
630
            }
631
            $emailObj->date_sent = TimeDate::getInstance()->nowDb();
632
            $emailObj->modified_user_id = '1';
633
            $emailObj->created_by = '1';
634
            $emailObj->status = 'sent';
635
            $emailObj->save();
636
637
            return true;
638
        }
639
        else {
640
            return false;
641
        }
642
    }
643
}
644