<?php
/*
	Copyright © Alexander Sunvas*
	http://eleanor-cms.ru
	a@eleanor-cms.ru
	*Pseudonym
*/
class TplForumPost
{
	public static $lang;

	public static function PostErrors(array$errors)
	{
		foreach($errors as $k=>&$v)
			if(is_int($k) and is_string($v) and isset(static::$lang[$v]))
				$v=static::$lang[$v];

		return Eleanor::$Template->ForumMenu()->Message($errors,'error');
	}

	public static function NewTopic($values,$forum,$rights,$prefixes,$bypost,$errors,$uploader,$voting,$captcha)
	{
		$nav=array();
		$forum['parents']=explode(',',$forum['parents']);
		$Forum = $GLOBALS['Eleanor']->Forum;
		foreach($forum['parents'] as $v)
			if(isset($Forum->Forums->dump[$v]))
				$nav[]=array($Forum->Links->Forum($v), $Forum->Forums->dump[$v]['title']);
		array_push($nav,array($Forum->Links->Forum($forum['id']),$forum['title']),array(false,'Создание темы'));

		$C=Eleanor::$Template->ForumMenu($nav,null);
		if($errors)
		{
			foreach($errors as $k=>&$v)
				if(is_int($k) and is_string($v) and isset(static::$lang[$v]))
					$v=static::$lang[$v];
			$C->Message($errors,'error');
		}

		$ti=1;
		$Lst=Eleanor::LoadListTemplate('table-form')->begin();
		if(!$Forum->user)
			$Lst->item('Ваше имя',Eleanor::Input('name',$values['name'],array('tabindex'=>$ti++)));

		$js='';
		if(is_array($values['title']))
		{
			$prefs=$title=$text=$descr=$checks=array();
			foreach($values['title'] as $l=>$v)
			{
				$prefo='';
				if(isset($prefixes[$l]))
					foreach($prefixes[$l] as $pref)
						$prefo.=Eleanor::Option($pref['title'],$pref['id'],$pref['id']==$values['prefix'][$l]);

				if($prefo)
					$prefs[$l]=Eleanor::Select('prefix['.$l.']',Eleanor::Option('-выберите префикс-',0).$prefo,array('tabindex'=>$ti++));

				$title[$l]=Eleanor::Input('title['.$l.']',$v,array('tabindex'=>$ti++));
				$descr[$l]=Eleanor::Input('description['.$l.']',$values['description'][$l],array('tabindex'=>$ti++));
				$text[$l]=$GLOBALS['Eleanor']->Editor->Area('text['.$l.']',$values['text'][$l],array('bypost'=>$bypost,'bb'=>array('tabindex'=>$ti++)));
				if(isset($uploader[$l]))
				{
					$text[$l].=$uploader[$l];
					$js.='FI'.$l.'.editor="text'.$l.'";';
				}

				$checks[]='<label>'.Eleanor::Check('langs[]',in_array($l,$values['langs']),array('tabindex'=>$ti++,'value'=>$l)).' '.Eleanor::$langs[$l]['name'].'</label>';
			}

			if($prefs)
				$Lst->item('Префикс темы',Eleanor::$Template->LangEdit($prefs,false));

			$Lst->item('Заголовок',Eleanor::$Template->LangEdit($title,false))
				->item('Описание',Eleanor::$Template->LangEdit($descr,false))
				->item('Текст',Eleanor::$Template->LangEdit($text,false));

			if(count($checks)>1)
			{
				$GLOBALS['jscripts'][]='js/multilang.js';
				$Lst->item('Для языков',join('<br />',$checks)
					.'<script type="text/javascript">/*<![CDATA[*/$(function(){new MultilangChecks({mainlang:"'.$Forum->language
					.'",langs:"[name=\"langs[]\"]",Switch:function(show,hide,where){
	for(var i in show)
		show[i]="."+show[i];
	for(var i in hide)
		hide[i]="."+hide[i];
	$(show.join(","),where).show().filter(show[0]).each(function(){
		try
		{
			this.Switch();
		}catch(e){}
	});
	if(show.length==1 && show[0]==".'.$GLOBALS['Eleanor']->Forum->language.'")
		hide.push(show[0]);
	$(hide.join(","),where).hide();
}});
});//]]></script>');
			}
		}
		else
		{
			$prefo='';
			foreach($prefixes as $id=>$pref)
				$prefo.=Eleanor::Option($pref,$id,$id==$values['prefix']);

			if($prefo)
				$Lst->item('Префикс темы',Eleanor::Select('prefix',Eleanor::Option('-выберите префикс-',0).$prefo,array('tabindex'=>$ti++)));

			$Lst->item('Заголовок',Eleanor::Input('title',$values['title'],array('tabindex'=>$ti++)))
				->item('Описание',Eleanor::Input('description',$values['description'],array('tabindex'=>$ti++)))
				->item('Текст',$GLOBALS['Eleanor']->Editor->Area('text',$values['text'],array('bypost'=>$bypost,'bb'=>array('tabindex'=>$ti++))).$uploader);

			if($uploader)
				$js.='FI.editor="text";';
		}

		if($captcha)
			$Lst->item('Введите код',$captcha.'<br />'.Eleanor::Input('check','',array('tabindex'=>$ti++)));

		if($Forum->user)
			$Lst->item('Подписка на тему',Eleanor::Select('subscription',Eleanor::Option('Подписка отсутствует',0,!$values['subscription']).Eleanor::OptGroup('Подписка с уведомлением',Eleanor::Option('Немедленным','i',$values['subscription']=='i').Eleanor::Option('Ежедневным','d',$values['subscription']=='d').Eleanor::Option('Еженедельным','w',$values['subscription']=='w').Eleanor::Option('Ежемесячным','m',$values['subscription']=='m')),array('tabindex'=>$ti++)));

		if($Forum->ugr['supermod'] or $forum['_moderator'] and in_array(1,$forum['_moderator']['chstatust']))
			$Lst->item('Установить статус',Eleanor::Select('status',Eleanor::Option('На модерации',-1,$values['status']==-1).Eleanor::Option('Блокировка',0,$values['status']==0).Eleanor::Option('Активировать',1,$values['status']==1),array('tabindex'=>$ti++)));

		if($Forum->ugr['supermod'] or $forum['_moderator'] and in_array(1,$forum['_moderator']['pin']))
			$Lst->item('Закрепить тему до',Dates::Calendar('pinned',$values['pinned'],true,array('tabindex'=>$ti++)));

		if($Forum->ugr['supermod'] or $forum['_moderator'] and in_array(1,$forum['_moderator']['opcl']) or in_array(1,$rights['close']))
			$Lst->item('Закрыть тему',Eleanor::Check('closed',$values['closed'],array('tabindex'=>$ti++)));

		$extra='';
		foreach($values['extra'] as $k=>$v)
			$extra.=Eleanor::Input('extra['.$k.']',$v,array('type'=>'hidden'));

		$topic=(string)$Lst->end();

		return$C.$Lst->form()
			->tabs(
				array('Тема',$topic),
				array('Опрос',$voting)
			)->submitline($extra.Eleanor::Button('Создать тему','submit',array('tabindex'=>$ti)))->endform()
			.($js ? '<script type="text/javascript">/*<![CDATA[*/$(function(){'.$js.'});//]]></script>' : '');
	}

	public static function AddEditPost($edit,$bypost,$values,$rights,$prights,$forum,$topic,$error,$hidden)
	{/*
		$Lst=Eleanor::LoadListTemplate('table-form');
		$nav='<nav><a href="'.$GLOBALS['Eleanor']->Url->Prefix().'">Форум</a>';
		$ids=$GLOBALS['Eleanor']->Forum->Forums->dump[$forum['id']]['parents'];
		if($ids)
		{
			$ids=explode(',',rtrim($ids,','));
			foreach($ids as &$v)
				$nav.=' &raquo; <a href="'.$GLOBALS['Eleanor']->Url->Construct($GLOBALS['Eleanor']->Forum->Forums->GetUrl($v),true,$GLOBALS['Eleanor']->Url->delimiter).'">'.$GLOBALS['Eleanor']->Forum->Forums->dump[$v]['title'].'</a>';
		}
		$fu=$GLOBALS['Eleanor']->Forum->Forums->GetUrl($forum['id']);
		$nav.=' &raquo; <a href="'.$GLOBALS['Eleanor']->Url->Construct($fu,true,$GLOBALS['Eleanor']->Url->delimiter).'">'.$forum['title'].'</a> &raquo; <a href="'.$GLOBALS['Eleanor']->Url->Construct(($GLOBALS['Eleanor']->Url->furl and $topic['url']) ? $fu+array('t'=>$topic['url']) : array(array('t'=>$topic['id']))).'">'.$topic['title'].'</a> &raquo; </nav>';
		$hiddens='';
		foreach($hidden as $k=>&$v)
			$hiddens.=Eleanor::Control('h['.$k.']','hidden',$v);

		$GLOBALS['Eleanor']->Editor->module=$GLOBALS['Eleanor']->module['name'];
		return Eleanor::$Template->ForumMenu()
			.$nav
			.($error ? Eleanor::$Template->Message($error) : '')
			.$Lst->form()
			.$Lst->begin()
			.$Lst->head($edit ? 'Редактирование поста' : 'Создание поста')
			.($GLOBALS['Eleanor']->Forum->user || $edit ? '' : $Lst->item('Ваше имя',Eleanor::Input('_name',$values['_name'])))
			.$Lst->item('Текст',$GLOBALS['Eleanor']->Editor->Area('text',$values['text'],array('bypost'=>$bypost,'bb'=>array('tabindex'=>1))).(isset($values['_uploader']) ? $values['_uploader'].'<script type="text/javascript">/*<![CDATA[* /$(function(){FI.editor="text"})//]]></script>' : ''))
			.(isset($values['edit_reason']) ? $Lst->item('Причина редактирования',Eleanor::Input('edit_reason',$values['edit_reason'],array('tabindex'=>2))) : '')
			.(($GLOBALS['Eleanor']->Forum->ugr['supermod'] or $forum['_moderator'] and in_array(1,$forum['_moderator']['chstatus']))
				? $Lst->item('Установить статус',Eleanor::Select('status',Eleanor::Option('На модерации',-1,in_array($values['status'],array(-1,-3))).Eleanor::Option('Блокировка',1,$values['status']==0).Eleanor::Option('Активировать',1,in_array($values['status'],array(1,-2))),array('tabindex'=>3)))
				: '')
			.(($GLOBALS['Eleanor']->Forum->ugr['supermod'] or $forum['_moderator'] and in_array(1,$forum['_moderator']['opcl']) or in_array(1,$rights['close']))
				? $Lst->item('Закрыть тему',Eleanor::Check('_closed',$values['_closed'],array('tabindex'=>4)))
				: '')
			.($GLOBALS['Eleanor']->Forum->user
				? $Lst->item('Подписка на тему',Eleanor::Select('_subscription',Eleanor::Option('Подписка отсутствует',0,!$values['_subscription']).Eleanor::OptGroup('Подписка с уведомлением',Eleanor::Option('Немедленным','i',$values['_subscription']=='i').Eleanor::Option('Ежедневным','d',$values['_subscription']=='d').Eleanor::Option('Еженедельным','w',$values['_subscription']=='w').Eleanor::Option('Ежемесячным','m',$values['_subscription']=='m')),array('tabindex'=>5)))
				: ''
			)
			.($GLOBALS['Eleanor']->Captcha->disabled ? '' : $Lst->item('Введите код',Eleanor::Input('_check','',array('tabindex'=>6)).$GLOBALS['Eleanor']->Captcha->GetCode()))
			.$Lst->button($hiddens.Eleanor::Button('Ok','submit',array('tabindex'=>7)).($edit ? ' '.Eleanor::Button(Eleanor::$Language['main']['delete'],'button',array('onclick'=>'window.location=\''.$GLOBALS['Eleanor']->Url->Construct(array('do'=>'delete','id'=>$values['id'])).'\'')) : ''))
			.$Lst->end()
			.$Lst->endform();
	*/}

	public static function AjaxEditPost($values,$hidden)
	{/*
		$hiddens='';
		foreach($hidden as $k=>&$v)
			$hiddens.=Eleanor::Control('h['.$k.']','hidden',$v);
		$GLOBALS['Eleanor']->Editor->module=$GLOBALS['Eleanor']->module['name'];
		return'<form method="post" action="'.$GLOBALS['Eleanor']->Url->Construct(array('do'=>'edit','id'=>$values['id']),true,'').'">'
			.$GLOBALS['Eleanor']->Editor->Area('text',$values['text'])
			.(isset($values['_uploader']) ? $values['_uploader'].'<script type="text/javascript">/*<![CDATA[* /$(function(){FI.editor="text"})//]]></script>' : '')
			.'<div class="clr"></div><div style="float:left">Причина: '.Eleanor::Input('edit_reason',$values['edit_reason']).'</div><div style="float:right">'
			.$hiddens.Eleanor::Button('Сохранить').' '.Eleanor::Button('Отменить','button',array('class'=>'fb-cancel')).' '.Eleanor::Button('Подробно...','submit',array('class'=>'fb-tofull','name'=>'_tofull')).'</div></form>';
	*/}

	public static function Edited($data)
	{/*
		return sprintf('%s этот пост отредактировал %s. Причина: %s',Eleanor::$Language->Date($data['edit_date'],'fdt'),$data['who_edit_id']>0 ? '<a href="'.Eleanor::$Login->UserLink(htmlspecialchars_decode($data['who_edit'],ELENT),$data['who_edit_id']).'">'.$data['who_edit'].'</a>' : $data['who_edit'],$data['edit_reason'] ? $data['edit_reason'] : '<i>Не указана</i>');
	*/}
}
TplForumPost::$lang=Eleanor::$Language->Load(dirname(__DIR__).'/langs/forum-user-post-*.php',false);