<?php
function dynamic_list_init() {

}


function dynamic_list_menu() {
	$items = array();
 
  // Put your menu items here.
  	$items['dynamic-list'] = array(
    'title' => 'Dynamic lists',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('dynamic_list_terms_render_select_list'),
    'access callback' => TRUE,
    'type' => MENU_NORMAL_ITEM,
	);
 
  return $items;
}



function dynamic_list_terms_render_select_list($form, &$form_state) {
  // Load the vocabulary. Instead of 'taxonomy_animals' you have to give the
  // machine name of your vocabulary.
  $vocabulary = taxonomy_vocabulary_machine_name_load('knowledge');
 
  // Load all the parent terms of the vocabulary.
  // The second parameter 0 means that we wish to generate the whole tree.
  // The third parameter 1 means that we wish to see only the parent terms.
  // Default is 0.
  $parent_terms = taxonomy_get_tree($vocabulary->vid, 0, 1);
 
  $animal_category = array('none' => '-- None --');
  // Build the parent select list values.
  foreach ($parent_terms as $parent) {
    $animal_category[$parent->tid] = $parent->name;
  }
 
  // Check the value of parent select list and populate the value of child
  // select list according to that.
  $default = !empty($form_state['values']['parent_select_list']) ? $form_state['values']['parent_select_list'] : 'none';
 
  if ($default != 'none') {
    // Load all the child terms of the selected parent item.
    // The second parameter tells to load the tree based on the parent term ID.
    // We are ignoring the third parameter since we know that the vocabulary has
    // only two levels.
    $child_terms = taxonomy_get_tree($vocabulary->vid, $default);
    // Build the child select list values.
    foreach ($child_terms as $child) {
      $child_select_options[$child->tid] = $child->name;
    }
  }
  else {
    // No value selected in parent selct list.
    $child_select_options = array('none' => '-- None --');
  }
 
  // Parent select list.
  $form['parent_select_list'] = array(
    '#type' => 'select',
    '#title' => t('Select category'),
    '#options' => $animal_category,
    '#default_value' => $animal_category['none'],
    '#description' => t('Select class'),
    '#ajax' => array(
      'callback' => 'dynamic_list_terms_select_list_ajax_callback',
      'wrapper' => 'child-select-list-wrapper',
      'event' => 'change',
    ),
  );
 
  // Child select list.
  $form['child_select_list'] = array(
    '#prefix' => '<div id="child-select-list-wrapper">',
    '#type' => 'checkboxes',
    '#title' => t('Select branche'),
    '#options' => $child_select_options,
    '#description' => t('Select branche'),
    '#suffix' => '</div>',
  );
  return $form;
}
 
/**
 * AJAX callback.
 */      
function dynamic_list_terms_select_list_ajax_callback($form, &$form_state) {
#  return $form['child_select_list'];

  if($form['field_inserat_branchen']) return $form['field_inserat_branchen'];
  return $form['field_experience'];
}


function dynamic_select_list($form, &$form_state) {
	global $user;

	

	$sql='SELECT *
	FROM {node} A
	WHERE 
	A.uid =:uid
	AND
	A.type = :type
	AND 
	A.status=1';
	$assigned_branches=array();
	$result=db_query($sql, array(':uid'=>$user->uid, ':type'=>'branche'));
	while ($record = $result->fetchObject()) {
		$branchenode=node_load($record->nid);
		$assigned_branches[]= $branchenode->field_experience_parent[LANGUAGE_NONE][0]['tid'];
	}  



  // Load the vocabulary. Instead of 'taxonomy_animals' you have to give the
  // machine name of your vocabulary.
  $vocabulary = taxonomy_vocabulary_machine_name_load('knowledge');
 
  // Load all the parent terms of the vocabulary.
  // The second parameter 0 means that we wish to generate the whole tree.
  // The third parameter 1 means that we wish to see only the parent terms.
  // Default is 0.
  $parent_terms = taxonomy_get_tree($vocabulary->vid, 0, 1);
  $animal_category = array('none' => '-- None --');
  // Build the parent select list values.

  foreach ($parent_terms as $parent) {
    if(!in_array($parent->tid, $assigned_branches)  || (isset($form['field_experience_parent'][LANGUAGE_NONE]['#default_value'][0]) && $parent->tid==$form['field_experience_parent'][LANGUAGE_NONE]['#default_value'][0])  ) {
        $animal_category[$parent->tid] = $parent->name;
    }
  }
 
  // Check the value of parent select list and populate the value of child
  // select list according to that.
#  dpm($form_state);
  
	  $default = !empty($form_state['values']['field_experience_parent'][LANGUAGE_NONE][0]['tid']) ? $form_state['values']['field_experience_parent'][LANGUAGE_NONE][0]['tid'] : 'none';

	  if($default=='none') {
		  if(isset($form['field_experience_parent'][LANGUAGE_NONE]['#default_value'][0])) {
				$default=$form['field_experience_parent'][LANGUAGE_NONE]['#default_value'][0];
		  }
	  }
 
  if ($default != 'none') {
    // Load all the child terms of the selected parent item.
    // The second parameter tells to load the tree based on the parent term ID.
    // We are ignoring the third parameter since we know that the vocabulary has
    // only two levels.
    
    $child_terms = taxonomy_get_tree($vocabulary->vid, $default);
    // Build the child select list values.
    foreach ($child_terms as $child) {
      $child_select_options[$child->tid] = $child->name;
    }
	    
  }
  else {
    // No value selected in parent selct list.
    $child_select_options = array('none' => '-- none --');
  }
 
 
/* 
  // Parent select list.
  $parents= array(
    '#type' => 'select',
    '#title' => t('Select category'),
    '#options' => $animal_category,
    '#default_value' => $animal_category['none'],
    '#description' => t('Select class'),
    '#ajax' => array(
      'callback' => 'dynamic_list_terms_select_list_ajax_callback',
      'wrapper' => 'field-experience-wrapper',
      'event' => 'change',
    ),
  );
*/ 
 $parents = $animal_category;
 
  // Child select list.

/*
  $children = array(
    '#prefix' => '<div id="field-experience-wrapper">',
    '#type' => 'checkboxes',
    '#title' => t('Select branche'),
    '#options' => $child_select_options,
    '#description' => t('Select branche'),
    '#suffix' => '</div>',
  );
*/	
	$children=$child_select_options;
	
	return array(
		'parents'=>$parents,
		'children'=>$children
	);

}


