515 lines
14 KiB
PHP
515 lines
14 KiB
PHP
<?php
|
|
require_once 'sphinxapi/sphinxapi-2.0.2.php';
|
|
|
|
Class Ciblage
|
|
{
|
|
private $sphinx;
|
|
private $alpha = '';
|
|
private $presentRcs = 0;
|
|
|
|
public $index = 'ciblage';
|
|
|
|
public $nb_results = 5000;
|
|
public $max_results = 500000;
|
|
|
|
public $sphinx_match = SPH_MATCH_EXTENDED2;
|
|
public $sphinx_sort = SPH_SORT_EXTENDED;
|
|
|
|
protected function setMinMax($name, $valeur)
|
|
{
|
|
/*if(!$this->need) {*/
|
|
if(!empty($name)) {
|
|
$default = Fields::getMinMax($name);
|
|
|
|
if(empty($valeur[0])){$valeur[0] = $default[0]['min'];}
|
|
if(empty($valeur[1])){$valeur[1] = $default[0]['max'];}
|
|
}
|
|
return ($valeur);
|
|
/*}*/
|
|
return ($valeur);
|
|
}
|
|
public function __construct($structure, $need = false)
|
|
{
|
|
$this->need = true;
|
|
$configuration = Zend_Registry::get('configuration');
|
|
|
|
//Instantiation Sphinx
|
|
$this->sphinx = new SphinxClient();
|
|
$this->sphinx->SetServer($configuration->sphinx->host, intval($configuration->sphinx->port));
|
|
$this->sphinx->SetMatchMode(SPH_MATCH_EXTENDED2);
|
|
$this->sphinx->ResetFilters();
|
|
foreach($structure as $key => $valeur) {
|
|
if (method_exists($this, $key)) {
|
|
if(is_array($valeur)) {
|
|
if(isset($valeur[0])) {
|
|
if($valeur[0] != "" and $valeur[0] != 'tous')
|
|
$this->$key($valeur);
|
|
}
|
|
} else {
|
|
if($valeur != 'tous' and $valeur != "")
|
|
$this->$key($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']);
|
|
}
|
|
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());
|
|
}
|
|
public function execute($need = false, $limit = false)
|
|
{
|
|
//echo $this->alpha;
|
|
if($need) {
|
|
$return = array();
|
|
$limit = 0;
|
|
do {
|
|
$this->sphinx->SetLimits($limit, 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)?$limitD:$resSphinx['total_found']);
|
|
return ($return);
|
|
} else
|
|
$resSphinx = $this->sphinx->Query($this->alpha, $this->index);
|
|
return ($resSphinx['total_found']);
|
|
}
|
|
protected function setFilter($name, $valeur, $ex = false) {
|
|
if(!is_array($valeur)) {
|
|
$this->sphinx->SetFilter($name, array(intval($valeur)),($ex)?true:false);
|
|
}else {
|
|
$this->sphinx->SetFilter($name, $this->transformIntval($valeur), ($ex)?true:false);
|
|
}
|
|
}
|
|
protected function setFilterRange($name, $valeur) {
|
|
$this->sphinx->SetFilterRange($name, intval($valeur[0]), intval($valeur[1]));
|
|
}
|
|
|
|
private function getListeDepartement()
|
|
{
|
|
$table = new Table_Departements();
|
|
$sql = $table->select();
|
|
$result = $table->fetchAll($sql)->toArray();
|
|
foreach ($result as $dept) {
|
|
$depts .= $dept['numdep'].' | ';
|
|
}
|
|
$depts = substr($depts, 0, strlen($depts)-2);
|
|
return ($depts);
|
|
}
|
|
protected function writeRequest($name, $valeur, $ex, $or = false) {
|
|
if(in_array($name, array('adr_com_ex', 'adr_dept_ex', 'bilType_ex', 'ape_etab_ex', 'ape_entrep_ex'))) {
|
|
if(!strstr($this->alpha, 'adr_dept') or
|
|
!strstr($this->alpha, 'ape_etab') or
|
|
!strstr($this->alpha, 'bilType') or
|
|
!strstr($this->alpha, 'ape_entrep')) {
|
|
$this->alpha .= ' @exclu (ex) ';
|
|
}
|
|
switch($name) {
|
|
case 'adr_com_ex':
|
|
$name = 'adr_com';
|
|
break;
|
|
case 'ape_entrep_ex':
|
|
$name = 'ape_entrep';
|
|
break;
|
|
case 'ape_etab_ex':
|
|
$name = 'ape_etab';
|
|
break;
|
|
case 'adr_dept_ex':
|
|
$name = 'adr_dept';
|
|
break;
|
|
case 'bilType_ex':
|
|
$name = 'bilType';
|
|
break;
|
|
}
|
|
}
|
|
$this->alpha .= ((!empty($this->alpha))?' ':'').'@'.$name . (($ex)?" -":" ").'(';
|
|
if(!is_array($valeur)) {
|
|
$this->alpha .= $valeur.')';
|
|
} else {
|
|
$i = 0;
|
|
foreach($valeur as $item) {
|
|
if($i != 0)
|
|
$this->alpha .= ' | ';
|
|
$this->alpha .= $item;
|
|
$i++;
|
|
}
|
|
$this->alpha .= ')';
|
|
}
|
|
|
|
}
|
|
protected function setAlpha($name, $valeur, $ex = false)
|
|
{
|
|
$or = array('adr_com', 'adr_dept', 'adr_com_ex', 'adr_dept_ex');
|
|
$and = array('bilType', 'ape_etab_ex', 'ape_etab','ape_entrep', 'ape_entrep_ex', 'NaceEntrep', 'NaceEtab', 'bilType_ex');
|
|
if(!empty($this->alpha))
|
|
$this->alpha .= ' ';
|
|
if(in_array($name, $or)) {
|
|
if(!empty($valeur)) {
|
|
$this->writeRequest($name, $valeur, $ex);
|
|
}
|
|
} else if(in_array($name, $and)) {
|
|
if(!empty($valeur))
|
|
$this->writeRequest($name, $valeur, $ex);
|
|
}
|
|
}
|
|
protected function transformIntval($array) {
|
|
$copy = array();
|
|
foreach($array as $item => $valeur) {
|
|
$copy[$item] = intval($valeur);
|
|
}
|
|
return ($copy);
|
|
}
|
|
protected function dirNom($valeur) {
|
|
$this->setFilter('dirNom', $valeur);
|
|
}
|
|
protected function siege($valeur) {
|
|
$this->setFilter('siege', $valeur);
|
|
}
|
|
protected function adr_com($valeur) {
|
|
$this->setAlpha('adr_com', $valeur);
|
|
}
|
|
protected function adr_com_ex($valeur) {
|
|
$this->setAlpha('adr_com_ex', $valeur, true);
|
|
}
|
|
protected function adr_dept($valeur) {
|
|
$this->setAlpha('adr_dept', $valeur);
|
|
}
|
|
protected function adr_dept_ex($valeur) {
|
|
$this->setAlpha('adr_dept_ex', $valeur, true);
|
|
}
|
|
protected function action($valeur) {
|
|
$this->setFilter('action', $valeur);
|
|
}
|
|
protected function part($valeur) {
|
|
$this->setFilter('part', $valeur);
|
|
}
|
|
protected function tel($valeur) {
|
|
$this->setFilter('tel', $valeur);
|
|
}
|
|
protected function fax($valeur) {
|
|
$this->setFilter('fax', $valeur);
|
|
}
|
|
protected function cj($valeur) {
|
|
$this->setFilter('cj', $valeur);
|
|
}
|
|
protected function cj_ex($valeur) {
|
|
$this->setFilter('cj', $valeur, true);
|
|
}
|
|
protected function capital($valeur) {
|
|
$valeur = $this->setMinMax($name, $valeur);
|
|
|
|
$this->setFilterRange('capital', $valeur);
|
|
}
|
|
protected function ape_etab($valeur) {
|
|
$this->setAlpha('ape_etab', $valeur);
|
|
}
|
|
protected function ape_etab_ex($valeur) {
|
|
$this->setAlpha('ape_etab_ex', $valeur, true);
|
|
}
|
|
protected function ape_entrep($valeur) {
|
|
$this->setAlpha('ape_entrep', $valeur);
|
|
}
|
|
protected function ape_entrep_ex($valeur) {
|
|
$this->setAlpha('ape_entrep_ex', $valeur, true);
|
|
}
|
|
protected function age_entrep($valeur) {
|
|
$this->setAlpha('age_entrep', $valeur);
|
|
}
|
|
protected function age_etab($valeur) {
|
|
$this->setAlpha('age_etab', $valeur);
|
|
}
|
|
protected function tcaexp($valeur) {
|
|
$this->setFilterRange('tcaexp', $valeur);
|
|
}
|
|
protected function teff_entrep($valeur) {
|
|
if($valeur[0] != 0 or $valeur[1] != 0)
|
|
$this->setFilterRange('teff_entrep', $valeur);
|
|
}
|
|
protected function teff_etab($valeur) {
|
|
//$valeur = $this->setMinMax($name, $valeur);
|
|
|
|
$this->setFilterRange('teff_etab', $valeur);
|
|
}
|
|
protected function rang($valeur) {
|
|
$this->setFilterRange('rang', $valeur);
|
|
}
|
|
protected function actifEco($valeur) {
|
|
$this->setFilter('actifEco', $valeur);
|
|
}
|
|
protected function presentRcs($valeur) {
|
|
$this->setFilter('presentRcs', $valeur);
|
|
}
|
|
protected function procolHisto($valeur) {
|
|
$this->setFilter('procolHisto', $valeur);
|
|
}
|
|
protected function tvaIntraValide($valeur) {
|
|
$this->setFilter('tvaIntraValide', $valeur);
|
|
}
|
|
protected function NaceEtab($valeur) {
|
|
$this->setAlpha('NaceEtab', $valeur);
|
|
}
|
|
protected function NaceEntrep($valeur) {
|
|
$this->setAlpha('NaceEntrep', $valeur);
|
|
}
|
|
protected function dateCrea_etab($valeur) {
|
|
if(empty($valeur[0])) {$valeur[0] = date('Ymd');}
|
|
if(empty($valeur[1])) {$valeur[1] = date('Ymd');}
|
|
|
|
if(!is_array($valeur)) {
|
|
$format = explode('/', $valeur);
|
|
$valeur = $format[2].$format[1].$format[0];
|
|
} else {
|
|
$format1 = explode('/', $valeur[0]);
|
|
$format2 = explode('/', $valeur[1]);
|
|
|
|
$valeur[0] = $format1[2].$format1[1].$format1[0];
|
|
$valeur[1] = $format2[2].$format2[1].$format2[0];
|
|
}
|
|
$this->setFilterRange('dateCrea_etab', $valeur);
|
|
|
|
}
|
|
protected function dateCrea_ent($valeur) {
|
|
if(empty($valeur[0])) {$valeur[0] = date('Ymd');}
|
|
if(empty($valeur[1])) {$valeur[1] = date('Ymd');}
|
|
|
|
if(!is_array($valeur)) {
|
|
$format = explode('/', $valeur);
|
|
$valeur = $format[2].$format[1].$format[0];
|
|
} else {
|
|
$format1 = explode('/', $valeur[0]);
|
|
$format2 = explode('/', $valeur[1]);
|
|
|
|
$valeur[0] = $format1[2].$format1[1].$format1[0];
|
|
$valeur[1] = $format2[2].$format2[1].$format2[0];
|
|
}
|
|
$this->setFilterRange('dateCrea_etab', $valeur);
|
|
}
|
|
protected function dateImmat($valeur) {
|
|
if(empty($valeur[0])) {$valeur[0] = date('Ymd');}
|
|
if(empty($valeur[1])) {$valeur[1] = date('Ymd');}
|
|
|
|
if(!is_array($valeur)) {
|
|
$format = explode('/', $valeur);
|
|
$valeur = $format[2].$format[1].$format[0];
|
|
} else {
|
|
$format1 = explode('/', $valeur[0]);
|
|
$format2 = explode('/', $valeur[0]);
|
|
|
|
$valeur[0] = $format1[2].$format1[1].$format1[0];
|
|
$valeur[1] = $format2[2].$format2[1].$format2[0];
|
|
}
|
|
$this->setFilterRange('dateImmat', $valeur);
|
|
}
|
|
protected function eff_entrep($valeur) {
|
|
$valeur = $this->setMinMax($name, $valeur);
|
|
|
|
$this->setFilter('eff_entrep', $valeur);
|
|
}
|
|
protected function eff_etab($valeur) {
|
|
$valeur = $this->setMinMax($name, $valeur);
|
|
|
|
$this->setFilter('eff_etab', $valeur);
|
|
}
|
|
protected function rivoli($valeur) {
|
|
$this->setFilter('rivoli', $valeur);
|
|
}
|
|
protected function nbEtab($valeur) {
|
|
$valeur = $this->setMinMax($name, $valeur);
|
|
$this->setFilterRange('nbEtab', $valeur);
|
|
}
|
|
protected function sirenGrp($valeur) {
|
|
$this->setFilter('sirenGrp', $valeur);
|
|
}
|
|
protected function nbActio($valeur) {
|
|
$valeur = $this->setMinMax('nbActio', $valeur);
|
|
|
|
$this->setFilterRange('nbActio', $valeur);
|
|
}
|
|
protected function nbMPubli($valeur) {
|
|
$this->setFilter('nbMPubli', $valeur);
|
|
}
|
|
protected function nbPart($valeur) {
|
|
$valeur = $this->setMinMax('nbPart', $valeur);
|
|
|
|
$this->setFilterRange('nbPart', $valeur);
|
|
}
|
|
protected function bilDuree($valeur) {
|
|
$this->setFilterRange('bilDuree', $valeur);
|
|
}
|
|
protected function bilType($valeur) {
|
|
foreach ($valeur as $val) {
|
|
$string .= '-'.$val;
|
|
}
|
|
$string = substr($string, 1);
|
|
$valeur = explode('-', $string);
|
|
$this->setAlpha('bilType', $valeur);
|
|
}
|
|
protected function bilType_ex($valeur) {
|
|
foreach ($valeur as $val) {
|
|
$string .= '-'.$val;
|
|
}
|
|
$string = substr($string, 1);
|
|
$valeur = explode('-', $string);
|
|
$this->setAlpha('bilType_ex', $valeur, true);
|
|
}
|
|
protected function bilAnnee($valeur) {
|
|
$this->setFilter('bilAnnee', $valeur);
|
|
}
|
|
protected function bilTca($valeur) {
|
|
$this->setFilter('bilTca', $valeur);
|
|
}
|
|
protected function bilTca_ex($valeur) {
|
|
$this->setFilter('bilTca', $valeur, true);
|
|
}
|
|
protected function bilEE($valeur) {
|
|
$valeur = $this->setMinMax('bilEE', $valeur);
|
|
$this->setFilterRange('bilEE', $valeur);
|
|
}
|
|
protected function bilFL($valeur) {
|
|
$valeur = $this->setMinMax('bilFL', $valeur);
|
|
$this->setFilterRange('bilFL', $valeur);
|
|
}
|
|
protected function bilFK($valeur) {
|
|
$valeur = $this->setMinMax('bilFK', $valeur);
|
|
$this->setFilterRange('bilFK', $valeur);
|
|
}
|
|
protected function bilFR($valeur) {
|
|
$valeur = $this->setMinMax('bilFR', $valeur);
|
|
$this->setFilterRange('bilFR', $valeur);
|
|
}
|
|
protected function bilGF($valeur) {
|
|
$valeur = $this->setMinMax('bilGF', $valeur);
|
|
$this->setFilterRange('bilGF', $valeur);
|
|
}
|
|
protected function bilGP($valeur) {
|
|
$valeur = $this->setMinMax('bilGP', $valeur);
|
|
$this->setFilterRange('bilGP', $valeur);
|
|
}
|
|
protected function bilGU($valeur) {
|
|
$valeur = $this->setMinMax('bilGU', $valeur);
|
|
$this->setFilterRange('bilGU', $valeur);
|
|
}
|
|
protected function bilGW($valeur) {
|
|
$valeur = $this->setMinMax('bilGW', $valeur);
|
|
$this->setFilterRange('bilGW', $valeur);
|
|
}
|
|
protected function bilHD($valeur) {
|
|
$valeur = $this->setMinMax('bilHD', $valeur);
|
|
$this->setFilterRange('bilHD', $valeur);
|
|
}
|
|
protected function bilHL($valeur) {
|
|
$valeur = $this->setMinMax('bilHL', $valeur);
|
|
$this->setFilterRange('bilHL', $valeur);
|
|
}
|
|
protected function bilHM($valeur) {
|
|
$valeur = $this->setMinMax('bilHM', $valeur);
|
|
$this->setFilterRange('bilHM', $valeur);
|
|
}
|
|
protected function bilHN($valeur) {
|
|
$valeur = $this->setMinMax('bilHN', $valeur);
|
|
$this->setFilterRange('bilHN', $valeur);
|
|
}
|
|
protected function bilYP($valeur) {
|
|
$valeur = $this->setMinMax('bilYP', $valeur);
|
|
$this->setFilterRange('bilYP', $valeur);
|
|
}
|
|
protected function avisCs($valeur) {
|
|
foreach ($valeur as $val) {
|
|
$string .= '-'.$val;
|
|
}
|
|
$string = substr($string, 1);
|
|
$valeur = explode('-', $string);
|
|
$this->setFilter('avisCs', $valeur);
|
|
}
|
|
protected function avisCs_ex($valeur) {
|
|
foreach ($valeur as $val) {
|
|
$string .= '-'.$val;
|
|
}
|
|
$string = substr($string, 1);
|
|
$valeur = explode('-', $string);
|
|
$this->setFilter('avisCs', $valeur, true);
|
|
}
|
|
protected function bilFS($valeur) {
|
|
$this->setFilterRange('bilFS', $valeur);
|
|
}
|
|
protected function lieuAct($valeur) {
|
|
$this->setFilter('lieuAct', $valeur);
|
|
}
|
|
protected function bilGG($valeur) {
|
|
$this->setFilterRange('bilGG', $valeur);
|
|
}
|
|
protected function bilGV($valeur) {
|
|
$this->setFilterRange('bilGV', $valeur);
|
|
}
|
|
protected function bilHI($valeur) {
|
|
$this->setFilterRange('bilHI', $valeur);
|
|
}
|
|
protected function adrDom($valeur) {
|
|
$this->setFilter('adrDom', $valeur);
|
|
}
|
|
protected function web($valeur) {
|
|
$this->setFilter('web', $valeur);
|
|
}
|
|
protected function mail($valeur) {
|
|
$this->setFilter('mail', $valeur);
|
|
}
|
|
protected function bilCloture($valeur) {
|
|
if(!is_array($valeur)) {
|
|
$format = explode('/', $valeur);
|
|
$valeur = $format[2].$format[1].$format[0];
|
|
} else {
|
|
$format1 = explode('/', $valeur[0]);
|
|
$format2 = explode('/', $valeur[0]);
|
|
|
|
$valeur[0] = $format1[2].$format1[1].$format1[0];
|
|
$valeur[1] = $format2[2].$format2[1].$format2[0];
|
|
}
|
|
$this->setFilterRange('bilCloture', $valeur);
|
|
}
|
|
protected function zru($valeur) {
|
|
$this->setFilter('zru', $valeur);
|
|
}
|
|
protected function zus($valeur){
|
|
$this->setFilter('zus', $valeur);
|
|
}
|
|
protected function zfu($valeur){
|
|
$this->setFilter('zfu', $valeur);
|
|
}
|
|
protected function cucs($valeur){
|
|
$this->setFilter('cucs', $valeur);
|
|
}
|
|
protected function zrr($valeur){
|
|
$this->setFilter('zrr', $valeur);
|
|
}
|
|
protected function zafr($valeur){
|
|
$this->setFilter('zafr', $valeur);
|
|
}
|
|
|
|
} |