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

Contents of /contributions/modules/publishcontent/publishcontent.module

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


Revision 1.8 - (show annotations) (download) (as text)
Tue Feb 24 01:07:35 2009 UTC (9 months ago) by malaussene
Branch: MAIN
CVS Tags: HEAD
Changes since 1.7: +3 -3 lines
File MIME type: text/x-php
Fixed: broken previous commit.
1 <?php
2 // $Id: publishcontent.module,v 1.7 2009/02/24 00:44:57 malaussene Exp $
3
4 /**
5 * @file
6 * Add button to publish or unpublish a node,
7 * with access control based on the node type
8 */
9
10 /**
11 * Implementation of hook_menu().
12 *
13 * @see publishcontent_install()
14 */
15 function publishcontent_menu($may_cache) {
16 $items = array();
17 if (!$may_cache && arg(0) == 'node' && is_numeric(arg(1))) {
18 $node = node_load(arg(1));
19 if ($node->nid) {
20 $items[] = array(
21 'path' => 'node/'. arg(1) .'/publish',
22 'title' => t('Publish'),
23 'callback' => 'publishcontent_toggle_status',
24 'callback arguments' => array($node),
25 'access' => !$node->status && _publishcontent_publish_access($node),
26 'weight' => 5,
27 'type' => MENU_LOCAL_TASK,
28 );
29 $items[] = array(
30 'path' => 'node/'. arg(1) .'/unpublish',
31 'title' => t('Unpublish'),
32 'callback' => 'publishcontent_toggle_status',
33 'callback arguments' => array($node),
34 'access' => $node->status && _publishcontent_unpublish_access($node),
35 'weight' => 5,
36 'type' => MENU_LOCAL_TASK,
37 );
38 // force the access to the node
39 if (!user_access('administer nodes')) {
40 $items[] = array(
41 'path' => 'node/'. arg(1),
42 'title' => t('View'),
43 'callback' => 'node_page_view',
44 'callback arguments' => array($node),
45 'access' => _publishcontent_view_access($node),
46 'type' => MENU_CALLBACK,
47 );
48 }
49 }
50 }
51 return $items;
52 }
53
54 function _publishcontent_view_access($node) {
55 return user_access(' publish *all* content') ||
56 user_access('unpublish *all* content') ||
57 user_access(' publish '. check_plain($node->type) .' content') ||
58 user_access('unpublish '. check_plain($node->type) .' content') ||
59 node_access('view', $node);
60 }
61
62 function _publishcontent_publish_access($node) {
63 global $user;
64 return !$node->status && (user_access(' publish *all* content') ||
65 (user_access(' publish own '. check_plain($node->type) .' content', $user) || $user->uid == $node->uid) ||
66 user_access(' publish '. check_plain($node->type) .' content'));
67 }
68
69 function _publishcontent_unpublish_access($node) {
70 global $user;
71 return $node->status && (user_access('unpublish *all* content') ||
72 (user_access('unpublish own '. check_plain($node->type) .' content', $user) || $user->uid == $node->uid) ||
73 user_access('unpublish '. check_plain($node->type) .' content'));
74 }
75
76 /**
77 * Implementation of hook_perm().
78 *
79 * @note: the 2 extra spaces is a hack: Drupal 5-6 user permissions page set
80 * 'publish *all* content' auto-magically when 'unpublish *all* content'
81 * is set.
82 */
83 function publishcontent_perm() {
84 $perms = array(
85 ' publish *all* content',
86 'unpublish *all* content',
87 );
88
89 foreach (node_get_types() as $type) {
90 if ($type->module == 'node') {
91 $perms[] = ' publish '. check_plain($type->type) .' content';
92 $perms[] = ' publish own '. check_plain($type->type) .' content';
93 $perms[] = 'unpublish '. check_plain($type->type) .' content';
94 $perms[] = 'unpublish own '. check_plain($type->type) .' content';
95 }
96 }
97 return $perms;
98 }
99
100 function _publishcontent_get_message($nid, $title, $status) {
101 return t($status ? '"@title" [@nid] has been published'
102 : '"@title" [@nid] has been unpublished',
103 array('@title' => $title, '@nid' => $nid));
104 }
105 /**
106 * @param $node a node object
107 */
108 function publishcontent_toggle_status($node) {
109 $node->status = !$node->status;
110 node_save($node);
111 drupal_set_message(_publishcontent_get_message($node->nid, $node->title, $node->status));
112 drupal_goto('node/'. $node->nid);
113 }
114
115 /**
116 * Implementation of hook_simpletest().
117 */
118 function publishcontent_simpletest() {
119 $dir = drupal_get_path('module', 'publishcontent') .'/tests';
120 $tests = file_scan_directory($dir, '\.test$');
121 return array_keys($tests);
122 }
123
124
125 /**
126 * Implementation of hook_views_tables_alter().
127 * implements a a link to un/publish for views
128 */
129 function publishcontent_views_tables_alter(&$tables) {
130 $tables['node']['fields']['publish'] = array(
131 'name' => t('Node: Publish link'),
132 'handler' => array(
133 'publishcontent_views_handler_node_publish_destination' => t('Return To View'),
134 'publishcontent_views_handler_node_publish' => t('Return to Node')
135 ),
136 'notafield' => TRUE,
137 'addlfields' => array('type', 'uid', 'status'),
138 'help' => t('Display a link to publish the node.'),
139 );
140 }
141
142 /**
143 * display a link to publish a node
144 */
145 function publishcontent_views_handler_node_publish($fieldinfo, $fielddata, $value, $data, $destination = NULL) {
146 // try to build a fake node object
147 $data->type = $data->node_type;
148 $data->uid = $data->node_uid;
149 $data->status = $data->node_status;
150
151 if (($data->status && _publishcontent_unpublish_access($data)) ||
152 (!$data->status && _publishcontent_publish_access($data))) {
153 return l(t($data->status ? 'Unpublish' : 'Publish'),
154 "node/$data->nid/". ($data->status ? 'unpublish' : 'publish'),
155 NULL, $destination);
156 }
157 }
158
159 /**
160 * display a link to edit a node with a destination return
161 */
162 function publishcontent_views_handler_node_publish_destination($fieldinfo, $fielddata, $value, $data) {
163 return publishcontent_views_handler_node_publish($fieldinfo, $fielddata, $value, $data, drupal_get_destination());
164 }

  ViewVC Help
Powered by ViewVC 1.1.2