Arborescence des NAFs

This commit is contained in:
Michael RICOIS 2012-05-14 15:32:50 +00:00
parent 81f4059371
commit 8bfa2f6f2b
3 changed files with 151 additions and 45 deletions

View File

@ -10,23 +10,26 @@ class Object_Naf
$valuesUndetermined = array();
if ($val != null){
$valuesChecked = explode(',',$val);
foreach($valuesChecked as $value){
$valuesUndetermined = array_merge($valuesUndetermined, $this->getNafParent($value, true));
$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');
$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)
{
$structure = array(
'data' => $item['code'].' - '.$item['lib'],
'attr' => array('id' => $item['code'], 'niveau' => $item['niveau']),
'state' => 'closed',
'children' => 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';

View File

@ -61,6 +61,12 @@ class Ciblage
}
}
/**
*
* @param unknown_type $name
* @param unknown_type $valeur
* @param unknown_type $ex
*/
protected function setFilter($name, $valeur, $ex = false)
{
//All values must be integer
@ -71,18 +77,32 @@ class Ciblage
}
$this->sphinx->SetFilter($name, $valeur, $ex);
}
/**
*
* @param unknown_type $name
* @param unknown_type $min
* @param unknown_type $max
*/
protected function setFilterRange($name, $min, $max)
{
$this->sphinx->SetFilterRange($name, intval($min), intval($max));
}
/**
*
* @param unknown_type $name
* @param unknown_type $valeur
*/
protected function setAlpha($name, $valeur)
{
if ( $this->alpha!='' ) $this->alpha.= ' & ';
$this->alpha.= '@'.$name.' '.$valeur;
}
/**
*
*/
public function extract()
{
$resultats = $this->execute(true);
@ -98,6 +118,9 @@ class Ciblage
return $total;
}
/**
*
*/
public function calculRedevanceInsee()
{
$this->sphinx->SetFilter('presentrcs', array(0));
@ -105,6 +128,10 @@ class Ciblage
return ($resSphinx['total_found']);
}
/**
*
* @param unknown_type $structure
*/
protected function getSiret($structure)
{
$siret = array();
@ -116,6 +143,12 @@ class Ciblage
} else
return (array());
}
/**
*
* @param unknown_type $need
* @param unknown_type $limitD
*/
public function execute($need = false, $limitD = false)
{
if($need) {
@ -138,6 +171,100 @@ class Ciblage
return $resSphinx['total_found'];
}
protected function getChildNaf($list)
{
$lastLevel = array();
if ( count($list)>0 ) {
$nafM = new Application_Model_Naf();
foreach ( $list as $item ) {
$childItem = array();
$result = array();
//First code level is letter, length 1
if ( !is_numeric($item) && strlen($item)==1 ) {
$sql = $nafM->select()->from($nafM, array('code'))->where('parent = ?', strtoupper($item));
Zend_Registry::get('firebug')->info($sql->__toString());
$result = $nafM->fetchAll($sql)->toArray();
}
//Other code level is number, length 2 to 5 => go directy to the last level
$sql = $nafM->select()->from($nafM, array('code'))->where('niveau = 5');
if (count($result)>0) {
$countResult = 0;
$where = '';
foreach ( $result as $r ) {
$where.= 'code LIKE "'.$r['code'].'%"';
$countResult++;
if ( count($result)!=$countResult ) $where.= ' OR ';
}
$sql->where($where);
} else {
$sql->where('code LIKE "'.$item.'%"');
}
Zend_Registry::get('firebug')->info($sql->__toString());
$result = $nafM->fetchAll($sql)->toArray();
if ( count($result)>0 ) {
foreach ( $result as $i ) {
$lastLevel[] = $i['code'];
}
}
}
}
return $lastLevel;
}
/**
*
* @param array $value
* @return string
*/
protected function transformNAF($value)
{
//Search children for all item given
$valueIn = $valueEx = array();
if ( array_key_exists('in', $value) && is_array($value['in']) && count($value['in'])>0 ) {
$valueIn = $this->getChildNaf($value['in']);
}
if ( array_key_exists('ex', $value) && is_array($value['ex']) && count($value['ex'])>0 ) {
$valueEx = $this->getChildNaf($value['ex']);
}
//Construct alpha request for sphinx
$req = '';
if ( count($valueIn)>0 ) {
$req.= '('.implode('|', $valueIn).')';
}
if ( count($valueEx)>0 ) {
if (empty($req)) {
//Get all NAFs and remove excluded
$nafM = new Application_Model_Naf();
$sql = $nafM->select()->from($nafM, array('code'))->where('niveau = 5');
$allnaf = $nafM->fetchAll()->toArray();
$value = array_diff($allnaf, $valueEx);
$req.= '('.implode('|', $value).')';
} else {
//Simply exclude
$req.= ' -('.implode('|', $valueEx).')';
}
}
return $req;
}
/**
*
* @param unknown_type $value
*/
protected function siege($value)
{
if ( array_key_exists('in', $value) ) {
@ -531,49 +658,24 @@ class Ciblage
}
}
/**
*
* @param unknown_type $value
*/
protected function ape_etab($value)
{
$req = '';
if ( array_key_exists('in', $value) && is_array($value['in']) && count($value['in'])>0 ) {
$req.= '('.implode('|', $value['in']).')';
}
if ( array_key_exists('ex', $value) && is_array($value['ex']) && count($value['ex'])>0 ) {
if (empty($req)) {
//Get all NAFs and remove excluded
$nafM = new Application_Model_Naf();
$sql = $nafM->select()->columns('code')->where('niveau = 5');
$allnaf = $nafM->fetchAll()->toArray();
$value = array_diff($allnaf, $value['ex']);
$req.= '('.implode('|', $value).')';
} else {
//Simply exclude
$req.= ' -('.implode('|', $value['ex']).')';
}
}
$req = $this->transformNAF($value);
$this->setAlpha('ape_etab', $req);
}
protected function ape_entrep($value)
{
$req = '';
if ( array_key_exists('in', $value) && is_array($value['in']) && count($value['in'])>0 ) {
$req.= '('.implode('|', $value['in']).')';
}
if ( array_key_exists('ex', $value) && is_array($value['ex']) && count($value['ex'])>0 ) {
if (empty($req)) {
//Get all NAFs and remove excluded
$nafM = new Application_Model_Naf();
$sql = $nafM->select()->columns('code')->where('niveau = 5');
$allnaf = $nafM->fetchAll()->toArray();
$value = array_diff($allnaf, $value['ex']);
$req.= '('.implode('|', $value).')';
} else {
//Simply exclude
$req.= ' -('.implode('|', $value['ex']).')';
}
}
$req = $this->transformNAF($value);
$this->setAlpha('ape_entrep', $req);
}
}

View File

@ -712,7 +712,8 @@ class Scores_Fields
//Remove all space in string value
$value = str_replace(' ', '', $value);
//Try to detect separator et transform as array
if ( preg_match_all('/([0-9A-Z]*)(,|;)/i', $value, $matches) ){
if ( preg_match_all('/([0-9A-Z]{1,5})(,|;){0,1}/', $value, $matches) ){
Zend_Registry::get('firebug')->info($matches);
$value = $matches[1];
}
}