2012-11-06 09:50:35 +00:00

376 lines
10 KiB
PHP

<?php
class Zend_View_Helper_Field extends Zend_View_Helper_Abstract
{
protected $display = true;
protected $name = '';
public function Field($name)
{
$this->name = $name;
//Read the dico
$fields = new Scores_Fields();
$params = $fields->get($name);
$html = '';
$label = $labelG = $fields->getLabel($name);
$title = '';
if ( array_key_exists('title', $params) && !empty($params['title']) ) {
$title = $params['title'];
}
//How many type of fields is defined
$nbFields = count($params['fields']);
$display = true;
$out = '';
//If more than one field exist we need to make a special display
if ( $nbFields > 1) {
$out.= '<div class="fieldgrp clearfix">';
$out.= $this->structureLabel($label);
$out.= '<div class="field">';
foreach ( $params['fields'] as $type => $options )
{
switch ($type) {
case 'tree':
$href = $this->view->url(array('controller' => 'arborescence', 'action' => $options['action'], 'key' => $name));
$out.= '<a class="arborescence" title="'.$options['title'].'" href="'.$href.'">';
$out.= 'Arborescence';
$out.= '</a>';
break;
case 'text':
$out.= '<a class="text" title="'.$options['title'].'" href="#" id="'.$name.'" >';
$out.= 'Recherche';
$out.= '</a>';
break;
case 'list':
$out.= '<a class="dlist" title="'.$options['title'].'" href="#" id="'.$name.'" >';
$out.= 'Liste';
$out.= '</a>';
break;
}
$out.= '&nbsp;';
}
$out.= '</div>';
$out.= '</div>';
//Masquer les champs
$display = false;
}
//Draw fields
foreach ( $params['fields'] as $type => $options )
{
switch($type)
{
case 'select':
$html = $this->selectHTML($name, $options);
break;
case 'selectMultiple':
$html = $this->selectMultipleHTML($name, $options);
break;
case 'intervalSelect':
$html = $this->intervalSelectHTML($name, $options);
break;
case 'checkbox':
$html = $this->checkboxHTML($name, $options);
break;
case 'intervalDate':
$html = $this->intervalDateHTML($name, $options);
break;
case 'interval':
$html = $this->intervalHTML($name, $options);
break;
case 'date':
$html = $this->dateHTML($name, $options);
break;
case 'text':
$html = $this->textHTML($name, $options);
break;
case 'textarea':
$html = $this->textareaHTML($name, $options);
break;
case 'radio':
$html = $this->radioHTML($name, $options);
break;
case 'file':
$html = $this->fileuploadHtml($name, $options);
break;
case 'tree':
//Do nothing
break;
case 'list':
$html = $this->textareaHTML($name, $options);
break;
default:
$html = $this->structureHTML($name, '');
break;
}
$label = $labelG;
if ( array_key_exists('label', $options) ) {
$label = $options['label'];
}
$style = '';
$class = ' class="fieldgrp"';
if ( $display===false ) {
$style = ' style="display:none;"';
}
if ( !empty($title) ) {
$title = ' title="'.$title.'"';
}
$out.= '<div id="field_'.$type.'_'.$this->name.'" '.$class.''.$style.''.$title.'>';
$out.= $this->structureLabel($label);
$out.= '<div class="field">'.$html.'</div>';
$out.= '</div>';
}
return $out;
}
/**
* Définition du label
* @param unknown_type $label
*/
private function structureLabel($label)
{
$return = '<label>'.$label.'</label>';
return $return;
}
protected function getMinMax($name, $valeur)
{
if(!empty($name)) {
//$fields = new Scores_Fields();
//$default = $fields->getMinMax($name);
if(empty($valeur[0])){$valeur[0] = $default[0]['min'];}
if(empty($valeur[1])){$valeur[1] = $default[0]['max'];}
}
return ($valeur);
}
private function intervalSelectHTML($name, $options)
{
$session = new Scores_Fields();
$sessionValeur = explode(',', $session->getCritere($name));
$minMax = $this->getMinMax($name, array(0,0));
$return = '<select class="intervalSelect" name="'.$name.'1" id="'.$name.'" number="1">';
foreach($options['value'] as $elements) {
if($elements[0] == 0)
$return .= '<option value="'.$minMax[0].'">Min</option>';
else
$return .= '<option '.(($sessionValeur[0] == $elements[0])?'selected':'').' value="'. $elements[0].'">'.number_format($elements[0], 0,'', ' ').'</option>';
}
$return .= '</select> à ';
$return .= '<select class="intervalSelect" name="'.$name.'2" id="'.$name.'" number="2">';
foreach($options['value'] as $elements) {
if($elements[1] == 0)
$return .= '<option value="'.$minMax[1].'">Max</option>';
else
$return .= '<option '.(($sessionValeur[1] == $elements[1])?'selected':'').' 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);
}
/**
*
* @param unknown_type $name
* @param unknown_type $options
*/
protected function checkboxHTML($name, $options)
{
$session = new Scores_Fields();
$sessionValeur = $session->getCritere($name);
$select = '';
if ($sessionValeur==1) $select = ' checked';
$return = '<input type="checkbox" class="criteres" name="'.$name.'" value="'.$options['value'].'"'.$select.'>';
return $return;
}
/**
*
* @param unknown_type $name
* @param unknown_type $options
* @return string
*/
protected function selectMultipleHTML($name, $options)
{
$session = new Scores_Fields();
$sessionValeur = $session->getCritere($name);
$return = '<select size="'.count($options['value']).'" class="criteres " name="'.$name.'" multiple>';
foreach($options['value'] as $value => $label) {
$selected = "";
if( !empty($sessionValeur) ) {
if ( is_array($sessionValeur) && in_array($value, $sessionValeur) ) {
$selected = " selected";
}
}
$return .= '<option'.$selected.' value="'.$value.'">'.$label.'</option>';
}
$return .= '</select>';
$return .= '<a href="#" class="selectMultiple">Sélectionner</a>';
$return .= '&nbsp;<a href="#" class="selectMultipleEx">Exclure</a>';
return $return;
}
/**
*
* @param unknown_type $name
* @param unknown_type $options
* @return string
*/
protected function selectHTML($name, $options)
{
$session = new Scores_Fields();
$sessionValeur = $session->getCritere($name);
if( is_array($sessionValeur) ) {
$sessionValeur = implode(',', $sessionValeur);
}
$return = '<select class="criteres" name="'.$name.'">';
$return .= '<option '.(($sessionValeur == '-')?'selected':'').' value="-">-</option>';
foreach($options['value'] as $value => $label) {
$selected = "";
if( $sessionValeur!==null && $sessionValeur != "-" && $sessionValeur==$value )
$selected = " selected";
$return .= '<option'.$selected.' value="'.$value.'">'.$label.'</option>';
}
$return .= '</select>';
return $return;
}
protected function intervalDateHTML($name, $options)
{
$session = new Scores_Fields();
$sessionValeur = $session->getCritere($name);
$return = '';
$return.= '<select name="'.$name.'1_j" style="width:40px;">';
for ($i=1;$i<=31;$i++) {
$return.= '<option>'.str_pad($i,2,'0',STR_PAD_LEFT).'</option>';
}
$return.= '</select>';
$return.= '<select name="'.$name.'1_m" style="width:40px;">';
for ($i=1;$i<=12;$i++) {
$return.= '<option>'.str_pad($i,2,'0',STR_PAD_LEFT).'</option>';
}
$return.= '</select>';
$return.= '<select name="'.$name.'1_a" style="width:55px;">';
for ($i=1900;$i<=date('Y');$i++) {
$return.= '<option>'.$i.'</option>';
}
$return.= '</select>';
$return.= ' au ';
$return.= '<select name="'.$name.'2_j" style="width:40px;">';
for ($i=1;$i<=31;$i++) {
$select = '';
if ($i==date('d')) {
$select = ' selected';
}
$return.= '<option'.$select.'>'.str_pad($i,2,'0',STR_PAD_LEFT).'</option>';
}
$return.= '</select>';
$return.= '<select name="'.$name.'2_m" style="width:40px;">';
for ($i=1;$i<=12;$i++) {
$select = '';
if ($i==date('m')) {
$select = ' selected';
}
$return.= '<option'.$select.'>'.str_pad($i,2,'0',STR_PAD_LEFT).'</option>';
}
$return.= '</select>';
$return.= '<select name="'.$name.'2_a" style="width:55px;">';
$lastYear = date('Y');
for ($i=1900;$i<=$lastYear;$i++) {
$select = '';
if ($i==$lastYear) {
$select = ' selected';
}
$return.= '<option'.$select.'>'.$i.'</option>';
}
$return.= '</select>';
$return .= '<br/><a href="#" id="'.$name.'" class="intervalDate">Valider</a>';
return $return;
}
/* Interval */
private function intervalHTML($name, $field)
{
$session = new Scores_Fields();
$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 Scores_Fields();
$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 */
protected function textHTML($name, $options)
{
$return = '<input type="text" class="criteres autocomplete" name="'.$name.'" />';
$return.= '<ul id="selectqueries"></ul>';
$return.= '<a href="#" class="autocomplete">Sélectionner</a>';
$return.= '&nbsp;<a href="#" class="autocompleteEx">Exclure</a>';
return $return;
}
/* Textarea */
protected function textareaHTML($name, $field)
{
$return = '<textarea class="criteres" name="'.$name.'" ></textarea>';
$return.= '<a href="#" class="list">Sélectionner</a>';
$return.= '&nbsp;<a href="#" class="listEx">Exclure</a>';
return $return;
}
private function fileuploadHtml($name, $field)
{
$return = '<input class="upload" type="file" name="fichier" id="'.$name.'" /> ';
return ($return);
}
/* Radios */
private function radioHTML($name, $field, $item)
{
$return = '<input type="radio" name="'.$name.'" />';
return ($return);
}
}