<div>HELLO ODM</div>
<?php

require_once __DIR__ . "/vendor/autoload.php";

$workspace = 'default';
$user = 'jackalope';
$pass = '1234test';

$params = array(
    'driver'    => 'pdo_mysql', // or pdo_pgsql
    'host'      => 'localhost',
    'user'      => $user,
    'password'  => $pass,
    'dbname'    => 'jackalope',
);

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Bootstrap Doctrine DBAL
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$dbConn = \Doctrine\DBAL\DriverManager::getConnection($params);

$repository = \Jackalope\RepositoryFactoryDoctrineDBAL::getRepository(
    array('jackalope.doctrine_dbal_connection' => $dbConn)
);
// dummy credentials to comply with the API
$credentials = new \PHPCR\SimpleCredentials(null, null);
$session = $repository->login($credentials, $workspace);


// initialize doctrine annonations
use Doctrine\Common\Annotations\AnnotationRegistry;

// for translations 
use Doctrine\ODM\PHPCR\Translation\LocaleChooser\LocaleChooser;

$file = 'vendor/autoload.php';
if (file_exists($file)) {
    $autoload = require_once $file;
} else {
    throw new RuntimeException('Install dependencies to run test suite.');
}
AnnotationRegistry::registerFile(__DIR__.'/vendor/doctrine/phpcr-odm/lib/Doctrine/ODM/PHPCR/Mapping/Annotations/DoctrineAnnotations.php');

$reader = new \Doctrine\Common\Annotations\AnnotationReader();
$driver = new \Doctrine\ODM\PHPCR\Mapping\Driver\AnnotationDriver($reader, array('src/Odmtest/Model'));


/* prepare the doctrine configuration */
$config = new \Doctrine\ODM\PHPCR\Configuration();
$config->setMetadataDriverImpl($driver);
$documentManager = \Doctrine\ODM\PHPCR\DocumentManager::create($session, $config);

$localePrefs = array(
    'en'  => array('en', 'fr'),
    'fr'    => array('en', 'de'),
    'de'   => array('en', 'it'),
);
$documentManager->setLocaleChooserStrategy(new LocaleChooser($localePrefs, 'en'));
$rootDocument = $documentManager->find(Null,'/');

//use \src\Odmtest\Model\Document;
require_once 'src/Odmtest/Model/Document.php';

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// END Bootstrap Doctrine DBAL
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// CREATING DOCUMENTS

$langdoc = new MyDocument();
$langdoc->setParent($rootDocument);
$langdoc->setName('langdoc');
$langdoc->setTitle('My first document EN');
$langdoc->setTopic('topic in EN');
$langdoc->setContent('The document content EN');
$documentManager->persist($langdoc);
$documentManager->flush();

$documentManager->bindTranslation($langdoc, 'de');
$langdoc->setTitle( 'Das erste Dokument in deutsch DE');
$langdoc->setTopic( 'Der Topic in deutsch DE');
$langdoc->setContent( 'Der erste Content in deutsch DE');
$documentManager->persist($langdoc);
$documentManager->flush();

$childDocument = new MyDocument();
$childDocument->setParent($langdoc);
$childDocument->setName('child_langdoc');
$childDocument->setTitle('My child document DE?');
$childDocument->setLocale('de');
$childDocument->setContent('The child document content DE?');
$documentManager->persist($childDocument);
$documentManager->flush();

// Get the document in French
$langdoc = $documentManager->findTranslation(null, '/langdoc', 'fr');
$langdoc->setLocale('fr');
$documentManager->bindTranslation($langdoc, 'fr');
$langdoc->setTitle('nouveau');
$langdoc->setContent('nouveau contendddde FR');
$langdoc->setTopic('Un sujet intéressant');


$documentManager->persist($langdoc);
$documentManager->flush(); 


// READ TRANSLATED DOCUMENTS

$doc = $documentManager->find(NULL, '/langdoc');
echo 'LOCALES for DOCUMENT: ' . implode(' -- ',$documentManager->getLocalesFor($doc)) . '<br />';
$doc_locales = $documentManager->getLocalesFor($doc);

foreach($doc_locales as $locale)
{
    echo 'CURRENT LOCALE: ' . $locale . '<br />';
    $translatedDocument = $documentManager->findTranslation(NULL, '/langdoc', $locale, FALSE);
    echo 'LOCALE: ' . $translatedDocument->getLocale() . '<br />';
    echo 'TITLE: ' . $translatedDocument->getTitle() . '<br />';
    echo 'TOPIC: ' . $translatedDocument->getTopic() . '<br />';
    echo 'CONTENT: ' . $translatedDocument->getContent() . '<br />';
    echo '<br />';
}

$tmp = $documentManager->findTranslation(NULL, '/langdoc/child_langdoc', 'fr');
echo 'CHILDDOC ' . $tmp->getName() . ' CHILD TITLE:  ' . $tmp->getTitle() . '<br />'; 

$tmp->setTitle('My child document FR?');
$tmp->setLocale('fr');
$tmp->setContent('The child document content FR?');
$documentManager->persist($tmp);
$documentManager->flush(); 

$tmp2 = $documentManager->findTranslation(NULL, '/langdoc/child_langdoc', 'fr');
echo 'CHILDDOC: ' . $tmp2->getName() . ' CHILD TITLE:  ' . $tmp2->getTitle() . '<br />'; 

$tmp3 = $documentManager->findTranslation(\NULL, '/langdoc/child_langdoc', 'de');
echo 'CHILDDOC: ' . $tmp3->getName() . ' CHILD TITLE:  ' . $tmp3->getTitle() . '<br />'; 

// HERE I WOULD EXPECT THE OUTPUT 
//
// CHILDDOC: child_langdoc  CHILD TITLE: My child document FR?  
// CHILDDOC: child_langdoc  CHILD TITLE: My child document DE?
//
// BUT I GET:
// CHILDDOC: child_langdoc  CHILD TITLE: My child document DE?
// CHILDDOC: child_langdoc  CHILD TITLE: My child document DE?
//
// THE FRENCH TRANSLATION WILL NOT BE SAVED... I HAVE NO CLUE WHY