If the group is active, the next stage is to check that the currently logged in user is either the creator of the group, or is a member of the group. require_once( FRAMEWORK_PATH . 'models/groupmembership.php'); $gm = new Groupmembership( $this->registry ); $user = $this->registry->getObject('authenticate')>getUser()->getUserID(); $gm->getByUserAndGroup( $user, $this->groupID ); if( $this->group->getCreator() == $user || $gm>getApproved() ) {
If the currently logged in user is a member of the group, or its creator, then the controller looks up the full details of the user's request, and passes control to the appropriate method. if( isset( $urlBits[2] ) ) { switch( $urlBits[2] ) { case 'create-topic': $this->createTopic(); break; case 'view-topic': $this->viewTopic( intval( $urlBits[3] ) ); break; case 'reply-to-topic': $this->replyToTopic( intval( $urlBits[3] ) ); break; case 'membership': $this->manageMembership( intval( $urlBits[3] ) ); break;
If the user isn't a member of the group, control is passed to a secondary controller (we will create it shortly) which, if appropriate, will provide a mechanism for the user to join the group. require_once( FRAMEWORK_PATH . 'controllers/group/membership.php'); $membership = new Membershipcontroller( $this->registry, $this->groupID ); $membership->join(); } } else
{ $this->registry->errorPage( 'Group not found', 'Sorry, the group you requested was not found' ); } } else { $this->registry->errorPage( 'Group not found', 'Sorry, the group you requested was not found' ); } } else { $this->registry->errorPage( 'Please login', 'Sorry, you must be logged in to view groups' ); } }
If the user's request was to view a group, then the group model data is sent to the template engine, the template is built, and the cache of topics from the group is generated and also sent to the template engine. private function viewGroup() { $this->group->toTags( 'group_' ); $this->registry->getObject('template')->buildFromTemplates( 'header.tpl.php', 'groups/view.tpl.php', 'footer.tpl.php' ); $cache = $this->group->getTopics(); $this->registry->getObject('template')->getPage()->addTag( 'topics', array( 'SQL', $cache ) ); }
private function viewTopic( $topic ) { [ 333 ]
Download from Wow! eBook <www.wowebook.com>
Groups // next part of this chapter } private function replyToTopic( $topic ) { // next part of this chapter } private function manageMembership() { if( $group->getCreator() == $this->registry>getObject('authenticate')->getUser()->getUserID() ) { require_once( FRAMEWORK_PATH . 'controllers/group/membership.php'); $membership = new Membershipcontroller( $this->registry, $this>groupID ); $membership->manage(); } else { $this->registry->errorPage( 'Permission denied', 'Only the gorup creator can manage membership' ); } } }
?>
View
The template (views/default/templates/groups/view.tpl.php) for viewing a group is fairly straightforward, containing some information on the group and the topics within it. <div id="main"> <div id="rightside"> <ul> <li><a href="group/{group_id}/create-topic">Create new topic</a></li> </ul> </div> <div id="content">
To view a topic, we simply add the following method to our controller: /** * View a topic within the group * @return void */ private function viewTopic( $topic ) { $this->group->toTags( 'group_' ); require_once( FRAMEWORK_PATH . 'models/topic.php' ); $topic = new Topic( $this->registry, $topic ); if( $topic->getGroup() == $this->groupID ) { $topic->toTags( 'topic_' ); $sql = $topic->getPostsQuery();
To reply to a topic we simply add the following method to our controller: /** * Reply to a topic within a group * @param int $topic * @return void */ private function replyToTopic( $topici ) { $this->group->toTags( 'group_' ); require_once( FRAMEWORK_PATH . 'models/topic.php' ); $topic = new Topic( $this->registry, $topici ); if( $topic->getGroup() == $this->groupID ) [ 337 ]
Download from Wow! eBook <www.wowebook.com>
Groups { require_once( FRAMEWORK_PATH . 'models/post.php' ); $post = new Post( $this->registry, 0 ); $user = $this->registry->getObject('authenticate')->getUser()>getUserID(); $post->setPost( $this->registry->getObject('db')->sanitizeData( $_POST['post'] ) ); $post->setCreator( $user ); $post->setTopic( $topici ); $post->save(); $this->registry->redirectUser( $this->registry->buildURL(array( 'group', $this->groupID, 'view-topic', $topici ), '', false ), 'Reply saved', 'Thanks, the topic topic reply has been saved', false ); } else { $this->registry->errorPage( 'Invalid topic', 'Sorry, you tried to view an invalid topic'); } }
View
For each of the three methods we have created, we now need to create template files for them (only two, as reply and view use the same template).
Creating a topic
This template file (Views/default/templates/groups/create-topic.tpl.php) is simply a form with fields for the name of the topic and the contents of the first post. <div id="main"> <div id="rightside"> <ul> <li><a href="group/{group_id}">{group_name}</a></li> </ul> </div> <div id="content">
This template file (Views/default/templates/groups/view-topic.tpl.php) simply contains a loop of template tags representing the posts within the topic, as well as information on the topic itself. <div id="main"> <div id="rightside"> <ul> <li><a href="group/{group_id}">{group_name}</a></li> <li><a href="group/{group_id}/create-topic">Create topic</a></li> </ul> </div> <div id="content">
{topic_name}
<!-- START posts -->
{post}
<em>Posted by {creator_friendly_post} on {friendly_created_post}</em>
Let's now take a look at viewing a topic from within one of our newly-created groups.
Joining a group
Now that our users can create groups, view groups, and communicate within groups, we need to provide users the ability to join groups, request admission to groups, or allow group members to send out invitations to join a group. In this chapter we will simply look at members joining public groups. Feel free to extend this to meet the needs of your social network, and similarly include membership management options.
Joining (public) groups
Our group controller automatically passes control to a secondary controller if the user was not a member of the group, or was not the group's creator. This secondary controller can detect the type of group, and then display information regarding joining, or in the case of public groups, automatically sign them up.
This secondary controller is controllers/group/membership.php. class Membershipcontroller { private $registry; private $groupID; [ 340 ]
Download from Wow! eBook <www.wowebook.com>
Chapter 10 private $group; public function __construct( Registry $registry, $groupID ) { $this->registry = $registry; $this->groupID = $groupID; require_once( FRAMEWORK_PATH . 'models/group.php'); $this->group = new Group( $this->registry, $this->groupID ); } public function join() { $type = $this->group->getType(); switch( $type ) { case 'public': $this->autoJoinGroup(); break; } } private function autoJoinGroup()
{ require_once( FRAMEWORK_PATH . 'models/groupmembership.php'); $gm = new Groupmembership( $this->registry, 0 ); $user = $this->registry->getObject('authenticate')->getUser()>getUserID(); $gm->getByUserAndGroup( $user, $this->groupID ); if( $gm->isValid() ) { $gm = new Groupmembership( $this->registry, $gm->getID() ); } $gm->setApproved( 1 ); $gm->save(); $this->registry->errorPage('New membership', 'Thanks, you have now joined the group'); } } ?>