Created
July 8, 2015 14:25
-
-
Save muratsplat/6e029907a70f37a24c97 to your computer and use it in GitHub Desktop.
SearchController on Laravel Apps
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Muratsplat\mvc\controller\front; | |
use Illuminate\Http\Request; | |
use Illuminate\Database\Eloquent\Collection; | |
use Muratsplat\Multilang\Interfaces\LangInterface; | |
use Muratsplat\Multilang\Interfaces\MainInterface; | |
use Muratsplat\mvc\model\Page\Page; | |
use Muratsplat\mvc\model\Page\PageLang; | |
use Muratsplat\mvc\model\Page\PageCategoryLang; | |
use Muratsplat\mvc\model\Reference\ReferenceLang; | |
use Muratsplat\mvc\model\News\News; | |
use Muratsplat\mvc\model\News\NewsLang; | |
use Muratsplat\mvc\controller\front\HomeBaseController; | |
/** | |
* Search item in resources | |
* | |
* @access public | |
* @author Murat Ödünç <[email protected]> | |
*/ | |
class SearchController extends HomeBaseController | |
{ | |
/** | |
* @var \Illuminate\Http\Request; | |
*/ | |
private $request; | |
/** | |
* @var \Muratsplat\mvc\model\News\News | |
*/ | |
private $news; | |
/** | |
* @var \Muratsplat\mvc\model\News\NewsLang | |
*/ | |
private $newsLang; | |
/** | |
* @var \Muratsplat\mvc\model\Page\Page | |
*/ | |
private $page; | |
/** | |
* @var \Muratsplat\mvc\model\Page\PageLang | |
*/ | |
private $pageLang; | |
/** | |
* @var \Illuminate\Database\Eloquent\Collection | |
*/ | |
private $rawCollection; | |
/** | |
* @var \Illuminate\Database\Eloquent\Collection | |
*/ | |
private $wrapperCollection; | |
/** | |
* @var \Muratsplat\mvc\model\Page\PageCategoryLang | |
*/ | |
private $pageCategoryLang; | |
/** | |
* @var \Muratsplat\mvc\model\Reference\ReferenceLang | |
*/ | |
private $referenceLang; | |
/** | |
* Validation rules | |
* | |
* @var type | |
*/ | |
private $rules = array( | |
'words' => 'min:3|max:20|required', | |
); | |
/** | |
* The list of viewer controller methods for model resurces | |
* | |
* @var array | |
*/ | |
private $controllerList = array( | |
'Muratsplat\mvc\model\Page\Page' => 'Muratsplat\mvc\controller\front\PageController@show', | |
'Muratsplat\mvc\model\Page\PageCategory' => 'Muratsplat\mvc\controller\front\PageCategoryController@show', | |
'Muratsplat\mvc\model\News\News' => 'Muratsplat\mvc\controller\front\NewsController@show', | |
'Muratsplat\mvc\model\Reference\Reference' => 'Muratsplat\mvc\controller\front\ReferenceController@show' | |
); | |
/** | |
* Constructer | |
* | |
* @param \Illuminate\Http\Request $request | |
* @param \Muratsplat\mvc\model\Page\Page $page | |
* @param \Muratsplat\mvc\model\Page\PageLang $pageLang | |
* @param \Muratsplat\mvc\model\Page\PageCategoryLang $pageCategoryLang | |
* @param \Muratsplat\mvc\model\News\News $news | |
* @param \Muratsplat\mvc\model\News\NewsLang $newsLang | |
* @param \Muratsplat\mvc\model\Reference\ReferenceLang $referenceLang | |
*/ | |
public function __construct( | |
Request $request, | |
Page $page, | |
PageLang $pageLang, | |
PageCategoryLang $pageCategoryLang, | |
News $news, | |
NewsLang $newsLang, | |
ReferenceLang $referenceLang | |
) { | |
parent::__construct(); | |
$this->request = $request; | |
$this->page = $page; | |
$this->pageLang = $pageLang; | |
$this->pageCategoryLang = $pageCategoryLang; | |
$this->news = $news; | |
$this->newsLang = $newsLang; | |
$this->referenceLang = $referenceLang; | |
$this->rawCollection = new Collection(); | |
$this->wrapperCollection= new Collection(); | |
} | |
/** | |
* To make response to search request | |
* | |
* @return Response | |
*/ | |
public function getSearch() { | |
$validation = $this->makeValidateToRequest(); | |
$words = $this->getWordsInParameter(); | |
if($validation->fails()) { | |
$fail = true; | |
return \View::make('Muratsplat\mvc\view\front::search') | |
->with(compact('fail', 'words', 'validation')); | |
} | |
$fail = false; | |
$this->searchWordsInDB($words); | |
$results = $this->getOnlyEnabledItemsInResults(); | |
$urlCreater = $this->getUrlCreaterForViewModelResources(); | |
$controllers= $this->controllerList; | |
return \View::make('Muratsplat\mvc\view\front::search') | |
->with(compact('results', 'words','fail', 'urlCreater', 'controllers')); | |
} | |
/** | |
* To get 'words' value in the request | |
* | |
* @return string | |
*/ | |
private function getWordsInParameter() { | |
return $this->request->get('words'); | |
} | |
/** | |
* To call all searching methods by using given words | |
* | |
* @param string $words | |
*/ | |
protected function searchWordsInDB($words='') { | |
$lists = explode(' ', $words); | |
$methods = $this->getSearchingMethods(); | |
foreach ($methods as $method) { | |
$this->callMethodWithParams($method, $lists); | |
} | |
} | |
/** | |
* To call given methods with parameters | |
* | |
* @param string $method | |
* @param array $words | |
* @return \Illuminate\Database\Eloquent\Collection | |
*/ | |
protected function callMethodWithParams($method, array $words = array()) { | |
return call_user_func_array([$this, $method], [$words]); | |
} | |
/** | |
* To get all searching methods in this object | |
* | |
* @return array | |
*/ | |
protected function getSearchingMethods() { | |
$searchMethods = get_class_methods($this); | |
return array_filter($searchMethods, function($item){ | |
if(strpos($item, 'searchIn') ===0) { | |
return true; | |
} | |
}); | |
} | |
/** | |
* To search words in page translation | |
* | |
* @param array $words | |
* @return Illuminate\Database\Eloquent\Collection | |
*/ | |
protected function searchInPageLang(array $words=array()) { | |
foreach ($words as $word) { | |
$resultTitle = $this->pageLang->query()->where('title', 'LIKE', '%'. $word .'%')->get(); | |
$this->addToCollectionToRawColletion($resultTitle); | |
$resultContent = $this->pageLang->query()->where('content', 'LIKE', '%'. $word .'%')->get(); | |
$this->addToCollectionToRawColletion($resultContent); | |
} | |
} | |
/** | |
* To search words in page category translation | |
* | |
* @param array $words | |
* @return Illuminate\Database\Eloquent\Collection | |
*/ | |
protected function searchInPageCategoryLang(array $words=array()) | |
{ | |
foreach ($words as $word) { | |
$resultTitle = $this->pageCategoryLang->query()->where('name', 'LIKE', '%'. $word .'%')->get(); | |
$this->addToCollectionToRawColletion($resultTitle); | |
} | |
} | |
/** | |
* To search words in page category translation | |
* | |
* @param array $words | |
* @return Illuminate\Database\Eloquent\Collection | |
*/ | |
protected function searchInReferenceLang(array $words=array()) | |
{ | |
foreach ($words as $word) { | |
$resultTitle = $this->referenceLang->query()->where('title', 'LIKE', '%'. $word .'%')->get(); | |
$this->addToCollectionToRawColletion($resultTitle); | |
$resultContent = $this->referenceLang->query()->where('content', 'LIKE', '%'. $word .'%')->get(); | |
$this->addToCollectionToRawColletion($resultContent); | |
} | |
} | |
/** | |
* To search words in news translation | |
* | |
* @param array $words | |
* @return Illuminate\Database\Eloquent\Collection | |
*/ | |
protected function searchInNewsLang(array $words=array()) | |
{ | |
foreach ($words as $word) { | |
$resultTitle = $this->newsLang->query()->where('title', 'LIKE', '%'. $word .'%')->get(); | |
$this->addToCollectionToRawColletion($resultTitle); | |
$resultContent = $this->newsLang->query()->where('content', 'LIKE', '%'. $word .'%')->get(); | |
$this->addToCollectionToRawColletion($resultContent); | |
} | |
} | |
/** | |
* To get resaults which is converted multilang wrappers | |
* | |
* @return Illuminate\Database\Eloquent\Collection | |
*/ | |
protected function getResultCollection() | |
{ | |
$this->rawCollection->each(function($item) { | |
if ($item instanceof MainInterface) { | |
$wrapper = $this->multilang->makeWrapper($item); | |
$this->addToWrapperToWrapperCollection($wrapper); | |
return; | |
} | |
$wrapper = $this->multilang->makeWrapper($item->mainModel, $item->__lang_id__); | |
$this->addToWrapperToWrapperCollection($wrapper); | |
}); | |
return $this->wrapperCollection; | |
} | |
/** | |
* To make validate client's request | |
* | |
* @return \Illuminate\Validation\Validator | |
*/ | |
private function makeValidateToRequest() { | |
return \Validator::make($this->request->all(), $this->rules); | |
} | |
/** | |
* To determine given wrapper in wrapper collection | |
* | |
* @param Object $wrapper | |
* @return bool | |
*/ | |
private function wrapperIsExists($wrapper) { | |
$results = $this->wrapperCollection->filter(function($item) use($wrapper){ | |
if ((integer) $item->id === (integer) $wrapper->id) { | |
return true; | |
} | |
}); | |
return !$results->isEmpty(); | |
} | |
/** | |
* To add wrapper if it is not exists | |
* | |
* @param object $wrapper | |
* @return void | |
*/ | |
private function addToWrapperToWrapperCollection($wrapper) { | |
if ($this->wrapperIsExists($wrapper)) { return;} | |
$this->wrapperCollection->push($wrapper); | |
} | |
/** | |
* Simple url creater | |
* | |
* @return callback | |
*/ | |
private function getUrlCreaterForViewModelResources() { | |
return function($model, $controllerList) { | |
$className = get_class($model->getMainModel()); | |
if(!array_key_exists($className, $controllerList)) { | |
return '#'; | |
} | |
if ($className ==='Muratsplat\mvc\model\Product\Product') { | |
return action($controllerList[$className], $model->code); | |
} | |
if ($className ==='Muratsplat\mvc\model\Page\PageCategory') { | |
return action($controllerList[$className], $model->id); | |
} | |
return action($controllerList[$className], $model->slug); | |
}; | |
} | |
/** | |
* To add items of given collection to rawCollection | |
* | |
* @param \Illuminate\Database\Eloquent\Collection $collection | |
*/ | |
private function addToCollectionToRawColletion(Collection $collection) | |
{ | |
if ($collection->isEmpty() ) { return; } | |
foreach ($collection as $one) { | |
$this->rawCollection->push($one); | |
} | |
} | |
/** | |
* To get only enabled items in result collection | |
* | |
* @return \Illuminate\Database\Eloquent\Collection | |
*/ | |
protected function getOnlyEnabledItemsInResults() | |
{ | |
return $this->getResultCollection()->filter(function($item){ | |
return (integer) $item->enable === 1; | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment