You are here

function weight_node_selector in Weight 5

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

Generate JS code for selecting individual node weights on admin page

1 call to weight_node_selector()
weight_form_alter in ./weight.module
Implementation of hook_form_alter().

File

./weight.module, line 361

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 = "\n" . "<form><select style=\"margin: 0;\"\n    onchange='httpRequest(\"GET\", \"" . base_path() . "admin/node/weight/_change_weight/\" + [NID] + \"/\" +\n    this.options[this.selectedIndex].value,true)' >";

  //  $sticky = db_result(db_query('SELECT sticky FROM {node} WHERE nid = %d', $nid));
  $node = node_load($nid);

  // Convert to our weight range.
  //  _encoded_sticky2weight($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 = 0 - $weight_range; $i <= $weight_range; $i++) {
    $selector_template .= "<option value='{$i}'>{$i}</option>";
  }
  $selector_template .= '</select></form>';
  $weight_selector = $selector_template;

  // TODO: need node weight to be able to choose this!
  $weight_selector = preg_replace("/(value='{$weight}')/", "\$1 selected='selected'", $weight_selector);
  $weight_selector = preg_replace("/\\[NID\\]/", $nid, $weight_selector);
  $weight_selector = '<div class="weight-selector">' . $weight_selector . '</div>';
  $status = 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' => $weight_selector,
    'status' => $status,
  );
}