| 1 |
<?php /* $Id: premium.module,v 1.15 2008/08/03 05:40:51 jerdavis Exp $ */ |
<?php |
| 2 |
|
// $Id$ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* @file Restrict access to the full body of premium content |
* @file Restrict access to the full body of premium content |
| 6 |
*/ |
*/ |
| 8 |
/** |
/** |
| 9 |
* Implementation of hook_menu() |
* Implementation of hook_menu() |
| 10 |
*/ |
*/ |
| 11 |
function premium_menu($may_cache) { |
function premium_menu() { |
| 12 |
$items = array(); |
$items = array(); |
| 13 |
if ($may_cache) { |
$items['admin/settings/premium'] = array( |
| 14 |
$items[] = array('path' => 'admin/settings/premium', |
'title' => 'Premium', |
| 15 |
'title' => t('Premium'), |
'description' => 'Settings for access control to premium content.', |
| 16 |
'description' => t('Settings for access control to premium content.'), |
'page callback' => 'drupal_get_form', |
| 17 |
'callback' => 'drupal_get_form', |
'page arguments' => array('premium_settings'), |
| 18 |
'callback arguments' => 'premium_settings', |
'access arguments' => array('administer site configuration'), |
| 19 |
'access' => user_access('administer site configuration')); |
); |
|
} |
|
| 20 |
return $items; |
return $items; |
| 21 |
} |
} |
| 22 |
|
|
| 32 |
*/ |
*/ |
| 33 |
function premium_cron() { |
function premium_cron() { |
| 34 |
$ts = time(); |
$ts = time(); |
| 35 |
db_query("DELETE FROM {premium} WHERE start_ts < $ts AND end_ts != 0 AND end_ts < $ts"); |
db_query("DELETE FROM {premium} WHERE start_ts < %d AND end_ts <> 0 AND end_ts < %d", $ts, $ts); |
| 36 |
} |
} |
| 37 |
|
|
| 38 |
/** |
/** |
| 55 |
return; |
return; |
| 56 |
|
|
| 57 |
case 'view': |
case 'view': |
| 58 |
if(!$node->premium_access) { |
if (!$node->premium_access) { |
| 59 |
$node->content['body']['#value'] = theme('premium_body', $node); |
$node->content['body']['#value'] = theme('premium_body', $node); |
| 60 |
} |
} |
| 61 |
return; |
return; |
| 86 |
* Callback for hook_node_operations() |
* Callback for hook_node_operations() |
| 87 |
*/ |
*/ |
| 88 |
function _premium_node_operations_premium($nids, $premium = 0) { |
function _premium_node_operations_premium($nids, $premium = 0) { |
| 89 |
foreach($nids as $nid) { |
foreach ($nids as $nid) { |
| 90 |
$node = node_load($nid); |
$node = node_load($nid); |
| 91 |
_premium_set_premium($node, $premium); |
_premium_set_premium($node, $premium); |
| 92 |
} |
} |
| 98 |
* Add the Premium checkbox to the node editing options and default settings |
* Add the Premium checkbox to the node editing options and default settings |
| 99 |
* The Premium flag will behave like other options (published, promote, etc) |
* The Premium flag will behave like other options (published, promote, etc) |
| 100 |
*/ |
*/ |
| 101 |
function premium_form_alter($form_id, &$form) { |
function premium_form_alter(&$form, $form_state, $form_id) { |
| 102 |
$type = $form['type']['#value']; |
$type = $form['type']['#value']; |
| 103 |
$title = t('Access restricted for non-premium users'); |
$title = t('Access restricted for non-premium users'); |
| 104 |
switch ($form_id) { |
switch ($form_id) { |
| 128 |
*/ |
*/ |
| 129 |
function premium_settings() { |
function premium_settings() { |
| 130 |
$form = array(); |
$form = array(); |
| 131 |
$form['#validate'] = array('premium_settings_save' => array()); |
$form['#validate'] = array('premium_settings_validate'); |
| 132 |
|
|
| 133 |
$premium_types = array(); |
$premium_types = array(); |
| 134 |
foreach(node_get_types('names') as $type => $value) { |
foreach (node_get_types('names') as $type => $value) { |
| 135 |
if(_premium_node($type)) { |
if (_premium_node($type)) { |
| 136 |
$premium_types[$type] = $type; |
$premium_types[$type] = $type; |
| 137 |
} else { |
} |
| 138 |
|
else { |
| 139 |
$premium_types[$type] = 0; |
$premium_types[$type] = 0; |
| 140 |
} |
} |
| 141 |
} |
} |
| 159 |
), |
), |
| 160 |
); |
); |
| 161 |
|
|
| 162 |
$options = (range(1, 15)); |
$options = (range(0, 15)); |
| 163 |
unset($options[0]); |
unset($options[0]); |
| 164 |
$form['premium_time_count'] = array( |
$form['premium_time_count'] = array( |
| 165 |
'#type' => 'select', |
'#type' => 'select', |
| 194 |
); |
); |
| 195 |
|
|
| 196 |
if (module_exists('filter')) { |
if (module_exists('filter')) { |
| 197 |
$form['premium_format'] = filter_form(variable_get('premium_format', FILTER_FORMAT_DEFAULT), null, array('premium_format')); |
$form['premium_format'] = filter_form(variable_get('premium_format', FILTER_FORMAT_DEFAULT), NULL, array('premium_format')); |
| 198 |
} |
} |
| 199 |
return system_settings_form($form); |
return system_settings_form($form); |
| 200 |
} |
} |
| 202 |
/** |
/** |
| 203 |
* Save premium status as set on admin/settings/premium to each type |
* Save premium status as set on admin/settings/premium to each type |
| 204 |
*/ |
*/ |
| 205 |
function premium_settings_save($form_id, $form_values) { |
function premium_settings_validate($form, &$form_state) { |
| 206 |
$count = $form_values['premium_time_count']; |
$count = $form_state['values']['premium_time_count']; |
| 207 |
$unit = $form_values['premium_time_unit']; |
$unit = $form_state['values']['premium_time_unit']; |
| 208 |
$mode = $form_values['premium_mode']; |
$mode = $form_state['values']['premium_mode']; |
| 209 |
$types = $form_values['premium_node_types']; |
$types = $form_state['values']['premium_node_types']; |
| 210 |
|
|
| 211 |
foreach($types as $type => $premium) { |
foreach ($types as $type => $premium) { |
| 212 |
$node_options = variable_get('node_options_' . $type, array()); |
$node_options = variable_get('node_options_'. $type, array()); |
| 213 |
if (in_array('premium', $node_options)) { |
if (in_array('premium', $node_options)) { |
| 214 |
$premium_key = array_search('premium', $node_options); |
$premium_key = array_search('premium', $node_options); |
| 215 |
unset($node_options[$premium_key]); |
unset($node_options[$premium_key]); |
| 218 |
$node_options = array_merge($node_options, array('premium')); |
$node_options = array_merge($node_options, array('premium')); |
| 219 |
$premium_types[] = $types[$type]; |
$premium_types[] = $types[$type]; |
| 220 |
} |
} |
| 221 |
variable_set('node_options_' . $type, $node_options); |
variable_set('node_options_'. $type, $node_options); |
| 222 |
} |
} |
| 223 |
|
|
| 224 |
if ($form_values['premium_bulk_update']) { |
if ($form_state['values']['premium_bulk_update']) { |
| 225 |
db_query("DELETE from {premium}"); |
db_query("DELETE from {premium}"); |
| 226 |
|
|
|
$start = $end = 0; |
|
|
_premium_offset(0, $start, $end, $mode, $count, $unit); |
|
|
|
|
| 227 |
foreach ($premium_types as $type) { |
foreach ($premium_types as $type) { |
| 228 |
|
$start = $end = 0; |
| 229 |
|
_premium_offset(0, $start, $end, $mode, $count, $unit); |
| 230 |
// Apply the timestamp delta's to the node's created date. |
// Apply the timestamp delta's to the node's created date. |
| 231 |
if ($start) $start = 'created + '. (int) $start; |
if ($start) $start = 'created + '. (int) $start; |
| 232 |
if ($end) $end = 'created + '. (int) $end; |
if ($end) $end = 'created + '. (int) $end; |
| 323 |
} |
} |
| 324 |
|
|
| 325 |
/** |
/** |
| 326 |
|
* Implementation of hook_theme(). |
| 327 |
|
*/ |
| 328 |
|
function premium_theme() { |
| 329 |
|
return array( |
| 330 |
|
'premium_body' => array( |
| 331 |
|
'arguments' => array('node' => NULL), |
| 332 |
|
), |
| 333 |
|
); |
| 334 |
|
} |
| 335 |
|
|
| 336 |
|
/** |
| 337 |
* Reformat the message body with a premium content message |
* Reformat the message body with a premium content message |
| 338 |
*/ |
*/ |
| 339 |
function theme_premium_body($node) { |
function theme_premium_body($node) { |
| 340 |
return check_markup($node->teaser, $node->format, false) .'<div class="premium-message">'. check_markup(variable_get('premium_message', t('Full text available to premium subscribers only')), variable_get('premium_format', FILTER_FORMAT_DEFAULT), false) .'</div>'; |
return check_markup($node->teaser, $node->format, FALSE) .'<div class="premium-message">'. check_markup(variable_get('premium_message', t('Full text available to premium subscribers only')), variable_get('premium_format', FILTER_FORMAT_DEFAULT), FALSE) .'</div>'; |
| 341 |
} |
} |