<?php
////////////////////////////////////////////////////////////////////////////////
//   Copyright (C) ReloadCMS Development Team                                 //
//   http://reloadcms.com                                                     //
//   This product released under GNU General Public License v2                //
////////////////////////////////////////////////////////////////////////////////
?>
<script type="text/javascript">
<!--
function selChange(seln) {
selNum = seln.files.selectedIndex;
modeind = seln.mode.selectedIndex;
val = seln.mode.options[modeind].value;
var a = /^[\w\-\_]+\.(gif|jpg|png){1}$/;
	if (selNum > 0) {
	Isel = seln.files.options[selNum].text;
	if (val=='text') {
	if (Isel.search(a) !== -1) Isel='[img]<?=FILES_PATH?>'+Isel+'[/img]\n';
	else Isel='[url]<?=FILES_PATH?>'+Isel+'[/url]\n';
	document.forms['arted'].elements['text'].value += Isel;
	} else {
	if (Isel.search(a) !== -1) Isel='<img src="<?=FILES_PATH?>'+Isel+'"/>\n';
	else Isel='<a href="<?=FILES_PATH?>'+Isel+'">'+Isel+'</a>\n';
	document.forms['arted'].elements['text'].value += Isel;
	}
	}
}
//-->
</script> 
<?
$articles = new articles();
if(!$system->checkForRight('ARTICLES-EDITOR')) $c = '#hidden';
$c = (empty($_POST['c'])) ? null : $_POST['c'];
$b = (empty($_POST['b'])) ? null : $_POST['b'];
$nb = (empty($_POST['nb'])) ? null : $_POST['nb'];
$a = (empty($_POST['a'])) ? null : $_POST['a'];

/******************************************************************************
* Perform deletion of articles                                                *
******************************************************************************/
if(!empty($_POST['delete'])) {
	foreach ($_POST['delete'] as $id => $chk) {
		if($chk && $articles->setWorkContainer($c) && $articles->deleteArticle($b, $id)){
			rcms_showAdminMessage(__('Article removed') . ': ' . $c . '/' . $b . '/' . $id);
		} else {
			rcms_showAdminMessage($articles->last_error . ': ' . $c . '/' . $b . '/' . $id);
		}
	}
}

/******************************************************************************
* Perform changing of article                                                 *
******************************************************************************/
if((!empty($_POST['save']) && !empty($c) && (!empty($b) || $c == '#hidden' || $c == '#root') && !empty($a) && $articles->setWorkContainer($c) && ($article = $articles->getArticle($b, $a, false, true, true, false))!== false)) {
$time=sql_to_unix_time($_POST['time']);
if($_POST['mode']=='php' && !$system->checkForRight('GENERAL')) 
return rcms_showAdminMessage(__('Error occurred').': '.__('You are not administrator of this site'));
	if(!@$articles->saveArticle($b, $a, 
					$_POST['title'], 
					$_POST['source'], 
					$_POST['keywords'], 
					$_POST['sef_desc'], 
					$_POST['description'], 
					$_POST['text'], 
					$_POST['mode'], 
					$_POST['comments'],
					$time
					)){
			rcms_showAdminMessage($articles->last_error);
	} else {
		rcms_showAdminMessage(__('Article saved'));
		sleep(1);
		if(!empty($nb) && $nb != $b){
			if(!$articles->moveArticle($b, $a, $nb)){
				rcms_showAdminMessage($articles->last_error);
			} else {
				rcms_showAdminMessage(__('Article moved'));
			}
		} else rcms_redirect (RCMS_ROOT_PATH.'?module=articles&c='.$c.'&b='.$b.'&a='.$a);
	}
}

/******************************************************************************
* Interface                                                                   *
******************************************************************************/
// Show container selector
$frm =new InputForm ('', 'post', __('Submit'));
$frm->addrow(__('Select section'), $frm->select_tag('c', $articles->getContainers(2), $c), 'top');
// Show category selector
if(!empty($c) && $c != '#hidden' && $c != '#root'){
	if($articles->setWorkContainer($c)){
		$frm->addrow(__('Select category'), $frm->select_tag('b', $articles->getCategories(true, false), $b), 'top');
	} else rcms_showAdminMessage($articles->last_error);
}
$frm->show();

if(!empty($_POST['tc']) && !empty($_POST['tb']) && !empty($_POST['ms']) && $_POST['ms'] == '2'){
	if(!$articles->moveArticleToContainer($_POST['mc'], $_POST['mb'], $_POST['ma'], $_POST['tc'], $_POST['tb'])){
		rcms_showAdminMessage($articles->last_error);
	} else {
		rcms_showAdminMessage(__('Article moved'));
	}
} elseif(!empty($_POST['tc']) && !empty($_POST['ms']) && $_POST['ms'] == '1'){
	if($_POST['mc'] == $_POST['tc']) {
		rcms_showAdminMessage(__('Cannot move article to source section'));
	} elseif(!$articles->setWorkContainer($_POST['tc'])) {
		rcms_showAdminMessage($articles->last_error);
	} elseif ($_POST['tc'] == '#hidden' || $_POST['tc'] == '#root') {
		if(!$articles->moveArticleToContainer($_POST['mc'], $_POST['mb'], $_POST['ma'], $_POST['tc'])){
			rcms_showAdminMessage($articles->last_error);
		} else {
			rcms_showAdminMessage(__('Article moved'));
		}
	} else {
		$containers = $articles->getContainers(1);
		$frm = new InputForm ('', 'post', __('Submit'));
		$frm->addbreak(__('Target where we will place article'));
		$frm->hidden('ms', '2');
		$frm->hidden('mc', $_POST['mc']);
		$frm->hidden('mb', $_POST['mb']);
		$frm->hidden('ma', $_POST['ma']);
		$frm->hidden('tc', $_POST['tc']);
		$frm->addrow(__('Section'), $containers[$_POST['tc']], 'top');
		$frm->addrow(__('Select category'), $frm->select_tag('tb', $articles->getCategories(true, false), $b), 'top');
		$frm->show();
	}
} elseif(!empty($c) && (!empty($b) || $c == '#hidden' || $c == '#root') && !empty($_POST['move'])){
	$frm = new InputForm ('', 'post', __('Submit'));
	$frm->addbreak(__('Target where we will place article'));
	$frm->hidden('ms', '1');
	$frm->hidden('mc', $c);
	$frm->hidden('mb', $b);
	$frm->hidden('ma', $_POST['move']);
	$frm->addrow(__('Select section'), $frm->select_tag('tc', $articles->getContainers(1), $c), 'top');
	$frm->show();
} elseif(!empty($c) && (!empty($b) || $c == '#hidden' || $c == '#root') && !empty($a) && $articles->setWorkContainer($c) && ($article = $articles->getArticle($b, $a, false, true, true, false))!== false){
	$categories_list = $articles->getCategories(true, false);
	$frm = new InputForm ('', 'post', __('Submit'), '', '', '', 'arted');
	$frm->addbreak(__('Edit article') . ' - ' . $article['title']);
	$frm->hidden('save', '1');
	$frm->hidden('c', $c);
	$frm->hidden('a', $a);
	$frm->hidden('b', $b);
	if($c !== '#root' && $c !== '#hidden') $frm->addrow(__('Select category'), $frm->select_tag('nb', $categories_list, $article['catid']), 'top');
	$frm->addrow(__('Title'), $frm->text_box('title', $article['title']), 'top');
	$frm->addrow(__('Author/source'), $frm->text_box('source', $article['src']), 'top');
	$frm->addrow(__('Keywords'), $frm->text_box('keywords', @$article['keywords']), 'top');
	$frm->addrow(__('Description for search engines'), $frm->text_box('sef_desc', @$article['sef_desc']), 'top');
	$frm->addrow('', rcms_show_bbcode_panel('arted.description'));
	$frm->addrow(__('Short description').'<br />'.tinymce_selector('description',false), $frm->textarea('description', $article['desc'], 70, 5), 'top');
	$frm->addrow('', rcms_show_bbcode_panel('arted.text'));
	$frm->addrow(__('Text').'<br />'.tinymce_selector('text',false), $frm->textarea('text', $article['text'], 70, 25), 'top');
	$files = rcms_scandir(FILES_PATH);										//Start Insert list uploaded files
	$key_thumb=array_search('_thumb',$files);
	if ($key_thumb!==FALSE) unset($files[$key_thumb]);
	if(!empty($files))			{	
	$frm->addrow(__('Add link to file') , $frm->select_tag('files',$files,-1,'onChange="selChange(this.form)">\n
	<option value="-1">'. __('Select file').'</option')
	.'&nbsp;&nbsp;&nbsp;'.__('You entered filename of file uploaded through upload interface'), 'top');
	}																		//End Insert list uploaded files
	if ($system->checkForRight('GENERAL'))
	$arr_modes=array('html' => __('HTML'), 'text' => __('Text'), 'htmlbb' => __('bbCodes') . '+' . __('HTML'), 'php' => __('PHP'));
	else	$arr_modes=array('html' => __('HTML'), 'text' => __('Text'), 'htmlbb' => __('bbCodes') . '+' . __('HTML'));
	$frm->addrow(__('Mode'), 
	$frm->select_tag('mode', $arr_modes, $article['mode']), 'top');
	$frm->addrow(__('Date').' (yyyy-mm-dd hh:mm:ss)', $frm->text_box('time', gmdate("Y-m-d H:i:s",$article['time'])), 'top');
	$frm->addrow(__('Allow comments'), $frm->radio_button('comments', array('yes' => __('Allow'), 'no' => __('Disallow')), $article['comments']), 'top');
	$frm->show();
} elseif(!empty($b) || $c == '#hidden' || $c == '#root'){
	if($articles->setWorkContainer($c)){
		$frm = new InputForm ('', 'post', __('Submit'), __('Reset'));
		$frm->addbreak(__('List of articles'));
		$frm->hidden('c', $c);
		$frm->hidden('b', $b);
		if(($list = $articles->getArticles($b, false, false, false)) !== false){
			foreach (array_reverse($list, true) as $id => $article){
			$frm->addrow('<a href="' . RCMS_ROOT_PATH . '?module=articles&c=' . $c . '&b=' . $b . '&a=' . $article['id'] . '" target="_blank">' . $article['title'] . '</a>' . ' [' . user_create_link($article['author_name'], $article['author_nick'], '_blank') . '] [' . rcms_format_time('d F Y H:i:s', $article['time']) . ']',
				$frm->checkbox('delete[' . $article['id'] . ']', '1', __('Delete')) . $frm->radio_button('a', array($article['id'] => __('Edit'))) . $frm->radio_button('move', array($article['id'] => __('Move')))
				);
			}
		} else rcms_showAdminMessage($articles->last_error);
		$frm->show();
	} else rcms_showAdminMessage($articles->last_error);
}
?>