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

Contents of /contributions/modules/views_actions_links/views_actions_links.module

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


Revision 1.1 - (show annotations) (download) (as text)
Wed Jun 13 04:45:12 2007 UTC (2 years, 5 months ago) by morrissinger
Branch: MAIN
CVS Tags: DRUPAL-5--1-0, HEAD
Branch point for: DRUPAL-5
File MIME type: text/x-php
Initial commit of views_actions_links. The module integrations the Actions module into Views.
1 <?php
2 // $Id$
3
4 /**
5 * @file
6 * Integrates the Actions module into Views by providing a field for
7 * each action.
8 */
9
10 /*********************************************************************
11 * Hooks: Drupal
12 */
13
14 /**
15 * Implementation of hook_perm().
16 */
17 function views_actions_links_perm() {
18 $permissions = array();
19 $actions = actions_get_all_actions();
20
21 foreach($actions as $aid => $action) {
22 $permissions[] = 'execute ' . $action['description'] . ' from views';
23 }
24
25 return $permissions;
26 }
27
28 /**
29 * Implementation of hook_menu().
30 */
31 function views_actions_links_menu($may_cache) {
32 $items = array();
33 if(!$may_cache) {
34 if(arg(0) == 'views_actions_links' && arg(1) == 'action') {
35 $access = user_access(_views_actions_links_get_perm(arg(2)));
36 } else {
37 $access = FALSE;
38 }
39
40 $items[] = array(
41 'path' => 'views_actions_links/action',
42 'type' => MENU_CALLBACK,
43 'access' => $access,
44 'callback' => 'views_actions_links_do'
45 );
46 }
47 return $items;
48 }
49
50 /*********************************************************************
51 * Hooks: Views
52 */
53
54 /**
55 * Implementation of hook_views_tables().
56 */
57 function views_actions_links_views_tables() {
58 $actions = actions_get_all_actions();
59
60 $action_fields = array();
61 foreach($actions as $aid => $action) {
62 $action_fields['views_actions_links_' . $aid] = array(
63 'name' => t('!type: !name (action)', array('!type' => $action['type'], '!name' => $action['description'])),
64 'help' => t('Provide a link to trigger the !name action.', array('!name' => $action['description'])),
65 'notafield' => TRUE,
66 'handler' => 'views_actions_links_provide_link',
67 'action' => $action,
68 );
69 }
70
71 $tables['views_actions_links'] = array(
72 'name' => 'views_actions_links',
73 'join' => array(
74 'left' => array(
75 'table' => 'node',
76 'field' => 'nid',
77 ),
78 'right' => array(
79 'field' => 'nid',
80 ),
81 ),
82 'fields' => $action_fields,
83 );
84
85 return $tables;
86 }
87
88 /**
89 * Views field handler to provide the link to a menu callback that will
90 * trigger the action.
91 */
92 function views_actions_links_provide_link($fieldinfo, $fielddata, $value, $data) {
93 $nid = $data->nid;
94 $action = $fieldinfo['action'];
95
96 if(user_access(_views_actions_links_get_perm($action))) {
97 return l($action['description'], 'views_actions_links/action/' . $action['function'] . '/' . $nid, array(), drupal_get_destination());
98 }
99 }
100
101 /*********************************************************************
102 * Menu Callbacks
103 */
104
105 /**
106 * The actual menu callback that triggers the action for a given nid.
107 */
108 function views_actions_links_do($action, $nid) {
109 $result = actions_do($action, node_load($nid));
110 drupal_set_message(t('Action performed.'));
111 drupal_goto(substr(drupal_get_destination(), 12));
112 }
113
114 /*********************************************************************
115 * Helper functions
116 */
117
118 function _views_actions_links_get_perm($action) {
119 if(is_array($action)) {
120 return 'execute ' . $action['description'] . ' from views';
121 } else {
122 return 'execute ' . $action . ' from views';
123 }
124 }

  ViewVC Help
Powered by ViewVC 1.1.2