function tripal_cv_get_term_children

1.x trees.inc tripal_cv_get_term_children($cvterm_id, $cnt_table = NULL, $fk_column = NULL, $cnt_column = NULL, $filter = NULL, $label = NULL)

Generates SON needed for jsTree -expanding a term to view children

This function returns the JSON array for the jsTree jQuery code when expanding a term to view it's children.

Related topics

1 call to tripal_cv_get_term_children()
tripal_cv_update_tree in tripal_cv/includes/trees.inc

File

tripal_cv/includes/trees.inc, line 247
@todo Stephen describe this file

Code

function tripal_cv_get_term_children($cvterm_id, $cnt_table = NULL, 
$fk_column = NULL, $cnt_column = NULL, $filter = NULL, $label = NULL) {

  // get the children for the term provided
  $sql = "
    SELECT CVTR.cvterm_relationship_id,CVTR.subject_id,
       CVT1.name as subject_name, CVT3.name as type_name, CVTR.type_id,
       CVT2.name as object_name,CVTR.object_id
    FROM {cvterm_relationship} CVTR
       INNER JOIN {CVTerm} CVT1 on CVTR.subject_id = CVT1.cvterm_id
       INNER JOIN {CVTerm} CVT2 on CVTR.object_id = CVT2.cvterm_id
       INNER JOIN {CVTerm} CVT3 on CVTR.type_id = CVT3.cvterm_id
       INNER JOIN {CV} on CV.cv_id = CVT1.cv_id
    WHERE CVTR.object_id = %d
    ORDER BY CVT1.name
  ";
  $results = chado_query($sql, $cvterm_id);

  // prepare the SQL statement that will allow us to pull out count
  // information for each term in the tree.
  if ($cnt_table) {
    if (!$filter) {
      $filter = '(1=1)';
    }
    $cnt_sql = "
       SELECT CVT.name, CVT.cvterm_id, CNT.$cnt_column as num_items
       FROM {$cnt_table} CNT
        INNER JOIN {cvterm} CVT on CNT.$fk_column = CVT.cvterm_id
       WHERE $filter AND CVT.cvterm_id = %d
       ORDER BY $cnt_column desc
    ";
  }

  // populate the JSON content array
  while ($term = db_fetch_object($results)) {
    // count the number of items per term if requested
    $name = $term->subject_name;
    $count = 0;
    if ($cnt_table) {
      $cnt_results = chado_query($cnt_sql, $term->subject_id);
      while ($cnt = db_fetch_object($cnt_results)) {
        $count += $cnt->num_items;
      }
      if ($count > 0) {
        $name .= " (" . number_format($count) . " $label)";

        // check if we have any children if so then set the value
        $children = db_fetch_object(chado_query($sql, $term->subject_id));
        $state = 'leaf';
        if ($children) {
          $state = 'closed';
        }
        $content[] = array(
          'attributes' => array(
            'id' => $term->subject_id,
          ),
          'state' => $state,
          'data' => $name,
          'children' => array(),
        );
      }
    }
    else {
      // check if we have any children if so then set the value
      $children = db_fetch_object(chado_query($sql, $term->subject_id));
      $state = 'leaf';
      if ($children) {
        $state = 'closed';
      }
      $content[] = array(
        'attributes' => array(
          'id' => $term->subject_id,
        ),
        'state' => $state,
        'data' => $name,
        'children' => array(),
      );
    }
  }
  $content[] = $cnt_sql;

  return $content;
}