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

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

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.4 MB, 10 trang )

Groups

Group information

There will need to be standard information saved for each group, so that groups
have meaning to other users, and if appropriate can be found through the search
feature. At a minimum, we need to store the following information:







Name of the group
Creator / owner of the group: So we know who has permission to manage it
Description of the group: So users know what the group is about, in a little
more detail than its name
Permission structure of the group: So that the group can be shown if
appropriate, and so that it is clear if and how new members can join
that group
Date the group was created: We could also store the creation date, so if one
group becomes old and isn't participated in very often, a newer one may take
priority in the search results

This information is what would be stored in the groups table in the database.

Types of groups

To make the groups system flexible, there need to be various types of groups
available, with different permissions relating to who can have access to them.


Typically, most social networks tend to provide the following options:





Global / public groups: Open to everyone on the site
Network specific: Only open to a section of the social network. For
example, those from a particular geographical region, or working
for one particular institution
Private, with these options:
°°

Only available to those who have been explicitly invited by
the group's creator

°°

Semi-private: Only available to those who have been
explicitly invited by another member of the group

°°

Invite-only: Private groups where users can request
membership (similar to friend requests), which is then
decided by the group's creator or administrator

As our Dino Space network doesn't, in its current form, have any provisions for
subsidiary networks, such as users based in the US or the UK, or users who work
at a specific company, we will not look to implement this feature. The other types

of groups seem appropriate, so we will look to implement those.
[ 302 ]

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


Chapter 10

Ownership

There are a number of ways we could facilitate the ownership or management of
a community group on our social network. Primarily there are two options:



The creator is the owner / administrator of the group
The creator can appoint owner / administrator(s) of the group

We will implement the first of these options. However, it shouldn't be too difficult to
extend this to support appointed administrators, should you wish to extend this for
your own social network.
In this sense, the creator of the group will be listed on the group's page, and they
alone will have full control of the group.

Membership

Finally, we need to plan how membership will be organized, particularly in light of
the types of groups that we may support. We need to be able to store and manage
membership lists, lists of invited users who can become members of protected
groups should they wish, and also users who have requested to be members of a

group, but have not yet been granted access.

Features

Groups need to offer users a dedicated area where they can communicate
and collaborate on specific topics related to the purpose of the group,
including discussions.

A group

With an idea of what our group needs to do, the information it needs to contain,
and how it will work, let's now create the functionality. This, as with other features,
will involve creation of a model, a controller, and a series of template files to form
the view.

Discussion

In order to facilitate communication and collaboration, groups require some new
functionality that we don't yet have in Dino Space—discussion forum style topics.
Let's create models for topics and discussion forum posts. Initially, we will tie these
with groups; however, it would be easy for us to extend them to other areas of the
social network should we wish.
[ 303 ]

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


Groups

We won't create controllers at this stage, as topics (for now) will only be accessed via

groups. So either the group's controller will handle this, or the group's controller will
delegate control to a group topic controller.
Discussion forums can be very complicated systems; there are numerous open
source and commercial forum software products available with a wealth of features.
Creating a fully-featured discussion forum would be the series of a number of
books in itself. For the purposes of Dino Space, we are going to create a very simple
discussion-style feature to plug into our social network.

Database

This discussion feature will require two new database tables, one for topics
themselves, and one for the posts they relate to.

Topics

Topic records in the database will simply contain a name, who created it and when,
and the group they are related to, as illustrated by the table below:
Field

Type

Description

ID

Integer, Auto-increment,
Primary Key

Internal reference for the topic of conversation


Name

Varchar

The name of the topic

Creator

Integer

The user who created the topic

Created

Timestamp

The time the topic was created

Group

Integer

The group the topic was created within

Posts

Posts will contain the content of the post, who created it and when, and the topic that
it relates to, as illustrated by the table below.
Field


Type

Description

ID

Integer, Auto-increment,
Primary Key

Internal reference for the post within a topic

Topic

Integer

The topic the post is part of

Post

Longtext

The post itself

Creator

Integer

The user who created the post

Created


Timestamp

The time the post was created

[ 304 ]

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


Chapter 10

Post

When a topic is created, in most cases so is the first post. Hence, we should link our
post and topic models so that both are created at the same time. Since the topic will
create the post, we should create the post model first.

Model

The post model (models/post.php) only needs to be basic: various properties for the
object, a constructor to get the post from the database, some setter methods to update
the properties, and a save method to create a new post or update an existing post.
/**
* Post model object
*/
class Post{

As usual we start with our class variables; these include a reference to the registry

object itself, and the variables required for a post.
/**
* Registry object
*/
private $registry;
/**
* ID of the post
*/
private $id;
/**
* ID of the creator of the post
*/
private $creator;
/**
* Name of the creator of the post
*/
private $creatorName;
/**
* Timestamp of when the post was created
*/
private $created;

[ 305 ]

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


Groups
/**
* Friendly representation of when the post was created

*/
private $createdFriendly;
/**
* ID of the topic the post relates to
*/
private $topic;
/**
* The post itself
*/
private $post;

The constructor takes the registry object as a parameter and, optionally, an ID for
the post. If a post ID is supplied, then it queries the database for the post, and if a
record exists, it populates the class variables with the results.
/**
* Post constructor
* @param Registry $registry the registry object
* @param int $id the ID of the post
* @return void
*/
public function __construct( Registry $registry, $id=0 )
{
$this->registry = $registry;
$this->id = $id;
if( $this->id > 0 )
{
$sql = "SELECT p.*, DATE_FORMAT(p.created, '%D %M %Y') as
created_friendly, pr.name as creator_name FROM posts p,
profile pr WHERE pr.user_id=p.creator AND p.ID=" . $this->id;
$this->registry->getObject('db')->executeQuery( $sql );

if( $this->registry->getObject('db')->numRows() > 0 )
{
$data = $this->registry->getObject('db')->getRows();
$this->creator = $data['creator'];
$this->creatorName = $data['creator_name'];
$this->createdFriendly = $data['created_friendly'];
$this->topic = $data['topic'];
$this->post = $data['post'];
}
else
[ 306 ]

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


Chapter 10
{
$this->id = 0;
}
}
}

We have our setter methods to set the private class variables from outside the object.
/**
* Set the creator of the post
* @param int $c the creator
* @return void
*/
public function setCreator( $c )
{

$this->creator = $c;
}
/**
* Set the topic the post relates to
* @param int $t the topic ID
* @return void
*/
public function setTopic( $t )
{
$this->topic = $t;
}
/**
* Set the post content
* @param String $p the post itself
* @return void
*/
public function setPost( $p )
{
$this->post = $p;
}

Finally, we have our save method. If an ID is set, it updates an existing record; if no
ID is set, then it inserts a record into the database.
/**
* Save the post in the database
* @return void
*/
[ 307 ]

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



Groups
public function save()
{
if( $this->id > 0 )
{
$update = array();
$update['topic'] = $this->topic;
$update['post'] = $this->post;
$update['creator'] = $this->creator;
$this->registry->getObject('db')->updateRecords( 'posts',
$update, 'ID=' . $this->id );
}
else
{
$insert = array();
$insert['topic'] = $this->topic;
$insert['post'] = $this->post;
$insert['creator'] = $this->creator;
$this->registry->getObject('db')->insertRecords( 'posts',
$insert );
$this->id = $this->registry->getObject('db')->lastInsertID();
}
}
}
?>

Topic


Our topic model needs to link into the post model. So we should create a method
that creates a new instance of the post model and stores it publicly within the topic
model, so that our controllers can update the properties of both of these objects.
When it comes to saving the topic, if the post was created, it should pass the topic
ID to indicate the topic the post relates to.

Model

Because of what we discussed above, this model (models/topic.php) will be
slightly more complicated than the post model, but not by much.
/**
* Discussion topic class
*/
class Topic {

[ 308 ]

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


Chapter 10

As usual, we have various properties within the object.
/**
* The registry object
*/
private $registry;
/**
* ID of the topic

*/
private $id=0;
/**
* ID of the creator
*/
private $creator;
/**
* Name of the creator
*/
private $creatorName;
/**
* Name of the topic
*/
private $name;
/**
* When the topic was created (TIMESTAMP)
*/
private $created;
/**
* Friendly reference for the date the topic was created
*/
private $createdFriendly;

[ 309 ]

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


Groups


A few of these properties are of more than just data from the topics table. We also
have the numPosts property, which is for the number of posts within a topic; we
have a Boolean field, which determines if the topic should also save the first post,
and in that instance, we also have a post object.
/**
* Number of posts in the topic
*/
private $numPosts;
/**
* If we are also saving the first post
*/
private $includeFirstPost;
/**
* Post object - if saving the first post too
*/
private $post;
/**
* Group the topic was posted within
*/
private $group;

We have our standard constructor, populating fields if the ID is valid.
/**
* Topic constructor
* @param Registry $registry the registry object
* @param int $id the ID of the topic
* @return void
*/
public function __construct( Registry $registry, $id=0 )
{

$this->registry = $registry;
$this->id = $id;
if( $this->id > 0 )
{
$sql = "SELECT t.*, (SELECT COUNT(*) FROM posts po WHERE
po.topic=t.ID) as posts, DATE_FORMAT(t.created, '%D %M %Y')
as created_friendly, p.name as creator_name FROM topics t,
profile p WHERE p.user_id=t.creator AND t.ID=" . $this->id;
$this->registry->getObject('db')->executeQuery( $sql );
if( $this->registry->getObject('db')->numRows() > 0 )
[ 310 ]

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


Chapter 10
{
$data = $this->registry->getObject('db')->getRows();
$this->creator = $data['creator'];
$this->creatorName = $data['creator_name'];
$this->createdFriendly = $data['created_friendly'];
$this->name = $data['name'];
$this->numPosts = $data['posts'];
$this->group = $data['group'];
}
else
{
$this->id = 0;
}
}

}
/**
* Get query of the posts in the topic (i.e. collection of posts ==
topic )
*/
public function getPostsQuery()
{
$sql = "SELECT p.*, DATE_FORMAT() as friendly_created_post,
pr.name as creator_friendly_post FROM posts p, profile pr WHERE
pr.user_id=p.creator AND p.topic=" . $this->id;
return $sql;
}

We have a method to set if we are including the first post. If we are, we include the
class, instantiate the object, and assign it to the post property in the topic object.
/**
* Set if this save should also save the first post
* @param bool $ifp
* @return void
*/
public function includeFirstPost( $ifp )
{
$this->includeFirstPost = $ifp;
require_once( FRAMEWORK_PATH . 'models/post.php' );
$this->post = new Post( $this->registry, 0 );
}

[ 311 ]

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




×