<?php

/* Security Check **********************************/
if(!$_SESSION['OBJ_user']->isDeity()) {
  header('location:./../index.php');
  exit();
}
/***************************************************/
/* Include announce classes */
require_once(PHPWS_SOURCE_DIR . 'mod/announce/class/Announcement.php');
$ArticleM_path = $GLOBALS['core']->home_dir . 'images/article/ANN/';
$ANNOUNCE_path = $GLOBALS['core']->home_dir . 'images/announce/';

/* If you require your announcements to look different than your articles,
 * define a default announcement template here.  
 * Make sure it does not have the ".tpl" extension.
 * ex: $DefaultPageTemplate = "Announcement";
 */
$DefaultArticleTemplate = NULL;
$DefaultSectionTemplate = NULL;

/* Get all the pages from old database */
$page_result = $GLOBALS['core']->getAll('SELECT id FROM mod_announce ORDER BY dateUpdated', true);
/* Check to see if any pages were found */
if(!$page_result) 
{
  echo 'No Announcements were found for conversion!<br />';
  return;
}
foreach($page_result as $value) 
{
  /* Create an old page object to store data temporarily */
  $old_ann = new PHPWS_Announcement($value['id']);

  /* Create a new page object to transfer old data into */
  $_SESSION['SES_ART_article'] = new PHPWS_Article;

	/* Assign directly transferable data to class variables */
	$_SESSION['SES_ART_article']->title = $old_ann->_subject;
	$_SESSION['SES_ART_article']->created_username = $old_ann->_userCreated;
	$_SESSION['SES_ART_article']->updated_username = $old_ann->_userUpdated;
	$_SESSION['SES_ART_article']->created_date = $old_ann->_dateCreated;
	$_SESSION['SES_ART_article']->updated_date = $old_ann->_dateUpdated;
	$_SESSION['SES_ART_article']->summary = PHPWS_Text::parseOutput($old_ann->_summary);
	$_SESSION['SES_ART_article']->allow_comments = $old_ann->_comments;
	$_SESSION['SES_ART_article']->allow_anonymous = $old_ann->_anonymous;
	$_SESSION['SES_ART_article']->hits = $old_ann->_hits;
	$_SESSION['SES_ART_article']->expiration_date = $old_ann->_expiration;
	$_SESSION['SES_ART_article']->publication_date = $old_ann->_dateCreated;
	$_SESSION['SES_ART_article']->approved = $old_ann->_approved;

  /* Set up page template if necessary */
  if ($DefaultArticleTemplate)
    $_SESSION['SES_ART_article']->template = $DefaultArticleTemplate;

  /* Create a new section to transfer old data into */
  $current_section = new PHPWS_Article_Section;
  
  if ($old_ann->_body)
    $current_section->text = PHPWS_Text::parseOutput($old_ann->_body);
  
  if ($DefaultSectionTemplate)
    $current_section->template = $DefaultSectionTemplate;

  /* Image Data Conversion */
  if($old_ann->_image && is_file($ANNOUNCE_path.$old_ann->_image['name']))
  {
    $current_section->image = $old_ann->_image; /* name, width, height & alt */
    $current_section->image['directory'] = 'ANN/';
    $current_section->image['link'] = $current_section->image['caption'] = '';
    /* Image file copy */
    PHPWS_File::fileCopy($ANNOUNCE_path . $current_section->image['name']
      , $ArticleM_path, $current_section->image['name'], 1, 1);
  }

  /* Add the section to the page */
  $current_section->edited = true;
  $_SESSION['SES_ART_article']->sections[$current_section->id] = $current_section;
  $_SESSION['SES_ART_article']->order[] = $current_section->id;
  
  /* Save final page structure into database */
  $_SESSION['SES_ART_article']->save();

  /* Setup page id array for menu conversion use */
  $page_ids[$value['id']] = $_SESSION['SES_ART_article']->id;
}// END PAGE LOOP

/* Convert Comments */
$page_result = $GLOBALS['core']->getAll('SELECT cid,itemId FROM mod_comments_data WHERE module="announce"', true);
if($page_result) 
  foreach($page_result as $value) 
  { 
    $newid = $page_ids[$value['itemId']];
    $GLOBALS['core']->query('UPDATE mod_comments_data SET module="article",itemId='.$newid.' WHERE cid='.$value['cid'], true);
  }

/* Convert Menu */
$page_result = $GLOBALS['core']->getAll('SELECT menu_item_id,menu_item_url FROM mod_menuman_items WHERE menu_item_url LIKE "%ANN_user_op=view%"', true);
if($page_result) 
  foreach($page_result as $value) 
  {
    $newid = $page_ids[(int)substr(strrchr($value['menu_item_url'], '='), 1)];
    $GLOBALS['core']->query('UPDATE mod_menuman_items SET menu_item_url="./index.php?module=article&ART=view:'.$newid.'" WHERE menu_item_id='.$value['menu_item_id'], true);
  }

/* Convert FatCat */
$page_result = $GLOBALS['core']->getAll('SELECT element_id,module_id FROM mod_fatcat_elements WHERE module_title="announce"', true);
if($page_result) 
  foreach($page_result as $value) 
  { 
    $newid = $page_ids[$value['module_id']];
    $GLOBALS['core']->query('UPDATE mod_fatcat_elements SET module_title="article",module_id='.$newid.',link="index.php?module=article&view='.$newid.'" WHERE element_id='.$value['element_id'], true);
  }

/* Convert Approval */
$page_result = $GLOBALS['core']->getAll('SELECT id,mod_title,mod_id FROM mod_approval_jobs WHERE mod_title="announce"', true);
if($page_result) 
  foreach($page_result as $value) 
  { 
    $newid = $page_ids[$value['mod_id']];
    $GLOBALS['core']->query('UPDATE mod_approval_jobs SET mod_title="article",mod_id=$newid WHERE id='.$value['id'], true);
  }

/* Write routing map to disk (taken from function writeReroute in convert/db.php */
$directory = $GLOBALS['core']->home_dir . 'convert/reroute/';
if (!is_dir($directory) || !is_writable($directory))
{
  echo 'Unable to create reroute file in convert/reroute directory.';
  return;
}
else
{
  $file = "<?php \n";
  foreach ($page_ids as $oldID=>$newID)
    $file .= "\$convert['". $oldID . "'] = ". $newID . ";\n";
  $file .= "\n?>";
  PHPWS_File::writeFile($directory.'/announce.AM', $file, TRUE, TRUE);
}

echo "<h3>Announcements Conversion Complete!</h3>";

?>
