| 1 |
<?php /* $Id: premium.module,v 1.9 2008/07/13 17:54:29 vauxia Exp $ */ |
<?php /* $Id: premium.module,v 1.13 2008/08/03 04:05:25 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 |
*/ |
*/ |
| 5 |
|
|
| 6 |
|
/** |
| 7 |
|
* Implementation of hook_menu() |
| 8 |
|
*/ |
| 9 |
function premium_menu($may_cache) { |
function premium_menu($may_cache) { |
| 10 |
$items = array(); |
$items = array(); |
| 11 |
if ($may_cache) { |
if ($may_cache) { |
| 39 |
*/ |
*/ |
| 40 |
function premium_nodeapi(&$node, $op, $teaser) { |
function premium_nodeapi(&$node, $op, $teaser) { |
| 41 |
$node->premium = _premium_node($node); |
$node->premium = _premium_node($node); |
| 42 |
|
$node->premium_access = _premium_access($node, $teaser); |
| 43 |
|
|
| 44 |
switch ($op) { |
switch ($op) { |
| 45 |
|
case 'load': |
| 46 |
|
return array( |
| 47 |
|
'premium' => $node->premium, |
| 48 |
|
'premium_access' => $node->premium_access); |
| 49 |
|
|
| 50 |
case 'insert': |
case 'insert': |
| 51 |
case 'delete': |
case 'delete': |
| 52 |
case 'update': |
case 'update': |
| 53 |
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 ; |
|
|
_premium_offset($node, $start_ts, $end_ts); |
|
|
db_query('INSERT INTO {premium} (nid, start_ts, end_ts) |
|
|
VALUES ( %d, %d, %d )', $node->nid, $start_ts, $end_ts); |
|
|
} |
|
| 54 |
return; |
return; |
| 55 |
|
|
|
case 'load': |
|
|
$ts = time(); |
|
|
return array('premium' => (int) db_result(db_query( |
|
|
'SELECT 1 FROM {premium} WHERE nid = %d |
|
|
AND ( start_ts = 0 OR start_ts > %d) |
|
|
AND end_ts < %d', $ts, $ts, $node->nid))); |
|
|
|
|
| 56 |
case 'view': |
case 'view': |
| 57 |
$node->premium_access = true; |
if(!$node->premium_access) { |
| 58 |
|
$node->content['body']['#value'] = theme('premium_body', $node); |
|
global $user; |
|
|
if (!$node->premium || user_access('access premium content')) { |
|
|
return; // not premium content or user has privileges |
|
|
} |
|
|
if ($teaser) { |
|
|
return; // not viewing the body |
|
| 59 |
} |
} |
| 60 |
foreach (module_implements('premium_access') as $name) { |
return; |
|
$function = $name .'_premium_access'; |
|
|
if ($function($user, $node)) { |
|
|
return; // access granted explicitly |
|
|
} |
|
|
} |
|
|
|
|
|
$node->premium_access = false; |
|
|
$node->content['body']['#value'] = theme('premium_body', $node); |
|
| 61 |
} |
} |
| 62 |
return; |
return; |
| 63 |
} |
} |
| 64 |
|
|
| 65 |
/** |
/** |
| 66 |
|
* Implementation of hook_node_operations(). |
| 67 |
|
*/ |
| 68 |
|
function premium_node_operations() { |
| 69 |
|
$operations = array( |
| 70 |
|
'premium' => array( |
| 71 |
|
'label' => t('Set premium status'), |
| 72 |
|
'callback' => '_premium_node_operations_premium', |
| 73 |
|
'callback arguments' => array(1), |
| 74 |
|
), |
| 75 |
|
'unpremium' => array( |
| 76 |
|
'label' => t('Remove premium status'), |
| 77 |
|
'callback' => '_premium_node_operations_premium', |
| 78 |
|
'callback arguments' => array(0), |
| 79 |
|
), |
| 80 |
|
); |
| 81 |
|
return $operations; |
| 82 |
|
} |
| 83 |
|
|
| 84 |
|
/** |
| 85 |
|
* Callback for hook_node_operations() |
| 86 |
|
*/ |
| 87 |
|
function _premium_node_operations_premium($nids, $premium = 0) { |
| 88 |
|
foreach($nids as $nid) { |
| 89 |
|
$node = node_load($nid); |
| 90 |
|
_premium_set_premium($node, $premium); |
| 91 |
|
} |
| 92 |
|
} |
| 93 |
|
|
| 94 |
|
/** |
| 95 |
* Implementation of hook_form_alter() |
* Implementation of hook_form_alter() |
| 96 |
* |
* |
| 97 |
* Add the Premium checkbox to the node editing options and default settings |
* Add the Premium checkbox to the node editing options and default settings |
| 127 |
*/ |
*/ |
| 128 |
function premium_settings() { |
function premium_settings() { |
| 129 |
$form = array(); |
$form = array(); |
| 130 |
|
$form['#validate'] = array('premium_settings_save' => array()); |
| 131 |
|
|
| 132 |
|
$premium_types = array(); |
| 133 |
|
foreach(node_get_types('names') as $type => $value) { |
| 134 |
|
if(_premium_node($type)) { |
| 135 |
|
$premium_types[$type] = $type; |
| 136 |
|
} else { |
| 137 |
|
$premium_types[$type] = 0; |
| 138 |
|
} |
| 139 |
|
} |
| 140 |
|
|
| 141 |
|
$form['premium_node_types'] = array( |
| 142 |
|
'#type' => 'checkboxes', |
| 143 |
|
'#options' => node_get_types('names'), |
| 144 |
|
'#title' => t('Node Types'), |
| 145 |
|
'#default_value' => $premium_types, |
| 146 |
|
); |
| 147 |
|
|
| 148 |
// timeframe for premium + update existing nodes |
// timeframe for premium + update existing nodes |
| 149 |
$form['premium_mode'] = array( |
$form['premium_mode'] = array( |
| 150 |
'#type' => 'radios', |
'#type' => 'radios', |
| 175 |
'M' => t('Months'), |
'M' => t('Months'), |
| 176 |
'Y' => t('Years')), |
'Y' => t('Years')), |
| 177 |
); |
); |
| 178 |
|
|
| 179 |
|
$form['premium_bulk_update'] = array( |
| 180 |
|
'#type' => 'checkbox', |
| 181 |
|
'#title' => t('Bulk update premium status'), |
| 182 |
|
'#description' => t('Update all existing nodes of the types selected above with the new premium settings.'), |
| 183 |
|
); |
| 184 |
|
|
| 185 |
$form['premium_message'] = array( |
$form['premium_message'] = array( |
| 186 |
'#type' => 'textarea', |
'#type' => 'textarea', |
| 187 |
'#title' => t('Premium body text'), |
'#title' => t('Premium body text'), |
| 188 |
'#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')), |
| 189 |
'#cols' => 60, |
'#cols' => 60, |
| 190 |
'#rows' => 30, |
'#rows' => 15, |
| 191 |
'#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') |
| 192 |
); |
); |
| 193 |
|
|
| 197 |
return system_settings_form($form); |
return system_settings_form($form); |
| 198 |
} |
} |
| 199 |
|
|
|
|
|
| 200 |
/** |
/** |
| 201 |
* Calculate time offset for auto-aging / auto-archiving |
* Save premium status as set on admin/settings/premium to each type |
| 202 |
*/ |
*/ |
| 203 |
function _premium_offset($node, &$start_ts, &$end_ts) { |
function premium_settings_save($form_id, $form_values) { |
| 204 |
$c = variable_get('premium_time_count', 2); |
$count = $form_values['premium_time_count']; |
| 205 |
$u = variable_get('premium_time_unit', 'W'); |
$unit = $form_values['premium_unit_count']; |
| 206 |
$s = $node->created; |
$mode = $form_values['premium_mode']; |
| 207 |
|
$types = $form_values['premium_node_types']; |
| 208 |
|
|
| 209 |
|
foreach($types as $type => $premium) { |
| 210 |
|
$node_options = variable_get('node_options_' . $type, array()); |
| 211 |
|
if (in_array('premium', $node_options)) { |
| 212 |
|
$premium_key = array_search('premium', $node_options); |
| 213 |
|
unset($node_options[$premium_key]); |
| 214 |
|
} |
| 215 |
|
if ($types[$type]) { |
| 216 |
|
$node_options = array_merge($node_options, array('premium')); |
| 217 |
|
$premium_types[] = $types[$type]; |
| 218 |
|
} |
| 219 |
|
variable_set('node_options_' . $type, $node_options); |
| 220 |
|
} |
| 221 |
|
|
| 222 |
switch ( variable_get('premium_mode', '0') ) { |
if ($form_values['premium_bulk_update']) { |
| 223 |
|
db_query("DELETE from {premium}"); |
| 224 |
case 'archive' : // public first, premium after awhile |
|
| 225 |
$start_ts = mktime( |
$start = $end = 0; |
| 226 |
date('H', $s)+($u == 'H')*$c, 0, 0, |
_premium_offset(0, $start, $end, $mode, $count, $unit); |
| 227 |
date('m', $s)+($u=='M')*$c, |
|
| 228 |
date('d', $s)+($u=='D')*$c+($u=='W')*$c*7, |
foreach ($premium_types as $type) { |
| 229 |
date('y', $s)+($u=='Y')*$c |
// Apply the timestamp delta's to the node's created date. |
| 230 |
); |
if ($start) $start = 'created + '. (int) $start; |
| 231 |
return; |
if ($end) $end = 'created + '. (int) $end; |
| 232 |
|
|
| 233 |
case 'latest' : // premium first, ages to general availability |
db_query("INSERT INTO {premium} (nid, start_ts, end_ts) |
| 234 |
$end_ts = mktime( |
SELECT nid, $start, $end FROM {node} WHERE type = '%s'", $type); |
| 235 |
date('H', $s)+($u=='H')*$c, 0, 0, |
} |
|
date('m', $s)+($u=='M')*$c, |
|
|
date('d', $s)+($u=='D')*$c+($u=='W')*$c*7, |
|
|
date('y', $s)+($u=='Y')*$c |
|
|
); |
|
|
return; |
|
| 236 |
} |
} |
| 237 |
|
} |
| 238 |
|
|
| 239 |
|
/** |
| 240 |
|
* Calculate time offset for auto-aging / auto-archiving |
| 241 |
|
*/ |
| 242 |
|
function _premium_offset($timestamp, &$start_ts, &$end_ts, $mode, $count, $unit) { |
| 243 |
|
|
| 244 |
|
// If the timestamp is zero, set it to "now" so mktime() will work properly. |
| 245 |
|
$ts = $timestamp ? $timestamp : time(); |
| 246 |
|
$offset = mktime( |
| 247 |
|
date('H', $ts)+($unit=='H')*$count, 0, 0, |
| 248 |
|
date('m', $ts)+($unit=='M')*$count, |
| 249 |
|
date('d', $ts)+($unit=='D')*$count+($unit=='W')*$count*7, |
| 250 |
|
date('y', $ts)+($unit=='Y')*$count |
| 251 |
|
); |
| 252 |
|
|
| 253 |
|
// If we faked a timestamp, remove it. |
| 254 |
|
if ($ts != $timestamp) $offset -= $ts; |
| 255 |
|
|
| 256 |
|
if ($mode == 'archive') $start_ts = $offset; |
| 257 |
|
if ($mode == 'latest') $end_ts = $offset; |
| 258 |
return; |
return; |
| 259 |
} |
} |
| 260 |
|
|
| 267 |
return in_array('premium', variable_get("node_options_{$node}", array())); |
return in_array('premium', variable_get("node_options_{$node}", array())); |
| 268 |
} |
} |
| 269 |
|
|
| 270 |
// Already has a value |
// Already has a value. |
| 271 |
if (isset($node->premium)) return $node->premium; |
if (isset($node->premium)) return $node->premium; |
| 272 |
|
|
| 273 |
// Use default settings for this node type |
if ($node->nid) { |
| 274 |
|
// Attempt to find the value from the premium table. |
| 275 |
|
return (int) db_result(db_query("SELECT 1 FROM {premium} WHERE nid = %d |
| 276 |
|
AND ( start_ts = 0 and end_ts > %d) |
| 277 |
|
OR ( start_ts < %d AND end_ts = 0) |
| 278 |
|
OR ( start_ts = 0 AND end_ts = 0)", $node->nid, time(), time())); |
| 279 |
|
} |
| 280 |
|
|
| 281 |
|
// Use default settings for this node type. |
| 282 |
return in_array('premium', variable_get("node_options_{$node->type}", array())); |
return in_array('premium', variable_get("node_options_{$node->type}", array())); |
| 283 |
} |
} |
| 284 |
|
|
| 285 |
/** |
/** |
| 286 |
|
* Establish premium visibility settings for a node |
| 287 |
|
*/ |
| 288 |
|
function _premium_access($node, $teaser) { |
| 289 |
|
if (isset($node->premium_access)) return $node->premium_access; |
| 290 |
|
|
| 291 |
|
// Not viewing the body, or it's not premium, or user has privileges. |
| 292 |
|
if ($teaser || !$node->premium || user_access('access premium content')) { |
| 293 |
|
return TRUE; |
| 294 |
|
} |
| 295 |
|
|
| 296 |
|
// Access is granted explicitly. |
| 297 |
|
foreach (module_implements('premium_access') as $name) { |
| 298 |
|
$function = $name .'_premium_access'; |
| 299 |
|
if (is_bool($access = $function($user, $node))) { |
| 300 |
|
return $access; |
| 301 |
|
} |
| 302 |
|
} |
| 303 |
|
|
| 304 |
|
// Nobody said we could access the node. |
| 305 |
|
return FALSE; |
| 306 |
|
} |
| 307 |
|
|
| 308 |
|
/** |
| 309 |
|
* Update the premium table with appropriate premium values for a node. |
| 310 |
|
*/ |
| 311 |
|
function _premium_set_premium($node, $premium = FALSE) { |
| 312 |
|
db_query('DELETE FROM {premium} WHERE nid = %d', $node->nid); |
| 313 |
|
if ($premium) { |
| 314 |
|
$start_ts = $end_ts = 0 ; |
| 315 |
|
$mode = variable_get('premium_mode', 0); |
| 316 |
|
$count = variable_get('premium_time_count', 2); |
| 317 |
|
$unit = variable_get('premium_time_unit', 'W'); |
| 318 |
|
_premium_offset($node->created, $start_ts, $end_ts, $mode, $count, $unit); |
| 319 |
|
db_query('INSERT INTO {premium} (nid, start_ts, end_ts) |
| 320 |
|
VALUES ( %d, %d, %d )', $node->nid, $start_ts, $end_ts); |
| 321 |
|
} |
| 322 |
|
} |
| 323 |
|
|
| 324 |
|
/** |
| 325 |
* Reformat the message body with a premium content message |
* Reformat the message body with a premium content message |
| 326 |
*/ |
*/ |
| 327 |
function theme_premium_body($node) { |
function theme_premium_body($node) { |