-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcyoa.module
147 lines (133 loc) · 4.08 KB
/
cyoa.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
<?php
/*
* Because I hate globals // FIXME
*/
function current_cyoacid($in_cyoac = NULL){
static $cyoac = NULL;
if ($in_cyoac) $cyoac = $in_cyoac;
return $cyoac;
}
/*
* Implementation of hook_views_api
*/
function cyoa_views_api() {
return array(
'api' => 2,
'path' => drupal_get_path('module', 'cyoa'),
);
}
/**
* Implementation of hook_init().
*/
function cyoa_init() {
global $user;
if ($_GET['cyoa_start'] && drupal_get_token($_GET['cyoa_nid'] . ' - ' . $_GET['cyoac_nid']) == $_GET['token']) {
//start a new enterally
$vals =
array(
'uid'=>$user->uid,
'nid'=>$_GET['cyoa_nid'],
'hostname' => ip_address(),
'timestamp' => time(),
);
$inserted = drupal_write_record('cyoa',$vals);
if ($inserted) {
$vals = array(
'nid'=>$_GET['cyoac_nid'],
'cyoaid' => $vals['cyoaid'],
'timestamp' => time(),
);
$inserted = drupal_write_record('cyoac',$vals);
$vals = (object )$vals;
current_cyoacid($vals); // set for choice
}
} elseif ($_GET['cyoacid'] && $_GET['token']
&& drupal_get_token($_GET['cyoacid'] . ' - ' . $_GET['cyoac_nid']) == $_GET['token'])
{
if (db_result(db_query("SELECT 1 from {cyoac} where pcyoacid = %d",
$_GET['cyoacid']))) {
drupal_set_message("You have already followed this path");
} else {
$info = db_fetch_object(db_query("SELECT * from {cyoac} where cyoacid=%d",$_GET['cyoacid']));
$vals = array(
'nid'=>$_GET['cyoac_nid'],
'cyoaid' => $info->cyoaid,
'timestamp' => time(),
'pcyoacid' => $_GET['cyoacid'],
);
drupal_write_record('cyoac',$vals);
$vals = (object )$vals;
current_cyoacid($vals);
}
}
}
/**
* Implementation of hook_field_formatter_info().
*/
function cyoa_field_formatter_info() {
return array(
'cyoas' => array(
'label' => t('Start [cyoa]'),
'field types' => array('nodereference'),
'multiple values' => CONTENT_HANDLE_CORE,
),
'cyoac' => array(
'label' => t('Continue [cyoa]'),
'field types' => array('nodereference'),
'multiple values' => CONTENT_HANDLE_CORE,
),
);
}
/**
* Theme function for starting nodereference field formatter; this is how someone starts their adventure.
*/
function theme_cyoa_formatter_cyoas($element) {
$output = '';
static $choices;
if (!empty($element['#item']['nid']) && is_numeric($element['#item']['nid']) && ($title = _nodereference_titles($element['#item']['nid']))) {
$node = $element['#node'];
$query = '';
if ($node->nid) {
$token = drupal_get_token($node->nid . ' - ' . $element['#item']['nid']);
$query = array('cyoa_start'=>1, 'cyoa_nid'=>$node->nid, 'cyoac_nid'=> $element['#item']['nid'],'token'=>$token);
}
$output = l($title, 'node/'. $element['#item']['nid'],array('query'=>$query, 'attributes'=> array('rel' =>'nofollow')));
}
return $output;
}
/*
* Formatter for 'continueing' the adventure
*/
function theme_cyoa_formatter_cyoac($element) {
$output = '';
if (!empty($element['#item']['nid']) && is_numeric($element['#item']['nid']) && ($title = _nodereference_titles($element['#item']['nid']))) {
$node = $element['#node'];
$query = '';
$cyoac= current_cyoacid();
if ($cyoac) {
$cyoacid = $cyoac->cyoacid;
if (!$choices[$cyoacid]) {
$choices[$cyoacid] = db_fetch_object(db_query("SELECT * from {cyoac} where cyoacid=%d",$cyoacid));
}
if ( $node->nid == $choices[$cyoacid]->nid) {
$token = drupal_get_token($cyoacid . ' - ' . $element['#item']['nid']);
$query = array('cyoacid'=> $cyoacid, 'cyoac_nid'=> $element['#item']['nid'],'token'=>$token);
}
}
$output = l($title, 'node/'. $element['#item']['nid'],array('query'=>$query,'attributes'=>array('rel'=>'nofollow')));
}
return $output;
}
/**
* Implementation of hook_theme().
*/
function cyoa_theme() {
return array(
'cyoa_formatter_cyoac' => array(
'arguments' => array('element'),
),
'cyoa_formatter_cyoas' => array(
'arguments' => array('element'),
),
);
}