$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' ); } } } ?>
[ 322 ]
Download from Wow! eBook <www.wowebook.com>
Chapter 10
View
The 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.
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 model
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.
[ 324 ]
Download from Wow! eBook <www.wowebook.com>
Chapter 10
The following database structure would be a suitable group_memberships table for our site: Field
Type
Description
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. /** * 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; [ 325 ]
Download from Wow! eBook <www.wowebook.com>
Groups /** * 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. [ 326 ]
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; [ 327 ]
Download from Wow! eBook <www.wowebook.com>
Groups $this->group = $group; $sql = "SELECT * FROM group_membership WHERE user={$user} AND
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 */ [ 328 ]
Download from Wow! eBook <www.wowebook.com>
Chapter 10 public 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; }
[ 329 ]
Download from Wow! eBook <www.wowebook.com>
Groups
Finally, 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 ); }
Our 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. 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] ) );