odea/application/controllers/ArborescenceController.php
2012-05-16 07:48:46 +00:00

289 lines
8.0 KiB
PHP

<?php
class ArborescenceController extends Zend_Controller_Action
{
/* Nafs */
public function nafAction()
{
$this->_helper->layout()->disableLayout();
$request = $this->getRequest();
$key = $request->getParam('key');
$niveau = $request->getParam('niveau', 1);
$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;
}
$this->view->key = $key;
$this->view->naf = json_encode($tabNaf);
}
public function nafajaxAction()
{
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender();
$request = $this->getRequest();
$key = $request->getParam('key');
$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));
}
}
}
$niveau++;
$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."%'");
}
$sql->where('niveau = ?', $niveau)->order('code ASC');
$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);
}
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;
}
/* Geographiques */
public function geographiqueAction()
{
$this->_helper->layout()->disableLayout();
$object = new Object_Codepostaux();
$key = $this->getRequest()->getParam('key');
$this->view->key = $key;
$this->view->regions = $object->_getRegions();
}
public function geographiqueajaxAction()
{
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender();
$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));
}
}
/* Forme Juridique */
public function juridiqueAction()
{
$this->_helper->layout()->disableLayout();
$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;
}
$key = $this->getRequest()->getParam('key');
$this->view->key = $key;
$this->view->formejuridiques = json_encode($tabFJ);
}
public function juridiqueajaxAction()
{
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender();
$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);
}
}
}