<?php
Class Zend_View_Helper_Field extends Zend_View_Helper_Abstract
{	
	public function Field($name, $field)
	{
		$html = '';
		if($field != null) {
			$html.= '<div class="fieldgrp">';			
			$type = $field['type'];
			switch($type)
			{
				case 'select':
					$html.= $this->structureHTML(
						$field['label'], 
						$this->selectHTML($name, $field));
					break;
				case 'selectMultiple':
					$html.= $this->structureHTML(
						$field['label'], 
						$this->selectMultipleHTML($name, $field));
					break;
				case 'intervalSelect':
					$html.= $this->structureHTML(
						$field['label'], 
						$this->intervalSelectHTML($name, $field));
					break;
				case 'interval':
					$html.= $this->structureHTML(
						$field['label'], 
						$this->intervalHTML($name, $field));
					break;
				case 'date':
					$html.= $this->structureHTML(
						$field['label'], 
						$this->dateHTML($name, $field));
					break;
				case 'text':
					$html.= $this->structureHTML(
						$field['label'], 
						$this->textHTML($name, $field));
					break;
				case 'textarea':
					$html.= $this->structureHTML(
						$field['label'], 
						$this->textareaHTML($name, $field));
					break;
				case 'radio':
					$html.= $this->structureHTML(
						$field['label'], 
						$this->radioHTML($name, $field));
					break;
			}
			$html.= '</div>';
		}
		return $html;
	}
	
	private function structureHTML($label, $html, $liens = array())
	{
		$out = '';
		$out.= $this->structureLabel($label);
		$out.= '<div class="field">'.$html.$this->structureLink($liens).'</div>';
		return $out;
	}
	
	/**
	 * Définition du label
	 * @param unknown_type $label
	 */
	private function structureLabel($label)
	{
		return '<label>'.$label.'</label>';
	}
	
	/**
	 * Définition des liens
	 */
	private function structureLink($liens = array())
	{
		$html = '';
		if (count($liens)>0) {
			foreach($liens as $lien){
				$html.= '<a href="'.$lien['url'].'">'.$lien['lib'].'</a>';		
			}
		}
		if (!empty($html)) { 
			return '<span>'.$html.'</span>';		
		}
		return '';
	}
	
	private function intervalSelectHTML($name, $field)
	{
		$session = new SessionCiblage();
		
		$return = '<select class="intervalSelect" name="'.$name.'1" id="'.$name.'" number="1">';
			foreach($field['fields'][$field['type']]['value'] as $elements) {
				$return .= '<option value="'. $elements[0].'">'.number_format($elements[0], 0,'', ' ').'</option>';
			}
			
		$return .= '</select> à ';
		$return .= '<select class="intervalSelect" name="'.$name.'2" id="'.$name.'" number="2">';
			foreach($field['fields'][$field['type']]['value'] as $elements) {
				$return .= '<option value="'. $elements[1].'">'.number_format($elements[1], 0,'', ' ').'</option>';
			}
			
		$return .= '</select>';
		$return .= ' <span id="'.$name.'"><a href="" class="intervalSelect" id="'.$name.'">Valider</a></span>';
		return ($return);
	}
	
	/* Select */
	private function selectMultipleHTML($name, $field)
	{
		$session = new SessionCiblage();
		
		$return	= '<select class="criteres" name="'.$name.'">';
		$return .= '<option value="tous">Tous</option>';
		foreach($field['fields'][$field['type']]['value'] as $value => $label) {
			$selected = "";
			if(($session->getCritere($name) != null) and $session->getCritere($name) == $value)
				$selected = " selected";
			$return .= '<option'.((!empty($selected))?$selected:null).' value="'.$value.'">'.$label.'</option>';
		}
		$return .= '</select>';
		return ($return);
	}
	
	/* Select Multiple */
	private function selectHTML($name, $field)
	{
		$session = new SessionCiblage();
		
		$sessionValeur = $session->getCritere($name);
		if(is_array($sessionValeur))
			$sessionValeur = implode(',', $sessionValeur);
		$return	= '<select class="criteres '.$field['type']['class'].'" name="'.$name.'">';
		$return .= '<option value="tous">Tous</option>';
		foreach($field['fields'][$field['type']]['value'] as $value => $label) {
			$selected = "";
			if(($sessionValeur != null) and $sessionValeur == $value)
				$selected = " selected";
			$return .= '<option'.((!empty($selected))?$selected:null).' value="'.$value.'">'.$label.'</option>';
		}
		$return .= '</select>';
		return ($return);
	}
	
	/* Interval */
	private function intervalHTML($name, $field)
	{
		$session = new SessionCiblage();
		$valeur = $session->getCritere($name);
		
		$return = '<div class="interval" >';
			$return .= '<input class="'.$field['class'].'" type="text" name="'.$name.'1" value="'.$valeur[0].'" /> à ';
			$return .= '<input class="'.$field['class'].'" type="text" name="'.$name.'2" value="'.$valeur[1].'" />';
			$return .= ' <a href="" class="interval" id="'.$name.'">Valider</a>';
		$return .= '</div>';
		
		return ($return);
	}
	
	/* Dates */
	private function dateHTML($name, $field)
	{
		$session = new SessionCiblage();
		
		$return = '<div class="date" >';
			$return .= '<input value="'.$session->getCritere($name).'"  type="text" class="datepicker" name="'.$name.'1" /> a ';
			$return .= '<input  type="text" class="datepicker" name="'.$name.'2" />';
		$return .= '</div>';
		
		return ($return);
	}
	
	/* Textes */
	private function textHTML($name, $field)
	{
		$session = new SessionCiblage();
		
		$return = '<input style="border:1px inset silver;width:60%" class="criteres" type="text" name="'.$name.'" value="'.$session->getCritere($name).'" />';
		
		return ($return);
	}
	
	/* Textarea */
	private function textareaHTML($name, $field)
	{
		$return = '<a class="arborescence" title="'.$field['title'].'" href="'.$this->view->url(array('controller' => 'arborescence', 'action' => $field['action'], 'key' => $name)).'">
							Selection dans une arborescence
					</a>';
		return ($return);
	}
	
	/* Radios */
	private function radioHTML($name, $field, $item)
	{
		$return = '<input type="radio" name="'.$name.'" />';		
		return ($return);
	}
}