<?php

function _digett_block_taxonomy_configure($block=array()){
  $form = array();
  $form['digett_block_title'] = array(
    '#type' => 'textfield',
    '#title' => t('Title'),
    '#default_value' => digett_block_variable_get($block,'title',''),
  );

  $vocs = taxonomy_get_vocabularies();
  foreach($vocs as $vid=>$voc){
    $options[$vid] = $voc->name;
  }
  $form['digett_block_vid'] = array(
    '#type' => 'select',
    '#title' => t('Vocabulary'),
    '#options' => $options,
    '#default_value' => digett_block_variable_get($block,'vid',0),
  );
  $form['digett_block_arg_type'] = array(
    '#type' => 'radios',
    '#title' => t('Argument Type'),
    '#options' => array('tid'=>'Term ID','name'=>'Term Name'),
    '#default_value' => digett_block_variable_get($block,'arg_type','tid'),
  );
  $form['digett_block_link_prefix'] = array(
    '#type' => 'textfield',
    '#title' => t('Link pattern'),
    '#default_value' => digett_block_variable_get($block,'link_prefix','taxonomy/term/'),
  );
  $form['digett_block_display_type'] = array(
    '#type' => 'select',
    '#title' => t('Display'),
    '#options' => array('all'=>'All terms','used'=>'Only used terms'),
    '#default_value' => digett_block_variable_get($block,'display_type','all'),
  );
  return $form;
}

function _digett_block_taxonomy_view($block=array()){
  $vid = digett_block_variable_get($block,'vid',0);
  $arg = digett_block_variable_get($block,'arg_type','tid');
  $prefix = digett_block_variable_get($block,'link_prefix','taxonomy/term/');
  $tree = taxonomy_get_tree($vid);
  if($tree){
    foreach($tree as $term){
      if(digett_block_variable_get($block,'display_type','all') == 'used'){
        $result = taxonomy_select_nodes(array($term->tid));
        if(db_num_rows($result) > 0){
          $list[] = l($term->name, $prefix.$term->$arg);
        }
      }else{
        $list[] = l($term->name, $prefix.$term->$arg);
      }
    }
    $return['content'] = theme('item_list',$list);
  }else{
    $return['content'] = '';
  }
  $return['subject'] = digett_block_variable_get($block,'title','');
  
  return $return;  
}

