You are here

function weight_node_selector in Weight 7

Same name and namespace in other branches
  1. 5 weight.module \weight_node_selector()

Generate JS code for selecting individual node weights on admin page

1 call to weight_node_selector()
theme_weight_node_admin_nodes in ./weight.module
Theme node administration overview. Mostly copied from node.admin.inc.

File

./weight.module, line 184
This module uses the sticky column of the node table to add weighting to nodes.

Code

function weight_node_selector($nid) {
  static $js_included;
  if (!$js_included) {
    $path = drupal_get_path('module', 'weight');
    drupal_add_js($path . '/httpRequest.js', 'module', 'header', TRUE);
    $js_included = TRUE;
    drupal_add_css($path . '/weight.css');
  }
  $selector_template = "<select style=\"margin: 0;\"\n    onchange='httpRequest(\"GET\", \"" . base_path() . "?q=admin/node/weight/_weight_change/\" + [NID] + \"/\" +\n    this.options[this.selectedIndex].value,true)' >";

  // Get more stuff about the node.
  $node = db_query("SELECT nid, `status`, sticky, promote, translate, moderate FROM {node} WHERE nid = :nid", array(
    ':nid' => $nid,
  ))
    ->fetchObject();

  // Convert to our weight range.
  _weight_decode($node);
  $weight = $node->node_weight;

  // Ugly bit of javascript we use for each dropdown to submit weight changes
  // in the background. Relies on even uglier httpRequest.js file that comes
  // with this module. Ironically, Ajax makes me feel dirty.
  $weight_range = variable_get('weight_range', 20);
  for ($i = -$weight_range; $i <= $weight_range; ++$i) {
    $selector_template .= "<option value='{$i}'" . ($i == $node->node_weight ? " selected='selected'" : '') . ">{$i}</option>";
  }
  $selector_template .= '</select>';
  $weight_selector = str_replace('[NID]', $nid, $selector_template);
  $status = $node->status ? t('published') : NULL;
  $status .= $node->sticky ? '<br />' . t('sticky') : NULL;
  $status .= $node->promote ? '<br />' . t('promoted') : NULL;
  $status .= $node->translate ? '<br />' . t('translate') : NULL;
  $status .= $node->moderate ? '<br />' . t('moderated') : NULL;
  return array(
    'selector' => '<div class="weight-selector">' . $weight_selector . '</div>',
    'status' => $status,
  );
}