<?php

use Bitweaver\Blogs\BitBlog;
use Bitweaver\Blogs\BitBlogPost;
use Bitweaver\KernelTools;
use Bitweaver\Rss\FeedItem;
use Bitweaver\Users\RolePermUser;
use Bitweaver\Users\RoleUser;
/**
 * @version $Header$
 * @package blogs
 * @subpackage functions
 */

/**
 * Initialization
 */
require_once( "../kernel/includes/setup_inc.php" );

$gBitSystem->verifyPackage( 'rss' );
$gBitSystem->verifyPackage( 'blogs' );
$gBitSystem->verifyFeature( 'blogs_rss' );

require_once RSS_PKG_INCLUDE_PATH.'rss_inc.php';

// default feed info
$rss->title = $gBitSystem->getConfig( 'blogs_rss_title', $gBitSystem->getConfig( 'site_title' ).' - '.KernelTools::tra( 'Blog Posts' ) );
$rss->description = $gBitSystem->getConfig( 'blogs_rss_description', $gBitSystem->getConfig( 'site_title' ).' - '.KernelTools::tra( 'RSS Feed' ) );

// check permission to view wiki pages
if( !$gBitUser->hasPermission( 'p_blogs_view' ) ) {
	require_once RSS_PKG_PATH."rss_error.php";
} else {
	// check if we want to use the cache file
	$cacheFile = TEMP_PKG_PATH.RSS_PKG_NAME.'/'.BLOGS_PKG_NAME.( !empty( $_REQUEST['user_id'] ) ? "_".$_REQUEST['user_id'] : "" ).( !empty( $_REQUEST['group_id'] ) ? "_".$_REQUEST['group_id'] : "" ).( !empty( $_REQUEST['role_id'] ) ? "_".$_REQUEST['role_id'] : "" ).( !empty( $_REQUEST['blog_id'] ) ? "_".$_REQUEST['blog_id'] : "" ).'_'.$cacheFileTail;
	$rss->useCached( $rss_version_name, $cacheFile, $gBitSystem->getConfig( 'rssfeed_cache_time' ));

	$blogPost = new BitBlogPost();
	$listHash['sort_mode'] = 'last_modified_desc';
	$listHash['max_records'] = $gBitSystem->getConfig( 'blogs_rss_max_records', 10 );
	$listHash['parse_data'] = true;
	$listHash['full_data'] = true;
	if( !empty( $_REQUEST['user_id'] ) ) {
		$blogUser = new RoleUser();

		$userData = $blogUser->getUserInfo( [ 'user_id' => $_REQUEST['user_id'] ] );
		// dont try and fool me
		if (!empty($userData)) {
			$userName = $userData['real_name']?$userData['real_name']:$userData['login'];
			$rss->title = $userName." at ".$gBitSystem->getConfig( 'site_title' );
			$listHash['user_id'] = $_REQUEST['user_id'];
		}
	} else if( !empty( $_REQUEST['role_id'] ) ) {
		$permUser = new RolePermUser();
		$roleData = $permUser->getRoleInfo( $_REQUEST['role_id'] );
		// dont try and fool me
		if (!empty($roleData)){
			$roleName = $roleData['role_name'];
			$rss->title = $roleName." Role at ".$gBitSystem->getConfig( 'site_title' );
			$listHash['role_id'] = $_REQUEST['role_id'];
		}
	}

	if( !empty( $_REQUEST['blog_id'] ) ) {
		$listHash['blog_id'] = $_REQUEST['blog_id'];
		$gBlog = new BitBlog( $_REQUEST['blog_id'] );
		$gBlog->load();
		if( isset($gBlog->mContentId) ) {
			// adjust feed title to blog title
			$rss->title = $gBlog->getTitle()." at ".$gBitSystem->getConfig( 'site_title' );
			if (isset($userName)){
				$rss->title = $userName."'s Posts in ".$rss->title;
			}
			$rss->description = $gBlog->getParsedData();
		}
	}
	$feeds = $blogPost->getList( $listHash );

	// set the rss link
	$rss->link = 'http://'.$_SERVER['HTTP_HOST'].BLOGS_PKG_URL.( !empty( $_REQUEST['blog_id'] ) ? "?blog_id=".$_REQUEST['blog_id'] : "" );
	// get all the data ready for the feed creator
	foreach( $feeds as $feed ) {
		$item = new FeedItem();
		$item->title = BitBlogPost::getTitleFromHash( $feed );
		$item->link = BIT_BASE_URI.BitBlogPost::getDisplayUrlFromHash( $feed );
		$item->description = $feed['parsed'];

		$item->date = ( int )$feed['last_modified'];
		$item->source = 'https://'.$_SERVER['HTTP_HOST'].BIT_ROOT_URL;
		$item->author = $feed['real_name'];
		$item->authorEmail = $feed['email'];

		$item->descriptionTruncSize = $gBitSystem->getConfig( 'rssfeed_truncate', 50000 );
		$item->descriptionHtmlSyndicated = true;

		// pass the item on to the rss feed creator
		$rss->addItem( $item );
	}

	// finally we are ready to serve the data
	echo $rss->saveFeed( $rss_version_name, $cacheFile );
}