Implement belongsTo RelationController type

This commit is contained in:
Samuel Georges 2015-01-21 20:53:19 +11:00
parent a0490c3a4e
commit 25835e5f55
7 changed files with 145 additions and 57 deletions

View File

@ -259,13 +259,16 @@ class RelationController extends ControllerBehavior
protected function evalManageMode()
{
switch ($this->relationType) {
case 'belongsTo':
$mode = 'list';
break;
case 'belongsToMany':
$mode = (isset($this->config->pivot)) ? 'pivot' : 'list';
break;
case 'hasOne':
case 'hasMany':
case 'belongsTo':
$mode = 'form';
break;
}
@ -317,10 +320,16 @@ class RelationController extends ControllerBehavior
* @param string $field Relation definition.
* @return array The relation element selector as the key, and the relation view contents are the value.
*/
public function relationRefresh($field)
public function relationRefresh($field = null)
{
$field = $this->validateField($field);
return ['#'.$this->relationGetId('view') => $this->relationRenderView()];
$result = ['#'.$this->relationGetId('view') => $this->relationRenderView()];
if ($toolbar = $this->relationRenderToolbar()) {
$result['#'.$this->relationGetId('toolbar')] = $toolbar;
}
return $result;
}
/**
@ -486,6 +495,16 @@ class RelationController extends ControllerBehavior
return $this->onRelationManageDelete();
}
public function onRelationButtonLink()
{
return $this->onRelationManageForm();
}
public function onRelationButtonUnlink()
{
return $this->onRelationManageRemove();
}
public function onRelationButtonRemove()
{
return $this->onRelationManageRemove();
@ -532,12 +551,7 @@ class RelationController extends ControllerBehavior
$this->viewWidget->model->save();
}
$result = ['#'.$this->relationGetId('view') => $this->relationRenderView()];
if ($toolbar = $this->relationRenderToolbar()) {
$result['#'.$this->relationGetId('toolbar')] = $toolbar;
}
return $result;
return $this->relationRefresh();
}
/**
@ -595,12 +609,7 @@ class RelationController extends ControllerBehavior
$this->viewWidget->model = $this->relationModel;
}
$result = ['#'.$this->relationGetId('view') => $this->relationRenderView()];
if ($toolbar = $this->relationRenderToolbar()) {
$result['#'.$this->relationGetId('toolbar')] = $toolbar;
}
return $result;
return $this->relationRefresh();
}
/**
@ -610,31 +619,48 @@ class RelationController extends ControllerBehavior
{
$this->beforeAjax();
if ($this->viewMode != 'multi') {
throw new ApplicationException(Lang::get('backend::lang.relation.invalid_action_single'));
$recordId = post('record_id');
/*
* Add
*/
if ($this->viewMode == 'multi') {
$checkedIds = $recordId ? [$recordId] : post('checked');
if (is_array($checkedIds)) {
/*
* Remove existing relations from the array
*/
$existingIds = $this->findExistingRelationIds($checkedIds);
$checkedIds = array_diff($checkedIds, $existingIds);
$foreignKeyName = $this->relationModel->getKeyName();
$models = $this->relationModel->whereIn($foreignKeyName, $checkedIds)->get();
foreach ($models as $model) {
if ($this->model->exists) {
$this->relationObject->add($model);
}
else {
$this->relationObject->add($model, $this->relationGetSessionKey());
}
}
}
}
if (($checkedIds = post('checked')) && is_array($checkedIds)) {
/*
* Remove existing relations from the array
*/
$existingIds = $this->findExistingRelationIds($checkedIds);
$checkedIds = array_diff($checkedIds, $existingIds);
$foreignKeyName = $this->relationModel->getKeyName();
$models = $this->relationModel->whereIn($foreignKeyName, $checkedIds)->get();
foreach ($models as $model) {
if ($this->model->exists) {
$this->relationObject->add($model);
}
else {
$this->relationObject->add($model, $this->relationGetSessionKey());
}
/*
* Link
*/
elseif ($this->viewMode == 'single') {
if ($recordId && ($model = $this->relationModel->find($recordId))) {
$this->relationObject->associate($model);
$this->relationObject->getParent()->save();
$this->viewWidget->setFormValues($model->attributes);
}
}
return ['#'.$this->relationGetId('view') => $this->relationRenderView()];
return $this->relationRefresh();
}
/**
@ -644,15 +670,29 @@ class RelationController extends ControllerBehavior
{
$this->beforeAjax();
if ($this->viewMode != 'multi') {
throw new ApplicationException(Lang::get('backend::lang.relation.invalid_action_single'));
$recordId = post('record_id');
/*
* Remove
*/
if ($this->viewMode == 'multi') {
$checkedIds = $recordId ? [$recordId] : post('checked');
if (is_array($checkedIds)) {
$this->relationObject->detach($checkedIds);
}
}
/*
* Unlink
*/
elseif ($this->viewMode == 'single') {
$this->relationObject->dissociate();
$this->relationObject->getParent()->save();
$this->viewWidget->setFormValues([]);
}
if (($checkedIds = post('checked')) && is_array($checkedIds)) {
$this->relationObject->detach($checkedIds);
}
return ['#'.$this->relationGetId('view') => $this->relationRenderView()];
return $this->relationRefresh();
}
public function onRelationManagePivotForm()
@ -768,7 +808,7 @@ class RelationController extends ControllerBehavior
$config->showCheckboxes = $this->getConfig('view[showCheckboxes]', !$this->readOnly);
$defaultOnClick = sprintf(
"$.oc.relationBehavior.clickManageListRecord(:id, '%s', '%s')",
"$.oc.relationBehavior.clickViewListRecord(:id, '%s', '%s')",
$this->field,
$this->relationGetSessionKey()
);
@ -861,6 +901,16 @@ class RelationController extends ControllerBehavior
$config->showSorting = $this->getConfig('manage[showSorting]', true);
$config->defaultSort = $this->getConfig('manage[defaultSort]');
$config->recordsPerPage = $this->getConfig('manage[recordsPerPage]');
if ($this->viewMode == 'single') {
$config->showCheckboxes = false;
$config->recordOnClick = sprintf(
"$.oc.relationBehavior.clickManageListRecord(:id, '%s', '%s')",
$this->field,
$this->relationGetSessionKey()
);
}
$widget = $this->makeWidget('Backend\Widgets\Lists', $config);
/*

View File

@ -6,6 +6,20 @@
var RelationBehavior = function() {
this.clickManageListRecord = function(recordId, relationField, sessionKey) {
var oldPopup = $('#relationManagePopup')
$.request('onRelationManageAdd', {
data: {
'record_id': recordId,
'_relation_field': relationField,
'_session_key': sessionKey
}
})
oldPopup.popup('hide')
}
this.clickViewListRecord = function(recordId, relationField, sessionKey) {
var newPopup = $('<a />')
newPopup.popup({

View File

@ -0,0 +1,7 @@
<a
data-control="popup"
data-handler="onRelationButtonLink"
href="javascript:;"
class="btn btn-sm btn-primary oc-icon-link">
<?= e(trans('backend::lang.relation.link_name', ['name'=>trans($relationLabel)])) ?>
</a>

View File

@ -0,0 +1,8 @@
<a
href="javascript:;"
class="btn btn-sm btn-default oc-icon-unlink"
data-request="onRelationButtonUnlink"
data-request-confirm="<?= e(trans('backend::lang.relation.unlink_confirm')) ?>"
data-stripe-load-indicator>
<?= e(trans('backend::lang.relation.unlink_name', ['name'=>trans($relationLabel)])) ?>
</a>

View File

@ -1,5 +1,5 @@
<?= Form::open() ?>
<div data-request-data="_relation_field: '<?= $relationField ?>'">
<div id="relationManagePopup" data-request-data="_relation_field: '<?= $relationField ?>'">
<?= Form::open() ?>
<div class="modal-header">
<button type="button" class="close" data-dismiss="popup">&times;</button>
<h4 class="modal-title"><?= e(trans('backend::lang.relation.add_a_new', ['name'=>$relationLabel])) ?></h4>
@ -13,14 +13,16 @@
</div>
<div class="modal-footer">
<button
type="button"
class="btn btn-primary"
data-request="onRelationManageAdd"
data-dismiss="popup"
data-stripe-load-indicator>
<?= e(trans('backend::lang.relation.add_selected')) ?>
</button>
<?php if ($relationManageWidget->showCheckboxes): ?>
<button
type="button"
class="btn btn-primary"
data-request="onRelationManageAdd"
data-dismiss="popup"
data-stripe-load-indicator>
<?= e(trans('backend::lang.relation.add_selected')) ?>
</button>
<?php endif ?>
<button
type="button"
class="btn btn-default"
@ -28,5 +30,5 @@
<?= e(trans('backend::lang.relation.cancel')) ?>
</button>
</div>
</div>
<?= Form::close() ?>
<?= Form::close() ?>
</div>

View File

@ -10,7 +10,7 @@
<?= $this->relationMakePartial('button_add') ?>
<?= $this->relationMakePartial('button_remove') ?>
<?php elseif ($relationType == 'belongsToTODO'): ?>
<?php elseif ($relationType == 'belongsTo'): ?>
<?= $this->relationMakePartial('button_link') ?>
<?= $this->relationMakePartial('button_unlink') ?>

View File

@ -196,6 +196,8 @@ return [
'add' => "Add",
'add_selected' => "Add selected",
'add_a_new' => "Add a new :name",
'link_selected' => "Link selected",
'link_a_new' => "Link a new :name",
'cancel' => "Cancel",
'close' => "Close",
'add_name' => "Add :name",
@ -210,6 +212,11 @@ return [
'delete' => "Delete",
'delete_name' => "Delete :name",
'delete_confirm' => "Are you sure?",
'link' => "Link",
'link_name' => "Link :name",
'unlink' => "Unlink",
'unlink_name' => "Unlink :name",
'unlink_confirm' => "Are you sure?",
],
'model' => [
'name' => 'Model',