forked from wintercms/winter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRelation.php
More file actions
196 lines (164 loc) · 5.79 KB
/
Relation.php
File metadata and controls
196 lines (164 loc) · 5.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php namespace Backend\FormWidgets;
use Db;
use Backend\Classes\FormField;
use Backend\Classes\FormWidgetBase;
use Lang;
use Winter\Storm\Database\Relations\Relation as RelationBase;
use Winter\Storm\Exception\SystemException;
/**
* Form Relationship
* Renders a field prepopulated with a belongsTo and belongsToHasMany relation.
*
* @package winter\wn-backend-module
* @author Alexey Bobkov, Samuel Georges
*/
class Relation extends FormWidgetBase
{
use \Backend\Traits\FormModelWidget;
//
// Configurable properties
//
/**
* @var string Model column to use for the name reference
*/
public $nameFrom = 'name';
/**
* @var string Custom SQL column selection to use for the name reference
*/
public $sqlSelect;
/**
* @var string Empty value to use if the relation is singluar (belongsTo)
*/
public $emptyOption;
/**
* @var string Use a custom scope method for the list query.
*/
public $scope;
/**
* @var string Define the order of the list query.
*/
public $order;
//
// Object properties
//
/**
* @inheritDoc
*/
protected $defaultAlias = 'relation';
/**
* @var FormField Object used for rendering a simple field type
*/
public $renderFormField;
/**
* @inheritDoc
*/
public function init()
{
$this->fillFromConfig([
'nameFrom',
'emptyOption',
'scope',
'order',
]);
if (isset($this->config->select)) {
$this->sqlSelect = $this->config->select;
}
}
/**
* @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
* @throws SystemException if an unsupported relation type is used.
*/
protected function makeRenderFormField()
{
return $this->renderFormField = RelationBase::noConstraints(function () {
$field = clone $this->formField;
$relationObject = $this->getRelationObject();
$query = $relationObject->newQuery();
list($model, $attribute) = $this->resolveModelAttribute($this->valueFrom);
$relationType = $model->getRelationType($attribute);
$relationModel = $model->makeRelation($attribute);
if (in_array($relationType, ['belongsToMany', 'morphToMany', 'morphedByMany', 'hasMany'])) {
$field->type = 'checkboxlist';
} elseif (in_array($relationType, ['belongsTo', 'hasOne'])) {
$field->type = 'dropdown';
} else {
throw new SystemException(
Lang::get('backend::lang.relation.relationwidget_unsupported_type', [
'type' => $relationType
])
);
}
// Order query by the configured option.
if ($this->order) {
// Using "raw" to allow authors to use a string to define the order clause.
$query->orderByRaw($this->order);
}
// It is safe to assume that if the model and related model are of
// the exact same class, then it cannot be related to itself
if ($model->exists && (get_class($model) == get_class($relationModel))) {
$query->where($relationModel->getKeyName(), '<>', $model->getKey());
}
// 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 = [];
if ($scopeMethod = $this->scope) {
$query->$scopeMethod($model);
}
// Determine if the model uses a tree trait
$treeTraits = ['Winter\Storm\Database\Traits\NestedTree', 'Winter\Storm\Database\Traits\SimpleTree'];
$usesTree = count(array_intersect($treeTraits, class_uses($relationModel))) > 0;
// The "sqlSelect" config takes precedence over "nameFrom".
// A virtual column called "selection" will contain the result.
// Tree models must select all columns to return parent columns, etc.
if ($this->sqlSelect) {
$nameFrom = 'selection';
$selectColumn = $usesTree ? '*' : $relationModel->getKeyName();
$result = $query->select($selectColumn, Db::raw($this->sqlSelect . ' AS ' . $nameFrom));
}
else {
$nameFrom = $this->nameFrom;
$result = $query->getQuery()->get();
}
// Some simpler relations can specify a custom local or foreign "other" key,
// which can be detected and implemented here automagically.
$primaryKeyName = in_array($relationType, ['hasMany', 'belongsTo', 'hasOne'])
? $relationObject->getOtherKey()
: $relationModel->getKeyName();
$field->options = $usesTree
? $result->listsNested($nameFrom, $primaryKeyName)
: $result->lists($nameFrom, $primaryKeyName);
return $field;
});
}
/**
* @inheritDoc
*/
public function getSaveValue($value)
{
if ($this->formField->disabled || $this->formField->hidden) {
return FormField::NO_SAVE_DATA;
}
if (is_string($value) && !strlen($value)) {
return null;
}
if (is_array($value) && !count($value)) {
return null;
}
return $value;
}
}