protected function execute($arguments = array(), $options = array()) { // initialize the database connection $databaseManager = new sfDatabaseManager($this->configuration); $connection = $databaseManager->getDatabase($options['connection'] ? $options['connection'] : null)->getConnection(); $handle = fopen("php://stdin", "r"); $line = fgets($handle); while ($line = fgets($handle)) { $data = explode(";", "{$line}"); $c = new Criteria(); $c->add(PartidoPeer::ABREVIATURA, $data[5]); $partido = PartidoPeer::doSelectOne($c); if (!$partido) { echo "Not found: (" . $data[5] . ")\n"; continue; } $circu = CircunscripcionPeer::retrieveByPK($data[4]); $convocatoria = ConvocatoriaPeer::retrieveByPK($data[3]); $c = new Criteria(); $c->add(ListaPeer::CONVOCATORIA_ID, $data[3]); $c->add(ListaPeer::CIRCUNSCRIPCION_ID, $data[4]); $c->add(ListaPeer::PARTIDO_ID, $partido->getId()); $lista = ListaPeer::doSelectOne($c); if (!$lista) { $lista = new Lista(); $lista->setPartido($partido); $lista->setCircunscripcion($circu); $lista->setConvocatoria($convocatoria); $lista->save(); echo "Created lista ({$partido}, {$circu})\n"; } $politicos = false; $c = new Criteria(); $c->add("concat(nombre, ' ', apellidos)", trim($data[2])); //$c->add('fullname', utf8_encode( trim($data[2]) ), Criteria::EQUAL); $politicos = PoliticoPeer::doSelect($c); if (count($politicos) != 0) { echo "(ASIGNADO) " . $data[2] . "\n"; $politico = $politicos[0]; } else { echo "(NUEVO) " . $data[2] . "\n"; $politico = new Politico(); $nombreApellidos = explode(" ", $data[2]); $nombre = array_shift($nombreApellidos); $apellidos = implode(" ", $nombreApellidos); $politico->setNombre($nombre); $politico->setApellidos($apellidos); if ($data[7] != '1') { if (count($data) > 10 && trim($data[10]) != '') { $c = new Criteria(); $c->add(PartidoPeer::ABREVIATURA, $data[5]); $partido = PartidoPeer::doSelectOne($c); if ($partido) { $politico->setPartido($partido); } } else { $politico->setPartido($lista->getPartido()); } } if ($data[1] && trim($data[1]) != '') { $politico->setSexo($data[1] == "hombre" ? 'H' : 'M'); } $politico->save(); $politicoI18n = new PoliticoI18n(); $politicoI18n->setPolitico($politico); $politicoI18n->setCulture('es'); $politicoI18n->save(); $politicoI18n = new PoliticoI18n(); $politicoI18n->setPolitico($politico); $politicoI18n->setCulture('ca'); $politicoI18n->save(); } $c = new Criteria(); $c->add(PoliticoListaPeer::LISTA_ID, $lista->getId()); $c->add(PoliticoListaPeer::POLITICO_ID, $politico->getId()); $pl = PoliticoListaPeer::doSelectOne($c); if (!$pl) { $pl = new PoliticoLista(); $pl->setLista($lista); $pl->setPolitico($politico); } else { echo "Ya estaba.\n"; } if ($data[6] != '1') { $pl->setOrden($data[0]); } $pl->save(); //echo $data[2]; } fclose($handle); }
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); }
public function __construct($convocatoria_id, $partido_id, $geoName, $order = "pd") { $this->geoName = $geoName; $this->convocatoriaId = $convocatoria_id; $this->partidoId = $partido_id; $this->convocatoria = ConvocatoriaPeer::retrieveByPK($convocatoria_id); if ($this->convocatoria->getClosedAt()) { $query = "SELECT DISTINCT p.id, p.vanity, p.nombre, p.apellidos, p.imagen, l.sumu, l.sumd, r.last_date lastDate, p.sf_guard_user_id\n\t\t\tFROM politico p\n\t\t\tLEFT JOIN sf_guard_user u ON u.id = p.sf_guard_user_id\n\t\t\tINNER JOIN lista_calle l ON l.politico_id = p.id "; if ($geoName) { $query .= "INNER JOIN circunscripcion c ON c.id = l.circunscripcion_id "; $query .= "INNER JOIN geo g ON g.id = c.geo_id "; } $query .= "LEFT JOIN (SELECT entity_id, MAX(IFNULL(r.modified_at, r.created_at)) last_date FROM sf_review r WHERE r.value = 1 AND r.sf_review_type_id = 1 GROUP BY entity_id, sf_review_type_id) r ON (r.entity_id = p.id) "; $query .= "WHERE l.convocatoria_id = ? "; if ($partido_id) { $query .= "AND l.partido_id = ? "; } if ($geoName) { $query .= "AND g.nombre = ? "; } switch ($order) { case 'pa': $query .= "ORDER BY l.sumu ASC, l.sumd DESC, r.last_date DESC, p.apellidos ASC, p.nombre ASC;"; break; case 'pd': $query .= "ORDER BY l.sumu DESC, l.sumd ASC, r.last_date DESC, p.apellidos ASC, p.nombre ASC;"; break; case 'na': $query .= "ORDER BY l.sumd ASC, l.sumu DESC, r.last_date DESC, p.apellidos ASC, p.nombre ASC;"; break; case 'nd': $query .= "ORDER BY l.sumd DESC, l.sumu ASC, r.last_date DESC, p.apellidos ASC, p.nombre ASC;"; break; } $connection = Propel::getConnection(); $statement = $connection->prepare($query); $idx = 1; $statement->bindValue($idx++, $convocatoria_id); if ($partido_id) { $statement->bindValue($idx++, $partido_id); } if ($geoName) { $statement->bindValue($idx++, $geoName); } $statement->execute(); $this->politicos = $statement->fetchAll(PDO::FETCH_CLASS, 'Candidato'); } else { $query = "SELECT DISTINCT p.*, r.last_date lastDate\n\t\t\tFROM politico p\n\t\t\tINNER JOIN politico_lista pl ON pl.politico_id = p.id\n\t\t\tINNER JOIN lista l ON l.id = pl.lista_id "; if ($geoName) { $query .= "INNER JOIN circunscripcion c ON c.id = l.circunscripcion_id "; $query .= "INNER JOIN geo g ON g.id = c.geo_id "; } $query .= "LEFT JOIN (SELECT entity_id, MAX(IFNULL(r.modified_at, r.created_at)) last_date FROM sf_review r WHERE r.value = 1 AND r.sf_review_type_id = 1 GROUP BY entity_id, sf_review_type_id) r ON (r.entity_id = p.id) "; $query .= "WHERE l.convocatoria_id = ? "; if ($partido_id) { $query .= "AND l.partido_id = ? "; } if ($geoName) { $query .= "AND g.nombre = ? "; } switch ($order) { case 'pa': $query .= "ORDER BY p.sumu ASC, p.sumd DESC, r.last_date DESC, p.apellidos ASC, p.nombre ASC;"; break; case 'pd': $query .= "ORDER BY p.sumu DESC, p.sumd ASC, r.last_date DESC, p.apellidos ASC, p.nombre ASC;"; break; case 'na': $query .= "ORDER BY p.sumd ASC, p.sumu DESC, r.last_date DESC, p.apellidos ASC, p.nombre ASC;"; break; case 'nd': $query .= "ORDER BY p.sumd DESC, p.sumu ASC, r.last_date DESC, p.apellidos ASC, p.nombre ASC;"; break; } $connection = Propel::getConnection(); $statement = $connection->prepare($query); $idx = 1; $statement->bindValue($idx++, $convocatoria_id); if ($partido_id) { $statement->bindValue($idx++, $partido_id); } if ($geoName) { $statement->bindValue($idx++, $geoName); } $statement->execute(); $this->politicos = $statement->fetchAll(PDO::FETCH_CLASS, 'Candidato'); //echo (time()-$initTime). "-5 ($convocatoria_id , $partido_id , $geoName , $order)<br>"; } $this->numEscanyos = false; }