<?php

/*
 * Copyright (C) 2017 KAINOTOMO PH LTD <info@kainotomo.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

defined('_JEXEC') or die;

use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\Database\UTF8MB4SupportInterface;

/**
 * Script file of SPTts component
 */
class com_spStgSrvInstallerScript {

        /**
         * The Extension Version we are updating from
         *
         * @var    string
         * @since  3.7
         */
        protected $fromVersion = null;

        function __construct() {
                //import KAINOTOMO PH LTD libraries and language
                //\JFactory::getLanguage()->load('lib_kainotomo', JPATH_SITE);
        }

        /**
         * method to install the component
         *
         * @return void
         */
        function install($parent) {
                // $parent is the class calling this method

                /*
                  //Decide if existing table jos_sptts
                  $jAp=& \JFactory::getApplication();
                  $db =& \JFactory::getDBO();
                  $query = "
                  SELECT COUNT(*)
                  FROM ".$db->nameQuote('#__sptts')."
                  ";
                  $db->setQuery($query);
                  $count = $db->loadResult();
                  if ($count == 0) { //proceed if new table
                  //Copy all users
                  $query = "select `id` from `#__users`";
                  $db->setQuery($query);
                  $result = $db->query();
                  if (is_null($column= $db->loadObjectList())) {$jAp->enqueueMessage(nl2br($db->getErrorMsg()),'error'); return;}

                  foreach($column as $i => $item) {
                  $query = "INSERT INTO `#__sptts` (`id`, `sendEmail`) VALUES ('.$item->id.', '1');";
                  $db->setQuery($query);
                  if (!$db->query()) {$jAp->enqueueMessage(nl2br($db->getErrorMsg()),'error'); return;}
                  }
                  }
                 * 
                 */
                //$parent->getParent()->setRedirectURL('index.php?option=com_spstgmng');
        }

        /**
         * method to uninstall the component
         *
         * @return void
         */
        function uninstall($parent) {
                // $parent is the class calling this method
                echo '<p>' . JText::_('COM_SPSTGMNG_UNINSTALL_TEXT') . '</p>';
        }

        /**
         * method to update the component
         *
         * @return void
         */
        function update($parent) {
                /*
                  // $parent is the class calling this method

                  // Get the params and set the new values
                  $params = JComponentHelper::getParams('com_spstgsrv');
                  $params->set('hash', 'xxx');

                  // Get a new database query instance
                  $db = \JFactory::getDBO();
                  $query = $db->getQuery(true);

                  // Build the query
                  $query->update('#__extensions AS a');
                  $query->set('a.params = ' . $db->quote((string) $params));
                  $query->where('a.element = "com_spstgsrv"');

                  // Execute the query
                  $db->setQuery($query);
                  $db->query();

                  $conf = \JFactory::getConfig();

                  $options = array(
                  'defaultgroup' => '_system',
                  'cachebase' => $conf->get('cache_path', JPATH_SITE . '/cache')
                  );

                  $cache = JCache::getInstance('callback', $options);
                  $cache->clean();
                 * 
                 */

                /*
                  //Decide if existing table jos_sptts
                  $jAp=& \JFactory::getApplication();
                  $db =& \JFactory::getDBO();
                  $query = "
                  SELECT COUNT(*)
                  FROM ".$db->nameQuote('#__sptts')."
                  ";
                  $db->setQuery($query);
                  $count = $db->loadResult();
                  if ($count == 0) { //proceed if new table
                  //Copy all users
                  $query = "select `id` from `#__users`";
                  $db->setQuery($query);
                  $result = $db->query();
                  if (is_null($column= $db->loadObjectList())) {$jAp->enqueueMessage(nl2br($db->getErrorMsg()),'error'); return;}

                  foreach($column as $i => $item) {
                  $query = "INSERT INTO `#__sptts` (`id`, `sendEmail`) VALUES ('.$item->id.', '1');";
                  $db->setQuery($query);
                  if (!$db->query()) {$jAp->enqueueMessage(nl2br($db->getErrorMsg()),'error'); return;}
                  }
                  }
                 * 
                 */
                //echo '<p>' . JText::_('COM_SPSTGMNG_UPDATE_TEXT') . '</p>';
        }

        /**
         * method to run before an install/update/uninstall method
         *
         * @return void
         */
        public function preflight($action, $installer) {
                // $parent is the class calling this method
                // $type is the type of change (install, update or discover_install)
                //echo '<p>' . JText::_('COM_SPTTS_PREFLIGHT_' . $type . '_TEXT') . '</p>';
        }

        /**
         * method to run after an install/update/uninstall method
         *
         * @return void
         */
        function postflight($action, $installer) {

                /*
                  // $parent is the class calling this method
                  // $type is the type of change (install, update or discover_install)
                  //echo '<p>' . JText::_('COM_SPDIGITALGOODS_POSTFLIGHT_' . $type . '_TEXT') . '</p>';
                  //Add initial directory
                  $jAp = \JFactory::getApplication();

                  //Update client_id in extensions table to enable online upgrade
                  $db = \JFactory::getDBO();
                  $query = "UPDATE `#__extensions` SET `client_id` = '0'  WHERE `name` ='com_spstgsrv';";
                  $db->setQuery($query);
                  try {
                  $db->execute();
                  } catch (Exception $exc) {
                  $jAp->enqueueMessage(nl2br($exc->getMessage()), 'error');
                  return;
                  }

                  //Activate plugins
                  $query = 'UPDATE ' . $db->quoteName('#__extensions');
                  $query .= ' SET `enabled` = ' . $db->quote('1');
                  $query .= ' WHERE ' . $db->quoteName('element') . ' LIKE ' . $db->quote('spstgsrv');
                  $db->setQuery($query);
                  try {
                  $db->execute();
                  } catch (Exception $exc) {
                  $jAp->enqueueMessage(nl2br($exc->getMessage()), 'error');
                  return;
                  }
                 * 
                 */

                //Delete back end menus of com_sptransfer
                /*
                  $db->setQuery('DELETE FROM `#__menu` WHERE `title` LIKE "COM_SPTRANSFER%";');
                  if (!$db->query())
                  \JFactory::getApplication()->enqueueMessage($db->getErrorMsg(), 'error');
                 * 
                 */
        }

}
