2014-05-14 23:24:20 +10:00
|
|
|
<?php namespace Backend\FormWidgets;
|
|
|
|
|
|
|
|
use Lang;
|
|
|
|
use Backend\Classes\FormWidgetBase;
|
|
|
|
use System\Classes\SystemException;
|
2014-09-23 07:55:40 +10:00
|
|
|
use Illuminate\Database\Eloquent\Relations\Relation as RelationBase;
|
2014-05-14 23:24:20 +10:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Form Relationship
|
|
|
|
* Renders a field prepopulated with a belongsTo and belongsToHasMany relation.
|
|
|
|
*
|
|
|
|
* @package october\backend
|
|
|
|
* @author Alexey Bobkov, Samuel Georges
|
|
|
|
*/
|
|
|
|
class Relation extends FormWidgetBase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public $defaultAlias = 'relation';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string Relationship type
|
|
|
|
*/
|
|
|
|
public $relationType;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string Relationship name
|
|
|
|
*/
|
|
|
|
public $relationName;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var FormField Object used for rendering a simple field type
|
|
|
|
*/
|
|
|
|
public $renderFormField;
|
|
|
|
|
2014-05-28 12:37:38 +10:00
|
|
|
/**
|
|
|
|
* @var string Model column to use for the name reference
|
|
|
|
*/
|
2014-09-17 18:59:49 +10:00
|
|
|
public $nameFrom = 'name';
|
2014-05-28 12:37:38 +10:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string Model column to use for the description reference
|
|
|
|
*/
|
2014-09-17 18:59:49 +10:00
|
|
|
public $descriptionFrom = 'description';
|
2014-05-28 12:37:38 +10:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string Empty value to use if the relation is singluar (belongsTo)
|
|
|
|
*/
|
|
|
|
public $emptyOption;
|
|
|
|
|
2014-05-14 23:24:20 +10:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function init()
|
|
|
|
{
|
2014-09-17 19:46:49 +10:00
|
|
|
$this->relationName = $this->valueFrom;
|
2014-05-14 23:24:20 +10:00
|
|
|
$this->relationType = $this->model->getRelationType($this->relationName);
|
|
|
|
|
2014-09-17 18:59:49 +10:00
|
|
|
$this->nameFrom = $this->getConfig('nameFrom', $this->nameFrom);
|
|
|
|
$this->descriptionFrom = $this->getConfig('descriptionFrom', $this->descriptionFrom);
|
2014-05-28 12:37:38 +10:00
|
|
|
$this->emptyOption = $this->getConfig('emptyOption');
|
|
|
|
|
2014-10-10 23:50:05 +02:00
|
|
|
/* @todo Remove lines if year >= 2015 */
|
|
|
|
if ($this->getConfig('nameColumn')) {
|
|
|
|
$this->nameFrom = $this->getConfig('nameColumn');
|
|
|
|
}
|
|
|
|
/* @todo Remove lines if year >= 2015 */
|
|
|
|
if ($this->getConfig('descriptionColumn')) {
|
|
|
|
$this->descriptionFrom = $this->getConfig('descriptionColumn');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$this->model->hasRelation($this->relationName)) {
|
|
|
|
throw new SystemException(Lang::get(
|
|
|
|
'backend::lang.model.missing_relation',
|
2014-12-10 11:34:05 +00:00
|
|
|
['class'=>get_class($this->model), 'relation'=>$this->relationName]
|
2014-10-10 23:50:05 +02:00
|
|
|
));
|
|
|
|
}
|
2014-05-14 23:24:20 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function render()
|
|
|
|
{
|
|
|
|
$this->prepareVars();
|
|
|
|
return $this->makePartial('relation');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepares the view data
|
|
|
|
*/
|
|
|
|
public function prepareVars()
|
|
|
|
{
|
|
|
|
$this->vars['field'] = $this->makeRenderFormField();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Makes the form object used for rendering a simple field type
|
|
|
|
*/
|
|
|
|
protected function makeRenderFormField()
|
|
|
|
{
|
2014-10-10 23:50:05 +02:00
|
|
|
return $this->renderFormField = RelationBase::noConstraints(function () {
|
2014-09-23 07:55:40 +10:00
|
|
|
|
|
|
|
$field = clone $this->formField;
|
|
|
|
|
2015-01-05 11:18:43 +11:00
|
|
|
list($model, $attribute) = $this->resolveModelAttribute($this->relationName);
|
2014-09-23 07:55:40 +10:00
|
|
|
$relatedObj = $model->makeRelation($attribute);
|
|
|
|
$query = $model->{$attribute}()->newQuery();
|
|
|
|
|
2014-11-04 12:34:45 +11:00
|
|
|
if (in_array($this->relationType, ['belongsToMany', 'morphToMany', 'morphedByMany', 'hasMany'])) {
|
2014-09-23 07:55:40 +10:00
|
|
|
$field->type = 'checkboxlist';
|
2014-11-04 12:34:45 +11:00
|
|
|
}
|
|
|
|
elseif (in_array($this->relationType, ['belongsTo', 'hasOne'])) {
|
2014-09-23 07:55:40 +10:00
|
|
|
$field->type = 'dropdown';
|
|
|
|
}
|
|
|
|
|
2015-01-21 17:01:07 +11:00
|
|
|
$field->placeholder = $this->emptyOption;
|
|
|
|
|
2014-10-10 23:50:05 +02:00
|
|
|
// It is safe to assume that if the model and related model are of
|
2014-09-23 07:55:40 +10:00
|
|
|
// the exact same class, then it cannot be related to itself
|
|
|
|
if ($model->exists && (get_class($model) == get_class($relatedObj))) {
|
2014-09-25 18:35:10 +10:00
|
|
|
$query->where($relatedObj->getKeyName(), '<>', $model->getKey());
|
2014-09-23 07:55:40 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
// Even though "no constraints" is applied, belongsToMany constrains the query
|
|
|
|
// by joining its pivot table. Remove all joins from the query.
|
|
|
|
$query->getQuery()->getQuery()->joins = [];
|
|
|
|
|
2014-09-25 18:05:35 +10:00
|
|
|
$treeTraits = ['October\Rain\Database\Traits\NestedTree', 'October\Rain\Database\Traits\SimpleTree'];
|
2014-10-10 23:50:05 +02:00
|
|
|
if (count(array_intersect($treeTraits, class_uses($relatedObj))) > 0) {
|
2014-09-23 07:55:40 +10:00
|
|
|
$field->options = $query->listsNested($this->nameFrom, $relatedObj->getKeyName());
|
2014-11-04 17:41:48 +11:00
|
|
|
}
|
|
|
|
else {
|
2014-09-23 07:55:40 +10:00
|
|
|
$field->options = $query->lists($this->nameFrom, $relatedObj->getKeyName());
|
2014-10-10 23:50:05 +02:00
|
|
|
}
|
2014-09-23 07:55:40 +10:00
|
|
|
|
|
|
|
return $field;
|
|
|
|
});
|
2014-05-14 23:24:20 +10:00
|
|
|
}
|
|
|
|
|
2014-05-28 13:00:31 +10:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2015-01-05 09:43:39 +11:00
|
|
|
public function getSaveValue($value)
|
2014-05-28 13:00:31 +10:00
|
|
|
{
|
2014-10-10 23:50:05 +02:00
|
|
|
if (is_string($value) && !strlen($value)) {
|
2014-05-29 19:35:46 +10:00
|
|
|
return null;
|
2014-10-10 23:50:05 +02:00
|
|
|
}
|
2014-05-29 19:35:46 +10:00
|
|
|
|
2014-10-10 23:50:05 +02:00
|
|
|
if (is_array($value) && !count($value)) {
|
2014-05-29 19:35:46 +10:00
|
|
|
return null;
|
2014-10-10 23:50:05 +02:00
|
|
|
}
|
2014-05-29 19:35:46 +10:00
|
|
|
|
|
|
|
return $value;
|
2014-05-28 13:00:31 +10:00
|
|
|
}
|
2014-10-10 23:50:05 +02:00
|
|
|
}
|