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

Thiết kế mạng xã hội với PHP - 29

10 8 0

Đ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 5,39 MB

Nội dung

Tham khảo tài liệu ''thiết kế mạng xã hội với php - 29'', công nghệ thông tin, đồ họa - thiết kế - flash phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả

Events and Birthdays // 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 < || $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 == || $m == || $m == || $m == 11 ) { return 30; } [ 262 ] Download from Wow! eBook Chapter For any remaining month that isn't February (all the rest have 31), we return 31: else if( $m != ) { // 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 % != ) { // 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 != ) { // 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 != ) { // deja vu: which has 28 return 28; } else { [ 263 ] Download from Wow! eBook Events and Birthdays 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 ) % ]; } 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 [ 264 ] Download from Wow! eBook Chapter 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 > ) ? $this->month - : 12 ), ( ( $this->month == ) ? $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), ( ( $this->month == 12 ) ? $this->year + : $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 [ 265 ] Download from Wow! eBook Events and Birthdays 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 > && $m < 13 ) { } else { $m = date('m'); } } if( isset( $_GET['year'] ) ) { $y = intval( $_GET['year']); } // Instantiate the calendar object $calendar = new Calendar( '', $m, $y ); // Get next and previous month / year $nm = $calendar->getNextMonth()->getMonth(); [ 266 ] Download from Wow! eBook Chapter $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( >getDaysInOrder(),'cal_0_day_' ); // dates $this->registry->getObject('template')->dataToTags( >getDates(),'cal_0_dates_' ); // styles $this->registry->getObject('template')->dataToTags( >getDateStyles(),'cal_0_dates_style_' ); // data $this->registry->getObject('template')->dataToTags( >getDateData(),'cal_0_dates_data_' ); $calendar- $calendar- $calendar- $calendar- $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 [ 267 ] Download from Wow! eBook Events and Birthdays 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: {month_name} {the_year}

Next Previous

{cal_0_day_0} {cal_0_day_1} {cal_0_day_2} {cal_0_day_3} {cal_0_day_4} {cal_0_day_5} {cal_0_day_6} 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 to 41 (42 boxes): {cal_0_dates_0} {cal_0_dates_data_0} {cal_0_dates_1} {cal_0_dates_data_1} {cal_0_dates_2} {cal_0_dates_data_2} {cal_0_dates_3} {cal_0_dates_data_3} {cal_0_dates_4} {cal_0_dates_data_4} {cal_0_dates_5} {cal_0_dates_data_5} {cal_0_dates_6} {cal_0_dates_data_6} {cal_0_dates_7} {cal_0_dates_data_7} {cal_0_dates_8} {cal_0_dates_data_8} [ 268 ] Download from Wow! eBook Chapter {cal_0_dates_9} {cal_0_dates_data_9} {cal_0_dates_10} {cal_0_dates_data_10} {cal_0_dates_11} {cal_0_dates_data_11} {cal_0_dates_12} {cal_0_dates_data_12} {cal_0_dates_13} {cal_0_dates_data_13} {cal_0_dates_14} {cal_0_dates_data_14} {cal_0_dates_15} {cal_0_dates_data_15} {cal_0_dates_16} {cal_0_dates_data_16} {cal_0_dates_17} {cal_0_dates_data_17} {cal_0_dates_18} {cal_0_dates_data_18} {cal_0_dates_19} {cal_0_dates_data_19} {cal_0_dates_20} {cal_0_dates_data_20} {cal_0_dates_21} {cal_0_dates_data_21} {cal_0_dates_22} {cal_0_dates_data_22} {cal_0_dates_23} {cal_0_dates_data_23} {cal_0_dates_24} {cal_0_dates_data_24} {cal_0_dates_25} {cal_0_dates_data_25} {cal_0_dates_26} {cal_0_dates_data_26} {cal_0_dates_27} {cal_0_dates_data_27} {cal_0_dates_28} {cal_0_dates_data_28} {cal_0_dates_29} {cal_0_dates_data_29} {cal_0_dates_30} {cal_0_dates_data_30} [ 269 ] Download from Wow! eBook Events and Birthdays {cal_0_dates_31} {cal_0_dates_data_31} {cal_0_dates_32} {cal_0_dates_data_32} {cal_0_dates_33} {cal_0_dates_data_33} {cal_0_dates_34} {cal_0_dates_data_34} {cal_0_dates_35} {cal_0_dates_data_35} {cal_0_dates_36} {cal_0_dates_data_36} {cal_0_dates_37} {cal_0_dates_data_37} {cal_0_dates_38} {cal_0_dates_data_38} {cal_0_dates_39} {cal_0_dates_data_39} {cal_0_dates_40} {cal_0_dates_data_40} {cal_0_dates_41} {cal_0_dates_data_41} If we now go to /calendar/test/, we should see the following: [ 270 ] Download from Wow! eBook Chapter 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( >getDaysInOrder(),'cal_0_day_' ); // dates $this->registry->getObject('template')->dataToTags( >getDates(),'cal_0_dates_' ); // styles $this->registry->getObject('template')->dataToTags( >getDateStyles(),'cal_0_dates_style_' ); // data $this->registry->getObject('template')->dataToTags( >getDateData(),'cal_0_dates_data_' ); $calendar- $calendar- $calendar- $calendar- $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 [ 271 ] Download from Wow! eBook ... // data $this->registry->getObject('template' )-> dataToTags( >getDateData(),'cal_0_dates_data_' ); $calendar- $calendar- $calendar- $calendar- $this->registry->getObject('template' )-> buildFromTemplates(... template $this->registry->getObject('template' )-> getPage()>addTag('month_name', $calendar->getMonthName() ); $this->registry->getObject('template' )-> getPage()>addTag('the_year', $calendar->getYear()... $calendar- $calendar- $calendar- $calendar- $calendarPrevious = $calendar->getPreviousMonth(); // build the month, generate some data $calendarPrevious->buildMonth(); // days $this->registry->getObject('template' )-> dataToTags(

Ngày đăng: 08/05/2021, 17:31