/
notebook.module
212 lines (173 loc) · 5.58 KB
/
notebook.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
//------------------------------------------------------------------------------
// Drupal hooks
//------------------------------------------------------------------------------
/**
* Implementation of hook_perm().
*/
function notebook_perm() {
return array(
'create note content',
'delete own note content',
'delete any note content',
'edit own note content',
'edit any note content',
);
}
//------------------------------------------------------------------------------
// Node hooks
//------------------------------------------------------------------------------
/**
* Implementation of hook_node_info().
*/
function notebook_node_info() {
return array(
'note' => array(
'name' => t('Note'),
'module' => 'notebook',
'description' => t("A note is a message/reminder easily posted at any page of the site."),
'has_title' => FALSE,
'has_body' => TRUE,
'body_label' => t('Message'),
)
);
}
//------------------------------------------------------------------------------
/**
* Implementation of hook_access().
*/
function notebook_access($op, $node, $account) {
if ($op == 'create') {
return user_access('create note content', $account);
}
if ($op == 'update') {
if (user_access('edit any note content', $account) || (user_access('edit own note content', $account) && ($account->uid == $node->uid))) {
return TRUE;
}
}
if ($op == 'delete') {
if (user_access('delete any note content', $account) || (user_access('delete own note content', $account) && ($account->uid == $node->uid))) {
return TRUE;
}
}
}
//------------------------------------------------------------------------------
/**
* Implementation of hook_form().
*/
function notebook_form(&$node) {
// The site admin can decide if this node type has a title and body, and how
// the fields should be labeled. We need to load these settings so we can
// build the node form correctly.
$type = node_get_types('type', $node);
if ($type->has_title) {
$form['title'] = array(
'#type' => 'textfield',
'#title' => check_plain($type->title_label),
'#required' => TRUE,
'#default_value' => $node->title,
'#weight' => -5,
);
}
if ($type->has_body) {
// In Drupal 6, we can use node_body_field() to get the body and filter
// elements. This replaces the old textarea + filter_form() method of
// setting this up. It will also ensure the teaser splitter gets set up
// properly.
$form['body_field'] = node_body_field($node, $type->body_label, $type->min_word_count);
}
// A link is stored whenever only when we submit a new note.
$form['link'] = array(
'#type' => 'value',
'#value' => (isset($node->link) ? $node->link : $_GET['q']),
);
return $form;
}
//------------------------------------------------------------------------------
/**
* Implementation of hook_load().
*
* Now that we've defined how to manage the node data in the database, we
* need to tell Drupal how to get the node back out. This hook is called
* every time a node is loaded, and allows us to do some loading of our own.
*/
function notebook_load($node) {
$additions = db_fetch_object(db_query('SELECT link FROM {note} WHERE vid = %d', $node->vid));
return $additions;
}
//------------------------------------------------------------------------------
/**
* Implementation of hook_insert().
*/
function notebook_insert($node) {
db_query("INSERT INTO {note} (vid, nid, link) VALUES (%d, %d, '%s')", $node->vid, $node->nid, $node->link);
}
//------------------------------------------------------------------------------
/**
* Implementation of hook_update().
*/
function notebook_update($node) {
// if this is a new node or we're adding a new revision,
if ($node->revision) {
notebook_insert($node);
}
else {
db_query("UPDATE {note} SET link = '%s' WHERE vid = %d", $node->link, $node->vid);
}
}
//------------------------------------------------------------------------------
/**
* Implementation of hook_delete().
*/
function notebook_delete($node) {
db_query('DELETE FROM {note} WHERE nid = %d', $node->nid);
}
//------------------------------------------------------------------------------
/**
* Implementation of hook_nodeapi().
*/
function notebook_nodeapi(&$node, $op, $teaser, $page) {
switch ($op) {
case 'delete revision':
db_query('DELETE FROM {note} WHERE vid = %d', $node->vid);
break;
}
}
//------------------------------------------------------------------------------
/**
* Implementation of hook_view().
*/
function notebook_view($node, $teaser = FALSE, $page = FALSE) {
$node = node_prepare($node, $teaser);
$node->content['link'] = array(
'#value' => theme('note_link', $node),
'#weight' => 1,
);
return $node;
}
//------------------------------------------------------------------------------
/**
* Implementation of hook_theme().
*
* This lets us tell Drupal about our theme functions and their arguments.
*/
function notebook_theme() {
return array(
'note_link' => array(
'arguments' => array('node'),
),
);
}
//------------------------------------------------------------------------------
// Theme functions
//------------------------------------------------------------------------------
/**
* Render note link to page posted on.
*/
function theme_node_example_order_info($node) {
$output = '<div class="note-link">';
$output .= l(t('Link to page posted on'), $node->link);
$output .= '</div>';
return $output;
}
//------------------------------------------------------------------------------