Refactoring pour gérer les éléments et leurs valeurs

This commit is contained in:
Michael RICOIS 2014-06-26 15:55:53 +00:00
parent 2ac4689721
commit ab7e869ca8
8 changed files with 350 additions and 57 deletions

View File

@ -4,6 +4,12 @@ class Scores_Ciblage_Extract
/**
* Déclaration des champs exportable
* @var array
* key
* label => string
* column => string
* sql => Sql Expr or null
* join => ArrayOf ( label, column, table, cond )
* values => static value array(key => label)
*/
protected $fields = array(
'siege' => array(

View File

@ -4,7 +4,12 @@ class Scores_Ciblage_Field_List
protected $name = null;
protected $data = null;
public function construct($name, $data)
/**
*
* @param string $name
* @param array $data
*/
public function __construct($name, $data)
{
$this->name = $name;
$this->data = $data;
@ -15,6 +20,7 @@ class Scores_Ciblage_Field_List
$html = '<div class="row">' .
'<div class="col-md-6">' .
'<div class="btn-group criteres" name="'.$this->name.'">';
$valueTxt = '';
if ( count($valueSelected)>0 ) {
$i = 0;
@ -25,7 +31,7 @@ class Scores_Ciblage_Field_List
}
}
}
$html.= '<textarea class="form-control" rows="3">';
$html.= '<textarea class="form-control" rows="3" name="'.$this->name.'">';
$html.= $valueTxt;
$html.= '</textarea>';
$html.= '<div class="col-md-6">' .
@ -33,6 +39,6 @@ class Scores_Ciblage_Field_List
'</div>'.
'</div>'.
'</div>';
return html;
return $html;
}
}

View File

@ -4,7 +4,12 @@ class Scores_Ciblage_Field_Many
protected $name = null;
protected $data = null;
public function construct($name, $data)
/**
*
* @param string $name
* @param array $data
*/
public function __construct($name, $data)
{
$this->name = $name;
$this->data = $data;
@ -16,8 +21,8 @@ class Scores_Ciblage_Field_Many
'<div class="col-md-6">' .
'<div name="'.$this->name.'">';
if ( count($this->data)>0 ) {
$html.= '<select multiple class="form-control">';
foreach ( $this->data as $label => $value ) {
$html.= '<select multiple class="form-control" name="'.$this->name.'">';
foreach ( $this->data as $value => $label ) {
$selected = "";
if( in_array($value, $valueSelected) ) {
$selected = " selected";
@ -32,6 +37,6 @@ class Scores_Ciblage_Field_Many
'</div>'.
'</div>'.
'</div>';
return html;
return $html;
}
}

View File

@ -4,7 +4,12 @@ class Scores_Ciblage_Field_One
protected $name = null;
protected $data = null;
public function construct($name, $data)
/**
*
* @param string $name
* @param array $data
*/
public function __construct($name, $data)
{
$this->name = $name;
$this->data = $data;
@ -14,9 +19,9 @@ class Scores_Ciblage_Field_One
{
$html = '<div class="btn-group criteres pull-right" data-toggle="buttons" name="'.$this->name.'">';
if ( count($this->data)>0 ) {
foreach ( $this->data as $label => $value ) {
foreach ( $this->data as $value => $label ) {
$selected = "";
if( $valueSelected == $value ) {
if( $valueSelected === $value ) {
$selected = "active";
}
$html.= '<label class="btn btn-primary btn-sm '.$selected.'">'.
@ -25,6 +30,7 @@ class Scores_Ciblage_Field_One
}
}
$html .= '</div>';
return html;
return $html;
}
}

View File

@ -4,7 +4,12 @@ class Scores_Ciblage_Field_Range
protected $name = null;
protected $data = null;
public function construct($name, $data)
/**
*
* @param string $name
* @param array $data
*/
public function __construct($name, $data)
{
$this->name = $name;
$this->data = $data;
@ -30,7 +35,7 @@ class Scores_Ciblage_Field_Range
'</div>'.
'</div>'.
'</div>';
return html;
return $html;
}
public function getJS()

View File

@ -4,7 +4,12 @@ class Scores_Ciblage_Field_RangeDate
protected $name = null;
protected $data = null;
public function construct($name, $data)
/**
*
* @param string $name
* @param array $data
*/
public function __construct($name, $data)
{
$this->name = $name;
$this->data = $data;
@ -29,7 +34,7 @@ class Scores_Ciblage_Field_RangeDate
'</div>'.
'</div>'.
'</div>';
return html;
return $html;
}
public function getJS()

View File

@ -1,24 +1,15 @@
<?php
class Scores_Ciblage_Field
class Scores_Ciblage_FieldList
{
/**
* key
* display => null if no display
* label
* desc
* family
* type => ArrayOf (one, many, range, rangedate, tree, search, list)
* values => array(key => label)
* ou sql => array( columns, table)
* ou minmax
* extract => null if no extract
* key => Clé du profil d'enrichissement
* label => string
* column => string
* sql => Sql Expr or null
* join => ArrayOf ( label, column, table, cond )
* values => static value array(key => label)
*
* label
* desc
* family
* type => ArrayOf (one, many, range, rangedate, tree, search, list)
* values => array(key => label)
* ou sql => array( columns, table)
* ou minmax
*/
protected $items = array (
'siege' => array (
@ -131,57 +122,69 @@ class Scores_Ciblage_Field
'bilYP' => array(),
);
public function __construct()
{
public function __construct(){}
}
public function getItemHtml($name, $valueSelected)
public function getItemHtml($name, $valueSelected = null)
{
if ( !array_key_exists($name, $this->items) ) {
return false;
}
$type = $this->items[$name]['type'];
$values = $this->getItemValues($name);
//Bloc header
$html = '<div class="panel panel-info">';
$html.= '<div class="panel-heading">'.$this->label.'</div>';
$html.= '<div class="panel-heading">'.$this->items[$name]['label'].'</div>';
//Bloc content
$htmlfield = '';
switch ($this->type) {
switch ($type) {
case 'list':
$field = new Scores_Ciblage_Field_List();
$field = new Scores_Ciblage_Field_List($name, $values);
break;
case 'many':
$field = new Scores_Ciblage_Field_Many();
$field = new Scores_Ciblage_Field_Many($name, $values);
break;
case 'one':
$field = new Scores_Ciblage_Field_One();
$field = new Scores_Ciblage_Field_One($name, $values);
break;
case 'range':
$field = new Scores_Ciblage_Field_Range();
$field = new Scores_Ciblage_Field_Range($name, $values);
break;
case 'rangedate':
$field = new Scores_Ciblage_Field_RangeDate();
$field = new Scores_Ciblage_Field_RangeDate($name, $values);
break;
case 'search':
$field = new Scores_Ciblage_Field_Search();
$field = new Scores_Ciblage_Field_Search($name, $values);
break;
case 'tree':
$field = new Scores_Ciblage_Field_Tree();
$field = new Scores_Ciblage_Field_Tree($name, $values);
break;
}
$htmlfield = $field->getHTML($valueSelected);
$html.= '<div class="panel-body">'.$htmlfield.'</div>';
//Bloc footer
$html.= '</div>';
return $html;
}
public function getItemsByFamily($family)
{
return $this->__items(true, $family);
return $this->__items(false, $family);
}
public function getItems()
public function getItems($withValues = false)
{
return $this->__items(true);
return $this->__items($withValues);
}
public function getItem($key)
{
return $this->items[$key];
}
protected function __items($withValues = false, $family = null)
@ -195,7 +198,7 @@ class Scores_Ciblage_Field
$item['desc'] = $info['desc'];
if ( $withValues ) {
$item['values'] = $this->getItemValues($info);
$item['values'] = $this->getItemValues($key);
}
$result[$key] = $item;
@ -206,8 +209,15 @@ class Scores_Ciblage_Field
return $result;
}
protected function getItemValues($info)
/**
* Retourne la liste des valeurs ( value => label )
* @param string $key
* @return array
*/
public function getItemValues($key)
{
$info = $this->items[$key];
$values = array();
if ( array_key_exists('values', $info) ) {
$values = $info['values'];
@ -215,25 +225,25 @@ class Scores_Ciblage_Field
//Override info with values from getMinMax
if ( array_key_exists('minmax', $info) ) {
$values = $this->getMinMax();
$values = $this->getMinMax($key);
}
//Override info with values from Database - Mettre en cache
if ( array_key_exists('sql', $info) ) {
$values = $this->getDbValues();
$values = $this->getDbValues($key);
}
return $values;
}
protected function getMinMax()
protected function getMinMax($key)
{
return array();
}
protected function getDbValues()
protected function getDbValues($key)
{
return array();
}
}

View File

@ -0,0 +1,250 @@
<?php
class Scores_Ciblage_Session
{
/**
* Sauvegarde des paramètres du ciblage
* select => array(
* key => array(
* in =>
* ex =>
* )
* )
* count => total, insee
* @var array
*/
protected $ciblage;
/**
*
* @var Scores_Ciblage_FieldList
*/
protected $fields;
/**
* @param Scores_Ciblage_FieldList $fields
* @param boolean $batch
*/
public function __construct($fields, $batch = false)
{
$this->fields = $fields;
$this->setSession();
}
/**
* Save data to session
*/
protected function setSession()
{
$session = new Zend_Session_Namespace('ciblage');
$session->ciblage = $this->ciblage;
}
/**
* After define criteres and no error return, set the session externaly
* @return void
*/
public function setCritereSession()
{
$this->setSession();
}
/**
* Enregistre un critère et sa valeur en effectuant les controles nécesaire
* @param string $key
* Critere key
* @param mixed $value
* Single value or list of values in string separate by a comma
* @param boolean $ex
* true for an exclude value
* @return boolean
* true if all is ok, false if the key doesn't exist or value is not correct
*/
public function setSelected($key, $value, $ex = false)
{
//Check if the key exist
if ( !array_key_exists($key, $fields) ) {
Zend_Registry::get('firebug')->info('CLE INCORRECT');
return false;
}
//Control values sent
$staticValuesLabel = $this->fields->getItemValues($key);
$staticValues = array_keys($staticValuesLabel);
$valueToSave = null;
if ( is_array($value) ) {
foreach ( $value as $item ) {
if ( in_array($item, $staticValues) ) {
$valueToSave[] = $item;
}
}
} else {
if ( in_array($value, $staticValues) ) {
$valueToSave = array($value);
} else {
return false;
}
}
//Save data in session
if ( $valueToSave !== null ) {
//Save the value
if ($ex) {
Zend_Registry::get('firebug')->info('Exclude : ');
Zend_Registry::get('firebug')->info($value);
$this->ciblage->select[$key]['ex'] = $value;
} else {
Zend_Registry::get('firebug')->info('Include: ');
Zend_Registry::get('firebug')->info($value);
$this->ciblage->select[$key]['in'] = $value;
}
$this->setSession();
return true;
}
return false;
}
/**
* Définir les critères en une fois
* @param array $criteres Criteres as array with key => val
* @param boolean $inSession pour désactiver l'enregistrement en session
*/
public function setSelectedValues($criteres, $inSession = true)
{
$this->ciblage->select = $criteres;
if ( $inSession ) {
$this->setSession();
}
}
/**
* Désactivation d'un critère
* @param string $key
*/
public function unsetSelected($key)
{
if ( array_key_exists($key, $this->ciblage->select) ) {
unset($this->ciblage->select[$key]);
$this->setSession();
}
}
/**
* Permet de supprimer une valeur dans une clef de session
* @param string $key
* Nom de la clé
* @param int $pos
*
* @param boolean $ex
* Include or Exclude values
*/
public function unsetSelectedValue($key, $pos, $exclude = false)
{
if ( array_key_exists($key, $this->ciblage->select) ) {
if ( $ex ) {
$selectedValues = $this->ciblage->select[$key]['ex'];
unset($selectedValues[$pos]);
$this->ciblage->select[$key]['ex'] = array_values($selectedValues);
} else {
$selectedValues = $this->ciblage[$key]['in'];
unset($selectedValues[$pos]);
$this->ciblage->select[$key]['in'] = array_values($selectedValues);
}
$this->setSession();
}
}
/**
* Récupération de la valeur d'un critère de selection
* @param string $key
*/
public function getSelectedValue($key)
{
if ( $this->ciblage === null )
return false;
if ( array_key_exists($key, $this->ciblage->select) ) {
return $this->ciblage->select[$key];
}
return false;
}
/**
* Récupération des critères et de leurs valeurs
*/
public function getSelectedValues()
{
if ( $this->ciblage === null )
return false;
return $this->ciblage->select;
}
/**
*
*/
public function getCountTotal()
{
if ( $this->ciblage === null )
return false;
return $this->ciblage->count->total;
}
/**
*
* @param int $value
*/
public function setCountTotal($value)
{
$this->ciblage->count->total = $value;
$this->setSession();
}
/**
*
*/
public function getCountInsee()
{
if ( $this->ciblage === null )
return false;
return $this->ciblage->count->insee;
}
/**
*
* @param int $value
*/
public function setCountInsee($value)
{
$this->ciblage->count->insee = $value;
$this->setSession();
}
/**
* Suppression de toutes les valeurs en session
*/
public function clearCiblage()
{
Zend_Session::namespaceUnset('ciblage');
}
}