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

Tạo mạng xã hội với PHP - part 36 pps

10 204 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 3,29 MB

Nội dung

Groups [ 332 ] 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; default: $this->viewGroup(); break; } } else { $this->viewGroup(); } } else { This material is copyright and is licensed for the sole use by RAYMOND ERAZO on 25th October 2010 3146 KERNAN LAKE CIRCLE, JACKSONVILLE, 32246 Download from www.eBookTM.com Chapter 10 [ 333 ] 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 ) { This material is copyright and is licensed for the sole use by RAYMOND ERAZO on 25th October 2010 3146 KERNAN LAKE CIRCLE, JACKSONVILLE, 32246 Download from www.eBookTM.com Groups [ 334 ] // 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"> <h1>{group_name}</h1> This material is copyright and is licensed for the sole use by RAYMOND ERAZO on 25th October 2010 3146 KERNAN LAKE CIRCLE, JACKSONVILLE, 32246 Download from www.eBookTM.com Chapter 10 [ 335 ] <p>{group_description}</p> <h2>Topics</h2> <table> <tr> <th>Topic</th><th>Creator</th> <th>Created</th><th>Posts</th> </tr> <! START topics > <tr> <td><a href="group/{group_id}/view- topic/{ID}">{name}</a></td><td>{creator_name}</td> <td>{created_friendly}</td><td>{posts}</td> </tr> <! END topics > </table> </div> </div> In action Let's take a look at viewing a group in action (group/1/): We have our group information displayed, with the topics listed underneath. Discussing within a group Now that we can create and view groups, we need to integrate the discussion features which will be facilitated thanks to our topic and post models. This material is copyright and is licensed for the sole use by RAYMOND ERAZO on 25th October 2010 3146 KERNAN LAKE CIRCLE, JACKSONVILLE, 32246 Download from www.eBookTM.com Groups [ 336 ] Group controller additions Most of this functionality can be added in by adding suitable code to our group controller. Creating a topic To create a topic, we simply add the following method to the controller: /** * Create a new topic within the group * @return void */ private function createTopic() { if( isset( $_POST ) && is_array( $_POST ) && count( $_POST ) > 0 ) { require_once( FRAMEWORK_PATH . 'models/topic.php' ); $topic = new Topic( $this->registry, 0 ); $topic->includeFirstPost( true ); $user = $this->registry->getObject('authenticate')->getUser()- >getUserID(); $topic->setCreator( $user ); $topic->setGroup( $this->groupID ); $topic->setName( $this->registry->getObject('db')- >sanitizeData( $_POST['name'] ) ); $topic->getFirstPost()->setCreator( $user ); $topic->getFirstPost()->setPost( $this->registry- >getObject('db')->sanitizeData( $_POST['name'] ) ); $topic->save(); $this->registry->redirectUser( $this->registry->buildURL(array( 'group', $this->groupID ), '', false ), 'Topic created', 'Thanks, the topic has been created', false ); } else { $this->group->toTags( 'group_' ); $this->registry->getObject('template')->buildFromTemplates( 'header.tpl.php', 'groups/create-topic.tpl.php', 'footer.tpl.php' ); } } This material is copyright and is licensed for the sole use by RAYMOND ERAZO on 25th October 2010 3146 KERNAN LAKE CIRCLE, JACKSONVILLE, 32246 Download from www.eBookTM.com Chapter 10 [ 337 ] Viewing a topic 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(); $cache = $this->registry->getObject('db')->cacheQuery( $sql ); $this->registry->getObject('template')->getPage()- >addTag('posts', array( 'SQL', $cache ) ); $this->registry->getObject('template')->buildFromTemplates( 'header.tpl.php', 'groups/view-topic.tpl.php', 'footer.tpl.php' ); } else { $this->registry->errorPage( 'Invalid topic', 'Sorry, you tried to view an invalid topic'); } } Replying to a topic 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 ) This material is copyright and is licensed for the sole use by RAYMOND ERAZO on 25th October 2010 3146 KERNAN LAKE CIRCLE, JACKSONVILLE, 32246 Download from www.eBookTM.com Groups [ 338 ] { 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 les for them (only two, as reply and view use the same template). Creating a topic This template le (Views/default/templates/groups/create-topic.tpl.php) is simply a form with elds for the name of the topic and the contents of the rst post. <div id="main"> <div id="rightside"> <ul> <li><a href="group/{group_id}">{group_name}</a></li> </ul> </div> <div id="content"> <h1>Create a new topic</h1> <form action="group/{group_id}/create-topic" method="post"> <label for="name">Topic Name</label><br /> <input type="text" id="name" name="name" value="" /><br /> <label for="post">First Post</label><br /> This material is copyright and is licensed for the sole use by RAYMOND ERAZO on 25th October 2010 3146 KERNAN LAKE CIRCLE, JACKSONVILLE, 32246 Download from www.eBookTM.com Chapter 10 [ 339 ] <textarea id="post" name="post"></textarea><br /> <input type="submit" id="create" name="create" value="Create topic" /> </form> </div> </div> Viewing a topic This template le (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"> <h1>{topic_name}</h1> <! START posts > <p>{post}</p> <p><em>Posted by {creator_friendly_post} on {friendly_created_post}</em></p> <hr /> <! END posts > <h2>Reply to this topic</h2> <form action="group/{group_id}/reply-to-topic/{topic_id}" method="post"> <textarea id="post" name="post"> </textarea> <input type="submit" id="np" name="np" value="Reply" /> </form> </div> </div> This material is copyright and is licensed for the sole use by RAYMOND ERAZO on 25th October 2010 3146 KERNAN LAKE CIRCLE, JACKSONVILLE, 32246 Download from www.eBookTM.com Groups [ 340 ] Discussion in action—viewing a topic 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. <?php class Membershipcontroller { private $registry; private $groupID; This material is copyright and is licensed for the sole use by RAYMOND ERAZO on 25th October 2010 3146 KERNAN LAKE CIRCLE, JACKSONVILLE, 32246 Download from www.eBookTM.com Chapter 10 [ 341 ] 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'); } } ?> This material is copyright and is licensed for the sole use by RAYMOND ERAZO on 25th October 2010 3146 KERNAN LAKE CIRCLE, JACKSONVILLE, 32246 Download from www.eBookTM.com . $this->group->toTags( 'group_' ); $this->registry->getObject('template' )-& gt;buildFromTemplates( 'header.tpl .php& apos;, 'groups/create-topic.tpl .php& apos;,. 'models/post .php& apos; ); $post = new Post( $this->registry, 0 ); $user = $this->registry->getObject('authenticate' )-& gt;getUser( )- >getUserID(); $post->setPost( $this->registry->getObject('db' )-& gt;sanitizeData(. $user, $this->groupID ); if( $gm->isValid() ) { $gm = new Groupmembership( $this->registry, $gm->getID() ); } $gm->setApproved( 1 ); $gm->save(); $this->registry->errorPage('New

Ngày đăng: 04/07/2014, 22:20

w