Gestion régions, départements, codeCommune

This commit is contained in:
Michael RICOIS 2012-05-16 12:00:40 +00:00
parent 5fb0a6c8a0
commit 1444bd6283
2 changed files with 208 additions and 8 deletions

View File

@ -136,12 +136,50 @@ class ArborescenceController extends Zend_Controller_Action
public function geographiqueAction()
{
$this->_helper->layout()->disableLayout();
$key = $this->getRequest()->getParam('key');
$object = new Object_Codepostaux();
$key = $this->getRequest()->getParam('key');
$fields = new Scores_Fields();
$val = $fields->getCritere($key);
$valuesChecked = array();
$valuesUndetermined = array();
if ($val != null){
$valuesChecked = $val['in'];
if (count($valuesChecked)>0) {
foreach($valuesChecked as $value) {
if ( substr($value,0,1)!='R' ) {
$valuesUndetermined = array_merge($valuesUndetermined, $this->getGeoParent($value));
}
}
}
}
$regionsM = new Application_Model_Regions();
$sql = $regionsM->select()->order('NCCENR ASC');
$regions = $regionsM->fetchAll($sql)->toArray();
$tabRegions = array();
foreach($regions as $item) {
$structure = array(
'data' => $item['NCCENR'],
'attr' => array(
'id' => 'R'.$item['REGION'],
'niveau' => 0,
),
'state' => 'closed',
'children' => array()
);
if (in_array('R'.$item['REGION'], $valuesChecked)){
$structure['attr']['class'] = 'jstree-checked';
}
if (in_array('R'.$item['REGION'], $valuesUndetermined)){
$structure['attr']['class'] = 'jstree-undetermined';
}
$tabRegions[] = $structure;
}
$this->view->key = $key;
$this->view->regions = $object->_getRegions();
$this->view->regions = json_encode($tabRegions);
}
public function geographiqueajaxAction()
@ -152,15 +190,110 @@ class ArborescenceController extends Zend_Controller_Action
$request = $this->getRequest();
$id = $request->getParam('id');
$niveau = $request->getParam('niveau');
$object = new Object_Codepostaux();
if($niveau == 0) {
echo ($object->_getDepartements($id));
} else if ($niveau == 1) {
echo ($object->_getCommunes($id));
//Récupération des valeurs enregistrées en session
$fields = new Scores_Fields();
$val = $fields->getCritere('geo');
$valuesChecked = array();
$valuesUndetermined = array();
if ($val != null){
$valuesChecked = $val['in'];
if (count($valuesChecked)>0) {
foreach($valuesChecked as $value){
//$valuesUndetermined = array_merge($valuesUndetermined, $this->getNafParent($value, true));
}
}
}
// R[code] => Régions => recherche département
if ( substr($id,0,1)=='R' ) {
$region = substr($id,1);
$departementsM = new Application_Model_Departements();
$sql = $departementsM->select()->where('codeRegionInsee = ?', $region);
$departements = $departementsM->fetchAll($sql)->toArray();
$tabDepartements = array();
foreach($departements as $item) {
$structure = array(
'data' => $item['libdep'].' ('.$item['numdep'].')',
'attr' => array(
'id' => 'D'.$item['numdep'],
'niveau' => 1,
),
'state' => 'closed',
'children' => array()
);
if (in_array('D'.$item['numdep'], $valuesChecked)){
$structure['attr']['class'] = 'jstree-checked';
}
if (in_array('D'.$item['numdep'], $valuesUndetermined)){
$structure['attr']['class'] = 'jstree-undetermined';
}
$tabDepartements[] = $structure;
}
echo json_encode($tabDepartements);
}
// D[code] => Départements => recherche commune
else if ( substr($id,0,1)=='D' ) {
$departement = substr($id,1);
$codePostauxM = new Application_Model_CodePostaux();
$sql = $codePostauxM->select()
->where('Codepos LIKE ?', $departement.'%')
->order('Commune ASC');
Zend_Registry::get('firebug')->info($sql->__toString());
$communes = $codePostauxM->fetchAll($sql)->toArray();
$tabCommunes = array();
foreach($communes as $item) {
$structure = array(
'data' => $item['Commune'],
'attr' => array(
'id' => 'C'.$item['INSEE'],
'niveau' => 1,
)
);
if (in_array('C'.$item['INSEE'], $valuesChecked)){
$structure['attr']['class'] = 'jstree-checked';
}
$tabCommunes[] = $structure;
}
echo json_encode($tabCommunes);
}
}
protected function getGeoParent($code)
{
if (susbtr($code,0,1)=='D') {
$departementsM = new Application_Model_Departements();
$sql = $departementsM->select()
->from($departementsM, array('codeRegionInsee'))
->where('numdep = ?', substr($code,1));
$departements = $departementsM->fetchRow($sql);
Zend_Registry::get('firebug')->info($departements);
return array('R'.$departements->codeRegionInsee);
} elseif ( substr($code,0,1)=='C' ) {
$codePostauxM = new Application_Model_CodePostaux();
$sql = $codePostauxM->select()
->from($codePostauxM, array('Codepos'))
->where('INSEE = ?', substr($code,1));
$commune = $codePostauxM->fetchRow($sql);
return array('D'.substr($commune->Codepos,0,2));
}
}
/* Forme Juridique */
public function juridiqueAction()
{

View File

@ -703,7 +703,74 @@ class Ciblage
*/
protected function geo($value)
{
$departementValueIn = $departementValueEx = array();
$communeValueIn = $communeValueEx = array();
if ( array_key_exists('in', $value) && is_array($value['in']) && count($value['in'])>0 ) {
foreach ( $value['in'] as $item ) {
switch( substr($item,0,1) ) {
case 'C':
$communeValueIn[] = substr($item,1);
break;
case 'D':
$departementValueIn[] = substr($item,1);
break;
case 'R':
$departementsM = new Application_Model_Departements();
$sql = $departementsM->select()
->form($departementsM, array('numdep'))
->where('codeRegionInsee = ?', substr($item,1));
$departements = $departementsM->fetchAll($sql)->toArray();
foreach ($departements as $d) {
$departementValueIn[] = $d['numdep'];
}
break;
}
}
}
if ( array_key_exists('ex', $value) && is_array($value['ex']) && count($value['ex'])>0 ) {
foreach ( $value['ex'] as $item ) {
switch( substr($item,0,1) ) {
case 'C':
$communeValueEx[] = substr($item,1);
break;
case 'D':
$departementValueEx[] = substr($item,1);
break;
case 'R':
$departementsM = new Application_Model_Departements();
$sql = $departementsM->select()
->form($departementsM, array('numdep'))
->where('codeRegionInsee = ?', substr($item,1));
$departements = $departementsM->fetchAll($sql)->toArray();
foreach ($departements as $d) {
$departementValueEx[] = $d['numdep'];
}
break;
}
}
}
if ( count($departementValueIn)>0 ) {
$this->setFilter('adr_dep', $departementValueIn);
}
if ( count($departementValueEx)>0 ) {
$this->setFilter('adr_dep', $departementValueEx, true);
}
if ( count($departementValueIn)>0 ) {
$this->setFilter('codeCommne', $communeValueIn);
}
if ( count($departementValueEx)>0 ) {
$this->setFilter('codeCommune', $communeValueEx, true);
}
}
protected function getChildCJ($list)