| 1 |
<?php |
<?php |
| 2 |
// $Id: comment_closer.module,v 1.10.2.2.2.7 2009/01/10 19:07:05 rmiddle Exp $ |
// $Id: comment_closer.module,v 1.10.2.2.2.8 2009/01/19 01:03:27 rmiddle Exp $ |
| 3 |
/** |
/** |
| 4 |
* @file |
* @file |
| 5 |
* comment_closer.module |
* comment_closer.module |
| 25 |
return $output; |
return $output; |
| 26 |
} |
} |
| 27 |
|
|
| 28 |
function _comment_closer_nodeoptions($nodetypes) { |
function _comment_closer_nodeoptions() { |
| 29 |
return drupal_map_assoc(array_keys($nodetypes)); |
static $nodeoptions = array(); |
| 30 |
|
if (!count($nodeoptions)) { |
| 31 |
|
$nodetypes = node_get_types(); |
| 32 |
|
foreach ($nodetypes as $key => $value) { |
| 33 |
|
$nodeoptions[$key] = $value->name; |
| 34 |
|
} |
| 35 |
|
} |
| 36 |
|
return $nodeoptions; |
| 37 |
|
} |
| 38 |
|
|
| 39 |
|
function _comment_closer_age_options() { |
| 40 |
|
return array( |
| 41 |
|
'year', |
| 42 |
|
'month', |
| 43 |
|
'quarterly', |
| 44 |
|
'week', |
| 45 |
|
); |
| 46 |
} |
} |
| 47 |
|
|
| 48 |
function comment_closer_settings() { |
function comment_closer_settings() { |
| 49 |
// list of node types to affect |
$cc_form = array(); |
| 50 |
// age of nodes to close comments on |
|
| 51 |
///this next line must be replaced |
$cycle_length_list = array( |
| 52 |
$nodetypes = node_get_types(); |
'yearly' => t('yearly'), |
| 53 |
|
'monthly' => t('monthly'), |
| 54 |
$age_limit_list = array('year' => t('year'), |
'quarterly' => t('quarterly'), |
| 55 |
'month' => t('month'), |
'weekly' => t('weekly'), |
| 56 |
'quarterly' => t('quarterly'), |
'daily' => t('daily') |
| 57 |
'week' => t('week')); |
); |
| 58 |
$cycle_length_list = array(t('yearly') => t('yearly'), |
|
|
'monthly' => t('monthly'), |
|
|
'quarterly' => t('quarterly'), |
|
|
'weekly' => t('weekly'), |
|
|
'daily' => t('daily')); |
|
| 59 |
$cc_form['comment_closer_types'] = array( |
$cc_form['comment_closer_types'] = array( |
| 60 |
'#type' => 'select', |
'#type' => 'select', |
| 61 |
'#multiple' => '1', |
'#multiple' => '1', |
| 62 |
'#title' => t('Node types'), |
'#title' => t('Node types'), |
| 63 |
'#default_value' => variable_get('comment_closer_types', _comment_closer_nodeoptions($nodetypes)), |
'#default_value' => variable_get('comment_closer_types', array()), |
| 64 |
'#options' => _comment_closer_nodeoptions($nodetypes) |
'#options' => _comment_closer_nodeoptions(), |
| 65 |
); |
); |
| 66 |
$cc_form['comment_closer_age'] = array( |
$cc_form['comment_closer_age'] = array( |
| 67 |
'#type' => 'select', |
'#type' => 'textfield', |
| 68 |
'#title' => t('Older than'), |
'#title' => t('Older than'), |
| 69 |
|
'#description' => t('A number of days, or one of the following words: year, quarterly, month, week.'), |
| 70 |
'#default_value' => variable_get('comment_closer_age', 'month'), |
'#default_value' => variable_get('comment_closer_age', 'month'), |
| 71 |
'#options' => $age_limit_list |
'#size' => 10, |
| 72 |
|
'#required' => TRUE, |
| 73 |
); |
); |
| 74 |
$cc_form['comment_closer_cycle_period'] = array( |
$cc_form['comment_closer_cycle_period'] = array( |
| 75 |
'#type' => 'select', |
'#type' => 'select', |
| 80 |
return system_settings_form($cc_form); |
return system_settings_form($cc_form); |
| 81 |
} |
} |
| 82 |
|
|
| 83 |
|
function comment_closer_settings_validate($form, &$form_state) { |
| 84 |
|
$age = $form_state['values']['comment_closer_age']; |
| 85 |
|
if (!in_array($age, _comment_closer_age_options()) && !preg_match('/^\d+$/', $age)) { |
| 86 |
|
form_set_error('comment_closer_age', t('The field "Older than" should be a positive number of days, or one of the following words: year, quarterly, month, week.')); |
| 87 |
|
} |
| 88 |
|
} |
| 89 |
|
|
| 90 |
function comment_closer_cron() { |
function comment_closer_cron() { |
| 91 |
|
|
| 92 |
$now = time(); |
$now = time(); |
| 95 |
$process_node_type_list = variable_get('comment_closer_types', 0); |
$process_node_type_list = variable_get('comment_closer_types', 0); |
| 96 |
|
|
| 97 |
if ($now >= $next_cycle_time) { |
if ($now >= $next_cycle_time) { |
| 98 |
if ($process_node_type_list != 0 || is_array($process_node_type_list) ) { |
//set it up |
| 99 |
//set it up |
$limit = variable_get('comment_closer_age', 'month'); |
| 100 |
$limit = variable_get('comment_closer_age', 'month'); |
switch ($limit) { |
| 101 |
switch ($limit) { |
case 'month': { |
| 102 |
case 'month': { |
$current_date['mon'] = $current_date['mon'] - 1; |
| 103 |
$current_date['mon'] = $current_date['mon'] - 1; |
break; |
| 104 |
break; |
} |
| 105 |
} |
case 'quarterly': { |
| 106 |
case 'quarterly': { |
$current_date['mon'] = $current_date['mon'] - 3; |
| 107 |
$current_date['mon'] = $current_date['mon'] - 3; |
break; |
| 108 |
break; |
} |
| 109 |
} |
case 'year': { |
| 110 |
case 'year': { |
$current_date['year'] = $current_date['year'] - 1; |
| 111 |
$current_date['year'] = $current_date['year'] - 1; |
break; |
| 112 |
break; |
} |
| 113 |
|
case 'week': { |
| 114 |
|
$current_date['mday'] = $current_date['mday'] - 7; |
| 115 |
|
break; |
| 116 |
|
} |
| 117 |
|
default: { |
| 118 |
|
if (preg_match('/^\d+$/', $limit) && (int)$limit > 0) { |
| 119 |
|
$current_date['mday'] = $current_date['mday'] - (int)$limit; |
| 120 |
} |
} |
| 121 |
case 'week': { |
else { // default to 1 month |
| 122 |
$current_date['mday'] = $current_date['mday'] - 7; |
$current_date['mon'] = $current_date['mon'] - 1; |
|
break; |
|
| 123 |
} |
} |
| 124 |
|
break; |
| 125 |
} |
} |
| 126 |
$oldest_allowed = mktime($current_date['hours'], $current_date['minutes'], $current_date['seconds'], $current_date['mon'], $current_date['mday'], $current_date['year']); |
} |
| 127 |
|
$oldest_allowed = mktime($current_date['hours'], $current_date['minutes'], $current_date['seconds'], $current_date['mon'], $current_date['mday'], $current_date['year']); |
| 128 |
|
|
| 129 |
// knock it out |
watchdog('comment_closer', 'date_max :'. date('d/m/Y H:i', $oldest_allowed), array(), WATCHDOG_DEBUG); |
|
$args = array(); |
|
|
$query = "UPDATE {node} SET comment = 1 WHERE (created < %d)"; |
|
|
$args[] = $oldest_allowed; |
|
|
if (!empty($process_node_type_list) && is_array($process_node_type_list)) { |
|
|
$query .= " AND type IN (". db_placeholders($process_node_type_list) .")"; |
|
|
$args = array_merge($args, $process_node_type_list); |
|
|
} |
|
|
cache_clear_all(); |
|
|
$result = db_query($query, $args); |
|
|
watchdog('comment_closer', $query, $args, WATCHDOG_INFO); |
|
|
// clean it up |
|
|
$current_date = getdate(); |
|
| 130 |
|
|
| 131 |
switch (variable_get('comment_closer_cycle_period', 'weekly')) { |
// knock it out |
| 132 |
|
$args = array(); |
| 133 |
|
$query = "UPDATE {node} SET comment = 1 WHERE (created < %d)"; |
| 134 |
|
$args[] = $oldest_allowed; |
| 135 |
|
if (!empty($process_node_type_list) && is_array($process_node_type_list)) { |
| 136 |
|
$query .= " AND type IN (". db_placeholders($process_node_type_list) .")"; |
| 137 |
|
$args = array_merge($args, $process_node_type_list); |
| 138 |
|
} |
| 139 |
|
cache_clear_all(); |
| 140 |
|
$result = db_query($query, $args); |
| 141 |
|
$msg = 'sql = %sql'; |
| 142 |
|
$vars = array( '%sql' => $query ); |
| 143 |
|
watchdog('comment_closer', $msg, $vars, WATCHDOG_DEBUG); |
| 144 |
|
// clean it up |
| 145 |
|
$current_date = getdate(); |
| 146 |
|
|
| 147 |
|
switch (variable_get('comment_closer_cycle_period', 'weekly')) { |
| 148 |
case 'monthly': { |
case 'monthly': { |
| 149 |
$current_date['mon'] = $current_date['mon'] + 1; |
$current_date['mon'] = $current_date['mon'] + 1; |
| 150 |
break; |
break; |
| 165 |
$current_date['mday'] = $current_date['mday'] + 1; |
$current_date['mday'] = $current_date['mday'] + 1; |
| 166 |
break; |
break; |
| 167 |
} |
} |
|
} |
|
|
$comment_closer_next_date = mktime($current_date['hours'], $current_date['minutes'], $current_date['seconds'], $current_date['mon'], $current_date['mday'], $current_date['year']); |
|
|
variable_set('comment_closer_next_date', $comment_closer_next_date); |
|
| 168 |
} |
} |
| 169 |
|
$comment_closer_next_date = mktime($current_date['hours'], $current_date['minutes'], $current_date['seconds'], $current_date['mon'], $current_date['mday'], $current_date['year']); |
| 170 |
|
variable_set('comment_closer_next_date', $comment_closer_next_date); |
| 171 |
} |
} |
| 172 |
} |
} |
| 173 |
|
|