-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogression.module
441 lines (390 loc) · 14.3 KB
/
progression.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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
<?php
// $Id$
define('PROGRESSION_ENUM_COUNT_UP', 'up');
define('PROGRESSION_ENUM_COUNT_DOWN', 'down');
/**
* Display help and module information.
*
* @param string $path which part of the site we're displaying help in
* @param array $arg an array containing the current path as returned from that arg function
* @return string help text contextualized by the path parameter
*/
function progression_help($path, $arg) {
switch ($path) {
case 'admin/help#progression':
return '<p>' . t('Facilitates the management and display of user-specified countups and countdowns.') . '</p>';
}
return '';
}
/**
* Defines the human-readable name of node types and other attributes.
*
* @return array an array of information concerning this module's node types
*/
function progression_node_info() {
return array(
'progression' => array(
'name' => t('Counter'),
'module' => 'progression',
'description' => t('A user-created counter object that counts down to or up from a specified target date.')
)
);
}
/**
* Defines a set of valid permissions for this module.
*
* @return array an array of (nonduplicated) valid permissions for this module
*/
function progression_perm() {
return array('create counter', 'edit own counter');
}
/**
* Limits access to specific node types based on user privileges.
*
* @param string $op one of ('create', 'delete', 'update', 'view')
* @param object $node the node on which the operation is to be performed
* @param array $account represents the user for whom the operation is to be performed
* @return boolean TRUE if the operation is allowed based on the node type and account privileges, FALSE if the operation is to be denied,
* and null to not override the settings in the node_access table or access control modules
*/
function progression_access($op, $node, $account) {
if ($op == 'create') {
return user_access('create counter', $account);
}
if (($op == 'update') || ($op == 'delete')) {
if (user_access('edit own counter', $account) && ($account->uid == $node->uid)) {
return TRUE;
}
}
}
/**
* Enables module to register paths, which determines how incoming requests are to be handled. Depending on the type of
* registration requested by each path, a link is placed in the navigation block and / or an item appears in the
* menu administration page.
*
* @return array an array of menu items. each menu item has a key corresponding to the path being registered
*/
function progression_menu() {
$items['counter'] = array(
'title' => 'Progression',
'access callback' => TRUE,
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
'page callback' => 'progression_counter_json'
);
$items['admin/settings/progression'] = array(
'title' => 'Progression',
'description' => 'Configure settings related to Progression counters.',
'page callback' => 'drupal_get_form',
'page arguments' => array('progression_admin_settings'),
'access arguments' => array('administer progression'),
'type' => MENU_NORMAL_ITEM,
'file' => 'progression.admin.inc'
);
return $items;
}
/**
* Generates a JSON representation of the given node.
*/
function progression_counter_json() {
// if: the counter url suffix has more than one path segment
// then: we're dealing with a malformed url. throw a 404 and return
if (func_num_args() > 1) {
drupal_not_found();
return;
}
// if: there were no suffix path segments
if (!func_num_args()) {
progression_output_json_response(progression_fetch());
}
// else: there's exactly 1 path segment
else {
$param = trim(func_get_arg(0));
if (is_numeric($param)) {
progression_output_json_response($param);
}
else if (in_array($param, array(PROGRESSION_ENUM_COUNT_UP, PROGRESSION_ENUM_COUNT_DOWN))) {
progression_output_json_response(progression_fetch($param));
}
else {
drupal_not_found();
}
}
}
/**
* Accepts either a node ID or array of node IDs and prints a JSON representation.
*
* @param array/number $param either a node ID or array of node IDs
*/
function progression_output_json_response($param) {
$data = array();
$nids = array();
if (is_array($param)) {
$nids = $param;
}
else if (is_numeric($param)) {
array_push($nids, $param);
}
foreach ($nids as $nid) {
$jsonNode = progression_build_json_node($nid);
if ($jsonNode) {
array_push($data, $jsonNode);
}
}
// if: a callback GET parameter was sent
// then: output a wrapper around the JSON data to be compatible with JSONP (used for cross-domain AJAX)
if (isset($_GET['callback']) && strlen($_GET['callback'])) {
drupal_set_header('Content-Type: text/javascript; charset=utf-8');
print $_GET['callback'] . '(' . drupal_to_js($data) . ')';
return;
}
drupal_json($data);
}
/**
* Accepts a single node ID and returns an associative array representing the applicable fields to be included in
* the JSON output
*
* @return array applicable node fields to be included in the JSON output
*/
function progression_build_json_node($nodeID) {
if (!is_numeric($nodeID)) return FALSE;
$node = node_load(
array('type' => 'progression', 'nid' => $nodeID)
);
if (!$node || !intval($node->status)) return FALSE;
return array(
'title' => isset($node->title) ? $node->title : '',
'counterType' => isset($node->counterType) ? $node->counterType : '',
'targetDate' => isset($node->targetDate)
? strtotime($node->targetDate['year'] . '/' . $node->targetDate['month'] . '/' . $node->targetDate['day'])
: '',
'description' => isset($node->description) ? nl2br(htmlentities($node->description)) : ''
);
}
/**
* Fetches progression counter objects.
*
* @return array an array of progression counter node IDs
*/
function progression_fetch() {
$queryArgs = array();
$andFilters = array();
$joinTables = array();
if (isset($_GET['user']) && strlen($_GET['user'])) {
array_push($andFilters, "u.name = '%s'");
array_push($queryArgs, $_GET['user']);
array_push($joinTables, 'users');
}
else if (isset($_GET['uid']) && is_numeric($_GET['uid']) && ($_GET['uid'] > 0)) {
array_push($andFilters, 'u.uid = %d');
array_push($queryArgs, $_GET['uid']);
array_push($joinTables, 'users');
}
$joins = array();
if (in_array('users', $joinTables)) {
array_push($joins, 'INNER JOIN {users} u ON (u.uid = pc.uid)');
}
if (func_num_args()) {
$param = trim(func_get_arg(0));
if (in_array($param, array(PROGRESSION_ENUM_COUNT_UP, PROGRESSION_ENUM_COUNT_DOWN))) {
array_push($andFilters, "pc.counterType = '%s'");
array_push($queryArgs, $param);
}
}
$query = 'SELECT nid FROM {progression_counter} pc ' . join(' ', $joins)
. (count($andFilters) ? ' WHERE ' . join(' AND ', $andFilters) : '');
$result = count($queryArgs) ? call_user_func_array('db_query', array_merge(array($query), $queryArgs))
: db_query($query);
$nids = array();
while ($counter = db_fetch_array($result)) {
$nids[] = $counter['nid'];
}
return $nids;
}
/**
* Generate block content.
*
* @param string $op one of ('list', 'configure', 'view', 'save')
* @param integer $delta block identifier
* @param array $edit used in conjunection with the 'save' $op parameter
* @return string generated block content
*/
function progression_block($op = 'list', $delta = 0, $edit = array()) {
// TODO: need to address block out put at some point...
switch ($op) {
// generate a list of all module blocks for the admin / block page
case 'list':
$block[0]['info'] = t('Countdowns');
$block[1]['info'] = t('Countups');
return $block;
case 'configure':
break;
case 'view':
break;
case 'save':
break;
}
}
/**
* Displays a node editing form.
*
* @param object $node the node being added or edited
* @param array $form_state the form state array
* @return array an array containing the form elements to be displayed in the node edit form
*/
function progression_form(&$node, $form_state) {
$type = node_get_types('type', $node);
if ($type->has_title) {
$form['title'] = array(
'#type' => 'textfield',
'#title' => t('Title'),
'#default_value' => isset($node->title) ? $node->title : '',
'#description' => t('Human-readable description of the counter.'),
'#required' => TRUE,
);
}
$form['counterType'] = array(
'#type' => 'select',
'#title' => t('Counter Type'),
'#default_value' => isset($node->counterType) ? $node->counterType : PROGRESSION_ENUM_COUNT_UP,
'#options' => drupal_map_assoc(array(PROGRESSION_ENUM_COUNT_UP, PROGRESSION_ENUM_COUNT_DOWN)),
'#description' => t('The type of counter to use (e.g. countup, countdown).'),
'#required' => TRUE
);
$form['targetDate'] = array(
'#type' => 'date',
'#title' => t('Target Date'),
'#default_value' => isset($node->targetDate) ? $node->targetDate : $edit['targetDate'],
'#description' => t("The date associated with this counter. For countups, this is the start date; for countdowns, this is the date we're counting to."),
'#required' => TRUE
);
$form['description'] = array(
'#type' => 'textarea',
'#title' => t('Description'),
'#default_value' => isset($node->description) ? $node->description : '',
'#description' => t('A detailed account of what this counter is supposed to represent.'),
'#rows' => 5,
'#required' => FALSE
);
return $form;
}
function progression_process_node_for_db($node) {
return array(
'nid' => $node->nid,
'uid' => $node->uid,
'counterType' => $node->counterType,
'targetDate' => $node->targetDate['year'] . '-'
. (($node->targetDate['month'] < 10) ? '0' . $node->targetDate['month'] : $node->targetDate['month']) . '-'
. (($node->targetDate['day'] < 10) ? '0' . $node->targetDate['day'] : $node->targetDate['day']) . ' '
. '00:00:00',
'description' => $node->description
);
}
function progression_process_db_for_node($dbNode) {
// input: YYYY-MM-DD hh:mm:ss, output: array('YYYY', 'MM', 'DD')
$dateChunks = explode(' ', $dbNode->targetDate);
$dateChunks = explode('-', $dateChunks[0]);
$dbNode->targetDate = array(
'year' => intval($dateChunks[0]), // YYYY
'month' => intval($dateChunks[1]), // MM
'day' => intval($dateChunks[2]) // DD
);
return $dbNode;
}
/**
* Insert hook for progression counter objects.
*
* @param object $node the node logically attached to the progression counter being inserted
*/
function progression_insert($node) {
dd($node);
$data = progression_process_node_for_db($node);
db_query(
"INSERT INTO {progression_counter} (nid, uid, counterType, targetDate, description)"
. "VALUES (%d, %d, '%s', '%s', '%s')",
$data['nid'], $data['uid'], $data['counterType'], $data['targetDate'], $data['description']
);
}
/**
* Update hook for progression counter objects.
*
* @param object $node the node logically attached to the progression counter being updated
*/
function progression_update($node) {
$data = progression_process_node_for_db($node);
db_query(
"UPDATE {progression_counter} SET counterType = '%s', targetDate = '%s', description = '%s' WHERE nid = %d",
$data['counterType'], $data['targetDate'], $data['description'], $data['nid']
);
}
/**
* Delete hook for progression counter objects.
*
* @param object $node the node logically attached to the progression counter being deleted
*/
function progression_delete($node) {
db_query(
'DELETE FROM {progression_counter} WHERE nid = %d', $node->nid
);
}
/**
* Load hook for progression counter objects.
*
* @param object $node the node logically attached to the progression counter being updated
* @return object loaded object corresponding to the node parameter
*/
function progression_load($node) {
$dbNode = db_fetch_object(
db_query(
'SELECT * from {progression_counter} WHERE nid = %d', $node->nid
)
);
return progression_process_db_for_node($dbNode);
}
/**
* Registers a module theme implementation(s).
*
* @param array $existing an array of existing implementations that may be used for override purposes
* @param string $type theme type being processed. one of ('module', 'base_theme_engine', 'theme_engine', 'base_theme', 'theme')
* @param string $theme the actual name of the theme being checked
* @param string $path the directory path of the theme or module
* @return array a keyed array of theme hooks
*/
function progression_theme($existing, $type, $theme, $path) {
return array(
'progression_info' => array(
'template' => 'progression_info'
)
);
}
/**
* Generates a display of the given node.
*
* @param object $node the node to display
* @param boolean $teaser flag indicating whether to show the teaser only or the full form
* @param boolean $page flag indicating whether the node is being displayed by itself as a page
* @return string html representation of the themed node
*/
function progression_view($node, $teaser = FALSE, $page = FALSE) {
$node = node_prepare($node, $teaser);
$node->content['progression_info'] = array(
'#value' => theme(
'progression_info',
array(
'title' => $node->title,
'counterType' => $node->counterType,
'targetDate' => $node->targetDate,
'description' => $node->description
)
)
);
return $node;
}
/**
* Indicate that this module wraps Views 2 in a warm, loving embrace.
*/
function progression_views_api() {
return array(
'api' => 2
);
}