Repository navigation
Expand file tree
/
Copy pathModelListModel.php
More file actions
185 lines (148 loc) · 4.5 KB
/
Copy pathModelListModel.php
File metadata and controls
185 lines (148 loc) · 4.5 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
<?php namespace RainLab\Builder\Classes;
use ApplicationException;
use SystemException;
use ValidationException;
use Lang;
/**
* Represents and manages model lists.
*
* @package rainlab\builder
* @author Alexey Bobkov, Samuel Georges
*/
class ModelListModel extends ModelYamlModel
{
public $columns;
protected static $fillable = [
'fileName',
'columns'
];
protected $validationRules = [
'fileName' => ['required', 'regex:/^[a-z0-9\.\-_]+$/i']
];
public function loadForm($path)
{
$this->fileName = $path;
return parent::load($this->getFilePath());
}
public function fill(array $attributes)
{
if (!is_array($attributes['columns'])) {
$attributes['columns'] = json_decode($attributes['columns'], true);
if ($attributes['columns'] === null) {
throw new SystemException('Cannot decode columns JSON string.');
}
}
return parent::fill($attributes);
}
public static function validateFileIsModelType($fileContentsArray)
{
$modelRootNodes = [
'columns'
];
foreach ($modelRootNodes as $node) {
if (array_key_exists($node, $fileContentsArray)) {
return true;
}
}
return false;
}
public function validate()
{
parent::validate();
$this->validateDupicateColumns();
if (!$this->columns) {
throw new ValidationException(['columns' => 'Please create at least one column.']);
}
}
public function initDefaults()
{
$this->fileName = 'columns.yaml';
}
protected function validateDupicateColumns()
{
foreach ($this->columns as $outerIndex=>$outerColumn) {
foreach ($this->columns as $innerIndex=>$innerColumn) {
if ($innerIndex != $outerIndex && $innerColumn['field'] == $outerColumn['field']) {
throw new ValidationException([
'columns' => Lang::get('rainlab.builder::lang.list.error_duplicate_column',
['column' => $outerColumn['field']]
)
]);
}
}
}
}
/**
* Converts the model's data to an array before it's saved to a YAML file.
* @return array
*/
protected function modelToYamlArray()
{
$fileColumns = [];
foreach ($this->columns as $column) {
if (!isset($column['field'])) {
throw new ApplicationException('Cannot save the list - the column field name should not be empty.');
}
$columnName = $column['field'];
unset($column['field']);
if (array_key_exists('id', $column)) {
unset($column['id']);
}
$column = $this->preprocessColumnDataBeforeSave($column);
$fileColumns[$columnName] = $column;
}
return [
'columns'=>$fileColumns
];
}
/**
* Load the model's data from an array.
* @param array $array An array to load the model fields from.
*/
protected function yamlArrayToModel($array)
{
$fileColumns = $array['columns'];
$columns = [];
$index = 0;
foreach ($fileColumns as $columnName=>$column) {
if (!is_array($column)) {
// Handle the case when a column is defined as
// column: Title
$column = [
'label' => $column
];
}
$column['id'] = $index;
$column['field'] = $columnName;
$columns[] = $column;
$index++;
}
$this->columns = $columns;
}
protected function preprocessColumnDataBeforeSave($column)
{
$booleanFields = [
'searchable',
'invisible',
'sortable'
];
$column = array_filter($column, function($value)
{
return strlen($value) > 0;
});
foreach ($booleanFields as $booleanField) {
if (!array_key_exists($booleanField, $column)) {
continue;
}
$value = $column[$booleanField];
if ($value == '1' || $value == 'true') {
$value = true;
}
else {
$value = false;
}
$column[$booleanField] = $value;
}
return $column;
}
}