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