View source
<?php
module_load_include('admin.inc', 'fasttoggle');
module_load_include('inc', 'fasttoggle');
function fasttoggle_node_permission() {
return array(
'promote posts' => array(
'title' => t('Promote posts to front page'),
),
'make posts sticky' => array(
'title' => t('Make posts sticky'),
),
'moderate posts' => array(
'title' => t('Moderate posts'),
),
'promote own posts' => array(
'title' => t('Promote own posts to front page'),
),
'make own posts sticky' => array(
'title' => t('Make own posts sticky'),
),
'moderate own posts' => array(
'title' => t('Moderate own posts'),
),
);
}
function fasttoggle_node_save(array $options, $group, $instance, $new_value, $object) {
if ($new_value === '[unset]') {
unset($object->{$instance});
}
else {
$object->{$instance} = $new_value;
}
node_save($object);
}
function fasttoggle_node_get_field_value(array $options, $group, $instance, $object) {
return $object->{$instance};
}
function fasttoggle_node_status_access($obj) {
global $user;
return fasttoggle_allow_access_if(user_access("override {$obj->type} published option") || user_access('moderate posts') || user_access('moderate own posts') && $obj->uid == $user->uid);
}
function fasttoggle_node_sticky_access($obj) {
global $user;
return fasttoggle_allow_access_if(user_access("override {$obj->type} sticky option") || user_access('make posts sticky') || user_access('make own posts sticky') && $obj->uid == $user->uid);
}
function fasttoggle_node_promote_access($obj) {
global $user;
return fasttoggle_allow_access_if(user_access("override {$obj->type} promote option") || user_access('promote posts') || user_access('promote own posts') && $obj->uid == $user->uid);
}
function fasttoggle_node_edit_access($obj) {
return fasttoggle_deny_access_if(!node_access('update', $obj));
}
function fasttoggle_node_fasttoggle_available_links($type = NULL, $obj = NULL) {
if (!is_null($type) && $type != 'node') {
return array();
}
$result = array(
'node' => array(
'id_field' => 'nid',
'title_field' => 'title',
'save_fn' => 'fasttoggle_node_save',
'object_type' => 'node',
'subtype_field' => 'type',
'access' => array(
'fasttoggle_node_edit_access',
),
'global_settings_desc' => t('In addition to these global settings, you need to enable fasttoggle in the settings page for each content type you wish to use.'),
'nodetype_settings_desc' => t('In addition to these settings, you need to enable the sitewide setting for toggles you want to use.'),
'extra_settings' => array(
'help_text' => array(
'#value' => t('Configure access restrictions for these settings on the <a href="@url">access control</a> page.', array(
'@url' => url('admin/user/permissions', array(
'fragment' => 'module-fasttoggle',
)),
)),
'#prefix' => '<div>',
'#suffix' => '</div>',
),
'fasttoggle_enhance_node_overview_page' => array(
'#type' => 'checkbox',
'#title' => t('Add published/unpublished toggle links to the node overview page.'),
'#default_value' => variable_get('fasttoggle_enhance_node_overview_page', TRUE),
'#weight' => 50,
),
),
'fields' => array(
'status' => array(
'display' => array(
'node_links',
),
'instances' => array(
'status' => array(
'description' => t('Status <small>(published/unpublished)</small>'),
'access' => array(
'fasttoggle_node_status_access',
),
'labels' => array(
FASTTOGGLE_LABEL_ACTION => array(
0 => t('publish'),
1 => t('unpublish'),
),
FASTTOGGLE_LABEL_STATUS => array(
0 => t('not published'),
1 => t('published'),
),
),
),
'sticky' => array(
'description' => t('Sticky <small>(stays at the top of listings)</small>'),
'access' => array(
'fasttoggle_node_sticky_access',
),
'labels' => array(
FASTTOGGLE_LABEL_ACTION => array(
0 => t('make sticky'),
1 => t('make unsticky'),
),
FASTTOGGLE_LABEL_STATUS => array(
0 => t('not sticky'),
1 => t('sticky'),
),
),
),
'promote' => array(
'description' => t('Promoted <small>(visible on the front page)</small>'),
'access' => array(
'fasttoggle_node_promote_access',
),
'labels' => array(
FASTTOGGLE_LABEL_ACTION => array(
0 => t('promote'),
1 => t('demote'),
),
FASTTOGGLE_LABEL_STATUS => array(
0 => t('not promoted'),
1 => t('promoted'),
),
),
),
),
),
),
),
);
return $result;
}
function fasttoggle_node_form_alter(&$form, $form_state, $form_id) {
switch ($form_id) {
case 'node_admin_content':
if (variable_get('fasttoggle_enhance_node_overview_page', 0) && isset($form['admin']['nodes']['#options']) && user_access('moderate posts')) {
foreach ($form['admin']['nodes']['#options'] as $key => $detail) {
$node = node_load($key);
$status = intval($detail['status'] == t('published'));
$toggle_data = fasttoggle_get_allowed_links('node', $node, $node->nid, 'fasttoggle_togglable_options');
if (!empty($toggle_data['fields']['status']['instances']['status'])) {
$form['admin']['nodes']['#options'][$key]['status'] = fasttoggle($toggle_data, 'status', 'status', $node, FASTTOGGLE_FORMAT_HTML);
}
}
}
break;
}
}
function fasttoggle_node_form_node_type_form_alter(&$form, $form_state) {
$type = $form['#node_type']->type;
$form['fasttoggle'] = array(
'#type' => 'fieldset',
'#title' => t('Fasttoggle settings'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#group' => 'additional_settings',
);
$settings = array(
"fasttoggle_add_to_node_links",
"fasttoggle_togglable_options",
);
foreach ($settings as $setting) {
$config = array(
'#title' => $setting == "fasttoggle_add_to_node_links" ? "Toggles available when viewing nodes." : "Toggles available elsewhere",
'setting' => $setting,
'type' => 'node',
'subtype' => $type,
'write_key' => "{$setting}_node",
);
$form['fasttoggle']["{$setting}_items"] = fasttoggle_get_settings_form_items($config);
}
}
function fasttoggle_node_node_view($node, $view_mode) {
if ($view_mode == 'rss') {
return;
}
$options = fasttoggle_get_allowed_links('node', $node, $node->nid, "fasttoggle_add_to_node_links");
if (!empty($options['fields'])) {
foreach ($options['fields'] as $group => $flags) {
if ($group == $node->type) {
continue;
}
if (!empty($flags['instances'])) {
foreach ($flags['instances'] as $key => $data) {
$node->content['links']['node']['#links']['fasttoggle_' . $group . '_' . $key] = fasttoggle($options, $group, $key, $node, FASTTOGGLE_FORMAT_LINK_ARRAY, $view_mode);
fasttoggle_block_link_register(fasttoggle($options, $group, $key, $node, FASTTOGGLE_FORMAT_HTML, $view_mode), $node->nid, $key, $group);
}
}
}
}
}
function fasttoggle_node_menu_contextual_links_alter(array &$links, array $router_item, $root_path) {
if ($root_path != 'node/%' || !isset($router_item['map'][0]) || $router_item['map'][0] != 'node' || !isset($router_item['map'][1]) || !is_object($router_item['map'][1])) {
return;
}
$node = $router_item['map'][1];
$options = fasttoggle_get_allowed_links('node', $node, $node->nid, 'fasttoggle_add_to_node_links');
if (!empty($options['fields'])) {
foreach ($options['fields'] as $group => $flags) {
if (!empty($flags['instances'])) {
foreach ($flags['instances'] as $key => $data) {
$link = fasttoggle($options, $group, $key, $node, FASTTOGGLE_FORMAT_LINK_ARRAY);
$link['localized_options']['query'] = $link['query'];
unset($link['query']);
$link['localized_options']['attributes'] = $link['attributes'];
unset($link['attributes']);
$links['fasttoggle_' . $group . '_' . $key] = $link;
}
}
}
}
}
function fasttoggle_node_views_api() {
return array(
'api' => 3,
'path' => drupal_get_path('module', 'fasttoggle_node') . '/views',
);
}
function fasttoggle_node_preprocess_node(array &$variables) {
$variables['classes_array'][] = 'node-content-' . $variables['node']->nid;
}
function fasttoggle_node_fasttoggle_ajax_alter(array &$ajax_commands, $object_type, $object, array $params) {
if ($object_type != "node") {
return;
}
if (!isset($params['view'])) {
$params['view'] = "full";
}
$unrendered = node_view($object, $params['view']);
if ($object->comment > 0 && module_exists('comment') && $params['view'] == 'full') {
$unrendered['comments'] = comment_node_page_additions($object);
}
$replacement_content = drupal_render($unrendered);
$ajax_commands[] = ajax_command_replace('.' . 'node-content-' . $object->nid, $replacement_content);
}
function fasttoggle_node_node_type_delete($type_info) {
variable_del('fasttoggle_add_to_node_links_' . $type_info->type);
variable_del('fasttoggle_togglable_options_' . $type_info->type);
}