Tham khảo tài liệu ''thiết kế mạng xã hội với php - 30'', 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 $calendarNext->buildMonth(); // days $this->registry->getObject('template')->dataToTags( $calendarNext->getDaysInOrder(),'cal_2_day_' ); // dates $this->registry->getObject('template')->dataToTags( $calendarNext->getDates(),'cal_2_dates_' ); // styles $this->registry->getObject('template')->dataToTags( $calendarNext->getDateStyles(),'cal_2_dates_style_' ); // data $this->registry->getObject('template')->dataToTags( $calendarNext->getDateData(),'cal_2_dates_data_' ); With events To display event information alongside dates in the calendar, we simply need to pass an array of data to our calendar library, indicating which days have events, and we can also pass the data itself for inclusion in the calendar: // pass data to the calendar for inclusion $calendar->setData( $data ); // tell the calendar which days should be highlighted $calendar->setDaysWithEvents($days); We simply pass an array of dates that have events, and an array of event data Both arrays use the day of the month as the key Birthdays Birthdays should be a fairly straightforward feature for us to implement We need to: • Add an additional profile field for the user's birthday • Look up the date of birth of our user's contacts • Calculate their age • Send the data to the calendar • Generate the calendar view • Display a list of upcoming birthdays to our users [ 272 ] Download from Wow! eBook Chapter Getting relationship IDs For us to quickly look up the birthdays of a user's connections, we need to quickly gain access to the IDs of users another user is connected to We have some similar functionality in our relationships model (models/relationships.php); however, the following method will give us a query returning IDs We can then use this as a sub query in our birthdays lookup query: /** * Get IDs of users a user has a relationship with * @param int $user the user in question * @param bool $cache - cache the results, or return the query? * @return String / int */ public function getIDsByUser( $user, $cache=false ) { $sql = "SELECT u.ID FROM users u, profile p, relationships r, relationship_types t WHERE t.ID=r.type AND r.accepted=1 AND (r.usera={$user} OR r.userb={$user}) AND IF( r.usera={$user},u ID=r.userb,u.ID=r.usera) AND p.user_id=u.ID"; if( $cache == false ) { return $sql; } else { $cache = $this->registry->getObject('db')>cacheQuery( $sql ); return $cache; } } Setting up the calendar As we have done before, we need to set up the calendar (controllers/calendar/ controller.php); this simply involves requiring the class, instantiating the object, and setting the current date and month: // 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) [ 273 ] Download from Wow! eBook Events and Birthdays 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(); $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); // require the class [ 274 ] Download from Wow! eBook Chapter Getting the birthdays With the calendar setup, and a method in our relationships model for us to get the IDs of connected users, we can get the birthdays First, we need to require the relationships model, instantiate the object, and get the IDs SQL: require_once( FRAMEWORK_PATH 'models/relationships.php'); $relationships = new Relationships( $this->registry ); $idsSQL = $relationships->getIDsByUser( $this->registry>getObject('authenticate')->getUser()->getUserID() ); Next, we query the profile table, only selecting profiles that are in the result set of the IDs query We look for users with birthdays in the currently selected month, and calculate the age by subtracting the year of birth from the current year: $sql = "SELECT DATE_FORMAT(pr.user_dob, '%d' ) as profile_dob, pr.name as profile_name, pr.user_id as profile_id, ( ( YEAR( CURDATE() ) ) - ( DATE_FORMAT(pr.user_dob, '%Y' ) ) ) as profile_new_age FROM profile pr WHERE pr.user_id IN (".$idsSQL.") AND pr.user_dob LIKE '%-{$m}-%'"; $this->registry->getObject('db')->executeQuery( $sql ); Passing them to the calendar We then iterate through our results from the query, building an array of dates with events, and an array of event data: $dates = array(); $data = array(); if( $this->registry->getObject('db')->numRows() > ) { while( $row = $this->registry->getObject('db')->getRows() ) { $dates[] = $row['profile_dob']; $data[ intval($row['profile_dob']) ] = "" $row['profile_name']." (" $row['profile_new_age'] ")"; } } The data is then passed to the calendar: $calendar->setData( $data ); // tell the calendar which days should be highlighted $calendar->setDaysWithEvents($dates); [ 275 ] Download from Wow! eBook Events and Birthdays With everything passed to the calendar, we can now build the month and pass everything to the template: $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( 'header.tpl.php', 'bd-calendar.tpl.php', 'footer.tpl.php' ); The results If we now visit our birthdays calendar (calendar/birthdays), we have a calendar with the birthdays of our contacts shown, along with their age on that date [ 276 ] Download from Wow! eBook Chapter Events With calendar functionality and birthday lookups in place, adding event functionality should be straightforward Let's take a look at what we will need: • An event model °° To generate data from an event °° To create and edit an event • Integration with our calendar controller • An event controller to display the event details, manage the creation of the event, and manage attendees and RSVPs Event model As with all of our models, the first stage is to define the variables, which relate to the registry, the fields for the event in the database, and an array of invitees to the event: