-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmirrors.module
361 lines (303 loc) · 9.76 KB
/
mirrors.module
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
<?php
/**
* @file
* Creates an Export View and an Import Feed for your entities
*/
/**
* Per entity-bundle a feed importer and a views-exporter will be created.
*
* Logic:
* 1. Function that returns fields and properties for supported bundles.
* 2. Function that creates feed-configurations.
* 3. Function that creates view-configurations.
*/
/**
* Default separator is used in multivalue list-fields.
*/
define('MIRRORS_DEFAULT_SEPARATOR', '/');
/**
* Implements hook_ctools_plugin_type().
*/
function mirrors_ctools_plugin_type() {
return array(
'entity' => array(),
'property' => array(),
'field' => array(),
);
}
/**
* Implements ctools_plugin_directory().
*/
function mirrors_ctools_plugin_directory($module, $plugin) {
if ($module == 'mirrors') {
return "plugins/$plugin";
}
}
/**
* Implements hook_permission().
*/
function mirrors_permission() {
return array(
'administer mirrors' => array(
'title' => t('Administer mirrors'),
'description' => t('Use mirrors export-views.'),
),
);
}
/**
* Implements hook_menu().
*/
function mirrors_menu() {
$items = array();
$items['admin/structure/mirrors'] = array(
'title' => 'Mirrors',
'description' => 'Manage Mirrors settings for each bundle.',
'page callback' => 'mirrors_admin_page',
'access arguments' => array('administer mirrors'),
'file' => 'includes/mirrors.admin.inc',
);
return $items;
}
/**
* Implements hook_views_api().
*/
function mirrors_views_api() {
return array('api' => 3);
}
/**
* Implements hook_ctools_plugin_api().
*/
function mirrors_ctools_plugin_api() {
list($module, $api) = func_get_args();
if ($module == 'feeds' && $api == 'feeds_importer_default') {
return array('version' => 1);
}
if ($module == "feeds_tamper" && $api == "feeds_tamper_default") {
return array("version" => "2");
}
}
/**
* Returns bundle configuration for supported entity and field types.
*
* These configurations are used by mirrors_views and mirrors_feeds
* to create exporters and importers.
*/
function mirrors_bundles() {
$mirrors_bundles = _mirrors_supported_bundles();
_mirrors_add_property_settings($mirrors_bundles);
_mirrors_add_fields($mirrors_bundles);
return $mirrors_bundles;
}
/**
* Returns bundle configuration for supported entity and field types.
*
* These configurations are used by mirrors_views and mirrors_feeds
* to create exporters and importers.
*/
function mirrors_enabled_bundles() {
$mirrors_enabled_bundles = array();
$mirrors_bundles = mirrors_bundles();
// Load saved values from database.
$stored_values = variable_get('mirrors_settings');
if (isset($stored_values)) {
// Loop through all bundles and check if enabled.
foreach ($mirrors_bundles as $entity_type_key => $entity_type) {
foreach ($entity_type as $bundle_key => $mirrors_bundle) {
// Check if this bundle is enabled.
$stored_value_key = $entity_type_key . '__' . $bundle_key;
if (isset($stored_values[$stored_value_key]) && $stored_values[$stored_value_key] === $stored_value_key) {
$mirrors_enabled_bundles[$entity_type_key][$bundle_key] = $mirrors_bundle;
}
}
}
}
return $mirrors_enabled_bundles;
}
/**
* Implements hook_views_default_views().
*/
function mirrors_views_default_views() {
module_load_include('inc', 'mirrors', 'includes/views/mirrors.views');
$export = array();
$mirrors_enabled_bundles = mirrors_enabled_bundles();
foreach ($mirrors_enabled_bundles as $entity_type_key => $entity_type) {
foreach ($entity_type as $bundle_key => $mirrors_bundle) {
$name = "mirrors_" . $entity_type_key . "__" . $bundle_key;
$try = mirrors_views_create($name, $entity_type_key, $bundle_key, $mirrors_bundle);
if ($try !== FALSE) {
$export[$name] = $try;
}
}
}
return $export;
}
/**
* Implements hook_feeds_importer_default().
*/
function mirrors_feeds_importer_default() {
module_load_include('inc', 'mirrors', 'includes/feeds/mirrors.feeds');
$export = array();
$mirrors_enabled_bundles = mirrors_enabled_bundles();
foreach ($mirrors_enabled_bundles as $entity_type_key => $entity_type) {
foreach ($entity_type as $bundle_key => $mirrors_bundle) {
$name = "mirrors_" . $entity_type_key . "__" . $bundle_key;
$try = mirrors_feeds_create($name, $entity_type_key, $bundle_key, $mirrors_bundle);
if ($try !== FALSE) {
$export[$name] = $try;
}
}
}
return $export;
}
/**
* Implements hook_feeds_tamper_default().
*
* Feeds Tamper is acting only on multivalue-fields (lists). We find these
* and create appropriate tampers for them.
*
* @todo: list is not working for entity-reference.
*/
function mirrors_feeds_tamper_default() {
module_load_include('inc', 'mirrors', 'includes/feeds_tamper/mirrors.feeds_tamper');
$export = array();
$mirrors_enabled_bundles = mirrors_enabled_bundles();
foreach ($mirrors_enabled_bundles as $entity_type_key => $entity_type) {
foreach ($entity_type as $bundle_key => $mirrors_bundle) {
// Now through the fields and act when its a list field.
foreach ($mirrors_bundle['fields'] as $key => $field) {
if (array_key_exists('list', $field) && $field['list'] == TRUE) {
$name = "mirrors_" . $entity_type_key . "__" . $bundle_key;
$field_id = $key;
$try = mirrors_feeds_tamper_create($name, $field_id);
if ($try !== FALSE) {
$export[$name . '-' . $field_id . '-explode'] = $try;
}
}
}
}
}
return $export;
}
/**
* Returns supported entity_types, hookable.
*/
function mirrors_entity_types() {
ctools_include('plugins');
return ctools_get_plugins('mirrors', 'entity');
}
/**
* Returns supported field_types, hookable.
*/
function mirrors_field_types() {
ctools_include('plugins');
return ctools_get_plugins('mirrors', 'field');
}
/**
* Returns supported property_types, hookable.
*/
function mirrors_property_types() {
ctools_include('plugins');
return ctools_get_plugins('mirrors', 'property');
}
/**
* Returns a condensed array with active bundles per entity_type.
*/
function _mirrors_supported_bundles() {
$mirrors_bundles = array();
// $bundles contains all bundles in the drupal instance.
$bundles = field_info_bundles();
// $mirrors_entity_types contains only supported entity types.
$mirrors_entity_types = mirrors_entity_types();
// Loop through all entity types (node, user, etc.).
foreach ($bundles as $entity_type_key => $entity_type) {
// If supported entity type, loop through its bundles (article, page, etc.).
if (array_key_exists($entity_type_key, $mirrors_entity_types)) {
foreach ($entity_type as $bundle_key => $bundle) {
$mirrors_bundles[$entity_type_key][$bundle_key] = $mirrors_entity_types[$entity_type_key];
}
}
else {
// Unsupported entity_type.
}
}
return $mirrors_bundles;
}
/**
* Add properties settings.
*/
function _mirrors_add_property_settings(&$mirrors_bundles) {
$mirrors_property_types = mirrors_property_types();
// Loop through all entity types (node, user, etc.).
foreach ($mirrors_bundles as &$entity_type) {
// Loop through all bundles (article, page, etc.).
foreach ($entity_type as &$mirrors_bundle) {
// Loop through all properties (nid, status, etc.).
foreach ($mirrors_bundle['properties'] as &$property) {
// If supported property type, add to mirrors condensed array.
if (array_key_exists($property['type'], $mirrors_property_types)) {
$property = array_merge_recursive(
$property,
$mirrors_property_types[$property['type']]
);
}
}
}
}
}
/**
* Add fields to bundles.
*
* @todo
* - use 'field_info_field()' for recognizing field-types. Currently only used
* for entityreference fields.
*/
function _mirrors_add_fields(&$mirrors_bundles) {
$mirrors_field_types = mirrors_field_types();
$entities = entity_get_property_info();
// Loop through all entity types (node, user, etc.).
foreach ($mirrors_bundles as $entity_type_key => &$entity_type) {
// Loop through all bundles (article, page, etc.).
foreach ($entity_type as $bundle_key => &$mirrors_bundle) {
$mirrors_fields = array();
// Loop through all fields.
if (isset($entities[$entity_type_key]['bundles'][$bundle_key]['properties'])) {
$fields = $entities[$entity_type_key]['bundles'][$bundle_key]['properties'];
foreach ($fields as $key => $field) {
$field_type = mirrors_field_type_filter_list($field['type']);
// Lookup if a field is an entityreference.
$field_info = field_info_field($key);
if ($field_info['type'] == 'entityreference') {
$field_type = 'entityreference';
}
// If we deal with a list, we put that in the condensed array so
// importers and exporters can act on it.
if ($field['type'] !== $field_type) {
$mirrors_field_types[$field_type]['list'] = TRUE;
}
// If supported field type, add to mirrors condensed array.
if (array_key_exists($field_type, $mirrors_field_types)) {
$field = $mirrors_field_types[$field_type];
$field['type'] = $field_type;
$mirrors_fields[$key] = $field;
}
else {
// Unsupported field type.
}
}
}
$mirrors_bundle['fields'] = $mirrors_fields;
}
}
}
/**
* Helper function for lists.
*/
function mirrors_field_type_filter_list($field_type) {
// Check if we are dealing with a list.
if (substr($field_type, 0, 4) == 'list') {
// Extract field_type "list<$field_type>".
$length = strlen($field_type);
$field_type = substr($field_type, 5, $length - 6);
}
return $field_type;
}