<?php

/**
 * File name: ordonează-fluxul-după-dată.php
 * Sort an RSS feed by pubDate
 *
 * (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
 */

// Number of month by name
$monthnum = array(
	  "Ianuarie" => "1",
	  "Februarie" => "2",
	  "Martie" => "3",
	  "Aprilie" => "4",
	  "Mai" => "5",
	  "Iunie" => "6",
	  "Iulie" => "7",
	  "August" => "8",
	  "Septembrie" => "9",
	  "Octombrie" => "10",
	  "Noiembrie" => "11",
	  "Decembrie" => "12",
);

// Name of month by number
$monthname = array(
	  "01" => "ianuarie",
	  "02" => "februarie",
	  "03" => "martie",
	  "04" => "aprilie",
	  "05" => "mai",
	  "06" => "iunie",
	  "07" => "iulie",
	  "08" => "august",
	  "09" => "septembrie",
	  "10" => "octombrie",
	  "11" => "noiembrie",
	  "12" => "decembrie",
);

// Name of month (1-2 digits) by number
$monthname1 = array(
	  "1" => "ianuarie",
	  "2" => "februarie",
	  "3" => "martie",
	  "4" => "aprilie",
	  "5" => "mai",
	  "6" => "iunie",
	  "7" => "iulie",
	  "8" => "august",
	  "9" => "septembrie",
	  "10" => "octombrie",
	  "11" => "noiembrie",
	  "12" => "decembrie",
);

// Translate months from English to Romanian
$monthtrans = array(
	  "January" => "ianuarie",
	  "February" => "februarie",
	  "March" => "martie",
	  "April" => "aprilie",
	  "May" => "mai",
	  "June" => "iunie",
	  "July" => "iulie",
	  "August" => "august",
	  "September" => "septembrie",
	  "October" => "octombrie",
	  "November" => "noiembrie",
	  "December" => "decembrie",
);

// Abbreviated months in Romanian
$monthabr = array(
	  "ianuarie" => "ian.",
	  "februarie" => "feb.",
	  "martie" => "mart.",
	  "aprilie" => "apr.",
	  "mai" => "mai",
	  "iunie" => "iun.",
	  "iulie" => "iul.",
	  "august" => "aug.",
	  "septembrie" => "sept.",
	  "octombrie" => "oct.",
	  "noiembrie" => "nov.",
	  "decembrie" => "dec.",
);

function transformă($rss_file, $xsl_file, $res_file)
{
  $xml = new DOMDocument();
  $xml->load($rss_file);

  $xsl = new DOMDocument();
  $xsl->load($xsl_file);

  $proc = new XSLTProcessor();
  $proc->importStylesheet($xsl);

  $res = $proc->transformToXML($xml);

  $file = fopen($res_file, "w");
  fwrite($file, $res);
  fclose($file);
}

function cuEtichetă($tag, $value)
{
  return '<'.$tag.'>'.$value.'</'.$tag.'>'.PHP_EOL;
}

function extrageNumele($str)
{
  return strtok($str, " ");
}

function extragePrenumele($str)
{
  $surname = strtok($str, " ");
  $gnames = substr($str, strlen($surname)+1);

  return $gnames;
}

function inverseazăNumele($str)
{
  $surname = strtok($str, " ");
  $gnames = substr($str, strlen($surname)+1);
  $str = $gnames.' '.$surname;

  return $str;
}

function fărăDiacritice($str)
{
  $str = utf8_decode($str);
  $str = str_replace("ă", "a", $str);
  $str = str_replace("Ă", "a", $str);
  $str = str_replace("â", "a", $str);
  $str = str_replace("Â", "a", $str);
  $str = str_replace("î", "i", $str);
  $str = str_replace("Î", "i", $str);
  $str = str_replace("ş", "s", $str);
  $str = str_replace("Ş", "s", $str);
  $str = str_replace("ș", "s", $str);
  $str = str_replace("Ș", "s", $str);
  $str = str_replace("ţ", "t", $str);
  $str = str_replace("ț", "ț", $str);
  $str = str_replace("Ţ", "t", $str);

  return $str;
}

function cuDiacriticeCorecte($str)
{
  $str = utf8_decode($str);
  $str = str_replace("ş", "ș", $str);
  $str = str_replace("Ş", "Ș", $str);
  $str = str_replace("ţ", "ț", $str);
  $str = str_replace("Ţ", "Ț", $str);

  return $str;
}

function cuLiniuță($str)
{
  $str = str_replace(" - ", "-", $str);
  $str = str_replace(" – ", "-", $str);
  $str = str_replace(" -", "-", $str);

  return $str;
}

function fărăPunctuație($str)
{
  $str = str_replace("-", "", $str);
  $str = str_replace(".", "", $str);

  return $str;
}

?>
