-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.driver.php
More file actions
294 lines (249 loc) · 7.43 KB
/
extension.driver.php
File metadata and controls
294 lines (249 loc) · 7.43 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<?php
/**
* @package sections_panel
*/
require_once TOOLKIT . '/class.entrymanager.php';
require_once TOOLKIT . '/class.sectionmanager.php';
/**
* Add sections directly to the dashboard, skipping the datasource step.
*/
class Extension_Sections_Panel extends Extension {
/**
* Extension information.
*/
public function about() {
return array(
'name' => 'Sections Panel',
'version' => '1.0',
'release-date' => '2011-11-18',
'author' => array(
'name' => 'Rowan Lewis',
'website' => 'http://rowanlewis.com/',
'email' => 'me@rowanlewis.com'
),
'description' => 'Add sections directly to the dashboard, without having to create a separate datasource, or any datasource at all.'
);
}
/**
* Subscribe to Dashboard and Symphony delegates.
*/
public function getSubscribedDelegates() {
return array(
array(
'page' => '/backend/',
'delegate' => 'InitaliseAdminPageHead',
'callback' => 'dashboardAppendAssets'
),
array(
'page' => '/backend/',
'delegate' => 'DashboardPanelOptions',
'callback' => 'dashboardPanelOptions'
),
array(
'page' => '/backend/',
'delegate' => 'DashboardPanelRender',
'callback' => 'dashboardPanelRender'
),
array(
'page' => '/backend/',
'delegate' => 'DashboardPanelTypes',
'callback' => 'dashboardPanelTypes'
)
);
}
/**
* Append CSS and javaScript assets to the dashboard page.
*
* @param array $context
*/
public function dashboardAppendAssets($context) {
$page = $context['parent']->Page;
if ($page instanceof contentExtensionDashboardIndex) {
$page->addStylesheetToHead(URL . '/extensions/sections_panel/assets/panel.css', 'screen', 666);
$page->addScriptToHead(URL . '/extensions/sections_panel/assets/panel.js', 667);
}
}
/**
* Generate the panel options view.
*
* @param array $context
*/
public function dashboardPanelOptions($context) {
if ($context['type'] != 'section_to_table') return;
$config = $context['existing_config'];
$sm = new SectionManager(Symphony::Engine());
$section_selected = (
isset($config['section'])
? $config['section']
: null
);
$columns_selected = (
isset($config['columns'])
? $config['columns']
: array()
);
$fieldset = new XMLElement('fieldset');
$fieldset->setAttribute('class', 'settings');
$fieldset->appendChild(
new XMLElement('legend', __('Section to Table'))
);
$label = Widget::Label(__('Section'));
$select = new XMLElement('select');
$select->setAttribute('name', 'config[section]');
// Section options:
foreach ($sm->fetch() as $section) {
$option = new XMLElement('option');
$option->setAttribute('value', $section->get('id'));
$option->setValue($section->get('name'));
if ($section_selected == $section->get('id')) {
$option->setAttribute('selected', 'selected');
}
$select->appendChild($option);
}
$label->appendChild($select);
if (isset($context['errors']['section'])) {
$label = Widget::wrapFormElementWithError($label, $context['errors']['section']);
}
$fieldset->appendChild($label);
$label = Widget::Label(__('Show Columns'));
$fieldset->appendChild($label);
$current_context = null;
// Section contexts:
foreach ($sm->fetch() as $section) {
$div = new XMLElement('div');
$div->setAttribute('data-section-context', $section->get('id'));
$div->setAttribute('style', 'display: none;');
$select = new XMLElement('select');
$select->setAttribute('name', 'config[columns][]');
$select->setAttribute('multiple', 'multiple');
if (isset($first_select) === false) {
$current_context = $div;
}
foreach ($section->fetchFields() as $field) {
$option = new XMLElement('option');
$option->setAttribute('value', $field->get('id'));
$option->setValue($field->get('label'));
if (in_array($field->get('id'), $columns_selected)) {
$option->setAttribute('selected', 'selected');
$current_context = $div;
}
$select->appendChild($option);
}
$div->appendChild($select);
$fieldset->appendChild($div);
}
// Show the first/currently selected section:
if ($current_context) {
$current_context->setAttribute('style', 'display: block;');
}
$input = Widget::Input(
'config[entries]',
(
isset($config['entries'])
? $config['entries']
: 5
)
);
$input->setAttribute('type', 'number');
$input->setAttribute('size', '3');
$label = Widget::Label(__(
'Show the first %s entries in table.',
array($input->generate())
));
$fieldset->appendChild($label);
$context['form'] = $fieldset;
}
/**
* Generate the panel view.
*
* @param array $context
*/
public function dashboardPanelRender($context) {
if ($context['type'] != 'section_to_table') return;
$config = $context['config'];
$panel = $context['panel'];
$em = new EntryManager(Symphony::Engine());
$sm = new SectionManager(Symphony::Engine());
// Get section information:
$section = $sm->fetch($config['section']);
$fields = $section->fetchFields();
$section_url = sprintf(
'%s/publish/%s/',
SYMPHONY_URL, $section->get('handle')
);
// Get entry information:
$entries = $em->fetchByPage(1, $section->get('id'),
(
isset($config['entries'])
? $config['entries']
: 4
)
);
// Build table:
$table = new XMLElement('table');
$table->setAttribute('class', 'skinny');
$table_head = new XMLElement('thead');
$table->appendChild($table_head);
$table_body = new XMLElement('tbody');
$table->appendChild($table_body);
$panel->appendChild($table);
// Add table headers:
$row = new XMLElement('tr');
$table_head->appendChild($row);
foreach ($fields as $field) {
if (!in_array($field->get('id'), $config['columns'])) continue;
$cell = new XMLElement('th');
$cell->setValue($field->get('label'));
$row->appendChild($cell);
}
// Add table body:
foreach ($entries['records'] as $entry) {
$row = new XMLElement('tr');
$table_body->appendChild($row);
$entry_url = $section_url . 'edit/' . $entry->get('id') . '/';
foreach ($fields as $position => $field) {
if (!in_array($field->get('id'), $config['columns'])) continue;
$data = $entry->getData($field->get('id'));
$cell = new XMLElement('td');
$row->appendChild($cell);
$link = (
$position === 0
? Widget::Anchor(__('None'), $entry_url, $entry->get('id'), 'content')
: null
);
$value = $field->prepareTableValue($data, $link, $entry->get('id'));
if (isset($link)) {
$value = $link->generate();
}
if ($value == 'None' || strlen($value) === 0) {
$cell->setAttribute('class', 'inactive');
$cell->setValue(__('None'));
}
else {
$cell->setValue($value);
}
}
}
}
/**
* Let the Dashboard know that our panel type exists.
*
* @param array $context
*/
public function dashboardPanelTypes($context) {
$context['types']['section_to_table'] = __('Section to Table');
}
/**
* Generate a list of available sections for use in the Widget::Select function.
*
* @param array $context
*/
public function getSectionOption(Section $section, $selected_id = null) {
return array(
$section->get('id'),
$selected_id == $section->get('id'),
$section->get('name')
);
}
}
?>