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

Contents of /contributions/modules/comment_workflow/comment_workflow.module

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


Revision 1.3 - (show annotations) (download) (as text)
Thu Aug 21 20:05:08 2008 UTC (15 months ago) by gestaltware
Branch: MAIN
CVS Tags: DRUPAL-5--1-2, HEAD
Changes since 1.2: +3 -3 lines
File MIME type: text/x-php
 + fix get workflow_state by largest weight (http://drupal.org/node/293164)
1 <?php
2 // $Id: comment_workflow.module,v 1.2 2008/07/06 07:58:42 gestaltware Exp $
3
4 /**
5 * @file
6 * Closes comments when last state of workflow is reached.
7 */
8
9 define('COMMENT_NODE_DISABLED', 0);
10 define('COMMENT_NODE_READ_ONLY', 1);
11 define('COMMENT_NODE_READ_WRITE', 2);
12
13 /**
14 * Implementation of hook_help().
15 */
16 function comment_workflow_help($section) {
17 switch ($section) {
18 case 'admin/modules#description':
19 return t('Closes comments when last state of workflow is reached.');
20
21 case 'admin/help#comment_workflow':
22 return t('Comment Workflow closes comments if the node state is changed to the last state of a workflow and re-opens comments if the state is changed from the last state. Comment Workflow will make these changes if:<ul><li>Comment Workflow is enabled for the node type</li><li>The node\'s comment setting is Read only or Read/Write (Comment Workflow will not alter a Disabled comment setting)</li><li>A workflow with three or more states (including creation) is assigned to the node type</li></ul>');
23
24 case 'admin/settings/comment_workflow':
25 return t('Comment Workflow closes comments if the node state is changed to the last state of a workflow and re-opens comments if the state is changed from the last state. Existing nodes of the !node_types where Comment Workflow is enabled can have the comment setting synchronized to the node\'s current workflow state, which might be helpful after first installing the module or to reset from manual comment setting changes.',
26 array('!node_types' => l('node types', 'admin/content/types')));
27 }
28 }
29
30 /**
31 * Implementation of hook_menu().
32 */
33 function comment_workflow_menu($may_cache) {
34 $items = array();
35 if ($may_cache) {
36 $items[] = array(
37 'path' => 'admin/settings/comment_workflow',
38 'title' => 'Comment Workflow',
39 'description' => t('Closes comments when last state of workflow is reached.'),
40 'callback' => 'drupal_get_form',
41 'callback arguments' => 'comment_workflow_sync',
42 'type' => MENU_NORMAL_ITEM,
43 'access' => user_access('administer site configuration'), //do we *really* need yet another permission??
44 );
45 }
46 return $items;
47 }
48
49 /**
50 * Menu callback
51 *
52 * @return
53 * array of form content.
54 */
55 function comment_workflow_sync() {
56 $form = array();
57
58 $form['submit'] = array('#type' => 'submit', '#value' => 'Sync Comment Setting with Workflow States');
59 return $form;
60 }
61
62 /**
63 * Sync comment stting with workflow states for enabled node types.
64 *
65 * @param $form_id
66 * The unique string identifying the form.
67 * @param $form_values
68 * An array of values mirroring the values returned by the form
69 * when it is submitted by a user.
70 */
71 function comment_workflow_sync_submit($form_id, $form_values) {
72 foreach (node_get_types() as $type => $info) {
73 if (!variable_get('comment_workflow_'. $type, 0)) continue;
74 if ($wid = workflow_get_workflow_for_type($type)) {
75 $result = db_fetch_array(db_query('SELECT sid, states FROM {workflow_states} JOIN (SELECT COUNT(sid) as states FROM {workflow_states} WHERE wid=%d GROUP BY wid) as count WHERE wid=%d ORDER BY weight DESC LIMIT 0,1', $wid, $wid));
76 if ($result['states'] < 3) continue;
77
78 if ($count = db_num_rows(db_query('SELECT * FROM {node} LEFT JOIN {workflow_node} USING (nid) WHERE node.type="%s" AND node.comment=%d AND workflow_node.sid=%d', $type, COMMENT_NODE_READ_WRITE, $result['sid']))) {
79 db_query('UPDATE {node} LEFT JOIN {workflow_node} USING (nid) SET node.comment=%d WHERE node.type="%s" AND node.comment=%d AND workflow_node.sid=%d', COMMENT_NODE_READ_ONLY, $type, COMMENT_NODE_READ_WRITE, $result['sid']);
80 drupal_set_message(t('Synchronized %count !type where state is last and comments are read/write.', array('%count' => $count, '!type' => $info->name)));
81 $clear_cache = TRUE;
82 }
83
84 if ($count = db_num_rows(db_query('SELECT * FROM {node} LEFT JOIN {workflow_node} USING (nid) WHERE node.type="%s" AND node.comment=%d AND workflow_node.sid<>%d', $type, COMMENT_NODE_READ_ONLY, $result['sid']))) {
85 db_query('UPDATE {node} LEFT JOIN {workflow_node} USING (nid) SET node.comment=%d WHERE node.type="%s" AND node.comment=%d AND workflow_node.sid<>%d', COMMENT_NODE_READ_WRITE, $type, COMMENT_NODE_READ_ONLY, $result['sid']);
86 drupal_set_message(t('Synchronized %count !type where state is not last and comments are read only.', array('%count' => $count, '!type' => $info->name)));
87 $clear_cache = TRUE;
88 }
89 }
90 }
91 if ($clear_cache) cache_clear_all('*', 'cache', TRUE);
92 drupal_set_message(t('Synchronization complete.'));
93 }
94
95 /**
96 * Implementation of hook_form_alter().
97 */
98 function comment_workflow_form_alter($form_id, &$form) {
99 // Add checkbox to activate per node type
100 if ($form_id == 'node_type_form' && isset($form['identity']['type'])) {
101 // Developers: this will group all comment-related node content type
102 // settings. Copy this code block into your module and we can keep the form
103 // more intuitive until a standard arises.
104 if (!isset($form['comment_options'])) {
105 // create Comment Options fieldset
106 $form['comment_options'] = array(
107 '#type' => 'fieldset',
108 '#title' => t('Comment Options'),
109 '#collapsible' => TRUE,
110 '#weight' => 10 //place below the Workflow fieldset
111 );
112 // move default Comment setting to fieldset
113 if (isset($form['workflow']['comment'])) {
114 $form['comment_options']['comment'] = $form['workflow']['comment'];
115 $form['comment_options']['comment']['#weight'] = -10;
116 unset($form['workflow']['comment']);
117 }
118 }
119 // end comment option grouping
120
121 $form['comment_options']['comment_workflow'] = array(
122 '#type' => 'checkbox',
123 '#title' => t('Enable Comment Workflow for this node type'),
124 '#default_value' => variable_get('comment_workflow_'. $form['#node_type']->type, 0),
125 '#options' => array(t('Disabled'), t('Enabled')),
126 '#description' => t('Check this box to close comments if the node state is changed to the last state of a workflow and re-open comments if the state is changed from the last state.'),
127 );
128 }
129 }
130
131 /**
132 * Implementation of hook_workflow().
133 */
134 function comment_workflow_workflow($op, $old_state, $new_state, $node) {
135 if ($op != 'transition post') return;
136 if ($node->comment == COMMENT_NODE_DISABLED) return;
137 if (!variable_get('comment_workflow_'. $node->type, 0)) return;
138
139 if ($wid = workflow_get_workflow_for_type($node->type)) {
140 $result = db_fetch_array(db_query('SELECT sid, states FROM {workflow_states} JOIN (SELECT COUNT(sid) as states FROM {workflow_states} WHERE wid=%d GROUP BY wid) as count WHERE wid=%d ORDER BY weight DESC LIMIT 0,1', $wid, $wid));
141 if ($result['states'] < 3) return;
142 if ($new_state == $result['sid']) {
143 //last state
144 if ($node->comment == COMMENT_NODE_READ_WRITE) {
145 _comment_workflow_save($node, COMMENT_NODE_READ_ONLY);
146 }
147 }
148 else {
149 //not last state
150 if ($node->comment == COMMENT_NODE_READ_ONLY) {
151 _comment_workflow_save($node, COMMENT_NODE_READ_WRITE);
152 }
153 }
154 }
155 }
156
157 /**
158 * Internal function.
159 */
160 function _comment_workflow_save(&$node, $setting = COMMENT_NODE_READ_ONLY) {
161 $node->comment = $setting;
162 node_save($node);
163 $message = t('Comments have been !action on %title', array(
164 '!action' => ($setting == COMMENT_NODE_READ_ONLY) ? t('closed') : t('re-opened'),
165 '%title' => $node->title
166 ));
167 drupal_set_message($message);
168 watchdog('comment workflow', $message, WATCHDOG_NOTICE);
169 }

  ViewVC Help
Powered by ViewVC 1.1.2