<?php
// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
//
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
// $Id: mod-func-translation.php 66100 2018-04-19 18:00:56Z luciash $

//this script may only be included - so its better to die if called directly.
if (strpos($_SERVER['SCRIPT_NAME'], basename(__FILE__)) !== false) {
	header('location: index.php');
	exit;
}

/**
 * @return array
 */
function module_translation_info()
{
	return [
		'name' => tra('Translate Updates'),
		'description' => tra('Links to versions of the wiki page being viewed in other languages, distinguishing between better, equivalent or worse translations. Optionally displays the up-to-dateness of the translation being viewed.'),
		'prefs' => ['feature_multilingual'],
		'params' => [
			'pivot_language' => [
				'name' => tra('Reference language'),
				'description' => tra('If set to a language code, restricts the localized pages shown to the reference page, unless that page is being displayed.') . ' ' . tra('Example values:') . ' en, fr.' . ' ' . tra('Not set by default.')
			],
			'show_language' => [
				'name' => tra('Show language'),
				'description' => tra('If "y" the page language will be shown instead of the page name.') . tra('Default = "y".')
			]
		]
	];
}

// Filter localized pages according to the reference language
/**
 * @param $langInfo
 * @return bool
 */
function filter_languages_from_pivot($langInfo)
{
	global $pivotLanguage;
	global $pageLang;

	return empty($pivotLanguage)
		|| $pageLang == $pivotLanguage
		|| $langInfo['lang'] == $pivotLanguage;
}

/**
 * @param $mod_reference
 * @param $module_params
 */
function module_translation($mod_reference, $module_params)
{
	global $pivotLanguage, $prefs, $page;
	$smarty = TikiLib::lib('smarty');
	$tikilib = TikiLib::lib('tiki');

	//are we arriving from the edit page?
	if (isset($module_params['from_edit_page']) && $module_params['from_edit_page'] == 'y') {
		$smarty->assign('from_edit_page', 'y');
	} else {
		$smarty->assign('from_edit_page', 'n');
	}

	if ((! $page or $page == '') and isset($_REQUEST['page'])) {
		$page = $_REQUEST['page'];
	}
	$smarty->assign('page', $page);
	$smarty->assign('show_translation_module', false);

	if (! empty($page) && is_string($page)) {
		$multilinguallib = TikiLib::lib('multilingual');

		if (isset($module_params['show_language']) && $module_params['show_language'] == 'n') {
			$smarty->assign('show_language', 'n');
		} else {
			$smarty->assign('show_language', 'y');
		}

		$pivotLanguage = isset($module_params['pivot_language']) ? $module_params['pivot_language'] : '';
		$langs = $multilinguallib->preferredLangs();
		if (isset($GLOBALS['pageLang'])) {
			$pageLang = $GLOBALS['pageLang'];
		} else {
			$pageLang = '';
		}

		$transinfo = $tikilib->get_page_info($page);
		if (empty($transinfo)) {
			return;
		}

		$tempList = $multilinguallib->getTranslations('wiki page', $transinfo['page_id']);
		$completeList = [];
		foreach ($tempList as $row) {
			$t_id = $row['objId'];
			$t_page = $row['objName'];
			$t_lang = $row['lang'];
			$completeList[$t_id] = ['page' => $t_page, 'lang' => $t_lang];
		}

		unset($completeList[$transinfo['page_id']]);

		$smarty->assign('show_translation_module', ! empty($completeList));
		if (empty($completeList)) {
			return;
		}

		$origBetter = $better = $multilinguallib->getBetterPages($transinfo['page_id']);
		$better = array_filter($better, 'filter_languages_from_pivot');
		$known = [];
		$other = [];

		foreach ($better as $pageOption) {
			if (in_array($pageOption['lang'], $langs)) {
				$known[] = $pageOption;
			} else {
				$other[] = $pageOption;
			}
		}

		$smarty->assign('mod_translation_better_known', $known);
		$smarty->assign('mod_translation_better_other', $other);

		$origWorst = $worst = $multilinguallib->getWorstPages($transinfo['page_id']);
		$worst = array_filter($worst, 'filter_languages_from_pivot');
		$known = [];
		$other = [];

		foreach ($worst as $pageOption) {
			if (in_array($pageOption['lang'], $langs)) {
				$known[] = $pageOption;
			} else {
				$other[] = $pageOption;
			}
		}

		$smarty->assign('mod_translation_worst_known', $known);
		$smarty->assign('mod_translation_worst_other', $other);
		$smarty->assign('pageVersion', $transinfo['version']);

		foreach ($origBetter as $row) {
			$id = $row['page_id'];
			unset($completeList[$id]);
		}

		foreach ($origWorst as $row) {
			$id = $row['page_id'];
			unset($completeList[$id]);
		}

		$known = [];
		$other = [];
		foreach ($completeList as $pageOption) {
			if (in_array($pageOption['lang'], $langs)) {
				$known[] = $pageOption;
			} else {
				$other[] = $pageOption;
			}
		}

		$smarty->assign('mod_translation_equivalent_known', $known);
		$smarty->assign('mod_translation_equivalent_other', $other);

		if ($prefs['quantify_changes'] == 'y') {
			$quantifylib = TikiLib::lib('quantify');
			include_once 'lib/wiki-plugins/wikiplugin_gauge.php';
			$numeric = $quantifylib->getCompleteness($transinfo['page_id']);
			$smarty->assign('mod_translation_quantification', $numeric);
			$smarty->assign(
				'mod_translation_gauge',
				wikiplugin_gauge(
					'',
					[
							'value' => $numeric,
							'max' => 100,
							'size' => '100%',
							'color' => 'green',
							'bgcolor' => 'gray',
							'showvalue' => false,
					]
				)
			);
		}
	}
}
