/**
  * API (protetta da una API key)
  * torna flusso xml di tutti i parlamentari (divisi per ramo)
  * per ciascun parlamentare, visualizza i dati anagrafici e di carica
  * nonché informazioni variabili, a seconda del parametro info_type
  *  - presenze: presenze, assenza, missione
  *  - attivita: indice di attività parlamentare (nuovo)
  *  - ribelli:  numero di voti ribelli
  * progetto op_kw
  *
  *  <opkw xmlns="http://www.openpolis.it/2010/opkw"
  *         xmlns:op="http://www.openpolis.it/2010/op"
  *         xmlns:op_location="http://www.openpolis.it/2010/op_location"
  *         xmlns:op_politician="http://www.openpolis.it/2010/op_politician"
  *         xmlns:xlink="http://www.w3.org/1999/xlink">
  *    <op:content> 
  *      <camera n_rappresentanti="600">
  *        <parlamentare xlink:href="/parlamentari/12345.xml">
  *          <thumbnail xlink:href="/parlamentari/thumb/332104.jpeg" width="40" height="53"/>
  *          <nome>NOME</nome>
  *          <cognome>COGNOME</cognome>
  *          <gruppo>
  *            <nome>NOME GRUPPO</nome>
  *            <acronimo>NOME GRUPPO</acronimo>
  *          </gruppo>
  *          <data_inizio_carica>2008-04-12</data_inizio_carica> ** solo se successiva a data inizio leg.
  *          <circoscrizione>Lombardia 1</circoscrizione>
  *
  *          ...* informazioni *...
  *
  *        </parlamentare>
  *        ...
  *      </camera>
  *      <senato>
  *        <parlamentare> ... </parlamentare>
  *      </camera>
  *    </op:content>
  *  </opkw>
  *
  * Informazioni possono essere:
  *          <presenze>
  *            <numero>3898</numero>
  *            <percentuale>84,59</percentuale>
  *          </presenze>
  *          <assenze>
  *            <numero>710</numero>
  *            <percentuale>15,41</percentuale>
  *          </assenze>
  *          <missioni>
  *            <numero>0</numero>
  *            <percentuale>0,00</percentuale>
  *          </missioni>
  *          <mese_precedente>+1</mese_precedente>
  *
  *          <indice>
  *            <numero>389,8</numero>
  *          </indice>
  *          <mese_precedente>+-1</mese_precedente>
  *
  *          <voti_ribelli>
  *            <numero>15</numero>
  *          </indice>
  *          <mese_precedente>-1</mese_precedente>
  *       
  * Return error in case something's wrong
  * <opkw xlmns="http://www.openpolis.it/2010/opkw"
  *       xmlns:op="http://www.openpolis.it/2010/op"
  *       xlmns:op_location="http://www.openpolis.it/2010/op_location"
  *       xmlns:op_politician="http://www.openpolis.it/2010/op_politician">
  *   <op:error>Messaggio di errore</op:error>
  * </opkw>
  * @return String
  * @author Guglielmo Celata
  **/
 public function executeElencoParlamentari()
 {
     $key = $this->getRequestParameter('key');
     $ramo = $this->getRequestParameter('ramo');
     $infotype = $this->getRequestParameter('infotype');
     $order_by = $this->getRequestParameter('orderby');
     $order_type = $this->getRequestParameter('ordertype');
     $con = Propel::getConnection('propel');
     $current_data = OppPoliticianHistoryCachePeer::fetchLastData('P', null, $con);
     $is_valid_key = deppApiKeysPeer::isValidKey($key);
     $resp_node = new SimpleXMLElement('<opkw xmlns="' . $this->opkw_ns . '" ' . ' xmlns:op="' . $this->op_ns . '" ' . ' xmlns:xlink="' . $this->xlink_ns . '" >' . '</opkw>');
     if ($is_valid_key) {
         // start producing xml
         $content_node = $resp_node->addChild('op:content', null, $this->op_ns);
         // camera o senato
         if ($ramo == 'tutti') {
             $rami = array('camera' => 'C', 'senato' => 'S');
         } else {
             $rami = array($ramo => strtoupper(substr($ramo, 0, 1)));
         }
         foreach ($rami as $ramo_name => $ramo_code) {
             $ramo_node = $content_node->addChild($ramo_name, null, $this->opkw_ns);
             // dato storico aggregato per tutto il ramo
             $ramo_storico_node = $ramo_node->addChild('ramo', null, $this->opkw_ns);
             $rs = OppPoliticianHistoryCachePeer::getKWRamoRS($ramo_code, $con);
             $cnt = 0;
             while ($rs->next()) {
                 $cnt++;
                 $r = $rs->getRow();
                 $this->addRamoNode($ramo_storico_node, $r, $infotype);
             }
             // gruppi
             $gruppi_node = $ramo_node->addChild('gruppi', null, $this->opkw_ns);
             $n_gruppi = OppPoliticianHistoryCachePeer::countByDataRamoChiTipo($current_data, $ramo_code, 'G', $con);
             $rs = OppPoliticianHistoryCachePeer::getKWGruppoRSByDataRamo($current_data, $ramo_code, null, null, $con);
             $cnt = 0;
             while ($rs->next()) {
                 $cnt++;
                 $g = $rs->getRow();
                 $this->addGruppoNode($gruppi_node, $g, $infotype, $cnt);
             }
             $gruppi_node->addAttribute('n_gruppi', $n_gruppi);
             // parlamentari
             $parlamentari_node = $ramo_node->addChild('parlamentari', null, $this->opkw_ns);
             $n_parlamentari = OppPoliticianHistoryCachePeer::countByDataRamoChiTipo($current_data, $ramo_code, 'P', $con);
             $rs = OppPoliticianHistoryCachePeer::getKWPoliticiRSByDataRamo($current_data, $ramo_code, $order_by, $order_type, $con);
             $cnt = 0;
             while ($rs->next()) {
                 $cnt++;
                 $p = $rs->getRow();
                 $this->addParlamentareNode($parlamentari_node, $p, $infotype, $cnt);
             }
             $parlamentari_node->addAttribute('n_rappresentanti', $n_parlamentari);
         }
     } else {
         $resp_node->addChild('op:error', 'Chiave di accesso non valida', $this->op_ns);
     }
     $xmlContent = $resp_node->asXML();
     $this->_send_output($xmlContent);
     return sfView::NONE;
 }
 public static function retrieveByPKs($pks, $con = null)
 {
     if ($con === null) {
         $con = Propel::getConnection(self::DATABASE_NAME);
     }
     $objs = null;
     if (empty($pks)) {
         $objs = array();
     } else {
         $criteria = new Criteria();
         $criteria->add(deppApiKeysPeer::ID, $pks, Criteria::IN);
         $objs = deppApiKeysPeer::doSelect($criteria, $con);
     }
     return $objs;
 }