<?php
/***********************************************************************
 | Cerb(tm) developed by Webgroup Media, LLC.
 |-----------------------------------------------------------------------
 | All source code & content (c) Copyright 2002-2019, Webgroup Media LLC
 |   unless specifically noted otherwise.
 |
 | This source code is released under the Devblocks Public License.
 | The latest version of this license can be found here:
 | http://cerb.ai/license
 |
 | By using this software, you acknowledge having read this license
 | and agree to be bound thereby.
 | ______________________________________________________________________
 |	http://cerb.ai	    http://webgroup.media
 ***********************************************************************/

class UmScKbRssController extends Extension_UmScRssController {
	const ID = 'cerberusweb.kb.sc.rss.controller';

	function __const($manifest=null) {
		parent::_const($manifest);
	}
	
	function handleRequest(DevblocksHttpRequest $request) {
		$path = $request->path;
		
		if(empty($path) || !is_array($path))
			return;

		if(null == ($portal = ChPortalHelper::getCode()))
			return;
		
		switch(array_shift($path)) {
			case 'most_popular':
				$this->_renderMostPopularRss($portal);
				break;
				
			case 'recent_changes':
				$this->_renderRecentChangesRss($portal);
				break;
				
			case 'new_articles':
				$this->_renderNewArticlesRss($portal);
				break;
		}
	}
	
	private function _renderMostPopularRss($portal) {
		DevblocksPlatform::services()->http()->setHeader('Content-Type', 'application/rss+xml');
		
		$xmlstr = <<<XML
		<rss version='2.0' xmlns:atom='http://www.w3.org/2005/Atom'>
		</rss>
XML;

		$xml = new SimpleXMLElement($xmlstr);
		$url = DevblocksPlatform::services()->url();

		// Portal details
		$portal_name = DAO_CommunityToolProperty::get($portal, UmScApp::PARAM_PAGE_TITLE, '');

		// Channel
		$channel = $xml->addChild('channel');
		$channel->addChild('title', (!empty($portal_name) ? ('['.$portal_name.'] ') : '') . "Most Popular Articles");
		$channel->addChild('link', $url->write('c=rss&kb=kb&a=most_popular',true));
		$channel->addChild('description', '');
		
		// Limit topics to portal config
		$topics = DAO_CommunityToolProperty::getJson($portal, UmScKbController::PARAM_KB_ROOTS, '[]');
		if(empty($topics))
			return;
		
		// Search Results
		list($results,) = DAO_KbArticle::search(
			array(
				SearchFields_KbArticle::ID,
				SearchFields_KbArticle::UPDATED,
			),
			array(
				SearchFields_KbArticle::TOP_CATEGORY_ID => new DevblocksSearchCriteria(SearchFields_KbArticle::TOP_CATEGORY_ID,'in',array_keys($topics)),
			),
			25,
			0,
			SearchFields_KbArticle::VIEWS,
			false,
			false
		);

		$articles = DAO_KbArticle::getWhere(
			sprintf("%s IN (%s)",
				DAO_KbArticle::ID,
				implode(',', array_keys($results))
			),
			DAO_KbArticle::VIEWS,
			false
		);
		
		foreach($articles as $article) { /* @var $article Model_KbArticle */
			$created = intval($article->updated);
			if(empty($created)) $created = time();

			$eItem = $channel->addChild('item');
			
			$escapedSubject = DevblocksPlatform::strEscapeHtml($article->title);
			//filter out a couple non-UTF-8 characters (0xC and ESC)
			$escapedSubject = mb_convert_encoding($escapedSubject, 'utf-8', LANG_CHARSET_CODE);
			$eItem->addChild('title', $escapedSubject);

			$escapedDesc = DevblocksPlatform::strEscapeHtml($article->getPublicContent());
			$escapedDesc = mb_convert_encoding($escapedDesc, 'utf-8', LANG_CHARSET_CODE);
			$eItem->addChild('description', $escapedDesc);

			$link = $url->write('c=kb&a=article&id='.$article->id, true);
			$eItem->addChild('link', $link);
			
			$eItem->addChild('pubDate', gmdate('D, d M Y H:i:s T', $created));
			
			$eGuid = $eItem->addChild('guid', md5($escapedSubject . $link . $created));
			$eGuid->addAttribute('isPermaLink', "false");
		}

		echo $xml->asXML();
	}
	
	private function _renderRecentChangesRss($portal) {
		DevblocksPlatform::services()->http()->setHeader('Content-Type', 'application/rss+xml');

		$xmlstr = <<<XML
		<rss version='2.0' xmlns:atom='http://www.w3.org/2005/Atom'>
		</rss>
XML;

		$xml = new SimpleXMLElement($xmlstr);
		$url = DevblocksPlatform::services()->url();

		// Portal details
		$portal_name = DAO_CommunityToolProperty::get($portal, UmScApp::PARAM_PAGE_TITLE, '');

		// Channel
		$channel = $xml->addChild('channel');
		$channel->addChild('title', (!empty($portal_name) ? ('['.$portal_name.'] ') : '') . "Recently Updated Articles");
		$channel->addChild('link', $url->write('c=rss&kb=kb&a=recent_changes',true));
		$channel->addChild('description', '');
		
		// Limit topics to portal config
		$topics = DAO_CommunityToolProperty::getJson($portal, UmScKbController::PARAM_KB_ROOTS, '[]');
		if(empty($topics))
			return;
		
		// Search Results
		list($results,) = DAO_KbArticle::search(
			array(
				SearchFields_KbArticle::ID,
				SearchFields_KbArticle::UPDATED,
			),
			array(
				SearchFields_KbArticle::TOP_CATEGORY_ID => new DevblocksSearchCriteria(SearchFields_KbArticle::TOP_CATEGORY_ID,'in',array_keys($topics)),
			),
			25,
			0,
			SearchFields_KbArticle::UPDATED,
			false,
			false
		);

		if(empty($results)) {
			echo $xml->asXML();
			return;
		}
		
		$articles = DAO_KbArticle::getWhere(
			sprintf("%s IN (%s)",
				DAO_KbArticle::ID,
				implode(',', array_keys($results))
			),
			DAO_KbArticle::UPDATED,
			false
		);
		
		foreach($articles as $article) { /* @var $article Model_KbArticle */
			$created = intval($article->updated);
			if(empty($created)) $created = time();

			$eItem = $channel->addChild('item');
			
			$escapedSubject = DevblocksPlatform::strEscapeHtml($article->title);
			//filter out a couple non-UTF-8 characters (0xC and ESC)
			$escapedSubject = mb_convert_encoding($escapedSubject, 'utf-8', LANG_CHARSET_CODE);
			$eItem->addChild('title', $escapedSubject);

			$escapedDesc = DevblocksPlatform::strEscapeHtml($article->getPublicContent());
			$escapedDesc = mb_convert_encoding($escapedDesc, 'utf-8', LANG_CHARSET_CODE);
			$eItem->addChild('description', $escapedDesc);

			$link = $url->write('c=kb&a=article&id='.$article->id, true);
			$eItem->addChild('link', $link);
			
			$eItem->addChild('pubDate', gmdate('D, d M Y H:i:s T', $created));
			
			$eGuid = $eItem->addChild('guid', md5($escapedSubject . $link . $created));
			$eGuid->addAttribute('isPermaLink', "false");
		}
		
		echo $xml->asXML();
	}
	
	private function _renderNewArticlesRss($portal) {
		DevblocksPlatform::services()->http()->setHeader('Content-Type', 'application/rss+xml');

		$xmlstr = <<<XML
		<rss version='2.0' xmlns:atom='http://www.w3.org/2005/Atom'>
		</rss>
XML;

		$xml = new SimpleXMLElement($xmlstr);
		$url = DevblocksPlatform::services()->url();

		// Portal details
		$portal_name = DAO_CommunityToolProperty::get($portal, UmScApp::PARAM_PAGE_TITLE, '');

		// Channel
		$channel = $xml->addChild('channel');
		$channel->addChild('title', (!empty($portal_name) ? ('['.$portal_name.'] ') : '') . "Recently Added Articles");
		$channel->addChild('link', $url->write('c=rss&kb=kb&a=new_articles', true));
		$channel->addChild('description', '');
		
		// Limit topics to portal config
		$topics = DAO_CommunityToolProperty::getJson($portal, UmScKbController::PARAM_KB_ROOTS, '[]');
		if(empty($topics))
			return;
		
		// Search Results
		list($results,) = DAO_KbArticle::search(
			array(
				SearchFields_KbArticle::ID,
				SearchFields_KbArticle::UPDATED,
			),
			array(
				SearchFields_KbArticle::TOP_CATEGORY_ID => new DevblocksSearchCriteria(SearchFields_KbArticle::TOP_CATEGORY_ID,'in',array_keys($topics)),
			),
			25,
			0,
			SearchFields_KbArticle::ID,
			false,
			false
		);

		if(empty($results)) {
			echo $xml->asXML();
			return;
		}
		
		$articles = DAO_KbArticle::getWhere(
			sprintf("%s IN (%s)",
				DAO_KbArticle::ID,
				implode(',', array_keys($results))
			),
			DAO_KbArticle::ID,
			false
		);
		
		foreach($articles as $article) { /* @var $article Model_KbArticle */
			$created = intval($article->updated);
			if(empty($created)) $created = time();

			$eItem = $channel->addChild('item');
			
			$escapedSubject = DevblocksPlatform::strEscapeHtml($article->title);
			//filter out a couple non-UTF-8 characters (0xC and ESC)
			$escapedSubject = mb_convert_encoding($escapedSubject, 'utf-8', LANG_CHARSET_CODE);
			$eItem->addChild('title', $escapedSubject);

			$escapedDesc = DevblocksPlatform::strEscapeHtml($article->getPublicContent());
			$escapedDesc = mb_convert_encoding($escapedDesc, 'utf-8', LANG_CHARSET_CODE);
			$eItem->addChild('description', $escapedDesc);

			$link = $url->write('c=kb&a=article&id='.$article->id, true);
			$eItem->addChild('link', $link);
			
			$eItem->addChild('pubDate', gmdate('D, d M Y H:i:s T', $created));
			
			$eGuid = $eItem->addChild('guid', md5($escapedSubject . $link . $created));
			$eGuid->addAttribute('isPermaLink', "false");
		}

		echo $xml->asXML();
	}
	
	
};