<?php
/** 
 * @package     VikRestaurants
 * @subpackage  core
 * @author      E4J s.r.l.
 * @copyright   Copyright (C) 2023 E4J s.r.l. All Rights Reserved.
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 * @link        https://vikwp.com
 */

namespace E4J\VikRestaurants\Update\Adapters\Update1_11;

// No direct access
defined('ABSPATH') or die('No script kiddies please!');

use E4J\VikRestaurants\Update\UpdateRule;

/**
 * Changes the charset and the collation of certain database columns to support emojis.
 *
 * @since 1.11
 */
class EmojiSupportFixer extends UpdateRule
{
    /**
     * @inheritDoc
     */
    protected function run($parent)
    {
        try
        {
            $db = \JFactory::getDbo();

            // restaurant reservation notes
            $db->setQuery("ALTER TABLE `#__vikrestaurants_reservation` MODIFY `notes` text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL");
            $db->execute();

            // take-away order notes
            $db->setQuery("ALTER TABLE `#__vikrestaurants_takeaway_reservation` MODIFY `notes` text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL");
            $db->execute();

            // restaurant item notes
            $db->setQuery("ALTER TABLE `#__vikrestaurants_res_prod_assoc` MODIFY `notes` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL");
            $db->execute();

            // take-away item notes
            $db->setQuery("ALTER TABLE `#__vikrestaurants_takeaway_res_prod_assoc` MODIFY `notes` varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL");
            $db->execute();

            // reviews comment
            $db->setQuery("ALTER TABLE `#__vikrestaurants_reviews` MODIFY `comment` text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL");
            $db->execute();
        }
        catch (\Exception $error)
        {
            // UTF8MB4 not supported by the current database version
        }

        return true;
    }
}
