/[drupal]/contributions/modules/commentcloser/comment_closer.module
ViewVC logotype

Contents of /contributions/modules/commentcloser/comment_closer.module

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


Revision 1.12 - (show annotations) (download) (as text)
Sun Jun 29 19:37:15 2008 UTC (16 months, 4 weeks ago) by rmiddle
Branch: MAIN
CVS Tags: HEAD
Changes since 1.11: +116 -98 lines
File MIME type: text/x-php
Added uninstall for the module.  Also added the abitly to add a comment during closing.
1 <?php
2 // $Id$
3 /**
4 * @file
5 * comment_closer.module
6 * Automatically close comments on nodes beyond a configurable age
7 */
8
9
10 define('COMMENT_CLOSER_SUBJECT', t('Comments closed at !date'));
11 define('COMMENT_CLOSER_BODY', t('This item is no longer accepting comments.'));
12
13 function comment_closer_help($path, $arg) {
14 switch ($path) {
15 case 'admin/block/help':
16 return t('<p>Automatically close comments</p>');
17 break;
18 case "admin/modules#description":
19 $output = t('Schedule automatic closing of comments for selected node types based on the age of the node');
20 break;
21 case "admin/help#comment_closer":
22 $output = t('<p>The commentcloser module allows you to automatically close comments via a cron hook. You can select any combination of available node types to process,</p><p>Configure the module to close comments on nodes more than one week, month or year old and schedule the closings to be done daily, weekly, monthly or annually</p>
23
24 ');
25 break;
26 default:
27 $output = "";
28 break;
29 }
30 return $output;
31 }
32
33 function _comment_closer_nodeoptions($nodetypes) {
34 return drupal_map_assoc(array_keys($nodetypes));
35 }
36
37 function comment_closer_settings() {
38 // list of node types to affect
39 // age of nodes to close comments on
40 ///this next line must be replaced
41 $nodetypes = node_get_types();
42
43 $age_limit_list = array('year' => t('year'),
44 'month' => t('month'),
45 'quarterly' => t('quarterly'),
46 'week' => t('week'));
47 $cycle_length_list = array(t('yearly') => t('yearly'),
48 'monthly' => t('monthly'),
49 'quarterly' => t('quarterly'),
50 'weekly' => t('weekly'),
51 'daily' => t('daily'));
52 $cc_form['comment_closer_types'] = array(
53 '#type' => 'select',
54 '#multiple' => '1',
55 '#title' => t('Node types'),
56 '#default_value' => variable_get('comment_closer_types', _comment_closer_nodeoptions($nodetypes)),
57 '#options' => _comment_closer_nodeoptions($nodetypes)
58 );
59 $cc_form['comment_closer_age'] = array(
60 '#type' => 'select',
61 '#title' => t('Older than'),
62 '#default_value' => variable_get('comment_closer_age', 'month'),
63 '#options' => $age_limit_list
64 );
65 $cc_form['comment_closer_cycle_period'] = array(
66 '#type' => 'select',
67 '#title' => t('Execute'),
68 '#default_value' => variable_get('comment_closer_cycle_period', array('daily')),
69 '#options' => $cycle_length_list,
70 );
71 $cc_form['comment_closer_insert_closing_comment'] = array(
72 '#type' => 'checkbox',
73 '#title' => t('Insert Comment'),
74 '#default_value' => variable_get('comment_closer_insert_closing_comment', 0),
75 '#description' => t('Insert a comment when closed comments are closed.'),
76 );
77 $cc_form['comment_closer_insert_subject'] = array(
78 '#type' => 'textfield',
79 '#title' => t('Comment Closer Subject'),
80 '#default_value' => variable_get('comment_closer_insert_subject', COMMENT_CLOSER_SUBJECT),
81 '#description' => t('Subject of messages when closing the comments')
82 );
83 $cc_form['comment_closer_insert_body'] = array(
84 '#type' => 'textarea',
85 '#title' => t('Comment Closer Body'),
86 '#default_value' => variable_get('comment_closer_insert_body', COMMENT_CLOSER_BODY),
87 '#rows' => 10,
88 '#description' => t('Customize the messages when closing the comments.') .' '. t('Available variables are:') .' !site, !uri, !uri_brief, !date.'
89 );
90 return system_settings_form($cc_form);
91 }
92
93 function _comment_closer_node_select($nodetypes) {
94 foreach ($nodetypes as $nodetype_index) {
95 $node_condition[] = '(type=\''. $nodetype_index .'\')';
96 }
97 return implode(' OR ', $node_condition);
98 }
99
100 function _comment_closer_comment_save($post_comment) {
101 if ($cid = comment_save($post_comment)) {
102 $node = node_load($post_comment->$nid);
103 comment_new_page_count($node->comment_count, 1, $node);
104 return;
105 }
106 }
107
108
109 function comment_closer_cron() {
110
111 $now = time();
112 $current_date = getdate($now);
113 $next_cycle_time = variable_get('comment_closer_next_date', $now);
114 $process_node_type_list = variable_get('comment_closer_types', 0);
115
116 if ($now >= $next_cycle_time) {
117 if ($process_node_type_list != 0 || is_array($process_node_type_list) ) {
118 //set it up
119 $variables = array(
120 '!site' => variable_get('site_name', 'Drupal'),
121 '!uri' => $base_url,
122 '!uri_brief' => substr($base_url, strlen('http://')),
123 '!date' => format_date(time()),
124 );
125 $admin = user_load (1); // load the 1st account
126 $limit = variable_get('comment_closer_age', 'month');
127 switch ($limit) {
128 case 'month': {
129 $current_date['mon'] = $current_date['mon'] - 1;
130 break;
131 }
132 case 'quarterly': {
133 $current_date['mon'] = $current_date['mon'] - 3;
134 break;
135 }
136 case 'year': {
137 $current_date['year'] = $current_date['year'] - 1;
138 break;
139 }
140 case 'week': {
141 $current_date['mday'] = $current_date['mday'] - 7;
142 break;
143 }
144 }
145 $oldest_allowed = mktime($current_date['hours'], $current_date['minutes'], $current_date['seconds'], $current_date['mon'], $current_date['mday'], $current_date['year']);
146
147 // knock it out
148 cache_clear_all();
149 if (variable_get('comment_closer_insert_closing_comment', 0)==0) {
150 $sql = sprintf("UPDATE {node} SET comment = 1 WHERE (created < '%d') AND (comment = 2) AND (%s)", //%s is escaped on in the fuction and breaks the sql statement
151 $oldest_allowed,
152 _comment_closer_node_select($process_node_type_list)
153 );
154 $result = db_query($sql);
155 $msg = 'sql = %sql';
156 $vars = array( '%sql' => $sql );
157 watchdog('comment_closer',$msg, $vars, WATCHDOG_DEBUG);
158 }
159 else {
160 $sql = sprintf(
161 "SELECT * FROM {node} WHERE (created < '%d') AND (comment = 2) AND (%s)", //%s is escaped on in the fuction and breaks the sql statement
162 $oldest_allowed,
163 _comment_closer_node_select($process_node_type_list)
164 );
165 $result = db_query($sql);
166 $msg = 'sql = %sql';
167 $vars = array( '%sql' => $sql );
168 watchdog('comment_closer',$msg, $vars, WATCHDOG_DEBUG);
169
170 while ($data = db_fetch_object($result)) {
171 $post_comment = array(
172 'nid' => $data->nid,
173 'pid' => 0,
174 'uid' => 1,
175 'subject' => t(variable_get('comment_closer_insert_subject', COMMENT_CLOSER_SUBJECT), $variables),
176 'comment' => t(variable_get('comment_closer_insert_body', COMMENT_CLOSER_BODY), $variables),
177 'format' => FILTER_FORMAT_DEFAULT,
178 'timestamp' => format_date($now, 'custom', 'Y-m-d H:i O'),
179 'status' => 0,
180 'name' => $admin->name,
181 'mail' => variable_get('site_mail', ini_get('sendmail_from')),
182 'homepage' => '',
183 );
184 _comment_closer_comment_save($post_comment);
185 $sql = sprintf(
186 "UPDATE {node} SET comment = 1 WHERE nid = %d",
187 $data->nid
188 );
189 db_query($sql);
190 $msg = 'comment_closer: Closing comments for node %node';
191 $vars = array( '%node' => $data->nid);
192 watchdog('comment_closer', $msg, $vars, WATCHDOG_INFO);
193 }
194 }
195 // clean it up
196 $current_date = getdate();
197
198 switch (variable_get('comment_closer_cycle_period', 'weekly')) {
199 case 'monthly': {
200 $current_date['mon'] = $current_date['mon'] + 1;
201 break;
202 }
203 case 'quarterly': {
204 $current_date['mon'] = $current_date['mon'] + 3;
205 break;
206 }
207 case 'yearly': {
208 $current_date['year'] = $current_date['year'] + 1;
209 break;
210 }
211 case 'weekly': {
212 $current_date['mday'] = $current_date['mday'] + 7;
213 break;
214 }
215 case 'daily': {
216 $current_date['mday'] = $current_date['mday'] + 1;
217 break;
218 }
219 }
220 $comment_closer_next_date = mktime($current_date['hours'], $current_date['minutes'], $current_date['seconds'], $current_date['mon'], $current_date['mday'], $current_date['year']);
221
222 variable_set('comment_closer_next_date', $comment_closer_next_date);
223 }
224 }
225 }
226
227 function comment_closer_menu() {
228 $items['admin/settings/comment_closer'] = array(
229 'title' => 'Comment closer',
230 'description' => 'Set age, frequency and types of nodes for which comments will be closed.',
231 'page callback' => 'drupal_get_form',
232 'page arguments' => array('comment_closer_settings'),
233 'access arguments' => array('administer site configuration'),
234 );
235 return $items;
236 }

  ViewVC Help
Powered by ViewVC 1.1.2