The template for creating a group views/default/templates/groups/create.. Create a new group Name Description Type Public Group Private Group Private Invite Only Group P
Trang 1$this->listPublicGroups( 0 );
}
}
else
{
if( isset( $urlBits[1] ) )
{
$this->listPublicGroups( intval( $urlBits[1] ) );
}
else
{
$this->listPublicGroups( 0 );
}
}
}
/**
* Create a new group
* @return void
*/
private function createGroup()
{
if( isset( $_POST ) && is_array( $_POST ) && count( $_POST ) > 0
)
{
require_once( FRAMEWORK_PATH 'models/group.php');
$group = new Group( $this->registry, 0 );
$group->setCreator( $this->registry->getObject('authenticate')-
>getUser()->getUserID() );
$group->setName( $this->registry->getObject('db')-
>sanitizeData( $_POST['name'] ) );
$group->setDescription( $this->registry->getObject('db')-
>sanitizeData( $_POST['description'] ) );
$group->setType( $_POST['type'] );
$group->save();
$this->registry->errorPage('Group created', 'Thank you, your
new group has been created');
}
else
{
$this->registry->getObject('template')->buildFromTemplates(
'header.tpl.php', 'groups/create.tpl.php', 'footer.tpl.php' );
}
}
}
?>
Trang 2The template for creating a group (views/default/templates/groups/create
tpl.php) is simply a form, with a text box for name and description and a
drop-down list of types.
<div id="main">
<div id="rightside">
</div>
<div id="content">
<h1>Create a new group</h1>
<form action="groups/create" method="post">
<label for="name">Name</label><br />
<input type="text" id="name" name="name" value="" /><br />
<label for="description">Description</label><br />
<textarea id="description"
name="description"></textarea><br />
<label for="type">Type</label><br />
<select id="type" name="type">
<option value="public">Public Group</option>
<option value="private">Private Group</option>
<option value="private-member-invite">Private (Invite
Only) Group</option>
<option value="private-self-invite">Private (Self-Invite)
Group</option>
</select><br />
<input type="submit" id="create" name="create"
value="Create group" />
</form>
</div>
</div>
Trang 3Creating a group—in action
If we navigate to groups/create, we see the create group form and are able
to successfully create a new group.
Viewing a group
For us to view a group (and also participate in a group), we will need:
• A controller for the group
• A way of maintaining membership lists for groups, including:
° A database table
• A way of determining if a user is either a member of a group or the creator,
and thus has permission to view the group
Membership
Maintaining a list of memberships is important so we know that a user can view
a group, and more importantly, we know if a user is a member of a group, not a
member of a group, or if they have requested to join or have been invited to join.
Trang 4The following database structure would be a suitable group_memberships table for
our site:
ID Integer, PK, Auto-increment Internal reference for the membership
Group Integer The ID of the group
User Integer The ID of the user
Approved Boolean Indicates if the user's membership has
been approved Invited Boolean Indicates if the user has been invited
to join Requested Boolean Indicates if the user has requested to
join InvitedDate Timestamp Date the user was invited
RequestedDate Timestamp Date the user requested to join
JoinDate Timestamp Date the user joined
Inviter Integer The user who invited the user
Membership model
We also need a model (models/groupmembership.php) to represent a user's
membership, pending membership, or lack of membership of a group This model
will be used to create memberships, create requests to join a group, and to create
invites to a group It will also be used to determine if a user has access to a group.
<?php
/**
* Group membership model
*/
class Groupmembership{
Our model starts with our class variables, including a reference to the registry
object itself, and the properties related to an individual's membership of a group.
/**
* ID of the membership record
*/
private $id;
/**
* ID of the user
*/
private $user;
Trang 5
/**
* ID of the group
*/
private $group;
/**
* Indicates if the membership is active / approved
*/
private $approved = 0;
/**
* Indicates if the user was invited
*/
private $invited;
/**
* Indicates if the user has requested to join
*/
private $requested;
/**
* Date user was invited to join
*/
private $invitedDate;
/**
* Date user requested to join
*/
private $requestedDate;
/**
* Join date
*/
private $joinDate;
/**
* User who invited the user to join the group
*/
private $inviter;
The constructor takes the registry object and an optional ID as a parameter If the
ID is passed, it queries the database, and if records are found, it assigns the relevant
fields to the class variables.
Trang 6/**
* Constructor
* @param Registry $registry
* @param int $id
* @return void
*/
public function construct( Registry $registry, $id=0 )
{
$this->registry = $registry;
if( $id > 0 )
{
$sql = "SELECT * FROM group_membership WHERE ID={$id} LIMIT 1";
$this->registry->getObject('db')->executeQuery( $sql );
if( $this->registry->getObject('db')->numRows() == 1 )
{
$data = $this->registry->getObject('db')->getRows();
$this->approved = $data['approved'];
$this->invited = $data['invited'];
$this->requested = $data['requested'];
$this->invitedDate = $data['invited_date'];
$this->requestedDate = $data['requested_date'];
$this->joinDate = $data['join_date'];
$this->inviter = $data['inviter'];
}
}
else
{
$this->id = 0;
}
}
If part of our code needs to look up a user's group membership but doesn't know
the ID of the membership, we can instead call the following method, passing the
user ID and group ID as parameters The method will populate the class variables
with results from the query:
/**
* Get membership information by user and group
* @param int $user
* @param int $group
* @return void
*/
public function getByUserAndGroup( $user, $group )
{
$this->user = $user;
Trang 7$this->group = $group;
$sql = "SELECT * FROM group_membership WHERE user={$user} AND
`group`={$group} LIMIT 1";
$this->registry->getObject('db')->executeQuery( $sql );
if( $this->registry->getObject('db')->numRows() == 1 )
{
$data = $this->registry->getObject('db')->getRows();
$this->approved = $data['approved'];
$this->invited = $data['invited'];
$this->requested = $data['requested'];
}
}
We have some of our standard getter methods to return some of the private
properties to other objects within the framework.
/**
* Get if the membership is approved
* @return boolean
*/
public function getApproved()
{
return $this->approved;
}
/**
* Get if the user was invited
* @return boolean
*/
public function getInvited()
{
return $this->invited;
}
/**
* Get if the user requested to join
* @return boolean
*/
public function getRequested()
{
return $this->requested;
}
/**
* Get the user who invited this user to the group
* @return int
*/
Trang 8public function getInviter()
{
return $this->inviter;
}
Next, we have our setter methods, which set the relevant private properties
of the class.
/**
* Set membership to approved
* @param boolean $approved
* @return void
*/
public function setApproved( $approved )
{
$this->approved = $approved;
}
/**
* Set membership status to requested
* @param boolean $requested
* @return void
*/
public function setRequested( $requested )
{
$this->requested = $requested;
}
/**
* Set if the user was invited
* @param boolean $invited
* @return void
*/
public function setInvited( $invited )
{
$this->invited = $invited;
}
/**
* Set the inviter
* @param int $inviter
* @return void
*/
public function setInviter( $inviter )
{
$this->inviter = $inviter;
}
Trang 9Finally, we have our save method, which either creates a new record in the database,
or updates an existing one.
/**
* Save the membership record
* @return void
*/
public function save()
{
if( $this->id > 0 )
{
$update = array();
$update['user'] = $this->user;
$update['group'] = $this->group;
$update['approved'] = $this->approved;
$update['requested'] = $this->requested;
$update['invited'] = $this->invited;
$update['invited_date'] = $this->invitedDate;
$update['requested_date'] = $this->requestedDate;
$update['join_date'] = $this->joinDate;
$update['inviter'] = $this->inviter;
$this->registry->getObject('db')->updateRecords(
'group_memberships', $update, 'ID=' $this->id );
}
else
{
$insert = array();
$insert['user'] = $this->user;
$insert['group'] = $this->group;
$insert['approved'] = $this->approved;
$insert['requested'] = $this->requested;
$insert['invited'] = $this->invited;
$insert['invited_date'] = $this->invitedDate;
$insert['requested_date'] = $this->requestedDate;
$insert['join_date'] = $this->joinDate;
$insert['inviter'] = $this->inviter;
$this->registry->getObject('db')->insertRecords(
'group_memberships', $insert );
$this->id = $this->registry->getObject('db')->lastInsertID();
}
}
}
?>
Trang 10Our group controller (controllers/group/controller.php) needs to do a fair bit
of logic to work out what it should do, even if the request is simply to view a group,
as we want to do now.
<?php
class Groupcontroller {
/**
* Controller constructor - direct call to false when being
embedded via another controller
* @param Registry $registry our registry
* @param bool $directCall - are we calling it directly via the
framework (true), or via another controller (false)
*/
public function construct( Registry $registry, $directCall )
{
$this->registry = $registry;
$urlBits = $this->registry->getObject('url')->getURLBits();
Firstly, the controller checks that the current user is logged in.
if( $this->registry->getObject('authenticate')->isLoggedIn() )
{
Secondly, the controller checks that there is at least one bit in the URL The first bit of
the URL should be the group ID.
if( isset( $urlBits[1] ) )
{`
The group model is then included and instantiated, to determine if the requested
group is valid / active.
require_once( FRAMEWORK_PATH 'models/group.php');
$this->group = new Group( $this->registry, intval(
$urlBits[1] ) );
$this->groupID = intval( $urlBits[1] );
if( $this->group->isValid() && $this->group->isActive() )
{