fillFromConfig([ 'title', 'prompt', 'keyFrom', 'nameFrom', 'descriptionFrom', 'scope', 'conditions', 'searchMode', 'searchScope', 'recordsPerPage', ]); if (post('recordfinder_flag')) { $this->listWidget = $this->makeListWidget(); $this->listWidget->bindToController(); $this->searchWidget = $this->makeSearchWidget(); $this->searchWidget->bindToController(); $this->listWidget->setSearchTerm($this->searchWidget->getActiveTerm()); /* * Link the Search Widget to the List Widget */ $this->searchWidget->bindEvent('search.submit', function () { $this->listWidget->setSearchTerm($this->searchWidget->getActiveTerm()); return $this->listWidget->onRefresh(); }); } } /** * @inheritDoc */ public function render() { $this->prepareVars(); return $this->makePartial('container'); } public function onRefresh() { list($model, $attribute) = $this->resolveModelAttribute($this->valueFrom); $model->{$attribute} = post($this->getFieldName()); $this->prepareVars(); return ['#'.$this->getId('container') => $this->makePartial('recordfinder')]; } /** * Prepares the list data */ public function prepareVars() { $this->relationModel = $this->getLoadValue(); $this->vars['value'] = $this->getKeyValue(); $this->vars['field'] = $this->formField; $this->vars['nameValue'] = $this->getNameValue(); $this->vars['descriptionValue'] = $this->getDescriptionValue(); $this->vars['listWidget'] = $this->listWidget; $this->vars['searchWidget'] = $this->searchWidget; $this->vars['title'] = $this->title; $this->vars['prompt'] = str_replace('%s', '', e(trans($this->prompt))); } /** * @inheritDoc */ protected function loadAssets() { $this->addJs('js/recordfinder.js', 'core'); } /** * @inheritDoc */ public function getSaveValue($value) { return strlen($value) ? $value : null; } /** * @inheritDoc */ public function getLoadValue() { list($model, $attribute) = $this->resolveModelAttribute($this->valueFrom); if (!is_null($model)) { return $model->{$attribute}; } return null; } public function getKeyValue() { if (!$this->relationModel) { return null; } return $this->relationModel->{$this->keyFrom}; } public function getNameValue() { if (!$this->relationModel || !$this->nameFrom) { return null; } return $this->relationModel->{$this->nameFrom}; } public function getDescriptionValue() { if (!$this->relationModel || !$this->descriptionFrom) { return null; } return $this->relationModel->{$this->descriptionFrom}; } public function onFindRecord() { $this->prepareVars(); /* * Purge the search term stored in session */ if ($this->searchWidget) { $this->listWidget->setSearchTerm(null); $this->searchWidget->setActiveTerm(null); } return $this->makePartial('recordfinder_form'); } protected function makeListWidget() { $config = $this->makeConfig($this->getConfig('list')); $config->model = $this->getRelationModel(); $config->alias = $this->alias . 'List'; $config->showSetup = false; $config->showCheckboxes = false; $config->recordsPerPage = $this->recordsPerPage; $config->recordOnClick = sprintf("$('#%s').recordFinder('updateRecord', this, ':" . $this->keyFrom . "')", $this->getId()); $widget = $this->makeWidget('Backend\Widgets\Lists', $config); $widget->setSearchOptions([ 'mode' => $this->searchMode, 'scope' => $this->searchScope, ]); if ($sqlConditions = $this->conditions) { $widget->bindEvent('list.extendQueryBefore', function($query) use ($sqlConditions) { $query->whereRaw($sqlConditions); }); } elseif ($scopeMethod = $this->scope) { $widget->bindEvent('list.extendQueryBefore', function($query) use ($scopeMethod) { $query->$scopeMethod($this->model); }); } else { $widget->bindEvent('list.extendQueryBefore', function($query) { $this->getRelationObject()->addDefinedConstraintsToQuery($query); }); } return $widget; } protected function makeSearchWidget() { $config = $this->makeConfig(); $config->alias = $this->alias . 'Search'; $config->growable = false; $config->prompt = 'backend::lang.list.search_prompt'; $widget = $this->makeWidget('Backend\Widgets\Search', $config); $widget->cssClasses[] = 'recordfinder-search'; return $widget; } }