<?php

function cobalttaxonomy_init() {
  if (user_access('use cobalt')) {
    drupal_add_js(drupal_get_path('module', 'cobalt') . '/js/cobalt.taxonomy.js');
  }
}

function cobalttaxonomy_menu() {
  $items = array();
  $items['cobalt/data/taxonomy_json'] = array(
    'title' => 'Serialized taxonomies',
    'page callback' => 'cobalttaxonomy_json',
    'page arguments' => array('update', 3),
    'access arguments' => array('use cobalt'),
    'type' => MENU_CALLBACK,
  );
  return $items;
}


function cobalttaxonomy_json($op, $value) {
  global $user;

  $vocabularies = taxonomy_get_vocabularies();

  $voc_data = array();
  foreach ($vocabularies as $vocabulary) {
    $voc_data[$vocabulary->vid] = $vocabulary->name;
  }

  $term_tree = array();
  foreach ($vocabularies as $vocabulary) {
    $term_tree += taxonomy_get_tree($vocabulary->vid);
  }

  $term_data = array();

  foreach ($term_tree as $term) {
    $term_data[$term->tid] = array($term->name, $term->vid);
  }

  $data = array('vocabularies' => $voc_data, 'terms' => $term_data, 'access' => user_access('administer taxonomy'));

  print drupal_json_output($data);
  exit;
}

