AnimelistController.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 id is null,
  58. if ('id' == null) {
  59. //Redirect them to the search page
  60. return $this->redirect('Animelist::addsearch');
  61. }
  62. //Find the requested anime
  63. $anime = Anime::find('first', array('conditions' => array('special_id' => $id)));
  64. $entry = null;
  65. //If data was submitted
  66. if ($this->request->data)
  67. {
  68. //Create an entry with the data
  69. $entry = Entry::create($this->request->data);
  70. //Get the current user
  71. $user = Auth::check('default');
  72. $username = $user['username'];
  73. //Break the tags into an array
  74. if (isset($this->request->data['tags']))
  75. {
  76. $entry->my_tags = explode(',', $this->request->data['tags']);
  77. unset($this->request->data['tags']);
  78. }
  79. //If the entry is valid
  80. if ($entry->validates()) {
  81. //Store it, :TODO: make sure this passes at some point, silent failure sucks
  82. $entry->add($username);
  83. //Redirect the user to their anime list
  84. return $this->redirect("/animelist/view/$username");
  85. }
  86. }
  87. return compact('entry', 'anime');
  88. }
  89. public function edit($id = null)
  90. {
  91. if (id != null)
  92. {
  93. //Get the ani
  94. }
  95. else
  96. {
  97. //Error message
  98. }
  99. }
  100. }