This repository has been archived by the owner on Jan 25, 2021. It is now read-only.
/
structure.php
171 lines (132 loc) · 3.61 KB
/
structure.php
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
<?php
class StructureField extends BaseField {
static public $assets = array(
'js' => array(
'structure.js'
),
'css' => array(
'structure.css'
)
);
public $fields = array();
public $entry = null;
public $structure = null;
public $style = 'items';
public $modalsize = 'medium';
public $sort = null;
public $flip = false;
public function routes() {
return array(
array(
'pattern' => 'add',
'method' => 'get|post',
'action' => 'add'
),
array(
'pattern' => 'sort',
'method' => 'post',
'action' => 'sort',
),
array(
'pattern' => '(:any)/update',
'method' => 'get|post',
'action' => 'update'
),
array(
'pattern' => '(:any)/delete',
'method' => 'get|post',
'action' => 'delete',
)
);
}
public function modalsize() {
$sizes = array('small', 'medium', 'large');
return in_array($this->modalsize, $sizes) ? $this->modalsize : 'medium';
}
public function style() {
$styles = array('table', 'items');
return in_array($this->style, $styles) ? $this->style : 'items';
}
public function sort() {
return $this->sort ? str::split($this->sort) : false;
}
public function flip() {
return $this->flip === true ? true : false;
}
public function sortable() {
return $this->readonly() or $this->sort() or $this->flip();
}
public function structure() {
if(!is_null($this->structure)) {
return $this->structure;
} else {
return $this->structure = $this->model->structure()->forField($this->name);
}
}
public function fields() {
$output = array();
foreach($this->structure->fields() as $k => $v) {
$v['name'] = $k;
$v['value'] = '{{' . $k . '}}';
$output[] = $v;
}
return $output;
}
public function entries() {
$entries = $this->structure()->data();
if($sort = $this->sort()) {
$entries = call_user_func_array(array($entries, 'sortBy'), $sort);
}
if($this->flip()) {
$entries = $entries->flip();
}
return $entries;
}
public function result() {
return $this->structure()->toYaml();
}
public function entry($data) {
if(is_null($this->entry) or !is_string($this->entry)) {
$html = array();
foreach($this->fields as $name => $field) {
if(isset($data->$name)) {
$html[] = $data->$name;
}
}
return implode('<br>', $html);
} else {
$text = $this->entry;
foreach((array)$data as $key => $value) {
if(is_array($value)) {
$value = implode(', ', array_values($value));
}
$text = str_replace('{{' . $key . '}}', $value, $text);
}
return $text;
}
}
public function label() {
return null;
}
public function headline() {
if(!$this->readonly) {
$add = new Brick('a');
$add->html('<i class="icon icon-left fa fa-plus-circle"></i>' . l('fields.structure.add'));
$add->addClass('structure-add-button label-option');
$add->data('modal', true);
$add->attr('href', purl($this->model, 'field/' . $this->name . '/structure/add'));
} else {
$add = null;
}
$label = parent::label();
$label->addClass('structure-label');
$label->append($add);
return $label;
}
public function content() {
return tpl::load(__DIR__ . DS . 'template.php', array('field' => $this));
}
public function url($action) {
return purl($this->model(), 'field/' . $this->name() . '/structure/' . $action);
}
}