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

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

$pnames = new DOMDocument();
$pnames->load('../numele-partidelor.xml');
$xnames = new DOMXpath($pnames);

// Load template for profiles
$tpl = file_get_contents('../șabloane/profil.tpl');

foreach($persons as $person)
{
  // Copy template to page
  $page = $tpl;

  // Get index name
  $comname = $xpath->query("comname", $person)->item(0)->nodeValue;
  $surname = $xpath->query("surname", $person)->item(0)->nodeValue;
  $iname = $comname.'-'.$surname;

  $comnamep = $xpath->query("comnamep", $person)->item(0)->nodeValue;
  $surnamep = $xpath->query("surnamep", $person)->item(0)->nodeValue;
  $shortname = $comnamep.' '.$surnamep;

  $page = str_replace("<!-- iname -->", $iname, $page);

  // Load news page
  $news = file_get_contents('../consilieri/știri/'.$iname.'.html');

  $page = str_replace("<!-- news -->", $news, $page);

  // Load profile xml
  $xmla = new DOMDocument();
  $xmla->load('../profile/'.$iname.'.xml');
  $xpatha = new DOMXpath($xmla);

  $fullname = $xpatha->query("/xml/fullname")->item(0)->nodeValue;
  $comnamef = $xpatha->query("/xml/comname")->item(0)->nodeValue;
  $surnamef = $xpatha->query("/xml/surname")->item(0)->nodeValue;

  $birthdate = $xpatha->query("/xml/birthdate")->item(0)->nodeValue;
  if ($birthdate)
  {
    $age = intval((time() - strtotime($birthdate))/31536000);
    $age .= " de ani";
    $month = "";
    preg_match('/\.([0-9]+)\./', $birthdate, $month);
    $month = trim($month[0], ".");
    $month = $monthname1[$month];
    $birthdate = preg_replace('/\.(.*)\./', ' '.$month.' ', $birthdate);
  }
  else $age = "";

  $birthplace = $xpatha->query("/xml/birthplace")->item(0)->nodeValue;
  $occupation = $xpatha->query("/xml/occupation")->item(0)->nodeValue;
  $profession = $xpatha->query("/xml/profession")->item(0)->nodeValue;
  $party = $xpatha->query("/xml/party")->item(0)->nodeValue;
  $affiliation = $xpatha->query("/xml/affiliation")->item(0)->nodeValue;
  $sex = $xpatha->query("/xml/sex")->item(0)->nodeValue;

  $partyname = $xnames->query('//party[@id="'.strtolower($party).'"]')->item(0)->nodeValue;
  if ($party != $affiliation)
    $partyname .= ' ('.$xnames->query('//party[@id="'.strtolower($affiliation).'"]')->item(0)->nodeValue.')';

  if ($sex == "F")
    $sex = "feminin";
  else $sex = "masculin";

  $image = '../imagini/00-portrete/'.$iname.'.jpg';
  $placeholder = '../imagini/naught101-ProfilePlaceholderSuit.png';
  if (!file_exists($image))
    $image = $placeholder;

  $page = str_replace("<!-- comnamef -->", $comnamef, $page);
  $page = str_replace("<!-- surnamef -->", $surnamef, $page);
  $page = str_replace("<!-- fullname -->", $fullname, $page);
  $page = str_replace("<!-- shortname -->", $shortname, $page);
  $page = str_replace("<!-- birthdate -->", $birthdate, $page);
  $page = str_replace("<!-- birthplace -->", $birthplace, $page);
  $page = str_replace("<!-- occupation -->", $occupation, $page);
  $page = str_replace("<!-- profession -->", $profession, $page);
  $page = str_replace("<!-- party -->", $party, $page);
  $page = str_replace("<!-- partyname -->", $partyname, $page);
  $page = str_replace("<!-- affiliation -->", $affiliation, $page);
  $page = str_replace("<!-- sex -->", $sex, $page);
  $page = str_replace("<!-- image -->", $image, $page);

  $page = str_replace("<!-- age -->", $age, $page);  

  $declarations = $xpatha->query("//declaration");
  $wealth = $interest = "";
  foreach($declarations as $declaration)
  {
    if ($declaration->getAttribute('type') == 'wealth')
    {
      if ($declaration->getAttribute('year') == '2012')
        $wealth .= '<li><a href="'.$declaration->getAttribute('file').'" target="_blank" class="extern">Declarația de început de mandat</a></li>';
      if ($declaration->getAttribute('year') == '2013')
        $wealth .= '<li><a href="'.$declaration->getAttribute('file').'" target="_blank" class="extern">Declarația pentru anul 2012</a></li>';
    }
    if ($declaration->getAttribute('type') == 'interest')
    {
      if ($declaration->getAttribute('year') == '2012')
        $interest .= '<li><a href="'.$declaration->getAttribute('file').'" target="_blank" class="extern">Declarația de început de mandat</a></li>';
      if ($declaration->getAttribute('year') == '2013')
        $interest .= '<li><a href="'.$declaration->getAttribute('file').'" target="_blank" class="extern">Declarația pentru anul 2012</a></li>';
    }
  }

  if ($wealth != "")
    $wealth = '<ul>'.$wealth.'</ul>';
  if ($interest != "")
    $interest = '<ul>'.$interest.'</ul>';

  $page = str_replace("<!-- wealth -->", $wealth, $page);
  $page = str_replace("<!-- interest -->", $interest, $page);

  $file = fopen('../consilieri/'.$iname.'.html', "w");
  fwrite($file, $page);
  fclose($file);
}

?>
