-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathah_dirt.module
212 lines (200 loc) · 5.76 KB
/
ah_dirt.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
<?php
/**
* @file
* Module file for ah_dirt.
*/
/**
* Implements hook_menu().
*/
function ah_dirt_menu() {
return array(
'admin/config/services/dirt' => array(
'title' => 'DiRT Tools Config',
'description' => 'Configure RESTful DiRT retieval.',
'page callback' => 'drupal_get_form',
'access arguments' => array('administer site configuration'),
'page arguments' => array('ah_dirt_admin'),
'file' => 'includes/ah_dirt_admin.form.inc',
'type' => MENU_NORMAL_ITEM,
),
);
}
/**
* Implements hook_services_resources().
*/
function ah_dirt_services_resources() {
$api = array(
'item' => array(
'operations' => array(
'retrieve' => array(
'help' => 'Retrieves posted items',
'callback' => '_ah_dirt_item_retrieve',
'access callback' => 'user_access',
'access arguments' => array('access content'),
'access arguments append' => FALSE,
'args' => array(
array(
'name' => 'fn',
'type' => 'string',
'description' => 'Function to perform',
'source' => array('path' => '0'),
'optional' => TRUE,
'default' => '0',
),
array(
'name' => 'nitems',
'type' => 'int',
'description' => 'Number of latest items to get',
'source' => array('param' => 'nitems'),
'optional' => TRUE,
'default' => '0',
),
array(
'name' => 'since',
'type' => 'int',
'description' => 'Posts from the last number of days',
'source' => array('param' => 'since'),
'optional' => TRUE,
'default' => '0',
),
),
),
),
'actions' => array(
'fetch' => array(
'help' => 'Retrieves posted items',
'callback' => '_ah_dirt_item_retrieve',
'access callback' => 'user_access',
'access arguments' => array('access content'),
'access arguments append' => FALSE,
'args' => array(
array(
'name' => 'fn',
'type' => 'string',
'description' => 'Function to perform',
'source' => array('path' => '0'),
'optional' => TRUE,
'default' => '0',
),
array(
'name' => 'nitems',
'type' => 'int',
'description' => 'Number of latest items to get',
'source' => array('param' => 'nitems'),
'optional' => TRUE,
'default' => '0',
),
array(
'name' => 'since',
'type' => 'int',
'description' => 'Posts from the last number of days',
'source' => array('param' => 'since'),
'optional' => TRUE,
'default' => '0',
),
),
),
),
),
);
return $api;
}
/**
* Callback function for item retrieval.
*/
function _ah_dirt_item_retrieve($fn, $nitems, $timestamp) {
// Sanitize inputs.
$nitems = intval($nitems);
$timestamp = intval($timestamp);
return
ah_dirt_find_item_items($nitems, $timestamp);
}
/**
* Gets item posts.
*/
function ah_dirt_find_item_items($nitems, $timestamp) {
global $language;
$lang_name = $language->language;
$nitems = isset($_POST['nitems']) ? $_POST['nitems'] : NULL;
$serialized_tools = filter_xss($_POST['tools']);
$tools = unserialize($serialized_tools);
$query = db_select('node', 'n');
$query->join('node_revision', 'v', '(n.nid = v.nid) AND (n.vid = v.vid)');
$query->addField('n', 'nid', 'nid');
$query->condition('n.type', 'item', '=');
if ($tools) {
$query->condition('v.title', $tools, 'in');
}
if ($timestamp) {
$query->condition('v.timestamp', time() - ($timestamp * 60 * 60 * 24), '>');
}
$query->orderBy('v.timestamp', 'DESC');
// Limited by items?
if ($nitems) {
$query->range(0, $nitems);
}
$items = $query->execute()->fetchAll();
$objects = array();
foreach ($items as $item) {
$alias = drupal_get_path_alias("node/{$item->nid}");
$node = node_load($item->nid);
$description = '';
// Allow for either defined or undefined language type.
if (isset($node->body['und'][0]['value'])) {
$description = $node->body[$lang_name][0]['value'];
}
if (isset($node->body[$lang_name][0]['value'])) {
$description = $node->body[$lang_name][0]['value'];
}
$object = new stdClass();
$object->nid = $node->nid;
$object->title = $node->title;
$object->description = $description;
$object->path = $alias;
$objects[] = $object;
}
return $objects;
}
/**
* Implements hook_block_info().
*/
function ah_dirt_block_info() {
$blocks['dh_project_block'] = array(
'info' => t('DHCommons Projects'),
'status' => FALSE,
'region' => 'sidebar_first',
'visibility' => BLOCK_VISIBILITY_LISTED,
'pages' => 'node/*',
);
return $blocks;
}
/**
* Implements hook_block_view().
*/
function ah_dirt_block_view($delta = '') {
module_load_include('inc', 'ah_dirt', 'includes/utilities');
switch ($delta) {
case 'dh_project_block':
$block['subject'] = t('DHCommons');
$block['content'] = ah_dirt_get_projects();
break;
}
return $block;
}
/**
* Implements hook_theme().
*/
function ah_dirt_theme() {
return array(
'dirt_dhcommons_item' => array(
'file' => 'theme/theme.inc',
'template' => 'theme/dirt-dhcommons-item',
'variables' => array('object' => NULL),
),
'dirt_dhcommons_block' => array(
'file' => 'theme/theme.inc',
'template' => 'theme/dirt-dhcommons-block',
'variables' => array('object' => NULL),
),
);
}