<?php
/*======================================================================================
Module: Articles
======================================================================================*/

// Check authorisation
if (!isset($gCms)) exit;
if ( !$this->CheckPermission('Modify Templates') ) exit;

// Set Tab name
$tabto = 'templates';

// Display form for editing a template
if (isset($params['template'])) {
    // Get template name
    $template = preg_replace('/[^0-9a-zA-Z_-]/','',$params['template']);
	$template_content = $template ? $this->GetTemplate($template, $this->GetName()) : '';
    // Assign Smarty vars
    $smarty->assign('formstart',$this->CreateFormStart($id,'admin_edittemplate',$returnid,'post','multipart/form-data',true,array()));
    $smarty->assign('formend', $this->CreateFormEnd());
    $smarty->assign('submit', $this->CreateInputSubmit($id,$template ? 'submit': 'add',$this->Lang('submit')));
    $smarty->assign('cache', $this->CreateInputSubmit($id,$template ? 'cache' : 'cacheadd',$this->Lang('cache')));
    $smarty->assign('reset', $this->CreateInputReset($id,'reset',$this->Lang('reset')));
    $smarty->assign('cancel', $this->CreateInputSubmit($id,'cancel',$this->Lang('cancel')));
    $smarty->assign('tabto', $this->CreateInputHidden($id,'tabto',$tabto));
	$smarty->assign('prompt_title',  $this->Lang('prompt_template_name'));
	$smarty->assign('input_title', $template ? $template.$this->CreateInputHidden($id,'templatename',$template) : $this->CreateInputText($id,'template','',50,100));
    $smarty->assign('prompt_content', $this->Lang('prompt_template_code'));
	$smarty->assign('input_content', $this->CreateTextArea(false, $id, $template_content, 'content', 'pagebigtextarea'));
    // Display Template
    echo $this->ProcessTemplate('edittemplate.tpl');
}
// Display a list with available templates
else {
    // Set vars and images
    $admintheme = cms_utils::get_theme_object();
	$imageview = $admintheme->DisplayImage('icons/system/view.gif',$this->Lang('view'),'','','systemicon');
	$imagenostandard = $admintheme->DisplayImage('icons/system/false.gif',$this->Lang('makestandard'),'','','systemicon');
	$imageinactive = $admintheme->DisplayImage('icons/system/false.gif',$this->Lang('makeactive'),'','','systemicon');
	$imageactive = $admintheme->DisplayImage('icons/system/true.gif',$this->Lang('makeinactive'),'','','systemicon');
	$imageedit = $admintheme->DisplayImage('icons/system/edit.gif',$this->Lang('edit'),'','','systemicon');
	$imagedelete = $admintheme->DisplayImage('icons/system/delete.gif',$this->Lang('delete'),'','','systemicon');
	$imagenew = $admintheme->DisplayImage('icons/system/newobject.gif',$this->Lang('newtemplate'),'','','systemicon');
	// Get templates
	$alltemplates = $this->ListTemplates($this->getName());
	// Show templates
	$rowarray = array();
	$rowclass = 'row1';
	foreach ( $alltemplates as $onetemplate ) {
		// Build edit link
		$row = new StdClass();
		$row->name = $this->CreateLink( $id, 'admin_edittemplate', $returnid, $onetemplate, array ( 'tabto'=>$tabto, 'template'=>$onetemplate ), '', false, false, ' title="'.$this->Lang('edit').'"');
		$row->rowclass = $rowclass;
		// Build link for making the template to the default template
		$default = ($this->GetPreference('default_template') == $onetemplate) ? true : false;
		if ( $default ) { $row->default = $imageactive; }
		else { $row->default = $this->CreateLink( $id, 'admin_makestandardtemplate', $returnid, $imageinactive, array ( 'tabto'=>$tabto, 'template'=>$onetemplate ), $this->Lang('reallymakestandard'), false, false, ' title="'.$this->Lang('makestandard').'"'); }
		// Build edit icon
		$row->editlink = $this->CreateLink( $id, 'admin_edittemplate', $returnid, $imageedit, array ( 'tabto'=>$tabto, 'template'=>$onetemplate ), '', false, false, ' title="'.$this->Lang('edit').'"');
		// Build delete icon
		if ( $default ) { $row->deletelink = '&nbsp;'; }
		else { $row->deletelink = $this->CreateLink( $id, 'admin_deletetemplate', $returnid, $imagedelete, array ( 'tabto'=>$tabto, 'template'=>$onetemplate ), $this->Lang('reallydelete'), false, false, ' title="'.$this->Lang('delete').'"'); }
		// merge new template to array
		array_push ($rowarray, $row);
		// Alternate row color
		($rowclass == "row1" ? $rowclass = "row2" : $rowclass = "row1");
	}
	// Assign smarty vars
	$smarty->assign('items', $rowarray );
	$smarty->assign('id', $this->Lang('id'));
	$smarty->assign('template', $this->Lang('template'));
	$smarty->assign('standard', $this->Lang('standard'));
	$smarty->assign('header_name', $this->Lang('headername'));
	$smarty->assign('addlink', $this->CreateLink( $id, 'defaultadmin', $returnid, $imagenew.' '.$this->Lang('addtemplate'), array ( 'tabto'=>$tabto, 'template'=>'' ), '', false, false, ' title="'.$this->Lang('addtemplate').'"'));
	$smarty->assign($this->CreateFormEnd());
	echo $this->ProcessTemplate('listtemplates.tpl');
}

// EOF