<?php
/**
* Displays differences between 2 versions of this article.
*
* @author Eloi George <eloi@NOSPAM.bygeorgeware.com>
* @module Article Manager
* @param int new : Version # of the newer revision. 
* @param int old : Version # of the older revision. 
* @return none
*/

/* If user is unathorized to use this method, leave */

/* Retrieve the requested versions */
/* If both the requested versions aren't available, leave */

/* Create Diff output of the requested versions */
/* Set the change summary text */

require_once(PHPWS_SOURCE_DIR . 'mod/article/class/unified_diff.php');
$sql = 'SELECT * FROM mod_article_oldversions WHERE article_id='.$this->id.' AND version=';

/* Retrieve the older article version */
$result = $GLOBALS['core']->query($sql.$oldversion, 1);
$viewtext = $_SESSION['translate']->it('View this version on a new page');
if ($result && $row = $result->fetchrow(DB_FETCHMODE_ASSOC)) {
  $old_object = unserialize(stripslashes($row['article']));
  $tags['THIS_LABEL'] = $_SESSION['translate']->it('Revision [[var2]] as of [var1]', date(PHPWS_DATE_FORMAT . ' ' . PHPWS_TIME_FORMAT, (strtotime($old_object->updated_date)+$GLOBALS['core']->datetime->time_dif))
    , '<a href="index.php?module=article&viewarchive='.$this->id.'&version='.$oldversion
      . '" title="'.$viewtext.'" alt="'.$viewtext.'" target="_blank">'.$oldversion.'</a>');
}
else
  echo 'ERROR: Version '.$oldversion.' not found!!!<br />';

/* Retrieve the newer article version */
if($newversion==$this->version) {
  $new_object = $this;
  $tags['THAT_LABEL'] = $_SESSION['translate']->it('Current Version');
}
else {
  $result = $GLOBALS['core']->query($sql.$newversion, 1);
  if ($result && $row = $result->fetchrow(DB_FETCHMODE_ASSOC)) {
    $new_object = unserialize(stripslashes($row['article']));
    $tags['THAT_LABEL'] = $_SESSION['translate']->it('Revision [[var2]] as of [var1]', date(PHPWS_DATE_FORMAT . ' ' . PHPWS_TIME_FORMAT, (strtotime($new_object->updated_date)+$GLOBALS['core']->datetime->time_dif))
    , '<a href="index.php?module=article&viewarchive='.$this->id.'&version='.$newversion
      . '" title="'.$viewtext.'" alt="'.$viewtext.'" target="_blank">'.$newversion.'</a>');
  }
  else
    echo 'ERROR: Version '.$newversion.' not found!!!<br />';
}

/* Create a Diff object. (Unified HTML - Tags Stripped) */
$allowedtags = '<b>,<a>,<i>,<img>,<h1>,<h2>,<h3>,<h4>,<h5>,<h6>,<font>';
$lines1 = PHPWS_Text::sentence(strip_tags($old_object->view(1,1), $allowedtags));
$lines2 = PHPWS_Text::sentence(strip_tags($new_object->view(1,1), $allowedtags));
$diff = &new Text_Diff($lines1, $lines2);
/* Determine what changes were made */
$renderer = &new Text_Diff_Renderer_html(999);

$tags['DIFFCONTENT'] = $renderer->render($diff);
$tags['BACKLINK'] = '<a href="index.php?module=article&viewhistory='. $_REQUEST['viewdiff'].'">' . $_SESSION['translate']->it('Back To Revision History').'</a>';
$tags['TITLE'] = $_SESSION['translate']->it('Difference Between Revisions');
$tags['STATS'] = $_SESSION['translate']->it('Lines Added:[var1] Deleted:[var2] Changed:[var3]', (string)$renderer->linesadded, $renderer->linesdeleted, $renderer->lineschanged);
$tags['TITLE2'] = $_SESSION['translate']->it('Current Version of This Article');
$tags['CURR_ARTICLE'] = $this->view(1,1);

$GLOBALS['CNT_article']['title'] = $this->title;
$GLOBALS['CNT_article']['content'] = PHPWS_Template::processTemplate($tags, 'article', 'diff.tpl');
?>