<?php
use Joomla\CMS\Factory;
use Joomla\CMS\Uri\Uri;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\CMS\HTML\HTMLHelper;
/**
 * @version        1.14.0
 * @package        Joomla
 * @subpackage     EDocman
 * @author         Tuan Pham Ngoc
 * @copyright      Copyright (C) 2011 - 2024 Ossolution Team
 * @license        GNU/GPL, see LICENSE.php
 */
defined('_JEXEC') or die();

class EDocmanViewEditcategoryHtml extends OSViewItem
{
	function prepareView()
	{
        parent::prepareView();
        $db                         = Factory::getContainer()->get('db');
		$query						= $db->getQuery(true);
		$user						= Factory::getApplication()->getIdentity();
        $app                        = Factory::getApplication();
        $this->config               = EDocmanHelper::getConfig();
        $catid                      = $app->getInput()->getInt('catid',0);
		$addsubcat					= $app->getInput()->getInt('addsubcat',0);
		$newcat						= $app->getInput()->getInt('newcat',0);
        if (!$user->get('id'))
        {
            // Allow users to login
            $return                 = base64_encode(Uri::getInstance()->toString());
            Factory::getApplication()->redirect('index.php?option=com_users&view=login&return=' . $return);
        }
		
		if($addsubcat == 0)
		{
			if($newcat == 1)
			{
				$this->model->set('id', 0);
			}
			else
			{
				$id = $this->model->getState()->id;
			}
		}
		else
		{
			$this->model->set('id', 0);
		}
        if (!$id)
        {
            //new campaign
            $ret                    = $user->authorise('core.create', 'com_edocman');
			if(! $ret)
			{
				$ret                = $user->authorise('edocman.manage_categories', 'com_edocman');
			}
            if(! $ret)
            {
				if($catid > 0 && $addsubcat == 1)
				{
					if(!$user->authorise('edocman.manage_subcategories', 'com_edocman.category.'.$catid))
					{
						$app->enqueueMessage(Text::_('EDOCMAN_NOT_ALLOWED_ACTION'));
						$url = Route::_(Uri::root());
						$app->redirect($url);
					}
				}
				else
				{
					$app->enqueueMessage(Text::_('EDOCMAN_NOT_ALLOWED_ACTION'));
					$url = Route::_(Uri::root());
					$app->redirect($url);
				}
            }
        }
        else
        {
			$query->clear();
			$query->select('parent_id')->from('#__edocman_categories')->where('id='.$id);
			$db->setQuery($query);
			$parent_id = (int) $db->loadResult();

            //edit campaign
            $ret     = $user->authorise('core.edit', 'com_edocman');
			if(! $ret)
			{
				$ret                = $user->authorise('edocman.manage_categories', 'com_edocman');
			}
            //only allow edit campaign when user has permission and is owner of campaign
            if(! $ret )
            {
				if($parent_id > 0)
				{
					if(!$user->authorise('edocman.manage_subcategories', 'com_edocman.category.'.$parent_id))
					{
						$app->enqueueMessage(Text::_('EDOCMAN_NOT_ALLOWED_ACTION'));
						$url = Route::_(Uri::root());
						$app->redirect($url);
					}
				}
				else
				{
					$app->enqueueMessage(Text::_('EDOCMAN_NOT_ALLOWED_ACTION'));
					$url = Route::_(Uri::root());
					$app->redirect($url);
				}
            }
        }

        $document = Factory::getApplication()->getDocument();
        if($this->item->id > 0 && $addsubcat == 0)
        {
            $headerText = str_replace('CATEGORY_TITLE', $this->item->title, Text::_('EDOCMAN_EDIT_CATEGORY'));
            $document->setTitle($headerText);
        }
        else
        {
            $headerText = Text::_('EDOCMAN_ADD_CATEGORY');
            $document->setTitle($headerText);
        }

        if($catid > 0)
        {
            //$query = $db->getQuery(true);
            $query->clear();
            $query->select('title')
                ->from('#__edocman_categories')
                ->where('id=' . $catid);
            $db->setQuery($query);
            $parentCategory = $db->loadResult();
            $this->parentId = $catid;
            $this->parentCategoryTitle = $parentCategory;
        }
		else
		{
			$this->parentId = 0;
			$this->parentCategoryTitle = '';
		}

		if((int)$this->item->id == 0)
		{
			$this->item->published = 1;
		}

		if ($this->item->id && PluginHelper::isEnabled('edocman', 'notification'))
		{
			$this->item->notify_group_ids = explode(',', $this->item->notify_group_ids);
		}
		$this->item->groups = array();
		if($this->item->id > 0) 
		{
			$groups = EDocmanHelper::getGroupLevels(0, $this->item->id);
			if($groups != '')
			{
				$this->item->groups = explode(",",$groups);
			}
		}

		$optionArr = [];
		$optionArr[] = HTMLHelper::_('select.option','default','Default');
		$optionArr[] = HTMLHelper::_('select.option','table','Table');
		$optionArr[] = HTMLHelper::_('select.option','column','Column');
		$optionArr[] = HTMLHelper::_('select.option','blog','Blog');
		$this->lists['category_layout'] = HTMLHelper::_('select.genericlist', $optionArr , 'jform[category_layout]', 'class="form-select input-large"','value', 'text', $this->item->category_layout);


		$option = array();
		$option[] = HTMLHelper::_('select.option', 1 , Text::_('EDOCMAN_PUBLISHED'));
		$option[] = HTMLHelper::_('select.option', 0 , Text::_('EDOCMAN_UNPUBLISHED'));
		$this->lists['published'] = HTMLHelper::_('select.genericlist',$option, 'jform[published]', 'class="inputbox form-select input-small form-control"','value','text',(int)$this->item->published);

		$this->addsubcat= $addsubcat;
        $this->form = $this->model->getForm();
		if($addsubcat == 1 || $newcat == 1)
		{
			$this->item = [];
		}
        $this->bootstrapHelper = new EDocmanHelperBootstrap($this->config->twitter_bootstrap_version);
	}
}
