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
/
drupal_sync.module
227 lines (198 loc) · 6.41 KB
/
drupal_sync.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
<?php
/**
* @file
* Drupal Sync module.
*/
define('DRUPAL_SYNC_MAX_ERRORS', 3);
module_load_include('inc', 'drupal_sync', 'includes/drupal_sync.api');
module_load_include('inc', 'drupal_sync', 'includes/drupal_sync.xmlrpc');
module_load_include('inc', 'drupal_sync', 'includes/drupal_sync.entity');
module_load_include('inc', 'drupal_sync', 'includes/drupal_sync.queue');
module_load_include('inc', 'drupal_sync', 'includes/drupal_sync.forms');
module_load_include('inc', 'drupal_sync', 'drupal_sync.admin');
/**
* Implements hook_menu().
*/
function drupal_sync_menu() {
$items = array();
$items['admin/config/drupal_sync'] = array(
'title' => 'Drupal Sync',
'description' => 'Adjust Drupal Sync options.',
'position' => 'right',
'weight' => -5,
'page callback' => 'system_admin_menu_block_page',
'access arguments' => array('administer site configuration'),
'file' => 'system.admin.inc',
'file path' => drupal_get_path('module', 'system'),
);
$items['admin/config/drupal_sync/settings'] = array(
'title' => 'Drupal Sync settings',
'type' => MENU_NORMAL_ITEM,
'access arguments' => array('administer drupal sync settings'),
'page callback' => 'drupal_get_form',
'page arguments' => array('drupal_sync_admin_settings_form'),
'file' => 'drupal_sync.admin.inc',
);
$items['admin/config/drupal_sync/settings/edit'] = array(
'title' => 'Drupal Sync settings',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items['admin/config/drupal_sync/settings/backup'] = array(
'title' => 'Drupal Sync settings backup/restore',
'type' => MENU_LOCAL_TASK,
'access arguments' => array('administer drupal sync settings'),
'page callback' => 'drupal_sync_settings_backup',
'file' => 'drupal_sync.admin.inc',
);
$items['run-drupal-sync'] = array(
'title' => 'Run drupal sync manually',
'type' => MENU_CALLBACK,
'page callback' => 'drupal_sync_run_manually',
'access arguments' => array('administer drupal sync settings'),
);
//add node sync settings tab
$items['admin/structure/types/manage/%node_type/sync_settings'] = array(
'title' => 'Synchronization settings',
'type' => MENU_LOCAL_TASK,
'access callback' => 'drupal_sync_access_entity_settings_menu_tabs',
'access arguments' => array('node', 4),
'page callback' => 'drupal_get_form',
'page arguments' => array('drupal_sync_add_entity_sync_settings_form', 'node', 4),
);
//add taxonomy sync settings tab
$items['admin/structure/taxonomy/%taxonomy_vocabulary_machine_name/sync_settings'] = array(
'title' => 'Synchronization settings',
'type' => MENU_LOCAL_TASK,
'access callback' => 'drupal_sync_access_entity_settings_menu_tabs',
'access arguments' => array('taxonomy_term', 3),
'page callback' => 'drupal_get_form',
'page arguments' => array('drupal_sync_add_entity_sync_settings_form', 'taxonomy_term', 3),
);
return $items;
}
/***********************************************************
* Menu callbacks
***********************************************************/
/**
* Manually run drupal sync process.
*/
function drupal_sync_run_manually() {
drupal_sync_run_sync();
drupal_set_message(t("Drupal sync ran successfully."));
drupal_goto("admin/config/drupal_sync/drupal_sync_manager");
}
/**
* Menu access callback
*
* @param type $entity_type
* @param type $entity_type_obj
*
* @return boolean
*/
function drupal_sync_access_entity_settings_menu_tabs($entity_type, $entity_type_obj) {
if (!user_access('administer drupal sync settings')) {
return FALSE;
}
else {
$drupal_sync_settings = variable_get('drupal_sync_settings', array());
$entity_type_id = _drupal_sync_get_entity_type_id($entity_type, $entity_type_obj);
if (isset($drupal_sync_settings['drupal_sync_entities'][$entity_type][$entity_type_id])) {
return TRUE;
}
}
return FALSE;
}
/***********************************************************
* Hooks implementations
***********************************************************/
/**
* Implements of hook_cron().
*/
function drupal_sync_cron() {
$last_update_time = variable_get('drupal_sync_update_last_time', 0);
if ((time() - $last_update_time) > variable_get('drupal_sync_queue_update_frequency', 0)) {
variable_set('drupal_sync_update_last_time', time());
// start synct
drupal_sync_run_sync();
}
}
/**
* Implements hook_init().
*/
function drupal_sync_init() {
if (!drupal_sync_check_site_domain()) {
$message = t('Drupal sync settings is wrong. Check settings and site domain !link.', array('!link' => l(t("here"), 'admin/config/drupal_sync/settings')));
drupal_set_message($message, 'warning');
}
}
/**
* Implements hook_permission().
*/
function drupal_sync_permission() {
return array(
'administer drupal sync settings' => array(
'title' => t('Administer drupal sync settings'),
'description' => t('Administer drupal sync settings'),
),
'access drupal sync' => array(
'title' => t('Access to Drupal Sync'),
'description' => t('Access to Drupal Sync'),
),
);
}
/***********************************************************
* Module functions
***********************************************************/
/**
* Get entity type id
*
* @param type $entity_type
* @param type $entity_type_obj
*
* @return type
*/
function _drupal_sync_get_entity_type_id($entity_type, $entity_type_obj) {
$type_id = NULL;
switch ($entity_type) {
case 'node' :
$type_id = $entity_type_obj->type;
break;
case 'taxonomy_term':
$type_id = $entity_type_obj->vid;
break;
}
drupal_alter('drupal_sync_get_entity_type_id', $type_id, $entity_type, $entity_type_obj);
return isset($type_id) ? $type_id : NULL ;
}
/**
* Get entity type id
*
* @param type $entity_type
* @param array $entity
*
* @return type
*/
function _drupal_sync_get_entity_id($entity_type, $entity) {
$type_id = NULL;
switch ($entity_type) {
case 'node' :
$type_id = $entity['nid'];
break;
case 'taxonomy_term':
$type_id = $entity['tid'];
break;
}
drupal_alter('drupal_sync_get_entity_id', $type_id, $entity_type, $entity);
return isset($type_id) ? $type_id : NULL ;
}
/**
* Make 'sha256' hash based on site info, and curent time
*
* @return string
*/
function drupal_sync_make_site_id_hash() {
$data = $_SERVER['HTTP_HOST'];
$data_hash = hash('sha256', $data);
return $data_hash;
}