<?php
/* For licensing terms, see /license.txt */

$cidReset = true;

require_once __DIR__.'/../inc/global.inc.php';

// Access restrictions
api_protect_admin_script(true);

$em = Database::getManager();
$connection = Database::getManager()->getConnection();
$sm = $connection->createSchemaManager();
$fromSchema = $sm->createSchema();

$tool = new \Doctrine\ORM\Tools\SchemaTool($em);
$metadatas = $em->getMetadataFactory()->getAllMetadata();
$toSchema = $tool->getSchemaFromMetadata($metadatas);

$schemaDiff = $sm->createComparator()->compareSchemas($fromSchema, $toSchema);

$sqlList = $schemaDiff->toSaveSql($connection->getDatabasePlatform());
$content = '';
if (!empty($sqlList)) {
    $form = new FormValidator('update');
    $form->addHtml(
        Display::return_message('If you click in update database. The SQL queries below will be executed in the database. Consider creating a DB dump before doing this.')
    );
    $form->addButtonSave(get_lang('Update database'));
    $content = $form->returnForm();

    if ($form->validate()) {
        error_log('---- Sync DB with schema ---');
        foreach ($sqlList as $sql) {
            Database::query($sql);
            error_log($sql);
        }
        error_log('---- End sync ---');
        Display::addFlash(
            Display::return_message(
                get_lang(count($sqlList).' queries were executed. Check your error.log'),
                'success'
            )
        );
        header('Location: '.api_get_self());
        exit;
    }

    $content .= '<pre>';
    foreach ($sqlList as $sql) {
        $content .= ($sql).';  <br />';
    }
    $content .= '</pre>';
} else {
    Display::addFlash(Display::return_message(get_lang('Nothing left to update.')));
}

Display::display_header(get_lang('Synchronize the database with the schema'));
echo $content;
Display::display_footer();
