Browse Source

Moved search into the model

Michael Francis 14 năm trước cách đây
mục cha
commit
b1629b5d2f
2 tập tin đã thay đổi với 39 bổ sung23 xóa
  1. 20 23
      controllers/SearchController.php
  2. 19 0
      models/Anime.php

+ 20 - 23
controllers/SearchController.php

@@ -2,38 +2,35 @@
 
 namespace app\controllers; 
 
-use app\models\anime;
-use \MongoRegex;
+use app\models\Anime;
+use app\models\Manga;
+use app\models\Kdrama;
+
 
 class SearchController extends \lithium\action\Controller {
 	public $publicActions = array('index');
 
-	public function index($type, $by = "series_title")
-	{
+	protected $_types = array('Anime' => 'app\models\Anime',
+								  'Manga' => 'app\models\Manga',
+								  'Kdrama' => 'app\models\Kdrama',
 
-		if (empty($this->request->query['search'])) {
-			//Redirect them or something 
-		}
+								  'anime' => 'app\models\Anime',
+								  'manga' => 'app\models\Manga',
+								  'kdrama' => 'app\models\Kdrama');
 
-
-		$searchParam = '/' . $this->request->query['search'] . '/i';
+	public function index($type, $by = 'title')
+	{
 		
-		$content;
-		$limit = 20; 
-		$page = $this->request->page ?: 1;
-		$total; //<-- number of search results
+		$searchParam = '/' . $this->request->query['search'] . '/i';		
+		$page = $this->request->query['page'] ?: 1;
 
-
-		switch($type)
+		//If the type part of the URL is a valid type (as defined above),
+		if (isset($this->_types[$type]))
 		{
-			case "anime": $content = Anime::find('all', array('conditions' => array('title' => array('like' => $searchParam)), $limit, $page)); 
-			$total = Anime::count(array('title' => array('like' => $searchParam)));
-			break;
-			case "kdrama": break;
-			case "manga": break; 
+			$model = $this->_types[$type];
+
+			//Forcing search to title for now, until the search frontend is done
+			return $model::search($searchParam, $page, 'title');
 		}
-		return compact('content', 'type', 'by', 'limit', 'total', 'page');
 	}
-
-
 }

+ 19 - 0
models/Anime.php

@@ -5,4 +5,23 @@ namespace app\models;
 class Anime extends \lithium\data\Model {
 	protected $_meta = array('key' => '_id', 'source' => 'anime');
 
+		public static function search($query, $page = 1, $by = 'title') 
+		{
+			$defaults = array(
+				'limit' => 20 
+				);
+		
+		$limit = 20;
+
+		$content = self::find('all',
+			array(
+				'conditions' => array(
+					$by => array('like' => $query)
+				),
+				'limit' => $limit,
+				'page' => $page
+			));
+		$total = Anime::count(array('title' => array('like' => $query)));
+		return compact('content', 'by', 'limit', 'total', 'page');
+	}
 }