odea/library/Scores/Ciblage.php

1162 lines
26 KiB
PHP
Raw Normal View History

2012-02-02 17:29:14 +00:00
<?php
/**
* Ciblage construct the request send to Sphinx
*/
class Ciblage
2012-02-02 17:29:14 +00:00
{
/**
* Name of the index store in Sphinx
* @var string
*/
protected $index = 'ciblage';
2012-05-02 19:06:36 +00:00
/**
* Sphinx instantiation
* @var object
*/
protected $sphinx;
2012-05-02 19:06:36 +00:00
/**
* Construct request
* @var string
*/
protected $alpha = '';
2012-05-02 19:06:36 +00:00
/**
* Marker to limit only the RNCS elements and disable all which are INSEE
* @var int
*/
protected $filterRNCS = 0;
/**
*
2012-05-25 09:19:42 +00:00
* @param null|array $structure
* @param boolean filterRNCS
*/
public function __construct($structure = null, $filterRNCS = 0)
2012-02-02 17:29:14 +00:00
{
if ($filterRNCS===true) {
$this->filterRNCS();
}
2012-05-11 20:25:55 +00:00
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';
//Load configuration from registry
2012-03-22 10:51:38 +00:00
$configuration = Zend_Registry::get('configuration');
2012-04-06 08:29:25 +00:00
//Sphinx init
2012-02-02 17:29:14 +00:00
$this->sphinx = new SphinxClient();
2012-05-11 20:25:55 +00:00
$this->sphinx->SetServer(
$configuration->sphinx->host,
intval($configuration->sphinx->port)
);
2012-09-07 12:53:39 +00:00
$this->sphinx->SetMatchMode(SPH_MATCH_EXTENDED);
2012-02-02 17:29:14 +00:00
$this->sphinx->ResetFilters();
//Follow each elements
2012-05-25 09:19:42 +00:00
if ( $structure!==null && count($structure)>0 ) {
2012-05-11 20:25:55 +00:00
foreach($structure as $key => $valeur) {
if ($key!= 'NB' && method_exists($this, $key)) {
$this->{$key}($valeur);
2012-05-23 19:40:49 +00:00
} else {
Zend_Registry::get('firebug')->info('Method non exist KEY:'.$key);
2012-02-02 17:29:14 +00:00
}
}
}
}
2012-05-25 09:19:42 +00:00
/**
* Return array of status variables
2012-05-25 09:19:42 +00:00
*/
public function Status()
{
return $this->sphinx->Status();
}
/**
* Set marker RNCS to true
*/
public function filterRNCS()
{
$this->filterRNCS = 1;
}
2012-05-14 15:32:50 +00:00
/**
*
2012-08-21 19:37:35 +00:00
* @param string $name
* @param mixed $valeur
* @param boolean $ex
2012-05-14 15:32:50 +00:00
*/
2012-05-11 20:25:55 +00:00
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);
}
2012-05-14 15:32:50 +00:00
/**
*
2012-08-21 19:37:35 +00:00
* @param string $name
* @param int $min
* @param int $max
2012-05-14 15:32:50 +00:00
*/
2012-05-11 20:25:55 +00:00
protected function setFilterRange($name, $min, $max)
{
2012-10-05 14:30:07 +00:00
Zend_Registry::get('firebug')->info($name.' - Min:'.$min.', Max:'.$max);
$this->sphinx->SetFilterRange($name, intval($min), intval($max));
2012-05-11 20:25:55 +00:00
}
2012-05-14 15:32:50 +00:00
/**
*
2012-08-21 19:37:35 +00:00
* @param string $name
* @param string $valeur
2012-05-14 15:32:50 +00:00
*/
2012-05-11 20:25:55 +00:00
protected function setAlpha($name, $valeur)
{
2012-05-14 13:16:14 +00:00
if ( $this->alpha!='' ) $this->alpha.= ' & ';
2012-05-11 20:25:55 +00:00
$this->alpha.= '@'.$name.' '.$valeur;
}
2012-05-14 15:32:50 +00:00
/**
*
*/
public function extract()
2012-02-21 13:39:02 +00:00
{
$resultats = $this->execute(true);
$siret = array();
foreach ($resutlats['matches'] as $element) {
2012-02-21 13:39:02 +00:00
$siret[] = $element['attrs']['siren'].$element['attrs']['nic'];
}
$total = array(
'total' => count($resultats),
'insee' => $this->calculRedevanceInsee(),
'result'=> $siret
2012-05-02 19:06:36 +00:00
);
return $total;
2012-02-21 13:39:02 +00:00
}
2012-05-14 15:32:50 +00:00
/**
*
*/
2012-02-02 17:29:14 +00:00
public function calculRedevanceInsee()
{
if ($this->filterRNCS!=1) {
$this->sphinx->SetFilter('presentrcs', array(0));
$resSphinx = $this->sphinx->Query($this->alpha, $this->index);
return $resSphinx['total_found'];
} else {
return 0;
}
2012-02-02 17:29:14 +00:00
}
2012-05-14 15:32:50 +00:00
/**
*
* @param unknown_type $structure
*/
2012-03-15 15:44:10 +00:00
protected function getSiret($structure)
{
$siret = array();
2012-04-12 13:54:45 +00:00
if(count($structure['matches'])>0) {
foreach($structure['matches'] as $element){
$siret[] = $element['attrs']['siren'].$element['attrs']['nic'];
}
2012-05-15 11:38:51 +00:00
return $siret;
2012-05-02 19:06:36 +00:00
} else
2012-05-15 11:38:51 +00:00
return array();
2012-03-15 15:44:10 +00:00
}
2012-05-14 15:32:50 +00:00
/**
*
2012-05-16 15:52:34 +00:00
* @param boolean $need
* @param int $nbReturn
2012-05-14 15:32:50 +00:00
*/
2012-05-16 15:52:34 +00:00
public function execute($need = false, $nbReturn = 0)
2012-02-02 17:29:14 +00:00
{
if ($this->filterRNCS==1) {
$this->sphinx->SetFilter('presentrcs', array(1));
}
2012-02-02 17:29:14 +00:00
if($need) {
2012-03-15 15:44:10 +00:00
$return = array();
2012-05-15 11:38:51 +00:00
$limit = 1000;
2012-05-16 15:52:34 +00:00
$maxMatches = 50000;
if ( $nbReturn > 0 ) {
2012-09-07 11:58:43 +00:00
$limit = $nbReturn;
$maxMatches = $nbReturn;
2012-09-07 12:53:39 +00:00
$this->sphinx->SetSortMode( SPH_SORT_EXTENDED, '@random' );
2012-05-16 15:52:34 +00:00
}
2012-09-07 11:58:43 +00:00
2012-05-16 15:52:34 +00:00
$this->sphinx->SetLimits(0, $limit, $maxMatches);
2012-05-15 11:38:51 +00:00
$resSphinx = $this->sphinx->Query($this->alpha, $this->index);
2012-09-07 11:58:43 +00:00
2012-05-16 15:52:34 +00:00
//Make an array with only the data needed
2012-05-15 11:38:51 +00:00
$return = $this->getSiret($resSphinx);
2012-09-07 11:58:43 +00:00
$total = $resSphinx['total_found'];
if ( $nbReturn == 0 && $limit<$total ) {
2012-05-15 11:38:51 +00:00
$max = ceil($total/$limit);
//Get siret by $limit units for each request
for($i=1; $i<$max; $i++){
$offset = $i*$limit;
2012-09-07 11:58:43 +00:00
$this->sphinx->SetLimits($offset, $limit);
2012-05-15 11:38:51 +00:00
$resSphinx = $this->sphinx->Query($this->alpha, $this->index);
$tmpSiret = $this->getSiret($resSphinx);
$return = array_merge($return, $tmpSiret);
}
}
return $return;
2012-05-10 09:24:05 +00:00
} else {
2012-05-15 11:38:51 +00:00
2012-05-10 09:24:05 +00:00
$this->sphinx->SetLimits(0, 1);
Zend_Registry::get('firebug')->info("Sphinx : ".$this->alpha);
$resSphinx = $this->sphinx->Query($this->alpha, $this->index);
2012-05-15 11:38:51 +00:00
Zend_Registry::get('firebug')->info($resSphinx);
if ($resSphinx === false)
return false;
return $resSphinx['total_found'];
2012-05-10 09:24:05 +00:00
}
2012-02-02 17:29:14 +00:00
}
/**
*
* @param unknown $key
* @param string $min
* @param string $max
* @return multitype:unknown
*/
protected function minmax($key, $min=null, $max=null)
{
//Read min max
$minmaxM = new Application_Model_MinMax();
$minmax = $minmaxM->find($key)->current();
if ($minmax!==false) {
$valMin = $minmax->min;
$valMax = $minmax->max;
}
2012-10-05 14:30:07 +00:00
$min = !is_int($min) ? $valMin : $min;
$max = !is_int($max) ? $valMax : $max;
return array(
'min' => $min,
'max' => $max,
);
}
2012-05-14 15:32:50 +00:00
/**
*
* @param unknown_type $value
*/
2012-05-11 20:25:55 +00:00
protected function siege($value)
{
2012-05-11 20:25:55 +00:00
if ( array_key_exists('in', $value) ) {
$this->setFilter('siege', $value['in']);
2012-02-02 17:29:14 +00:00
}
}
2012-05-11 20:25:55 +00:00
protected function groupe($value)
2012-04-10 13:20:08 +00:00
{
2012-05-11 20:25:55 +00:00
if ( array_key_exists('in', $value) ) {
$this->setFilter('sirenGrp', $value['in']);
2012-04-10 13:20:08 +00:00
}
}
2012-05-02 19:06:36 +00:00
2012-05-11 20:25:55 +00:00
protected function tel($value)
{
if ( array_key_exists('in', $value) ) {
$this->setFilter('tel', $value['in']);
2012-05-11 20:25:55 +00:00
}
2012-02-02 17:29:14 +00:00
}
2012-05-11 20:25:55 +00:00
protected function fax($value)
{
if ( array_key_exists('in', $value) ) {
$this->setFilter('fax', $value['in']);
2012-05-11 20:25:55 +00:00
}
2012-02-02 17:29:14 +00:00
}
2012-05-11 20:25:55 +00:00
protected function web($value)
{
if ( array_key_exists('in', $value) ) {
$this->setFilter('web', $value['in']);
2012-05-11 20:25:55 +00:00
}
2012-02-02 17:29:14 +00:00
}
2012-05-11 20:25:55 +00:00
protected function mail($value)
{
if ( array_key_exists('in', $value) ) {
$this->setFilter('mail', $value['in']);
2012-05-11 20:25:55 +00:00
}
2012-02-02 17:29:14 +00:00
}
2012-05-11 20:25:55 +00:00
protected function presentRcs($value)
2012-04-13 08:56:39 +00:00
{
2012-05-11 20:25:55 +00:00
if ( array_key_exists('in', $value) ) {
$this->setFilter('presentRcs', $value['in']);
2012-04-13 08:56:39 +00:00
}
}
2012-05-11 20:25:55 +00:00
protected function adrDom($value)
2012-04-13 08:56:39 +00:00
{
2012-05-11 20:25:55 +00:00
if ( array_key_exists('in', $value) ) {
$this->setFilter('adrDom', $value['in']);
2012-04-13 08:56:39 +00:00
}
}
2012-05-11 20:25:55 +00:00
protected function dirNom($value)
2012-04-13 08:56:39 +00:00
{
2012-05-11 20:25:55 +00:00
if ( array_key_exists('in', $value) ) {
$this->setFilter('dirNom', $value['in']);
2012-04-13 08:56:39 +00:00
}
}
2012-05-02 19:06:36 +00:00
2012-05-11 20:25:55 +00:00
protected function participation($value)
{
if ( array_key_exists('in', $value) ) {
$this->setFilter('part', $value['in']);
2012-05-11 20:25:55 +00:00
}
2012-04-13 15:33:59 +00:00
}
2012-02-20 14:07:14 +00:00
2012-05-11 20:25:55 +00:00
protected function action($value)
{
if ( array_key_exists('in', $value) ) {
$this->setFilter('actio', $value['in']);
2012-05-11 20:25:55 +00:00
}
2012-02-02 17:29:14 +00:00
}
2012-05-24 14:14:00 +00:00
2012-05-11 20:25:55 +00:00
protected function dateCrea_etab($value)
{
2012-05-24 13:43:15 +00:00
if ( array_key_exists('in', $value) ) {
$min = $value['in'][0];
$max = $value['in'][1];
if ($min===null || $max===null)
return;
2012-05-11 20:25:55 +00:00
2012-05-24 13:43:15 +00:00
$this->setFilterRange('dateCrea_etab', $min, $max);
}
2012-02-02 17:29:14 +00:00
}
2012-05-11 20:25:55 +00:00
protected function dateCrea_ent($value)
{
2012-05-24 13:43:15 +00:00
$min = $value['in'][0];
$max = $value['in'][1];
if ($min===null || $max===null)
return;
2012-05-11 20:25:55 +00:00
2012-05-24 13:43:15 +00:00
$this->setFilterRange('dateCrea_ent', $min, $max);
2012-02-02 17:29:14 +00:00
}
2012-05-11 20:25:55 +00:00
protected function nbActio($value)
{
if ( array_key_exists('in', $value) ) {
2012-05-23 19:40:49 +00:00
2012-10-05 14:30:07 +00:00
$result = $this->minmax('nbActio', $value['in'][0], $value['in'][1]);
$min = $result['min'];
2012-10-05 14:34:41 +00:00
$max = $result['max'];
2012-10-05 14:30:07 +00:00
2012-05-24 07:34:12 +00:00
if ($min===null || $max===null)
2012-05-23 19:40:49 +00:00
return;
2012-05-11 20:25:55 +00:00
$this->setFilterRange('nbActio', $min, $max);
}
2012-02-02 17:29:14 +00:00
}
2012-05-11 20:25:55 +00:00
protected function nbPart($value)
{
if ( array_key_exists('in', $value) ) {
2012-10-05 14:30:07 +00:00
$result = $this->minmax('nbPart', $value['in'][0], $value['in'][1]);
$min = $result['min'];
2012-10-05 14:34:41 +00:00
$max = $result['max'];
2012-10-05 14:30:07 +00:00
2012-05-24 07:34:12 +00:00
if ($min===null || $max===null)
return;
2012-05-11 20:25:55 +00:00
$this->setFilterRange('nbPart', $min, $max);
}
2012-02-02 17:29:14 +00:00
}
2012-05-02 19:06:36 +00:00
2012-05-11 20:25:55 +00:00
protected function nbMPubli($value)
{
2012-05-24 07:34:12 +00:00
if ( array_key_exists('in', $value) ) {
2012-10-05 14:30:07 +00:00
$result = $this->minmax('nbMPubli', $value['in'][0], $value['in'][1]);
$min = $result['min'];
2012-10-05 14:34:41 +00:00
$max = $result['max'];
2012-10-05 14:30:07 +00:00
2012-05-24 07:34:12 +00:00
if ($min===null || $max===null)
return;
2012-05-11 20:25:55 +00:00
$this->setFilter('nbMPubli', $value);
2012-02-02 17:29:14 +00:00
}
}
2012-05-02 19:06:36 +00:00
2012-05-11 20:25:55 +00:00
protected function capital($value)
{
if ( array_key_exists('in', $value) ) {
2012-10-05 14:30:07 +00:00
$result = $this->minmax('capital', $value['in'][0], $value['in'][1]);
$min = $result['min'];
2012-10-05 14:34:41 +00:00
$max = $result['max'];
2012-10-05 14:30:07 +00:00
2012-05-24 07:34:12 +00:00
if ($min===null || $max===null)
return;
2012-05-11 20:25:55 +00:00
$this->setFilterRange('capital', $min, $max);
2012-02-02 17:29:14 +00:00
}
}
2012-05-02 19:06:36 +00:00
2012-05-11 20:25:55 +00:00
protected function age_entrep($value)
{
2012-05-24 07:34:12 +00:00
if ( array_key_exists('in', $value) ) {
2012-10-05 14:30:07 +00:00
$result = $this->minmax('age_entrep', $value['in'][0], $value['in'][1]);
$min = $result['min'];
2012-10-05 14:34:41 +00:00
$max = $result['max'];
2012-10-05 14:30:07 +00:00
2012-05-24 07:34:12 +00:00
if ($min===null || $max===null)
return;
2012-07-31 11:37:52 +00:00
2012-05-11 20:25:55 +00:00
$this->setFilterRange('age_entrep', $min, $max);
2012-02-02 17:29:14 +00:00
}
}
2012-05-02 19:06:36 +00:00
2012-05-11 20:25:55 +00:00
protected function age_etab($value)
{
if ( array_key_exists('in', $value) ) {
2012-10-05 14:30:07 +00:00
$result = $this->minmax('age_etab', $value['in'][0], $value['in'][1]);
$min = $result['min'];
2012-10-05 14:34:41 +00:00
$max = $result['max'];
2012-05-24 07:34:12 +00:00
if ($min===null || $max===null)
return;
2012-05-11 20:25:55 +00:00
$this->setFilterRange('age_etab', $min, $max);
}
2012-02-02 17:29:14 +00:00
}
2012-05-02 19:06:36 +00:00
2012-05-11 20:25:55 +00:00
protected function teff_entrep($value)
{
if ( array_key_exists('in', $value) ) {
$this->setFilter('teff_entrep', $value['in']);
2012-04-06 09:49:41 +00:00
}
2012-05-11 20:25:55 +00:00
if ( array_key_exists('ex', $value) ) {
$this->setFilter('teff_entrep', $value['ex'], true);
2012-04-06 09:49:41 +00:00
}
2012-02-02 17:29:14 +00:00
}
2012-05-11 20:25:55 +00:00
protected function teff_etab($value)
{
if ( array_key_exists('in', $value) ) {
$this->setFilter('teff_etab', $value['in']);
2012-04-05 15:50:33 +00:00
}
2012-05-11 20:25:55 +00:00
if ( array_key_exists('ex', $value) ) {
$this->setFilter('teff_etab', $value['ex'], true);
2012-05-11 20:25:55 +00:00
}
2012-02-02 17:29:14 +00:00
}
2012-05-02 19:06:36 +00:00
2012-05-23 19:40:49 +00:00
protected function eff_etab($value)
{
if ( array_key_exists('in', $value) ) {
2012-10-05 14:30:07 +00:00
$result = $this->minmax('eff_etab', $value['in'][0], $value['in'][1]);
$min = $result['min'];
2012-10-05 14:34:41 +00:00
$max = $result['max'];
2012-10-05 14:30:07 +00:00
2012-05-24 07:34:12 +00:00
if ($min===null || $max===null)
return;
2012-05-23 19:40:49 +00:00
$this->setFilterRange('eff_etab', $min, $max);
}
}
protected function eff_entrep($value)
{
if ( array_key_exists('in', $value) ) {
2012-10-05 14:30:07 +00:00
$result = $this->minmax('eff_entrep', $value['in'][0], $value['in'][1]);
$min = $result['min'];
2012-10-05 14:34:41 +00:00
$max = $result['max'];
2012-10-05 14:30:07 +00:00
2012-05-23 19:40:49 +00:00
if ($min===null || $max===null)
return;
$this->setFilterRange('eff_entrep', $min, $max);
}
}
protected function nbEtab($value)
{
if ( array_key_exists('in', $value) ) {
2012-10-05 14:30:07 +00:00
$result = $this->minmax('nbEtab', $value['in'][0], $value['in'][1]);
$min = $result['min'];
2012-10-05 14:34:41 +00:00
$max = $result['max'];
2012-10-05 14:30:07 +00:00
2012-05-23 19:40:49 +00:00
if ($min===null || $max===null)
return;
$this->setFilterRange('nbEtab', $min, $max);
}
}
2012-05-11 20:25:55 +00:00
protected function geo_domtom($value)
{
if( $value['in']==1 ) {
$this->setFilter('adr_dep', array(971, 972, 973, 974, 976), true);
2012-02-02 17:29:14 +00:00
}
2012-04-05 15:50:33 +00:00
}
2012-05-13 13:20:28 +00:00
2012-05-11 20:25:55 +00:00
protected function geo_etranger($value)
{
if( $value['in']==1 ) {
$this->setFilter('adr_dep', array(986, 987, 988), true);
}
2012-04-05 15:50:33 +00:00
}
2012-05-13 13:20:28 +00:00
2012-05-11 20:25:55 +00:00
protected function geo_corse($value)
{
if( $value['in']==1 ) {
$this->setFilter('adr_dep', array(201, 202), true);
}
2012-04-05 15:50:33 +00:00
}
2012-05-13 13:20:28 +00:00
protected function zus($value)
{
if ( array_key_exists('in', $value) ) {
$this->setFilter('zus', $value['in']);
2012-05-13 13:20:28 +00:00
}
}
protected function zru($value)
{
if ( array_key_exists('in', $value) ) {
$this->setFilter('zru', $value['in']);
2012-05-13 13:20:28 +00:00
}
}
protected function zfu($value)
{
if ( array_key_exists('in', $value) ) {
$this->setFilter('zfu', $value['in']);
2012-05-13 13:20:28 +00:00
}
}
protected function cucs($value)
{
if ( array_key_exists('in', $value) ) {
$this->setFilter('cucs', $value['in']);
2012-05-13 13:20:28 +00:00
}
}
protected function zrr($value)
{
if ( array_key_exists('in', $value) ) {
$this->setFilter('zrr', $value['in']);
2012-05-13 13:20:28 +00:00
}
}
protected function zafr($value)
{
if ( array_key_exists('in', $value) ) {
$this->setFilter('zafr', $value['in']);
2012-05-13 13:20:28 +00:00
}
}
protected function actifEco($value)
{
if ( array_key_exists('in', $value) ) {
$this->setFilter('actifEco', $value['in']);
2012-05-13 13:20:28 +00:00
}
}
protected function procolHisto($value)
{
if ( array_key_exists('in', $value) ) {
$this->setFilter('procolHisto', $value['in']);
2012-05-13 13:20:28 +00:00
}
}
2012-05-24 14:14:00 +00:00
protected function dateImmat($value)
2012-05-13 13:20:28 +00:00
{
if ( array_key_exists('in', $value) ) {
2012-05-24 14:14:00 +00:00
$min = $value['in'][0];
$max = $value['in'][1];
if ($min===null || $max===null)
return;
$this->setFilterRange('dateImmat', $min, $max);
2012-05-13 13:20:28 +00:00
}
}
2012-05-24 14:14:00 +00:00
protected function tvaIntraValide($value)
{
if ( array_key_exists('in', $value) ) {
$this->setFilter('tvaIntraValide', $value['in']);
2012-05-24 14:14:00 +00:00
}
2012-05-23 19:40:49 +00:00
}
2012-05-13 13:20:28 +00:00
protected function bilType($value)
{
if ( array_key_exists('in', $value) ) {
$this->setFilter('bilType', $value['in']);
2012-05-13 13:20:28 +00:00
}
if ( array_key_exists('ex', $value) ) {
$this->setFilter('bilType', $value['ex'], true);
2012-05-13 13:20:28 +00:00
}
}
protected function avisCs($value)
{
if ( array_key_exists('in', $value) ) {
$this->setFilter('avisCs', $value['in']);
2012-05-13 13:20:28 +00:00
}
if ( array_key_exists('ex', $value) ) {
$this->setFilter('avisCs', $value['ex'], true);
2012-05-13 13:20:28 +00:00
}
}
2012-10-01 08:51:56 +00:00
protected function bilDuree($value)
{
if ( array_key_exists('in', $value) ) {
$this->setFilter('bilDuree', $value['in']);
}
if ( array_key_exists('ex', $value) ) {
$this->setFilter('bilDuree', $value['ex'], true);
}
}
2012-05-13 13:20:28 +00:00
protected function bilTca($value)
{
if ( array_key_exists('in', $value) ) {
$this->setFilter('bilTca', $value['in']);
2012-05-13 13:20:28 +00:00
}
if ( array_key_exists('ex', $value) ) {
$this->setFilter('bilTca', $value['ex'], true);
2012-05-13 13:20:28 +00:00
}
}
protected function bilEE($value)
{
if ( array_key_exists('in', $value) ) {
2012-10-05 14:30:07 +00:00
$result = $this->minmax('bilEE', $value['in'][0], $value['in'][1]);
$min = $result['min'];
2012-10-05 14:34:41 +00:00
$max = $result['max'];
if ($min===null || $max===null)
return;
2012-05-13 13:20:28 +00:00
$this->setFilterRange('bilEE', $min, $max);
}
}
protected function bilFL($value)
{
if ( array_key_exists('in', $value) ) {
2012-10-05 14:30:07 +00:00
$result = $this->minmax('bilFL', $value['in'][0], $value['in'][1]);
$min = $result['min'];
2012-10-05 14:34:41 +00:00
$max = $result['max'];
2012-10-05 14:30:07 +00:00
if ($min===null || $max===null)
return;
2012-05-13 13:20:28 +00:00
$this->setFilterRange('bilFL', $min, $max);
}
}
protected function bilFK($value)
{
if ( array_key_exists('in', $value) ) {
2012-10-05 14:30:07 +00:00
$result = $this->minmax('bilFK', $value['in'][0], $value['in'][1]);
$min = $result['min'];
2012-10-05 14:34:41 +00:00
$max = $result['max'];
2012-10-05 14:30:07 +00:00
if ($min===null || $max===null)
return;
2012-05-13 13:20:28 +00:00
$this->setFilterRange('bilFK', $min, $max);
}
}
protected function bilFR($value)
{
if ( array_key_exists('in', $value) ) {
2012-10-05 14:30:07 +00:00
$result = $this->minmax('bilFR', $value['in'][0], $value['in'][1]);
$min = $result['min'];
2012-10-05 14:34:41 +00:00
$max = $result['max'];
2012-10-05 14:30:07 +00:00
if ($min===null || $max===null)
return;
2012-05-13 13:20:28 +00:00
$this->setFilterRange('bilFR', $min, $max);
}
}
protected function bilGF($value)
{
if ( array_key_exists('in', $value) ) {
2012-10-05 14:30:07 +00:00
$result = $this->minmax('bilGF', $value['in'][0], $value['in'][1]);
$min = $result['min'];
2012-10-05 14:34:41 +00:00
$max = $result['max'];
if ($min===null || $max===null)
return;
2012-05-13 13:20:28 +00:00
$this->setFilterRange('bilGF', $min, $max);
}
}
protected function bilGP($value)
{
if ( array_key_exists('in', $value) ) {
2012-10-05 14:30:07 +00:00
$result = $this->minmax('bilGP', $value['in'][0], $value['in'][1]);
$min = $result['min'];
2012-10-05 14:34:41 +00:00
$max = $result['max'];
if ($min===null || $max===null)
return;
2012-05-13 13:20:28 +00:00
$this->setFilterRange('bilGP', $min, $max);
}
}
protected function bilGW($value)
{
if ( array_key_exists('in', $value) ) {
2012-10-05 14:30:07 +00:00
$result = $this->minmax('bilGW', $value['in'][0], $value['in'][1]);
$min = $result['min'];
2012-10-05 14:34:41 +00:00
$max = $result['max'];
if ($min===null || $max===null)
return;
2012-05-13 13:20:28 +00:00
$this->setFilterRange('bilGW', $min, $max);
}
}
protected function bilHD($value)
{
if ( array_key_exists('in', $value) ) {
2012-10-05 14:30:07 +00:00
$result = $this->minmax('bilHD', $value['in'][0], $value['in'][1]);
$min = $result['min'];
2012-10-05 14:34:41 +00:00
$max = $result['max'];
if ($min===null || $max===null)
return;
2012-05-13 13:20:28 +00:00
$this->setFilterRange('bilHD', $min, $max);
}
}
protected function bilHH($value)
{
if ( array_key_exists('in', $value) ) {
2012-10-05 14:30:07 +00:00
$result = $this->minmax('bilHH', $value['in'][0], $value['in'][1]);
$min = $result['min'];
2012-10-05 14:34:41 +00:00
$max = $result['max'];
if ($min===null || $max===null)
return;
2012-05-13 13:20:28 +00:00
$this->setFilterRange('bilHH', $min, $max);
}
}
protected function bilHL($value)
{
if ( array_key_exists('in', $value) ) {
2012-10-05 14:30:07 +00:00
$result = $this->minmax('bilHL', $value['in'][0], $value['in'][1]);
$min = $result['min'];
2012-10-05 14:34:41 +00:00
$max = $result['max'];
if ($min===null || $max===null)
return;
2012-05-13 13:20:28 +00:00
$this->setFilterRange('bilHL', $min, $max);
}
}
protected function bilHM($value)
{
if ( array_key_exists('in', $value) ) {
2012-10-05 14:30:07 +00:00
$result = $this->minmax('bilHM', $value['in'][0], $value['in'][1]);
$min = $result['min'];
2012-10-05 14:34:41 +00:00
$max = $result['max'];
if ($min===null || $max===null)
return;
2012-05-13 13:20:28 +00:00
$this->setFilterRange('bilHM', $min, $max);
}
}
protected function bilHN($value)
{
if ( array_key_exists('in', $value) ) {
2012-10-05 14:30:07 +00:00
$result = $this->minmax('bilHN', $value['in'][0], $value['in'][1]);
$min = $result['min'];
2012-10-05 14:34:41 +00:00
$max = $result['max'];
if ($min===null || $max===null)
return;
2012-05-13 13:20:28 +00:00
$this->setFilterRange('bilHN', $min, $max);
}
}
protected function bilYP($value)
{
if ( array_key_exists('in', $value) ) {
2012-10-05 14:30:07 +00:00
$result = $this->minmax('bilYP', $value['in'][0], $value['in'][1]);
$min = $result['min'];
2012-10-05 14:34:41 +00:00
$max = $result['max'];
if ($min===null || $max===null)
return;
2012-05-13 13:20:28 +00:00
$this->setFilterRange('bilYP', $min, $max);
}
}
2012-05-15 15:20:09 +00:00
/**
* Get the last child of NAF in the tree
* @param unknown_type $list
*/
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));
2012-05-23 19:40:49 +00:00
//Zend_Registry::get('firebug')->info($sql->__toString());
2012-05-15 15:20:09 +00:00
$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.'%"');
}
2012-05-23 19:40:49 +00:00
//Zend_Registry::get('firebug')->info($sql->__toString());
2012-05-15 15:20:09 +00:00
$result = $nafM->fetchAll($sql)->toArray();
if ( count($result)>0 ) {
foreach ( $result as $i ) {
$lastLevel[] = $i['code'];
}
}
}
}
return $lastLevel;
}
/**
* Make the string request of NAFs
* @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)) {
$req = 'EX ';
}
$req.= ' -('.implode('|', $valueEx).')';
2012-05-15 15:20:09 +00:00
}
return $req;
}
2012-05-14 15:32:50 +00:00
/**
*
2012-05-15 15:20:09 +00:00
* @param array $value
2012-05-14 15:32:50 +00:00
*/
2012-05-14 13:16:14 +00:00
protected function ape_etab($value)
{
2012-05-14 15:32:50 +00:00
$req = $this->transformNAF($value);
2012-05-14 13:16:14 +00:00
$this->setAlpha('ape_etab', $req);
}
2012-05-15 15:20:09 +00:00
/**
*
* @param array $value
*/
2012-05-14 13:16:14 +00:00
protected function ape_entrep($value)
{
2012-05-14 15:32:50 +00:00
$req = $this->transformNAF($value);
2012-05-14 13:16:14 +00:00
$this->setAlpha('ape_entrep', $req);
}
2012-05-15 15:20:09 +00:00
/**
* Add geographic filter
* R[code] => Region (Region need to be transform as Departements)
* D[code] => Departement
* C[code] => Code Commune Insee
* @param array $value
*/
protected function geo($value)
{
$departementValueIn = $departementValueEx = array();
$communeValueIn = $communeValueEx = array();
if ( array_key_exists('in', $value) && is_array($value['in']) && count($value['in'])>0 ) {
foreach ( $value['in'] as $item ) {
switch( substr($item,0,1) ) {
case 'C':
$communeValueIn[] = intval(substr($item,1));
break;
case 'D':
$departementValueIn[] = intval(substr($item,1));
break;
case 'R':
$departementsM = new Application_Model_Departements();
$sql = $departementsM->select()
->from($departementsM, array('numdep'))
->where('codeRegionInsee = ?', substr($item,1));
$departements = $departementsM->fetchAll($sql)->toArray();
foreach ($departements as $d) {
$departementValueIn[] = intval($d['numdep']);
}
break;
}
}
}
if ( array_key_exists('ex', $value) && is_array($value['ex']) && count($value['ex'])>0 ) {
foreach ( $value['ex'] as $item ) {
switch( substr($item,0,1) ) {
case 'C':
$communeValueEx[] = intval(substr($item,1));
break;
case 'D':
$departementValueEx[] = intval(substr($item,1));
break;
case 'R':
$departementsM = new Application_Model_Departements();
$sql = $departementsM->select()
->from($departementsM, array('numdep'))
->where('codeRegionInsee = ?', substr($item,1));
$departements = $departementsM->fetchAll($sql)->toArray();
foreach ($departements as $d) {
$departementValueEx[] = intval($d['numdep']);
}
break;
}
}
}
2012-05-15 15:20:09 +00:00
if ( count($departementValueIn)>0 ) {
$this->setFilter('adr_dep', $departementValueIn);
}
if ( count($departementValueEx)>0 ) {
$this->setFilter('adr_dep', $departementValueEx, true);
}
if ( count($communeValueIn)>0 ) {
//Check if a departement include a codeCommune else remove this codeCommune
if (count($departementValueIn)>0) {
foreach ( $departementValueIn as $item ) {
$codePostauxM = new Application_Model_CodePostaux();
$sql = $codePostauxM->select()
->from($codePostauxM, array('INSEE'))
->where('Codepos LIKE ?', $item.'%');
$result = $codePostauxM->fetchAll($sql)->toArray();
$arrayDiff = array();
foreach ( $result as $insee ) {
$arrayDiff[] = intval($insee['INSEE']);
}
$communeValueIn = array_diff($communeValueIn, $arrayDiff);
}
}
if ( count($communeValueIn)>0 ) {
$this->setFilter('codeCommune', $communeValueIn);
}
}
if ( count($communeValueEx)>0 ) {
//Check if a departement include a codeCommune else remove this codeCommune
if (count($departementValueEx)>0) {
foreach ( $departementValueEx as $item ) {
$codePostauxM = new Application_Model_CodePostaux();
$sql = $codePostauxM->select()
->from($codePostauxM, array('INSEE'))
->where('Codepos LIKE ?', $item.'%');
$result = $codePostauxM->fetchAll($sql)->toArray();
$arrayDiff = array();
foreach ( $result as $insee ) {
$arrayDiff[] = intval($insee['INSEE']);
}
$communeValueEx = array_diff($communeValueEx, $arrayDiff);
}
}
if ( count($communeValueEx)>0 ) {
$this->setFilter('codeCommune', $communeValueEx, true);
}
}
2012-05-15 15:20:09 +00:00
}
2012-07-31 11:37:52 +00:00
protected function codesinsee($value)
{
if ( array_key_exists('in', $value) && is_array($value['in']) && count($value['in'])>0 ) {
$this->setFilter('codeCommune', $value['in']);
}
if ( array_key_exists('ex', $value) && is_array($value['ex']) && count($value['ex'])>0 ) {
$this->setFilter('codeCommune', $value['ex'], true);
}
}
2012-07-31 15:23:46 +00:00
protected function codespostaux($value)
2012-07-31 11:37:52 +00:00
{
if ( array_key_exists('in', $value) && is_array($value['in']) && count($value['in'])>0 ) {
$this->setFilter('adr_cp', $value['in']);
}
if ( array_key_exists('ex', $value) && is_array($value['ex']) && count($value['ex'])>0 ) {
$this->setFilter('adr_cp', $value['ex']);
}
}
2012-05-15 15:20:09 +00:00
protected function getChildCJ($list)
{
$lastLevel = array();
if ( count($list)>0 ) {
foreach ( $list as $item ) {
$childItem = array();
$result = array();
$fjM = new Application_Model_FormeJuridique();
//Only code with a length between 1 and 3
if ( strlen($item)<4 ) {
$sql = $fjM->select()->from($fjM, array('fjCode'))
2012-05-15 15:33:26 +00:00
->where('fjCode LIKE "'.$item.'%"')
2012-05-15 15:20:09 +00:00
->where('LENGTH(fjCode)=4');
2012-05-15 15:33:26 +00:00
Zend_Registry::get('firebug')->info($sql->__toString());
2012-05-15 15:20:09 +00:00
$result = $fjM->fetchAll($sql)->toArray();
if ( count($result)>0 ) {
foreach ( $result as $i ) {
$lastLevel[] = $i['fjCode'];
}
}
} else {
$lastLevel[] = $item;
}
}
}
return $lastLevel;
}
/**
* Add filter for forme juridique
* @param array $value
*/
protected function cj($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->getChildCJ($value['in']);
}
if ( array_key_exists('ex', $value) && is_array($value['ex']) && count($value['ex'])>0 ) {
$valueEx = $this->getChildCJ($value['ex']);
}
if ( count($valueIn)>0 ) {
Zend_Registry::get('firebug')->info($valueIn);
$this->setFilter('cj', $valueIn);
}
2012-05-14 15:32:50 +00:00
2012-05-15 15:20:09 +00:00
if ( count($valueEx)>0 ) {
$this->setFilter('cj', $valueEx, true);
}
}
2012-05-14 15:32:50 +00:00
2012-02-02 17:29:14 +00:00
}