<?php 

define("HOSTNAME", "ceata.org");
define("JOURNAL", "blog");
define("EVENTS", "events");
define("EVENTS2", "events2");
define("NEWS", "news");

$events2 = FALSE;

error_reporting(-1);
ini_set('display_errors', '1');

// Define the path to the site root directory.
$site_path = "./";

require_once 'read-feed.php';

// Update home page.
update_home_page();

function update_home_page()
{
    global $site_path;

    // Read header template content.
    $template_content = read_template($site_path."templates/header.ro.tpl");

    // Read templates content.
    $template_content .= read_template($site_path."templates/index.ro.tpl");

    // Read the footer template content.
    $template_content .= read_template($site_path."templates/footer.ro.tpl"); 

    // Update the the site title. 
    $template_content = str_replace("<!-- site-title -->", "Fundația Ceata &mdash; Ceata eliberează artele și tehnologiile actuale", $template_content);

    // Get Ceata events block content.
    $block_content = feed_list("events.ro.rss", 5, EVENTS, TRUE);

    // Verify if block content variable is not empty. 
    if (!empty($block_content))
      {
          $events2 = FALSE;
          $pre  = '<div id="events">';
          $pre .= '<a href="/events.ro.rss" class="flux"><img src="/files/images/feed.png" /></a>';
          $pre .= '<h2 class="block-title"><a href="/events.ro.html">Evenimente</a></h2>';
      
	  $post  = '<p class="toate">';
          $post .= '<a href="/events.ro.html">Vezi și evenimentele trecute &gt;&gt;</a>';
          $post .= '</p></div>';

	  $block_content = $pre.$block_content.$post;

          // Update journals block content. 
          $template_content = str_replace(
              "<!-- Evenimente -->", $block_content, $template_content
          );
      }
    else
      $events2 = TRUE;

    // Get Ceata journals block content.
    $block_content = feed_list("blog.rss", 5, JOURNAL);

    // Verify if block content variable is not empty. 
    if (!empty($block_content))
      {
          // Update journals block content. 
          $template_content = str_replace(
              "<!-- Jurnal -->", $block_content, $template_content
          );
      }

    if ($events2)
    {
      // Get Ceata events block content.
      $block_content = feed_list("events.ro.rss", 5, EVENTS2);

      // Verify if block content variable is not empty. 
      if (!empty($block_content))
        {
          $pre  = '<div id="events">';
          $pre .= '<a href="/events.ro.rss" class="flux"><img src="/files/images/feed.png" /></a>';
          $pre .= '<h2 class="block-title"><a href="/events.ro.html">Evenimente</a></h2>';
      
	  $post  = '<p class="toate">';
          $post .= '<a href="/events.ro.html">Vezi toate evenimentele &gt;&gt;</a>';
          $post .= '</p></div>';

	  $block_content = $pre.$block_content.$post;

          // Update journals block content. 
          $template_content = str_replace(
              "<!-- Evenimente2 -->", $block_content, $template_content
          );
        }
     }

    // Get Ceata news block content.
    $block_content = feed_list("news.ro.rss", 5);

    // Verify if block content variable is not empty. 
    if (!empty($block_content))
      {
          // Update journals block content. 
          $template_content = str_replace(
              "<!-- News -->", $block_content, $template_content
          );
      }

    // Create or update the home page.
    create_page($site_path."index.ro.html", $template_content);
}

function feed_list($feed_url, $number, $type=NEWS, $reverse=FALSE)
{
    $feed_list = '';

    // Get Ceata journals feeds.
    $feeds = get_feed($feed_url);

    $feedset = array();

    foreach($feeds as $key => $feed)
      {
        if ($key > $number-1) break;
        if ($type == EVENTS && (strtotime($feed['date']) < (strtotime("now") - 24*3600)))
		  break;
        array_push($feedset, $feed);
      }

    if ($reverse)
      $feedset = array_reverse($feedset, TRUE);
    
    if (!empty($feedset))
      {
          foreach($feedset as $key => $feed)
            {
                $description = $feed['description'];
		$append = '<a href="'.$feed['link'].'" target="_blank">[...]</a>';
		if ($type == JOURNAL)
		  $description = truncate_text($feed['description'], 255, $append);

                $feed_list .= '<li>';

		if ($type == JOURNAL)
		  $feed_list .= '<a href="'.$feed['link'].'" target="_blank" class="titlu">';
		else
		{
		  $link = str_replace("http://".HOSTNAME."/", "", $feed['link']);
                  $feed_list .= '<a href="/'.$link.'" class="titlu">';
		}

		$title = $feed['title'];
		if ($type == JOURNAL)
		{
		  $pos = strpos($feed['title'], ":");
		  $author = substr($feed['title'], 0, $pos);
		  $title = substr($feed['title'], $pos+1);
		}

                $feed_list .= $title.'</a>';

		if ($type == JOURNAL)
		  $feed_list .= '<br /><i>de '.$author.'</i>';

		// $feed_list .= '<span class="data">'.$feed['date'].'</span>';
                $feed_list .= '<div class="rezumat"><p>'.$description.'</p></div></li>'; 
            }

	  if ($type == EVENTS && empty($feed_list))
	    return $feed_list;

          $feed_list = '<ul class="listă-simplă">'.$feed_list.'</ul>';
      }

    return $feed_list;
}

/**
 * Read template function.
 *
 * Reads the content of a page template.
 *
 * @param $template_file
 *   A string, template file name.
 */
function read_template($template_file)
{
    // Get template content.
    $template_content = file_get_contents($template_file);

    return $template_content;
}

/**
 * Create page function.
 *
 * Creates or update a HTML page.
 *
 * @param $page_name
 *   A string, page file name.
 *
 * @param $content
 *   A string, page content.
 */
function create_page($page_name, $content)
{
    // Open file for writing.
    $file = fopen($page_name,"w");

    // Write content to file.
    fwrite($file, $content);

    fclose($file);
}

/**
 * Truncate text function.
 *
 * Truncates a text.
 *
 * @param $text
 *   Text to be truncated.
 * @param $max_text_length
 *   The length of truncated text.
 * @param $append
 *   String to append to the end of truncated text (such as '...').
 * @param $strip
 *   Strip tags or not.
 *
 * @return
 *   Truncated text or an empty string.
 */
function truncate_text($text, $text_length, $append = "", $strip = TRUE) 
{
    // Verify text.
    if ( empty($text) ) return '';

    // Verify wether to strip tags.
    if ($strip)
      {
          // Strip tags.
          $text = strip_tags($text);
      }

    if (strlen($text) > $text_length) 
      {
          $text_length += strpos(substr($text, $text_length), ".");
	  $text = utf8_encode($text);

          // Get the the substring of legn $text_length.
          $text = mb_substr($text, 0, $text_length, "UTF-8");
	  $text = utf8_decode($text).'.';

          // Add $append string.
          $text .= " ".$append;
      }

    return $text;
}

?>
