forked from symphonists/content_type_mappings
-
Notifications
You must be signed in to change notification settings - Fork 1
/
extension.driver.php
237 lines (192 loc) · 7.79 KB
/
extension.driver.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
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
<?php
Class extension_Content_Type_Mappings extends Extension {
const SETTINGS_GROUP = 'content-type-mappings';
public function getSubscribedDelegates()
{
return array(
array(
'page' => '/frontend/',
'delegate' => 'FrontendPreRenderHeaders',
'callback' => 'setContentType'
),
array(
'page' => '/system/preferences/',
'delegate' => 'AddCustomPreferenceFieldsets',
'callback' => 'addCustomPreferenceFieldsets'
),
array(
'page' => '/system/preferences/',
'delegate' => 'Save',
'callback' => 'save'
),
array(
'page' => '/backend/',
'delegate' => 'AdminPagePreGenerate',
'callback' => '__appendAssets'
)
);
}
public function __appendAssets($context)
{
$callback = Symphony::Engine()->getPageCallback();
if ($callback['driver'] == 'systempreferences') {
Administration::instance()->Page->addScriptToHead(URL . '/extensions/content_type_mappings/assets/content_type_mappings.preferences.js', 401, false);
}
}
/**
* Delegate handle that adds Custom Preference Fieldsets
*
* @param string $page
* @param array $context
*/
public function addCustomPreferenceFieldsets($context)
{
$mappings = Symphony::Configuration()->get();
$mappings = $mappings[self::SETTINGS_GROUP];
// Creates the field set
$fieldset = new XMLElement('fieldset');
$fieldset->setAttribute('class', 'settings');
$fieldset->appendChild(new XMLElement('legend', __('Content Type Mappings')));
// Create a paragraph for short intructions
$p = new XMLElement('p', __('Content Types defined here are usable in the Pages Editor.'), array('class' => 'help'));
// Append intro paragraph
$fieldset->appendChild($p);
// Outter wrapper
$out_wrapper = new XMLElement('div', null, array(
'class' => 'frame',
'id' => 'ctm-duplicator'
));
// Create a wrapper
$wrapper = new XMLElement('ol');
// Template
$wrapper->appendChild($this->generateRow('New Content Mapping','template'));
// Data
if (is_array($mappings)) {
foreach ($mappings as $type => $content_type) {
$values = array('mime-type'=>$content_type,'page-type'=>$type);
$wrapper->appendChild($this->generateRow($values['page-type'], 'instance expanded', $values));
}
}
$out_wrapper->appendChild($wrapper);
// Wrapper into fieldset
$fieldset->appendChild($out_wrapper);
// Adds the field set to the wrapper
$context['wrapper']->appendChild($fieldset);
}
/**
* Quick utility function that creates a duplicator row
*
* @param string $title
* @param string $class @optional
* @param array $values @optional
*/
public function generateRow($title, $class = '', $values = array())
{
// Create the label and the input field
$wrapper = new XMLElement('li');
$wrapper->setAttribute('class', $class);
// HEader
$header = new XMLElement('header');
$header->setValue(__($title));
// Content
$content = new XMLElement('div', null, array('class' => 'content'));
$columns = new XMLElement('div', null, array('class' => 'two columns'));
$content->appendChild($columns);
// Page type column
$page_type = $this->generateLabelInput($columns, 'Page Type', 'page-type', $values['page-type']);
// Mime type column
$mime_type = $this->generateLabelInput($columns, 'Mime Type', 'mime-type', $values['mime-type']);
// Append header and content
$wrapper->appendChild($header);
$wrapper->appendChild($columns);
return $wrapper;
}
private function generateLabelInput(&$wrap, $title, $name, $value=null)
{
$type = Widget::Label();
$type->setAttribute('class', 'column');
$type->appendChild(new XMLElement('span',__($title)));
$type->appendChild(Widget::Input('settings[content-type-mappings][mappings][]['.$name.']', $value));
$wrap->appendChild($type);
}
/**
* Delegate handle that is called prior to saving the settings
* @param array $context
*/
public function save(&$context)
{
$s = $context['settings'][self::SETTINGS_GROUP]['mappings'];
// If it's an array
if ( is_array($s) ) {
// Flush all the group
Symphony::Configuration()->remove(self::SETTINGS_GROUP);
// Create a pointer to the prev element
$last_page_type = null;
// Recreate them, iterate all values and assemble them
foreach ($s as $setting) {
if (isset($setting['page-type']) && !empty($setting['page-type'])) {
$last_page_type = $setting['page-type'];
}
if (isset($setting['mime-type']) && !empty($setting['mime-type'])) {
Symphony::Configuration()->set($last_page_type, $setting['mime-type'], self::SETTINGS_GROUP);
$last_page_type = null;
}
}
// Save the changes
Symphony::Configuration()->write();
// Unset from the context
unset($context['settings'][self::SETTINGS_GROUP]['mappings']);
}
}
/*-------------------------------------------------------------------------
Installation:
-------------------------------------------------------------------------*/
public function install()
{
$initial_mappings = array(
'xml' => 'text/xml; charset=utf-8',
'text' => 'text/plain; charset=utf-8',
'css' => 'text/css; charset=utf-8',
'json' => 'application/json; charset=utf-8'
);
foreach ($initial_mappings as $type => $content_type) {
Symphony::Configuration()->set($type, $content_type, self::SETTINGS_GROUP);
}
Symphony::Configuration()->write();
}
public function uninstall()
{
Symphony::Configuration()->remove(self::SETTINGS_GROUP);
Symphony::Configuration()->write();
}
/*-------------------------------------------------------------------------
Utilities:
-------------------------------------------------------------------------*/
public function resolveType($type)
{
// Fix issue #2, for downloadables files
if ($type{0} == '.') {
return Symphony::Configuration()->get(strtolower(substr($type, 1)), self::SETTINGS_GROUP);
} else {
return Symphony::Configuration()->get(strtolower($type), self::SETTINGS_GROUP);
}
}
public function setContentType(array $context=NULL)
{
$page_data = Frontend::Page()->pageData();
if (!isset($page_data['type']) || !is_array($page_data['type']) || empty($page_data['type'])) {
return;
}
foreach ($page_data['type'] as $type) {
$content_type = $this->resolveType($type);
if (!is_null($content_type)) {
Frontend::Page()->addHeaderToPage('Content-Type', $content_type);
}
if ($type{0} == '.') {
$page_params = Frontend::Page()->Params();
$filename = trim(str_replace('/', '.', $page_params['current-path']), '.');
Frontend::Page()->addHeaderToPage('Content-Disposition', "attachment; filename={$filename}{$type}");
}
}
}
}