<?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';

$names = 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)
{
  $dataset = array();

  $dataset[] = $xpath->query("comname", $person)->item(0)->nodeValue;
  $dataset[] = $xpath->query("surname", $person)->item(0)->nodeValue;

  $names[] = $dataset;
}

$profiles = array();

$html = file_get_contents('http://www.pmb.ro/institutii/cgmb/componenta/consilieri/consilieri.php');
$doc = new DOMDocument();
$doc->loadHTML($html);
$xpath = new DOMXpath($doc);
  
$elements = $xpath->query("//table[@id='tabel']/tbody/tr[@class='table_row0']");

foreach ($elements as $e)
{
  $dataset = array();
  $iname = "";

  // Get row from person table
  $row = $xpath->query("td", $e);

  $name = $row->item(1)->nodeValue;
  $dataset['name'] = substr($name, 2);
  $surname = fărăDiacritice(lcfirst(strtok($dataset['name'], "– ")));
  if ($surname == 'alexandre') $surname = 'fontoura';
  if ($surname == 'boaja')
  {
    $dataset['name'] = utf8_encode("Boajă Minică");
  }
  if ($surname == 'popescu')
  {
    $comname = "";
    while ($r = strtok("– "))
    {
      $comname = lcfirst($r);
    }
    $iname = $comname.'-'.$surname;
  }
  else
  {
    foreach ($names as $name)
      if ($name[1] == $surname)
        $iname = $name[0].'-'.$name[1];
  }
  $dataset['name'] = cuLiniuță(trim(cuDiacriticeCorecte($dataset['name'])));

  $birth = $row->item(2)->nodeValue;
  $dataset['date'] = substr(strtok($birth, ", "), 2);
  $dataset['place'] = trim(cuDiacriticeCorecte(substr($birth, 14)));
  if ($iname == 'violeta-popescu')
  {
    $dataset['place'] = "Târgu Jiu, Jud. Gorj";
  }
  else if ($surname == 'pieptea')
  {
    $dataset['place'] = "Com. Văleni, Jud. Olt";
  }

  $occup = $row->item(3)->nodeValue;
  $dataset['occup'] = trim(cuDiacriticeCorecte(substr($occup, 2)));
  if ($surname == 'nicolescu')
  {
    $dataset['occup'] = "Cadru universitar";
  }
  else if ($surname == 'pieptea')
  {
    $dataset['occup'] = "Consilier C.G.M.B.";
  }

  $prof = $row->item(4)->nodeValue;
  $dataset['prof'] = trim(cuDiacriticeCorecte(substr($prof, 2)));

  $affil = $row->item(5)->nodeValue;
  $dataset['affil'] = fărăPunctuație(trim(cuDiacriticeCorecte(substr($affil, 2))));

  $profiles[$iname] = $dataset;
}

$declarations = array();

$html = file_get_contents('http://www.pmb.ro/institutii/declaratii_avere/d_avere_12_16_cgmb.php');
$doc = new DOMDocument();
$doc->loadHTML($html);
$xpath = new DOMXpath($doc);
  
$elements = $xpath->query("//table[@class='tabel']/tbody/tr[not(@class)]");

foreach ($elements as $e)
{
  $dataset = array();
  $iname = "";

  // Get row from person table
  $row = $xpath->query("td", $e);

  $res = $xpath->query("p", $row->item(0));
  if ($res->length) 
    $name = $res->item(0)->nodeValue;
  else
    $name = $row->item(0)->nodeValue;
  $surname = fărăDiacritice(lcfirst(strtok($name, "– ")));
  if ($surname == 'alexandre') $surname = 'fontoura';
  if ($surname == 'popescu')
  {
    $comname = "";
    while ($r = strtok("– "))
    {
      $comname = lcfirst($r);
    }
    $iname = $comname.'-'.$surname;
  }
  else
  {
    foreach ($names as $name)
      if ($name[1] == $surname)
        $iname = $name[0].'-'.$name[1];
    if ($iname == "")
      continue;
  }

  // Start wealth
  $wstart = "";
  $res = $xpath->query("a", $row->item(1));
  if ($res->length) 
    $wstart = $res->item(0)->getAttribute('href');
  $dataset['wstart'] = $wstart;

  // Start interests
  $istart = "";
  $res = $xpath->query("a", $row->item(2));
  if ($res->length) 
    $istart = $res->item(0)->getAttribute('href');
  $dataset['istart'] = $istart;

  // 2012 wealth
  $w2012 = "";
  $res = $xpath->query("a", $row->item(3));
  if ($res->length) 
    $w2012 = $res->item(0)->getAttribute('href');
  $dataset['w2012'] = $w2012;

  // 2012 interests
  $i2012 = "";
  $res = $xpath->query("a", $row->item(4));
  if ($res->length) 
    $i2012 = $res->item(0)->getAttribute('href');
  $dataset['i2012'] = $i2012;

  $declarations[$iname] = $dataset;
}

foreach ($names as $n)
{
  $in = $n[0].'-'.$n[1];

  $xml = '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL;
  $xml .= '<xml>'.PHP_EOL;
  $xml .= cuEtichetă("comname", extragePrenumele($profiles[$in]['name']));
  $xml .= cuEtichetă("surname", extrageNumele($profiles[$in]['name']));
  $xml .= cuEtichetă("fullname", inverseazăNumele($profiles[$in]['name']));

  $xml .= cuEtichetă("birthdate", $profiles[$in]['date']);
  $xml .= cuEtichetă("birthplace", $profiles[$in]['place']);
  $xml .= cuEtichetă("occupation", $profiles[$in]['occup']);
  $xml .= cuEtichetă("profession", $profiles[$in]['prof']);
  $xml .= cuEtichetă("affiliation", $profiles[$in]['affil']);

  $xml .= '<declarations>'.PHP_EOL;
  $xml .= "<declaration type='wealth' year='2012' file='".$declarations[$in]['wstart']."' />".PHP_EOL;
  $xml .= "<declaration type='wealth' year='2013' file='".$declarations[$in]['w2012']."' />".PHP_EOL;
  $xml .= "<declaration type='interest' year='2012' file='".$declarations[$in]['istart']."' />".PHP_EOL;
  $xml .= "<declaration type='interest' year='2013' file='".$declarations[$in]['i2012']."' />".PHP_EOL;
  $xml .= '</declarations>'.PHP_EOL;
  $xml .= '</xml>'.PHP_EOL;

  $file = fopen('../profile/00-pmb/'.$n[0].'-'.$n[1].'.xml', "w");
  fwrite($file, $xml);
  fclose($file);
}

?>
