<?php

defined('_IN_JOHNADM') 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\EnvironmentInterface $env */
$env = $container->get(Johncms\Api\EnvironmentInterface::class);

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

/** @var Johncms\Api\ConfigInterface $config */

$config = $container->get('config')['johncms']; 

if ($systemUser->rights < 9) {
    header('Location: ../?err');
    exit;
} 
$from = isset($_GET['from']) ? trim($_GET['from']) : '';
$id = isset($_GET['id']) ? abs(intval($_GET['id'])) : 0;


switch ($from) {

    default:
    echo '<div class="mainblok">'; 
    if (isset($_POST['submit'])) {
            $label = htmlentities($_POST['label'], ENT_QUOTES, 'UTF-8');
            $name = htmlentities($_POST['name'], ENT_QUOTES, 'UTF-8');
            $co = $db->query("select count(1) from `prefix_forum` ");
            $mo = $co->fetch();
            $max = $mo[0];

            $db->exec("INSERT INTO `prefix_forum` SET `pos`='" . ($max + 1) .
                "' , `label`='" . $label . "', `name`='" . $name . "';");

echo '<div class="phdr"><a href="index.php"><b>Admin Panel</b></a> | Add Prefix </div>';
            echo '<div class="gmenu">Prefix successfully added. </div>';
            echo '<div class="phdr"><a href="index.php?act=prefix"><b>Back</b></a></div>';
   } else {
    echo '<div class="phdr"><a href="index.php"><b>Admin Panel</b></a> | Add Prefix</div>';
    echo '<form action="index.php?act=prefix" method="post">';
    echo '<div class="gmenu"><b>Name :</b><br /><input type="text" name="name"/><br/>';

   echo '<b>Span Class label_1 - 6:<br /><input type="text" name="label" value="label_1" /><br/><input type="submit" name="submit" value="Add" /></div></form>';
    echo '<div class="phdr">Example</div>';
    echo '<div class="topmenu">';
    echo '<span class="label_1">label_1</span>  ';
    echo '<span class="label_2">label_2</span>  ';
    echo '<span class="label_3">label_3</span>  ';
    echo '<span class="label_4">label_4</span>  ';
    echo '<span class="label_5">label_5</span>  ';
    echo '<span class="label_6">label_6</span>  ';
    echo '</div>';
    echo '<div class="phdr">List Prefix</div>';
$sql_p = $db->query("SELECT * FROM `prefix_forum` ORDER BY `id`");
            if ($sql_p) {
                while ($co = $sql_p->fetch()) {
                    echo '<div class="list2"><table width="100%" cellpadding="0" cellspacing="0"><tr><td width="auto"><span class="' . $co['label'] . '">' . $co['name'] . '</span></td><td width="auto" align="right"><a href="index.php?act=prefix&amp;from=edit&amp;id=' . $co['id'] . '"> <button class="aan-red"> <i class="icon-pencil"></i> </button> </a>   <a href="index.php?act=prefix&amp;from=dell&amp;id=' . $co['id'] . '"> <button class="aan-red"> <i class="icon-trash"></i> </button> </a></td></tr></table></div>';
                }
            } else {
               echo '<div class="rmenu">Not Found Prefix</div>';
               echo '<div class="phdr"><a href="index.php?act=prefix"><b>Back</b></a></div>';
            }
     echo '<div class="phdr"><a href="index.php"><b>Back</b></a></div>';
    }
    echo '</div>';
   break;

case 'dell':
       //$id = intval($_GET['id']);
       if ($id) {
           $db->exec("DELETE FROM `prefix_forum` WHERE `id`='" . $id . "';");
            echo '<div class="mainblok">';
            echo '<div class="phdr"><a href="index.php"><b>Admin Panel</b></a> | Delete Prefix </div>'; 
            echo '<div class="rmenu"> The prefix was deleted successfully. </div>';
            echo '<div class="phdr"><a href="index.php?act=prefix"><b>Back</b></a></div>';
           echo '</div>';
        } else {             
            echo '<div class="mainblok">';
            echo '<div class="phdr"><a href="index.php"><b>Admin Panel</b></a> | Delete Prefix </div>'; 
            echo '<div class="rmenu"> Please select file to deleted. </div>';
            echo '<div class="phdr"><a href="index.php?act=prefix"><b>Back</b></a></div>';
           echo '</div>';
        }
        break;
    case 'edit':
       //$id = intval($_GET['id']);
        echo '<div class="mainblok">';
        if (isset($_POST['submit'])) {
           $label = htmlentities($_POST['label'], ENT_QUOTES, 'UTF-8');
           $name = htmlentities($_POST['name'], ENT_QUOTES, 'UTF-8');
           $db->exec("UPDATE `prefix_forum` SET `label`='" . $label . "', `name`='" . $name . "' WHERE `id`='" . $id . "';");
           echo '<div class="phdr"><a href="index.php"><b>Admin Panel</b></a> | Edit Prefix </div>';
          echo '<div class="gmenu">Prefix successfully added. </div>';
          echo '<div class="phdr"><a href="index.php?act=prefix"><b>Back</b></a></div>';
        } else {
            $m_group = $db->query("SELECT * FROM `prefix_forum` WHERE `id`='" . $id .
                "' LIMIT 1;");
            $m_group_n = $m_group->fetch();
            echo '<div class="phdr"><a href="index.php"><b>Admin Panel</b></a> | Edit Prefix</div>';
            echo '<form action="index.php?act=prefix&amp;from=edit&amp;id=' . $id . '" method="post">';
            echo '<div class="gmenu"><b>Name :</b><br /><input type="text" name="name" value="' . $m_group_n['name'] . '" /><br/>';

           echo '<b>Span Class label_1 - 6:<br /><input type="text" name="label" value="' . $m_group_n['label'] . '" /><br/><input type="submit" name="submit" value="Save" /></div></form>';
            echo '<div class="phdr">Example</div>';
            echo '<div class="topmenu">';
            echo '<span class="label_1">label_1</span>';
            echo '<span class="label_2">label_2</span>';
            echo '<span class="label_3">label_3</span>';
            echo '<span class="label_4">label_4</span>';
            echo '<span class="label_5">label_5</span>';
            echo '<span class="label_6">label_6</span>';
            echo '</div>';
echo '<div class="phdr"><a href="index.php"><b>Back</b></a></div>';
}
echo '</div>';
break;

}

?>





