1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Feeds Tamper UI - Defines the user interface for Feeds Tamper.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Implements hook_menu().
|
10
|
*/
|
11
|
function feeds_tamper_ui_menu() {
|
12
|
$defaults = array(
|
13
|
'page callback' => 'drupal_get_form',
|
14
|
'access callback' => 'feeds_tamper_access',
|
15
|
'access arguments' => array(3),
|
16
|
'title arguments' => array(3),
|
17
|
'file' => 'feeds_tamper_ui.admin.inc',
|
18
|
'type' => MENU_CALLBACK,
|
19
|
);
|
20
|
|
21
|
$items = array();
|
22
|
$items['admin/structure/feeds/%feeds_importer/tamper'] = array(
|
23
|
'title' => 'Tamper',
|
24
|
'title arguments' => array(),
|
25
|
'page arguments' => array('feeds_tamper_ui_list_form', 3),
|
26
|
'type' => MENU_LOCAL_TASK,
|
27
|
'weight' => 10,
|
28
|
) + $defaults;
|
29
|
|
30
|
$items['admin/structure/feeds/%feeds_importer/tamper/manage'] = array(
|
31
|
'title' => 'List',
|
32
|
'type' => MENU_DEFAULT_LOCAL_TASK,
|
33
|
);
|
34
|
|
35
|
$items['admin/structure/feeds/%feeds_importer/tamper/export'] = array(
|
36
|
'title' => 'Export',
|
37
|
'title arguments' => array(),
|
38
|
'page arguments' => array('feeds_tamper_ui_export_form', 3),
|
39
|
'access callback' => 'user_access',
|
40
|
'access arguments' => array('administer feeds_tamper'),
|
41
|
'type' => MENU_LOCAL_TASK,
|
42
|
'weight' => 10,
|
43
|
) + $defaults;
|
44
|
|
45
|
$items['admin/structure/feeds/%feeds_importer/tamper/add/%feeds_tamper_ui_source'] = array(
|
46
|
'title callback' => 'feeds_tamper_ui_add_title',
|
47
|
'title arguments' => array(6),
|
48
|
'page arguments' => array('feeds_tamper_ui_add_plugin_form', 3, 6),
|
49
|
) + $defaults;
|
50
|
|
51
|
$items['admin/structure/feeds/%feeds_importer/tamper/%feeds_tamper_plugin_instance/edit'] = array(
|
52
|
'title callback' => 'feeds_tamper_ui_edit_title',
|
53
|
'page arguments' => array('feeds_tamper_ui_edit_plugin_form', 5),
|
54
|
'access arguments' => array(NULL, 5),
|
55
|
) + $defaults;
|
56
|
|
57
|
$items['admin/structure/feeds/%feeds_importer/tamper/%feeds_tamper_plugin_instance/delete'] = array(
|
58
|
'title callback' => 'feeds_tamper_ui_delete_title',
|
59
|
'page arguments' => array('feeds_tamper_ui_delete_form', 5),
|
60
|
'access arguments' => array(NULL, 5),
|
61
|
) + $defaults;
|
62
|
|
63
|
return $items;
|
64
|
}
|
65
|
|
66
|
/**
|
67
|
* Implements hook_theme().
|
68
|
*/
|
69
|
function feeds_tamper_ui_theme($existing, $type, $theme, $path) {
|
70
|
return array(
|
71
|
'feeds_tamper_ui_list_form' => array(
|
72
|
'render element' => 'form',
|
73
|
'file' => 'feeds_tamper_ui.admin.inc',
|
74
|
),
|
75
|
);
|
76
|
}
|
77
|
|
78
|
/**
|
79
|
* Menu loader callback for plugin instances.
|
80
|
*/
|
81
|
function feeds_tamper_plugin_instance_load($instance_id) {
|
82
|
return feeds_tamper_load_instance($instance_id);
|
83
|
}
|
84
|
|
85
|
/**
|
86
|
* Menu loader callback for grabbing the source from the URL.
|
87
|
*/
|
88
|
function feeds_tamper_ui_source_load($source) {
|
89
|
// We've HEX encoded the source to allow all possible characters.
|
90
|
return pack('H*', $source);
|
91
|
}
|
92
|
|
93
|
/**
|
94
|
* Add plugin title callback.
|
95
|
*/
|
96
|
function feeds_tamper_ui_add_title($source) {
|
97
|
// Title callbacks get check_plain'ed already.
|
98
|
return t('Add plugin to: !source', array('!source' => $source));
|
99
|
}
|
100
|
|
101
|
/**
|
102
|
* Edit plugin title callback.
|
103
|
*/
|
104
|
function feeds_tamper_ui_edit_title($instance) {
|
105
|
if ($instance->export_type == EXPORT_IN_DATABASE) {
|
106
|
return t('Edit: @id', array('@id' => $instance->description));
|
107
|
}
|
108
|
return t('Override: @id', array('@id' => $instance->description));
|
109
|
}
|
110
|
|
111
|
/**
|
112
|
* Delete plugin title callback.
|
113
|
*/
|
114
|
function feeds_tamper_ui_delete_title($instance) {
|
115
|
if ($instance->export_type == EXPORT_IN_DATABASE) {
|
116
|
return t('Delete plugin: @id', array('@id', $instance->id));
|
117
|
}
|
118
|
return t('Revert plugin: @id', array('@id', $instance->id));
|
119
|
}
|
120
|
|
121
|
/**
|
122
|
* Implements hook_form_FORM_ID_alter().
|
123
|
*
|
124
|
* Modify feeds_ui_overview_form(), adding Tamper links if the user has access.
|
125
|
*/
|
126
|
function feeds_tamper_ui_form_feeds_ui_overview_form_alter(&$form, &$form_state) {
|
127
|
if (!empty($form['enabled'])) {
|
128
|
foreach ($form['enabled'] as $id => &$table) {
|
129
|
if (feeds_tamper_access($id)) {
|
130
|
$table['operations']['#markup'] .= ' | ' . l(t('Tamper'), "admin/structure/feeds/$id/tamper");
|
131
|
}
|
132
|
}
|
133
|
}
|
134
|
}
|
135
|
|
136
|
/**
|
137
|
* Calculate display name for source.
|
138
|
*
|
139
|
* @param stdClass $instance
|
140
|
* A plugin instance object.
|
141
|
*
|
142
|
* @return string
|
143
|
* The unsanitized name to display for a Feeds source.
|
144
|
*/
|
145
|
function feeds_tamper_ui_source_name(stdClass $instance) {
|
146
|
$importer = feeds_importer($instance->importer);
|
147
|
$sources = $importer->parser->getMappingSources();
|
148
|
return !empty($sources[$instance->source]['name']) ? $sources[$instance->source]['name'] : $instance->source;
|
149
|
}
|
150
|
|
151
|
/**
|
152
|
* Implements hook_form_BASE_FORM_ID_alter().
|
153
|
*/
|
154
|
function feeds_tamper_form_feeds_ui_create_form_alter(array &$form, array &$form_state) {
|
155
|
|
156
|
// Only make these alterations if we're trying to clone a feed.
|
157
|
if (arg(4) !== 'clone') {
|
158
|
return;
|
159
|
}
|
160
|
|
161
|
// Add checkbox to clone tamper plugins.
|
162
|
$form['clone_tamper_plugins'] = array(
|
163
|
'#type' => 'checkbox',
|
164
|
'#title' => t('Clone Tamper Plugins'),
|
165
|
'#description' => t('Check this box if you also want to clone the Feeds Tamper plugins for this importer.'),
|
166
|
'#weight' => 1,
|
167
|
);
|
168
|
|
169
|
// Give submit button a higher weight so it shows up under the checkbox.
|
170
|
$form['submit']['#weight'] = 2;
|
171
|
$form['#submit'][] = '_feeds_tamper_clone_tamper_plugins';
|
172
|
}
|
173
|
|
174
|
/**
|
175
|
* Additional submit handler for feeds_build_create_form().
|
176
|
*
|
177
|
* @see feeds_tamper_form_feeds_ui_create_form_alter()
|
178
|
*/
|
179
|
function _feeds_tamper_clone_tamper_plugins(array &$form, array &$form_state) {
|
180
|
|
181
|
if (empty($form_state['values']['clone_tamper_plugins'])) {
|
182
|
return;
|
183
|
}
|
184
|
|
185
|
$from_importer_id = isset($form['#from_importer']->id) ? $form['#from_importer']->id : '';
|
186
|
|
187
|
if (!$from_importer_id) {
|
188
|
return;
|
189
|
}
|
190
|
|
191
|
$tamper_plugins = feeds_tamper_load_by_importer($from_importer_id, FALSE);
|
192
|
|
193
|
// Copy plugins to the new importer.
|
194
|
foreach ($tamper_plugins as $tamper_plugin) {
|
195
|
foreach ($tamper_plugin as $old_instance) {
|
196
|
$new_instance = clone $old_instance;
|
197
|
|
198
|
$id_parts = explode('-', $old_instance->id);
|
199
|
// Normal id.
|
200
|
if (count($id_parts) === 3) {
|
201
|
$id_parts[0] = $form_state['values']['id'];
|
202
|
$new_instance->id = implode('-', $id_parts);
|
203
|
}
|
204
|
// Someone manually set the id.
|
205
|
else {
|
206
|
$new_instance->id = $form_state['values']['id'] . '-' . implode('-', $id_parts);
|
207
|
}
|
208
|
|
209
|
$new_instance->importer = $form_state['values']['id'];
|
210
|
$new_instance->export_type = NULL;
|
211
|
$new_instance->type = t('Local');
|
212
|
unset($new_instance->table);
|
213
|
unset($new_instance->disabled);
|
214
|
feeds_tamper_save_instance($new_instance);
|
215
|
}
|
216
|
}
|
217
|
}
|