Friendship requests system in CodeIgniter

0

FIRST THAT NOTHING, THIS QUESTION WILL BE LONG; beforehand, thank you very much for going over and taking your time to read the question.

I've been working on a friend request system like Facebook on CodeIgniter, the problem I'm having is that I can not figure out the correct way to create the follow / add function as they want to call it.

Let's say that I can send requests to X user but if that user already sent me a ami, instead of sending another request to the table, this should update the user request sent to me (or vice versa if I was send it to the first one, instead of the X user sending me one, to update mine).

So far the only way I've been updating the rows is through the database directly ...

Here is an image of the database ?:

And here is the HTML and PHP code that I have in the profile view:

<?php
if($item->username == $this->session->username){
   echo '<a class="btn btn-warning btn-xs">You</a>';
} elseif(empty($relationship->status)) {
    echo form_open('users/follow/'.$item->username);

    $data = array(
        'name' => 'friend_id',
        'id' => 'friend_id',
        'value' => $item->id
    );

    echo form_hidden($data);
    echo form_submit('mysubmit', 'Follow', array('class' => 'btn btn-warning btn-xs'));
    echo form_close();
 } elseif($relationship->status == 'accepted') {
     echo form_open('users/unfollow/'.$item->id.'/'.$item->username);

     $data = array(
         'name' => 'friend_id',
         'id' => 'friend_id',
         'value' => $item->id,
     );

     echo form_hidden($data);
     echo form_submit('mysubmit', 'Unfollow', array('class' => 'btn btn-warning btn-xs'));
     echo form_close();
 } elseif($relationship->status == 'pending') {
     echo form_open('users/unfollow/'.$item->id.'/'.$item->username);

     $data = array(
         'name' => 'friend_id',
         'id' => 'friend_id',
         'value' => $item->id,
     );

     echo form_hidden($data);
     echo form_submit('mysubmit', 'Cancel', array('class' => 'btn btn-warning btn-xs'));
     echo form_close();
 } elseif($item->user_id) {
     echo 'Ho.a';
 } else {
     echo 'Cannot follow yourself';
 }
?>

And here I have the function of follow / add / add friend, as you want to call him:

public function follow($username){

        // Check Login
        if(!$this->session->userdata('user_id')){
            // Redirect to page
            redirect('users/login');
        }

        // Get item
        $item = $this->User_model->get_username($username);

        // Already friends?
        $friendship = $this->User_model->the_relationship($item->id);

        if ($friendship->status == 'accepted') {

            // Create Message
            $this->session->set_flashdata('error', 'You are already following this user.');

            // Redirect to pages
            redirect('users/dashboard');

        } elseif($friendship->status == 'pending') {

            $item = $this->User_model->get_username($username);

            // Page Data
            $data = array(
                'user_id'   => $item->id,
                'friend_id' => $this->session->userdata('user_id'),
                'status'    => 'accepted',
                'type'      => 'friendship'
            );

            $this->Relationship_model->updateFriendship($item->id, $data);

            // Create Message
            $this->session->set_flashdata('error', 'You accepted the friendship request.');

            // Redirect to pages
            redirect('users/dashboard');

        } else {

        // Get item
        $item = $this->User_model->get_username($username);

        // Page Data
        $data = array(
            'user_id'       => $this->session->userdata('user_id'),
            'friend_id'     => $item->id,
            'status'        => 'pending',
            'type'          => 'friendship',
        );

        $this->Relationship_model->add($data);

        // Activity Array
        $data = array(
            'resource_id' => $this->db->insert_id(),
            'type'        => 'friendship',
            'action'      => 'sent',
            'user_id'     => $this->session->userdata('user_id'),
            'message'     => '(' . $this->session->userdata('username') . ') sent a friend request to ('.$item->username.') ',
        );

        // Insert Activity
        $this->Activity_model->add($data);

        // Set Message
        $this->session->set_flashdata('success', 'Friend request has been sent');

        // Redirect
        redirect('users/profile/'.$item->username);

    }

}

Here is the function that I have in the model that verifies if there is a relationship between users:

public function the_relationship($id){  
    $this->db->select('*');
    $this->db->from($this->relationship);
    $this->db->where('user_id', $this->session->userdata('user_id'));
    $this->db->where('friend_id', $id);
    $this->db->where('type', $this->type);

    $query = $this->db->get();

    if($query->num_rows() >= 1){
        return $query->row();
    } else {
        return false;
    }
}

I hope to have explained well and many thanks to those who decide to help me with this little problem that I have had for several days.

NOTE: Here is an image of the current behavior !:

    
asked by Kirasiris 10.09.2018 в 02:54
source

0 answers