SearchController.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace app\controllers;
  3. use app\models\Anime;
  4. use app\models\Manga;
  5. use app\models\Kdrama;
  6. class SearchController extends \lithium\action\Controller {
  7. public $publicActions = array('index');
  8. protected $_types = array('Anime' => 'app\models\Anime',
  9. 'Manga' => 'app\models\Manga',
  10. 'Kdrama' => 'app\models\Kdrama',
  11. 'anime' => 'app\models\Anime',
  12. 'manga' => 'app\models\Manga',
  13. 'kdrama' => 'app\models\Kdrama');
  14. public function index($type, $by = 'title')
  15. {
  16. //Regex-ize the search param
  17. $searchParam = (isset($this->request->query['search'])) ?
  18. '/' . $this->request->query['search'] . '/i' : "";
  19. //Get the page number
  20. $page = isset($this->request->query['page']) ? $this->request->query['page'] : 1;
  21. $headers = array();
  22. switch ($type)
  23. {
  24. case 'Anime':
  25. case 'anime': $headers = array('title' => 'Title',
  26. 'episode_count' => 'Episodes',
  27. 'view_type' => 'Type', 'mal_score' => 'MAL Score');
  28. case 'kdrama':
  29. case 'Kdrama': $headers = array('title' => 'Title',
  30. 'episode_count' => 'Episodes');
  31. }
  32. //If the type part of the URL is a valid type (as defined above),
  33. if (isset($this->_types[$type]))
  34. {
  35. $model = $this->_types[$type];
  36. //Forcing search to title for now, until the search frontend is done
  37. $search = $model::search($searchParam, $page, 'title');
  38. return array('content' => $search['content'], 'page' => $search['page'], 'headers' => $headers, 'total' => $search['total'], 'limit' => $search['limit']);
  39. }
  40. }
  41. }