This repository has been archived by the owner on Jan 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrupal_sync.admin.inc
578 lines (497 loc) · 20.9 KB
/
drupal_sync.admin.inc
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
<?php
/**
* @file
* Settings forms
*/
/**
* Implements hook_drupal_sync_prepare_types().
*/
function drupal_sync_drupal_sync_prepare_types($types) {
$node_types = node_type_get_types();
foreach ($node_types as $key => $value) {
$types['node'][$key] = array(
'type' => $value->type,
'name' => $value->name
);
}
$taxonomy_voc = taxonomy_get_vocabularies();
foreach ($taxonomy_voc as $key => $value) {
$types['taxonomy_term'][$value->machine_name] = array(
'vid' => $value->vid,
'name' => $value->name
);
}
return $types;
}
/**
* Implements hook_drupal_sync_default_fields_info().
*/
function drupal_sync_drupal_sync_default_fields_info($fields) {
$fields['node'] = array(
'title' => array(
'label' => t('Title'),
),
'status' => array(
'label' => t('Published'),
),
'promote' => array(
'label' => t('Promoted to front page'),
),
'comment' => array(
'label' => t('Comments on/off'),
),
);
$fields['taxonomy_term'] = array(
'name' => array(
'label' => t('Name'),
),
'description' => array(
'label' => t('Description'),
),
'parent' => array(
'label' => t('Parent'),
),
);
return $fields;
}
/**
* Implements hook_drupal_sync_types_build_form_alter().
*/
function drupal_sync_drupal_sync_types_build_form_alter(&$form, $types, $defaults = array()) {
foreach ($types as $type => $value) {
switch ($type) {
case 'node':
$options = array();
foreach ($value as $option) {
$options[$option['type']] = $option['name'];
}
$form['drupal_sync_settings']['drupal_sync_entities']['drupal_sync_node_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Available node types'),
'#collapsible' => TRUE,
);
$form['drupal_sync_settings']['drupal_sync_entities']['drupal_sync_node_settings']['drupal_sync_node_types'] = array(
'#type' => 'checkboxes',
'#title' => t('Choose node types for sync'),
'#default_value' => (isset($defaults['node'])) ? array_keys($defaults['node']) : array(),
'#options' => $options,
);
break;
case 'taxonomy_term':
$options = array();
foreach ($value as $option) {
$options[$option['vid']] = $option['name'];
}
$form['drupal_sync_settings']['drupal_sync_entities']['drupal_sync_taxonomy_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Available taxonomy vocabularies'),
'#collapsible' => TRUE,
);
$form['drupal_sync_settings']['drupal_sync_entities']['drupal_sync_taxonomy_settings']['drupal_sync_taxonomy_vocabularies'] = array(
'#type' => 'checkboxes',
'#title' => t('Choose vocabularies for sync'),
'#default_value' => (isset($defaults['taxonomy_term'])) ? array_keys($defaults['taxonomy_term']) : array(),
'#options' => $options,
);
break;
}
}
return $form;
}
/**
* Implements hook_drupal_sync_settings_submit_form_alter().
*/
function drupal_sync_drupal_sync_settings_submit_form_alter(&$data, $form_state) {
$settings = (isset($form_state['values']['drupal_sync_settings'])) ? $form_state['values']['drupal_sync_settings'] : array();
$node_settings = isset($settings['drupal_sync_entities']['drupal_sync_node_settings']['drupal_sync_node_types']) ? $settings['drupal_sync_entities']['drupal_sync_node_settings']['drupal_sync_node_types'] : array();
$node_settings = array_filter($node_settings);
foreach ($node_settings as $key => $value) {
$node_settings[$key] = (isset($form_state['original_entity_types']['node'][$key]['name'])) ? $form_state['original_entity_types']['node'][$key]['name'] : $value;
}
$taxonomy_settings = isset($settings['drupal_sync_entities']['drupal_sync_taxonomy_settings']['drupal_sync_taxonomy_vocabularies']) ? $settings['drupal_sync_entities']['drupal_sync_taxonomy_settings']['drupal_sync_taxonomy_vocabularies'] : array();
$taxonomy_settings = array_filter($taxonomy_settings);
$original_taxonomy = (isset($form_state['original_entity_types']['taxonomy_term'])) ? $form_state['original_entity_types']['taxonomy_term'] : array();
$tmp_taxonomy = array();
foreach ($original_taxonomy as $machine_name => $value) {
$tmp_taxonomy[$value['vid']] = array(
'name' => $value['name'],
'machine_name' => $machine_name
);
}
$original_taxonomy = $tmp_taxonomy;
foreach ($taxonomy_settings as $key => $value) {
$taxonomy_settings[$key] = (isset($original_taxonomy[$key]['name'])) ? $original_taxonomy[$key]['name'] : $value;
}
$data['drupal_sync_entities']['node'] = $node_settings;
$data['drupal_sync_entities']['taxonomy_term'] = $taxonomy_settings;
return $data;
}
/**
* Create settings form in drupal configuration setings section for module
*/
function drupal_sync_admin_settings_form($form, &$form_state) {
$request_limit = drupal_map_assoc(array(1, 3, 10, 20, 30, 50, 100));
$data = variable_get('drupal_sync_settings', array());
$types = array();
$types = module_invoke_all('drupal_sync_prepare_types', $types);
$form_state['original_entity_types'] = $types;
$queue_update_frequency = drupal_map_assoc(array(60, 300, 600, 1800, 3600), 'format_interval');
$queue_update_count = drupal_map_assoc(array(1, 3, 5, 10, 20, 50, 100, 200, 300, 500, 1000));
/* * ***************************************************************
* Base section
* *************************************************************** */
$form['#tree'] = TRUE;
$form['#attached']['css'][] = drupal_get_path('module', 'drupal_sync') . '/css/drupal_sync_admin.css';
$form['drupal_sync_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Sync settings'),
);
/* * ***************************************************
* Site domains
* **************************************************** */
$domains = (isset($data['drupal_sync_domains']['drupal_sync_domain'])) ? $data['drupal_sync_domains']['drupal_sync_domain'] : array();
$form['drupal_sync_settings']['drupal_sync_domains'] = array(
'#type' => 'fieldset',
'#title' => t('Current site domains'),
'#description' => t('Current site domains withour protocol and slash. E.g. site.com, m.site.com'),
'#prefix' => '<div id="drupal-sync-domain-wrapper">',
'#suffix' => '</div>',
);
if (!isset($form_state['drupal_sync_domains_num']) || empty($form_state['drupal_sync_domains_num'])) {
$form_state['drupal_sync_domains_num'] = (isset($data['drupal_sync_domains_num'])) ? $data['drupal_sync_domains_num'] : 1;
}
for ($i = 0; $i < $form_state['drupal_sync_domains_num']; $i++) {
$form['drupal_sync_settings']['drupal_sync_domains']['drupal_sync_domain'][$i] = array(
'#type' => 'textfield',
'#title' => t('Domain @count', array('@count' => ($i + 1))),
'#default_value' => (isset($domains[$i])) ? $domains[$i] : '',
'#required' => empty($i) ? TRUE : FALSE
);
}
$form['drupal_sync_settings']['drupal_sync_domains']['drupal_sync_add_domain'] = array(
'#type' => 'submit',
'#value' => t('Add one more domain'),
'#submit' => array('drupal_sync_domain_add_one'),
'#ajax' => array(
'callback' => 'drupal_sync_domain_add_more_callback',
'wrapper' => 'drupal-sync-domain-wrapper',
),
);
if ($form_state['drupal_sync_domains_num'] > 1) {
$form['drupal_sync_settings']['drupal_sync_domains']['drupal_sync_remove_domain'] = array(
'#type' => 'submit',
'#value' => t('Remove one last domain'),
'#submit' => array('drupal_sync_domain_remove_one'),
'#ajax' => array(
'callback' => 'drupal_sync_domain_add_more_callback',
'wrapper' => 'drupal-sync-domain-wrapper',
),
);
}
$form['drupal_sync_settings']['drupal_sync_request_limit'] = array(
'#type' => 'select',
'#title' => t('Count http requests (if receive fail)'),
'#default_value' => (isset($data['drupal_sync_request_limit'])) ? $data['drupal_sync_request_limit'] : 3,
'#options' => $request_limit,
'#required' => TRUE
);
$form['drupal_sync_settings']['drupal_sync_queue_update_frequency'] = array(
'#type' => 'select',
'#title' => t('Frequency of updates'),
'#default_value' => (isset($data['drupal_sync_queue_update_frequency'])) ? $data['drupal_sync_queue_update_frequency'] : 60,
'#options' => $queue_update_frequency,
'#required' => TRUE
);
$form['drupal_sync_settings']['drupal_sync_queue_update_count'] = array(
'#type' => 'select',
'#title' => t('Could of updates per request'),
'#default_value' => (isset($data['drupal_sync_queue_update_count'])) ? $data['drupal_sync_queue_update_count'] : 10,
'#options' => $queue_update_count,
'#required' => TRUE
);
$form['drupal_sync_settings']['drupal_sync_notification_mail'] = array(
'#type' => 'textfield',
'#title' => t('Email for notifications'),
'#default_value' => (isset($data['drupal_sync_notification_mail'])) ? $data['drupal_sync_notification_mail'] : '',
);
$form['drupal_sync_settings']['drupal_sync_entities'] = array(
'#type' => 'fieldset',
'#title' => t('Sync entities'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
/* * ***************************************************************
* Entities section
* *************************************************************** */
$defaults = isset($data['drupal_sync_entities']) ? $data['drupal_sync_entities'] : array();
drupal_alter('drupal_sync_types_build_form', $form, $types, $defaults);
/* * ***************************************************************
* Sync remote section
* *************************************************************** */
$form['drupal_sync_settings']['drupal_sync_remote'] = array(
'#type' => 'fieldset',
'#title' => t('Remote sites'),
'#description' => t('Send updates from current site to remote sites.'),
'#prefix' => '<div id="drupal-sync-remote-wrapper">',
'#suffix' => '</div>',
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
if (empty($form_state['drupal_sync_remote_num'])) {
$form_state['drupal_sync_remote_num'] = (isset($data['drupal_sync_remote_count'])) ? $data['drupal_sync_remote_count'] : 1;
}
for ($i = 0; $i < $form_state['drupal_sync_remote_num']; $i++) {
$form['drupal_sync_settings']['drupal_sync_remote'][$i] = array(
'#type' => 'fieldset',
'#title' => t('Site @count', array('@count' => ($i + 1))),
'#collapsible' => TRUE,
);
$form['drupal_sync_settings']['drupal_sync_remote'][$i]['drupal_sync_remote_name'] = array(
'#type' => 'textfield',
'#title' => t('Site name'),
'#description' => t('Site name must be unique, only lowercase letters, numbers, and underscores'),
'#default_value' => (isset($data['drupal_sync_remote'][$i]['name'])) ? $data['drupal_sync_remote'][$i]['name'] : '',
);
$form['drupal_sync_settings']['drupal_sync_remote'][$i]['drupal_sync_remote_url'] = array(
'#type' => 'textfield',
'#title' => t('Url'),
'#description' => t('Site Url must be unique. Site url must be with protocol but without close slash. E.g. http://site.com'),
'#default_value' => (isset($data['drupal_sync_remote'][$i]['url'])) ? $data['drupal_sync_remote'][$i]['url'] : '',
);
$form['drupal_sync_settings']['drupal_sync_remote'][$i]['drupal_sync_remote_login'] = array(
'#type' => 'textfield',
'#title' => t('Login'),
'#default_value' => (isset($data['drupal_sync_remote'][$i]['login'])) ? $data['drupal_sync_remote'][$i]['login'] : '',
);
$form['drupal_sync_settings']['drupal_sync_remote'][$i]['drupal_sync_remote_pass'] = array(
'#type' => 'textfield',
'#title' => t('Password'),
'#default_value' => (isset($data['drupal_sync_remote'][$i]['pass'])) ? $data['drupal_sync_remote'][$i]['pass'] : '',
);
}
$form['drupal_sync_settings']['drupal_sync_remote']['drupal_sync_add_name'] = array(
'#type' => 'submit',
'#value' => t('Add one more'),
'#submit' => array('drupal_sync_remote_add_one'),
'#ajax' => array(
'callback' => 'drupal_sync_remote_add_more_callback',
'wrapper' => 'drupal-sync-remote-wrapper',
),
);
if ($form_state['drupal_sync_remote_num'] > 1) {
$form['drupal_sync_settings']['drupal_sync_remote']['drupal_sync_remove_name'] = array(
'#type' => 'submit',
'#value' => t('Remove one'),
'#submit' => array('drupal_sync_remote_remove_one'),
'#ajax' => array(
'callback' => 'drupal_sync_remote_add_more_callback',
'wrapper' => 'drupal-sync-remote-wrapper',
),
);
}
$form['submit'] = array('#type' => 'submit', '#value' => t('Save'));
$form['#submit'][] = 'drupal_sync_admin_settings_form_submit';
$form['#validate'][] = 'drupal_sync_admin_settings_form_validate';
return $form;
}
/**
* Submit handler for the "add-one-more" button.
*
* Increments the max counter and causes a rebuild.
*/
function drupal_sync_remote_add_one($form, &$form_state) {
$form_state['drupal_sync_remote_num']++;
$form_state['rebuild'] = TRUE;
}
/**
* Callback for both ajax-enabled buttons.
*
* Selects and returns the fieldset with the names in it.
*/
function drupal_sync_remote_add_more_callback($form, $form_state) {
return $form['drupal_sync_settings']['drupal_sync_remote'];
}
/**
* Submit handler for the "remove one" button.
*
* Decrements the max counter and causes a form rebuild.
*/
function drupal_sync_remote_remove_one($form, &$form_state) {
if ($form_state['drupal_sync_remote_num'] > 1) {
$form_state['drupal_sync_remote_num']--;
}
$form_state['rebuild'] = TRUE;
}
/**
* Submit handler for the "add-one-more" button.
*
* Increments the max counter and causes a rebuild.
*/
function drupal_sync_domain_add_one($form, &$form_state) {
$form_state['drupal_sync_domains_num']++;
$form_state['rebuild'] = TRUE;
}
/**
* Callback for both ajax-enabled buttons.
*
* Selects and returns the fieldset with the names in it.
*/
function drupal_sync_domain_add_more_callback($form, $form_state) {
return $form['drupal_sync_settings']['drupal_sync_domains'];
}
/**
* Submit handler for the "remove one" button.
*
* Decrements the max counter and causes a form rebuild.
*/
function drupal_sync_domain_remove_one($form, &$form_state) {
if ($form_state['drupal_sync_domains_num'] > 1) {
$form_state['drupal_sync_domains_num']--;
}
$form_state['rebuild'] = TRUE;
}
/**
* Form drupal_sync_admin_settings_form Validation
*/
function drupal_sync_admin_settings_form_validate($form, &$form_state) {
$url_values = array();
$name_values = array();
foreach ($form_state['values']['drupal_sync_settings']['drupal_sync_remote'] as $key => $value) {
if (is_numeric($key)) {
$url_values[$key] = $value['drupal_sync_remote_url'];
$name_values[$key] = $value['drupal_sync_remote_name'];
}
}
//check url duplicates
$duplicate_url_values = _drupal_sync_get_duplicates_values($url_values);
if (!empty($duplicate_url_values)) {
//search url duplicates
foreach ($url_values as $i => $value) {
if (in_array($value, $duplicate_url_values)) {
form_set_error('drupal_sync_settings][drupal_sync_remote][' . $i . '][drupal_sync_remote_url', 'Error, site URLs must be unique');
}
}
}
//check allowed name symbols
foreach ($name_values as $i => $value) {
if (preg_match('@[^a-z0-9_]@', $value)) {
form_set_error('drupal_sync_settings][drupal_sync_remote][' . $i . '][drupal_sync_remote_name', t('The machine-readable name must contain only lowercase letters, numbers, and underscores.'));
}
}
//check names duplicates
$duplicate_name_values = _drupal_sync_get_duplicates_values($name_values);
if (!empty($duplicate_name_values)) {
//search url duplicates
foreach ($name_values as $i => $value) {
if (in_array($value, $duplicate_name_values)) {
form_set_error('drupal_sync_settings][drupal_sync_remote][' . $i . '][drupal_sync_remote_name', 'Error, site Name must be unique');
}
}
}
}
/**
* Takes an array and returns an array of duplicate items
*
* @param array $array
*
* @return array
*/
function _drupal_sync_get_duplicates_values($array) {
return array_diff_assoc($array, array_unique($array));
}
/**
* Form drupal_sync_admin_settings_form submit function
*/
function drupal_sync_admin_settings_form_submit($form, &$form_state) {
$data = array();
$settings = (isset($form_state['values']['drupal_sync_settings'])) ? $form_state['values']['drupal_sync_settings'] : array();
$data['drupal_sync_request_limit'] = $settings['drupal_sync_request_limit'];
$data['drupal_sync_queue_update_frequency'] = $settings['drupal_sync_queue_update_frequency'];
$data['drupal_sync_queue_update_count'] = $settings['drupal_sync_queue_update_count'];
$data['drupal_sync_notification_mail'] = $settings['drupal_sync_notification_mail'];
$data['drupal_sync_remote_count'] = (isset($form_state['drupal_sync_remote_num'])) ? $form_state['drupal_sync_remote_num'] : 1;
$data['drupal_sync_domains_num'] = (isset($form_state['drupal_sync_domains_num'])) ? $form_state['drupal_sync_domains_num'] : 1;
$data['drupal_sync_domains'] = $settings['drupal_sync_domains'];
$remote = (isset($settings['drupal_sync_remote'])) ? $settings['drupal_sync_remote'] : array();
for ($i = 0; $i < $data['drupal_sync_remote_count']; $i++) {
if (isset($remote[$i])) {
$data['drupal_sync_remote'][$i] = array(
'name' => (isset($remote[$i]['drupal_sync_remote_name'])) ? $remote[$i]['drupal_sync_remote_name'] : '',
'url' => (isset($remote[$i]['drupal_sync_remote_url'])) ? $remote[$i]['drupal_sync_remote_url'] : '',
'login' => (isset($remote[$i]['drupal_sync_remote_login'])) ? $remote[$i]['drupal_sync_remote_login'] : '',
'pass' => (isset($remote[$i]['drupal_sync_remote_pass'])) ? $remote[$i]['drupal_sync_remote_pass'] : '',
);
}
}
drupal_alter('drupal_sync_settings_submit_form', $data, $form_state);
variable_set('drupal_sync_settings', $data);
// clear auth cache
variable_del('drupal_sync_xmlrpc_auth_options');
// clear menu cache
menu_cache_clear_all();
drupal_set_message(t('The settings are saved.'), 'status');
}
/**
* Drupal sync backup/restore functionality
*
* @return string
*/
function drupal_sync_settings_backup() {
$action = arg(5);
$domain_name = str_replace('www.', '', $_SERVER['HTTP_HOST']);
$domain_name = str_replace(array('.', '-'), array('_', '_'), $domain_name);
if (!drupal_realpath('private://')) {
drupal_set_message(t('Please, set up Private file system path !link', array('!link' => l(t('here'), 'admin/config/media/file-system'))), 'error');
return '';
}
$filedir = 'private://drupal_sync';
file_prepare_directory($filedir, FILE_CREATE_DIRECTORY);
$file_real_name = drupal_realpath($filedir) . "/" . $domain_name . ".dat";
if (!file_exists($file_real_name)) {
$fh = fopen($file_real_name, "x+");
fclose($fh);
chmod($file_real_name, 0440);
}
if ($action == 'restore') {
$data = file_get_contents($file_real_name);
$data = unserialize($data);
if (isset($data['settings']) && isset($data['relations'])) {
variable_set('drupal_sync_settings', $data['settings']);
variable_set('drupal_sync_remote_relation_settings', $data['relations']);
drupal_set_message(t("Drupal sync settings have been restored"));
}
else {
drupal_set_message(t("Error! Drupal sync settings have not been restored. Wrong format of backup file!"), 'error');
}
drupal_goto("admin/config/drupal_sync/settings/backup");
}
elseif ($action == 'backup') {
$data = array(
'settings' => variable_get('drupal_sync_settings', array()),
'relations' => variable_get('drupal_sync_remote_relation_settings', array())
);
if (function_exists('posix_getpwuid') && function_exists('posix_geteuid')) {
$apache_user = posix_getpwuid(posix_geteuid());
$file_owner = posix_getpwuid(fileowner($file_real_name));
if ($apache_user != $file_owner) {
drupal_set_message(t("Drupal sync settings have not been saved.") . " <br>" . t("Check backup file owner permissions!"), 'error');
drupal_goto("admin/config/drupal_sync/settings/backup");
}
}
$perms = chmod($file_real_name, 0774);
if ($perms) {
$result = file_put_contents($file_real_name, serialize($data));
chmod($file_real_name, 0440);
drupal_set_message(t('Drupal sync settings have been saved'));
}
else {
$result = file_put_contents($file_real_name, serialize($data));
chmod($file_real_name, 0440);
drupal_set_message(t('Drupal sync settings have not been saved. Check backup file permissions!'), 'error');
}
drupal_goto("admin/config/drupal_sync/settings/backup");
}
$content = l("[ " . t("Create backup") . " ]", "admin/config/drupal_sync/settings/backup/backup") . "<br><br>" .
l("[ " . t("Restore backup") . " ]", "admin/config/drupal_sync/settings/backup/restore");
return $content;
}