<?php
defined('_JEXEC') or die('Restricted access');

/**
* @package OS CCK
* @copyright 2020 OrdaSoft.
* @author Andrey Kvasnevskiy(akbet@mail.ru),Roman Akoev (akoevroman@gmail.com)
* @link http://ordasoft.com/cck-content-construction-kit-for-joomla.html
* @description OrdaSoft Content Construction Kit
*/

require_once(JPATH_SITE . "/components/com_os_cck/functions.php");

function com_uninstall()
{

    $db = JFactory::getDBO();
    $os_cck_configuration = $GLOBALS['os_cck_configuration'] = JComponentHelper::getParams('com_os_cck');
    $conf = JFactory::getConfig();

    if(!file_exists(JPATH_SITE . '/images/os-cck')){
        recursiveRemoveDir(JPATH_SITE . '/images/os-cck');
    }
    
    if($os_cck_configuration->get('save_database','0'))return;
    $query = "SHOW TABLES";
    $db->setQuery($query);
    $tables = (version_compare(JVERSION, "3.0.0", "lt")) ? $db->loadResultArray() : $db->loadColumn();
    $os_tables = array();

    foreach ($tables as $table) {
        if (preg_match("/^" . $conf->get('dbprefix') . "os_cck.*$/", $table)) {
            $os_tables[] = $table;
        }
    }

    if (count($os_tables) > 0) {
        $query = "DROP TABLE " . implode(", ", $os_tables);
        $db->setQuery($query);
        $db->execute();
    }

    echo "Uninstalled! ";
}

//(previously declared in /functions.php
// function recursiveRemoveDir($dir) {

//   $includes = new FilesystemIterator($dir);

//   foreach ($includes as $include) {

//     if(is_dir($include) && !is_link($include)) {

//       recursiveRemoveDir($include);
//     }

//     else {

//       unlink($include);
//     }
//   }

//   rmdir($dir);
// }

?>

