Beispiel #1
0
 public function executeSearch(sfWebRequest $request)
 {
     $culture = $this->getUser()->getCulture();
     $this->page = $this->getRequestParameter('page', 1);
     $tag = $request->getParameter("tag", false);
     $tag = str_replace("_2E_", ".", $tag);
     if ($tag) {
         $this->q = "#{$tag}";
     } else {
         $this->q = $request->getParameter("q");
     }
     $request->setAttribute("q", $this->q);
     $resultsArray = array();
     $cl = $this->resetSphinxClient();
     $needle = $this->q;
     $this->ext = "";
     if (strlen($needle) > 0 && strpos($needle, '#') === 0) {
         $this->ext = "_tag";
         $needle = substr($needle, 1);
     } else {
         $needle = SfVoUtil::stripAccents($this->q);
     }
     $this->politicoCounts = false;
     $this->partidoCounts = false;
     $this->propuestaCounts = false;
     if ($needle) {
         if ($this->ext == "_tag") {
             $cl->SetMatchMode(SPH_MATCH_PHRASE);
             $cl->SetSortMode(SPH_SORT_EXTENDED, "cnt DESC");
             $this->politicoCounts = array();
             $this->partidoCounts = array();
             $this->propuestaCounts = array();
             $indexes = "partido_tag_{$culture}, politico_tag_{$culture}, propuesta_tag_{$culture}";
             $this->res = $cl->Query($needle, $indexes);
             if ($this->res !== false) {
                 $this->total = $this->res['total'];
                 $this->totalFound = $this->res['total_found'];
                 $cl->SetLimits(($this->page - 1) * $this->limit, $this->limit);
                 $this->res = $cl->Query($needle, $indexes);
             }
         } else {
             $cl->SetFieldWeights(array('abreviatura_partido' => 5, 'nombre' => 5, 'apellidos' => 5, 'alias' => 5, 'titulo' => 5, 'nombre_insti' => 8, 'nombre_ele' => 9));
             $cl->SetSortMode(SPH_SORT_EXPR, "@weight + ( 1 * votes/max_votes )");
             $indexes = "politico_{$culture}, partido_{$culture},propuesta_{$culture}, institucion_{$culture}, usuario, eleccion_{$culture}";
             $this->res = $cl->Query($needle, $indexes);
             if ($this->res !== false) {
                 $this->total = $this->res['total'];
                 $this->totalFound = $this->res['total_found'];
                 $cl->SetLimits(($this->page - 1) * $this->limit, $this->limit);
                 $this->res = $cl->Query($needle, $indexes);
             }
         }
         if ($this->res !== false && isset($this->res["matches"]) && is_array($this->res["matches"])) {
             foreach ($this->res["matches"] as $match) {
                 //echo "<pre>";print_r( $match );echo "</pre>";
                 switch ($match['attrs']['type']) {
                     case 1:
                         if ($this->ext == "_tag") {
                             $this->politicoCounts[$match['attrs']['politico_id']] = $match['attrs']['cnt'];
                             $resultsArray[] = PoliticoPeer::retrieveByPK($match['attrs']['politico_id']);
                         } else {
                             $resultsArray[] = PoliticoPeer::retrieveByPK($match['id']);
                         }
                         break;
                     case 2:
                         if ($this->ext == "_tag") {
                             $this->partidoCounts[$match['attrs']['partido_id']] = $match['attrs']['cnt'];
                             $resultsArray[] = PartidoPeer::retrieveByPK($match['attrs']['partido_id']);
                         } else {
                             $resultsArray[] = PartidoPeer::retrieveByPK($match['id']);
                         }
                         break;
                     case 3:
                         if ($this->ext == "_tag") {
                             $this->propuestaCounts[$match['attrs']['propuesta_id']] = $match['attrs']['cnt'];
                             $resultsArray[] = PropuestaPeer::retrieveByPK($match['attrs']['propuesta_id']);
                         } else {
                             $resultsArray[] = PropuestaPeer::retrieveByPK($match['id']);
                         }
                         break;
                     case 101:
                         $resultsArray[] = InstitucionPeer::retrieveByPK($match['id']);
                         break;
                     case 102:
                         $resultsArray[] = SfGuardUserPeer::retrieveByPK($match['id']);
                         break;
                     case 103:
                         $resultsArray[] = ConvocatoriaPeer::retrieveByPK($match['id']);
                         break;
                 }
             }
         }
     }
     $this->results = $resultsArray;
     $this->title = sfContext::getInstance()->getI18N()->__('"%1%" en Voota', array('%1%' => $this->q));
     $descStr = "";
     foreach ($resultsArray as $idx => $result) {
         if ($idx < 3) {
             $descStr .= ($descStr ? ', ' : '') . $result;
         }
     }
     $this->response->addMeta('Description', sfContext::getInstance()->getI18N()->__('Resultados de la búsqueda "%1%" en Voota: %2%, ...', array('%1%' => $this->q, '%2%' => $descStr)));
     $this->response->setTitle($this->title);
 }
Beispiel #2
0
 private function search($data)
 {
     $q = $this->getRequestParameter("q", '');
     if (!$q) {
         throw new BadRequestException('A search string must be provided.');
     }
     $page = $this->getRequestParameter("page", '1');
     $limit = $this->getRequestParameter("limit", self::PAGE_SIZE);
     $culture = $this->getRequestParameter("culture", 'es');
     $type = $this->getRequestParameter("type", false);
     $cl = new SphinxClient();
     $dbConf = Propel::getConfiguration();
     $dsn = $dbConf['datasources']['propel']['connection']['dsn'];
     $sphinxServer = sfConfig::get('sf_sphinx_server');
     $cl->SetServer($sphinxServer, 3312);
     $this->limit = 1000;
     $cl->SetLimits(0, $this->limit, $this->limit);
     $cl->SetArrayResult(true);
     $entities = array();
     $cl->SetArrayResult(true);
     if ($type && $type != 'party' && $type != 'politician' && $type != 'proposal') {
         throw new BadRequestException('Invalid type.');
     }
     if (!$type || $type == 'party') {
         $this->res = $cl->Query(SfVoUtil::stripAccents($q), "partido_{$culture}");
         if ($this->res !== false) {
             if (isset($this->res["matches"]) && is_array($this->res["matches"])) {
                 $c = new Criteria();
                 $list = array();
                 foreach ($this->res["matches"] as $idx => $match) {
                     $list[] = $match['id'];
                 }
                 $c->add(PartidoPeer::ID, $list, Criteria::IN);
                 //$c->addDescendingOrderByColumn(PartidoPeer::SUMU);
                 $pager = new sfPropelPager('Partido', $limit);
                 $pager->setCriteria($c);
                 $pager->setPage($this->getRequestParameter('page', $page));
                 $pager->init();
                 foreach ($pager->getResults() as $partido) {
                     $entities[] = new Entity($partido);
                 }
             }
         }
     }
     if (!$type || $type == 'politician') {
         $this->res = $cl->Query(SfVoUtil::stripAccents($q), "politico_{$culture}");
         if ($this->res !== false) {
             if (isset($this->res["matches"]) && is_array($this->res["matches"])) {
                 $c = new Criteria();
                 $list = array();
                 foreach ($this->res["matches"] as $idx => $match) {
                     $list[] = $match['id'];
                 }
                 $c = new Criteria();
                 $c->add(PoliticoPeer::ID, $list, Criteria::IN);
                 //$c->addDescendingOrderByColumn(PoliticoPeer::SUMU);
                 $pager = new sfPropelPager('Politico', $limit);
                 $pager->setCriteria($c);
                 $pager->setPage($this->getRequestParameter('page', $page));
                 $pager->init();
                 foreach ($pager->getResults() as $politico) {
                     $entities[] = new Entity($politico);
                 }
             }
         }
     }
     if (!$type || $type == 'proposal') {
         $this->res = $cl->Query(SfVoUtil::stripAccents($q), "propuesta_{$culture}");
         if ($this->res !== false) {
             if (isset($this->res["matches"]) && is_array($this->res["matches"])) {
                 $c = new Criteria();
                 $list = array();
                 foreach ($this->res["matches"] as $idx => $match) {
                     $list[] = $match['id'];
                 }
                 $c = new Criteria();
                 $c->add(PropuestaPeer::ID, $list, Criteria::IN);
                 //$c->addDescendingOrderByColumn(PropuestaPeer::SUMU);
                 $pager = new sfPropelPager('Propuesta', $limit);
                 $pager->setCriteria($c);
                 $pager->setPage($this->getRequestParameter('page', $page));
                 $pager->init();
                 foreach ($pager->getResults() as $propuesta) {
                     $entities[] = new Entity($propuesta);
                 }
             }
         }
     }
     return $entities;
 }