Bugfix to relation controller now allowing multiple relations in the same instance

This commit is contained in:
Sam Georges 2014-07-11 18:51:01 +10:00
parent acce6eb902
commit 1d4d4c0899
1 changed files with 27 additions and 16 deletions

View File

@ -58,6 +58,16 @@ class RelationController extends ControllerBehavior
*/ */
protected $requiredConfig = []; protected $requiredConfig = [];
/**
* @var array Original configuration values
*/
private $originalConfig;
/**
* @var bool Has the behavior been initialized.
*/
private $initialized = false;
/** /**
* @var string Relationship type * @var string Relationship type
*/ */
@ -93,11 +103,6 @@ class RelationController extends ControllerBehavior
*/ */
protected $alias; protected $alias;
/**
* @var bool Has the behavior been initialized.
*/
protected $initialized = false;
/** /**
* @var string Relation has many (multi) or has one (single). * @var string Relation has many (multi) or has one (single).
*/ */
@ -138,7 +143,7 @@ class RelationController extends ControllerBehavior
/* /*
* Build configuration * Build configuration
*/ */
$this->config = $this->makeConfig($controller->relationConfig, $this->requiredConfig); $this->config = $this->originalConfig = $this->makeConfig($controller->relationConfig, $this->requiredConfig);
} }
/** /**
@ -149,8 +154,7 @@ class RelationController extends ControllerBehavior
*/ */
public function initRelation($model, $field = null) public function initRelation($model, $field = null)
{ {
$field = $field ?: post(self::PARAM_FIELD); $this->config = $this->originalConfig;
$this->model = $model; $this->model = $model;
$this->field = $field; $this->field = $field;
@ -207,8 +211,6 @@ class RelationController extends ControllerBehavior
if ($this->pivotWidget = $this->makePivotWidget()) if ($this->pivotWidget = $this->makePivotWidget())
$this->pivotWidget->bindToController(); $this->pivotWidget->bindToController();
} }
$this->initialized = true;
} }
/** /**
@ -323,10 +325,12 @@ class RelationController extends ControllerBehavior
*/ */
private function validateField($field) private function validateField($field)
{ {
if (!$this->initialized) $field = $field ?: post(self::PARAM_FIELD);
if ($field && $field != $this->field)
$this->initRelation($this->model, $field); $this->initRelation($this->model, $field);
if ($field == null && !$this->initialized) if (!$field && !$this->field)
throw new ApplicationException(Lang::get('backend::lang.relation.missing_definition', compact('field'))); throw new ApplicationException(Lang::get('backend::lang.relation.missing_definition', compact('field')));
return $field ?: $this->field; return $field ?: $this->field;
@ -355,11 +359,13 @@ class RelationController extends ControllerBehavior
*/ */
protected function beforeAjax() protected function beforeAjax()
{ {
if ($this->initialized) return; if ($this->initialized)
return;
$this->controller->pageAction(); $this->controller->pageAction();
$this->validateField(post(self::PARAM_FIELD)); $this->validateField();
$this->prepareVars(); $this->prepareVars();
$this->initialized = true;
} }
/** /**
@ -578,8 +584,13 @@ class RelationController extends ControllerBehavior
$config = $this->makeConfig($this->config->list); $config = $this->makeConfig($this->config->list);
$config->model = $this->relationModel; $config->model = $this->relationModel;
$config->alias = $this->alias . 'ViewList'; $config->alias = $this->alias . 'ViewList';
$config->recordOnClick = sprintf("$.oc.relationBehavior.clickManageListRecord(:id, '%s', '%s')", $this->field, $this->relationGetSessionKey()); $config->showSorting = $this->getConfig('showSorting', true);
$config->showCheckboxes = true; $config->defaultSort = $this->getConfig('defaultSort');
if (!$this->readOnly) {
$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; $config->noRecordsMessage = $emptyMessage;