Implement hasOne RelationController type
This commit is contained in:
parent
560acc5220
commit
39daf773bc
|
|
@ -375,7 +375,9 @@ class RelationController extends ControllerBehavior
|
|||
$this->vars['relationType'] = $this->relationType;
|
||||
$this->vars['relationSearchWidget'] = $this->searchWidget;
|
||||
$this->vars['relationToolbarWidget'] = $this->toolbarWidget;
|
||||
$this->vars['relationManageMode'] = $this->manageMode;
|
||||
$this->vars['relationManageWidget'] = $this->manageWidget;
|
||||
$this->vars['relationViewMode'] = $this->viewMode;
|
||||
$this->vars['relationViewWidget'] = $this->viewWidget;
|
||||
$this->vars['relationPivotWidget'] = $this->pivotWidget;
|
||||
$this->vars['relationSessionKey'] = $this->relationGetSessionKey();
|
||||
|
|
@ -454,18 +456,46 @@ class RelationController extends ControllerBehavior
|
|||
//
|
||||
// Overrides
|
||||
//
|
||||
|
||||
|
||||
/**
|
||||
* Controller override: Extend the query used for populating the list
|
||||
* after the default query is processed.
|
||||
* @param October\Rain\Database\Builder $query
|
||||
* @param string $field
|
||||
* @param string $manageMode
|
||||
*/
|
||||
public function relationExtendQuery($query, $field, $manageMode)
|
||||
public function relationExtendQuery($query, $field)
|
||||
{
|
||||
}
|
||||
|
||||
//
|
||||
// AJAX (Buttons)
|
||||
//
|
||||
|
||||
public function onRelationButtonAdd()
|
||||
{
|
||||
return $this->onRelationManageForm();
|
||||
}
|
||||
|
||||
public function onRelationButtonCreate()
|
||||
{
|
||||
return $this->onRelationManageForm();
|
||||
}
|
||||
|
||||
public function onRelationButtonDelete()
|
||||
{
|
||||
return $this->onRelationManageDelete();
|
||||
}
|
||||
|
||||
public function onRelationButtonRemove()
|
||||
{
|
||||
return $this->onRelationManageRemove();
|
||||
}
|
||||
|
||||
public function onRelationButtonUpdate()
|
||||
{
|
||||
return $this->onRelationManageForm();
|
||||
}
|
||||
|
||||
//
|
||||
// AJAX
|
||||
//
|
||||
|
|
@ -491,12 +521,23 @@ class RelationController extends ControllerBehavior
|
|||
public function onRelationManageCreate()
|
||||
{
|
||||
$this->beforeAjax();
|
||||
|
||||
$saveData = $this->manageWidget->getSaveData();
|
||||
$newModel = $this->relationObject->create($saveData, $this->relationGetSessionKey(true));
|
||||
$newModel->commitDeferred($this->manageWidget->getSessionKey());
|
||||
|
||||
return ['#'.$this->relationGetId('view') => $this->relationRenderView()];
|
||||
if ($this->viewMode == 'multi') {
|
||||
$newModel = $this->relationObject->create($saveData, $this->relationGetSessionKey(true));
|
||||
$newModel->commitDeferred($this->manageWidget->getSessionKey());
|
||||
}
|
||||
elseif ($this->viewMode == 'single') {
|
||||
$this->viewWidget->setFormValues($saveData);
|
||||
$this->viewWidget->model->save();
|
||||
}
|
||||
|
||||
$result = ['#'.$this->relationGetId('view') => $this->relationRenderView()];
|
||||
if ($toolbar = $this->relationRenderToolbar()) {
|
||||
$result['#'.$this->relationGetId('toolbar')] = $toolbar;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -505,9 +546,16 @@ class RelationController extends ControllerBehavior
|
|||
public function onRelationManageUpdate()
|
||||
{
|
||||
$this->beforeAjax();
|
||||
|
||||
$saveData = $this->manageWidget->getSaveData();
|
||||
$this->relationObject->find($this->manageId)->save($saveData, $this->manageWidget->getSessionKey());
|
||||
|
||||
if ($this->viewMode == 'multi') {
|
||||
$model = $this->relationObject->find($this->manageId);
|
||||
$model->save($saveData, $this->manageWidget->getSessionKey());
|
||||
}
|
||||
elseif ($this->viewMode == 'single') {
|
||||
$this->viewWidget->setFormValues($saveData);
|
||||
$this->viewWidget->model->save();
|
||||
}
|
||||
|
||||
return ['#'.$this->relationGetId('view') => $this->relationRenderView()];
|
||||
}
|
||||
|
|
@ -519,18 +567,40 @@ class RelationController extends ControllerBehavior
|
|||
{
|
||||
$this->beforeAjax();
|
||||
|
||||
if (($checkedIds = post('checked')) && is_array($checkedIds)) {
|
||||
$relatedModel = $this->relationObject->getRelated();
|
||||
foreach ($checkedIds as $relationId) {
|
||||
if (!$obj = $relatedModel->find($relationId)) {
|
||||
continue;
|
||||
}
|
||||
/*
|
||||
* Multiple (has many, belongs to many)
|
||||
*/
|
||||
if ($this->viewMode == 'multi') {
|
||||
if (($checkedIds = post('checked')) && is_array($checkedIds)) {
|
||||
$relatedModel = $this->relationObject->getRelated();
|
||||
foreach ($checkedIds as $relationId) {
|
||||
if (!$obj = $relatedModel->find($relationId)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$obj->delete();
|
||||
$obj->delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
/*
|
||||
* Single (belongs to, has one)
|
||||
*/
|
||||
elseif ($this->viewMode == 'single') {
|
||||
$relatedModel = $this->viewWidget->model;
|
||||
if ($relatedModel->exists) {
|
||||
$relatedModel->delete();
|
||||
}
|
||||
|
||||
return ['#'.$this->relationGetId('view') => $this->relationRenderView()];
|
||||
$this->viewWidget->setFormValues([]);
|
||||
$this->viewWidget->model = $this->relationModel;
|
||||
}
|
||||
|
||||
$result = ['#'.$this->relationGetId('view') => $this->relationRenderView()];
|
||||
if ($toolbar = $this->relationRenderToolbar()) {
|
||||
$result['#'.$this->relationGetId('toolbar')] = $toolbar;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -715,7 +785,7 @@ class RelationController extends ControllerBehavior
|
|||
*/
|
||||
$widget = $this->makeWidget('Backend\Widgets\Lists', $config);
|
||||
$widget->bindEvent('list.extendQuery', function ($query) {
|
||||
$this->controller->relationExtendQuery($query, $this->field, $this->manageMode);
|
||||
$this->controller->relationExtendQuery($query, $this->field);
|
||||
|
||||
$this->relationObject->setQuery($query);
|
||||
if ($this->model->exists) {
|
||||
|
|
@ -744,13 +814,18 @@ class RelationController extends ControllerBehavior
|
|||
* Single (belongs to, has one)
|
||||
*/
|
||||
elseif ($this->viewMode == 'single') {
|
||||
$query = $this->relationObject;
|
||||
$this->controller->relationExtendQuery($query, $this->field);
|
||||
$model = $query->getResults() ?: $this->relationModel;
|
||||
|
||||
$config = $this->makeConfig($this->config->form);
|
||||
$config->model = $this->relationModel;
|
||||
$config->model = $model;
|
||||
$config->arrayName = class_basename($this->relationModel);
|
||||
$config->context = 'relation';
|
||||
$config->alias = $this->alias . 'ViewForm';
|
||||
|
||||
$widget = $this->makeWidget('Backend\Widgets\Form', $config);
|
||||
$widget->previewMode = true;
|
||||
}
|
||||
|
||||
return $widget;
|
||||
|
|
@ -836,7 +911,7 @@ class RelationController extends ControllerBehavior
|
|||
*/
|
||||
if ($this->manageMode == 'pivot' || $this->manageMode == 'list') {
|
||||
$widget->bindEvent('list.extendQuery', function ($query) {
|
||||
$this->controller->relationExtendQuery($query, $this->field, $this->manageMode);
|
||||
$this->controller->relationExtendQuery($query, $this->field);
|
||||
|
||||
/*
|
||||
* Where not in the current list of related records
|
||||
|
|
|
|||
|
|
@ -11,6 +11,9 @@
|
|||
.relation-behavior .control-list:last-child > table {
|
||||
margin-bottom: 0;
|
||||
}
|
||||
.relation-behavior.relation-view-single {
|
||||
padding: 0 20px;
|
||||
}
|
||||
.relation-flush .relation-behavior {
|
||||
border-top: 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,6 +17,10 @@
|
|||
.control-list:last-child > table {
|
||||
margin-bottom: 0;
|
||||
}
|
||||
|
||||
&.relation-view-single {
|
||||
padding: 0 20px;
|
||||
}
|
||||
}
|
||||
|
||||
// Relation manager to sit flush to the element above
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
<a
|
||||
data-control="popup"
|
||||
data-handler="onRelationManageForm"
|
||||
data-handler="onRelationButtonAdd"
|
||||
href="javascript:;"
|
||||
class="btn btn-sm btn-primary oc-icon-plus">
|
||||
<?= e(trans('backend::lang.relation.add_name', ['name'=>trans($relationLabel)])) ?>
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
<a
|
||||
data-control="popup"
|
||||
data-handler="onRelationManageForm"
|
||||
data-handler="onRelationButtonCreate"
|
||||
href="javascript:;"
|
||||
class="btn btn-sm btn-primary oc-icon-plus">
|
||||
<?= e(trans('backend::lang.relation.create_name', ['name'=>trans($relationLabel)])) ?>
|
||||
|
|
|
|||
|
|
@ -1,14 +1,24 @@
|
|||
<button
|
||||
class="btn btn-sm btn-default oc-icon-trash-o"
|
||||
onclick="$(this).data('request-data', {
|
||||
checked: $('#<?= $this->relationGetId('view') ?> .control-list').listWidget('getChecked')
|
||||
})"
|
||||
disabled="disabled"
|
||||
data-request="onRelationManageDelete"
|
||||
data-request-confirm="<?= e(trans('backend::lang.relation.delete_confirm')) ?>"
|
||||
data-trigger-type="enable"
|
||||
data-trigger="#<?= $this->relationGetId('view') ?> .control-list input[type=checkbox]"
|
||||
data-trigger-condition="checked"
|
||||
data-stripe-load-indicator>
|
||||
<?= e(trans('backend::lang.relation.delete')) ?>
|
||||
</button>
|
||||
<?php if ($relationViewMode == 'single'): ?>
|
||||
<button
|
||||
class="btn btn-sm btn-default oc-icon-trash-o"
|
||||
data-request="onRelationButtonDelete"
|
||||
data-request-confirm="<?= e(trans('backend::lang.relation.delete_confirm')) ?>"
|
||||
data-stripe-load-indicator>
|
||||
<?= e(trans('backend::lang.relation.delete')) ?>
|
||||
</button>
|
||||
<?php else: ?>
|
||||
<button
|
||||
class="btn btn-sm btn-default oc-icon-trash-o"
|
||||
onclick="$(this).data('request-data', {
|
||||
checked: $('#<?= $this->relationGetId('view') ?> .control-list').listWidget('getChecked')
|
||||
})"
|
||||
disabled="disabled"
|
||||
data-request="onRelationButtonDelete"
|
||||
data-request-confirm="<?= e(trans('backend::lang.relation.delete_confirm')) ?>"
|
||||
data-trigger-type="enable"
|
||||
data-trigger="#<?= $this->relationGetId('view') ?> .control-list input[type=checkbox]"
|
||||
data-trigger-condition="checked"
|
||||
data-stripe-load-indicator>
|
||||
<?= e(trans('backend::lang.relation.delete')) ?>
|
||||
</button>
|
||||
<?php endif ?>
|
||||
|
|
@ -4,7 +4,7 @@
|
|||
checked: $('#<?= $this->relationGetId('view') ?> .control-list').listWidget('getChecked')
|
||||
})"
|
||||
disabled="disabled"
|
||||
data-request="onRelationManageRemove"
|
||||
data-request="onRelationButtonRemove"
|
||||
data-trigger-type="enable"
|
||||
data-trigger="#<?= $this->relationGetId('view') ?> .control-list input[type=checkbox]"
|
||||
data-trigger-condition="checked"
|
||||
|
|
|
|||
|
|
@ -0,0 +1,8 @@
|
|||
<a
|
||||
data-control="popup"
|
||||
data-handler="onRelationButtonUpdate"
|
||||
data-request-data="manage_id: '<?= $relationManageId ?>'"
|
||||
href="javascript:;"
|
||||
class="btn btn-sm btn-primary oc-icon-pencil">
|
||||
<?= e(trans('backend::lang.relation.update_name', ['name'=>trans($relationLabel)])) ?>
|
||||
</a>
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
<div
|
||||
data-request-data="_relation_field: '<?= $relationField ?>'" id="<?= $this->relationGetId() ?>"
|
||||
class="relation-behavior">
|
||||
class="relation-behavior relation-view-<?= $relationViewMode ?>">
|
||||
|
||||
<?php if ($toolbar = $this->relationRenderToolbar()): ?>
|
||||
<!-- Relation Toolbar -->
|
||||
|
|
|
|||
|
|
@ -15,12 +15,17 @@
|
|||
<?= $this->relationMakePartial('button_link') ?>
|
||||
<?= $this->relationMakePartial('button_unlink') ?>
|
||||
|
||||
<?php elseif ($relationType == 'hasOneTODO'): ?>
|
||||
<?php elseif ($relationType == 'hasOne'): ?>
|
||||
|
||||
<?= $this->relationMakePartial('button_create') ?>
|
||||
<?= $this->relationMakePartial('button_update') ?>
|
||||
<?= $this->relationMakePartial('button_delete') ?>
|
||||
<?php if ($relationViewWidget->model->exists): ?>
|
||||
<?= $this->relationMakePartial('button_update', ['relationManageId' => $relationViewWidget->model->id]) ?>
|
||||
<?= $this->relationMakePartial('button_delete') ?>
|
||||
<?php else: ?>
|
||||
<?= $this->relationMakePartial('button_create') ?>
|
||||
<?php endif ?>
|
||||
|
||||
<?php endif ?>
|
||||
|
||||
|
||||
|
||||
</div>
|
||||
|
|
|
|||
Loading…
Reference in New Issue