<?php
/**
* Komento is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*/

use Joomla\CMS\Factory;
use Joomla\CMS\Installer\Installer;
use Joomla\CMS\Installer\InstallerHelper;
use Joomla\CMS\Language\Text;
use Joomla\Database\DatabaseDriver;

defined('_JEXEC') or die('Unauthorized Access');

require_once(__DIR__ . '/controller.php');

class KomentoControllerInstallToolbar extends KomentoSetupController
{
	/**
	 * Perform installation of Toolbar Package
	 *
	 */
	public function execute()
	{

		// Get the temporary path from the server.
		$tmpPath = $this->input->get('path', '', 'default');

		// There should be a queries.zip archive in the archive.
		$tmpToolbarPath = $tmpPath . '/pkg_toolbar.zip';

		$package = InstallerHelper::unpack($tmpToolbarPath);

        // REVISION for Joomla 6: Use manual database injection to avoid constructor object-to-string error.
        $db = Factory::getContainer()->get(DatabaseDriver::class);
		$installer = new Installer();
		$installer->setDatabase($db);

		$installer->setOverwrite(true);

		$state = $installer->install($package['dir']);

		if (!$state) {
			// If for some reason, the toolbar package failed, flag it.
			$this->updateConfig('toolbar_installation_failed', "1");

			$result = $this->getResultObj(Text::_('Unable to complete installation of Stackideas Toolbar. Manual installation of the module is required'), true);
			return $this->output($result);
		}

		$result = $this->getResultObj(Text::_('Stackideas Toolbar Package installed on the site'), true);

		return $this->output($result);
	}
}