<?php

function materio_search_api_settings(){
	$indexes = search_api_index_load_multiple(false);
	// dsm($indexes, '$indexes');

	foreach ($indexes as $machine_name => $index) {
		if($index->enabled == 1)
			$index_options[$index->machine_name] = $index->name;
	}

	$languages = locale_language_list();
	// dsm($languages, 'languages');

	$form = array();


	// Solr indexes by language
	foreach ($languages as $lcode => $name) {
		$form['fulltextsearchindex_'.$lcode] = array(
			'#type'=>'select',
			'#options'=>$index_options,
			'#default_value' => variable_get('fulltextsearchindex_'.$lcode, -1),
			'#title' => t('Full text search api index for %lang contents.', array('%lang'=>$name)),
		);

		$form['taxonomysearchindex_'.$lcode] = array(
			'#type'=>'select',
			'#options'=>$index_options,
			'#default_value' => variable_get('taxonomysearchindex_'.$lcode, -1),
			'#title' => t('Taxonomy search api index for %lang contents.', array('%lang'=>$name)),
		);

		$form['advancedsearchindex_'.$lcode] = array(
			'#type'=>'select',
			'#options'=>$index_options,
			'#default_value' => variable_get('advancedsearchindex_'.$lcode, -1),
			'#title' => t('Advanced search api index for %lang contents.', array('%lang'=>$name)),
		);

		$form['brevessearchindex_'.$lcode] = array(
			'#type'=>'select',
			'#options'=>$index_options,
			'#default_value' => variable_get('brevessearchindex_'.$lcode, -1),
			'#title' => t('Breves search api index for %lang contents.', array('%lang'=>$name)),
		);

		$form['autocompletesearchindex_'.$lcode] = array(
			'#type'=>'select',
			'#options'=>$index_options,
			'#default_value' => variable_get('autocompletesearchindex_'.$lcode, -1),
			'#title' => t('Autocomplete search api index for %lang contents.', array('%lang'=>$name)),
		);

	}


	// view modes
	// TODO:  select the activated viewmodes for change view mode and selected view mode
	$entity_infos = entity_get_info();
  // dsm($entity_infos, 'entity_infos');
  foreach ($entity_infos['node']['view modes'] as $viewmode => $value) {
  	$viewmode_options[$viewmode] = $value['label'];
  }

	$form['availableviewmodes'] = array(
		'#type'=>'select',
		'#options'=>$viewmode_options,
		'#default_value' => variable_get('availableviewmodes', array()),
		'#title' => t('Availble View modes'),
		'#multiple' => true,
	);

	$form['defaultviewmode'] = array(
		'#type'=>'select',
		'#options'=>$viewmode_options,
		'#default_value' => variable_get('defaultviewmode', 'full'),
		'#title' => t('Defalut View mode'),
	);

	// limits by view mode
	foreach (variable_get('availableviewmodes', array()) as $viewmode => $value) {
		$form[$viewmode.'_limite'] = array(
			'#type'=>'textfield',
			'#size'=>5,
			'#default_value' => variable_get($viewmode.'_limite', '10'),
			'#title' => t('@vm limite', array('@vm'=>$viewmode)),
		);
	}

	// advanced search vocabulary
	$vocs = taxonomy_get_vocabularies();
	// dsm($vocs, 'vocs');
	$vocs_options = array();
	foreach ($vocs as $vid => $voc) {
		$vocs_options[$vid] = $voc->name;
	}
	$form['msa-advancedsearchvocabulary'] = array(
		'#type' => 'select',
		'#options' => $vocs_options,
		'#default' => variable_get('msa-advancedsearchvocabulary', null),
		'#title' => t('advanced search vocabulary'),
		'#description' => 'Choose which vocabulary will be used for advanced search form',
	);

	// advanced search hide some taxonomy terms
	if($vid = variable_get('msa-advancedsearchvocabulary', null)){
		$tree = msa_taxonomy_get_nested_tree(taxonomy_get_tree($vid));
		// dsm($tree, "tree");

		$tree_options = array();
		msa_get_nested_tree_options($tree, $tree_options);
		// dsm($tree_options, 'tree_options');

		$form['msa-hiddentaxoterms'] = array(
			'#type' => 'select',
			'#options' => $tree_options,
			'#default_value' => variable_get('msa-hiddentaxoterms', array()),
			'#multiple' => TRUE,
			'#title' => t('Hidden taxonomy terms'),
			'#description' => t('Select terms which will be hidden from advanced search form')
		);

	}

	return system_settings_form($form);
}

function msa_taxonomy_get_nested_tree($terms = array(), $max_depth = NULL, $parent = 0, $parents_index = array(), $depth = 0) {
	  if (is_int($terms)) {
			$terms = taxonomy_get_tree($terms);
	  }

	  foreach($terms as $term) {
			foreach($term->parents as $term_parent) {
			  if ($term_parent == $parent) {
				$return[$term->tid] = $term;
			  }
			  else {
				$parents_index[$term_parent][$term->tid] = $term;
			  }
			}
	  }

	  foreach($return as &$term) {
			if (isset($parents_index[$term->tid]) && (is_null($max_depth) || $depth < $max_depth)) {
			  $term->children = msa_taxonomy_get_nested_tree($parents_index[$term->tid], $max_depth, $term->tid, $parents_index, $depth + 1);
			}
	  }

	  return $return;
	}

function msa_get_nested_tree_options($tree, &$options, $prefix = ""){
	foreach ($tree as $tid => $term) {
		$options[$tid] = $prefix . $term->name;
		if(isset($term->children)){
			msa_get_nested_tree_options($term->children, $options, $prefix."$term->name - ");
		}
	}
}
