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

// Check authorisation
if (!isset($gCms)) exit;
if ( !$this->CheckPermission('Articles: modify categories') ) exit;

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

// Check if Category is given and get Category ID
$category = isset($params['category']) ? (int)$params['category'] : 0;

// Give settings to smarty
$smarty->assign('use_longdesc', $this->GetPreference('longdesc_use') ? true : false);
$smarty->assign('use_image', $this->GetPreference('image_use') ? true : false);
$smarty->assign('use_order', $this->GetPreference('order_use') ? true : false);
$smarty->assign('use_seo', $this->GetPreference('seo_use') ? true : false);

// Display form for editing a category
if (isset($params['category'])) {

	// Get category info
	$categoryinfo = $db->GetRow('SELECT belongs_to,returnid,title,short_desc,long_desc,image_url,sort_order,active,seo_url,meta_title,meta_description,noindex,nofollow,noarchive,noodp,notranslate FROM '.cms_db_prefix().'module_articles_category WHERE entry_id = ?', array($category));
	if (!$categoryinfo) $categoryinfo = array( 'belongs_to'=>0,'title'=>'','short_desc'=>'','long_desc'=>'','image_url'=>'','sort_order'=>0,'active'=>1,'seo_url'=>'','meta_title'=>'','meta_description'=>'','noindex'=>0,'nofollow'=>0,'noarchive'=>1,'noodp'=>1,'notranslate'=>1 );

	// Get categories
	$categorylist = $db->getAssoc('SELECT title,entry_id FROM '.cms_db_prefix().'module_articles_category ORDER BY title');
	if (!$categorylist) $categorylist = array();
	$nothing = array($this->Lang('nothing')=>0);
	$categorylist = array_merge($nothing,$categorylist);
	// Get selected categories
	#if ($article>0) { $cats = $db->getCol('SELECT cat_id FROM '.cms_db_prefix().'module_articles_category_articles WHERE article_id=?',array($article)); }
	#else { $cats = array(); }

	// Assign Smarty vars
	$smarty->assign('formid',$id);
	$smarty->assign('formstart',$this->CreateFormStart($id,'admin_editcategory',$returnid,'post','multipart/form-data',true,array()));
    $smarty->assign('formend', $this->CreateFormEnd());
    $smarty->assign('submit', $this->CreateInputSubmit($id,$category ? 'submit': 'add',$this->Lang('submit')));
    $smarty->assign('cache', $this->CreateInputSubmit($id,$category ? '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('category', $this->CreateInputHidden($id,'category',$category));

	$smarty->assign('use_image', $this->GetPreference('image_use') ? true : false);
	$smarty->assign('use_longdesc', $this->GetPreference('longdesc_use') ? true : false);

	$smarty->assign('prompt_belongs_to', $this->Lang('belongs_to'));
	$smarty->assign('input_belongs_to',$this->CreateInputDropdown($id,'belongs_to',$categorylist,-1,0));

	$smarty->assign('prompt_title', $this->Lang('prompt_title'));
	$smarty->assign('input_title', $this->CreateInputText($id,'title',$categoryinfo['title'],50,100));
	$smarty->assign('prompt_short', $this->Lang('prompt_short'));
	$smarty->assign('input_short', $this->CreateTextArea(true, $id, $categoryinfo['short_desc'], 'short_desc', 'pagesmalltextarea'));
	$smarty->assign('prompt_long', $this->Lang('prompt_long'));
	$smarty->assign('input_long', $this->CreateTextArea(true, $id, $categoryinfo['long_desc'], 'long_desc'));
	$smarty->assign('prompt_image', $this->Lang('prompt_image'));
	$smarty->assign('input_image', $categoryinfo['image_url'] ? '' : $this->CreateFileUploadInput($id, 'image','',50));
	$smarty->assign('input_image_ov', $categoryinfo['image_url'] ? '<img src="'.$config['image_uploads_url'].'/Articles/categories/'.$category.'/'.$categoryinfo['image_url'].'" />' : '');
	$smarty->assign('input_image_del', $categoryinfo['image_url'] ? $this->Lang('delete_image').': '.$this->CreateInputCheckbox($id, 'image_del',1,0) : '');
	$smarty->assign('prompt_active', $this->Lang('active'));
	$smarty->assign('input_active', $this->CreateInputCheckbox($id, 'active', 'true', $categoryinfo['active'] ? 'true' : 'false'));
	$smarty->assign('prompt_order', $this->Lang('sort_order'));
	$smarty->assign('sort_order', $this->CreateInputHidden($id,'sort_order',$categoryinfo['sort_order']));

	// Return ID
	$return_id = $categoryinfo['returnid'] ? $categoryinfo['returnid'] : 0;
	if (!$return_id) $return_id = $db->GetOne('SELECT returnid FROM '.cms_db_prefix().'module_articles_category WHERE active=1 AND belongs_to = ? LIMIT 1', array($category));
	if (!$return_id) $return_id = $this->GetPreference('returnid');
	$smarty->assign('prompt_returnid', $this->Lang('returnpage'));
	$smarty->assign('input_returnid',$contentops->CreateHierarchyDropdown('',$return_id,$id.'return_id',0,1,0,1)); // current, parent, parent_id, allowcurrent, use_perms, ignore_current, allow_all

	$smarty->assign('seo_fields', $this->Lang('seo_fields'));
	$smarty->assign('prompt_seo_url', $this->Lang('seo_url'));
	$smarty->assign('input_seo_url', $this->CreateInputText($id,'seo_url',$categoryinfo['seo_url'],50,150));
	$smarty->assign('prompt_meta_title', $this->Lang('meta_title'));
	$smarty->assign('input_meta_title', $this->CreateInputText($id,'meta_title',$categoryinfo['meta_title'],70,70));
	$smarty->assign('prompt_meta_description', $this->Lang('meta_description'));
	$smarty->assign('input_meta_description', $this->CreateInputText($id,'meta_description',$categoryinfo['meta_description'],70,160));
	$smarty->assign('prompt_noindex', $this->Lang('noindex'));
	$smarty->assign('input_noindex', $this->CreateInputCheckbox($id, 'noindex', 'true', $categoryinfo['noindex'] ? 'true' : 'false'));
	$smarty->assign('prompt_nofollow', $this->Lang('nofollow'));
	$smarty->assign('input_nofollow', $this->CreateInputCheckbox($id, 'nofollow', 'true', $categoryinfo['nofollow'] ? 'true' : 'false'));
	$smarty->assign('prompt_noarchive', $this->Lang('noarchive'));
	$smarty->assign('input_noarchive', $this->CreateInputCheckbox($id, 'noarchive', 'true', $categoryinfo['noarchive'] ? 'true' : 'false'));
	$smarty->assign('prompt_noodp', $this->Lang('noodp'));
	$smarty->assign('input_noodp', $this->CreateInputCheckbox($id, 'noodp', 'true', $categoryinfo['noodp'] ? 'true' : 'false'));
	$smarty->assign('prompt_notranslate', $this->Lang('notranslate'));
	$smarty->assign('input_notranslate', $this->CreateInputCheckbox($id, 'notranslate', 'true', $categoryinfo['notranslate'] ? 'true' : 'false'));

    // Display Template
    echo $this->ProcessTemplate('editcategory.tpl');

}

// Display a list with available categories
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');
	$imagestandard = $admintheme->DisplayImage('icons/system/true.gif',$this->Lang('standard'),'','','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');
	$imagereorder = $admintheme->DisplayImage('icons/system/reorder.gif',$this->Lang('reorder'),'','','systemicon');
	$imagedown = $admintheme->DisplayImage('icons/system/arrow-d.gif',$this->Lang('down'),'','','systemicon');
	$imageup = $admintheme->DisplayImage('icons/system/arrow-u.gif',$this->Lang('up'),'','','systemicon');

	// Get categories
	$allcategories = $db->GetAll('SELECT belongs_to,entry_id,title,short_desc,sort_order,active,seo_url,meta_title,meta_description,noindex,nofollow FROM '.cms_db_prefix().'module_articles_category');
	if (!is_array($allcategories)) $allcategories = array();

	// Get maximum order number
	$maxorder = $db->GetOne('SELECT sort_order FROM '.cms_db_prefix().'module_articles_category ORDER BY sort_order DESC', array());

	// Show categories
	$rowarray = array();
	$rowclass = 'row1';
	foreach ($allcategories as $cat) {
		// Build a new row
		$row = new StdClass();
		$row->rowclass = $rowclass;
		// ID
		$row->id = $cat['entry_id'];
		// Title
		$row->title = $this->CreateLink( $id, 'admin_editcategory', $returnid, $cat['title'], array ( 'tabto'=>$tabto, 'category'=>$cat['entry_id'] ), '', false, false, ' title="'.$this->Lang('edit').'"');
		// Build link for de-/activate the category
		$active = $cat['active'] ? 1 : 0;
		if ( $active ) { $row->active = $this->CreateLink( $id, 'admin_categoryactive', $returnid, $imageactive, array ( 'tabto'=>$tabto, 'category'=>$cat['entry_id'], 'active'=>0 ), $this->Lang('reallymakeinactive'), false, false, ' title="'.$this->Lang('makeinactive').'"'); }
		else { $row->active = $this->CreateLink( $id, 'admin_categoryactive', $returnid, $imageinactive, array ( 'tabto'=>$tabto, 'category'=>$cat['entry_id'], 'active'=>1 ), $this->Lang('reallymakeactive'), false, false, ' title="'.$this->Lang('makeactive').'"'); }
		// SEO URL
		$row->seo_url = $cat['seo_url'];
		// Meta Title
		$row->meta_title = $cat['meta_title'];
		// Meta Description
		$row->meta_description = $cat['meta_description'];
		// NoIndex
		$row->noindex = $cat['noindex'];
		// NoFollow
		$row->nofollow = $cat['nofollow'];
		// Build edit icon
		$row->editlink = $this->CreateLink( $id, 'admin_editcategory', $returnid, $imageedit, array ( 'tabto'=>$tabto, 'category'=>$cat['entry_id'] ), '', false, false, ' title="'.$this->Lang('edit').'"');
		// Build delete icon
		$row->deletelink = $this->CreateLink( $id, 'admin_deletecategory', $returnid, $imagedelete, array ( 'tabto'=>$tabto, 'category'=>$cat['entry_id'] ), $this->Lang('reallydelete'), false, false, ' title="'.$this->Lang('delete').'"');
		// Merge new domain 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('active', $this->Lang('active'));
	$smarty->assign('header_name', $this->Lang('headername'));
	$smarty->assign('addlink', $this->CreateLink( $id, 'admin_editcategory', '', $imagenew.' '.$this->Lang('addcategory'), array ( 'tabto'=>$tabto, 'category'=>'' ), '', false, false, ' title="'.$this->Lang('addcategory').'"'));
	$smarty->assign('reorderlink', $this->GetPreference('order_use') ? $this->CreateLink( $id, 'admin_ordercategory', '', $imagereorder.' '.$this->Lang('reorder'), array ( 'tabto'=>$tabto ), '', false, false, ' title="'.$this->Lang('reorder').'"') : '');
	echo $this->ProcessTemplate('listcategories.tpl');

}

// EOF