| 1 |
<?php /* $Id: premium.module,v 1.10 2008/07/19 15:27:30 vauxia Exp $ */ |
<?php /* $Id: premium.module,v 1.11 2008/07/30 03:16:23 vauxia Exp $ */ |
| 2 |
/** |
/** |
| 3 |
* @file Restrict access to the full body of premium content |
* @file Restrict access to the full body of premium content |
| 4 |
*/ |
*/ |
| 41 |
|
|
| 42 |
case 'insert': |
case 'insert': |
| 43 |
case 'delete': |
case 'delete': |
| 44 |
case 'update': |
case 'update': |
| 45 |
if ($node->nid) { |
_premium_set_premium($node, $node->premium); |
|
db_query('DELETE FROM {premium} WHERE nid = %d', $node->nid); |
|
|
if ($op == 'delete') return; |
|
|
} |
|
|
if ($node->premium) { |
|
|
$start_ts = $end_ts = 0 ; |
|
|
$mode = variable_get('premium_mode', 0); |
|
|
$count = variable_get('premium_time_count', 2); |
|
|
$unit = variable_get('premium_time_unit', 'W'); |
|
|
_premium_offset($node->created, $start_ts, $end_ts, $mode, $count, $unit); |
|
|
|
|
|
db_query('INSERT INTO {premium} (nid, start_ts, end_ts) |
|
|
VALUES ( %d, %d, %d )', $node->nid, $start_ts, $end_ts); |
|
|
} |
|
| 46 |
return; |
return; |
| 47 |
|
|
| 48 |
case 'load': |
case 'load': |
| 77 |
} |
} |
| 78 |
|
|
| 79 |
/** |
/** |
| 80 |
|
* implementation of hook_node_operations(). |
| 81 |
|
*/ |
| 82 |
|
function premium_node_operations() { |
| 83 |
|
$operations = array( |
| 84 |
|
'premium' => array( |
| 85 |
|
'label' => t('Set premium status'), |
| 86 |
|
'callback' => '_premium_node_operations_premium', |
| 87 |
|
'callback arguments' => array(1), |
| 88 |
|
), |
| 89 |
|
'unpremium' => array( |
| 90 |
|
'label' => t('Remove premium status'), |
| 91 |
|
'callback' => '_premium_node_operations_premium', |
| 92 |
|
'callback arguments' => array(0), |
| 93 |
|
), |
| 94 |
|
); |
| 95 |
|
return $operations; |
| 96 |
|
} |
| 97 |
|
|
| 98 |
|
/** |
| 99 |
* Implementation of hook_form_alter() |
* Implementation of hook_form_alter() |
| 100 |
* |
* |
| 101 |
* Add the Premium checkbox to the node editing options and default settings |
* Add the Premium checkbox to the node editing options and default settings |
| 131 |
*/ |
*/ |
| 132 |
function premium_settings() { |
function premium_settings() { |
| 133 |
$form = array(); |
$form = array(); |
| 134 |
|
$form['#validate'] = array('premium_settings_save' => array()); |
| 135 |
|
|
| 136 |
|
$premium_types = array(); |
| 137 |
|
foreach(node_get_types('names') as $type => $value) { |
| 138 |
|
if(_premium_node($type)) { |
| 139 |
|
$premium_types[$type] = $type; |
| 140 |
|
} else { |
| 141 |
|
$premium_types[$type] = 0; |
| 142 |
|
} |
| 143 |
|
} |
| 144 |
|
|
| 145 |
|
$form['premium_node_types'] = array( |
| 146 |
|
'#type' => 'checkboxes', |
| 147 |
|
'#options' => node_get_types('names'), |
| 148 |
|
'#title' => t('Node Types'), |
| 149 |
|
'#default_value' => $premium_types, |
| 150 |
|
); |
| 151 |
|
|
| 152 |
// timeframe for premium + update existing nodes |
// timeframe for premium + update existing nodes |
| 153 |
$form['premium_mode'] = array( |
$form['premium_mode'] = array( |
| 154 |
'#type' => 'radios', |
'#type' => 'radios', |
| 179 |
'M' => t('Months'), |
'M' => t('Months'), |
| 180 |
'Y' => t('Years')), |
'Y' => t('Years')), |
| 181 |
); |
); |
| 182 |
|
|
| 183 |
|
$form['premium_bulk_update'] = array( |
| 184 |
|
'#type' => 'checkbox', |
| 185 |
|
'#title' => t('Bulk update premium status'), |
| 186 |
|
'#description' => t('Update all existing nodes of the types selected above with the new premium settings.'), |
| 187 |
|
); |
| 188 |
|
|
| 189 |
$form['premium_message'] = array( |
$form['premium_message'] = array( |
| 190 |
'#type' => 'textarea', |
'#type' => 'textarea', |
| 191 |
'#title' => t('Premium body text'), |
'#title' => t('Premium body text'), |
| 192 |
'#default_value' => variable_get('premium_message', t('Full text available to premium subscribers only')), |
'#default_value' => variable_get('premium_message', t('Full text available to premium subscribers only')), |
| 193 |
'#cols' => 60, |
'#cols' => 60, |
| 194 |
'#rows' => 30, |
'#rows' => 15, |
| 195 |
'#description' => t('When a visitor doesn\'t have access to a premium item they will see this message instead of its full text') |
'#description' => t('When a visitor doesn\'t have access to a premium item they will see this message instead of its full text') |
| 196 |
); |
); |
| 197 |
|
|
| 201 |
return system_settings_form($form); |
return system_settings_form($form); |
| 202 |
} |
} |
| 203 |
|
|
|
|
|
| 204 |
/** |
/** |
| 205 |
* Calculate time offset for auto-aging / auto-archiving |
* Save premium-ness as set on admin/settings/premium to each type |
| 206 |
*/ |
*/ |
| 207 |
function _premium_offset($ts, &$start_ts, &$end_ts, $mode, $count, $unitnit) { |
function premium_settings_save($form_id, $form_values) { |
| 208 |
$c = variable_get('premium_time_count', 2); |
$count = $form_values['premium_time_count']; |
| 209 |
$unit = variable_get('premium_time_unit', 'W'); |
$unit = $form_values['premium_unit_count']; |
| 210 |
$ts = $node->created; |
$mode = $form_values['premium_mode']; |
| 211 |
|
$types = $form_values['premium_node_types']; |
| 212 |
|
|
| 213 |
|
foreach($types as $type => $premium) { |
| 214 |
|
$node_options = variable_get('node_options_' . $type, array()); |
| 215 |
|
if (in_array('premium', $node_options)) { |
| 216 |
|
$premium_key = array_search('premium', $node_options); |
| 217 |
|
unset($node_options[$premium_key]); |
| 218 |
|
} |
| 219 |
|
if ($types[$type]) { |
| 220 |
|
$node_options = array_merge($node_options, array('premium')); |
| 221 |
|
$premium_types[] = $types[$type]; |
| 222 |
|
} |
| 223 |
|
variable_set('node_options_' . $type, $node_options); |
| 224 |
|
} |
| 225 |
|
|
| 226 |
switch ($mode) { |
if ($form_values['premium_bulk_update']) { |
| 227 |
|
db_query("DELETE from {premium}"); |
| 228 |
case 'archive' : // public first, premium after awhile |
|
| 229 |
$start_ts = mktime( |
$start = $end = 0; |
| 230 |
date('H', $ts)+($unit == 'H')*$count, 0, 0, |
_premium_offset(0, $start, $end, $mode, $count, $unit); |
| 231 |
date('m', $ts)+($unit=='M')*$count, |
|
| 232 |
date('d', $ts)+($unit=='D')*$count+($unit=='W')*$count*7, |
foreach ($premium_types as $type) { |
| 233 |
date('y', $ts)+($unit=='Y')*$count |
// Apply the timestamp delta's to the node's created date. |
| 234 |
); |
if ($start) $start = 'created + '. (int) $start; |
| 235 |
return; |
if ($end) $end = 'created + '. (int) $end; |
| 236 |
|
|
| 237 |
case 'latest' : // premium first, ages to general availability |
db_query("INSERT INTO {premium} (nid, start_ts, end_ts) |
| 238 |
$end_ts = mktime( |
SELECT nid, $start, $end FROM {node} WHERE type = '%s'", $type); |
| 239 |
date('H', $ts)+($unit=='H')*$count, 0, 0, |
} |
|
date('m', $ts)+($unit=='M')*$count, |
|
|
date('d', $ts)+($unit=='D')*$count+($unit=='W')*$count*7, |
|
|
date('y', $ts)+($unit=='Y')*$count |
|
|
); |
|
|
return; |
|
| 240 |
} |
} |
| 241 |
|
} |
| 242 |
|
|
| 243 |
|
/** |
| 244 |
|
* Calculate time offset for auto-aging / auto-archiving |
| 245 |
|
*/ |
| 246 |
|
function _premium_offset($timestamp, &$start_ts, &$end_ts, $mode, $count, $unit) { |
| 247 |
|
|
| 248 |
|
// If the timestamp is zero, set it to "now" so mktime() will work properly. |
| 249 |
|
$ts = $timestamp ? $timestamp : time(); |
| 250 |
|
$offset = mktime( |
| 251 |
|
date('H', $ts)+($unit=='H')*$count, 0, 0, |
| 252 |
|
date('m', $ts)+($unit=='M')*$count, |
| 253 |
|
date('d', $ts)+($unit=='D')*$count+($unit=='W')*$count*7, |
| 254 |
|
date('y', $ts)+($unit=='Y')*$count |
| 255 |
|
); |
| 256 |
|
|
| 257 |
|
// If we faked a timestamp, remove it. |
| 258 |
|
if ($ts != $timestamp) $offset -= $ts; |
| 259 |
|
|
| 260 |
|
if ($mode == 'archive') $start_ts = $offset; |
| 261 |
|
if ($mode == 'latest') $end_ts = $offset; |
| 262 |
return; |
return; |
| 263 |
} |
} |
| 264 |
|
|
| 278 |
return in_array('premium', variable_get("node_options_{$node->type}", array())); |
return in_array('premium', variable_get("node_options_{$node->type}", array())); |
| 279 |
} |
} |
| 280 |
|
|
| 281 |
|
function _premium_node_operations_premium($nids, $premium = 0) { |
| 282 |
|
foreach($nids as $nid) { |
| 283 |
|
$node = node_load($nid); |
| 284 |
|
_premium_set_premium($node, $premium); |
| 285 |
|
} |
| 286 |
|
} |
| 287 |
|
|
| 288 |
|
function _premium_set_premium($node, $premium = FALSE) { |
| 289 |
|
db_query('DELETE FROM {premium} WHERE nid = %d', $node->nid); |
| 290 |
|
if ($premium) { |
| 291 |
|
$start_ts = $end_ts = 0 ; |
| 292 |
|
$mode = variable_get('premium_mode', 0); |
| 293 |
|
$count = variable_get('premium_time_count', 2); |
| 294 |
|
$unit = variable_get('premium_time_unit', 'W'); |
| 295 |
|
_premium_offset($node->created, $start_ts, $end_ts, $mode, $count, $unit); |
| 296 |
|
db_query('INSERT INTO {premium} (nid, start_ts, end_ts) |
| 297 |
|
VALUES ( %d, %d, %d )', $node->nid, $start_ts, $end_ts); |
| 298 |
|
} |
| 299 |
|
} |
| 300 |
|
|
| 301 |
/** |
/** |
| 302 |
* Reformat the message body with a premium content message |
* Reformat the message body with a premium content message |
| 303 |
*/ |
*/ |