Improvements to data persistence, rework readOnly

Fixes #2308
This commit is contained in:
Samuel Georges 2016-09-27 20:21:32 +10:00
parent 1e3f5a22ef
commit 0d336151d2
3 changed files with 78 additions and 57 deletions

View File

@ -33,7 +33,7 @@ class RelationController extends ControllerBehavior
/**
* @var const Postback parameter for read only mode.
*/
const PARAM_READ_ONLY = '_relation_read_only';
const PARAM_EXTRA_CONFIG = '_relation_extra_config';
/**
* @var Backend\Classes\WidgetBase Reference to the search widget object.
@ -80,6 +80,11 @@ class RelationController extends ControllerBehavior
*/
protected $originalConfig;
/**
* @var array Config provided by the relationRender method
*/
protected $extraConfig;
/**
* @var bool Has the behavior been initialized.
*/
@ -242,8 +247,8 @@ class RelationController extends ControllerBehavior
$this->vars['relationViewWidget'] = $this->viewWidget;
$this->vars['relationViewModel'] = $this->viewModel;
$this->vars['relationPivotWidget'] = $this->pivotWidget;
$this->vars['relationReadOnly'] = $this->readOnly ? 1 : 0;
$this->vars['relationSessionKey'] = $this->relationGetSessionKey();
$this->vars['relationExtraConfig'] = $this->extraConfig;
}
/**
@ -305,6 +310,10 @@ class RelationController extends ControllerBehavior
throw new ApplicationException(Lang::get('backend::lang.relation.missing_definition', compact('field')));
}
if ($extraConfig = post(self::PARAM_EXTRA_CONFIG)) {
$this->applyExtraConfig($field, $extraConfig);
}
$this->alias = camel_case('relation ' . $field);
$this->config = $this->makeConfig($this->getConfig($field), $this->requiredRelationProperties);
$this->controller->relationExtendConfig($this->config, $this->field);
@ -317,6 +326,7 @@ class RelationController extends ControllerBehavior
$this->relationObject = $this->model->{$field}();
$this->relationModel = $this->relationObject->getRelated();
$this->readOnly = $this->getConfig('readOnly');
$this->deferredBinding = $this->getConfig('deferredBinding') || !$this->model->exists;
$this->toolbarButtons = $this->evalToolbarButtons();
$this->viewMode = $this->evalViewMode();
@ -364,13 +374,6 @@ class RelationController extends ControllerBehavior
$this->pivotWidget->bindToController();
}
}
/*
* Read only mode
*/
if (post(self::PARAM_READ_ONLY, $this->getConfig('readOnly'))) {
$this->makeReadOnly();
}
}
/**
@ -381,26 +384,30 @@ class RelationController extends ControllerBehavior
*/
public function relationRender($field, $options = [])
{
$field = $this->validateField($field);
/*
* Session key
*/
if (is_string($options)) {
$options = ['sessionKey' => $options];
}
/*
* Session key
*/
if (isset($options['sessionKey'])) {
$this->sessionKey = $options['sessionKey'];
}
/*
* Read only mode
* Apply options and extra config
*/
if (isset($options['readOnly']) && $options['readOnly']) {
$this->makeReadOnly();
$allowConfig = ['readOnly'];
if ($extraConfig = array_only($options, $allowConfig)) {
$this->extraConfig = $extraConfig;
$this->applyExtraConfig($field, $extraConfig);
}
/*
* Initialize
*/
$this->validateField($field);
$this->prepareVars();
/*
@ -526,9 +533,11 @@ class RelationController extends ControllerBehavior
/*
* Add buttons to toolbar
*/
$defaultButtons = $this->toolbarButtons
? '~/modules/backend/behaviors/relationcontroller/partials/_toolbar.htm'
: null;
$defaultButtons = null;
if (!$this->readOnly && $this->toolbarButtons) {
$defaultButtons = '~/modules/backend/behaviors/relationcontroller/partials/_toolbar.htm';
}
$defaultConfig['buttons'] = $this->getConfig('view[toolbarPartial]', $defaultButtons);
@ -596,15 +605,14 @@ class RelationController extends ControllerBehavior
$config->showSorting = $this->getConfig('view[showSorting]', true);
$config->defaultSort = $this->getConfig('view[defaultSort]');
$config->recordsPerPage = $this->getConfig('view[recordsPerPage]');
$config->showCheckboxes = $this->getConfig('view[showCheckboxes]', true);
$config->showCheckboxes = $this->getConfig('view[showCheckboxes]', !$this->readOnly);
$config->recordUrl = $this->getConfig('view[recordUrl]', null);
$defaultOnClick = sprintf(
"$.oc.relationBehavior.clickViewListRecord(':%s', '%s', '%s', %s)",
"$.oc.relationBehavior.clickViewListRecord(':%s', '%s', '%s')",
$this->relationModel->getKeyName(),
$this->field,
$this->relationGetSessionKey(),
$this->readOnly ? 1 : 0
$this->relationGetId(),
$this->relationGetSessionKey()
);
if ($config->recordUrl) {
@ -737,7 +745,7 @@ class RelationController extends ControllerBehavior
$config->recordOnClick = sprintf(
"$.oc.relationBehavior.clickManageListRecord(:%s, '%s', '%s')",
$this->relationModel->getKeyName(),
$this->field,
$this->relationGetId(),
$this->relationGetSessionKey()
);
}
@ -748,7 +756,7 @@ class RelationController extends ControllerBehavior
$config->recordOnClick = sprintf(
"$.oc.relationBehavior.clickManagePivotListRecord(:%s, '%s', '%s')",
$this->relationModel->getKeyName(),
$this->field,
$this->relationGetId(),
$this->relationGetSessionKey()
);
}
@ -1412,14 +1420,16 @@ class RelationController extends ControllerBehavior
case 'list':
if ($this->eventTarget == 'button-link') {
return 'backend::lang.relation.link_a_new';
} else {
}
else {
return 'backend::lang.relation.add_a_new';
}
break;
case 'form':
if ($this->readOnly) {
return 'backend::lang.relation.preview_name';
} else {
}
else {
return 'backend::lang.relation.update_name';
}
break;
@ -1492,18 +1502,15 @@ class RelationController extends ControllerBehavior
}
/**
* Apply read only mode.
* Apply extra configuration
*/
protected function makeReadOnly()
protected function applyExtraConfig($field, $config)
{
$this->readOnly = true;
if ($this->toolbarWidget && !$this->getConfig('view[toolbarPartial]', false)) {
$this->toolbarWidget->buttons = null;
}
if ($this->viewWidget && $this->viewMode == 'multi' && !$this->getConfig('view[showCheckboxes]', false)) {
$this->viewWidget->showCheckboxes = false;
if (is_array($config) && isset($this->originalConfig->{$field})) {
$this->originalConfig->{$field} = array_merge(
$this->originalConfig->{$field},
$config
);
}
}

View File

@ -9,38 +9,41 @@
$(el).closest('.control-list').listWidget('toggleChecked', [el])
}
this.clickViewListRecord = function(recordId, relationField, sessionKey, readOnly) {
var newPopup = $('<a />')
this.clickViewListRecord = function(recordId, relationId, sessionKey) {
var newPopup = $('<a />'),
$container = $('#'+relationId),
requestData = paramToObj('data-request-data', $container.data('request-data'))
newPopup.popup({
handler: 'onRelationClickViewList',
size: 'huge',
extraData: {
extraData: $.extend({}, requestData, {
'manage_id': recordId,
'_relation_field': relationField,
'_session_key': sessionKey,
'_relation_read_only': readOnly ? 1 : 0
}
'_session_key': sessionKey
})
})
}
this.clickManageListRecord = function(recordId, relationField, sessionKey) {
var oldPopup = $('#relationManagePopup')
this.clickManageListRecord = function(recordId, relationId, sessionKey) {
var oldPopup = $('#relationManagePopup'),
$container = $('#'+relationId),
requestData = paramToObj('data-request-data', $container.data('request-data'))
$.request('onRelationClickManageList', {
data: {
data: $.extend({}, requestData, {
'record_id': recordId,
'_relation_field': relationField,
'_session_key': sessionKey
}
})
})
oldPopup.popup('hide')
}
this.clickManagePivotListRecord = function(foreignId, relationField, sessionKey) {
this.clickManagePivotListRecord = function(foreignId, relationId, sessionKey) {
var oldPopup = $('#relationManagePivotPopup'),
newPopup = $('<a />')
newPopup = $('<a />'),
$container = $('#'+relationId),
requestData = paramToObj('data-request-data', $container.data('request-data'))
if (oldPopup.length) {
oldPopup.popup('hide')
@ -49,11 +52,10 @@
newPopup.popup({
handler: 'onRelationClickManageListPivot',
size: 'huge',
extraData: {
extraData: $.extend({}, requestData, {
'foreign_id': foreignId,
'_relation_field': relationField,
'_session_key': sessionKey
}
})
})
}
@ -71,6 +73,18 @@
})
}
function paramToObj(name, value) {
if (value === undefined) value = ''
if (typeof value == 'object') return value
try {
return JSON.parse(JSON.stringify(eval("({" + value + "})")))
}
catch (e) {
throw new Error('Error parsing the '+name+' attribute value. '+e)
}
}
}
$.oc.relationBehavior = new RelationBehavior;

View File

@ -1,6 +1,6 @@
<div
id="<?= $this->relationGetId() ?>"
data-request-data="_relation_field: '<?= $relationField ?>', _relation_read_only: <?= $relationReadOnly ?>"
data-request-data="_relation_field: '<?= $relationField ?>', _relation_extra_config: <?= e(json_encode($relationExtraConfig)) ?>"
class="relation-behavior relation-view-<?= $relationViewMode ?>">
<?php if ($toolbar = $this->relationRenderToolbar()): ?>