Tải bản đầy đủ (.pdf) (10 trang)

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

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (5.39 MB, 10 trang )

Groups

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
{
[ 332 ]

Download from Wow! eBook <www.wowebook.com>


Chapter 10

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">

{group_name}


[ 334 ]

Download from Wow! eBook <www.wowebook.com>


Chapter 10

{group_description}


Topics


<table>
<tr>
<th>Topic</th><th>Creator</th>
<th>Created</th><th>Posts</th>
</tr>
<!-- START topics -->

<tr>
<td><a href="group/{group_id}/viewtopic/{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.

[ 335 ]

Download from Wow! eBook <www.wowebook.com>


Groups

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' );
}
}

[ 336 ]

Download from Wow! eBook <www.wowebook.com>


Chapter 10

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 )
[ 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">

Create a new topic


<form action="group/{group_id}/create-topic" method="post">
<label for="name">Topic Name</label>

<input type="text" id="name" name="name" value="" />

<label for="post">First Post</label>

[ 338 ]

Download from Wow! eBook <www.wowebook.com>


Chapter 10
<textarea id="post" name="post"></textarea>

value="Create topic" />
</form>
</div>

</div>

Viewing a topic

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>


<hr />
<!-- END posts -->

Reply to this topic


method="post">
<textarea id="post" name="post">
</textarea>
<input type="submit" id="np" name="np" value="Reply" />
</form>

</div>
</div>

[ 339 ]

Download from Wow! eBook <www.wowebook.com>


Groups

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.
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');
}
}
?>

[ 341 ]

Download from Wow! eBook <www.wowebook.com>



×