681 lines
14 KiB
PHP
681 lines
14 KiB
PHP
<?php
|
|
/**
|
|
* Ciblage construct the request send to Sphinx
|
|
*/
|
|
class Ciblage
|
|
{
|
|
/**
|
|
* Name of the index store in Sphinx
|
|
* @var string
|
|
*/
|
|
protected $index = 'ciblage';
|
|
|
|
/**
|
|
* Sphinx instantiation
|
|
* @var object
|
|
*/
|
|
protected $sphinx;
|
|
|
|
/**
|
|
* Construct request
|
|
* @var string
|
|
*/
|
|
protected $alpha = '';
|
|
|
|
/**
|
|
*
|
|
* @param array $structure
|
|
* @param boolean $need
|
|
*/
|
|
public function __construct($structure, $need = false)
|
|
{
|
|
Zend_Registry::get('firebug')->info($structure);
|
|
|
|
/*
|
|
* First of all we need to load the Sphinx API
|
|
* @todo : Make a test and load the right version of API
|
|
*/
|
|
require_once 'sphinxapi/sphinxapi-2.0.4.php';
|
|
|
|
$this->need = true;
|
|
|
|
//Load configuration from registry
|
|
$configuration = Zend_Registry::get('configuration');
|
|
|
|
//Sphinx init
|
|
$this->sphinx = new SphinxClient();
|
|
$this->sphinx->SetServer(
|
|
$configuration->sphinx->host,
|
|
intval($configuration->sphinx->port)
|
|
);
|
|
$this->sphinx->SetMatchMode(SPH_MATCH_EXTENDED2);
|
|
$this->sphinx->ResetFilters();
|
|
|
|
//
|
|
if ( count($structure)>0 ) {
|
|
foreach($structure as $key => $valeur) {
|
|
if ($key!= 'NB' && method_exists($this, $key)) {
|
|
$this->{$key}($valeur);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param unknown_type $name
|
|
* @param unknown_type $valeur
|
|
* @param unknown_type $ex
|
|
*/
|
|
protected function setFilter($name, $valeur, $ex = false)
|
|
{
|
|
//All values must be integer
|
|
if ( is_array($valeur) ) {
|
|
$valeur = array_map('intval', $valeur);
|
|
} else {
|
|
$valeur = array(intval($valeur));
|
|
}
|
|
$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);
|
|
$siret = array();
|
|
foreach ($resutlats['matches'] as $element) {
|
|
$siret[] = $element['attrs']['siren'].$element['attrs']['nic'];
|
|
}
|
|
$total = array(
|
|
'total' => count($resultats),
|
|
'insee' => $this->calculRedevanceInsee(),
|
|
'result'=> $siret
|
|
);
|
|
return $total;
|
|
}
|
|
|
|
/**
|
|
*
|
|
*/
|
|
public function calculRedevanceInsee()
|
|
{
|
|
$this->sphinx->SetFilter('presentrcs', array(0));
|
|
$resSphinx = $this->sphinx->Query($this->alpha, $this->index);
|
|
return ($resSphinx['total_found']);
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param unknown_type $structure
|
|
*/
|
|
protected function getSiret($structure)
|
|
{
|
|
$siret = array();
|
|
if(count($structure['matches'])>0) {
|
|
foreach($structure['matches'] as $element){
|
|
$siret[] = $element['attrs']['siren'].$element['attrs']['nic'];
|
|
}
|
|
return ($siret);
|
|
} else
|
|
return (array());
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param unknown_type $need
|
|
* @param unknown_type $limitD
|
|
*/
|
|
public function execute($need = false, $limitD = false)
|
|
{
|
|
if($need) {
|
|
$return = array();
|
|
$limit = 0;
|
|
do {
|
|
$this->sphinx->SetLimits($limit, (($limitD!=false)?10:1000), 50000);
|
|
$resSphinx = $this->sphinx->Query($this->alpha, $this->index);
|
|
if(is_array($resSphinx))
|
|
$return = array_merge($return, $this->getSiret($resSphinx));
|
|
$limit = $limit + 1000;
|
|
}while($limit < (($limitD!=false)?10:$resSphinx['total_found']));
|
|
return ($return);
|
|
} else {
|
|
$this->sphinx->SetLimits(0, 1);
|
|
$resSphinx = $this->sphinx->Query($this->alpha, $this->index);
|
|
}
|
|
Zend_Registry::get('firebug')->info("Sphinx : ".$this->alpha);
|
|
Zend_Registry::get('firebug')->info($resSphinx);
|
|
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) ) {
|
|
$this->setFilter('siege', $value);
|
|
}
|
|
}
|
|
|
|
//Valeur NULL dans BDD
|
|
protected function groupe($value)
|
|
{
|
|
if ( array_key_exists('in', $value) ) {
|
|
//$this->setFilter('sirenGrp', $value);
|
|
}
|
|
}
|
|
|
|
protected function tel($value)
|
|
{
|
|
if ( array_key_exists('in', $value) ) {
|
|
$this->setFilter('tel', $value);
|
|
}
|
|
}
|
|
|
|
protected function fax($value)
|
|
{
|
|
if ( array_key_exists('in', $value) ) {
|
|
$this->setFilter('fax', $value);
|
|
}
|
|
}
|
|
|
|
protected function web($value)
|
|
{
|
|
if ( array_key_exists('in', $value) ) {
|
|
$this->setFilter('web', $value);
|
|
}
|
|
}
|
|
|
|
protected function mail($value)
|
|
{
|
|
if ( array_key_exists('in', $value) ) {
|
|
$this->setFilter('mail', $value);
|
|
}
|
|
}
|
|
|
|
protected function presentRcs($value)
|
|
{
|
|
if ( array_key_exists('in', $value) ) {
|
|
$this->setFilter('presentRcs', $value);
|
|
}
|
|
}
|
|
|
|
protected function adrDom($value)
|
|
{
|
|
if ( array_key_exists('in', $value) ) {
|
|
$this->setFilter('adrDom', $value);
|
|
}
|
|
}
|
|
|
|
protected function dirNom($value)
|
|
{
|
|
if ( array_key_exists('in', $value) ) {
|
|
$this->setFilter('dirNom', $value);
|
|
}
|
|
}
|
|
|
|
protected function participation($value)
|
|
{
|
|
if ( array_key_exists('in', $value) ) {
|
|
$this->setFilter('part', $value);
|
|
}
|
|
}
|
|
|
|
protected function action($value)
|
|
{
|
|
if ( array_key_exists('in', $value) ) {
|
|
$this->setFilter('actio', $value);
|
|
}
|
|
}
|
|
|
|
//Don't work
|
|
protected function dateCrea_etab($value)
|
|
{
|
|
|
|
}
|
|
|
|
//Don't work
|
|
protected function dateCrea_ent($value)
|
|
{
|
|
|
|
}
|
|
|
|
protected function nbActio($value)
|
|
{
|
|
if ( array_key_exists('in', $value) ) {
|
|
$min = $value['in'][0];
|
|
$max = $value['in'][1];
|
|
$this->setFilterRange('nbActio', $min, $max);
|
|
}
|
|
}
|
|
|
|
protected function nbPart($value)
|
|
{
|
|
if ( array_key_exists('in', $value) ) {
|
|
$min = $value['in'][0];
|
|
$max = $value['in'][1];
|
|
$this->setFilterRange('nbPart', $min, $max);
|
|
}
|
|
}
|
|
|
|
protected function nbMPubli($value)
|
|
{
|
|
if ( array_key_exists('in', $value) ) {
|
|
$this->setFilter('nbMPubli', $value);
|
|
}
|
|
}
|
|
|
|
protected function capital($value)
|
|
{
|
|
if ( array_key_exists('in', $value) ) {
|
|
$min = $value['in'][0];
|
|
$max = $value['in'][1];
|
|
$this->setFilterRange('capital', $min, $max);
|
|
}
|
|
}
|
|
|
|
protected function age_entrep($value)
|
|
{
|
|
if ( array_key_exists('in', $value) ) {
|
|
$min = $value['in'][0];
|
|
$max = $value['in'][1];
|
|
$this->setFilterRange('age_entrep', $min, $max);
|
|
}
|
|
}
|
|
|
|
protected function age_etab($value)
|
|
{
|
|
if ( array_key_exists('in', $value) ) {
|
|
$min = $value['in'][0];
|
|
$max = $value['in'][1];
|
|
$this->setFilterRange('age_etab', $min, $max);
|
|
}
|
|
}
|
|
|
|
protected function teff_entrep($value)
|
|
{
|
|
if ( array_key_exists('in', $value) ) {
|
|
$this->setFilter('teff_entrep', $value);
|
|
}
|
|
if ( array_key_exists('ex', $value) ) {
|
|
$this->setFilter('teff_entrep', $value, true);
|
|
}
|
|
}
|
|
|
|
protected function teff_etab($value)
|
|
{
|
|
if ( array_key_exists('in', $value) ) {
|
|
$this->setFilter('teff_etab', $value);
|
|
}
|
|
if ( array_key_exists('ex', $value) ) {
|
|
$this->setFilter('teff_etab', $value, true);
|
|
}
|
|
}
|
|
|
|
protected function geo_domtom($value)
|
|
{
|
|
if( $value['in']==1 ) {
|
|
$this->setFilter('adr_dep', array(971, 972, 973, 974, 976), true);
|
|
}
|
|
}
|
|
|
|
protected function geo_etranger($value)
|
|
{
|
|
if( $value['in']==1 ) {
|
|
$this->setFilter('adr_dep', array(986, 987, 988), true);
|
|
}
|
|
}
|
|
|
|
protected function geo_corse($value)
|
|
{
|
|
if( $value['in']==1 ) {
|
|
$this->setFilter('adr_dep', array(201, 202), true);
|
|
}
|
|
}
|
|
|
|
protected function zus($value)
|
|
{
|
|
if ( array_key_exists('in', $value) ) {
|
|
$this->setFilter('zus', $value);
|
|
}
|
|
}
|
|
|
|
protected function zru($value)
|
|
{
|
|
if ( array_key_exists('in', $value) ) {
|
|
$this->setFilter('zru', $value);
|
|
}
|
|
}
|
|
|
|
protected function zfu($value)
|
|
{
|
|
if ( array_key_exists('in', $value) ) {
|
|
$this->setFilter('zfu', $value);
|
|
}
|
|
}
|
|
|
|
protected function cucs($value)
|
|
{
|
|
if ( array_key_exists('in', $value) ) {
|
|
$this->setFilter('cucs', $value);
|
|
}
|
|
}
|
|
|
|
protected function zrr($value)
|
|
{
|
|
if ( array_key_exists('in', $value) ) {
|
|
$this->setFilter('zrr', $value);
|
|
}
|
|
}
|
|
|
|
protected function zafr($value)
|
|
{
|
|
if ( array_key_exists('in', $value) ) {
|
|
$this->setFilter('zafr', $value);
|
|
}
|
|
}
|
|
|
|
protected function actifEco($value)
|
|
{
|
|
if ( array_key_exists('in', $value) ) {
|
|
$this->setFilter('actifEco', $value);
|
|
}
|
|
}
|
|
|
|
protected function procolHisto($value)
|
|
{
|
|
if ( array_key_exists('in', $value) ) {
|
|
$this->setFilter('procolHisto', $value);
|
|
}
|
|
}
|
|
|
|
protected function tvaIntraValide($value)
|
|
{
|
|
if ( array_key_exists('in', $value) ) {
|
|
$this->setFilter('tvaIntraValide', $value);
|
|
}
|
|
}
|
|
|
|
protected function bilType($value)
|
|
{
|
|
if ( array_key_exists('in', $value) ) {
|
|
$this->setFilter('bilType', $value);
|
|
}
|
|
if ( array_key_exists('ex', $value) ) {
|
|
$this->setFilter('bilType', $value, true);
|
|
}
|
|
}
|
|
|
|
protected function avisCs($value)
|
|
{
|
|
if ( array_key_exists('in', $value) ) {
|
|
$this->setFilter('avisCs', $value);
|
|
}
|
|
if ( array_key_exists('ex', $value) ) {
|
|
$this->setFilter('avisCs', $value, true);
|
|
}
|
|
}
|
|
|
|
protected function bilTca($value)
|
|
{
|
|
if ( array_key_exists('in', $value) ) {
|
|
$this->setFilter('bilTca', $value);
|
|
}
|
|
if ( array_key_exists('ex', $value) ) {
|
|
$this->setFilter('bilTca', $value, true);
|
|
}
|
|
}
|
|
|
|
protected function bilEE($value)
|
|
{
|
|
if ( array_key_exists('in', $value) ) {
|
|
$min = $value['in'][0];
|
|
$max = $value['in'][1];
|
|
$this->setFilterRange('bilEE', $min, $max);
|
|
}
|
|
}
|
|
|
|
protected function bilFL($value)
|
|
{
|
|
if ( array_key_exists('in', $value) ) {
|
|
$min = $value['in'][0];
|
|
$max = $value['in'][1];
|
|
$this->setFilterRange('bilFL', $min, $max);
|
|
}
|
|
}
|
|
|
|
protected function bilFK($value)
|
|
{
|
|
if ( array_key_exists('in', $value) ) {
|
|
$min = $value['in'][0];
|
|
$max = $value['in'][1];
|
|
$this->setFilterRange('bilFK', $min, $max);
|
|
}
|
|
}
|
|
|
|
protected function bilFR($value)
|
|
{
|
|
if ( array_key_exists('in', $value) ) {
|
|
$min = $value['in'][0];
|
|
$max = $value['in'][1];
|
|
$this->setFilterRange('bilFR', $min, $max);
|
|
}
|
|
}
|
|
|
|
protected function bilGF($value)
|
|
{
|
|
if ( array_key_exists('in', $value) ) {
|
|
$min = $value['in'][0];
|
|
$max = $value['in'][1];
|
|
$this->setFilterRange('bilGF', $min, $max);
|
|
}
|
|
}
|
|
|
|
protected function bilGP($value)
|
|
{
|
|
if ( array_key_exists('in', $value) ) {
|
|
$min = $value['in'][0];
|
|
$max = $value['in'][1];
|
|
$this->setFilterRange('bilGP', $min, $max);
|
|
}
|
|
}
|
|
|
|
protected function bilGW($value)
|
|
{
|
|
if ( array_key_exists('in', $value) ) {
|
|
$min = $value['in'][0];
|
|
$max = $value['in'][1];
|
|
$this->setFilterRange('bilGW', $min, $max);
|
|
}
|
|
}
|
|
|
|
protected function bilHD($value)
|
|
{
|
|
if ( array_key_exists('in', $value) ) {
|
|
$min = $value['in'][0];
|
|
$max = $value['in'][1];
|
|
$this->setFilterRange('bilHD', $min, $max);
|
|
}
|
|
}
|
|
|
|
protected function bilHH($value)
|
|
{
|
|
if ( array_key_exists('in', $value) ) {
|
|
$min = $value['in'][0];
|
|
$max = $value['in'][1];
|
|
$this->setFilterRange('bilHH', $min, $max);
|
|
}
|
|
}
|
|
|
|
protected function bilHL($value)
|
|
{
|
|
if ( array_key_exists('in', $value) ) {
|
|
$min = $value['in'][0];
|
|
$max = $value['in'][1];
|
|
$this->setFilterRange('bilHL', $min, $max);
|
|
}
|
|
}
|
|
|
|
protected function bilHM($value)
|
|
{
|
|
if ( array_key_exists('in', $value) ) {
|
|
$min = $value['in'][0];
|
|
$max = $value['in'][1];
|
|
$this->setFilterRange('bilHM', $min, $max);
|
|
}
|
|
}
|
|
|
|
protected function bilHN($value)
|
|
{
|
|
if ( array_key_exists('in', $value) ) {
|
|
$min = $value['in'][0];
|
|
$max = $value['in'][1];
|
|
$this->setFilterRange('bilHN', $min, $max);
|
|
}
|
|
}
|
|
|
|
protected function bilYP($value)
|
|
{
|
|
if ( array_key_exists('in', $value) ) {
|
|
$min = $value['in'][0];
|
|
$max = $value['in'][1];
|
|
$this->setFilterRange('bilYP', $min, $max);
|
|
}
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param unknown_type $value
|
|
*/
|
|
protected function ape_etab($value)
|
|
{
|
|
$req = $this->transformNAF($value);
|
|
$this->setAlpha('ape_etab', $req);
|
|
}
|
|
|
|
|
|
protected function ape_entrep($value)
|
|
{
|
|
$req = $this->transformNAF($value);
|
|
$this->setAlpha('ape_entrep', $req);
|
|
}
|
|
|
|
|
|
|
|
|
|
} |