· 8 years ago · Mar 30, 2018, 08:44 PM
1// Variable Initializations
2public String errorMessage {get; set;}
3public string email{get;set;}
4public date startDate{get;set;}
5public string startDateString{get;set;}
6public decimal HoursWorked{get;set;}
7public string jobName{get;set;}
8public string comments{get;set;}
9public Contact ct{get;set;}
10Public list<contact> ctlist{get;set;}
11Public list<GW_Volunteers__Volunteer_Job__c> joblist{get;set;}
12public String selectedVal{get;set;} // This will hold the selected value (id) from the Volunteer Job dropdown
13
14private final GW_Volunteers__Volunteer_Hours__c volHours;
15private ApexPages.StandardController stdController;
16
17// Constructor
18public SubmitVolunteerHours_EXT(ApexPages.StandardController stdController) {
19 this.volHours = (GW_Volunteers__Volunteer_Hours__c)stdController.getRecord();
20 this.stdController = stdController;
21 startDateString=system.today().format();
22}
23
24// Validates Emails: =1 if email is not submitted.
25// =2 if email does not exist in the org but the format is correct.
26// =0 if email exists in the org.
27public static integer validateEmail(String email) {
28integer res = 0;
29if (string.isBlank(email)) {
30 res=1;
31 return res;
32}
33
34String emailRegex = '^[a-zA-Z0-9._|\\%#~`=?&/$^*!}{+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$'; // source: <a href="http://www.regular-expressions.info/email.html" target="_blank" rel="nofollow">http://www.regular-expressions.info/email.html</a>
35Pattern MyPattern = Pattern.compile(emailRegex);
36Matcher MyMatcher = MyPattern.matcher(email);
37
38if (!MyMatcher.matches())
39 res = 2;
40return res;
41}
42
43// Validates Dates to be in mm/dd/yyyy format.
44public static Boolean validateDate(String startDateString) {
45Boolean res = true;
46
47String dateRegex = '^((((0[13578])|([13578])|(1[02]))[\/](([1-9])|([0-2][0-9])|(3[01])))|(((0[469])|([469])|(11))[\/](([1-9])|([0-2][0-9])|(30)))|((2|02)[\/](([1-9])|([0-2][0-9]))))[\/]\d{4}$|^\d{4}$';
48Pattern MyPattern = Pattern.compile(dateRegex);
49Matcher MyMatcher = MyPattern.matcher(startDateString);
50
51if (!MyMatcher.matches())
52 res = false;
53return res;
54}
55
56// Creates options for dropdown list of Volunteer Jobs to select.
57public List<SelectOption> getVolunteerJobOptions(){
58 joblist = [select Id,Name from GW_Volunteers__Volunteer_Job__c];
59 List<SelectOption> optns = new List<Selectoption>();
60 for(GW_Volunteers__Volunteer_Job__c obj : joblist){
61 optns.add(new selectOption(obj.Id, obj.Name));
62 }
63
64 return optns;
65}
66
67// Saves record...referenced in saveAndRedirect().
68public PageReference save() {
69 errorMessage = '';
70 try {
71 upsert volHours;
72 return new ApexPages.StandardController(volHours).view();
73 } catch(System.Exception ex) {
74 errorMessage = ex.getMessage();
75 return null;
76 }
77}
78
79// Saves record and redirects to Thank You page.
80// If no email is submitted, creates error message.
81// If an email address in the wrong format is submitted, creates error message.
82// If a date not in format mm/dd/yyyy is submitted, creates error message.
83// Else, creates a Volunteer_Hours__c object given the user inputs and inserts it into our org, then redirects to the Thank You page.
84// If none of the above, creates error message saying the email given does not exist in our org.
85public PageReference saveAndRedirect() {
86
87
88 if(validateEmail(email)==1) {ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'You did not enter an email address. Please enter a valid email address in the form specified to the right of the email box.'));
89 return null;
90 }
91 else{
92 if(validateEmail(email)==2) {ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Not a valid email address. Please enter a valid email address in the form specified to the right of the email box.'));
93 return null;}
94 else{
95 if(validateDate(startDateString)==false) {ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Not a valid date. Please enter a valid date in the form specified to the right of the Date box.'));
96 return null;
97 }
98 else{
99 list<GW_Volunteers__Volunteer_Hours__c> newlist = new list<GW_Volunteers__Volunteer_Hours__c>();
100 ctlist=[select email,id from Contact where email=:email limit 1];
101 if(ctlist.size()>0){
102
103 for(contact cn:ctlist){
104 joblist=[select Name, id from GW_Volunteers__Volunteer_Job__c where Name=:jobName limit 1];
105 GW_Volunteers__Volunteer_Hours__c volHr = new GW_Volunteers__Volunteer_Hours__c();
106 volHr.GW_Volunteers__Contact__c =cn.id;
107 volHr.GW_Volunteers__Volunteer_Job__c = selectedVal;
108 volHr.GW_Volunteers__Status__c='Completed';
109 volHr.GW_Volunteers__Number_of_Volunteers__c=1;
110 startDate = date.parse(startDateString);
111 volHr.GW_Volunteers__Start_Date__c= startDate;
112 volHr.GW_Volunteers__Hours_Worked__c=HoursWorked;
113 volHr.GW_Volunteers__Comments__c=comments;
114 newlist.add(volHr);
115 }
116 insert newlist;
117 PageReference newPage = Page.VolunteerHoursThankYou; // This will be the visualforce page that we want to redirect to
118 newPage.setRedirect(true);
119 return newPage;
120 }
121 else{
122 String link = '<a href="http://www.google.com">here</a>';
123 ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Email Address does not exist. Please click' + link + 'or contact our volunteer coordinator ___@gmail.com to register as a volunteer in our system!'));
124 return null;
125 }
126 }
127 }
128 }
129}
130
131}