View source
<?php
define('NODE_NOT_PREMIUM', 0);
define('NODE_PREMIUM', 1);
function nopremium_form_node_type_form_alter(&$form, &$form_state) {
if (isset($form['workflow']['node_options']['#options'])) {
$form['workflow']['node_options']['#options']['premium'] = t('Premium content');
}
}
function nopremium_node_operations() {
return array(
'premium' => array(
'label' => t('Make premium'),
'callback' => 'node_mass_update',
'callback arguments' => array(
'updates' => array(
'status' => NODE_PUBLISHED,
'premium' => NODE_PREMIUM,
),
),
),
'non_premium' => array(
'label' => t('Make non-premium'),
'callback' => 'node_mass_update',
'callback arguments' => array(
'updates' => array(
'premium' => NODE_NOT_PREMIUM,
),
),
),
);
}
function nopremium_action_info() {
return array(
'node_make_premium_action' => array(
'type' => 'node',
'label' => t('Make content premium'),
'configurable' => FALSE,
'behavior' => array(
'changes_property',
),
'triggers' => array(
'node_presave',
'comment_insert',
'comment_update',
'comment_delete',
),
),
'node_make_non_premium_action' => array(
'type' => 'node',
'label' => t('Make content non-premium'),
'configurable' => FALSE,
'behavior' => array(
'changes_property',
),
'triggers' => array(
'node_presave',
'comment_insert',
'comment_update',
'comment_delete',
),
),
);
}
function node_make_premium_action($node, $context = array()) {
$node->premium = NODE_PREMIUM;
watchdog('action', 'Set @type %title to premium.', array(
'@type' => node_type_get_name($node),
'%title' => $node->title,
));
}
function node_make_non_premium_action($node, $context = array()) {
$node->premium = NODE_NOT_PREMIUM;
watchdog('action', 'Set @type %title to non-premium.', array(
'@type' => node_type_get_name($node),
'%title' => $node->title,
));
}
function nopremium_theme() {
return array(
'nopremium_message' => array(
'variables' => array(
'node' => NULL,
),
),
);
}
function nopremium_node_view($node, $view_mode, $langcode) {
if ($node->premium && !nopremium_access_full_content($node)) {
$node->content['nopremium_message'] = array(
'#markup' => theme('nopremium_message', array(
'node' => $node,
)),
);
}
}
function nopremium_node_view_alter(&$build) {
$node = $build['#node'];
if ($build['#view_mode'] != 'teaser' && $build['#view_mode'] != 'search_index' && $node->premium && !nopremium_access_full_content($node)) {
nopremium_alter_node($build);
}
}
function nopremium_node_prepare($node) {
if (!isset($node->nid) || isset($node->is_new)) {
$node_options = variable_get('node_options_' . $node->type, array());
if (!isset($node->premium)) {
$node->premium = (int) in_array('premium', $node_options);
}
}
}
function nopremium_form_node_form_alter(&$form, &$form_state, $form_id) {
$node = $form_state['node'];
$form['options']['premium'] = array(
'#type' => 'checkbox',
'#title' => t('Premium content'),
'#default_value' => $node->premium,
);
if (!user_access('administer nodes')) {
if (user_access('override premium option of any content type') || user_access('override ' . $node->type . ' premium option')) {
foreach (element_children($form['options']) as $key) {
if (!isset($form['options'][$key]['#access'])) {
$form['options'][$key]['#access'] = FALSE;
}
}
$form['options']['#access'] = $form['options']['premium']['#access'] = TRUE;
}
else {
$form['options']['premium']['#access'] = FALSE;
}
}
}
function nopremium_views_api() {
return array(
'api' => 2,
);
}
function nopremium_permission() {
$perms['view full premium content of any type'] = array(
'title' => t('View full premium content of any type'),
);
$perms['override premium option of any content type'] = array(
'title' => t('Override premium option of any content type'),
);
foreach (node_type_get_types() as $type => $info) {
$perms['view full ' . $type . ' premium content'] = array(
'title' => t('%type_name: View full premium content', array(
'%type_name' => $info->name,
)),
);
$perms['override ' . $type . ' premium option'] = array(
'title' => t('%type_name: Override premium option', array(
'%type_name' => $info->name,
)),
);
}
return $perms;
}
function nopremium_menu() {
$items['admin/config/workflow/nopremium'] = array(
'title' => 'Node Option Premium',
'description' => 'Configure Node Option Premium.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'nopremium_settings_form',
),
'access arguments' => array(
'administer permissions',
),
'file' => 'nopremium.admin.inc',
);
return $items;
}
function nopremium_field_extra_fields() {
$extra = array();
foreach (node_type_get_types() as $type) {
$extra['node'][$type->type] = array(
'display' => array(
'nopremium_message' => array(
'label' => t('Premium message'),
'description' => t('Node Option Premium module message.'),
'weight' => 1,
),
),
);
}
return $extra;
}
function nopremium_file_download($uri, $field_type = 'file') {
global $user;
$files = file_load_multiple(array(), array(
'uri' => $uri,
));
if (count($files)) {
foreach ($files as $item) {
if ($item->uri === $uri) {
$file = $item;
break;
}
}
}
if (!isset($file)) {
return;
}
$references = file_get_file_references($file, NULL, FIELD_LOAD_CURRENT, $field_type);
if (empty($references) && ($file->status == FILE_STATUS_PERMANENT || $file->uid != $user->uid)) {
return;
}
foreach ($references as $field_name => $field_references) {
if (!empty($field_references['node'])) {
foreach ($field_references['node'] as $id => $reference) {
$entity = entity_load('node', array(
$id,
));
$entity = reset($entity);
$field = field_info_field($field_name);
if ($entity && $entity->premium) {
$field_items = field_get_items('node', $entity, $field_name);
foreach ($field_items as $item) {
if ($item['uri'] == $uri) {
if (!nopremium_access_full_content($entity)) {
return -1;
}
break;
}
}
}
}
}
}
}
function theme_nopremium_message($variables) {
return '<div class="nopremium-message">' . check_markup(t(nopremium_get_message($variables['node']->type))) . '</div>';
}
function nopremium_access_full_content($node, $account = NULL) {
if (empty($account)) {
$account = $GLOBALS['user'];
}
return user_access('administer nodes', $account) || user_access('view full premium content of any type', $account) || user_access('view full ' . $node->type . ' premium content', $account) || node_access('update', $node, $account) || $account->uid && $account->uid == $node->uid;
}
function nopremium_alter_node(&$build) {
$view_mode = $build['#view_mode'];
if ($view_mode != 'teaser') {
$node = $build['#node'];
$langcode = $build['#language'];
node_build_content($node, 'teaser', $langcode);
unset($node->content['links']['node']['#links']['node-readmore']);
$build = $node->content;
unset($node->content);
$build += array(
'#theme' => 'node',
'#node' => $node,
'#view_mode' => 'teaser',
'#language' => $langcode,
);
if (!empty($node->nid) && !($view_mode == 'full' && node_is_page($node))) {
$build['#contextual_links']['node'] = array(
'node',
array(
$node->nid,
),
);
}
$type = 'node';
drupal_alter(array(
'node_view',
'entity_view',
), $build, $type);
if (isset($build['#pre_render'])) {
$key = array_search('_field_extra_fields_pre_render', $build['#pre_render']);
if ($key !== FALSE) {
$build['#pre_render'][$key] = '_nopremium_field_extra_fields_pre_render';
}
}
$build['#view_mode'] = $view_mode;
}
}
function _nopremium_field_extra_fields_pre_render($elements) {
if (isset($elements['#view_mode'])) {
$view_mode = $elements['#view_mode'];
$elements['#view_mode'] = 'teaser';
$elements = _field_extra_fields_pre_render($elements);
$elements['#view_mode'] = $view_mode;
}
return $elements;
}
function nopremium_get_message($type = '') {
if ($type) {
$message = variable_get('nopremium_message_' . $type, '');
if ($message) {
return $message;
}
}
return variable_get('nopremium_message', 'The full content of this page is available to premium users only.');
}