<?php

/*
 * @component UserPoints
 * @copyright Copyright (C) 2015-2022 Bernard Gilly - Adrien Roussel - Martin Brampton
 * @license : GNU/GPL
 * @Website : https://remository.com
 */

namespace BlackSheepResearch\Component\UserPoints\Administrator\Models;

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();

use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Filesystem\Folder;
use Joomla\CMS\Installer\Installer;
use BlackSheepResearch\Component\UserPoints\Administrator\Model;
use BlackSheepResearch\Component\UserPoints\Site\Problems\Rule;
use BlackSheepResearch\Component\UserPoints\Site\Problems\SQL;

class Plugins extends Model {

    function __construct() {
        parent::__construct();
    }

    function _loadPluginElements($xmlFile) {

        if (substr(strtolower($xmlFile), -4) != ".xml") {
            return;
        }

        $app = Factory::getApplication();

        $error = "";

        $_xmlFile = JPATH_COMPONENT_ADMINISTRATOR . '/assets/plugins/' . $xmlFile;
        $xmlDoc = simplexml_load_file($_xmlFile);
        $rule = $xmlDoc->rule;
        $pos = strrpos($_xmlFile, '/');
        if ($pos) {
            $_installPath = substr($_xmlFile, 0, $pos);
        }

        if ($xmlDoc) {
            if (in_array($xmlDoc->getName(), ['userpoints', 'altauserpoints', 'alphauserpoints'])) {
                $nameRule = addslashes((string) $rule->name);
                $descriptionRule = addslashes((string) $rule->description);
                $componentRule = (string) $rule->component;
                $pluginRule = (string) $rule->plugin_function;
                $fixedpointsRule = intval(empty($rule->fixed_points) OR 'true' === trim(strtolower($rule->fixed_points)));
                $categoryRule = (string) $rule->category;
                $displayMessage = intval(empty($rule->display_message) OR 'true' === trim(strtolower($rule->display_message)));
                $emailNotification = intval(empty($rule->email_notification) OR 'true' === trim(strtolower($rule->email_notification)));

                // insert in table
                if ($nameRule != '' && $descriptionRule != '' && $componentRule != '' && $pluginRule != '') {

                    // check if already exist...					
                    if (!(Rule::withRuleName($pluginRule))) {
                        $query = "INSERT INTO #__alpha_userpoints_rules VALUES (0, '" . $nameRule . "', '" . $descriptionRule . "', '" . $componentRule . "', '" . $pluginRule . "', '1', '" . $componentRule . "', '', '', '0', '0', 0, '1000-01-01 00:00:00', '', '', '', '', '0', '0', '0', '0', '1', '" . $fixedpointsRule . "', '" . $categoryRule . "', '" . $displayMessage . "', '', '0', '" . $emailNotification . "', '', '', '0', '0', '0', '0', '0', '1')";
                        $db = SQL::getInstance();
                        $db->setQuery($query);
                        if ($db->execute()) {
                            Rule::reset();
                            $msg = Text::_('AUP_NEW_RULE_INSTALLED_SUCCESSFULLY') . ' : <b>' . $nameRule . '</b>';
                            $app->enqueueMessage($msg);
                        } else {
                            $error = Text::_('This rule is not installed properly');
                            $app->enqueueMessage($error, 'notice');
                        }
                    } else {
                        $error = Text::_('AUP_THISRULEALREADYEXIST');
                        $app->enqueueMessage($error, 'notice');
                    }
                } else {
                    $error = Text::_('AUP_XML_FILE_INVALID');
                    $app->enqueueMessage($error, 'warning');
                }
            } elseif ($xmlDoc->getName() == 'extension') {

                // Install standard plugins for Joomla!
                jimport('joomla.installer.installer');
                jimport('joomla.filesystem.folder');
                jimport('joomla.filesystem.file');

                $plugin_installer = new Installer;
                $plugin_installer->install($_installPath);

                Folder::delete($_installPath);

                $redirecturl = "index.php?option=com_plugins";
                $app->redirect($redirecturl);
                //JControllerLegacy::redirect();
            } else {
                unset($xmlDoc);
                $error = Text::_('AUP_XML_FILE_INVALID');
                $app->enqueueMessage($error, 'warning');
            }
        } else {
            unset($xmlDoc);
            $error = Text::_('AUP_XML_FILE_INVALID');
            $app->enqueueMessage($error, 'warning');
        }
    }

}
