summaryrefslogtreecommitdiffstats
path: root/controllers/AnimelistController.php
blob: 7eb8c4cd182da38aa4479f6d676f9c8bab01fee3 (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
99
<?php

namespace app\controllers;

use app\models\contentList;
use app\models\User;
use app\models\Anime;
use app\models\Entry;
use \lithium\security\Auth;


class AnimeListController extends \lithium\action\Controller {
	public $publicActions = array('view');

	public function view($username, $sort = "all")
	{
		$user = User::find('first', array('conditions' => compact('username')));

		$watching = array();
		$paused = array();
		$dropped = array();
		$planning = array();
		$finished = array();
		
		//The anime list comes back as a DocumentArray, so we can
		//parse through them with a foreach
		//For each entry,
		foreach($user->animelist as $entry)
		{
			//Sort it based on status
			switch($entry->my_status)
			{
				case "Completed": $finished[] = $entry; break;
				case "Watching": $watching[] = $entry; break;
				case "On-Hold" : $paused[] = $entry; break;
				case "Dropped" : $dropped[] = $entry; break;
				case "Plan to Watch": $planning[] = $entry; break;
			}
		}

		
		//In the future we can use set or something
		switch($sort)
		{
			case "planning" : return compact('user', 'planning'); break;
			case "completed" : return compact('user', 'finished'); break;
			case "onhold": return compact('user', 'paused'); break;
			case "watching" : return compact('user', 'watching'); break;
			case "dropped": return compact('user', 'dropped');
			default: return compact('user', 'watching', 'paused', 'dropped', 'planning', 'finished'); break;
		}
	}

	public function addsearch()
	{
		if (isset($this->request->query['Search'])) {
			$searchParam = '/' . $this->request->query['Search'] . '/i';
			if($this->request->query['Search'])
			{
				return Anime::search($searchParam);
			}
		}
	}

	//Ensure the correct user here 
	public function add($id = null)
	{
		if (empty($this->request->data))
		{
			if ('id' != null) 
			{
				$anime = Anime::find('first', array('conditions' => array('special_id' => $id)));
				$entry = null;
				return compact('anime', 'entry');
			}
			return array('anime' => null, 'entry' => null);
		}
		$entry = Entry::create($this->request->data);
		$user = Auth::check('default');
		$username = $user['username'];

		if (isset($this->request->data['tags']))
		{
				$entry->my_tags = explode(' ', $this->request->data['tags']);
				unset($this->request->data['tags']);
		}
		
		if ($entry->validates()) {
			$entry->add($username);

			return $this->redirect("/animelist/view/$username");	
		}

		return $entry; 
		
	}


}