You are here

fasttoggle_node.module in Fasttoggle 7

File

module/fasttoggle_node/fasttoggle_node.module
View source
<?php

/**
 * @file
 * Fasttoggle node attribute support.
 */
module_load_include('admin.inc', 'fasttoggle');
module_load_include('inc', 'fasttoggle');

/**
 * Implements hook_perm().
 */
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'),
    ),
  );
}

/**
 * Modify a node attribute and save the node.
 *
 * @param array $options
 *   The array of setting options.
 * @param string $group
 *   The setting group.
 * @param string $instance
 *   The instance of the setting.
 * @param mixed $new_value
 *   The new value to be set (or '[unset'] to delete the attribute.
 * @param object $object
 *   The node to be saved.
 */
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);
}

/**
 * Get the value of a field.
 *
 * @param array $options
 *   The array of options.
 * @param string $group
 *   The setting group.
 * @param string $instance
 *   The setting instance.
 * @param object $object
 *   The object from which the attribute should be returned.
 *
 * @return mixed
 *   The value.
 */
function fasttoggle_node_get_field_value(array $options, $group, $instance, $object) {
  return $object->{$instance};
}

/**
 * Check access to publish/unpublish a node.
 *
 * @param object $obj
 *   Object for which access should be checked.
 *
 * @return int
 *   Return tristate access result.
 */
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);
}

/**
 * Check access to modify stickiness of a node.
 *
 * @param object $obj
 *   Object for which access should be checked.
 *
 * @return int
 *   Return tristate access result.
 */
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);
}

/**
 * Check promote access to a node.
 *
 * @param object $obj
 *   Object for which access should be checked.
 *
 * @return int
 *   Return tristate access result.
 */
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);
}

/**
 * Check edit access to a node.
 *
 * @param object $obj
 *   Object for which access should be checked.
 *
 * @return int
 *   Return tristate access result.
 */
function fasttoggle_node_edit_access($obj) {
  return fasttoggle_deny_access_if(!node_access('update', $obj));
}

/**
 * Implements hook_fasttoggle_labels().
 */
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;
}

/**
 * Implements hook_form_alter().
 */
function fasttoggle_node_form_alter(&$form, $form_state, $form_id) {
  switch ($form_id) {
    case 'node_admin_content':

      // Add published/unpublished toggle links to the node overview page.
      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;
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Adds Fasttoggle settings to the node type form.
 */
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);
  }
}

/**
 * Implements hook_node_view_alter().
 */
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);
        }
      }
    }
  }
}

/**
 * Implements hook_menu_contextual_links_alter().
 */
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);

          // Massage to $link so it fits the expected format.
          $link['localized_options']['query'] = $link['query'];
          unset($link['query']);
          $link['localized_options']['attributes'] = $link['attributes'];
          unset($link['attributes']);
          $links['fasttoggle_' . $group . '_' . $key] = $link;
        }
      }
    }
  }
}

/**
 * Implements hook_views_api().
 */
function fasttoggle_node_views_api() {
  return array(
    'api' => 3,
    'path' => drupal_get_path('module', 'fasttoggle_node') . '/views',
  );
}

/**
 * Implements hook_preprocess_node().
 */
function fasttoggle_node_preprocess_node(array &$variables) {
  $variables['classes_array'][] = 'node-content-' . $variables['node']->nid;
}

/**
 * Alter the ajax commands being returned to the browser.
 *
 * @param array $ajax_commands
 *   The array of ajax commands being returned.
 * @param string $object_type
 *   The type of object being modified.
 * @param object $object
 *   The object instance.
 * @param array $params
 *   Additional parameters.
 */
function fasttoggle_node_fasttoggle_ajax_alter(array &$ajax_commands, $object_type, $object, array $params) {
  if ($object_type != "node") {
    return;
  }

  // If we don't know what view was used (fields don't), give our best guess.
  if (!isset($params['view'])) {
    $params['view'] = "full";
  }

  // Replace the original content with that the updated content, so far as we're
  // able (we don't get it exactly right yet).
  $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);
}

/**
 * Delete variables related to a node type that is being deleted.
 *
 * @param string $type_info
 *   The machine name of the node type being deleted.
 */
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);
}

Functions

Namesort descending Description
fasttoggle_node_edit_access Check edit access to a node.
fasttoggle_node_fasttoggle_ajax_alter Alter the ajax commands being returned to the browser.
fasttoggle_node_fasttoggle_available_links Implements hook_fasttoggle_labels().
fasttoggle_node_form_alter Implements hook_form_alter().
fasttoggle_node_form_node_type_form_alter Implements hook_form_FORM_ID_alter().
fasttoggle_node_get_field_value Get the value of a field.
fasttoggle_node_menu_contextual_links_alter Implements hook_menu_contextual_links_alter().
fasttoggle_node_node_type_delete Delete variables related to a node type that is being deleted.
fasttoggle_node_node_view Implements hook_node_view_alter().
fasttoggle_node_permission Implements hook_perm().
fasttoggle_node_preprocess_node Implements hook_preprocess_node().
fasttoggle_node_promote_access Check promote access to a node.
fasttoggle_node_save Modify a node attribute and save the node.
fasttoggle_node_status_access Check access to publish/unpublish a node.
fasttoggle_node_sticky_access Check access to modify stickiness of a node.
fasttoggle_node_views_api Implements hook_views_api().