-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcbisnode.module
220 lines (196 loc) · 7.51 KB
/
cbisnode.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
<?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);
}