Calendar Page:
This example shows how to select a calendar date and create your own tasks and events in the time of record creation .The controller looks as
public class repeatCon {
public void next() {
addMonth(1);
}
public void prev() {
addMonth(-1);
}
public repeatCon() {
Date d = system.today(); // default to today
Integer mo = d.month();
String m_param = System.currentPageReference().getParameters().get('mo');
String y_param = System.currentPageReference().getParameters().get('yr');
// allow a month to be passed in on the url as mo=10
if (m_param != null) {
Integer mi = Integer.valueOf(m_param);
if (mi > 0 && mi <= 12) {
d = Date.newInstance(d.year(),mi,d.day());
}
}
// and year as yr=2008
if (y_param != null) {
Integer yr = Integer.valueOf(y_param);
d = Date.newInstance(yr, d.month(), d.day());
}
setMonth(d);
}
public List<Month.Week> getWeeks() {
//system.assert(month!=null,'month is null');
return month.getWeeks();
}
public Month getMonth() { return month; }
private void setMonth(Date d) {
month = new Month(d);
}
private void addMonth(Integer val) {
Date d = month.getFirstDate();
d = d.addMonths(val);
setMonth(d);
}
private Month month;
public static testMethod void chkrepeatCon() {
repeatCon rCon = new repeatCon();
List<Month.Week> testWeeks = new List<Month.Week>();
testWeeks = rCon.getWeeks();
//system.assertequals(1,testWeeks.size());
Integer testVal = 1;
rCon.addMonth(testVal);
rCon.next();
rCon.prev();
}
static testmethod void getValidDateRange(){
Date myDate = date.newinstance(1960, 2, 17);
Month mnth = new Month(myDate);
List<Date> dateList = mnth.getValidDateRange();
system.assertEquals(dateList.size(), 2);
}
static testmethod void getMonthName(){
Date myDate = date.newinstance(1960, 2, 17);
Month mnth = new Month(myDate);
String monthName = mnth.getMonthName();
system.assertEquals(monthName ,'February' );
}
static testmethod void getYearName(){
Date myDate = date.newinstance(1960, 2, 17);
Month mnth = new Month(myDate);
String yearName = mnth.getYearName();
system.assertEquals(yearName , '1960' );
}
static testmethod void getWeekdayNames(){
Date myDate = date.newinstance(1960, 2, 17);
Month mnth = new Month(myDate);
String[] weekNames = mnth.getWeekdayNames();
system.assertEquals(weekNames.size(), 7 );
}
static testmethod void getDayClass(){
Date myDate = date.newinstance(1960, 2, 17);
Month.Day mnthDay = new Month.Day(myDate,2);
system.assertEquals(mnthDay.getDayOfYear(), 48 );
system.assertEquals(mnthDay.getFormatedDate(), '12 21 08' );
system.assertEquals(mnthDay.getDayNumber(), null );
system.assertEquals( mnthDay.getCSSName(), 'calActive');
}
} |
also you need another class for calendar functions mentioned in the above class
public class Month {
public List<Week> weeks;
public Date firstDate; // always the first of the month
private Date upperLeft;
public List<Date> getValidDateRange() {
// return one date from the upper left, and one from the lower right
List<Date> ret = new List<Date>();
ret.add(upperLeft);
ret.add(upperLeft.addDays(5*7) );
return ret;
}
public String getMonthName() {
return DateTime.newInstance(firstDate.year(),firstdate.month(),firstdate.day()).format('MMMM');
}
public String getYearName() {
return DateTime.newInstance(
firstDate.year(),firstdate.month(),firstdate.day()).format('yyyy');
}
public String[] getWeekdayNames() {
Date today = system.today().toStartOfWeek();
DateTime dt = DateTime.newInstanceGmt(today.year(),today.month(),today.day());
list<String> ret = new list<String>();
for(Integer i = 0; i < 7;i++) {
ret.add( dt.formatgmt('EEEE') );
dt= dt.addDays(1);
}
return ret;
}
public Date getfirstDate() { return firstDate; }
public Month( Date value ) {
weeks = new List<Week>();
firstDate = value.toStartOfMonth();
upperLeft = firstDate.toStartOfWeek();
Date tmp = upperLeft;
for (Integer i = 0; i < 5; i++) {
Week w = new Week(i+1,tmp,value.month());
//system.assert(w!=null);
this.weeks.add( w );
tmp = tmp.addDays(7);
}
}
public List<Week> getWeeks() {
//system.assert(weeks!=null,'could not create weeks list');
return this.weeks;
}
/*
* helper classes to define a month in terms of week and day
*/
public class Week {
public List<Day> days;
public Integer weekNumber;
public Date startingDate; // the date that the first of this week is on
// so sunday of this week
public List<Day> getDays() { return this.days; }
public Week () {
days = new List<Day>();
}
public Week(Integer value,Date sunday,Integer month) {
this();
weekNumber = value;
startingDate = sunday;
Date tmp = startingDate;
for (Integer i = 0; i < 7; i++) {
Day d = new Day( tmp,month );
tmp = tmp.addDays(1);
d.dayOfWeek = i+1;
// system.debug(d);
days.add(d);
}
}
public Integer getWeekNumber() { return this.weekNumber;}
public Date getStartingDate() { return this.startingDate;}
}
public class Day {
public Date theDate;
public Integer month, dayOfWeek;
public String formatedDate; // for the formated time
private String cssclass = 'calActive';
public Date getDate() { return theDate; }
public Integer getDayOfMonth() { return theDate.day(); }
public String getDayOfMonth2() {
if ( theDate.day() <= 9 )
return '0'+theDate.day();
return String.valueof( theDate.day());
}
public Integer getDayOfYear() { return theDate.dayOfYear(); }
public String getFormatedDate() { return formatedDate; }
public Integer getDayNumber() { return dayOfWeek; }
public String getCSSName() { return cssclass; }
public Day(Date value,Integer vmonth) {
theDate=value; month=vmonth;
formatedDate = '12 21 08';// time range..
//9:00 AM - 1:00 PM
// three possible Inactive,Today,Active
if ( theDate.daysBetween(System.today()) == 0 ) cssclass ='calToday';
// define inactive, is the date in the month?
if ( theDate.month() != month) cssclass = 'calInactive';
}
}
} |
and the visualforce page is
<apex:page controller="repeatCon" id="thePage" >
<apex:stylesheet value="/sCSS/Theme2/default/homeCalendar.css" />
<apex:form id="theForm">
<apex:outputPanel id="theCalendar" >
<div class="mCalendar" style="width:182px;" ><div class="topLeft" ><div class="topRight"/></div>
<div class="body">
<table cellspacing="0" cellpadding="2" border="0">
<tbody>
<tr class="header">
<td><apex:commandLink action="{!prev}" rerender="theCalendar">
<img title="Previous Month" class="prevCalArrow" alt="Previous Month" src="/s.gif" />
</apex:commandLink></td>
<td colspan="5" >
{!month.monthname} {!month.yearname}
</td>
<td><apex:commandLink action="{!next}" rerender="theCalendar">
<img title="Next Month" class="nextCalArrow" alt="Next Month"
src="/s.gif" />
</apex:commandLink></td>
</tr>
<tr>
<th scope="col" class="calDays">Sun</th>
<th scope="col" class="calDays">Mon</th>
<th scope="col" class="calDays">Tue</th>
<th scope="col" class="calDays">Wed</th>
<th scope="col" class="calDays">Thu</th>
<th scope="col" class="calDays">Fri</th>
<th scope="col" class="calDays">Sat</th>
</tr>
<apex:repeat value="{!weeks}" var="wk" id="foreachWeek">
<tr class="days">
<!-- or highlight -->
<apex:repeat value="{!wk.days}" var="day" id="foreachday">
<td valign="top"><a class="calActive"
href="/00U/c?md0=2008&md3={!day.dayOfYear}" target="_self"
title="Day View - {!day.date}">{!day.dayofmonth2}</a></td>
</apex:repeat>
</tr>
</apex:repeat>
</tbody>
</table>
</div>
<div class="bottomLeft"><div class="bottomRight"/></div>
</div>
</apex:outputPanel>
</apex:form>
</apex:page>
No comments:
Post a Comment