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 $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 */ public function execute($need = false) { if($need) { $return = array(); $limit = 1000; $this->sphinx->SetLimits(0, $limit, 50000); $resSphinx = $this->sphinx->Query($this->alpha, $this->index); $total = $resSphinx['total_found']; // $return = $this->getSiret($resSphinx); if ( $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, 50000); $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); $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']; } } /** * * @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); } } /** * 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)); 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)) { //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 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[] = substr($item,1); break; case 'D': $departementValueIn[] = substr($item,1); break; case 'R': $departementsM = new Application_Model_Departements(); $sql = $departementsM->select() ->form($departementsM, array('numdep')) ->where('codeRegionInsee = ?', substr($item,1)); $departements = $departementsM->fetchAll($sql)->toArray(); foreach ($departements as $d) { $departementValueIn[] = $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[] = substr($item,1); break; case 'D': $departementValueEx[] = substr($item,1); break; case 'R': $departementsM = new Application_Model_Departements(); $sql = $departementsM->select() ->form($departementsM, array('numdep')) ->where('codeRegionInsee = ?', substr($item,1)); $departements = $departementsM->fetchAll($sql)->toArray(); foreach ($departements as $d) { $departementValueEx[] = $d['numdep']; } break; } } } if ( count($departementValueIn)>0 ) { $this->setFilter('adr_dep', $departementValueIn); } if ( count($departementValueEx)>0 ) { $this->setFilter('adr_dep', $departementValueEx, true); } if ( count($departementValueIn)>0 ) { $this->setFilter('codeCommne', $communeValueIn); } if ( count($departementValueEx)>0 ) { $this->setFilter('codeCommune', $communeValueEx, true); } } 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')) ->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); } } }