All pastes #956940 Raw Edit

Something

public text v1 · immutable
#956940 ·published 2008-03-25 18:02 UTC
rendered paste body
<?php

$taxTree = array();
$termNames = array();
$vid = 1;

function taxelect_perl() {
	return array('access taxelect content');
}

function taxelect_block($op='list', $delta=0) {
  global $vid;

  // listing of blocks, such as on the admin/block page
  if ($op == "list") {
    $block[0]["info"] = t("Taxelect");
    return $block;
  }
  else if ($op == 'view') {
    createTermNames();
    createTaxTree(0);

    drupal_add_css('/modules/taxelect/taxelect.css', 'module', 'all', TRUE);

    $block_content = createContent();

    if ($block_content == '') {
      return;
    }

    // set up the block
    $block['subject'] = 'Taxelect';
    $block['content'] = $block_content;
    return $block;
  }
}

/******************************
 * CREATECONTENT
 *
 * Generate the HTML for the select lists
 */
function createContent() {
  global $taxTree;
  global $termNames;

  $block_content .= createJs();
  $block_content .= "<ul class='taxelect'>";
  $block_content .= "<li class='taxelect'><select id='select1' onchange='updateTaxelect(1);' class='taxelect'>\n";
  $block_content .= "<option class='taxelect' value=''>-----------------------------------</option>";
  foreach ($taxTree[0] as $termId) {
    $block_content .= "<option class='taxelect' value='$termId'>$termNames[$termId]</option>";
  }
  $block_content .= "</select></li>\n";

  $block_content .= "<li class='taxelect'><select id='select2' onchange='updateTaxelect(2);' class='taxelect'>\n";
  $block_content .= "<option class='taxelect' value=''>-----------------------------------</option>";
  $block_content .= "<option  class='taxelect'> </option>";
  $block_content .= "</select></li>\n";

  $block_content .= "<li class='taxelect'><select id='select3' onchange='updateTaxelect(3);' class='taxelect'>\n";
  $block_content .= "<option class='taxelect' value=''>-----------------------------------</option>";
  $block_content .= "<option  class='taxelect'> </option>";
  $block_content .= "</select></li>\n";

  $block_content .= "<li class='taxelect'><select id='select4' class='taxelect'>\n";
  $block_content .= "<option class='taxelect' value=''>-----------------------------------</option>";
  $block_content .= "<option  class='taxelect'> </option>";
  $block_content .= "</select></li>\n";

  $block_content .= "<li class='taxelect taxelect-button-li'><br>\n";
  $block_content .= "<button class='taxelect' onclick='doSearch();'>Find Documents</button>\n";
  $block_content .= "</li>\n";

  $block_content .= "</ul>\n";


  return $block_content;
}


/******************************
 * CREATEJS
 *
 * Create JavaScript to auto update select lists and 'Find Documents' button URL
 */
function createJS() {
  global $base_url;
  global $vid;
  global $taxTree;
  global $termNames;

  $jsTermNames .= "var termNames = new Array();\n";
  foreach ($termNames as $id => $name) {
    $jsTermNames .= "termNames[$id] = '$name'\n";
  }

  $jsTaxTree .= "var taxTree = new Array();\n";
  foreach ($taxTree as $pidx => $pid) {
    $jsTaxTree .= "taxTree[$pidx] = new Array();\n";
    foreach ($taxTree[$pidx] as $cidx => $cid) {
      $jsTaxTree .= "taxTree[$pidx][$cidx] = $cid;\n";
    }
  }

  $js = <<< EOT
    <script type="text/javascript">
      $jsTermNames
      $jsTaxTree
      function updateTaxelect(parentNum) {
        var childNum = parentNum + 1;
        var parent = document.getElementById('select'+parentNum);
        var child = document.getElementById('select'+childNum);
        var clearChild = childNum + 1;
        while (clearChild < 5) {
          clearSelect(document.getElementById('select'+clearChild), 1);
          clearChild++;
        }


        var pid = parent.options[parent.selectedIndex].value;
        var i = 0;
        child.options[0] = new Option('-----------------------------------','');
        while(taxTree[pid] != undefined && taxTree[pid][i] != undefined) {
          child.options[i+1] = new Option(termNames[taxTree[pid][i]],taxTree[pid][i]);
          i++;
        }
        clearSelect(child, i+1);
      }
  
      function clearSelect(list, from) {
        while(list.options[from] != undefined) {
          list.remove(from);
        }
      }

      /*
       * 
       */
      function doSearch() {
        var url = '$base_url?q=topics/$vid';
        var sel;
        var id = 0;
        var i=1;
        while(i<5 && id != undefined) {
          sel = document.getElementById('select'+i);
          id = sel.options[sel.selectedIndex].value;
          if (id != null && id != '' && id != undefined) url += '/'+id;
          i++;
        }
        location.href=url;
      }

    </script>
EOT;

  return $js;
}

/******************************
 * CREATETAXTREE
 *
 * Create an flattened array-based of the term_hierarchy table
 */
function createTaxTree($termId) {
  global $taxTree;

  $query = "SELECT th.tid FROM term_hierarchy th WHERE th.parent=%d";
  $queryResult =  db_query($query, $termId);
  while ($row = db_fetch_object($queryResult)) {
    $taxTree[$termId][] = $row->tid;
    if ($taxTree[$row->tid] == null) {
      createTaxTree($row->tid);
    }
  }
}

/******************************
 * CREATETERMNAMES
 *
 * Create an array-based version of the term_hierarchy table
 */
function createTermNames() {
  global $termNames;

  $query = "SELECT td.tid,td.name FROM term_data td WHERE td.vid=%d";
  $queryResult =  db_query($query, 1);
  while ($row = db_fetch_object($queryResult)) {
    $termNames[$row->tid] = $row->name;
  }
}