-
Notifications
You must be signed in to change notification settings - Fork 2
/
ckan.module
243 lines (198 loc) · 6.27 KB
/
ckan.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
<?php
/**
* @file
* A Module to Integrate CKAN and Drupal.
*
* This works by
*
* 1. providing a serach and listing interface to CKAN with links to this Drupal site
* 2. using a wildcard menu handler to match these links in the first instance
* 3. this loads the packages and creates matching nodes (storing the ckan name)
* 4. the node has a url alias which overrides the wildcard match
*
* Now we have Drupal nodes which can have user commments associated with them, fivestar votes etc...
*
* @author Sean Burlington sean@practicalweb.co.uk
* @copyright 2010 PracticalWeb Ltd (UK company number 06427950)
* @license http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/gpl-2.0.html>.
*
*
*
*/
function ckan_menu() {
$items = array();
// link to the search form page
$items['ckan_search'] = array(
'title' => t('Search CKAN'),
'page callback' => 'drupal_get_form',
'page arguments' => array('ckan_search_form'),
'access callback' => TRUE,
);
$items['ckan/search/%'] = array(
'page callback' => 'ckan_search_results_page',
'page arguments' => array(2),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['ckan/data/%'] = array(
'page callback' => 'ckan_new',
'page arguments' => array(2),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Gets the ckan Object
* Initialises it in one central place reusing it if needed.
*
* @return Ckan $ckan
*/
function ckan_ckan() {
static $ckan = NULL;
if (!$ckan) {
require_once(dirname(__FILE__) . '/ckan.php');
$ckan = new Ckan(variable_get('ckan_site', 'http://www.ckan.net/'));
}
return $ckan;
}
/**
* Implements hook_form
*
* @param Array $form_state
* @return Array $form
*/
function ckan_search_form(&$form_state) {
$form = array();
$form['q'] = array(
'#type' => 'textfield',
'#default_value' => '',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Search CKAN'),
);
return $form;
}
function ckan_search_form_submit($form, &$form_state) {
// The search form relies on control of the redirect destination for its
// functionality, so we override any static destination set in the request,
// for example by drupal_access_denied() or drupal_not_found()
// (see http://drupal.org/node/292565).
if (isset($_REQUEST['destination'])) {
unset($_REQUEST['destination']);
}
if (isset($_REQUEST['edit']['destination'])) {
unset($_REQUEST['edit']['destination']);
}
$form_state['redirect'] = 'ckan/search/'. trim($form_state['values']['q']);
}
function ckan_search_results_page($search) {
// return $search;
try {
$ckan = ckan_ckan();
$results = $ckan->search(check_plain($search));
} catch(Exception $e) {
return $e->getMessage();
}
return theme('ckan_search_results', $results);
}
/**
* Create the node the first time this package is seen.
*
* This creates a specific URL which override the wildcard one subsequently
*
* @param String $ckan_name
*/
function ckan_new($ckan_name) {
$ckan = ckan_ckan();
try {
$ckan_data = $ckan->getPackage($ckan_name);
} catch(CkanException $e) {
drupal_set_message($e->getMessage(), 'error');
drupal_set_title("Error");
return "Error" ;
}
$node = ckan_create_node($ckan_data);
drupal_goto('node/'. $node->nid);
}
function ckan_create_node($ckan_data) {
$node = array(
'title' => $ckan_data->title,
'uid' => 1,
'body' => $ckan_data->name,
'promote' => 1,
'path' => 'ckan/data/' . $ckan_data->name,
'type' => 'ckan',
'comment' => 2,
);
if ($node = node_submit($node)) {
node_save($node);
} else {
drupal_set_message("Failed to create node for package.");
}
return $node;
}
function ckan_node_info() {
return array(
'ckan' => array(
'name' => t('CKAN Package'),
'module' => 'ckan',
'description' => t('A package of Open Data.'),
'has_title' => TRUE,
'title_label' => t('Title'),
'has_body' => TRUE,
'body_label' => t('Package Description'),
'min_word_count' => 0,
'locked' => TRUE
)
);
}
function ckan_load($node) {
$ckan = ckan_ckan();
try {
$node->ckan = $ckan->getPackage($node->body);
} catch(Exception $e) {
drupal_set_message($e->getMessage(), 'error');
}
// print_r($node->ckan);
return $node;
}
function ckan_view($node, $teaser = FALSE, $page = FALSE) {
$node = node_prepare($node, $teaser);
$node->content['title']['#value'] = check_plain($node->ckan->title);
$node->content['body']['#value'] = nl2br(check_plain($node->ckan->notes));
return $node;
}
function ckan_theme() {
return array(
'ckan_search_results' => array('arguments' => array('results')),
);
}
function theme_ckan_search_results($results) {
$content = '';
$content .= "<p>". $results->count .t(' Results found'). "</p>";
foreach ($results->results as $package) {
$content .= '<h2>' . l($package->title, 'ckan/data/' . urlencode(check_plain($package->name))) . '</h2>';
$content .= '<p>' . nl2br(check_plain($package->notes)) . '</p>';
$content .= '<p>';
foreach ($package->groups as $i => $group) {
$content .= '<b>' . check_plain($group) . '</b> ';
}
$content .= '</p>';
}
return $content;
}