From 39daf773bccd4d7071615d445ca0dd325376a3f0 Mon Sep 17 00:00:00 2001 From: Samuel Georges Date: Wed, 21 Jan 2015 19:47:38 +1100 Subject: [PATCH] Implement hasOne RelationController type --- .../backend/behaviors/RelationController.php | 115 +++++++++++++++--- .../assets/css/relation.css | 3 + .../assets/less/relation.less | 4 + .../partials/_button_add.htm | 2 +- .../partials/_button_create.htm | 2 +- .../partials/_button_delete.htm | 38 +++--- .../partials/_button_remove.htm | 2 +- .../partials/_button_update.htm | 8 ++ .../partials/_container.htm | 2 +- .../relationcontroller/partials/_toolbar.htm | 13 +- 10 files changed, 147 insertions(+), 42 deletions(-) create mode 100644 modules/backend/behaviors/relationcontroller/partials/_button_update.htm diff --git a/modules/backend/behaviors/RelationController.php b/modules/backend/behaviors/RelationController.php index 1487ecbc5..beb374589 100644 --- a/modules/backend/behaviors/RelationController.php +++ b/modules/backend/behaviors/RelationController.php @@ -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 diff --git a/modules/backend/behaviors/relationcontroller/assets/css/relation.css b/modules/backend/behaviors/relationcontroller/assets/css/relation.css index a52d54882..defc86865 100644 --- a/modules/backend/behaviors/relationcontroller/assets/css/relation.css +++ b/modules/backend/behaviors/relationcontroller/assets/css/relation.css @@ -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; } diff --git a/modules/backend/behaviors/relationcontroller/assets/less/relation.less b/modules/backend/behaviors/relationcontroller/assets/less/relation.less index 61227fd7a..4d1936421 100644 --- a/modules/backend/behaviors/relationcontroller/assets/less/relation.less +++ b/modules/backend/behaviors/relationcontroller/assets/less/relation.less @@ -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 diff --git a/modules/backend/behaviors/relationcontroller/partials/_button_add.htm b/modules/backend/behaviors/relationcontroller/partials/_button_add.htm index 65f0640c3..64e1988f5 100644 --- a/modules/backend/behaviors/relationcontroller/partials/_button_add.htm +++ b/modules/backend/behaviors/relationcontroller/partials/_button_add.htm @@ -1,6 +1,6 @@ trans($relationLabel)])) ?> diff --git a/modules/backend/behaviors/relationcontroller/partials/_button_create.htm b/modules/backend/behaviors/relationcontroller/partials/_button_create.htm index 77ada5bbe..b4e3ec610 100644 --- a/modules/backend/behaviors/relationcontroller/partials/_button_create.htm +++ b/modules/backend/behaviors/relationcontroller/partials/_button_create.htm @@ -1,6 +1,6 @@ trans($relationLabel)])) ?> diff --git a/modules/backend/behaviors/relationcontroller/partials/_button_delete.htm b/modules/backend/behaviors/relationcontroller/partials/_button_delete.htm index 108ff4af2..838887ec8 100644 --- a/modules/backend/behaviors/relationcontroller/partials/_button_delete.htm +++ b/modules/backend/behaviors/relationcontroller/partials/_button_delete.htm @@ -1,14 +1,24 @@ - \ No newline at end of file + + + + + \ No newline at end of file diff --git a/modules/backend/behaviors/relationcontroller/partials/_button_remove.htm b/modules/backend/behaviors/relationcontroller/partials/_button_remove.htm index 2ddd2770f..6d318b7fe 100644 --- a/modules/backend/behaviors/relationcontroller/partials/_button_remove.htm +++ b/modules/backend/behaviors/relationcontroller/partials/_button_remove.htm @@ -4,7 +4,7 @@ checked: $('#relationGetId('view') ?> .control-list').listWidget('getChecked') })" disabled="disabled" - data-request="onRelationManageRemove" + data-request="onRelationButtonRemove" data-trigger-type="enable" data-trigger="#relationGetId('view') ?> .control-list input[type=checkbox]" data-trigger-condition="checked" diff --git a/modules/backend/behaviors/relationcontroller/partials/_button_update.htm b/modules/backend/behaviors/relationcontroller/partials/_button_update.htm new file mode 100644 index 000000000..0da1eec88 --- /dev/null +++ b/modules/backend/behaviors/relationcontroller/partials/_button_update.htm @@ -0,0 +1,8 @@ + + trans($relationLabel)])) ?> + diff --git a/modules/backend/behaviors/relationcontroller/partials/_container.htm b/modules/backend/behaviors/relationcontroller/partials/_container.htm index 9a9a8e856..9b2ddc976 100644 --- a/modules/backend/behaviors/relationcontroller/partials/_container.htm +++ b/modules/backend/behaviors/relationcontroller/partials/_container.htm @@ -1,6 +1,6 @@
+ class="relation-behavior relation-view-"> relationRenderToolbar()): ?> diff --git a/modules/backend/behaviors/relationcontroller/partials/_toolbar.htm b/modules/backend/behaviors/relationcontroller/partials/_toolbar.htm index 9657b6042..98616ec46 100644 --- a/modules/backend/behaviors/relationcontroller/partials/_toolbar.htm +++ b/modules/backend/behaviors/relationcontroller/partials/_toolbar.htm @@ -15,12 +15,17 @@ relationMakePartial('button_link') ?> relationMakePartial('button_unlink') ?> - + - relationMakePartial('button_create') ?> - relationMakePartial('button_update') ?> - relationMakePartial('button_delete') ?> + model->exists): ?> + relationMakePartial('button_update', ['relationManageId' => $relationViewWidget->model->id]) ?> + relationMakePartial('button_delete') ?> + + relationMakePartial('button_create') ?> + + +