odea/application/controllers/ArborescenceController.php

425 lines
12 KiB
PHP
Raw Normal View History

2012-02-02 17:29:14 +00:00
<?php
2012-05-02 18:19:07 +00:00
class ArborescenceController extends Zend_Controller_Action
2012-02-02 17:29:14 +00:00
{
2012-02-15 09:25:21 +00:00
/* Nafs */
2012-02-02 17:29:14 +00:00
public function nafAction()
{
$this->_helper->layout()->disableLayout();
2012-05-15 11:38:51 +00:00
$request = $this->getRequest();
2012-05-16 07:48:46 +00:00
$key = $request->getParam('key');
$niveau = $request->getParam('niveau', 1);
2012-05-15 11:38:51 +00:00
2012-05-16 07:48:46 +00:00
$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){
$valuesUndetermined = array_merge($valuesUndetermined, $this->getNafParent($value, true));
}
}
}
$nafM = new Application_Model_Naf();
$sql = $nafM->select()->where('niveau = ?', $niveau)->order('code ASC');
$result = $nafM->fetchAll($sql)->toArray();
$tabNaf = array();
foreach($result as $item)
{
if ( $item['niveau']==1 ) {
$data = $item['lib'];
} else {
$data = $item['lib'].' ('.$item['code'].')';
}
$structure = array(
'data' => $data,
'attr' => array('id' => $item['code'], 'niveau' => $item['niveau']),
'state' => 'closed',
'children' => array(),
);
if (in_array($item['code'], $valuesChecked)){
$structure['attr']['class'] = 'jstree-checked';
}
if (in_array($item['code'], $valuesUndetermined)){
$structure['attr']['class'] = 'jstree-undetermined';
}
$tabNaf[] = $structure;
}
2012-02-02 17:29:14 +00:00
$this->view->key = $key;
2012-05-16 07:48:46 +00:00
$this->view->naf = json_encode($tabNaf);
2012-02-02 17:29:14 +00:00
}
public function nafajaxAction()
{
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender();
2012-05-02 18:19:07 +00:00
2012-05-16 07:48:46 +00:00
$request = $this->getRequest();
$key = $request->getParam('key');
2012-07-09 14:06:51 +00:00
$parent = $request->getParam('parent');
2012-05-16 07:48:46 +00:00
$niveau = $request->getParam('niveau', 1);
$niveau = $niveau + 1;
//Récupération des valeurs enregistrées en session
$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){
$valuesUndetermined = array_merge($valuesUndetermined, $this->getNafParent($value, true));
}
}
}
$nafM = new Application_Model_Naf();
$sql = $nafM->select();
if (!empty($parent) && $niveau==2) {
$sql->where('parent = ?', $parent);
} elseif (!empty($parent) && $niveau>2) {
$sql->where("code LIKE '".$parent."%'");
}
2012-07-09 14:06:51 +00:00
$sql->where('niveau = ?', $niveau)->order('code ASC');
2012-05-16 07:48:46 +00:00
$result = $nafM->fetchAll($sql)->toArray();
$tabNaf = array();
foreach($result as $item){
$structure = array(
'data' => $item['code'].' - '.$item['lib'],
'attr' => array('id' => $item['code'], 'niveau' => $item['niveau']),
);
if (in_array($item['code'], $valuesChecked)){
$structure['attr']['class'] = 'jstree-checked';
}
if (in_array($item['code'], $valuesUndetermined)){
$structure['attr']['class'] = 'jstree-undetermined';
}
if ($niveau<5){
$structure['state'] = 'closed';
$structure['children'] = array();
}
$tabNaf[] = $structure;
}
echo json_encode($tabNaf);
2012-02-02 17:29:14 +00:00
}
2012-05-16 07:48:46 +00:00
protected function getNafParent($value, $niveau1 = false)
{
$out = array();
if (strlen($value)>2) {
$niveau = strlen($value)-1;
$new = substr($value,0,$niveau);
$out = array_merge($out, array($new), $this->getNafParent($new, $niveau1));
} elseif (strlen($value)==2 && $niveau1 === true) {
$nafM = new Application_Model_Naf();
$sql = $nafM->select()
->from($nafM, array('parent'))
->where('code = ?', $value);
$result = $nafM->fetchRow($sql);
$out[] = $result->parent;
}
return $out;
}
2012-05-15 15:20:09 +00:00
2012-02-15 09:25:21 +00:00
/* Geographiques */
2012-02-02 17:29:14 +00:00
public function geographiqueAction()
{
$this->_helper->layout()->disableLayout();
$key = $this->getRequest()->getParam('key');
2012-05-02 18:19:07 +00:00
$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;
}
2012-05-02 18:19:07 +00:00
2012-02-15 09:25:21 +00:00
$this->view->key = $key;
$this->view->regions = json_encode($tabRegions);
2012-02-02 17:29:14 +00:00
}
2012-05-02 18:19:07 +00:00
2012-02-02 17:29:14 +00:00
public function geographiqueajaxAction()
{
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender();
2012-05-02 18:19:07 +00:00
2012-02-02 17:29:14 +00:00
$request = $this->getRequest();
$id = $request->getParam('id');
2012-02-15 09:25:21 +00:00
$niveau = $request->getParam('niveau');
2012-05-02 18:19:07 +00:00
//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->getGeoParent($value));
}
}
}
// 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);
2012-02-02 17:29:14 +00:00
}
}
2012-02-15 09:25:21 +00:00
protected function getGeoParent($code)
{
if (substr($code,0,1)=='D') {
$departementsM = new Application_Model_Departements();
$sql = $departementsM->select()
->from($departementsM, array('codeRegionInsee'))
->where('numdep = ?', substr($code,1));
$departement = $departementsM->fetchRow($sql);
return array('R'.$departement->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);
$departement = 'D'.substr($commune->Codepos,0,2);
$region = $this->getGeoParent($departement);
return array_merge(array($departement), $region);
}
return array();
}
2012-02-15 09:25:21 +00:00
/* Forme Juridique */
public function juridiqueAction()
{
$this->_helper->layout()->disableLayout();
2012-05-02 18:19:07 +00:00
2012-05-15 15:20:09 +00:00
$fields = new Scores_Fields();
$val = $fields->getCritere('cj');
$valuesChecked = array();
$valuesUndetermined = array();
if ($val != null){
$valuesChecked = $val['in'];
if (count($valuesChecked)>0) {
foreach($valuesChecked as $value){
if (strlen($value)>1) {
$valuesUndetermined = array_merge($valuesUndetermined, array(substr($value,0,1)));
}
}
}
}
$FormeJuridiqueM = new Application_Model_FormeJuridique();
$sql = $FormeJuridiqueM->select()
->where('LENGTH(fjCode) = 1')
->order('fjLibelle ASC');
$formes = $FormeJuridiqueM->fetchAll($sql)->toArray();
$tabFJ = array();
foreach($formes as $forme) {
$structure = array(
'data' => $forme['fjLibelle'],
'attr' => array( 'id' => $forme['fjCode'] ),
'state' => 'closed',
'children' => array(),
);
if (in_array($forme['fjCode'], $valuesChecked)){
$structure['attr']['class'] = 'jstree-checked';
}
if (in_array($forme['fjCode'], $valuesUndetermined)){
$structure['attr']['class'] = 'jstree-undetermined';
}
$tabFJ[] = $structure;
}
2012-05-15 11:38:51 +00:00
$key = $this->getRequest()->getParam('key');
2012-05-02 18:19:07 +00:00
2012-05-15 15:20:09 +00:00
$this->view->key = $key;
$this->view->formejuridiques = json_encode($tabFJ);
2012-02-15 09:25:21 +00:00
}
2012-05-02 18:19:07 +00:00
2012-02-15 09:25:21 +00:00
public function juridiqueajaxAction()
{
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender();
2012-05-02 18:19:07 +00:00
2012-05-15 15:20:09 +00:00
$request = $this->getRequest();
$id = $request->getParam('id');
$length = strlen($id);
$followingLevel = 5;
if ( $length==1 ) {
$followingLevel = 2;
} elseif ( $length==2 ) {
$followingLevel = 4;
}
$fields = new Scores_Fields();
$val = $fields->getCritere('cj');
$valuesChecked = array();
$valuesUndetermined = array();
if ($val != null){
$valuesChecked = $val['in'];
if (count($valuesChecked)>0) {
foreach($valuesChecked as $value){
if (strlen($value)>$followingLevel) {
$valuesUndetermined = array_merge($valuesUndetermined, substr($value,0,$followingLevel));
}
}
}
}
Zend_Registry::get('firebug')->info($valuesChecked);
if ($length<4) {
$FormeJuridiqueM = new Application_Model_FormeJuridique();
$sql = $FormeJuridiqueM->select()
->where('fjCode LIKE "'.$id.'%"')
->where('LENGTH(fjCode)=?', $followingLevel);
$formes = $FormeJuridiqueM->fetchAll($sql)->toArray();
$tabFJ = array();
foreach($formes as $forme) {
$data = $forme['fjLibelle'];
if ( $followingLevel==4 ) {
$data = $forme['fjLibelle'].' ('.$forme['fjCode'].')';
}
$structure = array(
'data' => $data,
'attr' => array( 'id' => $forme['fjCode'] ),
);
if ( $length<2 ) {
$structure['state'] = 'closed';
$structure['children'] = array();
}
if (in_array($forme['fjCode'], $valuesChecked)){
$structure['attr']['class'] = 'jstree-checked';
}
if (in_array($forme['fjCode'], $valuesUndetermined)){
$structure['attr']['class'] = 'jstree-undetermined';
}
$tabFJ[] = $structure;
}
echo json_encode($tabFJ);
}
2012-02-15 09:25:21 +00:00
}
2012-02-02 17:29:14 +00:00
}