<?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 vB_Channel
{
	const DEFAULT_CHANNEL_PARENT = 'vbulletin-4ecbdf567f3341.44451100';
	const DEFAULT_BLOG_PARENT = 'vbulletin-4ecbdf567f3a38.99555305';
	const DEFAULT_SOCIALGROUP_PARENT = 'vbulletin-4ecbdf567f3a38.99555306';
	const DEFAULT_UNCATEGORIZEDGROUPS_PARENT = 'vbulletin-4ecbdf567f3a38.99555307';
	const PRIVATEMESSAGE_CHANNEL = 'vbulletin-4ecbdf567f3da8.31769341';
	const VISITORMESSAGE_CHANNEL = 'vbulletin-4ecbdf567f36c3.90966558';
	const ALBUM_CHANNEL = 'vbulletin-4ecbdf567f3a38.99555303';
	const REPORT_CHANNEL = 'vbulletin-4ecbdf567f3a38.99555304';

	/**
	 * Returns an array of candidates for blog channel parents.
	 * @return array
	 */
	public static function getBlogChannelParents()
	{
		$result = array();

		// TODO: is there any special condition to be a blog channel parent?
		$channels = vB::getDbAssertor()->assertQuery('vBForum:getChannelInfoExport');

		foreach ($channels AS $channel)
		{
			if ($channel['guid'] == self::DEFAULT_BLOG_PARENT)
			{
				$result = array($channel['nodeid'] => $channel['title']) + $result;
			}
			else
			{
				$result[$channel['nodeid']] = $channel['title'];
			}
		}

		return $result;
	}

	/**
	 * Moves all blog channels from the old blog channel parent to the new one.
	 * @param int $oldChannelId
	 * @param int $newChannelId
	 */
	public static function moveBlogChannels($oldChannelId, $newChannelId)
	{
		if (empty($oldChannelId))
		{
			$oldChannelId = vB::getDbAssertor()->getField('channel', array(vB_dB_Query::TYPE_KEY => vB_dB_Query::QUERY_SELECT, 'guid' => self::DEFAULT_BLOG_PARENT));
		}

		$children = vB::getDbAssertor()->assertQuery('closure', array(
			vB_dB_Query::TYPE_KEY => vB_dB_Query::QUERY_SELECT,
			'parent' => $oldChannelId,
			'depth' => 1
		));

		$childrenIds = array();
		foreach ($children AS $child)
		{
			$childrenIds[] = $child['child'];
		}
		if (!empty($childrenIds))
		{
			vB_Api::instanceInternal('node')->moveNodes($childrenIds, $newChannelId);
		}
	}
}

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