<?require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_before.php");?>
<?
global $USER;
if( !$USER->IsAdmin() ) die('������������ ���� ��� �������');
$userID = $USER->GetID();
$forumId = isset($_POST['FORUM_ID']) ? $_POST['FORUM_ID'] : false;
$blogId = isset($_POST['BLOG_ID']) ? $_POST['BLOG_ID'] : false;
$bOnlyIPComments = isset($_POST['ONLY_IP']);
$userIP = \Bitrix\Main\Service\GeoIp\Manager::getRealIp();
?>

<style>
body {
	display: flex;
	align-items: center;
	justify-content: center;
	flex-wrap: wrap;
}
.text-center {text-align: center;font-size: 20px;line-height: 26px;}
.reviews-migrate {
	border: 1px solid #ebebeb;
	border-radius: 4px;
	padding: 50px;
	display: flex;
	flex-direction: column;
	box-shadow: 0px 0px 27px rgba(0,0,0,0.1);
}
.reviews-migrate__title {font-size: 30px;font-weight: bold;color: #333;margin-bottom: 30px;}
.reviews-migrate__label {margin-bottom: 5px;}
.reviews-migrate__input {margin-bottom: 15px;line-height: 20px;font-size: 16px;padding: 6px 14px;}
.reviews-migrate__button {
	margin-top: 10px;
	background: #4f82ba;
	border-radius: 4px;
	text-align: center;
	color: white;
	padding: 12px;
	font-size: 20px;
	cursor: pointer;
}
</style>

<?if(!$forumId || !$blogId):?>
	<form method="post" novalidate="novalidate" class="reviews-migrate">
		<div class="reviews-migrate__title">������� �������</div>

		<label class="reviews-migrate__label">
			<a class="reviews-migrate__link" target="_blank" href="/bitrix/admin/forum_admin.php?lang=ru">ID ������</a>
		</label>
		<input type="text" name="FORUM_ID" value="" class="reviews-migrate__input">

		<label class="reviews-migrate__label">
			<a class="reviews-migrate__link" target="_blank" href="/bitrix/admin/blog_blog.php?lang=ru">ID �����</a>
		</label>
		<input type="text" name="BLOG_ID" value="" class="reviews-migrate__input">

		<label class="reviews-migrate__label">
			<input type="checkbox" name="ONLY_IP" class="reviews-migrate__input"> ��������� ������ ������ � IP �������
		</label>

		<input class="reviews-migrate__button" type="submit" value="���������" >
	</form>
<?else:?>
<div>
<?if($_SESSION['NEW_REVIEWS_CONVERTED']) {
		foreach($_SESSION['NEW_REVIEWS_CONVERTED'] as $converted) {
			if($converted['forum'] == $forumId && $converted['blog'] == $blogId) {
				echo '<div class="text-center">��� ������ <b>'.$forumId.'</b> � ����� <b>'.$blogId.'</b> ������ ��� ���������� ('.$converted['count'].').</div>';
				die();
			}
		}
	}
	if(!CModule::IncludeModule('forum')) {
		echo '�� ���������� ������ <a target="_blank" href="/bitrix/admin/module_admin.php?lang=ru">�����</a>!';
		die();
	}
	if(!CModule::IncludeModule('blog')) {
		echo '�� ���������� ������ <a target="_blank" href="/bitrix/admin/module_admin.php?lang=ru">����</a>!';
		die();
	}
	if(!CModule::IncludeModule('iblock')) {
		echo '�� ���������� ������ <a target="_blank" href="/bitrix/admin/module_admin.php?lang=ru">�������������� �����</a>!';
		die();
	}
	?>


	<?
	// ���-�� ������������ ��� IP ������
	$iCommentsWithoutIP = $iCommentsWithoutIPAdded = 0;

	$resForumTopics = CForumTopic::GetList(array("ID"=>"ASC"),  array('FORUM_ID' => $forumId));
	while( $forumTopic = $resForumTopics->Fetch() ) {
		$productId = explode('_', $forumTopic['XML_ID'])[1];
		$userStartID = CUser::GetByID($forumTopic['USER_START_ID'])->SelectedRowsCount() > 0 ? $forumTopic['USER_START_ID'] : $userID;
		$arForumTopics[$forumTopic['ID']] = array(
			'ID' => $forumTopic['ID'],
			'TITLE' => $forumTopic['TITLE'],
			'PRODUCT_ID' => $productId,
			'USER_START_ID' => $userStartID,
			'START_DATE' => $forumTopic['START_DATE'],
		);
		$arProductsIds[] = $productId;
	}

	if($arForumTopics) {
		$resForums = CForumMessage::GetList(array("ID"=>"ASC"), array('FORUM_ID' => $forumId));
		$bForumExist = false;
		while( $forum = $resForums->Fetch() ) {
			// �������� �� IP
			if( !$bOnlyIPComments && !$forum['AUTHOR_IP'] ){
				$forum['AUTHOR_IP'] = $userIP;
				$iCommentsWithoutIP++;
				$iCommentsWithoutIPAdded++;
			}

			if( !$bOnlyIPComments && !$forum['AUTHOR_REAL_IP'] ){
				$forum['AUTHOR_REAL_IP'] = $userIP;
			}

			if( isset($arForumTopics[$forum['TOPIC_ID']]) && $forum['AUTHOR_IP'] ) {
				$authorID = CUser::GetByID($forum['AUTHOR_ID'])->SelectedRowsCount() > 0 ? $forum['AUTHOR_ID'] : $userID;

				$arForumTopics[$forum['TOPIC_ID']]['ITEMS'][$forum['ID']] = array(
					'ID' => $forum['ID'],
					'AUTHOR_ID' => $authorID,
					'AUTHOR_NAME' => $forum['AUTHOR_NAME'],
					'AUTHOR_EMAIL' => $forum['AUTHOR_EMAIL'],
					'AUTHOR_IP' => $forum['AUTHOR_IP'],
					'AUTHOR_IP1' => $forum['AUTHOR_REAL_IP'],
					'DATE_CREATE' => $forum['POST_DATE'],
					'POST_TEXT' => $forum['POST_MESSAGE'],
				);
				$bForumExist = true;
			}else{
				if( !$forum['AUTHOR_IP'] ) $iCommentsWithoutIP++;
			}
		}	

		if(!$bForumExist) {
			echo '��� ��������� ������ ��� ��������. ���� �� � ����� �� ������ �����.';
			die();
		}
	} else {
		echo '��� ��������� ��� �� ������.';
		die();
	}

	if($arProductsIds) {
		$resProducts = CIBlockElement::GetList(array(), array('ID' => $arProductsIds), false, false, array('DETAIL_PAGE_URL', 'PREVIEW_TEXT'));
		while( $product = $resProducts->GetNext() ) {
			$arProducts[$product['ID']] = $product;
		}

		if(!$arProducts) {
			echo '��� �������, ��������� � ������ ������.';
			die();
		}
	}


	$blogUrl = CBlog::GetByID($blogId)['URL'];

	foreach($arForumTopics as $topicId => $topic) {
		$detailText = '[URL='.$_SERVER['HTTP_ORIGIN'].$arProducts[$topic['PRODUCT_ID']]['DETAIL_PAGE_URL'].']'.$topic['TITLE'].'[/URL]'.PHP_EOL.$arProducts[$topic['PRODUCT_ID']]['PREVIEW_TEXT'].PHP_EOL;
		$fields = array(
			'TITLE' => $topic['TITLE'],
			'BLOG_ID' => $blogId,
			'AUTHOR_ID' => $topic['USER_START_ID'],
			'DETAIL_TEXT' => $detailText,
			'DATE_CREATE' => $topic['START_DATE'],
			'DATE_PUBLISH' => $topic['START_DATE'],
			'PUBLISH_STATUS' => 'P',
		);

		$postId = CBlogPost::Add($fields);
		if($postId) {
			CIBlockElement::SetPropertyValuesEx( $topic['PRODUCT_ID'], $arProducts[$topic['PRODUCT_ID']]['IBLOCK_ID'], array('BLOG_POST_ID' => $postId) );
			$arForumTopics[$topicId]['POST_ID'] = $postId;
			if($topic['ITEMS']) {
				foreach($arForumTopics[$topicId]['ITEMS'] as $commentId => $comment) {
					$fields = array(
						'BLOG_ID' => $blogId,
						'POST_ID' => $postId,
						'AUTHOR_IP' => $comment['AUTHOR_IP'],
						'AUTHOR_IP1' => $comment['AUTHOR_IP1'],
						'DATE_CREATE' => $comment['DATE_CREATE'],
						'POST_TEXT' => '<comment>'.$comment['POST_TEXT'].'</comment>',
						'PATH' => $_SERVER['HTTP_ORIGIN'].$arProducts[$topic['PRODUCT_ID']]['DETAIL_PAGE_URL'].'?commentId=#comment_id###comment_id#',
						'URL' => $blogUrl,
					);
					if($comment['AUTHOR_ID'] && $comment['AUTHOR_ID'] !== $userID && CUser::GetByID($comment['AUTHOR_ID'])->SelectedRowsCount() > 0 ) {
						$fields['AUTHOR_ID'] = $comment['AUTHOR_ID'];
					} else {
						if($comment['AUTHOR_NAME']) {
							$fields['AUTHOR_NAME'] = $comment['AUTHOR_NAME'];
						}
						if($comment['AUTHOR_EMAIL']) {
							$fields['AUTHOR_EMAIL'] = $comment['AUTHOR_EMAIL'];
						}
					}
					$newCommentFields[] = $fields;
					$newCommentId[] = CBlogComment::Add($fields);
					if ($ex = $GLOBALS['APPLICATION']->GetException())
						echo '<div>��������� ����� '.$topic['TITLE'].' : '.$comment['POST_TEXT'].' �� ���� ���������, '.$ex->GetString().'</div>';
				}
			}
		} else {
			if ($ex = $GLOBALS['APPLICATION']->GetException())
				echo '<div>���� ����� '.$topic['TITLE'].' �� ���� ���������, '.$ex->GetString().'</div>';
		}
	}

	$_SESSION['NEW_REVIEWS_CONVERTED'][$forumId.'_'.$blogId] = array(
		'forum' => $forumId,
		'blog' => $blogId,
		'count' => count($newCommentId),
	);

	if($newCommentId):?>
		<?php if( $iCommentsWithoutIP > 0 ): ?>
			<div class="text-center">������� ��� IP ������ ���������: <?= $iCommentsWithoutIP; ?></div>
			<br>
		<?php endif; ?>

		<?php if( $iCommentsWithoutIPAdded > 0 ): ?>
			<div class="text-center">������� ��� IP ������ ����������: <?= $iCommentsWithoutIPAdded; ?></div>
			<br>
		<?php endif; ?>

		<div class="text-center">�������! ���������� �������: <?=count($newCommentId)?>.<br>�� �������� ������� ���� BlogsCreater.php!</div>
	<?endif;?>
</div>
<?endif;?>