1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Version agnostic parts of feeds_tamper.module.
|
6
|
*/
|
7
|
|
8
|
|
9
|
/**
|
10
|
* @defgroup feeds_tamper_api Feeds Tamper API
|
11
|
* @{
|
12
|
* API functions for dealing with plugins and plugin instances.
|
13
|
*/
|
14
|
|
15
|
/**
|
16
|
* Create a new plugin instance with default values filled in.
|
17
|
*
|
18
|
* @return stdClass
|
19
|
* A new plugin instance object.
|
20
|
*/
|
21
|
function feeds_tamper_new_instance() {
|
22
|
ctools_include('export');
|
23
|
return ctools_export_crud_new('feeds_tamper');
|
24
|
}
|
25
|
|
26
|
/**
|
27
|
* Load all plugin instances.
|
28
|
*
|
29
|
* @param bool $disabled
|
30
|
* (optional) If TRUE, load disabled plugin instances. Defaults to FALSE.
|
31
|
* @param bool $reset
|
32
|
* (optional) If TRUE, the static cache of all objects will be flushed prior
|
33
|
* to loading. Defaults to FALSE.
|
34
|
*
|
35
|
* @return array
|
36
|
* An associative array of plugin instances keyed to their id.
|
37
|
*/
|
38
|
function feeds_tamper_load_all_instances($disabled = FALSE, $reset = FALSE) {
|
39
|
$instances = array();
|
40
|
ctools_include('export');
|
41
|
$configs = ctools_export_crud_load_all('feeds_tamper', $reset);
|
42
|
foreach ($configs as $instance_id => $config) {
|
43
|
if (!empty($config->id) && ($disabled || empty($config->disabled))) {
|
44
|
$instances[$instance_id] = $config;
|
45
|
}
|
46
|
}
|
47
|
return $instances;
|
48
|
}
|
49
|
|
50
|
/**
|
51
|
* Load a plugin instance by id.
|
52
|
*
|
53
|
* @param string $id
|
54
|
* The id of the plugin instance.
|
55
|
*
|
56
|
* @return stdClass
|
57
|
* A plugin instance object.
|
58
|
*/
|
59
|
function feeds_tamper_load_instance($id) {
|
60
|
$id = drupal_substr($id, 0, 127);
|
61
|
ctools_include('export');
|
62
|
return ctools_export_crud_load('feeds_tamper', $id);
|
63
|
}
|
64
|
|
65
|
/**
|
66
|
* Save a plugin instance.
|
67
|
*
|
68
|
* @param stdClass $instance
|
69
|
* A plugin instance object.
|
70
|
*
|
71
|
* @return mixed
|
72
|
* If the plugin save failed, returns FALSE. If it succeeded, returns
|
73
|
* SAVED_NEW or SAVED_UPDATED, depending on the operation performed.
|
74
|
*/
|
75
|
function feeds_tamper_save_instance($instance) {
|
76
|
$instance->id = drupal_substr($instance->id, 0, 127);
|
77
|
|
78
|
ctools_include('export');
|
79
|
// It's a new instance, give it the heaviest weight.
|
80
|
if (!isset($instance->weight)) {
|
81
|
$conditions = array('importer' => $instance->importer, 'source' => $instance->source);
|
82
|
$all = ctools_export_load_object('feeds_tamper', 'conditions', $conditions);
|
83
|
$weight = 0;
|
84
|
foreach ($all as $i) {
|
85
|
if ($i->weight >= $weight) {
|
86
|
$weight = $i->weight + 1;
|
87
|
}
|
88
|
}
|
89
|
$instance->weight = $weight;
|
90
|
}
|
91
|
$disabled = variable_get('default_feeds_tamper', array());
|
92
|
$disabled[$instance->id] = !empty($instance->disabled) ? TRUE : FALSE;
|
93
|
variable_set('default_feeds_tamper', $disabled);
|
94
|
return ctools_export_crud_save('feeds_tamper', $instance);
|
95
|
}
|
96
|
|
97
|
/**
|
98
|
* Removes plugin instances whos source was removed.
|
99
|
*
|
100
|
* @param FeedsImporter $importer
|
101
|
* The importer to rectify against.
|
102
|
*/
|
103
|
function feeds_tamper_rectify_instances(FeedsImporter $importer) {
|
104
|
ctools_include('export');
|
105
|
$instances = ctools_export_load_object('feeds_tamper', 'conditions', array('importer' => $importer->id));
|
106
|
$sources = feeds_tamper_get_unique_source_list($importer, FALSE);
|
107
|
|
108
|
foreach ($instances as $instance) {
|
109
|
if (in_array($instance->source, $sources)) {
|
110
|
continue;
|
111
|
}
|
112
|
|
113
|
if ($instance->export_type == EXPORT_IN_DATABASE) {
|
114
|
feeds_tamper_delete_instance($instance);
|
115
|
}
|
116
|
else {
|
117
|
$instance->disabled = TRUE;
|
118
|
feeds_tamper_save_instance($instance);
|
119
|
}
|
120
|
}
|
121
|
}
|
122
|
|
123
|
/**
|
124
|
* Delete a single plugin instance.
|
125
|
*
|
126
|
* @param string|object $instance
|
127
|
* A plugin instance object or the id of a plugin instance.
|
128
|
*/
|
129
|
function feeds_tamper_delete_instance($instance) {
|
130
|
// Allow for string id or plugin object.
|
131
|
if (is_scalar($instance)) {
|
132
|
$instance = feeds_tamper_load_instance($instance);
|
133
|
}
|
134
|
ctools_include('export');
|
135
|
ctools_export_crud_delete('feeds_tamper', $instance);
|
136
|
}
|
137
|
|
138
|
/**
|
139
|
* Load plugin instances by importer id.
|
140
|
*
|
141
|
* @param string|FeedsImporter $importer
|
142
|
* The importer id, or object to reference.
|
143
|
* @param bool $disabled
|
144
|
* (optional) If TRUE load disabled plugin instances. Defaults to FALSE.
|
145
|
*
|
146
|
* @return array
|
147
|
* An associative array of plugin instances, keyed by source.
|
148
|
*/
|
149
|
function feeds_tamper_load_by_importer($importer, $disabled = FALSE) {
|
150
|
if (is_scalar($importer)) {
|
151
|
$importer = feeds_importer($importer);
|
152
|
}
|
153
|
|
154
|
ctools_include('export');
|
155
|
$t = ctools_export_load_object('feeds_tamper', 'conditions', array('importer' => $importer->id));
|
156
|
|
157
|
$sources = feeds_tamper_get_unique_source_list($importer, FALSE);
|
158
|
|
159
|
$plugins = feeds_tamper_get_plugins();
|
160
|
|
161
|
$instances = array();
|
162
|
foreach ($t as $i) {
|
163
|
// Add an empty settings array, if the plugin instance doesn't provide any settings itself.
|
164
|
if (empty($i->settings)) {
|
165
|
$i->settings = array();
|
166
|
}
|
167
|
// Filter disabled and only pull plugins that actually have a corresponding
|
168
|
// source since we don't delete them with a mapping.
|
169
|
if (($disabled || empty($i->disabled)) && in_array($i->source, $sources) && isset($plugins[$i->plugin_id]) && function_exists($plugins[$i->plugin_id]['callback'])) {
|
170
|
$instances[$i->source][] = $i;
|
171
|
}
|
172
|
}
|
173
|
foreach ($instances as &$instance_list) {
|
174
|
usort($instance_list, '_feeds_tamper_cmp');
|
175
|
}
|
176
|
|
177
|
// Sort the plugins by the order they appear on the mapping page.
|
178
|
$return = array();
|
179
|
foreach ($sources as $source) {
|
180
|
if (isset($instances[$source])) {
|
181
|
$return[$source] = $instances[$source];
|
182
|
}
|
183
|
}
|
184
|
|
185
|
return $return;
|
186
|
}
|
187
|
|
188
|
/**
|
189
|
* Returns a unique list of sources in order.
|
190
|
*
|
191
|
* @param FeedsImporter $importer
|
192
|
* The importer.
|
193
|
*
|
194
|
* @return array
|
195
|
* A list of sources.
|
196
|
*/
|
197
|
function feeds_tamper_get_unique_source_list($importer, $lower = TRUE) {
|
198
|
$sources = array();
|
199
|
$is_csv = $lower && ($importer->parser instanceof FeedsCSVParser);
|
200
|
|
201
|
foreach ($importer->processor->getMappings() as $mapping) {
|
202
|
if ($is_csv) {
|
203
|
$sources[] = drupal_strtolower($mapping['source']);
|
204
|
}
|
205
|
else {
|
206
|
$sources[] = $mapping['source'];
|
207
|
}
|
208
|
}
|
209
|
|
210
|
return array_unique($sources);
|
211
|
}
|
212
|
|
213
|
/**
|
214
|
* Comparison callback that sorts by weight, then alphabetically by id.
|
215
|
*/
|
216
|
function _feeds_tamper_cmp($a, $b) {
|
217
|
// If weights are equal compare id's.
|
218
|
if ($a->weight == $b->weight) {
|
219
|
$tmp = array($a->id, $b->id);
|
220
|
sort($tmp);
|
221
|
if ($tmp[0] == $a->id) {
|
222
|
return -1;
|
223
|
}
|
224
|
return 1;
|
225
|
}
|
226
|
return ($a->weight < $b->weight) ? -1 : 1;
|
227
|
}
|
228
|
|
229
|
/**
|
230
|
* Get all available plugins.
|
231
|
*
|
232
|
* @return array
|
233
|
* An associative array where the keys are the plugin keys and the values are
|
234
|
* the plugin info arrays as defined in a plugin include file.
|
235
|
*/
|
236
|
function feeds_tamper_get_plugins() {
|
237
|
ctools_include('plugins');
|
238
|
return ctools_get_plugins('feeds_tamper', 'plugins');
|
239
|
}
|
240
|
|
241
|
/**
|
242
|
* Get a single plugin.
|
243
|
*
|
244
|
* @param string $id
|
245
|
* The id of a plugin.
|
246
|
*
|
247
|
* @return stdClass
|
248
|
* A plugin object.
|
249
|
*/
|
250
|
function feeds_tamper_get_plugin($id) {
|
251
|
ctools_include('plugins');
|
252
|
return ctools_get_plugins('feeds_tamper', 'plugins', $id);
|
253
|
}
|
254
|
|
255
|
/**
|
256
|
* Return a machine name safe version of a string.
|
257
|
*
|
258
|
* @param string $string
|
259
|
* String to get machine nameized.
|
260
|
*
|
261
|
* @return string
|
262
|
* A lowercase string with all values not in [a-zA-Z0-9] replaced with an
|
263
|
* underscore and shortened to 128 characters.
|
264
|
*/
|
265
|
function feeds_tamper_make_machine($string) {
|
266
|
return drupal_substr(preg_replace('/[^a-z0-9-]/u', '_', drupal_strtolower($string)), 0, 127);
|
267
|
}
|
268
|
|
269
|
/**
|
270
|
* @} End of "feeds_tamper_api".
|
271
|
*/
|
272
|
|
273
|
/**
|
274
|
* Menu access callback.
|
275
|
*
|
276
|
* @param string|FeedsImporter $importer
|
277
|
* The importer or importer id being tampered with.
|
278
|
* @param string|stdClass $instance
|
279
|
* (optional) If set, the importer attached to $instance will be tried first.
|
280
|
* Defaults to NULL.
|
281
|
*
|
282
|
* @return bool
|
283
|
* TRUE if the user has acces, FALSE if not.
|
284
|
*/
|
285
|
function feeds_tamper_access($importer, $instance = NULL) {
|
286
|
if (isset($instance)) {
|
287
|
if (is_object($instance)) {
|
288
|
$importer = $instance->importer;
|
289
|
}
|
290
|
else {
|
291
|
$importer = feeds_tamper_load_instance($instance)->importer;
|
292
|
}
|
293
|
}
|
294
|
elseif (is_object($importer)) {
|
295
|
$importer = $importer->id;
|
296
|
}
|
297
|
|
298
|
// Verify actual input if above failed.
|
299
|
if ($importer) {
|
300
|
// Check for permissions, otherwise return FALSE.
|
301
|
if (user_access('administer feeds_tamper') || user_access('tamper ' . $importer)) {
|
302
|
return TRUE;
|
303
|
}
|
304
|
}
|
305
|
return FALSE;
|
306
|
}
|
307
|
|
308
|
/**
|
309
|
* Implements hook_form_feeds_ui_mapping_form_alter().
|
310
|
*
|
311
|
* This is an interesting bit of work. Each source name has to be unique,
|
312
|
* but we have no idea how many to create with
|
313
|
* feeds_tamper_feeds_parser_sources_alter() because we don't know how many
|
314
|
* targets there are going to be.
|
315
|
*
|
316
|
* The solution is to keep track in the form how many have been added.
|
317
|
*/
|
318
|
function feeds_tamper_form_feeds_ui_mapping_form_alter(array &$form, array &$form_state) {
|
319
|
$form['#submit'][] = 'feeds_tamper_form_feeds_ui_mapping_form_submit';
|
320
|
|
321
|
// Don't alter the sources on the form for parsers that use manual input.
|
322
|
$manual_source_input = ($form['source']['#type'] == 'textfield');
|
323
|
|
324
|
$max_source = 0;
|
325
|
$max_target = 0;
|
326
|
foreach ($form['#mappings'] as $mapping) {
|
327
|
if (!$manual_source_input && strpos($mapping['source'], 'Blank source ') === 0) {
|
328
|
list(, , $source_value) = explode(' ', $mapping['source']);
|
329
|
if ($source_value > $max_source) {
|
330
|
$max_source = $source_value;
|
331
|
}
|
332
|
}
|
333
|
if (strpos($mapping['target'], 'Temporary target ') === 0) {
|
334
|
list(, , $target_value) = explode(' ', $mapping['target']);
|
335
|
if ($target_value > $max_target) {
|
336
|
$max_target = $target_value;
|
337
|
}
|
338
|
}
|
339
|
}
|
340
|
|
341
|
if (!$manual_source_input) {
|
342
|
if ($max_source) {
|
343
|
unset($form['source']['#options']['Blank source 1']);
|
344
|
$form['source']['#options']['Blank source ' . ++$max_source] = 'Blank source';
|
345
|
|
346
|
// Declare extra mapping sources.
|
347
|
for ($i = 2; $i <= $max_source; $i++) {
|
348
|
$form['#feeds_sources']['Blank source ' . $i] = $form['#feeds_sources']['Blank source 1'];
|
349
|
}
|
350
|
}
|
351
|
else {
|
352
|
$form['source']['#options']['Blank source 1'] = 'Blank source';
|
353
|
}
|
354
|
}
|
355
|
|
356
|
if ($max_target) {
|
357
|
unset($form['target']['#options']['Temporary target 1']);
|
358
|
$form['target']['#options']['Temporary target ' . ++$max_target] = 'Temporary target';
|
359
|
|
360
|
// Declare extra mapping targets.
|
361
|
for ($i = 2; $i <= $max_target; $i++) {
|
362
|
$form['#feeds_targets']['Temporary target ' . $i] = $form['#feeds_targets']['Temporary target 1'];
|
363
|
}
|
364
|
}
|
365
|
else {
|
366
|
$form['target']['#options']['Temporary target 1'] = 'Temporary target';
|
367
|
}
|
368
|
}
|
369
|
|
370
|
/**
|
371
|
* Submit callback for feeds_ui_mapping_form().
|
372
|
*/
|
373
|
function feeds_tamper_form_feeds_ui_mapping_form_submit(array &$form, array &$form_state) {
|
374
|
feeds_tamper_rectify_instances(feeds_importer($form['#importer']));
|
375
|
}
|
376
|
|
377
|
/**
|
378
|
* Implements hook_ctools_plugin_api().
|
379
|
*/
|
380
|
function feeds_tamper_ctools_plugin_api($owner, $api) {
|
381
|
if ($owner == 'feeds_tamper' && $api == 'plugins') {
|
382
|
return array('version' => 2);
|
383
|
}
|
384
|
}
|
385
|
|
386
|
/**
|
387
|
* Implements hook_ctools_plugin_directory().
|
388
|
*/
|
389
|
function feeds_tamper_ctools_plugin_directory($module, $plugin) {
|
390
|
if ($module == 'feeds_tamper') {
|
391
|
return 'plugins';
|
392
|
}
|
393
|
}
|