summaryrefslogtreecommitdiffstats
path: root/controllers/FriendsController.php
blob: 6d7922f5528f9e624ec620f41aa9b49c81bac436 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php

namespace app\controllers;

use app\models\Friends;
use lithium\action\DispatchException;
use lithium\security\Auth;

use app\models\User;

class FriendsController extends \lithium\action\Controller {

	public function index() {
		$id = Auth::check('default');
		$id = $id['id'];
		$conditions = array('User.id' => $id);
		$with = array('Friends');
		$user = User::find('first', compact('conditions', 'with'));

		return compact('user');
	}

	public function view() {
		$friend = Friends::first($this->request->id);
		return compact('friend');
	}

	public function add() {
		$friend = Friends::create();
		if ($this->request->data) {
		$user = Auth::check('default');
		$ToUserId = $this->request->data['userid'];

		//If the user exists
		if(User::count($ToUserId) == 1)
		{

			$friendship = Friends::create(array('FromUserId' => $user['id'],
												'ToUserId' => $ToUserId,
												'StatusId' => Friends::status("open"),
												'SentTime' => date('Y-m-d H:i:s', time())));

			$friendship->save();

		}
	}
	return compact('user');
	}

	public function accept() {
		$user == Auth::check('default');
		$id = $this->request->data['id'];

		$relationship = Friends::find($id);

		//Ensure the user is accepting their own request
		if ($relationship->ToUserId == $user->id)
		{
			$relationship->ResponseTime = date('Y-m-d H:i:s', time());
			$relationship->StatusId = Friends::status('accepted');
			$relationship->save();
		}

		//Return them to their friends page (with ajax return the status I guess)
		return $this->redirect("Friendships::view");	
	}
	
	//Copied and pasted the code from above but it really should be handled inside the friendship
	//model;
	public function decline() {
		$user == Auth::check('default');
		$id = $this->request->data['id'];

		$relationship = Friendships::find($id);

		//Ensure the user is accepting their own request
		if ($relationship->ToUserId == $user->id)
		{
			$relationship->ResponseTime = date('Y-m-d H:i:s', time());
			$relationship->StatusId = Friendship::status('rejected');
			$relationship->save();
		}

		//Return them to their friends page (with ajax return the status I guess)
		return $this->redirect("Friendships::view");	
	}

	public function delete() {
		if (!$this->request->is('post') && !$this->request->is('delete')) {
			$msg = "Friends::delete can only be called with http:post or http:delete.";
			throw new DispatchException($msg);
		}
		Friends::find($this->request->id)->delete();
		$this->redirect('Friends::index');
	}
}

?>