<?php

define('RENDER_MODE', 'catalyz');

$node = $page->getNode();

use_helper('Form');

$properties = array();
$properties['page'] = $page;
$properties['node'] = $node;
//$properties['culture'] = $culture;
$properties['ContentTree'] = $ContentTree;

ob_start();
use_helper('I18N');

$current_culture = 'en_US';
// set_time_limit(380);
// for($i = ord('a'); $i < ord('z'); $i++){
// for($j = ord('a'); $j < ord('z'); $j++){
// $iso = chr($i).chr($j);
// printf('%s: %s<br />', $iso, format_language($iso, $current_culture));
// }
// }
$allLanguages = sfConfig::get('app_translations_available_languages');
$default = sfConfig::get('app_translations_default_language');

$ContentTreeNode = $ContentTree->getCurrentNode();

$existing_languages = $node->getAvailableTranslations();
$allLanguages = Catalyz::getAllTranslations();
$perms = array();
$available_languages = array();
foreach($allLanguages as $iso => $name) {
    if (!isset($existing_languages[$iso])) {
        $available_languages[$iso] = ucfirst($name);
    }
    $perms[$iso]['edit'] = true;
    $perms[$iso]['delete'] = ($iso != $default);
}
include_partial('catalyz/header');

?>

<div id="catalyz-content">
<div id="catalyz-content-internal">
<h1><?php printf(__('Manage translations for page "%s"', null, 'catalyz'),
	sprintf('<a href="%s">%s</a>', $ContentTreeNode->getUrl(), $node->getTitle())
	);

?></h1>
<?php
include_partial('catalyz/flash');

?>
<p><?php echo __('Current page is available in the following translations:', null, 'catalyz'); ?></p>
<?php

if (count($existing_languages) > 0) {
    echo '<table cellspacing="1" cellpadding="3">';
    foreach($existing_languages as $iso => $language) {

        ?>
		<tr>
		<td><?php echo image_tag('catalyz/translations/' . $iso . '.gif');
        ?></td>
		<td width="80"><?php echo ucfirst($language);
        ?></td>
		<td><a href="<?php echo $ContentTreeNode->getUrl($iso);
?>"><?php echo image_tag('catalyz/view', array('border' => 0, 'align' => 'top')); ?></a>&nbsp;<a href="<?php echo $ContentTreeNode->getUrl($iso);
        ?>"><?php echo __('View', null, 'catalyz'); ?></a></td>
		<td><?php if ($perms[$iso]['edit']) {
			$link ='@node-translation-edit?node_id=' . $node->getId() . '&culture=' . $iso;
            echo link_to(image_tag('catalyz/edit', array('border' => 0, 'align' => 'top')), $link);
			echo '&nbsp;';
			 echo link_to(__('Edit', null, 'catalyz'), $link);
            }
        ?></td>
		<td><?php if ($perms[$iso]['delete']) {
	$link ='@node-translation-delete?node_id=' . $node->getId() . '&culture=' . $iso;
	echo link_to(image_tag('catalyz/delete', array('border' => 0, 'align' => 'top')), $link);
	echo '&nbsp;';
	echo link_to(__('Delete', null, 'catalyz'), $link, array(
                    'onclick' => 'return confirm(\''.__('Are you sure to remove "%lang" translation for current page?', array('%lang' => $language), 'catalyz'). '\');'
                    ));
             }
        ?></td>
		</tr>
		<?php
    }
    echo '</table>';
}

if (count($available_languages) > 0) {
    echo form_tag('@node-translation-add?node_id=' . $node->getId());

    ?><br />
	<fieldset>
		<legend><?php echo __('Add a new language version', null, 'catalyz'); ?></legend>
	<?php

    $options = options_for_select($available_languages);
    echo select_tag('language', $options);
    echo '&nbsp;';
    echo submit_tag(__('Add', null, 'catalyz'), array('name' => 'Add'));
    echo '</form></fieldset>';
}

?>
<p><?php echo __('You can get an overview of %link_start%all pages translations here%link_end%', array(
		'%link_start%' => '<a href="'. url_for('@node-translation-overview?node_id=' . $node->getId()).'">',
		'%link_end%' => '</a>'), 'catalyz'); ?></p>
</div>
</div>
<?php

$content = ob_get_contents();

ob_end_clean();

$layout = LayoutManager::instance()->getCatalyzTemplate($ContentTreeNode->getCurrentLayoutNameForAction());
$properties['layout_family'] = $layout[0];
$properties['layout_name'] = $layout[1];
$properties['content'] = $content;

include_component($properties['layout_family'], $properties['layout_name'], $properties);

?>