AnimelistController.php 2.8 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. use \lithium\util\Validator;
  9. class AnimeListController extends \lithium\action\Controller {
  10. public $publicActions = array('view');
  11. public function view($username, $sort = "all")
  12. {
  13. $user = User::find('first', array('conditions' => compact('username')));
  14. $watching = array();
  15. $paused = array();
  16. $dropped = array();
  17. $planning = array();
  18. $finished = array();
  19. //The anime list comes back as a DocumentArray, so we can
  20. //parse through them with a foreach
  21. //For each entry,
  22. foreach($user->animelist as $entry)
  23. {
  24. //Sort it based on status
  25. switch($entry->my_status)
  26. {
  27. case "Completed": $finished[] = $entry; break;
  28. case "Watching": $watching[] = $entry; break;
  29. case "On-Hold" : $paused[] = $entry; break;
  30. case "Dropped" : $dropped[] = $entry; break;
  31. case "Plan to Watch": $planning[] = $entry; break;
  32. }
  33. }
  34. //In the future we can use set or something
  35. switch($sort)
  36. {
  37. case "planning" : return compact('user', 'planning'); break;
  38. case "completed" : return compact('user', 'finished'); break;
  39. case "onhold": return compact('user', 'paused'); break;
  40. case "watching" : return compact('user', 'watching'); break;
  41. case "dropped": return compact('user', 'dropped');
  42. default: return compact('user', 'watching', 'paused', 'dropped', 'planning', 'finished'); break;
  43. }
  44. }
  45. public function addsearch()
  46. {
  47. if (isset($this->request->query['Search'])) {
  48. $searchParam = '/' . $this->request->query['Search'] . '/i';
  49. if($this->request->query['Search'])
  50. {
  51. return Anime::search($searchParam);
  52. }
  53. }
  54. }
  55. //Ensure the correct user here
  56. public function add($id = null)
  57. {
  58. //If id is null,
  59. if ('id' == null) {
  60. //Redirect them to the search page
  61. return $this->redirect('Animelist::addsearch');
  62. }
  63. //Find the requested anime
  64. $anime = Anime::find('first', array('conditions' => array('special_id' => $id)));
  65. $entry = null;
  66. //If data was submitted
  67. if ($this->request->data)
  68. {
  69. //Create an entry with the data
  70. $entry = Entry::create($this->request->data);
  71. //Get the current user
  72. $user = Auth::check('default');
  73. $username = $user['username'];
  74. //Break the tags into an array
  75. if (isset($this->request->data['tags']))
  76. {
  77. $entry->my_tags = explode(',', $this->request->data['tags']);
  78. unset($this->request->data['tags']);
  79. }
  80. if($entry->add($username))
  81. {
  82. //Redirect the user to their anime list
  83. return $this->redirect("/animelist/view/$username");
  84. }
  85. }
  86. return compact('entry', 'anime');
  87. }
  88. public function edit($id = null)
  89. {
  90. if (id != null)
  91. {
  92. //Get the ani
  93. }
  94. else
  95. {
  96. //Error message
  97. }
  98. }
  99. }