filterRNCS(); } 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 $c = Zend_Registry::get('config'); //Sphinx init $this->sphinx = new SphinxClient(); $this->sphinx->SetServer ( $c->profil->sphinx->host, intval($c->profil->sphinx->port) ); $this->sphinx->SetMatchMode(SPH_MATCH_EXTENDED); $this->sphinx->ResetFilters(); //Follow each elements if ( $structure!==null && count($structure)>0 ) { foreach($structure as $key => $valeur) { if ($key!= 'NB' && method_exists($this, $key)) { $this->{$key}($valeur); } else { Zend_Registry::get('firebug')->info('Method non exist KEY:'.$key); } } } } /** * Return array of status variables */ public function Status() { return $this->sphinx->Status(); } /** * Set marker RNCS to true */ public function filterRNCS() { $this->filterRNCS = 1; } /** * * @param string $name * @param mixed $valeur * @param boolean $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 string $name * @param int $min * @param int $max */ protected function setFilterRange($name, $min, $max) { Zend_Registry::get('firebug')->info($name.' - Min:'.$min.', Max:'.$max); $this->sphinx->SetFilterRange($name, intval($min), intval($max)); } /** * * @param string $name * @param string $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() { 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; } } /** * * @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 boolean $need * @param int $nbReturn */ public function execute($need = false, $nbReturn = 0) { if ($this->filterRNCS==1) { $this->sphinx->SetFilter('presentrcs', array(1)); } if($need) { $return = array(); $limit = 1000; $maxMatches = 50000; if ( $nbReturn > 0 ) { $limit = $nbReturn; $maxMatches = $nbReturn; $this->sphinx->SetSortMode( SPH_SORT_EXTENDED, '@random' ); } $this->sphinx->SetLimits(0, $limit, $maxMatches); $resSphinx = $this->sphinx->Query($this->alpha, $this->index); //Make an array with only the data needed $return = $this->getSiret($resSphinx); $total = $resSphinx['total_found']; if ( $nbReturn == 0 && $limit<$total ) { $max = ceil($total/$limit); //Get siret by $limit units for each request for($i=1; $i<$max; $i++){ $offset = $i*$limit; $this->sphinx->SetLimits($offset, $limit); $resSphinx = $this->sphinx->Query($this->alpha, $this->index); $tmpSiret = $this->getSiret($resSphinx); $return = array_merge($return, $tmpSiret); } } return $return; } else { $this->sphinx->SetLimits(0, 1); Zend_Registry::get('firebug')->info("Sphinx : ".$this->alpha); $resSphinx = $this->sphinx->Query($this->alpha, $this->index); Zend_Registry::get('firebug')->info($resSphinx); if ($resSphinx === false) return false; return $resSphinx['total_found']; } } /** * * @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_CiblageMinMax(); $minmax = $minmaxM->find($key)->current(); if ($minmax!==false) { $valMin = $minmax->min; $valMax = $minmax->max; } $min = ($min=='' || $min===null) ? $valMin : $min; $max = ($max=='' || $min===null) ? $valMax : $max; return array( 'min' => $min, 'max' => $max, ); } /** * * @param unknown_type $value */ protected function siege($value) { if ( array_key_exists('in', $value) ) { $this->setFilter('siege', $value['in']); } } protected function groupe($value) { if ( array_key_exists('in', $value) ) { $this->setFilter('sirenGrp', $value['in']); } } protected function tel($value) { if ( array_key_exists('in', $value) ) { $this->setFilter('tel', $value['in']); } } protected function fax($value) { if ( array_key_exists('in', $value) ) { $this->setFilter('fax', $value['in']); } } protected function web($value) { if ( array_key_exists('in', $value) ) { $this->setFilter('web', $value['in']); } } protected function mail($value) { if ( array_key_exists('in', $value) ) { $this->setFilter('mail', $value['in']); } } protected function presentRcs($value) { if ( array_key_exists('in', $value) ) { $this->setFilter('presentRcs', $value['in']); } } protected function adrDom($value) { if ( array_key_exists('in', $value) ) { $this->setFilter('adrDom', $value['in']); } } protected function dirNom($value) { if ( array_key_exists('in', $value) ) { $this->setFilter('dirNom', $value['in']); } } protected function participation($value) { if ( array_key_exists('in', $value) ) { $this->setFilter('part', $value['in']); } } protected function action($value) { if ( array_key_exists('in', $value) ) { $this->setFilter('actio', $value['in']); } } protected function dateCrea_etab($value) { if ( array_key_exists('in', $value) ) { $min = $value['in'][0]; $max = $value['in'][1]; if ($min===null || $max===null) return; $this->setFilterRange('dateCrea_etab', $min, $max); } } protected function dateCrea_ent($value) { $min = $value['in'][0]; $max = $value['in'][1]; if ($min===null || $max===null) return; $this->setFilterRange('dateCrea_ent', $min, $max); } protected function nbActio($value) { if ( array_key_exists('in', $value) ) { $result = $this->minmax('nbActio', $value['in'][0], $value['in'][1]); $min = $result['min']; $max = $result['max']; if ($min===null || $max===null) return; $this->setFilterRange('nbActio', $min, $max); } } protected function nbPart($value) { if ( array_key_exists('in', $value) ) { $result = $this->minmax('nbPart', $value['in'][0], $value['in'][1]); $min = $result['min']; $max = $result['max']; if ($min===null || $max===null) return; $this->setFilterRange('nbPart', $min, $max); } } protected function nbMPubli($value) { if ( array_key_exists('in', $value) ) { $result = $this->minmax('nbMPubli', $value['in'][0], $value['in'][1]); $min = $result['min']; $max = $result['max']; if ($min===null || $max===null) return; $this->setFilter('nbMPubli', $value); } } protected function capital($value) { if ( array_key_exists('in', $value) ) { $result = $this->minmax('capital', $value['in'][0], $value['in'][1]); $min = $result['min']; $max = $result['max']; if ($min===null || $max===null) return; $this->setFilterRange('capital', $min, $max); } } protected function age_entrep($value) { if ( array_key_exists('in', $value) ) { $result = $this->minmax('age_entrep', $value['in'][0], $value['in'][1]); $min = $result['min']; $max = $result['max']; if ($min===null || $max===null) return; $this->setFilterRange('age_entrep', $min, $max); } } protected function age_etab($value) { if ( array_key_exists('in', $value) ) { $result = $this->minmax('age_etab', $value['in'][0], $value['in'][1]); $min = $result['min']; $max = $result['max']; if ($min===null || $max===null) return; $this->setFilterRange('age_etab', $min, $max); } } protected function teff_entrep($value) { if ( array_key_exists('in', $value) ) { $this->setFilter('teff_entrep', $value['in']); } if ( array_key_exists('ex', $value) ) { $this->setFilter('teff_entrep', $value['ex'], true); } } protected function teff_etab($value) { if ( array_key_exists('in', $value) ) { $this->setFilter('teff_etab', $value['in']); } if ( array_key_exists('ex', $value) ) { $this->setFilter('teff_etab', $value['ex'], true); } } protected function eff_etab($value) { if ( array_key_exists('in', $value) ) { $result = $this->minmax('eff_etab', $value['in'][0], $value['in'][1]); $min = $result['min']; $max = $result['max']; if ($min===null || $max===null) return; $this->setFilterRange('eff_etab', $min, $max); } } protected function eff_entrep($value) { if ( array_key_exists('in', $value) ) { $result = $this->minmax('eff_entrep', $value['in'][0], $value['in'][1]); $min = $result['min']; $max = $result['max']; if ($min===null || $max===null) return; $this->setFilterRange('eff_entrep', $min, $max); } } protected function nbEtab($value) { if ( array_key_exists('in', $value) ) { $result = $this->minmax('nbEtab', $value['in'][0], $value['in'][1]); $min = $result['min']; $max = $result['max']; if ($min===null || $max===null) return; $this->setFilterRange('nbEtab', $min, $max); } } 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['in']); } } protected function zru($value) { if ( array_key_exists('in', $value) ) { $this->setFilter('zru', $value['in']); } } protected function zfu($value) { if ( array_key_exists('in', $value) ) { $this->setFilter('zfu', $value['in']); } } protected function cucs($value) { if ( array_key_exists('in', $value) ) { $this->setFilter('cucs', $value['in']); } } protected function zrr($value) { if ( array_key_exists('in', $value) ) { $this->setFilter('zrr', $value['in']); } } protected function zafr($value) { if ( array_key_exists('in', $value) ) { $this->setFilter('zafr', $value['in']); } } protected function actifEco($value) { if ( array_key_exists('in', $value) ) { $this->setFilter('actifEco', $value['in']); } } protected function procolHisto($value) { if ( array_key_exists('in', $value) ) { $this->setFilter('procolHisto', $value['in']); } } protected function dateImmat($value) { if ( array_key_exists('in', $value) ) { $min = $value['in'][0]; $max = $value['in'][1]; if ($min===null || $max===null) return; $this->setFilterRange('dateImmat', $min, $max); } } protected function tvaIntraValide($value) { if ( array_key_exists('in', $value) ) { $this->setFilter('tvaIntraValide', $value['in']); } } protected function bilType($value) { if ( array_key_exists('in', $value) ) { $this->setFilter('bilType', $value['in']); } if ( array_key_exists('ex', $value) ) { $this->setFilter('bilType', $value['ex'], true); } } protected function avisCs($value) { if ( array_key_exists('in', $value) ) { $this->setFilter('avisCs', $value['in']); } if ( array_key_exists('ex', $value) ) { $this->setFilter('avisCs', $value['ex'], true); } } protected function bilAnnee($value) { if ( array_key_exists('in', $value) ) { $this->setFilter('bilAnnee', $value['in']); } if ( array_key_exists('ex', $value) ) { $this->setFilter('bilAnnee', $value['ex'], true); } } protected function bilCloture($value) { if ( array_key_exists('in', $value) ) { $this->setFilter('bilCloture', $value['in']); } if ( array_key_exists('ex', $value) ) { $this->setFilter('bilCloture', $value['ex'], true); } } 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); } } protected function bilTca($value) { if ( array_key_exists('in', $value) ) { $this->setFilter('bilTca', $value['in']); } if ( array_key_exists('ex', $value) ) { $this->setFilter('bilTca', $value['ex'], true); } } protected function bilEE($value) { if ( array_key_exists('in', $value) ) { $result = $this->minmax('bilEE', $value['in'][0], $value['in'][1]); $min = $result['min']; $max = $result['max']; if ($min===null || $max===null) return; $this->setFilterRange('bilEE', $min, $max); } } protected function bilFL($value) { if ( array_key_exists('in', $value) ) { $result = $this->minmax('bilFL', $value['in'][0], $value['in'][1]); $min = $result['min']; $max = $result['max']; if ($min===null || $max===null) return; $this->setFilterRange('bilFL', $min, $max); } } protected function bilFK($value) { if ( array_key_exists('in', $value) ) { $result = $this->minmax('bilFK', $value['in'][0], $value['in'][1]); $min = $result['min']; $max = $result['max']; if ($min===null || $max===null) return; $this->setFilterRange('bilFK', $min, $max); } } protected function bilFR($value) { if ( array_key_exists('in', $value) ) { $result = $this->minmax('bilFR', $value['in'][0], $value['in'][1]); $min = $result['min']; $max = $result['max']; if ($min===null || $max===null) return; $this->setFilterRange('bilFR', $min, $max); } } protected function bilGF($value) { if ( array_key_exists('in', $value) ) { $result = $this->minmax('bilGF', $value['in'][0], $value['in'][1]); $min = $result['min']; $max = $result['max']; if ($min===null || $max===null) return; $this->setFilterRange('bilGF', $min, $max); } } protected function bilGP($value) { if ( array_key_exists('in', $value) ) { $result = $this->minmax('bilGP', $value['in'][0], $value['in'][1]); $min = $result['min']; $max = $result['max']; if ($min===null || $max===null) return; $this->setFilterRange('bilGP', $min, $max); } } protected function bilGW($value) { if ( array_key_exists('in', $value) ) { $result = $this->minmax('bilGW', $value['in'][0], $value['in'][1]); $min = $result['min']; $max = $result['max']; if ($min===null || $max===null) return; $this->setFilterRange('bilGW', $min, $max); } } protected function bilHD($value) { if ( array_key_exists('in', $value) ) { $result = $this->minmax('bilHD', $value['in'][0], $value['in'][1]); $min = $result['min']; $max = $result['max']; if ($min===null || $max===null) return; $this->setFilterRange('bilHD', $min, $max); } } protected function bilHH($value) { if ( array_key_exists('in', $value) ) { $result = $this->minmax('bilHH', $value['in'][0], $value['in'][1]); $min = $result['min']; $max = $result['max']; if ($min===null || $max===null) return; $this->setFilterRange('bilHH', $min, $max); } } protected function bilHL($value) { if ( array_key_exists('in', $value) ) { $result = $this->minmax('bilHL', $value['in'][0], $value['in'][1]); $min = $result['min']; $max = $result['max']; if ($min===null || $max===null) return; $this->setFilterRange('bilHL', $min, $max); } } protected function bilHM($value) { if ( array_key_exists('in', $value) ) { $result = $this->minmax('bilHM', $value['in'][0], $value['in'][1]); $min = $result['min']; $max = $result['max']; if ($min===null || $max===null) return; $this->setFilterRange('bilHM', $min, $max); } } protected function bilHN($value) { if ( array_key_exists('in', $value) ) { $result = $this->minmax('bilHN', $value['in'][0], $value['in'][1]); $min = $result['min']; $max = $result['max']; if ($min===null || $max===null) return; $this->setFilterRange('bilHN', $min, $max); } } protected function bilYP($value) { if ( array_key_exists('in', $value) ) { $result = $this->minmax('bilYP', $value['in'][0], $value['in'][1]); $min = $result['min']; $max = $result['max']; if ($min===null || $max===null) return; $this->setFilterRange('bilYP', $min, $max); } } /** * 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_CiblageNaf(); 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; } /** * 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).')'; } return $req; } /** * * @param array $value */ protected function ape_etab($value) { $req = $this->transformNAF($value); $this->setAlpha('ape_etab', $req); } /** * * @param array $value */ protected function ape_entrep($value) { $req = $this->transformNAF($value); $this->setAlpha('ape_entrep', $req); } /** * 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_CiblageDepartements(); $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_CiblageDepartements(); $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; } } } 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_CiblageCodePostaux(); $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_CiblageCodePostaux(); $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); } } } 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); } } protected function codesdepts($value) { if ( array_key_exists('in', $value) && is_array($value['in']) && count($value['in'])>0 ) { $this->setFilter('adr_dep', $value['in']); } if ( array_key_exists('ex', $value) && is_array($value['ex']) && count($value['ex'])>0 ) { $this->setFilter('adr_dep', $value['ex']); } } protected function codespostaux($value) { 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']); } } protected function getChildCJ($list) { $lastLevel = array(); if ( count($list)>0 ) { foreach ( $list as $item ) { $childItem = array(); $result = array(); $fjM = new Application_Model_CiblageFormeJuridique(); //Only code with a length between 1 and 3 if ( strlen($item)<4 ) { $sql = $fjM->select()->from($fjM, array('fjCode')) ->where('fjCode LIKE "'.$item.'%"') ->where('LENGTH(fjCode)=4'); Zend_Registry::get('firebug')->info($sql->__toString()); $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); } if ( count($valueEx)>0 ) { $this->setFilter('cj', $valueEx, true); } } }