<?php

/* ======================================================================*\
  || #################################################################### ||
  || # vBulletin 5.0.0 Beta 10
  || # ---------------------------------------------------------------- # ||
  || # Copyright �2000-2012 vBulletin Solutions Inc. All Rights Reserved. ||
  || # This file may not be redistributed in whole or significant part. # ||
  || # ---------------- VBULLETIN IS NOT FREE SOFTWARE ---------------- # ||
  || # http://www.vbulletin.com | http://www.vbulletin.com/license.html # ||
  || #################################################################### ||
  \*====================================================================== */

class vB5_Route_Channel extends vB5_Route
{
	public function __construct($routeInfo, $matches, $queryString = '')
	{
		parent::__construct($routeInfo, $matches, $queryString);
		
		if (isset($this->arguments['channelid']))
		{
			// check if we need to force a styleid
			$channel = vB_Library::instance('Content_Channel')->getContent($this->arguments['channelid']);
			if (is_array($channel))
			{
				$channel = array_pop($channel);
			}
			
			if (!empty($channel['styleid']))
			{
				$forumOptions = vB::getDatastore()->getValue('bf_misc_forumoptions');
				if($forumOptions['styleoverride'] & $channel['options'])
				{
					// the channel must force the style
					$this->arguments['forceStyleId'] = $channel['styleid'];
				}
				else
				{
					// the channel suggests to use this style
					$this->arguments['routeStyleId'] = $channel['styleid'];
				}
			}
			
			// styleid for channels are not final at this point, so let's not include them in the key
			$this->setPageKey('pageid', 'channelid');
			
			// set user action
			$this->setUserAction('viewing_forum_x', $channel['title'], $this->getFullUrl('nosession'));
			
			// remove link from last crumb
			$this->breadcrumbs[(count($this->breadcrumbs) - 1)]['url'] = '';
		}
	}

	public static function createPrefix($title, $parentId)
	{
		$channelApi = vB_Api::instanceInternal('content_channel');
		$parentChannel = $channelApi->fetchChannelById($parentId);
		$parentUrl = self::buildUrl("{$parentChannel['routeid']}|nosession");

		if ($parentUrl AND $parentUrl{0} == '/')
		{
			$parentUrl = substr($parentUrl, 1);
		}

		$url = '';
		if (!empty($parentUrl))
		{
			$url .= $parentUrl . '/';
		}
		$url .= self::prepareTitle($title);

		return $url;
	}

	protected static function validInput(array &$data)
	{
		if (isset($data['channelid']))
		{
			$data['nodeid'] = $data['channelid'];
			unset($data['channelid']);
		}

		if (
				!isset($data['nodeid']) OR !is_numeric($data['nodeid']) OR
				!isset($data['pageid']) OR !is_numeric($data['pageid']) // this is used for rendering the page
		)
		{
			return FALSE;
		}
		$data['nodeid'] = intval($data['nodeid']);
		$data['pageid'] = intval($data['pageid']);

		if (!isset($data['prefix']))
		{
			$channelApi = vB_Api::instanceInternal('content_channel');
			$newChannel = $channelApi->fetchChannelById($data['nodeid']);

			//use urlident if we have it.
			if (empty($data['urlident']))
			{
				$url = self::createPrefix($newChannel['title'], $newChannel['parentid']);
			}
			else
			{
				$url = self::createPrefix($newChannel['urlident'], $newChannel['parentid']);
			}
			$data['prefix'] = $data['regex'] = $url;
		}
		else
		{
			$data['regex'] = $data['prefix'];
		}

		$data['class'] = __CLASS__;
		$data['controller'] = 'page';
		$data['action'] = 'index';
		$data['arguments'] = serialize(array('channelid' => $data['nodeid'], 'nodeid' => $data['nodeid'], 'pageid' => $data['pageid']));
		// this field will be used to delete the route when deleting the channel (contains channel id)
		$data['contentid'] = $data['nodeid'];

		unset($data['nodeid']);
		unset($data['pageid']);

		return parent::validInput($data);
	}

	protected static function updateContentRoute($oldRouteInfo, $newRouteInfo)
	{
		$db = vB::getDbAssertor();

		$db->assertQuery('update_route_301', array('newrouteid' => $newRouteInfo['routeid'], 'oldrouteid' => $oldRouteInfo['routeid']));

		// update routeid in nodes table
		$db->update('node', array('routeid' => $newRouteInfo['routeid']), array('routeid' => $oldRouteInfo['routeid']));
		// don't modify the routeid for default pages, as it will still be used
		$db->update('page', array('routeid' => $newRouteInfo['routeid']), array('routeid' => $oldRouteInfo['routeid'], 'pagetype' => vB_Page::TYPE_CUSTOM));

		// update conversation generic route
		$routes = $db->getRows('routenew', array(
					vB_dB_Query::TYPE_KEY => vB_dB_Query::QUERY_SELECT,
					vB_dB_Query::CONDITIONS_KEY => array(
						array('field'=>'class',		'value'=>'vB5_Route_Conversation'),
						array('field'=>'prefix',	'value'=>$oldRouteInfo['prefix']),
						array('field'=>'contentid',	'value'=>$oldRouteInfo['channelid']),
						array('field'=>'redirect301', 'operator' => vB_dB_Query::OPERATOR_ISNULL)
					)
				));
		foreach ($routes AS $route)
		{
			$db->update(
				'routenew',
				array(
					'prefix'	=> $newRouteInfo['prefix'],
					'regex'		=> str_replace($oldRouteInfo['prefix'], $newRouteInfo['prefix'], $route['regex'])
				),
				array('routeid' => $route['routeid'])
			);
		}
	}

	public function getUrl()
	{
		return '/' . $this->prefix;
	}

	public function getCanonicalRoute()
	{
		if (!isset($this->canonicalRoute))
		{
			if (empty($this->arguments['channelid']))
			{
				return array();
			}
			$node = vB::getDbAssertor()->getRow('node', array('nodeid'=>$this->arguments['channelid']));
			$this->canonicalRoute = self::getRoute($node['routeid']);
		}

		return $this->canonicalRoute;
	}

	public static function exportArguments($arguments)
	{
		$data = unserialize($arguments);

		$channel = vB::getDbAssertor()->getRow('channel', array('nodeid' => $data['channelid']));
		if (empty($channel))
		{
			throw new Exception('Couldn\'t find channel');
		}
		$data['channelGuid'] = $channel['guid'];
		unset($data['channelid']);

		$page = vB::getDbAssertor()->getRow('page', array('pageid' => $data['pageid']));
		if (empty($page))
		{
			throw new Exception('Couldn\'t find page');
		}
		$data['pageGuid'] = $page['guid'];
		unset($data['pageid']);

		return serialize($data);
	}

	public static function importArguments($arguments)
	{
		$data = unserialize($arguments);

		$channel = vB::getDbAssertor()->getRow('channel', array('guid' => $data['channelGuid']));
		if (empty($channel))
		{
			throw new Exception('Couldn\'t find channel');
		}
		$data['channelid'] = $channel['nodeid'];
		unset($data['channelGuid']);

		$page = vB::getDbAssertor()->getRow('page', array('guid' => $data['pageGuid']));
		if (empty($page))
		{
			throw new Exception('Couldn\'t find page');
		}
		$data['pageid'] = $page['pageid'];
		unset($data['pageGuid']);

		return serialize($data);
	}

	public static function importContentId($arguments)
	{
		return $arguments['channelid'];
	}
}

/*======================================================================*\
|| ####################################################################
|| # 
|| # CVS: $RCSfile$ - $Revision: 40911 $
|| ####################################################################
\*======================================================================*/
