You are here

function nodeorder_order_nodes in Node Order 5

1 string reference to 'nodeorder_order_nodes'
nodeorder_menu in ./nodeorder.module
Implementation of hook_menu().

File

./nodeorder.module, line 293

Code

function nodeorder_order_nodes($tid) {
  drupal_add_js(drupal_get_path('module', 'nodeorder') . '/nodeorder.js');
  $nodeorder_css = drupal_get_path('module', 'nodeorder') . '/nodeorder.css';
  drupal_add_css($nodeorder_css);
  $nids = array();
  $items = array();
  $order = 'n.sticky DESC, tn0.weight_in_tid';

  // Send 0 in as the count to nodeorder_select_nodes, which tells
  // it to put all the nodes (as opposed to some preconfigured number
  // of nodes) in the result set...
  $list_div_id = 'node-sort-list-' . $tid;
  $count = 0;
  $result = nodeorder_select_nodes(array(
    $tid,
  ), 'and', 0, FALSE, $order, $count);
  if (db_num_rows($result) > 0) {
    $output = "<div id=\"{$list_div_id}\" class=\"node-sort-list\">\n";
    while ($node = db_fetch_object($result)) {
      $loaded_node = node_load($node->nid);
      $node_div_id = $list_div_id . '_' . $node->nid;
      $output .= "<div id=\"{$node_div_id}\" class=\"sort-wrapper\">\n";
      $title_only_while_ordering = variable_get('nodeorder_minimal_ordering_text', 0);
      if ($title_only_while_ordering) {
        $output .= "<div class=\"node node-shell\"><h2>" . l($loaded_node->title, "node/{$loaded_node->nid}") . "</h2></div>\n";
      }
      else {
        $output .= node_view($loaded_node, 1);
      }
      $output .= "</div>\n";
      $nids[] = $node->nid;
      $weights[] = $loaded_node->nodeorder[$tid];
    }
    $output .= "\n</div>\n";

    // Set up the page with the initial values (nids) and weights.  When a node gets
    // dragged & dropped, we'll update the initialValues with the new node order.  The
    // weights will stay the same since our algorithm just swaps weights around within
    // the tid...
    //
    // In Drupal 5.x with the port from Spajax to Prototype, the format of these variables
    // has changed from arrays to comma-separated strings.
    drupal_set_html_head('<script type="text/javascript">var initialValues = "' . implode(',', $nids) . '";</script>');
    drupal_set_html_head('<script type="text/javascript">var initialWeights = "' . implode(',', $weights) . '";</script>');
    $callback = url('nodeorder/reordered', NULL, NULL, TRUE);
    $onchange = <<<EOT
      function (obj) {
        var serial = \$.SortSerialize(obj[0].id);
        newValues = nodeorder_fix_values(serial.hash, '{<span class="php-variable">$list_div_id</span>}');
        \$.ajax(
          {
            type: "POST",
            url: "{<span class="php-variable">$callback</span>}",
            data: 'new-values='+newValues+'&initial-values='+initialValues+'&initial-weights='+initialWeights+'&tid='+{<span class="php-variable">$tid</span>}+'&list-id={<span class="php-variable">$list_div_id</span>}'
          }
        );
        initialValues = newValues;
      }
EOT;

    // Tell the Sortable that the child elements are DIVs with class="sort-wrapper"...
    $options = array(
      'accept' => 'sort-wrapper',
      'helperclass' => 'sortable-helper',
      'axis' => 'vertically',
      'handle' => 'h2',
      '#onchange' => $onchange,
    );
    interface_sortable_element($list_div_id, $options);
  }
  $term = taxonomy_get_term($tid);
  drupal_set_title(t('Set the order of nodes in ') . $term->name);
  return $output;
}