Events and Birthdays [ 292 ] Template for a new event Our "Create event" template simply needs elds for the name, date, start time, end time, description, and checkboxes for all of the user's friends. We can give the date eld a class of selectdate, to make use of the jQuery datepicker plug-in we used for proles. We may also wish to use the time picker plug-in too, to make time selection easier. The friends loop is highlighted in the following code: <div id="main"> <div id="rightside"> </div> <div id="content"> <h1>Create an event</h1> <form action="event/create" method="post"> <label for="">Name</label><br /> <input type="text" name="name" /><br /> <label for="">Type of event</label><br /> <select name="type"> <option value="public">Public event</option> <option value="private">Private event</option> </select><br /> <label for="">Date</label><br /> <input type="text" class="selectdate" name="date" /><br /> <label for="">Start time</label><br /> <input type="text" class="selecttime" name="start_ time" /><br /> <label for="">End time</label><br /> <input type="text" class="selecttime" name="end_ time" /><br /> <label for="">Description</label><br /> <textarea name="description" cols="45" rows="6"> </ textarea><br /> <h2>Invite friends?</h2> <p>Select any friends you would like to invite to the event.</p> <! START all ><input type="checkbox" name="invitees[]" value="{ID}" />{users_name} <! END all ><br /> Download from Wow! eBook <www.wowebook.com> Chapter 9 [ 293 ] <input type="submit" name="" value="Create event" /> </form> </div> </div> Now, if we visit the create event page (event/create), we see the form: This makes use of the very helpful datepicker plugin: Download from Wow! eBook <www.wowebook.com> Events and Birthdays [ 294 ] Calendar of events We looked earlier at integrating the calendar library with birthdays, so why not try and integrate the events with the calendar too? The code is in the download bundle for this book if you need help. Viewing an event To view an event, we simply call the toTags method on the model, and build the template. We should also list users who have been invited, are attending, are not attending, and who are maybe attending: /** * View an event * @param int $id * @return void */ private function viewEvent( $id ) { require_once( FRAMEWORK_PATH . 'models/event.php' ); $event = new Event( $this->registry, $id ); $show = true; if( $event->getType() == 'private' ) { // you may wish to add to support for private events here! $show = false; } if( $show == true ) { $event->toTags( 'event_' ); $this->registry->getObject('template')->buildFromTemplates( 'header.tpl.php', 'events/view.tpl.php', 'footer.tpl.php' ); $attendingCache = $event->getAttending(); $this->registry->getObject('template')->getPage()->addTag( 'attending', array('SQL', $attendingCache) ); $notAttendingCache = $event->getNotAttending(); $this->registry->getObject('template')->getPage()->addTag( 'notattending', array('SQL', $notAttendingCache) ); $maybeAttendingCache = $event->getMaybeAttending(); $this->registry->getObject('template')->getPage()->addTag( 'maybeattending', array('SQL', $maybeAttendingCache) ); $invitedCache = $event->getInvited(); $this->registry->getObject('template')->getPage()->addTag( 'invited', array('SQL', $invitedCache) ); Download from Wow! eBook <www.wowebook.com> Chapter 9 [ 295 ] $sql = "SELECT * FROM event_attendees WHERE event_id={$id} AND user_id=" . $this->registry->getObject('authenticate')- >getUser()->getUserId(); $this->registry->getObject('db')->executeQuery( $sql ); if( $this->registry->getObject('db')->numRows() == 1 ) { $data = $this->registry->getObject('db')->getRows(); if( $data['status'] == 'going' ) { $s = 'attending'; } elseif( $data['status'] == 'not going' ) { $s = 'notattending'; } elseif( $data['status'] == 'maybe' ) { $s = 'maybeattending'; } else { $s = 'unknown'; } $this->registry->getObject('template')->getPage()- >addTag( $s . '_select', "selected='selected'"); } else { $this->registry->getObject('template')->getPage()- >addTag('unknown_select', "selected='selected'"); } } else { // error handling } } Download from Wow! eBook <www.wowebook.com> Events and Birthdays [ 296 ] Event template The template for viewing the event contains tags for the event, and template loops for the different types of attendee status: <div id="main"> <div id="rightside"> </div> <div id="content"> <h1>{event_name}</h1> <p>{event_description}</p> <p>{event_date}: {event_start_time} until {event_end_time}</p> <h2>Your attendance</h2> <p>You are currently recorded as:</p> <form action="event/change-attendance/{event_id}" method="post"> <select name="status"> <option value="" {unknown_select}>Unknown - Please select </option> <option value="going" {attending_ select}>Attending</option> <option value="not going" {notattending_ select}>Not attending</option> <option value="maybe" {maybeattending_ select}>Maybe attending</option> </select> <input type="submit" name="" value="Update attendance" /> </form> <h2>Attending</h2> <ul> <! START attending ><li>{name}</li> <! END attending > </ul> <h2>Invited / Awaiting Reply</h2> <ul> <! START invited ><li>{name}</li> <! END invited > </ul> <h2>Maybe attending</h2> <ul> <! START maybeattending ><li>{name}</li> <! END maybeattending > </ul> <h2>Not attending</h2> <ul> <! START notattending ><li>{name}</li> Download from Wow! eBook <www.wowebook.com> Chapter 9 [ 297 ] <! END notattending > </ul> </div> </div> Viewing an event in action If we now view an event (event/view/1), we are presented with the view event screen: Upcoming events Thanks to the listUpcomingInNetwork method in our events model, we can easily build a list of events in our user's network: private function listUpcomingInNetwork() { require_once( FRAMEWORK_PATH . 'models/events.php' ); $events = new Events( $this->registry ); $cache = $events->listEventsFuture( $this->registry- >getObject('authenticate')->getUser()->getID(), 30 ); $this->registry->getObject('template')->getPage()- >addTag( 'events', array( 'SQL', $cache ) ); $this->registry->getObject('template')->buildFromTemplates( 'header.tpl.php', 'events/upcoming.tpl.php', 'footer.tpl.php' ); } Download from Wow! eBook <www.wowebook.com> Events and Birthdays [ 298 ] Reminders We may wish to remind users about specic events, either events created by their contacts, or events that they have indicated they wish to attend. We can easily remind them of the event either through: • Notices on the site reminding them • E-mails reminding them • Text/SMS messages Off-site reminders would need to work via a task-based system, for example a CRON job that is run each night, which looks up events a user is attending or may be attending X days in the future (those queries in the events model are looking extra handy now!) and sends them a reminder. On-site notications On-site reminders would simply call the appropriate method in the events model, to see whether there are any events the user may be attending within the next X days, and if there are, list them on the screen. E-mail notications We have an e-mail sending object in our registry; we can use this to e-mail out reminders to our users. SMS notications For us to send SMS notications, we need to use a mobile gateway, such as: • Clickatell: Large international SMS/mobile gateway - http://www. clickatell.com/ • Intellisoftware: UK based SMS gateway—http://www.intellisoftware. co.uk/ Such gateways provide simple APIs where a text message can be sent via a simple HTTP request containing API credentials, sender number, recipient number, and the message. We can create a library to talk to the API, and use this to send messages to our users. Download from Wow! eBook <www.wowebook.com> Chapter 9 [ 299 ] Packt have published a book called Mobile Web Development by Nirav Mehta (https://www.packtpub.com/mobile-web-development/book), which includes lots of great examples of clickatell, and well worth a read if you are considering implementing SMS integration. The Clickatell website and Intellisoftware websites both have in-depth information on working with their APIs too. Summary In this chapter, we have looked at developing a exible calendar library to integrate calendar functionality into various aspects of our site. We created a calendar controller to display birthdays of our users' connections, along with their ages. We then moved on to events, creating models and controllers for creating events, viewing events, listing events, and inviting connections to the events. With our calendar library, we are now able to integrate these events into the calendar. Following from events, we looked at inviting contacts to events, and how they can update us on their intention to attend the event, and with a little extra development, we can integrate reminders too. Now, we can move onto the nal core feature of any social network—groups! Download from Wow! eBook <www.wowebook.com> Download from Wow! eBook <www.wowebook.com> Groups We only have one user-facing feature left to implement for our Dino Space website, and that is groups within the social network. Most social networking websites have the ability for users to form their own sub-groups, a small sub-set of users discussing or sharing information about specic things. Within our site, this could be to allow groups of users to privately discuss matters, or for users who share common interests to discuss things without cluttering up everyone else's network with information that may not interest them. In this chapter you will learn: • How to create a group area • How to allow users to create groups • How users can be invited to join these groups • How to deal with the "ownership" of a particular group Let's get started and add groups to our social network! Some planning Before we can start implementing this feature, we need to think about what information it needs to manage, how it will work, and what it will offer to our users. Download from Wow! eBook <www.wowebook.com> . $this->registry->getObject('authenticate' )- >getUser( )-& gt;getUserId(); $this->registry->getObject('db' )-& gt;executeQuery( $sql ); if( $this->registry->getObject('db' )-& gt;numRows(). 'models/events .php& apos; ); $events = new Events( $this->registry ); $cache = $events->listEventsFuture( $this->registry- >getObject('authenticate' )-& gt;getUser( )-& gt;getID(),. $this->registry->getObject('template' )-& gt;getPage( )- >addTag( 'events', array( 'SQL', $cache ) ); $this->registry->getObject('template' )-& gt;buildFromTemplates(