Updating backend/behaviours

This commit is contained in:
Stefan Talen 2014-10-10 22:34:57 +02:00
parent d2d9f89117
commit b01d3e540f
4 changed files with 260 additions and 129 deletions

View File

@ -84,19 +84,19 @@ class FormController extends ControllerBehavior
*/
$this->formWidget = $this->makeWidget('Backend\Widgets\Form', $config);
$this->formWidget->bindEvent('form.extendFieldsBefore', function() {
$this->formWidget->bindEvent('form.extendFieldsBefore', function () {
$this->controller->formExtendFieldsBefore($this->formWidget);
});
$this->formWidget->bindEvent('form.extendFields', function() {
$this->formWidget->bindEvent('form.extendFields', function () {
$this->controller->formExtendFields($this->formWidget);
});
$this->formWidget->bindEvent('form.beforeRefresh', function($saveData) {
$this->formWidget->bindEvent('form.beforeRefresh', function ($saveData) {
return $this->controller->formExtendRefreshData($this->formWidget, $saveData);
});
$this->formWidget->bindEvent('form.refresh', function($result) {
$this->formWidget->bindEvent('form.refresh', function ($result) {
return $this->controller->formExtendRefreshResults($this->formWidget, $result);
});
@ -105,8 +105,9 @@ class FormController extends ControllerBehavior
/*
* Detected Relation controller behavior
*/
if ($this->controller->isClassExtendedWith('Backend.Behaviors.RelationController'))
if ($this->controller->isClassExtendedWith('Backend.Behaviors.RelationController')) {
$this->controller->initRelation($model);
}
}
//
@ -122,13 +123,15 @@ class FormController extends ControllerBehavior
{
try {
$this->context = strlen($context) ? $context : $this->getConfig('create[context]', 'create');
$this->controller->pageTitle = $this->controller->pageTitle ?: $this->getLang('create[title]', 'backend::lang.form.create_title');
$this->controller->pageTitle = $this->controller->pageTitle ?: $this->getLang(
'create[title]',
'backend::lang.form.create_title'
);
$model = $this->controller->formCreateModelObject();
$this->initForm($model);
$this->controller->vars['formModel'] = $model;
}
catch (Exception $ex) {
} catch (Exception $ex) {
$this->controller->handleError($ex);
}
}
@ -156,8 +159,9 @@ class FormController extends ControllerBehavior
Flash::success($this->getLang('create[flashSave]', 'backend::lang.form.create_success'));
if ($redirect = $this->makeRedirect('create', $model))
if ($redirect = $this->makeRedirect('create', $model)) {
return $redirect;
}
}
//
@ -174,13 +178,15 @@ class FormController extends ControllerBehavior
{
try {
$this->context = strlen($context) ? $context : $this->getConfig('update[context]', 'update');
$this->controller->pageTitle = $this->controller->pageTitle ?: $this->getLang('update[title]', 'backend::lang.form.update_title');
$this->controller->pageTitle = $this->controller->pageTitle ?: $this->getLang(
'update[title]',
'backend::lang.form.update_title'
);
$model = $this->controller->formFindModelObject($recordId);
$this->initForm($model);
$this->controller->vars['formModel'] = $model;
}
catch (Exception $ex) {
} catch (Exception $ex) {
$this->controller->handleError($ex);
}
}
@ -209,8 +215,9 @@ class FormController extends ControllerBehavior
Flash::success($this->getLang('update[flashSave]', 'backend::lang.form.update_success'));
if ($redirect = $this->makeRedirect('update', $model))
if ($redirect = $this->makeRedirect('update', $model)) {
return $redirect;
}
}
/**
@ -230,8 +237,9 @@ class FormController extends ControllerBehavior
Flash::success($this->getLang('update[flashDelete]', 'backend::lang.form.delete_success'));
if ($redirect = $this->makeRedirect('delete', $model))
if ($redirect = $this->makeRedirect('delete', $model)) {
return $redirect;
}
}
//
@ -248,13 +256,15 @@ class FormController extends ControllerBehavior
{
try {
$this->context = strlen($context) ? $context : $this->getConfig('preview[context]', 'preview');
$this->controller->pageTitle = $this->controller->pageTitle ?: $this->getLang('preview[title]', 'backend::lang.form.preview_title');
$this->controller->pageTitle = $this->controller->pageTitle ?: $this->getLang(
'preview[title]',
'backend::lang.form.preview_title'
);
$model = $this->controller->formFindModelObject($recordId);
$this->initForm($model);
$this->controller->vars['formModel'] = $model;
}
catch (Exception $ex) {
} catch (Exception $ex) {
$this->controller->handleError($ex);
}
}
@ -270,8 +280,9 @@ class FormController extends ControllerBehavior
*/
public function formRender($options = [])
{
if (!$this->formWidget)
if (!$this->formWidget) {
throw new ApplicationException(Lang::get('backend::lang.form.behavior_not_ready'));
}
return $this->formWidget->render($options);
}
@ -307,14 +318,17 @@ class FormController extends ControllerBehavior
public function makeRedirect($context = null, $model = null)
{
$redirectUrl = null;
if (post('close') && !ends_with($context, '-close'))
if (post('close') && !ends_with($context, '-close')) {
$context .= '-close';
}
if (post('redirect', true))
if (post('redirect', true)) {
$redirectUrl = Backend::url($this->getRedirectUrl($context));
}
if ($model && $redirectUrl)
if ($model && $redirectUrl) {
$redirectUrl = RouterHelper::parseValues($model, array_keys($model->getAttributes()), $redirectUrl);
}
return ($redirectUrl) ? Redirect::to($redirectUrl) : null;
}
@ -336,8 +350,9 @@ class FormController extends ControllerBehavior
'preview' => $this->getConfig('preview[redirect]', ''),
];
if (!isset($redirects[$context]))
if (!isset($redirects[$context])) {
return $redirects['default'];
}
return $redirects[$context];
}
@ -444,43 +459,57 @@ class FormController extends ControllerBehavior
* Called before the creation or updating form is saved.
* @param Model
*/
public function formBeforeSave($model) {}
public function formBeforeSave($model)
{
}
/**
* Called after the creation or updating form is saved.
* @param Model
*/
public function formAfterSave($model) {}
public function formAfterSave($model)
{
}
/**
* Called before the creation form is saved.
* @param Model
*/
public function formBeforeCreate($model) {}
public function formBeforeCreate($model)
{
}
/**
* Called after the creation form is saved.
* @param Model
*/
public function formAfterCreate($model) {}
public function formAfterCreate($model)
{
}
/**
* Called before the updating form is saved.
* @param Model
*/
public function formBeforeUpdate($model) {}
public function formBeforeUpdate($model)
{
}
/**
* Called after the updating form is saved.
* @param Model
*/
public function formAfterUpdate($model) {}
public function formAfterUpdate($model)
{
}
/**
* Called after the form model is deleted.
* @param Model
*/
public function formAfterDelete($model) {}
public function formAfterDelete($model)
{
}
/**
@ -528,14 +557,18 @@ class FormController extends ControllerBehavior
* @param Backend\Widgets\Form $host The hosting form widget
* @return void
*/
public function formExtendFieldsBefore($host) {}
public function formExtendFieldsBefore($host)
{
}
/**
* Called after the form fields are defined.
* @param Backend\Widgets\Form $host The hosting form widget
* @return void
*/
public function formExtendFields($host) {}
public function formExtendFields($host)
{
}
/**
* Called before the form is refreshed, should return an array of additional save data.
@ -543,7 +576,9 @@ class FormController extends ControllerBehavior
* @param array $saveData Current save data
* @return array
*/
public function formExtendRefreshData($host, $saveData) {}
public function formExtendRefreshData($host, $saveData)
{
}
/**
* Called after the form is refreshed, should return an array of additional result parameters.
@ -551,7 +586,9 @@ class FormController extends ControllerBehavior
* @param array $result Current result parameters.
* @return array
*/
public function formExtendRefreshResults($host, $result) {}
public function formExtendRefreshResults($host, $result)
{
}
/**
* Extend supplied model used by create and update actions, the model can
@ -570,7 +607,9 @@ class FormController extends ControllerBehavior
* @param October\Rain\Database\Builder $query
* @return void
*/
public function formExtendQuery($query) {}
public function formExtendQuery($query)
{
}
/**
* Static helper for extending form fields.
@ -580,8 +619,10 @@ class FormController extends ControllerBehavior
public static function extendFormFields($callback)
{
$calledClass = self::getCalledExtensionClass();
Event::listen('backend.form.extendFields', function($widget) use ($calledClass, $callback) {
if (!is_a($widget->getController(), $calledClass)) return;
Event::listen('backend.form.extendFields', function ($widget) use ($calledClass, $callback) {
if (!is_a($widget->getController(), $calledClass)) {
return;
}
$callback($widget, $widget->model, $widget->getContext());
});
}
@ -607,15 +648,21 @@ class FormController extends ControllerBehavior
{
$this->modelsToSave[] = $model;
if (!is_array($saveData))
if (!is_array($saveData)) {
return;
}
$singularTypes = ['belongsTo', 'hasOne', 'morphOne'];
foreach ($saveData as $attribute => $value) {
if (is_array($value) && $model->hasRelation($attribute) && in_array($model->getRelationType($attribute), $singularTypes))
if (
is_array($value) &&
$model->hasRelation($attribute) &&
in_array($model->getRelationType($attribute), $singularTypes)
) {
$this->setModelAttributes($model->{$attribute}, $value);
else
} else {
$model->{$attribute} = $value;
}
}
}
}

View File

@ -68,8 +68,7 @@ class ListController extends ControllerBehavior
if (is_array($controller->listConfig)) {
$this->listDefinitions = $controller->listConfig;
$this->primaryDefinition = key($this->listDefinitions);
}
else {
} else {
$this->listDefinitions = ['list' => $controller->listConfig];
$this->primaryDefinition = 'list';
}
@ -99,8 +98,9 @@ class ListController extends ControllerBehavior
*/
public function makeList($definition = null)
{
if (!$definition || !isset($this->listDefinitions[$definition]))
if (!$definition || !isset($this->listDefinitions[$definition])) {
$definition = $this->primaryDefinition;
}
$listConfig = $this->makeConfig($this->listDefinitions[$definition], $this->requiredConfig);
@ -117,39 +117,59 @@ class ListController extends ControllerBehavior
$columnConfig = $this->makeConfig($listConfig->list);
$columnConfig->model = $model;
$columnConfig->alias = $definition;
if (isset($listConfig->recordUrl)) $columnConfig->recordUrl = $listConfig->recordUrl;
if (isset($listConfig->recordOnClick)) $columnConfig->recordOnClick = $listConfig->recordOnClick;
if (isset($listConfig->recordsPerPage)) $columnConfig->recordsPerPage = $listConfig->recordsPerPage;
if (isset($listConfig->noRecordsMessage)) $columnConfig->noRecordsMessage = $listConfig->noRecordsMessage;
if (isset($listConfig->defaultSort)) $columnConfig->defaultSort = $listConfig->defaultSort;
if (isset($listConfig->showSorting)) $columnConfig->showSorting = $listConfig->showSorting;
if (isset($listConfig->showSetup)) $columnConfig->showSetup = $listConfig->showSetup;
if (isset($listConfig->showCheckboxes)) $columnConfig->showCheckboxes = $listConfig->showCheckboxes;
if (isset($listConfig->showTree)) $columnConfig->showTree = $listConfig->showTree;
if (isset($listConfig->treeExpanded)) $columnConfig->treeExpanded = $listConfig->treeExpanded;
if (isset($listConfig->recordUrl)) {
$columnConfig->recordUrl = $listConfig->recordUrl;
}
if (isset($listConfig->recordOnClick)) {
$columnConfig->recordOnClick = $listConfig->recordOnClick;
}
if (isset($listConfig->recordsPerPage)) {
$columnConfig->recordsPerPage = $listConfig->recordsPerPage;
}
if (isset($listConfig->noRecordsMessage)) {
$columnConfig->noRecordsMessage = $listConfig->noRecordsMessage;
}
if (isset($listConfig->defaultSort)) {
$columnConfig->defaultSort = $listConfig->defaultSort;
}
if (isset($listConfig->showSorting)) {
$columnConfig->showSorting = $listConfig->showSorting;
}
if (isset($listConfig->showSetup)) {
$columnConfig->showSetup = $listConfig->showSetup;
}
if (isset($listConfig->showCheckboxes)) {
$columnConfig->showCheckboxes = $listConfig->showCheckboxes;
}
if (isset($listConfig->showTree)) {
$columnConfig->showTree = $listConfig->showTree;
}
if (isset($listConfig->treeExpanded)) {
$columnConfig->treeExpanded = $listConfig->treeExpanded;
}
$widget = $this->makeWidget('Backend\Widgets\Lists', $columnConfig);
$widget->bindToController();
/*
* Extensibility helpers
*/
$widget->bindEvent('list.extendQueryBefore', function($query) use ($definition) {
$widget->bindEvent('list.extendQueryBefore', function ($query) use ($definition) {
$this->controller->listExtendQueryBefore($query, $definition);
});
$widget->bindEvent('list.extendQuery', function($query) use ($definition) {
$widget->bindEvent('list.extendQuery', function ($query) use ($definition) {
$this->controller->listExtendQuery($query, $definition);
});
$widget->bindEvent('list.injectRowClass', function($record) use ($definition) {
$widget->bindEvent('list.injectRowClass', function ($record) use ($definition) {
return $this->controller->listInjectRowClass($record, $definition);
});
$widget->bindEvent('list.overrideColumnValue', function($record, $column, $value) use ($definition) {
$widget->bindEvent('list.overrideColumnValue', function ($record, $column, $value) use ($definition) {
return $this->controller->listOverrideColumnValue($record, $column->columnName, $definition);
});
$widget->bindEvent('list.overrideHeaderValue', function($column, $value) use ($definition) {
$widget->bindEvent('list.overrideHeaderValue', function ($column, $value) use ($definition) {
return $this->controller->listOverrideHeaderValue($column->columnName, $definition);
});
@ -167,7 +187,7 @@ class ListController extends ControllerBehavior
* Link the Search Widget to the List Widget
*/
if ($searchWidget = $toolbarWidget->getSearchWidget()) {
$searchWidget->bindEvent('search.submit', function() use ($widget, $searchWidget) {
$searchWidget->bindEvent('search.submit', function () use ($widget, $searchWidget) {
$widget->setSearchTerm($searchWidget->getActiveTerm());
return $widget->onRefresh();
});
@ -193,7 +213,7 @@ class ListController extends ControllerBehavior
/*
* Filter the list when the scopes are changed
*/
$filterWidget->bindEvent('filter.update', function() use ($widget, $filterWidget){
$filterWidget->bindEvent('filter.update', function () use ($widget, $filterWidget) {
$widget->addFilter([$filterWidget, 'applyAllScopesToQuery']);
return $widget->onRefresh();
});
@ -213,7 +233,10 @@ class ListController extends ControllerBehavior
*/
public function index()
{
$this->controller->pageTitle = $this->controller->pageTitle ?: trans($this->getConfig('title', 'backend::lang.list.default_title'));
$this->controller->pageTitle = $this->controller->pageTitle ?: trans($this->getConfig(
'title',
'backend::lang.list.default_title'
));
$this->controller->bodyClass = 'slim-container';
$this->makeLists();
}
@ -225,19 +248,23 @@ class ListController extends ControllerBehavior
*/
public function listRender($definition = null)
{
if (!count($this->listWidgets))
if (!count($this->listWidgets)) {
throw new SystemException(Lang::get('backend::lang.list.behavior_not_ready'));
}
if (!$definition || !isset($this->listDefinitions[$definition]))
if (!$definition || !isset($this->listDefinitions[$definition])) {
$definition = $this->primaryDefinition;
}
$collection = [];
if (isset($this->toolbarWidgets[$definition]))
if (isset($this->toolbarWidgets[$definition])) {
$collection[] = $this->toolbarWidgets[$definition]->render();
}
if (isset($this->filterWidgets[$definition]))
if (isset($this->filterWidgets[$definition])) {
$collection[] = $this->filterWidgets[$definition]->render();
}
$collection[] = $this->listWidgets[$definition]->render();
@ -251,11 +278,13 @@ class ListController extends ControllerBehavior
*/
public function listRefresh($definition = null)
{
if (!count($this->listWidgets))
if (!count($this->listWidgets)) {
$this->makeLists();
}
if (!$definition || !isset($this->listDefinitions[$definition]))
if (!$definition || !isset($this->listDefinitions[$definition])) {
$definition = $this->primaryDefinition;
}
return $this->listWidgets[$definition]->onRefresh();
}
@ -279,14 +308,18 @@ class ListController extends ControllerBehavior
* before the default query is processed.
* @param October\Rain\Database\Builder $query
*/
public function listExtendQueryBefore($query, $definition = null) {}
public function listExtendQueryBefore($query, $definition = null)
{
}
/**
* Controller override: Extend the query used for populating the list
* after the default query is processed.
* @param October\Rain\Database\Builder $query
*/
public function listExtendQuery($query, $definition = null) {}
public function listExtendQuery($query, $definition = null)
{
}
/**
* Returns a CSS class name for a list row (<tr class="...">).
@ -294,7 +327,9 @@ class ListController extends ControllerBehavior
* @param string $definition List definition (optional)
* @return string HTML view
*/
public function listInjectRowClass($record, $definition = null) {}
public function listInjectRowClass($record, $definition = null)
{
}
/**
* Replace a table column value (<td>...</td>)
@ -303,7 +338,9 @@ class ListController extends ControllerBehavior
* @param string $definition List definition (optional)
* @return string HTML view
*/
public function listOverrideColumnValue($record, $columnName, $definition = null) {}
public function listOverrideColumnValue($record, $columnName, $definition = null)
{
}
/**
* Replace the entire table header contents (<th>...</th>) with custom HTML
@ -311,7 +348,9 @@ class ListController extends ControllerBehavior
* @param string $definition List definition (optional)
* @return string HTML view
*/
public function listOverrideHeaderValue($columnName, $definition = null) {}
public function listOverrideHeaderValue($columnName, $definition = null)
{
}
/**
* Static helper for extending form fields.
@ -321,10 +360,11 @@ class ListController extends ControllerBehavior
public static function extendListColumns($callback)
{
$calledClass = self::getCalledExtensionClass();
Event::listen('backend.list.extendColumns', function($widget) use ($calledClass, $callback) {
if (!is_a($widget->getController(), $calledClass)) return;
Event::listen('backend.list.extendColumns', function ($widget) use ($calledClass, $callback) {
if (!is_a($widget->getController(), $calledClass)) {
return;
}
$callback($widget, $widget->model);
});
}
}
}

View File

@ -158,24 +158,35 @@ class RelationController extends ControllerBehavior
*/
public function initRelation($model, $field = null)
{
if ($field == null)
if ($field == null) {
$field = post(self::PARAM_FIELD);
}
$this->config = $this->originalConfig;
$this->model = $model;
$this->field = $field;
if ($field == null)
if ($field == null) {
return;
}
if (!$this->model)
throw new ApplicationException(Lang::get('backend::lang.relation.missing_model', ['class'=>get_class($this->controller)]));
if (!$this->model) {
throw new ApplicationException(Lang::get(
'backend::lang.relation.missing_model',
['class'=>get_class($this->controller)]
));
}
if (!$this->model instanceof Model)
throw new ApplicationException(Lang::get('backend::lang.model.invalid_class', ['model'=>get_class($this->model), 'class'=>get_class($this->controller)]));
if (!$this->model instanceof Model) {
throw new ApplicationException(Lang::get(
'backend::lang.model.invalid_class',
['model'=>get_class($this->model), 'class'=>get_class($this->controller)]
));
}
if (!$this->getConfig($field))
if (!$this->getConfig($field)) {
throw new ApplicationException(Lang::get('backend::lang.relation.missing_definition', compact('field')));
}
$this->alias = camel_case('relation ' . $field);
$this->config = $this->makeConfig($this->getConfig($field), $this->requiredRelationProperties);
@ -196,27 +207,31 @@ class RelationController extends ControllerBehavior
/*
* Toolbar widget
*/
if ($this->toolbarWidget = $this->makeToolbarWidget())
if ($this->toolbarWidget = $this->makeToolbarWidget()) {
$this->toolbarWidget->bindToController();
}
/*
* View widget
*/
if ($this->viewWidget = $this->makeViewWidget())
if ($this->viewWidget = $this->makeViewWidget()) {
$this->viewWidget->bindToController();
}
/*
* Manage widget
*/
if ($this->manageWidget = $this->makeManageWidget())
if ($this->manageWidget = $this->makeManageWidget()) {
$this->manageWidget->bindToController();
}
/*
* Pivot widget
*/
if ($this->manageMode == 'pivot') {
if ($this->pivotWidget = $this->makePivotWidget())
if ($this->pivotWidget = $this->makePivotWidget()) {
$this->pivotWidget->bindToController();
}
}
}
@ -268,15 +283,18 @@ class RelationController extends ControllerBehavior
{
$field = $this->validateField($field);
if (is_string($options)) $options = ['sessionKey' => $options];
if (is_string($options)) {
$options = ['sessionKey' => $options];
}
$this->prepareVars();
/*
* Session key
*/
if (isset($options['sessionKey']))
if (isset($options['sessionKey'])) {
$this->sessionKey = $options['sessionKey'];
}
/*
* Determine the partial to use based on the supplied section option
@ -334,11 +352,13 @@ class RelationController extends ControllerBehavior
{
$field = $field ?: post(self::PARAM_FIELD);
if ($field && $field != $this->field)
if ($field && $field != $this->field) {
$this->initRelation($this->model, $field);
}
if (!$field && !$this->field)
if (!$field && !$this->field) {
throw new ApplicationException(Lang::get('backend::lang.relation.missing_definition', compact('field')));
}
return $field ?: $this->field;
}
@ -368,8 +388,9 @@ class RelationController extends ControllerBehavior
*/
protected function beforeAjax()
{
if ($this->initialized)
if ($this->initialized) {
return;
}
$this->controller->pageAction();
$this->validateField();
@ -386,8 +407,9 @@ class RelationController extends ControllerBehavior
public function relationMakePartial($partial, $params = [])
{
$contents = $this->controller->makePartial('relation_'.$partial, $params + $this->vars, false);
if (!$contents)
if (!$contents) {
$contents = $this->makePartial($partial, $params);
}
return $contents;
}
@ -400,11 +422,13 @@ class RelationController extends ControllerBehavior
public function relationGetId($suffix = null)
{
$id = class_basename($this);
if ($this->field)
if ($this->field) {
$id .= '-' . $this->field;
}
if ($suffix !== null)
if ($suffix !== null) {
$id .= '-' . $suffix;
}
return $this->controller->getId($id);
}
@ -420,8 +444,9 @@ class RelationController extends ControllerBehavior
->getBaseQuery()
->select($foreignKeyName);
if ($checkIds !== null && is_array($checkIds) && count($checkIds))
if ($checkIds !== null && is_array($checkIds) && count($checkIds)) {
$results = $results->whereIn($foreignKeyName, $checkIds);
}
return $results->lists($foreignKeyName);
}
@ -434,8 +459,9 @@ class RelationController extends ControllerBehavior
{
$this->beforeAjax();
if ($this->manageMode == 'pivot' && $this->manageId)
if ($this->manageMode == 'pivot' && $this->manageId) {
return $this->onRelationManagePivotForm();
}
// The form should not share its session key with the parent
$this->vars['newSessionKey'] = str_random(40);
@ -480,8 +506,9 @@ class RelationController extends ControllerBehavior
if (($checkedIds = post('checked')) && is_array($checkedIds)) {
foreach ($checkedIds as $relationId) {
if (!$obj = $this->relationObject->find($relationId))
if (!$obj = $this->relationObject->find($relationId)) {
continue;
}
$obj->delete();
}
@ -497,8 +524,9 @@ class RelationController extends ControllerBehavior
{
$this->beforeAjax();
if ($this->viewMode != 'multi')
if ($this->viewMode != 'multi') {
throw new ApplicationException(Lang::get('backend::lang.relation.invalid_action_single'));
}
if (($checkedIds = post('checked')) && is_array($checkedIds)) {
/*
@ -511,10 +539,11 @@ class RelationController extends ControllerBehavior
$models = $this->relationModel->whereIn($foreignKeyName, $checkedIds)->get();
foreach ($models as $model) {
if ($this->model->exists)
if ($this->model->exists) {
$this->relationObject->add($model);
else
} else {
$this->relationObject->add($model, $this->relationGetSessionKey());
}
}
}
@ -528,8 +557,9 @@ class RelationController extends ControllerBehavior
{
$this->beforeAjax();
if ($this->viewMode != 'multi')
if ($this->viewMode != 'multi') {
throw new ApplicationException(Lang::get('backend::lang.relation.invalid_action_single'));
}
if (($checkedIds = post('checked')) && is_array($checkedIds)) {
$this->relationObject->detach($checkedIds);
@ -560,8 +590,9 @@ class RelationController extends ControllerBehavior
$foreignKeyName = $this->relationModel->getKeyName();
$existing = $this->relationObject->where($foreignKeyName, $foreignId)->count();
if (!$existing)
if (!$existing) {
$this->relationObject->add($foreignModel, null, $saveData);
}
return ['#'.$this->relationGetId('view') => $this->relationRenderView()];
}
@ -593,8 +624,9 @@ class RelationController extends ControllerBehavior
protected function makeToolbarWidget()
{
if ($this->readOnly)
if ($this->readOnly) {
return;
}
$defaultConfig = [
'buttons' => '@/modules/backend/behaviors/relationcontroller/partials/_toolbar.htm',
@ -631,18 +663,23 @@ class RelationController extends ControllerBehavior
$config->recordsPerPage = $this->getConfig('view[recordsPerPage]');
if (!$this->readOnly) {
$config->recordOnClick = sprintf("$.oc.relationBehavior.clickManageListRecord(:id, '%s', '%s')", $this->field, $this->relationGetSessionKey());
$config->recordOnClick = sprintf(
"$.oc.relationBehavior.clickManageListRecord(:id, '%s', '%s')",
$this->field,
$this->relationGetSessionKey()
);
$config->showCheckboxes = true;
}
if ($emptyMessage = $this->getConfig('emptyMessage'))
if ($emptyMessage = $this->getConfig('emptyMessage')) {
$config->noRecordsMessage = $emptyMessage;
}
/*
* Constrain the query by the relationship and deferred items
*/
$widget = $this->makeWidget('Backend\Widgets\Lists', $config);
$widget->bindEvent('list.extendQuery', function($query) {
$widget->bindEvent('list.extendQuery', function ($query) {
$this->relationObject->setQuery($query);
if ($this->model->exists) {
$this->relationObject->addConstraints();
@ -657,7 +694,7 @@ class RelationController extends ControllerBehavior
*/
if ($this->toolbarWidget && $this->getConfig('view[showSearch]')) {
if ($searchWidget = $this->toolbarWidget->getSearchWidget()) {
$searchWidget->bindEvent('search.submit', function() use ($widget, $searchWidget) {
$searchWidget->bindEvent('search.submit', function () use ($widget, $searchWidget) {
$widget->setSearchTerm($searchWidget->getActiveTerm());
return $widget->onRefresh();
});
@ -665,11 +702,10 @@ class RelationController extends ControllerBehavior
$searchWidget->setActiveTerm(null);
}
}
}
/*
* Single (belongs to, has one)
*/
elseif ($this->viewMode == 'single') {
} elseif ($this->viewMode == 'single') {
$config = $this->makeConfig($this->config->form);
$config->model = $this->relationModel;
$config->arrayName = class_basename($this->relationModel);
@ -693,13 +729,16 @@ class RelationController extends ControllerBehavior
$config->model = $this->relationModel;
$config->alias = $this->alias . 'ManagePivotList';
$config->showSetup = false;
$config->recordOnClick = sprintf("$.oc.relationBehavior.clickManagePivotListRecord(:id, '%s', '%s')", $this->field, $this->relationGetSessionKey());
$config->recordOnClick = sprintf(
"$.oc.relationBehavior.clickManagePivotListRecord(:id, '%s', '%s')",
$this->field,
$this->relationGetSessionKey()
);
$widget = $this->makeWidget('Backend\Widgets\Lists', $config);
}
/*
* List
*/
elseif ($this->manageMode == 'list' && isset($this->config->list)) {
} elseif ($this->manageMode == 'list' && isset($this->config->list)) {
$config = $this->makeConfig($this->config->list);
$config->model = $this->relationModel;
$config->alias = $this->alias . 'ManageList';
@ -716,19 +755,17 @@ class RelationController extends ControllerBehavior
if ($this->getConfig('manage[showSearch]')) {
$this->searchWidget = $this->makeSearchWidget();
$this->searchWidget->bindToController();
$this->searchWidget->bindEvent('search.submit', function() use ($widget) {
$this->searchWidget->bindEvent('search.submit', function () use ($widget) {
$widget->setSearchTerm($this->searchWidget->getActiveTerm());
return $widget->onRefresh();
});
$this->searchWidget->setActiveTerm(null);
}
}
/*
* Form
*/
elseif ($this->manageMode == 'form' && isset($this->config->form)) {
} elseif ($this->manageMode == 'form' && isset($this->config->form)) {
$config = $this->makeConfig($this->config->form);
$config->model = $this->relationModel;
$config->arrayName = class_basename($this->relationModel);
@ -750,14 +787,15 @@ class RelationController extends ControllerBehavior
$widget = $this->makeWidget('Backend\Widgets\Form', $config);
}
if (!$widget)
if (!$widget) {
return null;
}
/*
* Exclude existing relationships
*/
if ($this->manageMode == 'pivot' || $this->manageMode == 'list') {
$widget->bindEvent('list.extendQuery', function($query) {
$widget->bindEvent('list.extendQuery', function ($query) {
/*
* Where not in the current list of related records
@ -806,16 +844,18 @@ class RelationController extends ControllerBehavior
*/
public function relationGetSessionKey($force = false)
{
if ($this->sessionKey && !$force)
if ($this->sessionKey && !$force) {
return $this->sessionKey;
}
if (post('_relation_session_key'))
if (post('_relation_session_key')) {
return $this->sessionKey = post('_relation_session_key');
}
if (post('_session_key'))
if (post('_session_key')) {
return $this->sessionKey = post('_session_key');
}
return $this->sessionKey = FormHelper::getSessionKey();
}
}
}

View File

@ -35,8 +35,9 @@ class UserPreferencesModel extends SettingsModel
*/
public function instance()
{
if (isset(self::$instances[$this->recordCode]))
if (isset(self::$instances[$this->recordCode])) {
return self::$instances[$this->recordCode];
}
$item = UserPreferences::forUser();
$item = $item->scopeFindRecord($this->model, $this->recordCode, $item->userContext)->first();
@ -44,10 +45,11 @@ class UserPreferencesModel extends SettingsModel
if (!$item) {
$this->model->initSettingsData();
if (method_exists($this->model, 'forceSave'))
if (method_exists($this->model, 'forceSave')) {
$this->model->forceSave();
else
} else {
$this->model->save();
}
$this->model->reload();
$item = $this->model;
@ -77,8 +79,9 @@ class UserPreferencesModel extends SettingsModel
$this->model->namespace = $namespace;
$this->model->user_id = $preferences->userContext->id;
if ($this->fieldValues)
if ($this->fieldValues) {
$this->model->value = $this->fieldValues;
}
}
/**
@ -90,9 +93,10 @@ class UserPreferencesModel extends SettingsModel
/*
* Let the core columns through
*/
if ($key == 'namespace' || $key == 'group')
if ($key == 'namespace' || $key == 'group') {
return true;
}
return parent::isKeyAllowed($key);
}
}
}