<?php

/**
 * File name: 
 * 
 *
 * (C) Copyright 2013 Friedrich-Ebert-Stiftung (http://fes.ro)
 * Author: Tiberiu C. Turbureanu (tct@ceata.org)
 *
 * This file is part of the project funded by FES
 *
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

require_once 'utile.php';

$inames = array();

// Load the list of persons
$xml = new DOMDocument();
$xml->load('../20131013-consilieri.xml');
$xpath = new DOMXpath($xml);
$persons = $xpath->query("/xml/person");

foreach($persons as $person)
{
  // Get index name
  $comname = $xpath->query("comname", $person)->item(0)->nodeValue;
  $surname = $xpath->query("surname", $person)->item(0)->nodeValue;

  $inames[] = $comname.'-'.$surname;
}

$v = new DOMDocument();
$v->formatOutput = true;
$table = $v->appendChild($v->createElement("table"));

$html = file_get_contents('../documente/victorioșii-din-bucurești-2012.html');
$doc = new DOMDocument();
$doc->loadHTML($html);
$xpath = new DOMXpath($doc);

$elements = $xpath->query("//tr[@class='ro2']");

foreach ($elements as $e)
{
  $fullname = $xpath->query("td/p", $e)->item(6)->nodeValue;
  $fullname = fărăDiacritice(utf8_encode(strtolower($fullname)));

  $fnames = explode(" ", $fullname);
  $fsurnames = trim($fnames[0]);
  $fsurnames = explode("-", $fsurnames);
  $fcomnames = array();
  for ($k = 1; $k < count($fnames); $k++)
  {
    // Delete any non-(chars and dash)
    preg_match('/[a-z-]+/', $fnames[$k], $temp);
    $temps = explode("-", $temp[0]);
    foreach ($temps as $t)
    {
      $fcomnames[] = trim($t);
    }
  }

  foreach ($inames as $i)
  {
    $names = explode("-", $i);
    $comname = $names[0];
    $surname = $names[1];

    if (in_array($surname, $fsurnames) && in_array($comname, $fcomnames))
    {
      $table->appendChild($v->importNode($e->cloneNode(true), true));
    }
  }
}

$v->saveHTMLFile('../documente/aleșii-din-bucurești-2012.html');

?>
