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

10 246 0
Thiết kế mạng xã hội với PHP - 30 doc

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

Thông tin tài liệu

Events and Birthdays [ 272 ] $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 prole eld 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 Download from Wow! eBook <www.wowebook.com> Chapter 9 [ 273 ] 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) Download from Wow! eBook <www.wowebook.com> Events and Birthdays [ 274 ] 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 $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 Download from Wow! eBook <www.wowebook.com> Chapter 9 [ 275 ] 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 prole table, only selecting proles 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() > 0 ) { while( $row = $this->registry->getObject('db')->getRows() ) { $dates[] = $row['profile_dob']; $data[ intval($row['profile_dob']) ] = "<br />". $row['profile_name']." (". $row['profile_new_age'] . ")<br />"; } } The data is then passed to the calendar: $calendar->setData( $data ); // tell the calendar which days should be highlighted $calendar->setDaysWithEvents($dates); Download from Wow! eBook <www.wowebook.com> Events and Birthdays [ 276 ] 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. Download from Wow! eBook <www.wowebook.com> Chapter 9 [ 277 ] 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 rst stage is to dene the variables, which relate to the registry, the elds for the event in the database, and an array of invitees to the event: <?php /** * Event model */ class Event{ /** * The registry */ private $registry; /** * Event ID */ private $ID; /** * Creators ID */ private $creator; /** * Event name */ Download from Wow! eBook <www.wowebook.com> Events and Birthdays [ 278 ] private $name; /** * Description */ private $description; /** * Event date */ private $event_date; /** * start time */ private $start_time; /** * End time */ private $end_time; /** * Type */ private $type; /** * Active */ private $active; /** * Invitees */ private $invitees = array(); Next, we have our constructor, which sets the registry, and if an ID is passed, looks up the event and populates the model's elds with those from the database: /** * Event constructor * @param Registry $registry the registry * @param int $ID the event ID * @return voID Download from Wow! eBook <www.wowebook.com> Chapter 9 [ 279 ] */ public function __construct( Registry $registry, $ID=0 ) { $this->registry = $registry; if( $ID != 0 ) { $this->ID = $ID; // if an ID is passed, populate based off that $sql = "SELECT * FROM events WHERE ID=" . $this->ID; $this->registry->getObject('db')->executeQuery( $sql ); if( $this->registry->getObject('db')->numRows() == 1 ) { $data = $this->registry->getObject('db')->getRows(); // populate our fields foreach( $data as $key => $value ) { $this->$key = $value; } } } } Next, we have setters for all of our elds, except for the ID: /** * Sets the events name * @param String $name * @return voID */ public function setName( $name ) { $this->name = $name; } /** * Sets the creator * @param int $creator the creator * @return voID */ public function setCreator( $ID ) { $this->creator = $ID; } Download from Wow! eBook <www.wowebook.com> Events and Birthdays [ 280 ] public function setInvitees( $invitees ) { $this->invitees = $invitees; } /** * Set the events description * @param String $description the description */ public function setDescription( $description ) { $this->description = $description; } Our setDate method takes an additional parameter to check whether the controller formatted the date, if it didn't then we assume it was passed in a format such as the default jQuery datepicker format, and format it here: /** * Set the event date * @param String $date the date * @param boolean $formatted - indicates if the controller has formatted the date, or if we need to do it here */ public function setDate( $date, $formatted=true ) { if( $formatted == true ) { $this->event_date = $date; } else { $temp = explode('/', $date ); $this->event_date = $temp[2].'-'.$temp[1].'-'.$temp[0]; } } /** * Sets the start time of the event * @param String $time * return voID */ public function setStartTime( $time ) { $this->start_time = $time; Download from Wow! eBook <www.wowebook.com> Chapter 9 [ 281 ] } /** * Sets the end time of the event * @param String $time * return voID */ public function setEndTime( $time ) { $this->end_time = $time; } The setType method takes an additional parameter to check whether the controller validated the type or not; if it didn't, then we check it against our list of allowed types: /** * Set the type of the event * @param String $type the type * @param boolean $checked - indicates if the controller has valIDated the type, or if we need to do it * @return voID */ public function setType( $type, $checked=true ) { if( $checked == true ) { $this->type = $type; } else { $types = array( 'public', 'private' ); if( in_array( $type, $types ) ) { $this->type = $type; } } } /** * Sets if the event is active * @param bool $active * @return voID */ public function setActive( $active ) { $this->active = $active; } Download from Wow! eBook <www.wowebook.com> . $calendar->getNextMonth( )-& gt;getMonth(); $ny = $calendar->getNextMonth( )-& gt;getYear(); $pm = $calendar->getPreviousMonth( )-& gt;getMonth(); $py = $calendar->getPreviousMonth( )-& gt;getYear(); . template $this->registry->getObject('template' )-& gt;getPage( )- >addTag('nm', $nm ); $this->registry->getObject('template' )-& gt;getPage( )- >addTag('pm',. $pm ); $this->registry->getObject('template' )-& gt;getPage( )- >addTag('ny', $ny ); $this->registry->getObject('template' )-& gt;getPage( )- >addTag('py',

Ngày đăng: 06/07/2014, 18:20

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan