-
Notifications
You must be signed in to change notification settings - Fork 0
/
flippy.module
executable file
·211 lines (188 loc) · 6.61 KB
/
flippy.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
<?php
/**
* @file
* Allows administrators to add previous/next pagers to any node type.
*/
/**
* Implements hook_theme()
*/
function flippy_theme() {
return array(
'flippy' => array(
'arguments' => array(
'list' => array(),
),
'template' => 'flippy',
),
);
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function flippy_form_node_type_form_alter(&$form, $form_state) {
$form['flippy'] = array(
'#type' => 'fieldset',
'#title' => t('Flippy settings'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['flippy']['flippy'] = array(
'#type' => 'checkbox',
'#title' => t('Build a pager for this content type'),
'#default_value' => isset($form['flippy']['flippy']) ?
$form['flippy']['flippy'] :
variable_get('flippy_' . $form['#node_type']->type, FALSE),
);
$form['flippy']['flippy_label_type'] = array(
'#type' => 'select',
'#title' => t('Pager label type'),
'#options' => array(
0 => t('Custom text'),
1 => t('Node title'),
2 => t('Node ID'),
),
'#default_value' => isset($form['flippy']['flippy_label_type']) ?
$form['flippy']['flippy_label_type'] :
variable_get('flippy_label_type_' . $form['#node_type']->type, 0),
'#description' => t('Select label type to display'),
);
$form['flippy']['flippy_prev_label'] = array(
'#type' => 'textfield',
'#title' => t('Label for "Previous" link'),
'#size' => 32,
'#default_value' => isset($form['flippy']['flippy_prev_label']) ?
$form['flippy']['flippy_prev_label'] :
variable_get('flippy_prev_label_' . $form['#node_type']->type, 'Previous'),
);
$form['flippy']['flippy_next_label'] = array(
'#type' => 'textfield',
'#title' => t('Label for "Next" link'),
'#size' => 32,
'#default_value' => isset($form['flippy']['flippy_next_label']) ?
$form['flippy']['flippy_next_label'] :
variable_get('flippy_next_label_' . $form['#node_type']->type, 'Next'),
);
$form['flippy']['flippy_firstlast'] = array(
'#type' => 'checkbox',
'#title' => t('Show first/last links'),
'#default_value' => isset($form['flippy']['flippy_firstlast']) ?
$form['flippy']['flippy_firstlast'] :
variable_get('flippy_firstlast_' . $form['#node_type']->type, FALSE),
);
$form['flippy']['flippy_first_label'] = array(
'#type' => 'textfield',
'#title' => t('Label for "First" link'),
'#size' => 32,
'#default_value' => isset($form['flippy']['flippy_first_label']) ?
$form['flippy']['flippy_first_label'] :
variable_get('flippy_first_label_' . $form['#node_type']->type, 'First'),
);
$form['flippy']['flippy_last_label'] = array(
'#type' => 'textfield',
'#title' => t('Label for "Last" link'),
'#size' => 32,
'#default_value' => isset($form['flippy']['flippy_last_label']) ?
$form['flippy']['flippy_last_label'] :
variable_get('flippy_last_label_' . $form['#node_type']->type, 'Last'),
);
}
/**
* Implements hook_content_extra_fields.
*/
function flippy_content_extra_fields($type_name) {
$type = node_get_types('type', $type_name);
$extra = array();
if (variable_get('flippy_'. $type_name, NULL)) {
$extra['flippy_pager'] = array(
'label' => t('Pager'),
'description' => t('Flippy module content pager'),
'weight' => variable_get('flippy_wt_'. $type_name, 0),
);
}
return $extra;
}
/**
* Implements hook_nodeapi().
*/
function flippy_nodeapi(&$node, $op) {
if ($op === 'view' && $node->build_mode === NODE_BUILD_NORMAL) {
if (variable_get('flippy_'. $node->type, NULL)) {
$node->content['flippy_pager'] = array(
'#value' => theme('flippy', flippy_build_list($node)),
);
}
}
}
/**
* Function that builds the list of nodes
*/
function flippy_build_list($node) {
static $master_list;
if (!isset($master_list)) {
$master_list = array();
}
if (!isset($master_list[$node->nid])) {
$q = array();
$list = array();
// Create a starting-point query object
$sql = "SELECT nid, title FROM {node}
WHERE status = 1 AND type = '%s' AND nid != '%d'";
$q = array(
'first' => "\nAND created <= '%d' ORDER BY created ASC",
'prev' => "\nAND created <= '%d' ORDER BY created DESC",
'next' => "\nAND created >= '%d' ORDER BY created ASC",
'last' => "\nAND created >= '%d' ORDER BY created DESC",
);
foreach ($q as $key => $query) {
$query = $sql . $query . "\n LIMIT 1";
$list[$key] = db_fetch_array(db_query($query, $node->type, $node->nid,
$node->created));
}
$list['current'] = array(
'nid' => $node->nid,
'title' => $node->title,
);
$master_list[$node->nid] = $list;
}
return $master_list[$node->nid];
}
/**
* Implements template_preprocess_hook()
*
* @see flippy.tpl.php
*
* @ingroup themeable
*/
function template_preprocess_flippy(&$vars) {
$node = menu_get_object();
$nav = $vars['list'];
drupal_add_css(drupal_get_path('module', 'flippy') . '/flippy.css');
// for getting node type
if ($node) {
$vars['node'] = $node;
}
$label_type = variable_get('flippy_label_type_' . $vars['node']->type, NULL);
if ($nav) {
if (variable_get('flippy_firstlast_'. $vars['node']->type, NULL)) {
$vars['first_link'] = empty($nav['first']) ? '' : l('« ' . t(variable_get('flippy_first_label_' . $vars['node']->type, NULL)), 'node/' . $nav['first']['nid']);
$vars['last_link'] = empty($nav['last']) ? '' : l(t(variable_get('flippy_last_label_' . $vars['node']->type, NULL)) . ' »', 'node/' . $nav['last']['nid']);
}
switch ($label_type) {
case '0':
$vars['previous_link'] = empty($nav['prev']) ? '' : l('‹ ' . t(variable_get('flippy_prev_label_' . $vars['node']->type, NULL)), 'node/' . $nav['prev']['nid']);
$vars['next_link'] = empty($nav['next']) ? '' : l(t(variable_get('flippy_next_label_' . $vars['node']->type, NULL)) . ' ›', 'node/' . $nav['next']['nid']);
break;
case '1':
$vars['previous_link'] = empty($nav['prev']) ? '' : l('‹ ' . t($nav['prev']['title']), 'node/' . $nav['prev']['nid']);
$vars['next_link'] = empty($nav['next']) ? '' : l(t($nav['next']['title']) . ' ›', 'node/' . $nav['next']['nid']);
break;
case '2':
$vars['previous_link'] = empty($nav['prev']) ? '' : l('‹ ' . t($nav['prev']['nid']), 'node/' . $nav['prev']['nid']);
$vars['next_link'] = empty($nav['next']) ? '' : l(t($nav['next']['nid']) . ' ›', 'node/' . $nav['next']['nid']);
break;
}
}
$vars = array_merge($vars, $vars['list']);
unset($vars['list']);
unset($vars['node']);
}