1. Trang chủ
  2. » Công Nghệ Thông Tin

Tạo mạng xã hội với PHP - part 29 potx

10 248 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 10
Dung lượng 3,3 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Ordered days As discussed earlier, our calendar is set to be customizable in terms of which day of the month is the start date.. For this to be done, we need to know the month and if a

Trang 1

// increment and loop

$start++;

$days++;

}

// done

$this->dates = $cal_dates;

$this->dateStyles = $cal_dates_style;

$this->dateData = $cal_dates_data;

}

Days in the month

Calculating the days in a month is a fairly simple task; all months have a set number

of days in them, except for February, so for February, we simply need to check

whether the current year is a leap year:

/**

* How many days are in a month?

* @param int $m month

* @param int $y year

* @return int the number of days in the month

*/

function daysInMonth($m, $y)

{

If we have been passed a month that isn't valid, simply return zero:

if( $m < 1 || $m > 12 )

{

return 0;

}

else

{

September, April, June, and November have 30 days—so for these months,

return 30:

// 30: 9, 4, 6, 11

if( $m == 9 || $m == 4 || $m == 6 || $m == 11 )

{

return 30;

Trang 2

For any remaining month that isn't February (all the rest have 31), we return 31:

else if( $m != 2 )

{

// all the rest have 31

return 31;

}

else

{

A year isn't a leap year if the year isn't divisible by 4, so in this instance,

we return 28:

// except for february alone

if( $y % 4 != 0 )

{

// which has 28

return 28;

}

else

{

If a year isn't divisible by 100, then it is a leap year, so we return 29:

if( $y % 100 != 0 )

{

// and on leap years 29

return 29;

}

else

{

If the year isn't divisible by 400, then it isn't a leap year, so we return 28:

if( $y % 400 != 0 )

{

// deja vu: which has 28

return 28;

}

else

{

Trang 3

If it is divisible by 400, then we return 29, as it is a leap year:

// deja vu: and on leap years 29

return 29;

}

}

}

}

}

}

And there we have a very handy function, as part of our calendar library, to

determine the number of days in any given month.

Ordered days

As discussed earlier, our calendar is set to be customizable in terms of which day of

the month is the start date Because of this, our array of days (private $days = ar

ray('Sun','Mon','Tue','Wed','Thu','Fri', 'Sat');) needs to be re-ordered

based on the chosen first day of the week:

/**

* Get days in order

* @return array array of days (as strings)

*/

function getDaysInOrder()

{

$ordered_days = array();

for( $i = 0; $i < 7; $i++ )

{

$ordered_days[] = $this->days[ ( $this->startDay + $i ) % 7

];

}

return $ordered_days;

}

Previous month

Most calendars display links to the next and previous month, making it easy for the

user to navigate between months For this to be done, we need to know the month

and if appropriate, year, of the next month and previous month.

Trang 4

We can easily get this information in integer form, by incrementing or decrementing

the current month, unless we are at an edge case, such as month 1, where we go to

12, and then decrease the year To make this as flexible as possible, we can simply

create a new calendar object, representing the previous month, and whichever

controller requires it can simply look up the month, month name, and year from

the object and display that to the user:

/**

* Get previous month

* @return Object calendar object

*/

public function getPreviousMonth()

{

$pm = new Calendar( '', ( ( $this->month > 1 ) ?

$this->month - 1 : 12 ), ( ( $this->month == 1 ) ?

$this->year-1 : $this->year ) );

return $pm;

}

Next month

As with the previous month, a method to return a calendar object for the next month:

/**

* Get next month

* @return Object calendar object

*/

public function getNextMonth()

{

$nm = new Calendar( '', ( ($this->month < 12 ) ?

$this->month + 1 : 1), ( ( $this->month == 12 ) ?

$this->year + 1 : $this->year ) );

return $nm;

}

Displaying a calendar

With our calendar library in place, we now need to look at how a controller would

leverage the power of the library to generate a particular month, and display it to

the user.

Trang 5

Generate and output

To actually display a calendar, we need some code which:

• Requires the calendar library

• Instantiates the object

• Generates the month

• Sends various bits of data to the template

• Outputs the template

We also need a template file with our 42 boxes in a calendar grid.

The following code can be used to generate a calendar (this isn't for a specific feature,

you can find the code in the testOutput() method in the calendar controller):

// require the class

require_once( FRAMEWORK_PATH 'lib/calendar/calendar.class.php'

);

// set the default month and year, i.e the current month and

year

$m = date('m');

$y = date('Y');

// check for a different Month / Year (i.e user has moved to

another month)

if( isset( $_GET['month'] ) )

{

$m = intval( $_GET['month']);

if( $m > 0 && $m < 13 )

{

}

else

{

$m = date('m');

}

}

if( isset( $_GET['year'] ) )

{

$y = intval( $_GET['year']);

}

// Instantiate the calendar object

Trang 6

$ny = $calendar->getNextMonth()->getYear();

$pm = $calendar->getPreviousMonth()->getMonth();

$py = $calendar->getPreviousMonth()->getYear();

// send next / previous month data to the template

$this->registry->getObject('template')->getPage()-

>addTag('nm', $nm );

$this->registry->getObject('template')->getPage()-

>addTag('pm', $pm );

$this->registry->getObject('template')->getPage()-

>addTag('ny', $ny );

$this->registry->getObject('template')->getPage()-

>addTag('py', $py );

// send the current month name and year to the template

$this->registry->getObject('template')->getPage()-

>addTag('month_name', $calendar->getMonthName() );

$this->registry->getObject('template')->getPage()-

>addTag('the_year', $calendar->getYear() );

// Set the start day of the week

$calendar->setStartDay(0);

// Get how many days there are in the month

// build the month, generate some data

$calendar->buildMonth();

// days

$this->registry->getObject('template')->dataToTags( $calendar-

>getDaysInOrder(),'cal_0_day_' );

// dates

$this->registry->getObject('template')->dataToTags( $calendar-

>getDates(),'cal_0_dates_' );

// styles

$this->registry->getObject('template')->dataToTags( $calendar-

>getDateStyles(),'cal_0_dates_style_' );

// data

$this->registry->getObject('template')->dataToTags( $calendar-

>getDateData(),'cal_0_dates_data_' );

$this->registry->getObject('template')->buildFromTemplates(

'test-calendar.tpl.php' );

In terms of the template, we need a grid of 42 potential calendar dates, each with a

template tag for a class, and a template tag for the date, and a template tag for any

potential data within.

Trang 7

The days of the week are also template tags, as we may wish to dynamically generate

them based off an individual user's preference, as highlighted below:

<html>

<body>

<h1> {month_name} {the_year} </h1>

<p><a href="calendar/?&amp;month={nm}&amp;year={ny}">Next</a>

<a href="calendar/?&amp;month={pm}&amp;year={py}">Previous</a></p>

<div>

<table id="ccc">

<tr>

<th class="weekend">{cal_0_day_0}</th>

<th class="">{cal_0_day_1}</th>

<th class="">{cal_0_day_2}</th>

<th class="">{cal_0_day_3}</th>

<th class="">{cal_0_day_4}</th>

<th class="">{cal_0_day_5}</th>

<th class="weekend">{cal_0_day_6}</th>

</tr>

<tr>

If we take a look at how an individual week (highlighted below) needs to be

represented, you can see that we prefix the template tag with cal_0_ (more on

that later), and that they range from 0 to 41 (42 boxes):

<td class="weekend {cal_0_dates_style_0}">

{cal_0_dates_0} {cal_0_dates_data_0}</td>

<td class="{cal_0_dates_style_1}">{cal_0_dates_1}

{cal_0_dates_data_1}</td>

<td class="{cal_0_dates_style_2}">{cal_0_dates_2}

{cal_0_dates_data_2}</td>

<td class="{cal_0_dates_style_3}">{cal_0_dates_3}

{cal_0_dates_data_3}</td>

<td class="{cal_0_dates_style_4}">{cal_0_dates_4}

{cal_0_dates_data_4}</td>

<td class="{cal_0_dates_style_5}">{cal_0_dates_5}

{cal_0_dates_data_5}</td>

<td class="weekend {cal_0_dates_style_6}">{cal_0_dates_6}

{cal_0_dates_data_6}</td>

</tr>

<tr>

Trang 8

<td class="{cal_0_dates_style_9}">{cal_0_dates_9}

{cal_0_dates_data_9}</td>

<td class="{cal_0_dates_style_10}">{cal_0_dates_10}

{cal_0_dates_data_10}</td>

<td class="{cal_0_dates_style_11}">{cal_0_dates_11}

{cal_0_dates_data_11}</td>

<td class="{cal_0_dates_style_12}">{cal_0_dates_12}

{cal_0_dates_data_12}</td>

<td class="weekend {cal_0_dates_style_13}">

{cal_0_dates_13} {cal_0_dates_data_13}</td>

</tr>

<tr>

<td class="weekend {cal_0_dates_style_14}">

{cal_0_dates_14} {cal_0_dates_data_14}</td>

<td class="{cal_0_dates_style_15}">{cal_0_dates_15}

{cal_0_dates_data_15}</td>

<td class="{cal_0_dates_style_16}">{cal_0_dates_16}

{cal_0_dates_data_16}</td>

<td class="{cal_0_dates_style_17}">{cal_0_dates_17}

{cal_0_dates_data_17}</td>

<td class="{cal_0_dates_style_18}">{cal_0_dates_18}

{cal_0_dates_data_18}</td>

<td class="{cal_0_dates_style_18}">{cal_0_dates_19}

{cal_0_dates_data_19}</td>

<td class="weekend {cal_0_dates_style_20}">

{cal_0_dates_20} {cal_0_dates_data_20}</td>

</tr>

<tr>

<td class="weekend {cal_0_dates_style_21}">

{cal_0_dates_21} {cal_0_dates_data_21}</td>

<td class="{cal_0_dates_style_22}">{cal_0_dates_22}

{cal_0_dates_data_22}</td>

<td class="{cal_0_dates_style_23}">{cal_0_dates_23}

{cal_0_dates_data_23}</td>

<td class="{cal_0_dates_style_24}">{cal_0_dates_24}

{cal_0_dates_data_24}</td>

<td class="{cal_0_dates_style_25}">{cal_0_dates_25}

{cal_0_dates_data_25}</td>

<td class="{cal_0_dates_style_26}">{cal_0_dates_26}

{cal_0_dates_data_26}</td>

<td class="weekend {cal_0_dates_style_27}">

{cal_0_dates_27} {cal_0_dates_data_27}</td>

</tr>

<tr>

<td class="weekend {cal_0_dates_style_28}">

{cal_0_dates_28} {cal_0_dates_data_28}</td>

<td class="{cal_0_dates_style_29}">{cal_0_dates_29}

{cal_0_dates_data_29}</td>

<td class="{cal_0_dates_style_30}">{cal_0_dates_30}

{cal_0_dates_data_30}</td>

Trang 9

<td class="{cal_0_dates_style_31}">{cal_0_dates_31}

{cal_0_dates_data_31}</td>

<td class="{cal_0_dates_style_32}">{cal_0_dates_32}

{cal_0_dates_data_32}</td>

<td class="{cal_0_dates_style_33}">{cal_0_dates_33}

{cal_0_dates_data_33}</td>

<td class="weekend {cal_0_dates_style_34}">

{cal_0_dates_34} {cal_0_dates_data_34}</td>

</tr>

<tr>

<td class="weekend {cal_0_dates_style_35}">

{cal_0_dates_35} {cal_0_dates_data_35}</td>

<td class="{cal_0_dates_style_36}">{cal_0_dates_36}

{cal_0_dates_data_36}</td>

<td class="{cal_0_dates_style_37}">{cal_0_dates_37}

{cal_0_dates_data_37}</td>

<td class="{cal_0_dates_style_38}">{cal_0_dates_38}

{cal_0_dates_data_38}</td>

<td class="{cal_0_dates_style_39}">{cal_0_dates_39}

{cal_0_dates_data_39}</td>

<td class="{cal_0_dates_style_40}">{cal_0_dates_40}

{cal_0_dates_data_40}</td>

<td class="weekend {cal_0_dates_style_41}">

{cal_0_dates_41} {cal_0_dates_data_41}</td>

</tr>

</table>

</body>

</html>

If we now go to /calendar/test/, we should see the following:

Trang 10

Multiple calendars

As our calendar returns information as arrays, we simply send it to the template

manager using the dataToTags method, with a prefix of our choice If we have

multiple instances of a calendar object, we can send them to different parts of a

template by changing the prefix For example, if we have a template with a large

calendar display for the current month (prefixed with cal_0_), a small calendar for

the previous month (prefixed with cal_1_) and a small calendar for the next month

(prefixed with cal_2_), we can put the three on the calendar with code such as

the following:

$calendar = new Calendar( '', $m, $y );

// build the month, generate some data

$calendar->buildMonth();

// days

$this->registry->getObject('template')->dataToTags( $calendar-

>getDaysInOrder(),'cal_0_day_' );

// dates

$this->registry->getObject('template')->dataToTags( $calendar-

>getDates(),'cal_0_dates_' );

// styles

$this->registry->getObject('template')->dataToTags( $calendar-

>getDateStyles(),'cal_0_dates_style_' );

// data

$this->registry->getObject('template')->dataToTags( $calendar-

>getDateData(),'cal_0_dates_data_' );

$calendarPrevious = $calendar->getPreviousMonth();

// build the month, generate some data

$calendarPrevious->buildMonth();

// days

$this->registry->getObject('template')->dataToTags(

$calendarPrevious->getDaysInOrder(),'cal_1_day_' );

// dates

$this->registry->getObject('template')->dataToTags(

$calendarPrevious->getDates(),'cal_1_dates_' );

// styles

$this->registry->getObject('template')->dataToTags(

$calendarPrevious->getDateStyles(),'cal_1_dates_style_' );

// data

$this->registry->getObject('template')->dataToTags(

$calendarPreviousndar->getDateData(),'cal_1_dates_data_' );

$calendarNext = $calendar->getNextMonth();

// build the month, generate some data

Ngày đăng: 04/07/2014, 21:20

TỪ KHÓA LIÊN QUAN