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

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

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

// Check if article is given and get article ID
$article = isset($params['article']) ? (int)$params['article'] : 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_url', $this->GetPreference('url_use') ? true : false);
$smarty->assign('use_detailpage', $this->GetPreference('detailpage_use') ? true : false);
$smarty->assign('use_dates', $this->GetPreference('dates_use') ? true : false);
$smarty->assign('use_availibility', $this->GetPreference('availibility_use') ? true : false);
$smarty->assign('use_deliverytime', $this->GetPreference('deliverytime_use') ? true : false);
$smarty->assign('use_shippingcosts', $this->GetPreference('shippingcosts_use') ? true : false);
$smarty->assign('use_price', $this->GetPreference('price_use') ? true : false);
$smarty->assign('use_vat', $this->GetPreference('vat_use') ? true : false);
$smarty->assign('use_cart', $this->GetPreference('cart_use') ? true : false);
$smarty->assign('currency', $this->GetPreference('currency'));
$smarty->assign('use_seo', $this->GetPreference('seo_use') ? true : false);

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

	// Get article info
	$articleinfo = $db->GetRow('SELECT title,returnid,short_desc,long_desc,user_id,datetime,start_date,end_date,detail_link,image_url,image_text,gallery_id,sort_order,availibility,price,vat,delivery_time,shipping_costs,cart,url,active,seo_url,meta_title,meta_description,noindex,nofollow,noarchive,noodp,notranslate,created,modified FROM '.cms_db_prefix().'module_articles_articles WHERE entry_id = ?', array($article));
	if (!$articleinfo) $articleinfo = array( 'title'=>'','short_desc'=>'','long_desc'=>'','user_id'=>0,'datetime'=>time(),'start_date'=>'','end_date'=>'','detail_link'=>0,'image_url'=>'','image_text'=>'','gallery_id'=>0,'sort_order'=>0,'availibility'=>0,'price'=>0.0,'vat'=>$this->GetPreference('vat_standard'),'delivery_time'=>'','shipping_costs'=>0.0,'cart'=>$this->GetPreference('cart_use') ? 1 : 0,'url'=>'','active'=>1,'seo_url'=>'','meta_title'=>'','meta_description'=>'','noindex'=>0,'nofollow'=>0,'noarchive'=>1,'noodp'=>1,'notranslate'=>1,'created'=>time(),'modified'=>time() );

	// Get categories
	$categorylist = $db->getAssoc('SELECT title,entry_id FROM '.cms_db_prefix().'module_articles_category ORDER BY title');
	if (!$categorylist) $categorylist = array();
	// 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_editarticle',$returnid,'post','multipart/form-data',true,array()));
    $smarty->assign('formend', $this->CreateFormEnd());
    $smarty->assign('submit', $this->CreateInputSubmit($id,$article ? 'submit': 'add',$this->Lang('submit')));
    $smarty->assign('cache', $this->CreateInputSubmit($id,$article ? '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('article', $this->CreateInputHidden($id,'article',$article));

	// common data
	$smarty->assign('prompt_title', $this->Lang('prompt_title'));
	$smarty->assign('input_title', $this->CreateInputText($id,'title',$articleinfo['title'],50,100));
	$smarty->assign('prompt_short', $this->Lang('prompt_short'));
	$smarty->assign('input_short', $this->CreateTextArea(true, $id, $articleinfo['short_desc'], 'short_desc', 'pagesmalltextarea'));
	$smarty->assign('prompt_long', $this->Lang('prompt_long'));
	$smarty->assign('input_long', $this->CreateTextArea(true, $id, $articleinfo['long_desc'], 'long_desc'));
	$smarty->assign('prompt_availibility', $this->Lang('availibility'));
	$smarty->assign('input_availibility', $this->CreateInputText($id,'availibility',$articleinfo['availibility'],50,100));
	$smarty->assign('prompt_price', $this->Lang('price'));
	$smarty->assign('input_price', $this->CreateInputText($id,'price',$articleinfo['price'],10,100));
	$smarty->assign('prompt_vat', $this->Lang('vat'));
	$smarty->assign('input_vat', $this->CreateInputText($id,'vat',$articleinfo['vat'],5,10));
	$smarty->assign('prompt_delivery_time', $this->Lang('delivery_time'));
	$smarty->assign('input_delivery_time', $this->CreateInputText($id,'delivery_time',$articleinfo['delivery_time'],50,100));
	$smarty->assign('prompt_shipping_costs', $this->Lang('shipping_costs'));
	$smarty->assign('input_shipping_costs', $this->CreateInputText($id,'shipping_costs',$articleinfo['shipping_costs'],50,100));
	$smarty->assign('prompt_cart', $this->Lang('cart'));
	$smarty->assign('input_cart', $this->CreateInputCheckbox($id, 'cart', 'true', $articleinfo['cart'] ? 'true' : 'false'));
	$smarty->assign('prompt_url', $this->Lang('url'));
	$smarty->assign('input_url', $this->CreateInputText($id,'url',$articleinfo['url'],50,100));
	$smarty->assign('prompt_link', $this->Lang('prompt_link'));
	$smarty->assign('input_link',$contentops->CreateHierarchyDropdown('',$articleinfo['detail_link'],$id.'detail_link',0,1,0,1)); // current, parent, parent_id, allowcurrent, use_perms, ignore_current, allow_all

	// meta data
	$smarty->assign('prompt_active', $this->Lang('active'));
	$smarty->assign('input_active', $this->CreateInputCheckbox($id, 'active', 'true', $articleinfo['active'] ? 'true' : 'false'));
	$smarty->assign('prompt_user', $this->Lang('user'));
	$smarty->assign('input_user_id', $this->CreateInputText($id,'user_id',$articleinfo['user_id'],4,10));
	$smarty->assign('prompt_datetime', $this->Lang('datetime'));
	$smarty->assign('prefix_datetime', $id . 'datetime_');
	$smarty->assign('datetime', $articleinfo['datetime']);
	$smarty->assign('prompt_order', $this->Lang('sort_order'));
	$smarty->assign('sort_order', $this->CreateInputHidden($id,'sort_order',$articleinfo['sort_order']));
	$smarty->assign('prompt_created', $this->Lang('created'));
	$smarty->assign('created', $articleinfo['created']);
	$smarty->assign('prompt_modified', $this->Lang('modified'));
	$smarty->assign('modified', $articleinfo['modified']);

	// Return ID
	$return_id = 0;
	if (isset($articleinfo['returnid'])) $return_id = $articleinfo['returnid'] ? $articleinfo['returnid'] : 0;
	if (!$return_id) $return_id = $db->GetOne('SELECT C.returnid FROM '.cms_db_prefix().'module_articles_category_articles CA, '.cms_db_prefix().'module_articles_articles A WHERE AC.cat_id=C.entry_id AND C.active=1 AND AC.article_id = ? LIMIT 1', array($article));
	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

	// category
	$smarty->assign('prompt_category', $this->Lang('prompt_category'));
	$smarty->assign('cats', $categorylist);
	$smarty->assign('cats_selected', $cats);

	// start- and end date
	$smarty->assign('prompt_start_date', $this->Lang('prompt_start_date'));
	$smarty->assign('prefix_startdate', $id . 'start_date_');
	$smarty->assign('start_date', $articleinfo['start_date']);
	$smarty->assign('prompt_end_date', $this->Lang('prompt_end_date'));
	$smarty->assign('prefix_enddate', $id . 'end_date_');
	$smarty->assign('end_date', $articleinfo['end_date']);

	// image(s)
	$smarty->assign('prompt_image', $this->Lang('image'));
	$smarty->assign('input_image', $articleinfo['image_url'] ? '' : $this->CreateFileUploadInput($id, 'image','',50));
	$smarty->assign('input_image_ov', $articleinfo['image_url'] ? '<img src="'.$config['image_uploads_url'].'/Articles/articles/'.$article.'/'.$articleinfo['image_url'].'" />' : '');
	$smarty->assign('input_image_del', $articleinfo['image_url'] ? $this->Lang('delete_image').': '.$this->CreateInputCheckbox($id, 'image_del',1,0) : '');
	$smarty->assign('prompt_image_text', $this->Lang('image_text'));
	$smarty->assign('input_image_text', $this->CreateInputText($id,'image_text',$articleinfo['image_text'],50,255));
	//$smarty->assign('prompt_gallery', $this->Lang('gallery'));
	//$smarty->assign('input_gallery_id', $this->CreateInputText($id,'gallery_id',$articleinfo['gallery_id'],4,10));

	// seo data
	$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',$articleinfo['seo_url'],50,150));
	$smarty->assign('prompt_meta_title', $this->Lang('meta_title'));
	$smarty->assign('input_meta_title', $this->CreateInputText($id,'meta_title',$articleinfo['meta_title'],70,70));
	$smarty->assign('prompt_meta_description', $this->Lang('meta_description'));
	$smarty->assign('input_meta_description', $this->CreateInputText($id,'meta_description',$articleinfo['meta_description'],70,160));
	$smarty->assign('prompt_noindex', $this->Lang('noindex'));
	$smarty->assign('input_noindex', $this->CreateInputCheckbox($id, 'noindex', 'true', $articleinfo['noindex'] ? 'true' : 'false'));
	$smarty->assign('prompt_nofollow', $this->Lang('nofollow'));
	$smarty->assign('input_nofollow', $this->CreateInputCheckbox($id, 'nofollow', 'true', $articleinfo['nofollow'] ? 'true' : 'false'));
	$smarty->assign('prompt_noarchive', $this->Lang('noarchive'));
	$smarty->assign('input_noarchive', $this->CreateInputCheckbox($id, 'noarchive', 'true', $articleinfo['noarchive'] ? 'true' : 'false'));
	$smarty->assign('prompt_noodp', $this->Lang('noodp'));
	$smarty->assign('input_noodp', $this->CreateInputCheckbox($id, 'noodp', 'true', $articleinfo['noodp'] ? 'true' : 'false'));
	$smarty->assign('prompt_notranslate', $this->Lang('notranslate'));
	$smarty->assign('input_notranslate', $this->CreateInputCheckbox($id, 'notranslate', 'true', $articleinfo['notranslate'] ? 'true' : 'false'));

	// gallery connection
	$galMod = $this->GetModuleInstance($this->GetPreference('use_gallery','IPTCGallery'));
	if (is_object($galMod)) {
		$smarty->assign('prompt_gallery', $galMod->GetFriendlyName()." auswählen");
		$smarty->assign('input_gallery_id', $this->CreateGalleryChooser($id, 'gallery_id', $articleinfo['gallery_id']));
	}

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

}

// Display a list with available articles
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
	$categorylist = $db->getAssoc('SELECT title,entry_id FROM '.cms_db_prefix().'module_articles_category ORDER BY title');
	if (!$categorylist) $categorylist = array();
        $categorylist[$this->Lang('not_assigned')] = '';

        if (empty($_SESSION['Articles_catfilter'])) { 
            $sql_val = "IS NULL"; 
            unset ($curcategory);
        } else {
            $sql_val = " = " . $_SESSION['Articles_catfilter']; 
            $curcategory = $_SESSION['Articles_catfilter'];
        }

        
        $smarty->assign('formkatstart',$this->CreateFormStart($id,'admin_catfilter',$returnid,'post','multipart/form-data',true,array()));
        $smarty->assign('formkatend', $this->CreateFormEnd());
        $smarty->assign('tabto',$this->CreateInputHidden($id,'tabto',$tabto));
        
        $smarty->assign('prompt_category',$this->Lang('category'));
        $smarty->assign('input_category',
		$this->CreateInputDropdown($id,'category',$categorylist,-1,isset($curcategory) ? $curcategory : 0,'id="filter_cat"'));
        $smarty->assign('submitfilter',
		$this->CreateInputSubmit($id,'submitfilter',$this->Lang('submit')));
        $smarty->assign('filtertext',$this->Lang('title_filter'));

	// Get articles
	$allarticles = $db->GetAll('SELECT entry_id,user_id,datetime,title,short_desc,start_date,end_date,detail_link,sort_order,availibility,price,vat,delivery_time,shipping_costs,cart,url,active,seo_url,meta_title,meta_description,noindex,nofollow,noarchive,noodp,notranslate,created,modified FROM '.cms_db_prefix().'module_articles_articles LEFT JOIN '.cms_db_prefix().'module_articles_category_articles ON '.cms_db_prefix().'module_articles_articles.entry_id = '.cms_db_prefix().'module_articles_category_articles.article_id WHERE '.cms_db_prefix().'module_articles_category_articles.cat_id '.$sql_val.' ORDER BY sort_order ASC, entry_id DESC');
	if (!is_array($allarticles)) $allarticles = array();

	// Show articles
	$rowarray = array();
	$rowclass = 'row1';
	foreach ($allarticles as $ar) {
		// Build a new row
		$row = new StdClass();
		$row->rowclass = $rowclass;
		// ID
		$row->id = $ar['entry_id'];
		// Created
		$row->created = $ar['created'];
		// Modified
		$row->modified = $ar['modified'];
		// Title
		$row->title = $this->CreateLink( $id, 'admin_editarticle', $returnid, $ar['title'], array ( 'tabto'=>$tabto, 'article'=>$ar['entry_id'] ), '', false, false, ' title="'.$this->Lang('edit').'"');
		// User ID
		$row->user_id = $ar['user_id'];
		// Article date
		$row->datetime = $ar['datetime'];
		// Start date
		$row->start_date = $ar['start_date'];
		// End date
		$row->end_date = $ar['end_date'];
		// Detail link
		$row->detail_link = $ar['detail_link'];
		// Availibility
		$row->availibility = $ar['availibility'];
		// Price
		$row->price = $ar['price'];
		// VAT
		$row->vat = $ar['vat'];
		// Delivery Time
		$row->delivery_time = $ar['delivery_time'];
		// Shipping costs
		$row->shipping_costs = $ar['shipping_costs'];
		// Cart
		$row->cart = $ar['cart'] ? $this->Lang('yes') : $this->Lang('no');
		// URL
		$row->url = $ar['url'];
		// SEO URL
		$row->seo_url = $ar['seo_url'];
		// Meta Title
		$row->meta_title = $ar['meta_title'];
		// Meta Description
		$row->meta_description = $ar['meta_description'];
		// NoIndex
		$row->noindex = $ar['noindex'];
		// NoFollow
		$row->nofollow = $ar['nofollow'];
		// Build link for de-/activate the article
		$active = $ar['active'] ? 1 : 0;
		if ( $active ) { $row->active = $this->CreateLink( $id, 'admin_articleactive', $returnid, $imageactive, array ( 'tabto'=>$tabto, 'article'=>$ar['entry_id'], 'active'=>0 ), $this->Lang('reallymakeinactive'), false, false, ' title="'.$this->Lang('makeinactive').'"'); }
		else { $row->active = $this->CreateLink( $id, 'admin_articleactive', $returnid, $imageinactive, array ( 'tabto'=>$tabto, 'article'=>$ar['entry_id'], 'active'=>1 ), $this->Lang('reallymakeactive'), false, false, ' title="'.$this->Lang('makeactive').'"'); }
		// Build edit icon
		$row->editlink = $this->CreateLink( $id, 'admin_editarticle', $returnid, $imageedit, array ( 'tabto'=>$tabto, 'article'=>$ar['entry_id'] ), '', false, false, ' title="'.$this->Lang('edit').'"');
		// Build delete icon
		$row->deletelink = $this->CreateLink( $id, 'admin_deletearticle', $returnid, $imagedelete, array ( 'tabto'=>$tabto, 'article'=>$ar['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('user', $this->Lang('user'));
	$smarty->assign('date', $this->Lang('date'));
	$smarty->assign('prompt_modified', $this->Lang('modified'));
	$smarty->assign('prompt_created', $this->Lang('created'));
	$smarty->assign('active', $this->Lang('active'));
	$smarty->assign('header_name', $this->Lang('headername'));
	$smarty->assign('order', $this->Lang('order'));
	$smarty->assign('seo_url', $this->Lang('prompt_seo_url'));
	$smarty->assign('price', $this->Lang('price'));
	$smarty->assign('vat', $this->Lang('vat'));
	$smarty->assign('availibility', $this->Lang('availibility_short'));
	$smarty->assign('addlink', $this->CreateLink( $id, 'admin_editarticle', '', $imagenew.' '.$this->Lang('addarticle'), array ( 'tabto'=>$tabto, 'article'=>'' ), '', false, false, ' title="'.$this->Lang('addarticle').'"'));
	$smarty->assign('reorderlink', $this->GetPreference('order_use') ? $this->CreateLink( $id, 'admin_orderarticle', '', $imagereorder.' '.$this->Lang('reorder'), array ( 'tabto'=>$tabto ), '', false, false, ' title="'.$this->Lang('reorder').'"') : '');
	echo $this->ProcessTemplate('listarticles.tpl');

}

// EOF