/[drupal]/contributions/modules/node_badges/node_badges.module
ViewVC logotype

Contents of /contributions/modules/node_badges/node_badges.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (show annotations) (download) (as text)
Tue Jun 3 04:25:25 2008 UTC (17 months, 3 weeks ago) by cwgordon7
Branch: MAIN
CVS Tags: HEAD
File MIME type: text/x-php
Initial commit.
1 <?php
2 // $Id$
3
4 /**
5 * Implementation of hook_perm().
6 */
7 function node_badges_perm() {
8 return array('administer node badges');
9 }
10
11 /**
12 * Implementation of hook_menu().
13 */
14 function node_badges_menu($may_cache) {
15 $items = array();
16 if ($may_cache) {
17 $items[] = array(
18 'path' => 'admin/content/node_badges',
19 'title' => t('Node badges'),
20 'description' => t('Configure what badges nodes are allowed to have.'),
21 'callback' => 'node_badges_admin_overview',
22 'access' => user_access('administer node badges'),
23 );
24 $items[] = array(
25 'path' => 'admin/content/node_badges/overview',
26 'title' => t('Overview'),
27 'type' => MENU_DEFAULT_LOCAL_TASK,
28 );
29 $items[] = array(
30 'path' => 'admin/content/node_badges/add',
31 'callback' => 'drupal_get_form',
32 'callback arguments' => array('node_badges_edit_form'),
33 'title' => t('Add new badge'),
34 'access' => user_access('administer node badges'),
35 'type' => MENU_LOCAL_TASK,
36 'weight' => 1,
37 );
38 }
39 else {
40 if (arg(0) == 'admin' && arg(1) == 'content' && arg(2) == 'node_badges' && is_numeric(arg(3)) && arg(3) > 0) {
41 $node_badge = node_badges_load(arg(3));
42 if (!empty($node_badge)) {
43 $items[] = array(
44 'path' => 'admin/content/node_badges/'. arg(3) .'/edit',
45 'callback' => 'drupal_get_form',
46 'callback arguments' => array('node_badges_edit_form'),
47 'title' => t('Edit'),
48 'access' => user_access('administer node badges'),
49 'type' => MENU_CALLBACK,
50 );
51 $items[] = array(
52 'path' => 'admin/content/node_badges/'. arg(3) .'/delete',
53 'callback' => 'drupal_get_form',
54 'callback arguments' => array('node_badges_delete_form'),
55 'title' => t('Delete'),
56 'access' => user_access('administer node badges'),
57 'type' => MENU_CALLBACK,
58 );
59 }
60 }
61 }
62 return $items;
63 }
64
65 /**
66 * Loads the node badge from the database.
67 *
68 * @param $bid
69 * The badge id of the badge to load.
70 * @param $refresh
71 * If TRUE, refreshes the static cache. Defaults to FALSE.
72 */
73 function node_badges_load($bid, $refresh = FALSE) {
74 static $badges = array();
75 if ($refresh) {
76 $badges = array();
77 }
78 if (!is_numeric($bid) || $bid <= 0) {
79 return FALSE;
80 }
81 if (!isset($badges[$bid])) {
82 $badge = array();
83 $query = db_query('SELECT nbb.*, nbbr.*, nbbt.* FROM {node_badges_badges} nbb LEFT JOIN {node_badges_badges_roles} nbbr ON nbb.bid = nbbr.bid LEFT JOIN {node_badges_badges_types} nbbt ON nbb.bid = nbbt.bid WHERE nbb.bid = %d', $bid);
84 while ($result = db_fetch_array($query)) {
85 if (isset($result['rid'])) {
86 $badge['roles'][$result['rid']] = $result['rid'];
87 }
88 if (isset($result['type'])) {
89 $badge['types'][$result['type']] = $result['type'];
90 }
91 if (isset($result['bid'])) {
92 $badge['bid'] = $result['bid'];
93 }
94 if (isset($result['source'])) {
95 $badge['source'] = $result['source'];
96 }
97 if (isset($result['caption'])) {
98 $badge['caption'] = $result['caption'];
99 }
100 if (isset($result['description'])) {
101 $badge['description'] = $result['description'];
102 }
103 }
104 $badges[$bid] = $badge;
105 }
106 return $badges[$bid];
107 }
108
109 /**
110 * Saves a badge to the database.
111 *
112 * @param $badge
113 * The badge to save.
114 */
115 function node_badges_save($badge) {
116 $insert = ($badge['bid'] == 'new');
117 if ($insert) {
118 db_lock_table('node_badges_badges');
119 db_query("INSERT INTO {node_badges_badges} (source, caption, description) VALUES ('%s', '%s', '%s')", $badge['source'], $badge['caption'], $badge['description']);
120 $badge['bid'] = db_result(db_query('SELECT MAX(bid) FROM {node_badges_badges}'));
121 db_unlock_tables();
122 }
123 else {
124 db_query("UPDATE {node_badges_badges} SET source = '%s', caption = '%s', description = '%s' WHERE bid = %b", $badge['source'], $badge['caption'], $badge['description'], $badge['bid']);
125 }
126 foreach ($badge['roles'] as $rid => $value) {
127 $exists = db_result(db_query('SELECT * FROM {node_badges_badges_roles} WHERE bid = %d AND rid = %d', $badge['bid'], $rid));
128 if (!$exists && $value) {
129 // Insert
130 db_query('INSERT INTO {node_badges_badges_roles} (bid, rid) VALUES (%d, %d)', $badge['bid'], $rid);
131 }
132 if ($exists && !$value) {
133 // Delete
134 db_query('DELETE FROM {node_badges_badges_roles} WHERE bid = %d AND rid = %d', $badge['bid'], $rid);
135 }
136 }
137 foreach ($badge['types'] as $type => $value) {
138 $exists = db_result(db_query("SELECT * FROM {node_badges_badges_types} WHERE bid = %d AND type = '%s'", $badge['bid'], $type));
139 if (!$exists && $value) {
140 // Insert
141 db_query("INSERT INTO {node_badges_badges_types} (bid, type) VALUES (%d, '%s')", $badge['bid'], $type);
142 }
143 if ($exists && !$value) {
144 // Delete
145 db_query("DELETE FROM {node_badges_badges_types} WHERE bid = %d AND type = '%s'", $badge['bid'], $type);
146 }
147 }
148 drupal_goto('admin/content/node_badges');
149 }
150
151 /**
152 * Menu callback for the administration overview page.
153 */
154 function node_badges_admin_overview() {
155 $query = db_query('SELECT * FROM {node_badges_badges}');
156 $node_badges = array();
157 while ($result = db_fetch_array($query)) {
158 $node_badge = array();
159 $bid = $result['bid'];
160 $node_badge['image'] = theme('image', $result['source']);
161 $node_badge['caption'] = filter_xss_admin($result['caption']);
162 $node_badge['edit'] = l(t('Edit'), "admin/content/node_badges/$bid/edit");
163 $node_badge['delete'] = l(t('Delete'), "admin/content/node_badges/$bid/delete");
164 $node_badges[] = $node_badge;
165 }
166 return theme('table', array(t('Badge'), t('Caption'), t('Edit'), t('Delete')), $node_badges);
167 }
168
169 /**
170 * Menu callback for badge edit page.
171 */
172 function node_badges_edit_form() {
173 $badge = array();
174 if (arg(4) == 'edit') {
175 $badge = node_badges_load(arg(3));
176 }
177 $form = array();
178 $form['bid'] = array(
179 '#type' => 'value',
180 '#value' => isset($badge['bid']) ? $badge['bid'] : 'new',
181 );
182 $form['source'] = array(
183 '#type' => 'textfield',
184 '#title' => t('Image source'),
185 '#description' => t('The source of the image that will be used to generate the node badge.'),
186 '#required' => TRUE,
187 '#default_value' => isset($badge['source']) ? $badge['source'] : '',
188 );
189 $form['caption'] = array(
190 '#type' => 'textfield',
191 '#title' => t('Caption'),
192 '#description' => t('A caption to be displayed beside the node badge. Can be left blank to disable this feature.'),
193 '#default_value' => isset($badge['caption']) ? $badge['caption'] : '',
194 );
195 $form['description'] = array(
196 '#type' => 'textarea',
197 '#title' => t('Description'),
198 '#description' => t('The description of the node badge that will appear next to the checkbox on the node add/edit page.'),
199 '#rows' => 5,
200 '#default_value' => isset($badge['description']) ? $badge['description'] : '',
201 );
202 $roles = user_roles();
203 foreach ($roles as $rid => $name) {
204 $roles[$rid] = check_plain($name);
205 }
206 $form['roles'] = array(
207 '#type' => 'checkboxes',
208 '#title' => t('Roles'),
209 '#description' => t('The roles allowed to assign this badge to nodes. If a user is a member of any of these roles, the user will have access to assign this badge to nodes.'),
210 '#options' => $roles,
211 '#default_value' => isset($badge['roles']) ? $badge['roles'] : array(),
212 );
213 $types = node_get_types('names');
214 foreach ($types as $type => $name) {
215 $types[$type] = check_plain($name);
216 }
217 $form['types'] = array(
218 '#type' => 'checkboxes',
219 '#title' => t('Types'),
220 '#description' => t('The node types this badge can be assigned to.'),
221 '#options' => $types,
222 '#default_value' => isset($badge['types']) ? $badge['types'] : array(),
223 );
224 $form['submit'] = array(
225 '#type' => 'submit',
226 '#value' => empty($badge) ? t('Add') : t('Save'),
227 );
228 return $form;
229 }
230
231 /**
232 * Validation of the edit form.
233 */
234 function node_badges_edit_form_validate($form_id, $form_values) {
235 if (!is_file($form_values['source'])) {
236 form_set_error('source', t('Invalid image source file.'));
237 }
238 }
239
240 /**
241 * Submit the edit form.
242 */
243 function node_badges_edit_form_submit($form_id, $form_values) {
244 node_badges_save($form_values);
245 }
246
247 /**
248 * Implementation of hook_nodeapi().
249 */
250 function node_badges_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
251 switch ($op) {
252 case 'load':
253 $node->badges = node_badges_node_badges($node->nid);
254 break;
255
256 case 'insert':
257 case 'update':
258 if (!isset($node->badges)) {
259 $node->badges = array();
260 }
261 if (!isset($node->new_badges)) {
262 $node->new_badges = array();
263 }
264 foreach ($node->new_badges as $bid => $value) {
265 if (!$value) {
266 unset($node->new_badges[$bid]);
267 }
268 }
269 $node->new_badges = drupal_map_assoc(array_keys($node->new_badges));
270 $insert = array_diff($node->new_badges, array_keys($node->badges));
271 $delete = array_diff(array_keys($node->badges), $node->new_badges);
272 foreach ($insert as $bid) {
273 db_query('INSERT INTO {node_badges_nodes} (nid, bid) VALUES (%d, %d)', $node->nid, $bid);
274 }
275 foreach ($delete as $bid) {
276 db_query('DELETE FROM {node_badges_nodes} WHERE nid = %d AND bid = %d', $node->nid, $bid);
277 }
278 $node->badges = $node->new_badges;
279 foreach ($node->badges as $key => $badge) {
280 $node->badges[$key] = node_badges_load($key);
281 }
282 unset($node->new_badges);
283 break;
284
285 case 'view':
286 drupal_add_css(drupal_get_path('module', 'node_badges') .'/node_badges.css');
287 if (isset($node->new_badges)) {
288 foreach ($node->new_badges as $key => $badge) {
289 if ($badge) {
290 $node->new_badges[$key] = node_badges_load($key);
291 }
292 else {
293 unset($node->new_badges[$key]);
294 }
295 }
296 }
297 $node->content['node_badges'] = array(
298 '#value' => theme('node_badges', isset($node->badges) ? $node->badges : (isset($node->new_badges) ? $node->new_badges : array())),
299 '#weight' => -10,
300 );
301 break;
302
303 case 'delete':
304 db_query('DELETE FROM {node_badges_nodes} WHERE nid = %d', $node->nid);
305 break;
306 }
307 }
308
309 /**
310 * Fetch the badges for a given node.
311 *
312 * @param $nid
313 * The node id of the node whose badges should be loaded.
314 * @param $refresh
315 * If TRUE, refreshes the static cache. Defaults to FALSE.
316 */
317 function node_badges_node_badges($nid, $refresh = FALSE) {
318 static $badges = array();
319 if ($refresh) {
320 $badges = array();
321 }
322 if (!isset($badges[$nid])) {
323 $badges[$nid] = array();
324 $query = db_query('SELECT bid FROM {node_badges_nodes} WHERE nid = %d', $nid);
325 while ($badge = db_fetch_array($query)) {
326 $badges[$nid][$badge['bid']] = node_badges_load($badge['bid']);
327 }
328 }
329 return $badges[$nid];
330 }
331
332 /**
333 * Implementation of hook_form_alter().
334 */
335 function node_badges_form_alter($form_id, &$form) {
336 if (isset($form['type'])) {
337 $node = $form['#node'];
338 if ($form['type']['#value'] .'_node_form' == $form_id) {
339 global $user;
340 $badges = array();
341 $query = db_query('SELECT bid FROM {node_badges_badges_types} WHERE type = \'%s\'', $form['type']['#value']);
342 while ($badge = db_fetch_array($query)) {
343 $badges[$badge['bid']] = node_badges_load($badge['bid']);
344 }
345 $form['new_badges'] = array(
346 '#type' => 'fieldset',
347 '#tree' => TRUE,
348 '#collapsible' => TRUE,
349 '#collapsed' => TRUE,
350 '#title' => t('Badges'),
351 );
352 $badges_exist = FALSE;
353 foreach ($badges as $bid => $badge) {
354 $access = FALSE;
355 if (count(array_intersect(array_keys($user->roles), $badge['roles'])) || $user->uid == 1) {
356 $access = TRUE;
357 $badges_exist = TRUE;
358 }
359 $form['new_badges'][$bid] = array(
360 '#type' => 'checkbox',
361 '#title' => filter_xss_admin($badge['description']),
362 '#prefix' => '<div class="container-inline">'. theme('image', $badge['source']),
363 '#default_value' => isset($node->badges[$bid]),
364 '#access' => $access,
365 '#suffix' => '</div>',
366 );
367 }
368 if (!$badges_exist) {
369 unset($form['new_badges']);
370 }
371 else {
372 drupal_add_css(drupal_get_path('module', 'node_badges') .'/node_badges.css');
373 }
374 }
375 }
376 }
377
378 /**
379 * Theming callback: theme the badges for a node.
380 *
381 * @param $badges
382 * The badges to theme.
383 * @return
384 * The HTML content of the themed badges.
385 */
386 function theme_node_badges($badges) {
387 if (empty($badges)) {
388 return '';
389 }
390 $output = '<div class="node-badges">';
391 foreach ($badges as $badge) {
392 $output .= theme('node_badge', $badge);
393 }
394 $output .= '</div>';
395 return $output;
396 }
397
398 /**
399 * Theming callback: theme a single badge.
400 *
401 * @param $badge
402 * The badge to theme.
403 * @return
404 * The HTML content of the themed badge.
405 */
406 function theme_node_badge($badge) {
407 $output = '<div class="node-badge">';
408 $output .= theme('image', $badge['source']);
409 if (!empty($badge['caption'])) {
410 $output .= '<span class="badge-caption">';
411 $output .= filter_xss_admin($badge['caption']);
412 $output .= '</span>';
413 }
414 $output .= '</div>';
415 return $output;
416 }
417
418 /**
419 * Delete form.
420 */
421 function node_badges_delete_form() {
422 $badge = node_badges_load(arg(3));
423 $form = array();
424 $form['bid'] = array(
425 '#type' => 'value',
426 '#value' => $badge['bid'],
427 );
428 $form['delete'] = array(
429 '#type' => 'submit',
430 '#prefix' => '<p>'. t('This action cannot be undone.') .'</p>',
431 '#value' => t('Delete'),
432 '#suffix' => l(t('Cancel'), 'admin/content/node_badges'),
433 );
434 return $form;
435 }
436
437 function node_badges_delete_form_submit($form_id, $form_values) {
438 foreach (array('node_badges_badges', 'node_badges_badges_roles', 'node_badges_badges_types', 'node_badges_nodes') as $table) {
439 db_query('DELETE FROM {'. $table .'} WHERE bid = %d', $form_values['bid']);
440 }
441 drupal_goto('admin/content/node_badges');
442 }

  ViewVC Help
Powered by ViewVC 1.1.2