forked from hugowetterberg/cbisimport
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcbisnode.module
More file actions
279 lines (252 loc) · 9.37 KB
/
cbisnode.module
File metadata and controls
279 lines (252 loc) · 9.37 KB
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
<?php
/**
* Implementation of hook_theme().
*/
function cbisnode_theme() {
$theme = array(
'cbis_product' => array(
'template' => 'cbis_product',
'preprocess functions' => array('cbisimport_preprocess_cbis_product'),
'arguments' => array(
'product' => NULL,
),
),
'cbis_product_address' => array(
'template' => 'cbis_product_address',
'arguments' => array(
'attributes' => NULL,
),
),
);
return $theme;
}
/**
* Implementation of hook_nodeapi().
*
* @param stdClass &$node The node the action is being performed on.
* @param string $op What kind of action is being performed.
* @param bool|array $a3
* If $op is "view" True is passed if the node is viewed as a node
* If $op is "validate" the form that should be validated is passed as an array
* @param bool $page If $op is "view" True is passed if the node is viewed as a page
*
* @return void|stdClass
* If $op is "load" the function should return an array containing pairs of
* fields => values to be merged into the node object.
**/
function cbisnode_nodeapi(&$node, $op, $a3=Null, $page=Null) {
switch ($op) {
// The node is about to be loaded from the database. This hook can be used to load additional data at this time.
case 'load':
$data = array();
$res = db_query("SELECT p.*
FROM {cbisimport_product_node} AS pn
INNER JOIN {cbisimport_product} AS p ON (p.pid = pn.pid AND p.language = pn.language)
WHERE pn.nid = %d", array(
':nid' => $node->nid,
));
if (($product = db_fetch_array($res))) {
$product['data'] = unserialize($product['data']);
$data['cbis_product'] = $product;
}
return $data;
break;
}
}
/**
* Add template suggestions based on the CBIS template.
*
* @param array $vars
* @return array
*/
function cbisnode_preprocess_cbis_product($vars) {
if (is_array($vars['product']['TemplateParents'])) {
foreach ($vars['product']['TemplateParents'] as $t) {
$vars['template_files'][] = 'cbis_product_' . $t['id'];
}
}
$vars['template_files'][] = 'cbis_product_' . $vars['product']['TemplateId'];
return $vars;
}
/**
* Implementation of hook_cbisimport_saving_product().
*/
function cbisnode_cbisimport_saving_product($info) {
global $user;
$product = $info->product;
$template = CbisTemplate::getTemplate($product['TemplateId']);
$type = $template->isInstanceOf(81) ? 'event' : 'location';
$tags = array();
foreach (cbisimport_array($product['Categories']->Category) as $category) {
if (cbisimport_category_is_instance_of($category, 2457) || cbisimport_category_is_instance_of($category, 2395)) { // Event
$type = 'event';
}
$tags = array_merge($tags, cbisimport_category_tags($category->Id, $product['LanguageId']));
}
$attr = $product['Attributes'];
$node = array(
'type' => $type,
'title' => $product['Name'],
'created' => $product['PublishedDate'],
'teaser' => $attr['Introduction'],
'body' => theme('cbis_product', $product),
'uid' => $user->uid,
'language' => cbisimport_language_code($product['LanguageId']),
'taxonomy' => array(
'tags' => array(
'1' => join(', ', $tags),
)
),
);
if (module_exists('simple_geo')) {
if (!empty($attr['Latitude'])) {
$node['simple_geo_position'] = sprintf('%s %s', $attr['Latitude'], $attr['Longitude']);
}
}
$node['nid'] = cbisimport_mapping_exists($info);
switch ($type) {
case 'event':
$has_occasions = FALSE;
foreach ($info->product['Occasions'] as $occasion) {
$has_occasions = $has_occasions || !empty($occasion->Expanded);
}
if ($has_occasions) {
if (!$node['nid']) {
cbisnode_api_create_event($node, $info);
}
else {
cbisnode_api_update_event($node, $info);
}
}
break;
case 'location':
$node['nid'] = cbisimport_mapping_exists($info);
if (!$node['nid']) {
cbisnode_api_create_info($node, $info);
}
else {
cbisnode_api_update_info($node, $info);
}
break;
case 'post':
$node['nid'] = cbisimport_mapping_exists($info);
if (!$node['nid']) {
cbisnode_api_create_post($node, $info);
}
else {
cbisnode_api_update_post($node, $info);
}
break;
}
// TODO: Delete stuff that've been removed.
}
function cbisnode_api_create_post($node_data, $info) {
$node = (object)$node_data;
module_invoke_all('cbisnode_api_before_create_post', $node, $info, $occasion, $interval);
module_invoke_all('cbisnode_api_before_post_save', $node, $info, $occasion, $interval);
node_save($node);
module_invoke_all('cbisnode_api_after_create_post', $node, $info, $occasion, $interval);
module_invoke_all('cbisnode_api_post_saved', $node, $info, $occasion, $interval);
cbisimport_mapping_add($info->product['Id'], $info->product['LanguageId'], $node->nid, $occasion->Id, $interval[0]);
}
function cbisnode_api_update_post($node_data, $info) {
$current = get_object_vars(node_load($node_data['nid']));
$node = (object)array_merge($current, $node_data);
module_invoke_all('cbisnode_api_before_update_post', $node, $info, $occasion, $interval);
module_invoke_all('cbisnode_api_before_post_save', $node, $info, $occasion, $interval);
node_save($node);
module_invoke_all('cbisnode_api_after_update_post', $node, $info, $occasion, $interval);
module_invoke_all('cbisnode_api_post_saved', $node, $info, $occasion, $interval);
}
function cbisnode_api_create_event($node_data, $info) {
$node = (object)$node_data;
module_invoke_all('cbisnode_api_before_create_event', $node, $info, $occasion, $interval);
module_invoke_all('cbisnode_api_before_event_save', $node, $info, $occasion, $interval);
node_save($node);
module_invoke_all('cbisnode_api_after_create_event', $node, $info, $occasion, $interval);
module_invoke_all('cbisnode_api_event_saved', $node, $info, $occasion, $interval);
cbisimport_mapping_add($info->product['Id'], $info->product['LanguageId'], $node->nid, $occasion->Id, $interval[0]);
}
function cbisnode_api_update_event($node_data, $info) {
$current = get_object_vars(node_load($node_data['nid']));
$node = (object)array_merge($current, $node_data);
module_invoke_all('cbisnode_api_before_update_event', $node, $info, $occasion, $interval);
module_invoke_all('cbisnode_api_before_event_save', $node, $info, $occasion, $interval);
node_save($node);
module_invoke_all('cbisnode_api_after_update_event', $node, $info, $occasion, $interval);
module_invoke_all('cbisnode_api_event_saved', $node, $info, $occasion, $interval);
}
function cbisnode_api_create_info($node_data, $info) {
$node = (object)$node_data;
module_invoke_all('cbisnode_api_before_create_info', $node, $info);
module_invoke_all('cbisnode_api_before_info_save', $node, $info);
node_save($node);
module_invoke_all('cbisnode_api_after_create_info', $node, $info);
module_invoke_all('cbisnode_api_info_saved', $node, $info);
cbisimport_mapping_add($info->product['Id'], $info->product['LanguageId'], $node->nid, 0, 0);
}
function cbisnode_api_update_info($node_data, $info) {
$current = get_object_vars(node_load($node_data['nid']));
$node = (object)array_merge($current, $node_data);
module_invoke_all('cbisnode_api_before_create_info', $node, $info);
module_invoke_all('cbisnode_api_before_info_save', $node, $info);
node_save($node);
module_invoke_all('cbisnode_api_after_create_info', $node, $info);
module_invoke_all('cbisnode_api_info_saved', $node, $info);
}
/**
* Goes through a batch of archived products that have nodes that still
* are published and invokes cbisnode_cbisimport_unpublishing_product()
* for each of them.
*
* @param int $batch_size
* Optional. Defaults to 20.
* @return void
*/
function cbisnode_unpublish_nodes_for_archived_products($batch_size = 20) {
$res = db_query("SELECT DISTINCT p.pid FROM cbisimport_product AS p
INNER JOIN `cbisimport_product_node` AS pn ON pn.pid = p.pid
INNER JOIN node AS n ON pn.nid = n.nid
WHERE p.archived = 1
AND n.status = 1
LIMIT %d", array(
':limit' => $batch_size,
));
while ($p = db_fetch_object($res)) {
cbisnode_cbisimport_unpublishing_product($p->pid);
}
}
/**
* Implementation of hook_cbisimport_unpublishing_product().
*/
function cbisnode_cbisimport_unpublishing_product($product_id) {
// Unpublish all nodes mapped to the product.
$res = db_query("SELECT DISTINCT nid FROM {cbisimport_product_node} WHERE pid = %d", array(
':pid' => $product_id,
));
while ($map = db_fetch_object($res)) {
$node = node_load($map->nid);
$node->status = 0;
node_save($node);
}
}
/**
* Implementation of hook_cron().
*/
function cbisnode_cron() {
// // Check for cbis nodes that have been unpublished for more then one month
// $old_nodes = db_query_range("SELECT p.pid AS pid, p.nid AS nid FROM {cbisimport_product_node} AS p
// INNER JOIN {node} AS n
// ON p.nid = n.nid
// WHERE TIMESTAMPDIFF(MONTH, FROM_UNIXTIME(n.changed), NOW()) > 1
// AND n.status = 0
// ORDER BY n.changed ASC", 0, variable_get('cbisnode_old_clean_limit', 5)
// );
//
// // Remove old nodes and tell cbisimport to do some cleanup
// while ($old = db_fetch_object($old_nodes)) {
// module_invoke('cbisnode_node_delete', $old);
// node_delete($old->nid);
// }
// cbisnode_unpublish_nodes_for_archived_products(20);
}