winter/modules/backend/traits/SearchableWidget.php

44 lines
915 B
PHP
Raw Normal View History

2014-09-10 20:48:53 -07:00
<?php namespace Backend\Traits;
use Str;
/**
* Searchable Widget Trait
* Adds search features to back-end widgets
*
* @package winter\wn-backend-module
2014-09-10 20:48:53 -07:00
* @author Alexey Bobkov, Samuel Georges
*/
trait SearchableWidget
{
protected $searchTerm = false;
protected function getSearchTerm()
{
return $this->searchTerm !== false ? $this->searchTerm : $this->getSession('search');
}
protected function setSearchTerm($term)
{
$this->searchTerm = trim($term);
$this->putSession('search', $this->searchTerm);
}
protected function textMatchesSearch(&$words, $text)
{
foreach ($words as $word) {
$word = trim($word);
2014-10-11 00:07:30 +02:00
if (!strlen($word)) {
2014-09-10 20:48:53 -07:00
continue;
2014-10-11 00:07:30 +02:00
}
2014-09-10 20:48:53 -07:00
2014-10-11 00:07:30 +02:00
if (Str::contains(Str::lower($text), $word)) {
2014-09-10 20:48:53 -07:00
return true;
2014-10-11 00:07:30 +02:00
}
2014-09-10 20:48:53 -07:00
}
return false;
}
2014-10-11 00:07:30 +02:00
}