AnimelistController.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace app\controllers;
  3. use app\models\contentList;
  4. use app\models\User;
  5. use app\models\Anime;
  6. use app\models\Entry;
  7. use \lithium\security\Auth;
  8. class AnimeListController extends \lithium\action\Controller {
  9. public $publicActions = array('view');
  10. public function view($username, $sort = "all")
  11. {
  12. $user = User::find('first', array('conditions' => compact('username')));
  13. $watching = array();
  14. $paused = array();
  15. $dropped = array();
  16. $planning = array();
  17. $finished = array();
  18. //The anime list comes back as a DocumentArray, so we can
  19. //parse through them with a foreach
  20. //For each entry,
  21. foreach($user->animelist as $entry)
  22. {
  23. //Sort it based on status
  24. switch($entry->my_status)
  25. {
  26. case "Completed": $finished[] = $entry; break;
  27. case "Watching": $watching[] = $entry; break;
  28. case "On-Hold" : $paused[] = $entry; break;
  29. case "Dropped" : $dropped[] = $entry; break;
  30. case "Plan to Watch": $planning[] = $entry; break;
  31. }
  32. }
  33. //In the future we can use set or something
  34. switch($sort)
  35. {
  36. case "planning" : return compact('user', 'planning'); break;
  37. case "completed" : return compact('user', 'finished'); break;
  38. case "onhold": return compact('user', 'paused'); break;
  39. case "watching" : return compact('user', 'watching'); break;
  40. case "dropped": return compact('user', 'dropped');
  41. default: return compact('user', 'watching', 'paused', 'dropped', 'planning', 'finished'); break;
  42. }
  43. }
  44. public function addsearch()
  45. {
  46. if (isset($this->request->query['Search'])) {
  47. $searchParam = '/' . $this->request->query['Search'] . '/i';
  48. if($this->request->query['Search'])
  49. {
  50. return Anime::search($searchParam);
  51. }
  52. }
  53. }
  54. //Ensure the correct user here
  55. public function add($id = null)
  56. {
  57. if (empty($this->request->data))
  58. {
  59. if ('id' != null)
  60. {
  61. $anime = Anime::find('first', array('conditions' => array('special_id' => $id)));
  62. $entry = null;
  63. return compact('anime', 'entry');
  64. }
  65. return array('anime' => null, 'entry' => null);
  66. }
  67. $entry = Entry::create($this->request->data);
  68. $user = Auth::check('default');
  69. $username = $user['username'];
  70. if (isset($this->request->data['tags']))
  71. {
  72. $entry->my_tags = explode(' ', $this->request->data['tags']);
  73. unset($this->request->data['tags']);
  74. }
  75. if ($entry->validates()) {
  76. $entry->add($username);
  77. return $this->redirect("/animelist/view/$username");
  78. }
  79. return $entry;
  80. }
  81. }