/**
 * Upgrades the module to version 2.4.0.
 *
 * Adds admin tab for PS 1.5+.
 * Registers hook `displayBackOfficeHeader`.
 *
 * @param NostoTagging $object
 * @return bool
 */
function upgrade_module_2_4_0($object)
{
    if (_PS_VERSION_ < '1.5') {
        return true;
    }
    return Nosto::helper('nosto_tagging/admin_tab')->install() && $object->registerHook('displayBackOfficeHeader');
}
/**
 * Upgrades the module to version 2.2.0.
 *
 * Registers product add hooks.
 *
 * @param NostoTagging $object
 * @return bool
 */
function upgrade_module_2_2_0($object)
{
    if (_PS_VERSION_ < '1.5') {
        return $object->registerHook('addproduct');
    } else {
        return $object->registerHook('actionObjectAddAfter');
    }
}
/**
 * Upgrades the module to version 1.1.0.
 *
 * Creates 'nostotagging_customer_link' db table.
 * Registers hooks 'actionPaymentConfirmation', 'displayPaymentTop' and 'displayHome'.
 * Sets default value for "inject category and search page recommendations" to 1.
 * Removes unused "NOSTOTAGGING_SERVER_ADDRESS" config variable.
 *
 * @param NostoTagging $object
 * @return bool
 */
function upgrade_module_1_1_0($object)
{
    $create_table = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'nostotagging_customer_link` (
						`id_customer` INT(10) UNSIGNED NOT NULL,
						`id_nosto_customer` VARCHAR(255) NOT NULL,
						`date_add` DATETIME NOT NULL,
						`date_upd` DATETIME NULL,
						PRIMARY KEY (`id_customer`, `id_nosto_customer`)
					) ENGINE ' . _MYSQL_ENGINE_;
    return Db::getInstance()->execute($create_table) && $object->registerHook('actionPaymentConfirmation') && $object->registerHook('displayPaymentTop') && $object->registerHook('displayHome') && Configuration::updateGlobalValue('NOSTOTAGGING_INJECT_SLOTS', 1) && Configuration::deleteByName('NOSTOTAGGING_SERVER_ADDRESS') && Configuration::deleteByName('NOSTOTAGGING_TOP_SELLERS_CMS_ID');
}
/**
 * Upgrades the module to version 2.1.0.
 *
 * Updates the customer link table to link the nosto customer id to the PS id_cart instead of id_customer.
 * Un-registers payment confirmation hooks.
 * Registers order status post update hooks.
 *
 * @param NostoTagging $object
 * @return bool
 */
function upgrade_module_2_1_0($object)
{
    $drop_table = 'DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'nostotagging_customer_link`';
    $create_table = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'nostotagging_customer_link` (
						`id_cart` INT(10) UNSIGNED NOT NULL,
						`id_nosto_customer` VARCHAR(255) NOT NULL,
						`date_add` DATETIME NOT NULL,
						`date_upd` DATETIME NULL,
						PRIMARY KEY (`id_cart`, `id_nosto_customer`)
					) ENGINE ' . _MYSQL_ENGINE_;
    if (_PS_VERSION_ > '1.5') {
        $hooks = $object->registerHook('actionObjectDeleteAfter') && $object->unregisterHook('actionPaymentConfirmation');
    } else {
        $hooks = true;
    }
    // We just drop the table and re-create as it's easier and we don't want the data we loose.
    return Db::getInstance()->execute($drop_table) && Db::getInstance()->execute($create_table) && $object->unregisterHook('paymentConfirm') && $object->registerHook('postUpdateOrderStatus') && $hooks;
}
/**
 * Upgrades the module to version 1.3.0.
 *
 * Purges existing nosto configs.
 * Removes unused config variables.
 * Registers new hooks.
 * Un-register left/right column hooks.
 *
 * @param NostoTagging $object
 * @return bool
 */
function upgrade_module_1_3_0($object)
{
    // Purge the nosto configs the plugin have created so far and reload the config.
    $config_table = _DB_PREFIX_ . 'configuration';
    $config_lang_table = $config_table . '_lang';
    Db::getInstance()->execute('
			DELETE `' . $config_lang_table . '` FROM `' . $config_lang_table . '`
			LEFT JOIN `' . $config_table . '`
			ON `' . $config_lang_table . '`.`id_configuration` = `' . $config_table . '`.`id_configuration`
			WHERE `' . $config_table . '`.`name` LIKE "NOSTOTAGGING_%"');
    Db::getInstance()->execute('
			DELETE FROM `' . $config_table . '`
			WHERE `' . $config_table . '`.`name` LIKE "NOSTOTAGGING_%"');
    Configuration::loadConfiguration();
    // Backward compatibility
    if (_PS_VERSION_ < '1.5') {
        $object->registerHook('header');
        $object->registerHook('top');
        $object->registerHook('footer');
        $object->registerHook('productfooter');
        $object->registerHook('shoppingCart');
        $object->registerHook('orderConfirmation');
        $object->registerHook('paymentConfirm');
        $object->registerHook('paymentTop');
        $object->registerHook('home');
        $object->registerHook('updateproduct');
        $object->registerHook('deleteproduct');
        $object->registerHook('updateQuantity');
    } else {
        $object->registerHook('actionObjectUpdateAfter');
    }
    $object->unregisterHook('displayLeftColumn');
    $object->unregisterHook('displayRightColumn');
    return true;
}