<?php
/**
 * EGroupware - Filemanager - UI for sharing with anonymous user
 *
 * @link http://www.egroupware.org
 * @package filemanager
 * @author Nathan Gray
 * @copyright (c) 2020 Nathan Gray
 * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
 */
namespace EGroupware\Filemanager\Sharing;

use EGroupware\Api\Etemplate\Widget\Nextmatch;
use \filemanager_ui;
use EGroupware\Api;
use EGroupware\Api\Vfs;

/**
 * This is the file list for an anonymous user.
 * Logged in users may also end up here, but they will normally just use filemanager_ui
 */
class AnonymousList extends filemanager_ui
{
	/**
	 * Constructor
	 *
	 * Reimplemented to load filemanager translations
	 */
	function __construct()
	{
		parent::__construct();

		Api\Translation::add_app('filemanager');
	}

	/**
	 * Get active view - override so it points to this class
	 *
	 * @return callable
	 */
	public static function get_view()
	{
		return array(new AnonymousList(), 'listview');
	}

	/**
	 * Filemanager listview
	 *
	 * @param array $content
	 * @param string $msg
	 */
	function listview(array $content = null, $msg = null)
	{
		Api\Translation::add_app("filemanager");

		$this->etemplate = $this->etemplate ? $this->etemplate : new Api\Etemplate(static::LIST_TEMPLATE);

		// Override and take over get_rows so we can customize
		$content['nm']['get_rows'] = '.' . get_class($this) . '.get_rows';
		$content['show_refresh'] = true;
		$content['nm']['no_filter'] = true;
		$content['nm']['favorites'] = false;
		$content['nm']['num_rows'] = Nextmatch::INITIAL_ROWS;

		$logo = !empty($GLOBALS['egw_info']['server']['login_logo_header']) ? Api\Framework::get_login_logo_or_bg_url('login_logo_header', 'logo')
			: Api\Framework::get_login_logo_or_bg_url('login_logo_file', 'logo');
		if($logo)
		{
			$this->etemplate->setElementAttribute("toolbar[logo]", "src", $logo);
		}
		if(!Vfs::is_writable($content['nm']['path']))//read-only share
		{
			$hidden=["[upload]","[new]","[button][edit]","[button][createdir]","[button][symlink]","[file_a_file]"];//new already gets hidden
			// share is read-only, we can just hide everything
			//even in read-only we do not want to hide everything
			$this->hideToolbarParts($hidden);
		}
		else //if(writable share)
		{
			$hidden=["[button][edit]","[button][symlink]","[file_a_file]"];
			// Set some unwanted buttons readonly - hide via CSS
			$this->hideToolbarParts($hidden);
		}
		//else if(hidden upload)
//		always make reload available
		$this->etemplate->setElementAttribute("toolbar[reload]","disabled", false);
		// No egw-app, no main-header, no filterbox
		$this->etemplate->setElementAttribute("toolbar[search]", "onchange", "window.app.filemanager.changeNmFilter(null, widget); return false;");
		$this->etemplate->setElementAttribute("nm", "filter_template", "");

		// No framework, no slots available
		$this->etemplate->setElementAttribute("filemanager.index.app-toolbar", "slot", "");

		// Anonymous view doesn't always get push, so refresh after upload
		$this->etemplate->setElementAttribute("toolbar[upload]", "onchange", 'ev.target.set_value(""); egw.refresh("","filemanager");');

		return parent::listview($content, $msg);
	}

	/**
	 * Get the configured start directory for the current user
	 *
	 * @return string
	 */
	static function get_home_dir()
	{
		if(isset($GLOBALS['egw']->sharing) && array_key_exists(Vfs\Sharing::get_token(), $GLOBALS['egw']->sharing))
		{
			return $GLOBALS['egw']->sharing[Vfs\Sharing::get_token()]->get_root();
		}
		return '/';
	}

	/**
	 * Context menu
	 *
	 * @return array
	 */
	public static function get_actions()
	{
		$actions = parent::get_actions();
		$group = 1;
		// do not add edit setting action when we are in sharing
		unset($actions['edit']);
		if (isset($GLOBALS['egw']->sharing) && array_key_exists(Vfs\Sharing::get_token(), $GLOBALS['egw']->sharing) &&
				Vfs::is_writable($GLOBALS['egw']->sharing[Vfs\Sharing::get_token()]->get_root()))
		{
			return $actions;
		}
		$actions += array(
				'egw_copy' => array(
						'enabled' => false,
						'group' => $group + 0.5,
						'hideOnDisabled' => true
				),
				'egw_copy_add' => array(
						'enabled' => false,
						'group' => $group + 0.5,
						'hideOnDisabled' => true
				),
				'paste' => array(
						'enabled' => false,
						'group' => $group + 0.5,
						'hideOnDisabled' => true
				),
		);
		return $actions;
	}

	protected function get_vfs_options($query)
	{
		$options = parent::get_vfs_options($query);

		// Hide symlinks
		// TODO: This hides everything, see Vfs::_check_add() line 648
		//$options['type'] = '!l';

		return $options;
	}

	/**
	 * Callback to fetch the rows for the nextmatch widget
	 *
	 * @param array $query
	 * @param array &$rows
	 * @return int
	 */
	function get_rows(&$query, &$rows)
	{
		// We turned off filter but still need to use the value
		$query['filter'] = '';

		// Check for navigating outside share, redirect back to share
		if(!empty($query['col_filter']['dir']) && (!Vfs::stat($query['col_filter']['dir'], false) || !Vfs::is_dir($query['col_filter']['dir']) || !Vfs::check_access($query['col_filter']['dir'], Vfs::READABLE)))
		{
			// only redirect, if it would be to some other location, gives redirect-loop otherwise
			if($query['col_filter']['dir'] != ($path = static::get_home_dir()))
			{
				// we will leave here, since we are not allowed, go back to root
				// TODO: Give message about it, redirect to home dir
			}
			$rows = array();
			return 0;
		}

		// Get file list from parent
		$total = parent::get_rows($query, $rows);

		return $total;
	}
	private function hideToolbarParts($parts)
	{
		foreach ($parts as $part){
			if($this->etemplate->getElementAttribute("toolbar" . $part, "hidden") === null)
			{
				$this->etemplate->setElementAttribute("toolbar" . $part, "hidden", true);
			}
		}
	}
}