From 1a7e1a113b4af0f531e217344f84e4e54cf325e8 Mon Sep 17 00:00:00 2001 From: Michael RICOIS Date: Wed, 18 Jan 2012 16:30:35 +0000 Subject: [PATCH] On retrouve les valeurs dans l'arborescence des NAFs --- .../controllers/ArborescenceController.php | 83 ++++++++++++++++--- .../default/scripts/arborescence/naf.phtml | 2 +- 2 files changed, 74 insertions(+), 11 deletions(-) diff --git a/application/controllers/ArborescenceController.php b/application/controllers/ArborescenceController.php index a9edd849..7cb08534 100644 --- a/application/controllers/ArborescenceController.php +++ b/application/controllers/ArborescenceController.php @@ -2,8 +2,7 @@ class ArborescenceController extends Zend_Controller_Action { /** - * - * Enter description here ... + * Affiche l'arborescence des NAFs de premier niveau */ public function nafAction() { @@ -13,27 +12,67 @@ class ArborescenceController extends Zend_Controller_Action $key = $request->getParam('key'); $this->view->assign('key', $key); + //Récupération des valeurs enregistrées en session + require_once 'Scores/SessionCiblage.php'; + $sessionCiblage = new SessionCiblage(); + $val = $sessionCiblage->getCritere($key); + $valuesChecked = array(); + $valuesUndetermined = array(); + if ($val != null){ + $valuesChecked = explode(',',$val); + foreach($valuesChecked as $value){ + $valuesUndetermined = $this->getNafParent($value, true); + } + } + $niveau = $request->getParam('niveau', 1); $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){ - $tabNaf[] = array( + foreach($result as $item) + { + $structure = array( 'data' => $item['code'].' - '.$item['lib'], '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; } $this->view->assign('naf', json_encode($tabNaf)); } + /** + * Retourne l'arborecence des NAFs de niveau supérieur à 1 + */ public function nafajaxAction() { $this->_helper->layout()->disableLayout(); $this->_helper->viewRenderer->setNoRender(); + $request = $this->getRequest(); + $key = $request->getParam('key'); + + //Récupération des valeurs enregistrées en session + require_once 'Scores/SessionCiblage.php'; + $sessionCiblage = new SessionCiblage(); + $val = $sessionCiblage->getCritere($key); + $valuesChecked = array(); + $valuesUndetermined = array(); + if ($val != null){ + $valuesChecked = explode(',',$val); + foreach($valuesChecked as $value){ + $valuesUndetermined = $this->getNafParent($value); + } + } + $request = $this->getRequest(); $niveau = $request->getParam('niveau', 1); $niveau++; @@ -50,19 +89,43 @@ class ArborescenceController extends Zend_Controller_Action $result = $nafM->fetchAll($sql)->toArray(); $tabNaf = array(); foreach($result as $item){ - $naf = array( + $structure = array( 'data' => $item['code'].' - '.$item['lib'], - 'attr' => array('id' => $item['code'], 'niveau' => $item['niveau']), + 'attr' => array('id' => $item['code'], 'niveau' => $item['niveau']), ); - if ($niveau<5){ - $naf['state'] = 'closed'; - $naf['children'] = array(); + if (in_array($item['code'], $valuesChecked)){ + $structure['attr']['class'] = 'jstree-checked'; } - $tabNaf[] = $naf; + 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); } + 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; + } + /** * * Enter description here ... diff --git a/application/views/default/scripts/arborescence/naf.phtml b/application/views/default/scripts/arborescence/naf.phtml index c2648f4b..adf6ccf2 100644 --- a/application/views/default/scripts/arborescence/naf.phtml +++ b/application/views/default/scripts/arborescence/naf.phtml @@ -11,7 +11,7 @@ $("#key?>").jstree({ "json_data" : { "data" : naf?>, "ajax" : { - "url" : 'url(array('controller'=>'arborescence', 'action'=>'nafajax'))?>', + "url" : 'url(array('controller'=>'arborescence', 'action'=>'nafajax', 'key'=> $this->key))?>', "data" : function(n) { return { parent: n.attr ? n.attr("id") : '' , niveau : n.attr ? n.attr("niveau") : 1 }; }, "cache" : true, }