<?php
/**
 * @brief		Front Navigation Extension: Your Activity Streams
 *
 * @copyright	(c) 2001 - SVN_YYYY Invision Power Services, Inc.
 *
 * @package		IPS Social Suite
 * @since		30 Jun 2015
 * @version		SVN_VERSION_NUMBER
 */

namespace IPS\core\extensions\core\FrontNavigation;

/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
	header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
	exit;
}

/**
 * Front Navigation Extension: Your Activity Streams
 */
class _YourActivityStreams extends \IPS\core\FrontNavigation\FrontNavigationAbstract
{
	/**
	 * Get Type Title which will display in the AdminCP Menu Manager
	 *
	 * @return	string
	 */
	public static function typeTitle()
	{
		return \IPS\Member::loggedIn()->language()->addToStack('your_activity_streams_acp');
	}
		
	/**
	 * Can access?
	 *
	 * @return	bool
	 */
	public function canView()
	{
		return parent::canView() and \IPS\Member::loggedIn()->canAccessModule( \IPS\Application\Module::get( 'core', 'discover' ) ) and count( $this->children() );
	}
	
	/**
	 * Get Title
	 *
	 * @return	string
	 */
	public function title()
	{
		return \IPS\Member::loggedIn()->language()->addToStack('your_activity_streams');
	}
	
	/**
	 * Get Link
	 *
	 * @return	\IPS\Http\Url
	 */
	public function link()
	{
		return NULL;
	}
	
	/**
	 * Is Active?
	 *
	 * @return	bool
	 */
	public function active()
	{
		return ( \IPS\Dispatcher::i()->application->directory === 'core' and \IPS\Dispatcher::i()->module->key === 'discover' and ( isset( \IPS\Request::i()->id ) or ( isset( \IPS\Request::i()->do ) and \IPS\Request::i()->do == 'create' ) ) ) or parent::active();
	}
	
	/**
	 * Cached Children
	 *
	 * @return	array
	 */
	protected static $children = array();
	
	/**
	 * Items
	 *
	 * @return	array
	 */
	public static function items( \IPS\Member $member = NULL )
	{	
		$member = $member ?: \IPS\Member::loggedIn();
		
		if ( !isset( static::$children[ $member->member_id ] ) )
		{
			static::$children[ $member->member_id ] = array();
			
			if ( !isset( \IPS\Data\Store::i()->globalStreamIds ) )
			{
				$globalStreamIds = array();
				foreach ( new \IPS\Patterns\ActiveRecordIterator( \IPS\Db::i()->select( '*', 'core_streams', 'member IS NULL' ), 'IPS\core\Stream' ) as $stream )
				{
					$globalStreamIds[ $stream->id ] = ( $stream->ownership == 'all' and $stream->read == 'all' and $stream->follow == 'all' and $stream->date_type != 'last_visit' );
				}
				
				\IPS\Data\Store::i()->globalStreamIds = $globalStreamIds;
			}
					
			$globalStreamIdsToShow = array_keys( !$member->member_id ? array_filter( \IPS\Data\Store::i()->globalStreamIds ) : \IPS\Data\Store::i()->globalStreamIds );
			
			if ( count( $globalStreamIdsToShow ) )
			{
				if ( $member->member_id )
				{
					static::$children[ $member->member_id ][] = new MenuHeader('default_streams');
				}
				
				foreach ( $globalStreamIdsToShow as $id )
				{
					static::$children[ $member->member_id ][] = new YourActivityStreamsItem( array(), $id, '*' );
				}
			}
			
			if ( $member->member_id )
			{
				if ( $member->member_streams and $streams = json_decode( $member->member_streams, TRUE ) and count( $streams ) )
				{
					static::$children[ $member->member_id ][] = new MenuHeader('custom_streams');
					foreach ( $streams['streams'] as $id => $title )
					{
						static::$children[ $member->member_id ][] = new YourActivityStreamsItem( array( 'title' => $title ), $id, '*' );
					}
				}
				
				static::$children[ $member->member_id ][] = new MenuSeparator;
				static::$children[ $member->member_id ][] = new MenuButton( 'create_new_stream', \IPS\Http\Url::internal( "app=core&module=discover&controller=streams&do=create", 'front', 'discover_all' ) );
			}
		}
		
		return static::$children[ $member->member_id ];
	}
	
	/**
	 * Children
	 *
	 * @return	array
	 */
	public function children()
	{	
		return static::items();		
	}
}