<?php
// +-------------------------------------------------------------+
// | HiveMail version 1.3.1
// +-------------------------------------------------------------+
// | HIVEMAIL IS NOT FREE SOFTWARE
// | If you have downloaded this software from a website other
// | than Hivemail or if you have otherwise received
// | this software from someone who is not a representative of
// | this organization you are involved in an illegal activity.
// +-------------------------------------------------------------+
// | $RCSfile: language.php,v $ - $Revision: 1.1 $
// | $Date: 2004/08/05 02:11:10 $ - $Author: tubedogg $
// +-------------------------------------------------------------+

error_reporting(E_ALL & ~E_NOTICE);
require_once('./global.php');
if ($_POST['cmd'] != 'serve') {
	cp_header(' &raquo; Languages');
}

// ############################################################################
// Set the default cmd
default_var($cmd, 'modify');
if ($cmd == 'add') {
	$cmd = 'edit';
}

// ############################################################################
// Download language
if ($_POST['cmd'] == 'serve') {
	$alanguage = getinfo('language', $alanguageid, false, false);
	if ($alanguage === false) {
		adminlog($alanguageid, false);
		cp_header();
		cp_nav('langdownup');
		cp_error('Invalid language specified.');
	}

	adminlog($alanguageid, true);
	header('Content-disposition: attachment; filename=hivemail_language_'.date('m_d_Y').'.lang');
	header('Content-type: unknown/unknown');
	$aphrases = array();
	while ($language = $DB_site->fetch_array($getlanguages, "SELECT * FROM hive_phrase WHERE languageid = $alanguage[languageid] ORDER BY variable")) {
		$aphrases["$language[variable]"] = array('value' => $language['value'], 'phraseareaid' => $language['phraseareaid']);
	}

	foreach ($alanguage as $key => $val) {
		if (is_numeric($key) or $key == 'languageid' or $key == 'phrases') {
			unset($alanguage["$key"]);
		}
	}
	$alanguage = array('settings' => $alanguage, 'phrases' => $aphrases);
	echo preg_replace("#\r?\n#", "\n", export_array($alanguage));
	exit;
}

// ############################################################################
// Upload language
if ($_POST['cmd'] == 'upload') {
	cp_nav('langdownup');
	if (!upload_file($filename, $filedata)) {
		adminlog(0, false);
		cp_error('There has been error while trying to upload the file.<br />Please go back and try again.');
	}

	$filedata = str_replace('\\\\\\\'', '\\\\\'', $filedata);
	$filedata = str_replace('\\\\\'', '\\\\\\\'', $filedata);
	eval('$alanguage = '.$filedata.';');
	
	$DB_site->query("
		INSERT INTO hive_language
		(languageid, title, phrases, userselectable, textdir, langcode, htmlcharset, emailcharset, dateformat, timeformat, cal_use24)
		VALUES (NULL, '".addslashes($newname)."', '".addslashes(export_array($alanguage['settings']['phrases']))."', ".$alanguage['settings']['userselectable'].", '".addslashes($alanguage['settings']['textdir'])."', '".addslashes($alanguage['settings']['langcode'])."', '".addslashes($alanguage['settings']['htmlcharset'])."', '".addslashes($alanguage['settings']['emailcharset'])."', '".addslashes($alanguage['settings']['dateformat'])."', '".addslashes($alanguage['settings']['timeformat'])."', '".addslashes($alanguage['settings']['cal_use24'])."')
	");
	$languageid = $DB_site->insert_id();

	foreach ($alanguage['phrases'] as $var => $info) {
		$values .= "\n(NULL, $info[phraseareaid], $languageid, '".addslashes($var)."', '".addslashes($info['value'])."'),";
	}
	$values = substr($values, 0, -1);
	$DB_site->query("
		INSERT INTO hive_phrase
		(phraseid, phraseareaid, languageid, variable, value)
		VALUES$values
	");
	
	adminlog($alanguageid, true);
	cp_redirect('The language has been created.', "language.php");
}

// ############################################################################
// Import/export language
if ($cmd == 'downup') {
	cp_nav('langdownup');
	adminlog();

	startform('language.php', 'serve');
	starttable('Export Language');
	textrow('Use this to download a file of the select language and save it to your computer.<br />This file can be later used to import the language back into the system.');
	tableselect('Language to download:', 'alanguageid', 'language');
	endform('Export Language');
	endtable();

	startform('language.php', 'upload', '', array(), true);
	starttable('Import Language');
	textrow('Use this form to upload a language file and create a new language based on it.');
	filefield('Language file:<br /><span class="cp_small">Click Browse and select the backup file from your computer. When you are done, click Open.</span>', 'file');
	inputfield('Name of new language:<br /><span class="cp_small">The new language that will be created from this file will have this name.</span>', 'newname');
	endform('Import Language');
	endtable();
}

// ############################################################################
// Remove language
if ($_POST['cmd'] == 'kill') {
	cp_nav('langmodify');
	$alanguage = getinfo('language', $alanguageid);
	if ($alanguageid == 1) {
		adminlog($alanguageid, false);
		cp_error('You cannot remove the original language.');
	}

	$DB_site->query("
		DELETE FROM hive_language
		WHERE languageid = $alanguageid
	");
	$DB_site->query("
		DELETE FROM hive_phrase
		WHERE languageid = $alanguageid
	");

	adminlog($alanguageid, true, 'kill');
	cp_redirect('The language has been removed.', 'language.php');
}

// ############################################################################
// Remove language
if ($cmd == 'remove') {
	cp_nav('langmodify');
	$alanguage = getinfo('language', $alanguageid);
	if ($alanguageid == 1) {
		adminlog($alanguageid, false);
		cp_error('You cannot remove the original language.');
	}

	adminlog($alanguageid);
	startform('language.php', 'kill', 'Are you sure you want to remove this language?');
	starttable('Remove language "'.$alanguage['title'].'" (ID: '.$alanguageid.')');
	textrow('Are you <b>sure</b> you want to remove this language? This procedure <b>cannot</b> be reversed!');
	hiddenfield('alanguageid', $alanguageid);
	endform('Remove Language', '', 'Go Back');
	endtable();
}

// ############################################################################
// Create a new language or update an existing one
if ($_POST['cmd'] == 'update') {
	cp_nav('langmodify');
	if (empty($alanguage['title'])) {
		adminlog($alanguageid, false);
		cp_error('The language must have a title.');
	} else {
		if ($alanguageid == 0) {
			// Create the language
			$DB_site->auto_query('language', $alanguage);
			$alanguageid = $DB_site->insert_id();

			adminlog($alanguageid, true);
			cp_redirect('The language has been created.', 'language.php');
		} else {
			$DB_site->auto_query('language', $alanguage, "languageid = $alanguageid");
			adminlog($alanguageid, true);
			cp_redirect('The language has been updated.', 'language.php');
		}
	}
}

// ############################################################################
// Create a new language or update an existing one
if ($cmd == 'edit') {
	cp_nav('langmodify');
	startform('language.php', 'update', '', array('alanguage_title' => 'title', 'alanguage_htmlcharset' => 'HTML chararacter set', 'alanguage_emailcharset' => 'email character set', 'alanguage_langcode' => 'language code'));

	$alanguage = getinfo('language', $alanguageid, false, false);
	adminlog($alanguageid);
	if ($alanguage === false) {
		$alanguage = array(
			'title' => '',
			'userselectable' => 1,
			'textdir' => 'ltr',
			'langcode' => 'en',
			'htmlcharset' => 'ISO-8859-1',
			'emailcharset' => 'iso-8859-1',
			'dateformat' => '',
			'timeformat' => '',
			'cal_use24' => getop('cal_use24'),
		);
		$alanguageid = 0;
		starttable('Create New Language');
	} else {
		starttable('Update Language "'.$alanguage['title'].'" (ID: '.$alanguageid.')');
	}

	hiddenfield('alanguageid', $alanguageid);
	inputfield('<b>Title</b>:', 'alanguage[title]', $alanguage['title']);
	radiofield('<b>User Selectable</b>:', 'alanguage[userselectable]', $alanguage['userselectable']);
	radiofield('<b>Text Direction</b>:', 'alanguage[textdir]', $alanguage['textdir'], array('ltr' => 'Left to Right', 'rtl' => 'Right to Left'));
	inputfield('<b>Language Code</b>:<br /><span class="cp_small">Enter the abbreviation for this language. You can find a list of abbreviations <a href="http://lcweb.loc.gov/standards/iso639-2/englangn.html" target="_blank">here</a>.</span>', 'alanguage[langcode]', $alanguage['langcode']);
	inputfield('<b>HTML Character Set</b>:<br /><span class="cp_small">The character set used in the Content Type meta tag (in the CSS template).</span>', 'alanguage[htmlcharset]', $alanguage['htmlcharset']);
	inputfield('<b>Email Character Set</b>:<br /><span class="cp_small">The character set used in the Content-Type header when sending email.</span>', 'alanguage[emailcharset]', $alanguage['emailcharset']);

	if ($alanguageid == 0) {
		emptyrow(2);
		endtable();
	} else {
		tablehead(array('<input class="button" type="submit" value="  Update Language  " />&nbsp;<input class="button" type="reset" value="  Reset Fields  " />'), 2);
		endtable();
	}

	starttable('Date and Time Options');
	inputfield('<b>Date format override</b>:<br /><span class="cp_small">Format for displaying the date; this will override the default setting.<br />For example: "F j, Y" will display July 1, 2002. "m-d-Y" will display 07-01-2002.<br />The syntax is identical to that of the PHP <a href="http://www.php.net/date" target="_blank">date function</a>.</span>', 'alanguage[dateformat]', $alanguage['dateformat']);
	inputfield('<b>Time format override</b>:<br /><span class="cp_small">Format for displaying the time; this will override the default setting.<br />For example: "H:i" will display 15:49. "h:i A" will display 03:49 PM.<br />The syntax is identical to that of the PHP <a href="http://www.php.net/date" target="_blank">date function</a>.</span>', 'alanguage[timeformat]', $alanguage['timeformat']);
	radiofield('<b>Use 24-hour format</b>:<br /><span class="cp_small">Enable this to use 24-hour format for calendar events; this will override the default setting.</span>', 'alanguage[cal_use24]', $alanguage['cal_use24']);

	if ($alanguageid == 0) {
		endform('Create Language');
	} else {
		endform('Update Language', 'Reset Fields');
	}
	endtable();
}

// ############################################################################
// List the languages
if ($cmd == 'modify') {
	cp_nav('langmodify');
	adminlog();
	$cells = array(
		'ID',
		'Title',
		'Used By',
		'Options'
	);
	$alanguages = $DB_site->query('
		SELECT language.*, COUNT(user.userid) AS users
		FROM hive_language AS language
		LEFT JOIN hive_user AS user USING (languageid)
		GROUP BY language.languageid
	');
	if ($DB_site->num_rows($alanguages) < 1) {
		starttable('Error', '450');
		tablehead($cells);
		textrow('No languages found', count($cells), 1);
	} else {
		starttable('Existing Languages', '450');
		tablehead($cells);
		while ($alanguage = $DB_site->fetch_array($alanguages)) {
			$cells = array(
				$alanguage['languageid'],
				"<a href=\"language.php?cmd=edit&alanguageid=$alanguage[languageid]\">$alanguage[title]</a>",
				iif($alanguage['users'] > 0, "<a href=\"user.php?cmd=results&find[elanguageid]=$alanguage[languageid]\">$alanguage[users] user".iif($alanguage['users'] != 1, 's').'</a>', "$alanguage[users] users"),
				makelink('view', "../".INDEX_FILE."?languageid=$alanguage[languageid]", 1) . '-' . makelink('edit', "language.php?cmd=edit&alanguageid=$alanguage[languageid]") . '-' . makelink('mass translate', "phrase.php?cmd=massmodify&languageid=$alanguage[languageid]") . iif($alanguage['languageid'] != 1, '-' . makelink('remove', "language.php?cmd=remove&alanguageid=$alanguage[languageid]"))
			);
			tablerow($cells);
		}
	}
	emptyrow(count($cells));
	endtable();

	button('', 'Create New Language', "window.location='language.php?cmd=edit'", true);

	starttable('Help', '450');
	textrow('The language system in HiveMail&trade; allows you to translate phrases used in the user side into other languages. If a phrase is not translated, the default (English) version of it will be used.<br /><br />
	Currently, in order to completely translate the user side of HiveMail&trade;, in addition to translating the phrases in the language system, you must translate text directly in the templates. In a future version, all text will be moved to the language system and templates will be language-independent, allowing you to use multiple languages with a single skin and template set.<br /><br />
	You can modify a language\'s settings (such as date and time format overrides, character set, etc.) by clicking [edit] across from the language name above. To translate phrases for a language, click [phrases]. If you wish to remove a language, click [remove]. (We recommend you make a backup of the language first, by downloading a copy of it.)<br /><br />
	The original language (ID: 1) cannot be removed.');
	endtable();
}

cp_footer();
?>