开发者

CakePHP Friendship system

I am building a friendship system in CakePHP that uses two tables: Users and Friends.

In Users (id,username,email,password) and Friends (id,user_from,user_to,status)

A user requests another user to be friends and this creates a record in the friends table storing both the user ids and setting a status of 'Requested'. The user can either accept the friendship and the status changes to 'Accepted' or cancel the friendship and the record is deleted from the database.

An example link for the request looks like and could be shown either in a users list or on the users details page:

<?php echo $this->Html->link('Add as Friend', array('controller'=>'friends','action'=>'add_friend',$user['User']['id'])); ?>

Question 1 is how could I make this link change to a cancel request link if the user has a request against them or is already friends?

This link corresponds to the following method in the controller:

function add_friend ( $id )
    {
        if(!empty($this->data))
        {
            $this->Friend->Create();
            if($this->Friend->save($this->data))
            {
                $this->Session->setFlash('Friendship Requested');
                $this->redirect(array('controller'=>'users','action'=>'login'));
            }
        }
    }

So we are passing the ID to the method which will be the user_to and then the 'user_from' needs to be the currently logged in user and set the status to 'Requested'. Question 2 is how to do I do this? Also how do I prevent a user from creating multiple records by just calling that method over and show a message saying you've alr开发者_Go百科eady requested friendship.

The next method is:

function accept_friendship ( $id )
    {
     $this->Session->setFlash('Friendship Accepted');
                $this->redirect(array('controller'=>'friends','action'=>'index'));
            }
        }
    }

Question 3: But again I'm confused as to how I would change the status of the record and mark the users as friends when the method is called. Also need to prevent this from being called multiple times on the same record.

The final bit is listing the friends for the user or another user:

function users_friends( $id )
{
 $this->set('friends', $this->Friend->find('all'));
}

function my_friends()
{
$this->set('friends', $this->Friend->find('all'));
}

As you can see the first method requires the id of the user you are viewing and then the second method will use the currently logged in user id. Question 4: How do I then use this to list the friends of that user?

If anyone can help put me on the right track with this it'd be much appreciated as I've ground to a halt and not sure how to do those 4 things and trying to learn CakePHP as best I can so help is much appreciated. Thanks

EDIT: It has occurred to me that a view with hidden fields could be used to store the information regarding the friend request that the user confirms but this isn't ideal as it means sending the user off somewhere else when in fact I want to just run the function and do the redirect straight off. NOT AJAX THOUGH!


Answer 1 and 2:

function add_friend ( $id )
{
    if(!empty($this->data))
    {
        $this->Friend->Create();
        if($this->Friend->save($this->data))
        {
            $this->Session->setFlash('Friendship Requested');
            $this->redirect(array('controller'=>'users','action'=>'login'));
        }
    }
if(empty($this->data))
    {
        $this->set('friends', $this->Friend->find('all',array('Friend.id'=>$id));
    }
}




<?php 

if($friends['Friend']['status']=="Requested")
{
  echo $this->Html->link('Request Pending', '#'); 
}
else if($friends['Friend']['status']=="Accepted")
{
  echo $this->Html->link('Already Friend', '#');
}
else
{
      echo $this->Html->link('Add as Friend',  array('controller'=>'friends','action'=>'add_friend',$user['User']['id']));
}
?>

Answer 3 and 4:

 funcrion friendlist($user_id)
 {
$session_user_id = $this->Session->read('Auth.User.id')
if($user_id == $session_user_id )
{
    $user_to = $session_user_id ;
}
else
{
    $user_to = $user_id;
}
$this->Friend->find('all',array('Friend.user_to'=>$user_to,'Friend.status'=>'Accepted')
 }


Answer 3 is something like this:

function accept_friendship ( $id ) {
    $this->Friend->id = $id;
    $current_status = $this->Friend->field('status');

    if($current_status=='Requested') {
        $this->Application->saveField('status', 'Accepted');
    }

    $this->Session->setFlash('Friendship Accepted');
    $this->redirect(array('controller'=>'friends','action'=>'index'));
}

Essentially get the ID of the friend request, check the status field, and if it's equal to Requested, then update it to Accepted. This way it will only be called once.

And also to prevent people from repeatedly "accepting" a friend, just remove the "Accept" link once it's been accepted. The if statement stops your code from updating unnecessarily.

You should also put some kind of prevention in place so that only the requested friend can accept the request. Otherwise I could type the URL yoursite.com/friends/accept_friendship/123 and accept a random persons request without any authentication.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜