<?php
/*
 * JohnCMS NEXT Mobile Content Management System (http://johncms.com)
 *
 * For copyright and license information, please see the LICENSE.md
 * Installing the system or redistributions of files must retain the above copyright notice.
 *
 * @link        http://johncms.com JohnCMS Project
 * @copyright   Copyright (C) JohnCMS Community
 * @license     GPL-3
 */

defined('_IN_JOHNCMS') or die('Error: restricted access');

/** @var Psr\Container\ContainerInterface $container */
$container = App::getContainer();

/** @var PDO $db */
$db = $container->get(PDO::class);

/** @var Johncms\Api\UserInterface $systemUser */
$systemUser = $container->get(Johncms\Api\UserInterface::class);

/** @var Johncms\Api\ToolsInterface $tools */
$tools = $container->get(Johncms\Api\ToolsInterface::class);

/** @var Johncms\Api\ConfigInterface $config */
$config = $container->get(Johncms\Api\ConfigInterface::class);

if (!$id && !$systemUser->isValid()) {
    require('../system/head.php');
    echo $tools->displayError(_t('Wrong data'));
    require('../system/end.php');
    exit;
}


$typ = $db->query("SELECT * FROM `forum` WHERE `id` = '$id'");
$ms = $typ->fetch(); 
if ($ms['type'] != 't') {
    require('../system/head.php');
    echo $tools->displayError(_t('Wrong data'));
    require('../system/end.php');
    exit;
}
 



if (isset($_POST['submit'])) {
    $nn = isset($_POST['nn']) ? $tools->checkout($_POST['nn']) : false;
    $error = [];
    if (!$nn) {
       $error[] = _t('You have not entered topic name');
     }
    if ($db->query("SELECT COUNT(*) FROM `forum` WHERE `type` = 't' AND `refid` = '".$ms['refid']."' AND `tags` = '$nn' LIMIT 1")->fetchColumn() > 0) {
    $error[] = _t('Topic with same name already exists in this section');
   }
   if (empty($error)) {
       $db->exec("update `forum` set  tags='" . $nn . "' where id='" . $id . "';");
        header("Location: index.php?id=$id");
   } else {
     require('../system/head.php');
     echo $tools->displayError($error);
     require('../system/end.php');
     exit;
   }
} else {
 
require('../system/head.php'); 
echo '<div class="mainblok">';
echo '<div class="phdr"><a href="index.php?id=' . $id . '"><b>Forum</b></a> | Tags for this Thread </div>';
echo '<div class="menu"><form action="index.php?act=tag&amp;id=' . $id . '" method="post">';
echo '<p><h3>Enter the search for the topic: "' . $ms['text'] . '"</h3>';
echo '<textarea type="text" name="nn" value="' . $ms['tags'] . '">' . $ms['tags'] . '</textarea></p>';
echo '<p><input type="submit" name="submit" value="Save"/></p>';
echo '</form></div>';
echo '<div class="rmenu"> You can add multiple tags by adding a comma (,) between the keywords. <br/>Note: Tags will be displayed for everyone and should be written in point, there should be no distance. </div>';
echo '<div class="phdr"><a href="index.php?id=' . $id . '">Back</a></div>';
echo '</div>';
}

?>