'admin/settings/taxonomy_strider',
'title' => t('Taxonomy Strider configuration'),
'description' => t('Enable TS for specific vocabulearies.'),
'callback' => 'drupal_get_form',
'callback arguments' => 'taxonomy_strider_config_form',
'access' => user_access('enable vocabularies')
);
$items[] = array(
'path' => 'taxonomy_strider/get',
'callback' => 'taxonomy_strider_get',
'type' => MENU_CALLBACK,
'access' => user_access('browse taxonomy')
);
$items[] = array(
'path' => 'taxonomy_strider/get_term_name',
'callback' => 'taxonomy_strider_getTermName',
'type' => MENU_CALLBACK,
'access' => user_access('browse taxonomy')
);
}
return $items;
}
/**
* Taxonomy Strider configuration form.
*/
function taxonomy_strider_config_form(){
$form = array();
$vocabularies = taxonomy_get_vocabularies();
foreach($vocabularies as $vid => $vocabulary){
$options[$vid] = $vocabulary->name;
}
$form['ts_vocabularies'] = array(
'#type' => 'checkboxes',
'#title' => t('Vocabularies'),
'#description' => t('Select vocabularies to be browsed by Taxonomy Strider Widget.'),
'#options' => $options,
'#default_value' => variable_get('ts_vocabularies', array())
);
$form['array_filter'] = array('#type' => 'hidden');
return system_settings_form($form);
}
/**
* Implementation of hook_form_alter().
*/
function taxonomy_strider_form_alter($form_id, &$form){
// Note edit form.
if(strstr($form_id,'_node_form') AND strstr($form['form_id']['#value'], '_node_form')){
$vocabularies = taxonomy_get_vocabularies($form['type']['#value']);
$flag = false;
foreach($vocabularies as $vid => $vocabulary){
if(in_array($vid, variable_get('ts_vocabularies', array()))){
$flag = true; // There's an active Taxonomy Strider
if($vocabulary->tags){
// Remove any tags from the autocomplete form item (prevent duplicates).
$tags[$vocabulary->vid] = $form['taxonomy']['tags'][$vocabulary->vid];
$tags[$vocabulary->vid]['#default_value'] = '';
$tags[$vocabulary->vid]['#required'] = FALSE;
$tags[$vocabulary->vid]['#parents'] = array('taxonomy', 'tags', $vocabulary->vid);
$tags[$vocabulary->vid]['#weight'] = -12;
$tags[$vocabulary->vid]['#title'] = t('Enter New Tags');
unset($form['taxonomy']['tags'][$vocabulary->vid]);
} else {
// Empty select box (will be hidden via JS).
$form['taxonomy'][$vid]['#options'] = array();
// unset($form['taxonomy']); // Rebuilt on submit...
}
$form['taxonomy']['ts_'.$vid] = taxonomy_strider_start_form($vocabulary);
}
}
if($flag){
$form['taxonomy']['keeper'] = array(
'#prefix' => 'Your Selections:',
'#value' => '
',
'#weight' => -2
);
$tax_paths = taxonomy_strider_get_tax_paths($form['nid']['#value']);
$form['taxpaths'] = array(
'#type' => 'hidden',
'#value' => $tax_paths
);
$form['#submit']['taxonomy_strider_submit'] = array();
// Load after Main JS has been loaded.
drupal_add_js(drupal_get_path('module','taxonomy_strider').'/jquery.taxonomy_strider.business.js');
}
}
}
/**
* This root of vocabulary navigation in form api format.
*/
function taxonomy_strider_start_form($vocabulary, $type = 'checkbox'){
drupal_add_css(drupal_get_path('module','taxonomy_strider').'/taxonomy_strider.css');
drupal_add_js(drupal_get_path('module','taxonomy_strider').'/jquery.taxonomy_strider.js');
$form = array();
$form['#weight'] = -1;
$form['fieldset'] = array(
'#type' => 'fieldset',
'#title' => $vocabulary->name,
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#tree' => TRUE
);
$form['fieldset']['strider'] = array(
'#prefix' => "",
'#value' => l("Browse $vocabulary->name...", "taxonomy_strider/get/$vocabulary->vid/$type", array('class' => 'tmystrider')),
'#suffix' => "
"
);
$form['locator'] = array(
'#prefix' => ' 'id="locator_'.$vocabulary->vid.'" value="'.$vocabulary->vid.'"',
'#suffix' => 'title="'.$vocabulary->name.'" />'
);
return $form;
}
/**
* Implementation of hook_submit().
*/
function taxonomy_strider_submit($form_id, &$form_values){
global $user;
if($vid = $form_values['vid'] AND $form_id == 'taxonomy_form_vocabulary'){
if(count($form_values['ts']['taxonomy_strider_vid_'.$vid])){
variable_set('taxonomy_strider_vid_'.$form_values['vid'], $form_values['ts']['taxonomy_strider_vid_'.$vid]);
} else {
variable_del('taxonomy_strider_vid_'.$form_values['vid']);
}
}
if(strstr($form_id, '_node_form')){
$tax_paths = explode("|", $_POST['taxpaths']);
foreach($tax_paths as $tax_path){
$term_ids = explode(",", $tax_path);
for($i=1; $i<(count($term_ids)); $i++){
$tid = $term_ids[$i];
// Term into taxonomy tables.
$form_values['taxonomy'][$vid][$tid] = $tid;
}
}
$_SESSION['tax_paths'] = $_POST['taxpaths'];
}
}
/**
* Implementation of hook_nodeapi();
*/
function taxonomy_strider_nodeapi($node, $op, $arg = 0){
global $user;
switch($op){
/* case 'view':
//print_r($node); exit();
$trails = db_query("SELECT tkey, tid, pkey FROM {taxonomy_strider} WHERE nid = %d ORDER BY tkey DESC", $node->nid);
$result = array();
$output = array();
while($trail = db_fetch_object($trails)){
array_unshift($output, $trail->tid);
if($trail->pkey == 0){
$ret = array();
while(count($output) > 0){
$list = implode(",", $output);
$term = taxonomy_get_term(array_pop($output));
array_unshift($ret, l($term->name, "taxonomy/term/".urldecode($list)));
}
$result[] = "".implode(' >> ', $ret)."";
}
}
$node->content['ts_links'] = array(
"#prefix" => "",
"#value" => implode("\n", $result),
"#suffix" => "
",
'#weight' => 10,
);
break; */
case 'insert':
case 'update':
if(isset($_POST['taxpaths'])){
db_query('DELETE FROM {taxonomy_strider} WHERE nid = %d', $node->nid);
$tax_paths = explode("|", $_POST['taxpaths']);
foreach($tax_paths as $tax_path){
$term_ids = explode(",", $tax_path);
for($i=1; $i<(count($term_ids)); $i++){
$tid = $term_ids[$i];
$pkey = $i>1 ? $tkey : null;
$tkey = db_next_id('pkeys');
db_query( 'INSERT INTO {taxonomy_strider} (tkey, nid, tid, pkey) VALUES (%d, %d, %d, %d)', $tkey, $node->nid, $tid, $pkey);
}
}
}
break;
case 'delete':
db_query('DELETE FROM {taxonomy_strider} WHERE nid = %d', $node->nid);
break;
}
}
/**
* This returns children of a determined term in js format.
*/
function taxonomy_strider_get($vid, $track = FALSE, $tid = FALSE){
$result = drupal_get_form('taxonomy_strider_navigation_form', $vid, $track, $tid);
print drupal_to_js(array($vid, $result));
}
/**
* Taxonomy strider navigation form.
*/
function taxonomy_strider_navigation_form($vid, $track = 0, $tid = FALSE){
if($track == 0){
$track = $vid;
}
$original = $track;
$elements = array($vid);
$track = explode(',', $track);
$count = count($track);
$vocabulary = taxonomy_get_vocabulary($vid);
if($tid){
$track[] = $tid;
$count = count($track);
$terms = taxonomy_get_children($tid, $vid);
} else {
if($count > 1){
$terms = taxonomy_get_children($track[$count-1], $vid);
array_pop($track);
} else {
$terms = taxonomy_get_tree($vid, 0, -1, 1);
}
}
$track = implode(',', $track);
if($tid){
$current = $track;
} else {
$current = $original;
}
$options = array();
foreach($terms AS $index => $term){
$children = taxonomy_get_children($term->tid, $vid);
if(count($children)){
$term->is_parent = TRUE;
}
$options[$term->tid] = $term->name." ";
$options[$term->tid].= $term->is_parent ? l('>', "taxonomy_strider/get/$vid/$current/$term->tid", array('class' => 'tmystrider')) : "";
}
$input = $vocabulary->multiple ? "checkboxes" : "radios";
// Term list
$form['terms'] = array(
'#type' => $input,
'#options' => $options,
"#prefix" => "",
"#suffix" => "
",
);
$current = isset($current) ? $current : $vid;
$form['trails'] = array(
'#prefix' => ' 'id="trails_'.$vid.'" value="'.$current.'"',
'#suffix' => ' />'
);
$breadcrumbs = taxonomy_strider_trail_to_breadcrumbs($vid, $current, true);
$form['breadcrumb'] = array(
"#prefix" => "",
"#suffix" => "
",
"#value" => implode(" > ", $breadcrumbs),
'#weight' => -1
);
$form['current'] = array(
'#type' => 'hidden',
'#value' => implode(" | ", taxonomy_strider_trail_to_breadcrumbs($vid, $current))
);
return $form;
}
/**
* Breadcrumb array helper
*/
function taxonomy_strider_trail_to_breadcrumbs($vid, $trail, $linked = FALSE){
$current = explode(",", $trail);
$cTerm = count($current) > 1 ? taxonomy_get_term(array_pop($current)) : taxonomy_get_vocabulary(array_pop($current));
$breadcrumbs = array();
foreach($current as $tid){
if(count($breadcrumbs) > 0){
$link.= ",".$tid;
$term = taxonomy_get_term($tid);
$breadcrumbs[] = $linked ? l($term->name, "taxonomy_strider/get/$vid/$link", array('class' => 'tmystrider')) : $term->name;
} else {
$link = $tid;
$cvoc = taxonomy_get_vocabulary($vid);
$breadcrumbs[] = $linked ? l($cvoc->name, "taxonomy_strider/get/$vid/$link", array('class' => 'tmystrider')) : $cvoc->name;
}
}
$breadcrumbs[] = $linked ? "".$cTerm->name."" : $cTerm->name;
return $breadcrumbs;
}
/**
* Returns a string with tax_paths.
*
* @param integer $nid
* @return string
*/
function taxonomy_strider_get_tax_paths($nid){
$result = db_query("SELECT tkey, tid, pkey FROM {taxonomy_strider} WHERE nid = %d ORDER BY tkey DESC", $nid);
$tax_paths = array();
$trail = array();
while($data = db_fetch_object($result)){
array_unshift($trail, $data->tid);
if($data->pkey == 0){
$term = taxonomy_get_term($data->tid);
array_unshift($trail, $term->vid);
$tax_paths[] = implode(',', $trail);
$trail = array();
}
}
return implode('|', $tax_paths);
}
/**
* Gets term name.
*/
function taxonomy_strider_getTermName($tid){
$term = taxonomy_get_term($tid);
print utf8_encode($term->name);
exit();
}