154 lines
3.4 KiB
PHP
Raw Normal View History

2014-05-14 23:24:20 +10:00
<?php namespace Backend\Widgets;
use Input;
use Backend\Classes\WidgetBase;
/**
* Search Widget
* Used for building a toolbar, Renders a search container.
*
* @package october\backend
* @author Alexey Bobkov, Samuel Georges
*/
class Search extends WidgetBase
{
/**
* {@inheritDoc}
*/
2015-02-28 12:43:53 +11:00
protected $defaultAlias = 'search';
2014-05-14 23:24:20 +10:00
/**
2014-07-08 18:21:40 +10:00
* @var string Search placeholder text.
2014-05-14 23:24:20 +10:00
*/
public $placeholder;
/**
2014-07-08 18:21:40 +10:00
* @var bool Field show grow when selected.
*/
public $growable = true;
/**
* @var string Active search term pulled from session data.
2014-05-14 23:24:20 +10:00
*/
public $activeTerm;
/**
* @var string Custom partial file definition, in context of the controller.
*/
public $customPartial;
2014-07-08 18:21:40 +10:00
/**
* @var array List of CSS classes to apply to the list container element.
*/
public $cssClasses = [];
2014-05-14 23:24:20 +10:00
/**
* Constructor.
*/
public function __construct($controller, $configuration = [])
{
parent::__construct($controller, $configuration);
/*
* Process configuration
*/
2014-10-11 00:39:34 +02:00
if (isset($this->config->prompt)) {
2014-05-14 23:24:20 +10:00
$this->placeholder = trans($this->config->prompt);
2014-10-11 00:39:34 +02:00
}
2014-05-14 23:24:20 +10:00
2014-10-11 00:39:34 +02:00
if (isset($this->config->partial)) {
2014-05-14 23:24:20 +10:00
$this->customPartial = $this->config->partial;
2014-10-11 00:39:34 +02:00
}
2014-07-08 18:21:40 +10:00
2014-10-11 00:39:34 +02:00
if (isset($this->config->growable)) {
2014-07-08 18:21:40 +10:00
$this->growable = $this->config->growable;
2014-10-11 00:39:34 +02:00
}
2014-07-08 18:21:40 +10:00
/*
* Add CSS class styles
*/
$this->cssClasses[] = 'icon search';
2014-10-11 00:39:34 +02:00
if ($this->growable) {
2014-07-08 18:21:40 +10:00
$this->cssClasses[] = 'growable';
2014-10-11 00:39:34 +02:00
}
2014-05-14 23:24:20 +10:00
}
/**
* Renders the widget.
*/
public function render()
{
$this->prepareVars();
2014-10-11 00:39:34 +02:00
if ($this->customPartial) {
2014-05-14 23:24:20 +10:00
return $this->controller->makePartial($this->customPartial);
}
else {
2014-05-14 23:24:20 +10:00
return $this->makePartial('search');
2014-10-11 00:39:34 +02:00
}
2014-05-14 23:24:20 +10:00
}
/**
* Prepares the view data
*/
public function prepareVars()
{
2014-07-08 18:21:40 +10:00
$this->vars['cssClasses'] = implode(' ', $this->cssClasses);
2014-05-14 23:24:20 +10:00
$this->vars['placeholder'] = $this->placeholder;
$this->vars['value'] = $this->getActiveTerm();
}
/**
* Search field has been submitted.
*/
public function onSubmit()
{
/*
* Save or reset search term in session
*/
$this->setActiveTerm(post($this->getName()));
2014-05-14 23:24:20 +10:00
/*
* Trigger class event, merge results as viewable array
*/
$params = func_get_args();
2014-05-15 17:23:46 +10:00
$result = $this->fireEvent('search.submit', [$params]);
2014-10-11 00:39:34 +02:00
if ($result && is_array($result)) {
return call_user_func_array('array_merge', $result);
2014-10-11 00:39:34 +02:00
}
2014-05-14 23:24:20 +10:00
}
/**
* Returns an active search term for this widget instance.
*/
public function getActiveTerm()
{
return $this->activeTerm = $this->getSession('term', '');
}
2014-07-08 18:21:40 +10:00
/**
* Sets an active search term for this widget instance.
*/
public function setActiveTerm($term)
{
2014-10-11 00:39:34 +02:00
if (strlen($term)) {
2014-07-08 18:21:40 +10:00
$this->putSession('term', $term);
}
else {
2014-07-08 18:21:40 +10:00
$this->resetSession();
2014-10-11 00:39:34 +02:00
}
2014-07-08 18:21:40 +10:00
$this->activeTerm = $term;
}
/**
* Returns a value suitable for the field name property.
* @return string
*/
public function getName()
{
return $this->alias . '[term]';
}
2014-10-11 00:39:34 +02:00
}